summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--protocols/JabberG/jabber.vcxproj4
-rw-r--r--protocols/JabberG/jabber.vcxproj.filters6
-rw-r--r--protocols/JabberG/src/jabber_omemo.cpp90
-rw-r--r--protocols/JabberG/src/stdafx.h2
-rw-r--r--protocols/WhatsAppWeb/WhatsAppWeb.vcxproj4
-rw-r--r--protocols/WhatsAppWeb/WhatsAppWeb.vcxproj.filters6
-rw-r--r--protocols/WhatsAppWeb/src/signal.cpp25
-rw-r--r--protocols/WhatsAppWeb/src/stdafx.h33
-rw-r--r--utils/mir_signal.cpp40
-rw-r--r--utils/mir_signal.h56
10 files changed, 151 insertions, 115 deletions
diff --git a/protocols/JabberG/jabber.vcxproj b/protocols/JabberG/jabber.vcxproj
index cffe651bac..ec58b9f728 100644
--- a/protocols/JabberG/jabber.vcxproj
+++ b/protocols/JabberG/jabber.vcxproj
@@ -31,6 +31,9 @@
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
+ <ClCompile Include="..\..\utils\mir_signal.cpp">
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ </ClCompile>
<ClCompile Include="src\jabber.cpp" />
<ClCompile Include="src\jabber_adhoc.cpp" />
<ClCompile Include="src\jabber_agent.cpp" />
@@ -87,6 +90,7 @@
<ClCompile Include="src\stdafx.cxx">
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>
+ <ClInclude Include="..\..\utils\mir_signal.h" />
<ClInclude Include="src\jabber_byte.h" />
<ClInclude Include="src\jabber_caps.h" />
<ClInclude Include="src\jabber_disco.h" />
diff --git a/protocols/JabberG/jabber.vcxproj.filters b/protocols/JabberG/jabber.vcxproj.filters
index 7894068683..17d86404b3 100644
--- a/protocols/JabberG/jabber.vcxproj.filters
+++ b/protocols/JabberG/jabber.vcxproj.filters
@@ -164,6 +164,9 @@
<ClCompile Include="src\jabber_voip.cpp">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="..\..\utils\mir_signal.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\jabber_byte.h">
@@ -235,6 +238,9 @@
<ClInclude Include="src\version.h">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="..\..\utils\mir_signal.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="res\jabber.rc">
diff --git a/protocols/JabberG/src/jabber_omemo.cpp b/protocols/JabberG/src/jabber_omemo.cpp
index 1d3fb9e1ea..ac80f8dd5b 100644
--- a/protocols/JabberG/src/jabber_omemo.cpp
+++ b/protocols/JabberG/src/jabber_omemo.cpp
@@ -29,13 +29,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "stdafx.h"
#include "jabber_omemo.h"
-#include "..\..\libs\libsignal\src\signal_protocol.h"
-#include "..\..\libs\libsignal\src\signal_protocol_types.h"
-#include "..\..\libs\libsignal\src\key_helper.h"
-#include "..\..\libs\libsignal\src\session_builder.h"
-#include "..\..\libs\libsignal\src\session_cipher.h"
-#include "..\..\libs\libsignal\src\protocol.h"
-
namespace omemo
{
int random_func(uint8_t *data, size_t len, void * /*user_data*/)
@@ -500,21 +493,17 @@ complete:
// generate and save device key
ec_public_key *public_key = ratchet_identity_key_pair_get_public(new_dev->device_key);
- signal_buffer *key_buf;
- ec_public_key_serialize(&key_buf, public_key);
{
- ptrA key(mir_base64_encode(signal_buffer_data(key_buf), signal_buffer_len(key_buf)));
- ptrA fingerprint((char*)mir_alloc((signal_buffer_len(key_buf) * 2) + 1));
- bin2hex(signal_buffer_data(key_buf), signal_buffer_len(key_buf), fingerprint);
+ SignalBuffer buf(public_key);
+ ptrA key(mir_base64_encode(buf.data(), buf.len()));
+ ptrA fingerprint((char*)mir_alloc((buf.len() * 2) + 1));
+ bin2hex(buf.data(), buf.len(), fingerprint);
proto->setString("OmemoFingerprintOwn", fingerprint);
proto->setString("OmemoDevicePublicKey", key);
}
- signal_buffer_free(key_buf);
- ec_private_key *private_key = ratchet_identity_key_pair_get_private(new_dev->device_key);
- ec_private_key_serialize(&key_buf, private_key);
- proto->setString("OmemoDevicePrivateKey", ptrA(mir_base64_encode(signal_buffer_data(key_buf), signal_buffer_len(key_buf))));
- signal_buffer_free(key_buf);
+ proto->setString("OmemoDevicePrivateKey",
+ SignalBuffer(ratchet_identity_key_pair_get_private(new_dev->device_key)).toBase64());
// generate and save signed pre key
session_signed_pre_key *signed_pre_key;
@@ -523,19 +512,15 @@ complete:
signal_protocol_key_helper_generate_signed_pre_key(&signed_pre_key, new_dev->device_key, signed_pre_key_id, time(0), global_context);
SIGNAL_UNREF(new_dev->device_key);
- signal_buffer *serialized_signed_pre_key;
- session_signed_pre_key_serialize(&serialized_signed_pre_key, signed_pre_key);
+ SignalBuffer buf(signed_pre_key);
CMStringA szSetting(FORMAT, "%s%u%d", "OmemoSignalSignedPreKey_", proto->m_omemo.GetOwnDeviceId(), signed_pre_key_id);
- db_set_blob(0, proto->m_szModuleName, szSetting, signal_buffer_data(serialized_signed_pre_key), (unsigned int)signal_buffer_len(serialized_signed_pre_key));
+ db_set_blob(0, proto->m_szModuleName, szSetting, buf.data(), buf.len());
}
// TODO: store signed_pre_key for libsignal data backend too
// TODO: dynamic signed pre_key id and setting name ?
ec_key_pair *signed_pre_key_pair = session_signed_pre_key_get_key_pair(signed_pre_key);
- public_key = ec_key_pair_get_public(signed_pre_key_pair);
- ec_public_key_serialize(&key_buf, public_key);
- proto->setString("OmemoSignedPreKeyPublic", ptrA(mir_base64_encode(signal_buffer_data(key_buf), signal_buffer_len(key_buf))));
- signal_buffer_free(key_buf);
+ proto->setString("OmemoSignedPreKeyPublic", SignalBuffer(ec_key_pair_get_public(signed_pre_key_pair)).toBase64());
ptrA signature(mir_base64_encode(session_signed_pre_key_get_signature(signed_pre_key), session_signed_pre_key_get_signature_len(signed_pre_key)));
proto->setString("OmemoSignedPreKeySignature", signature);
@@ -547,21 +532,14 @@ complete:
for (auto *it = keys_root; it; it = signal_protocol_key_helper_key_list_next(it)) {
session_pre_key *pre_key = signal_protocol_key_helper_key_list_element(it);
uint32_t pre_key_id = session_pre_key_get_id(pre_key);
- {
- signal_buffer *serialized_pre_key;
- session_pre_key_serialize(&serialized_pre_key, pre_key);
- szSetting.Format("%s%u%d", "OmemoSignalPreKey_", GetOwnDeviceId(), pre_key_id);
- db_set_blob(0, proto->m_szModuleName, szSetting, signal_buffer_data(serialized_pre_key), (unsigned int)signal_buffer_len(serialized_pre_key));
- SIGNAL_UNREF(serialized_pre_key);
- }
- ec_key_pair *pre_key_pair = session_pre_key_get_key_pair(pre_key);
- public_key = ec_key_pair_get_public(pre_key_pair);
- ec_public_key_serialize(&key_buf, public_key);
+ SignalBuffer buf(pre_key);
+ szSetting.Format("%s%u%d", "OmemoSignalPreKey_", GetOwnDeviceId(), pre_key_id);
+ db_set_blob(0, proto->m_szModuleName, szSetting, buf.data(), buf.len());
+ ec_key_pair *pre_key_pair = session_pre_key_get_key_pair(pre_key);
szSetting.Format("OmemoPreKey%uPublic", pre_key_id);
- proto->setString(szSetting, ptrA(mir_base64_encode(signal_buffer_data(key_buf), signal_buffer_len(key_buf))));
- signal_buffer_free(key_buf);
+ proto->setString(szSetting, SignalBuffer(ec_key_pair_get_public(pre_key_pair)).toBase64());
}
signal_protocol_key_helper_key_list_free(keys_root);
}
@@ -848,16 +826,8 @@ complete:
session_pre_key_deserialize(&prekey, record, record_len, global_context); //TODO: handle error
if (prekey) {
ec_key_pair *pre_key_pair = session_pre_key_get_key_pair(prekey);
- signal_buffer *key_buf = nullptr;
- ec_public_key *public_key = ec_key_pair_get_public(pre_key_pair);
- ec_public_key_serialize(&key_buf, public_key);
- SIGNAL_UNREF(public_key);
-
- char *key = mir_base64_encode(signal_buffer_data(key_buf), signal_buffer_len(key_buf));
szSetting.Format("OmemoPreKey%uPublic", pre_key_id);
- data->proto->setString(szSetting, key);
- mir_free(key);
- signal_buffer_free(key_buf);
+ data->proto->setString(szSetting, SignalBuffer(ec_key_pair_get_public(pre_key_pair)).toBase64());
}
}
@@ -1252,10 +1222,9 @@ complete:
bool fp_trusted = false;
{
// check fingerprint
- signal_buffer *key_buf2;
- ec_public_key_serialize(&key_buf2, identity_key_p);
- char *fingerprint = (char*)mir_alloc((signal_buffer_len(key_buf2) * 2) + 1);
- bin2hex(signal_buffer_data(key_buf2), signal_buffer_len(key_buf2), fingerprint);
+ SignalBuffer buf(identity_key_p);
+ ptrA fingerprint((char *)mir_alloc((buf.len() * 2) + 1));
+ bin2hex(buf.data(), buf.len(), fingerprint);
CMStringA szSetting(FORMAT, "%s%s", "OmemoFingerprintTrusted_", fingerprint);
char val = proto->getByte(hContact, szSetting, -1);
@@ -1271,7 +1240,6 @@ complete:
else if (ret == IDNO)
proto->setByte(hContact, szSetting, 0);
}
- mir_free(fingerprint);
}
if (!fp_trusted) {
@@ -1327,26 +1295,14 @@ complete:
for (auto *it = keys_root; it; it = signal_protocol_key_helper_key_list_next(it)) {
session_pre_key *pre_key = signal_protocol_key_helper_key_list_element(it);
uint32_t pre_key_id = session_pre_key_get_id(pre_key);
- {
- signal_buffer *serialized_pre_key;
- session_pre_key_serialize(&serialized_pre_key, pre_key);
- szSetting.Format("%s%u%d", "OmemoSignalPreKey_", proto->m_omemo.GetOwnDeviceId(), pre_key_id);
- db_set_blob(0, proto->m_szModuleName, szSetting, signal_buffer_data(serialized_pre_key), (unsigned int)signal_buffer_len(serialized_pre_key));
- SIGNAL_UNREF(serialized_pre_key);
- }
- signal_buffer *key_buf;
+ SignalBuffer buf(pre_key);
+ szSetting.Format("%s%u%d", "OmemoSignalPreKey_", proto->m_omemo.GetOwnDeviceId(), pre_key_id);
+ db_set_blob(0, proto->m_szModuleName, szSetting, buf.data(), buf.len());
+
ec_key_pair *pre_key_pair = session_pre_key_get_key_pair(pre_key);
- ec_public_key *public_key = ec_key_pair_get_public(pre_key_pair);
- ec_public_key_serialize(&key_buf, public_key);
- SIGNAL_UNREF(public_key);
-
- char *key = mir_base64_encode(signal_buffer_data(key_buf), signal_buffer_len(key_buf));
szSetting.Format("OmemoPreKey%uPublic", pre_key_id);
- proto->setString(szSetting, key);
-
- mir_free(key);
- signal_buffer_free(key_buf);
+ proto->setString(szSetting, SignalBuffer(ec_key_pair_get_public(pre_key_pair)).toBase64());
}
signal_protocol_key_helper_key_list_free(keys_root);
diff --git a/protocols/JabberG/src/stdafx.h b/protocols/JabberG/src/stdafx.h
index 985dbefefb..da81c661c9 100644
--- a/protocols/JabberG/src/stdafx.h
+++ b/protocols/JabberG/src/stdafx.h
@@ -107,6 +107,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <openssl/rand.h>
#include <openssl/sha.h>
+#include "../../utils/mir_signal.h"
+
#include "../../libs/zlib/src/zlib.h"
#include "resource.h"
diff --git a/protocols/WhatsAppWeb/WhatsAppWeb.vcxproj b/protocols/WhatsAppWeb/WhatsAppWeb.vcxproj
index 50ae4808dd..d39592a2f4 100644
--- a/protocols/WhatsAppWeb/WhatsAppWeb.vcxproj
+++ b/protocols/WhatsAppWeb/WhatsAppWeb.vcxproj
@@ -58,6 +58,9 @@
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
+ <ClCompile Include="..\..\utils\mir_signal.cpp">
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ </ClCompile>
<ClCompile Include="src\avatars.cpp" />
<ClCompile Include="src\chats.cpp" />
<ClCompile Include="src\iq.cpp" />
@@ -76,6 +79,7 @@
</ClCompile>
<ClCompile Include="src\utils.cpp" />
<ClCompile Include="src\wanode.cpp" />
+ <ClInclude Include="..\..\utils\mir_signal.h" />
<ClInclude Include="src\db.h" />
<ClInclude Include="src\dicts.h" />
<ClInclude Include="src\proto.h" />
diff --git a/protocols/WhatsAppWeb/WhatsAppWeb.vcxproj.filters b/protocols/WhatsAppWeb/WhatsAppWeb.vcxproj.filters
index 31a86f3851..dca4011eb0 100644
--- a/protocols/WhatsAppWeb/WhatsAppWeb.vcxproj.filters
+++ b/protocols/WhatsAppWeb/WhatsAppWeb.vcxproj.filters
@@ -44,6 +44,9 @@
<ClCompile Include="src\signal.cpp">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="..\..\utils\mir_signal.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\db.h">
@@ -67,6 +70,9 @@
<ClInclude Include="src\dicts.h">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="..\..\utils\mir_signal.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="res\version.rc">
diff --git a/protocols/WhatsAppWeb/src/signal.cpp b/protocols/WhatsAppWeb/src/signal.cpp
index 467572737a..92973ac629 100644
--- a/protocols/WhatsAppWeb/src/signal.cpp
+++ b/protocols/WhatsAppWeb/src/signal.cpp
@@ -207,14 +207,10 @@ static int store_pre_key(uint32_t pre_key_id, uint8_t *record, size_t record_len
session_pre_key_deserialize(&prekey, record, record_len, pStore->CTX()); //TODO: handle error
if (prekey) {
ec_key_pair *pre_key_pair = session_pre_key_get_key_pair(prekey);
- signal_buffer *key_buf = nullptr;
- ec_public_key *public_key = ec_key_pair_get_public(pre_key_pair);
- ec_public_key_serialize(&key_buf, public_key);
- SIGNAL_UNREF(public_key);
+ SignalBuffer key_buf(ec_key_pair_get_public(pre_key_pair));
szSetting.Format("PreKey%uPublic", pre_key_id);
- db_set_blob(0, pStore->pProto->m_szModuleName, szSetting, signal_buffer_data(key_buf), (int)signal_buffer_len(key_buf));
- signal_buffer_free(key_buf);
+ db_set_blob(0, pStore->pProto->m_szModuleName, szSetting, key_buf.data(), key_buf.len());
}
return 0;
@@ -330,10 +326,8 @@ void MSignalStore::init()
signal_protocol_key_helper_generate_signed_pre_key(&signed_pre_key, keyPair, 1, time(0), m_pContext);
SIGNAL_UNREF(keyPair);
- signal_buffer *my_prekey;
- session_signed_pre_key_serialize(&my_prekey, signed_pre_key);
- db_set_blob(0, pProto->m_szModuleName, DBKEY_PREKEY, my_prekey->data, (int)my_prekey->len);
- SIGNAL_UNREF(my_prekey);
+ SignalBuffer prekeyBuf(signed_pre_key);
+ db_set_blob(0, pProto->m_szModuleName, DBKEY_PREKEY, prekeyBuf.data(), prekeyBuf.len());
// generate and save pre keys set
CMStringA szSetting;
@@ -342,13 +336,10 @@ void MSignalStore::init()
for (auto *it = keys_root; it; it = signal_protocol_key_helper_key_list_next(it)) {
session_pre_key *pre_key = signal_protocol_key_helper_key_list_element(it);
uint32_t pre_key_id = session_pre_key_get_id(pre_key);
- {
- signal_buffer *serialized_pre_key;
- session_pre_key_serialize(&serialized_pre_key, pre_key);
- szSetting.Format("PreKey%d", pre_key_id);
- db_set_blob(0, pProto->m_szModuleName, szSetting, signal_buffer_data(serialized_pre_key), (unsigned int)signal_buffer_len(serialized_pre_key));
- SIGNAL_UNREF(serialized_pre_key);
- }
+
+ SignalBuffer buf(pre_key);
+ szSetting.Format("PreKey%d", pre_key_id);
+ db_set_blob(0, pProto->m_szModuleName, szSetting, buf.data(), buf.len());
ec_key_pair *pre_key_pair = session_pre_key_get_key_pair(pre_key);
pPubKey = ec_key_pair_get_public(pre_key_pair);
diff --git a/protocols/WhatsAppWeb/src/stdafx.h b/protocols/WhatsAppWeb/src/stdafx.h
index bbcda15825..5c6698d56e 100644
--- a/protocols/WhatsAppWeb/src/stdafx.h
+++ b/protocols/WhatsAppWeb/src/stdafx.h
@@ -51,18 +51,11 @@ Copyright © 2019-22 George Hazan
#include <openssl/kdf.h>
#include "../../libs/libqrencode/src/qrencode.h"
-
-#include "../../libs/libsignal/src/curve.h"
-#include "../../libs/libsignal/src/hkdf.h"
-#include "../../libs/libsignal/src/key_helper.h"
-#include "../../libs/libsignal/src/protocol.h"
-#include "../../libs/libsignal/src/session_cipher.h"
-#include "../../libs/libsignal/src/signal_protocol.h"
-
#include "../../libs/libsodium/src/include/sodium.h"
-
#include "../../libs/zlib/src/zlib.h"
+#include "../../utils/mir_signal.h"
+
/////////////////////////////////////////////////////////////////////////////////////////
// to obtain protobuf library do the following
// - install vcpkg (https://github.com/microsoft/vcpkg);
@@ -79,28 +72,6 @@ using namespace google::protobuf;
/////////////////////////////////////////////////////////////////////////////////////////
-struct signal_buffer
-{
- size_t len;
- uint8_t data[];
-};
-
-struct signal_type_base
-{
- unsigned int ref_count = 0;
- void (*destroy)(signal_type_base *instance) = 0;
-};
-
-struct ec_public_key : public signal_type_base
-{
- uint8_t data[32];
-};
-
-struct ec_private_key : public signal_type_base
-{
- uint8_t data[32];
-};
-
#include "db.h"
#include "utils.h"
#include "proto.h"
diff --git a/utils/mir_signal.cpp b/utils/mir_signal.cpp
new file mode 100644
index 0000000000..afca4f4188
--- /dev/null
+++ b/utils/mir_signal.cpp
@@ -0,0 +1,40 @@
+#include <Windows.h>
+
+#include <m_string.h>
+
+#include "mir_signal.h"
+
+SignalBuffer::SignalBuffer(const MBinBuffer &buf)
+{
+ m_buf = signal_buffer_create(buf.data(), buf.length());
+}
+
+SignalBuffer::SignalBuffer(const ec_public_key *key)
+{
+ ec_public_key_serialize(&m_buf, key);
+}
+
+SignalBuffer::SignalBuffer(const ec_private_key *key)
+{
+ ec_private_key_serialize(&m_buf, key);
+}
+
+SignalBuffer::SignalBuffer(const session_pre_key *pre_key)
+{
+ session_pre_key_serialize(&m_buf, pre_key);
+}
+
+SignalBuffer::SignalBuffer(const session_signed_pre_key *pre_key)
+{
+ session_signed_pre_key_serialize(&m_buf, pre_key);
+}
+
+SignalBuffer::~SignalBuffer()
+{
+ signal_buffer_free(m_buf);
+}
+
+CMStringA SignalBuffer::toBase64() const
+{
+ return ptrA(mir_base64_encode(m_buf->data, m_buf->len)).get();
+} \ No newline at end of file
diff --git a/utils/mir_signal.h b/utils/mir_signal.h
new file mode 100644
index 0000000000..ef443374ff
--- /dev/null
+++ b/utils/mir_signal.h
@@ -0,0 +1,56 @@
+#ifndef __MIR_SIGNAL_H__
+#define __MIR_SIGNAL_H__
+
+#include <m_system.h>
+#include <m_utils.h>
+
+#include "../libs/libsignal/src/curve.h"
+#include "../libs/libsignal/src/hkdf.h"
+#include "../libs/libsignal/src/key_helper.h"
+#include "../libs/libsignal/src/protocol.h"
+#include "../libs/libsignal/src/session_builder.h"
+#include "../libs/libsignal/src/session_cipher.h"
+#include "../libs/libsignal/src/signal_protocol.h"
+#include "../libs/libsignal/src/signal_protocol_types.h"
+
+struct signal_buffer
+{
+ size_t len;
+ uint8_t data[1];
+};
+
+struct signal_type_base
+{
+ unsigned int ref_count = 0;
+ void (*destroy)(signal_type_base *instance) = 0;
+};
+
+struct ec_public_key : public signal_type_base
+{
+ uint8_t data[32];
+};
+
+struct ec_private_key : public signal_type_base
+{
+ uint8_t data[32];
+};
+
+class SignalBuffer
+{
+ signal_buffer *m_buf;
+
+public:
+ SignalBuffer(const class MBinBuffer &buf);
+ SignalBuffer(const ec_public_key *key);
+ SignalBuffer(const ec_private_key *key);
+ SignalBuffer(const session_pre_key *pre_key);
+ SignalBuffer(const session_signed_pre_key *pre_key);
+ ~SignalBuffer();
+
+ __forceinline uint8_t* data() const { return m_buf->data; }
+ __forceinline unsigned len() const { return unsigned(m_buf->len); }
+
+ CMStringA toBase64() const;
+};
+
+#endif // __MIR_SMILEYS_H__