From 117a69fd49a24b8d2d56b4368569a71e90972ec6 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Tue, 16 May 2017 01:22:57 +0300 Subject: jabber: omemo: fixed decryption fixed decryption logic bug appeared due to my misunderstanding of omemo sessions concept --- protocols/JabberG/src/jabber_omemo.cpp | 143 ++++++++++++++++++++++----------- 1 file changed, 97 insertions(+), 46 deletions(-) (limited to 'protocols/JabberG') diff --git a/protocols/JabberG/src/jabber_omemo.cpp b/protocols/JabberG/src/jabber_omemo.cpp index 07f280bc1b..5cd8326de8 100755 --- a/protocols/JabberG/src/jabber_omemo.cpp +++ b/protocols/JabberG/src/jabber_omemo.cpp @@ -1585,64 +1585,115 @@ void CJabberProto::OmemoHandleMessage(HXML node, LPCTSTR jid, time_t msgTime) mir_free(iv_buf); } signal_buffer *decrypted_key = NULL; - pre_key_signal_message *pm; - //TODO: cleanup before return on error - { - int ret = pre_key_signal_message_deserialize(&pm, encrypted_key, encrypted_key_len, omemo::global_context); - switch (ret) + bool decrypted = false; + { //try to decrypt as pre_key_signal_message + + + pre_key_signal_message *pm = nullptr; + bool deserialized = false; + //TODO: cleanup before return on error { - case SG_SUCCESS: - break; - case SG_ERR_INVALID_PROTO_BUF: - debugLogA("Jabber OMEMO: error: pre_key_signal_message_deserialize failed SG_ERR_INVALID_PROTO_BUF"); - return; - break; - default: - debugLogA("Jabber OMEMO: error: pre_key_signal_message_deserialize failed with unknown error"); - return; - break; + int ret = pre_key_signal_message_deserialize(&pm, encrypted_key, encrypted_key_len, omemo::global_context); + switch (ret) + { + case SG_SUCCESS: + deserialized = true; + break; + case SG_ERR_INVALID_PROTO_BUF: + debugLogA("Jabber OMEMO: error: pre_key_signal_message_deserialize failed SG_ERR_INVALID_PROTO_BUF"); +// return; + break; + default: + debugLogA("Jabber OMEMO: error: pre_key_signal_message_deserialize failed with unknown error"); +// return; + break; + } + } + if (deserialized && pm) + { + int ret = session_cipher_decrypt_pre_key_signal_message((*(std::map >*)m_omemo.sessions_internal)[hContact][sender_dev_id_int].cipher, pm, 0, &decrypted_key); + switch (ret) + { + case SG_SUCCESS: + decrypted = true; + break; + case SG_ERR_INVALID_MESSAGE: + debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed SG_ERR_INVALID_MESSAGE"); +// return; + break; + case SG_ERR_DUPLICATE_MESSAGE: + debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed SG_ERR_DUPLICATE_MESSAGE"); +// return; + break; + case SG_ERR_LEGACY_MESSAGE: + debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed SG_ERR_LEGACY_MESSAGE"); +// return; + break; + case SG_ERR_INVALID_KEY_ID: + debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed SG_ERR_INVALID_KEY_ID"); +// return; + break; + case SG_ERR_INVALID_KEY: + debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed SG_ERR_INVALID_KEY"); +// return; + break; + case SG_ERR_UNTRUSTED_IDENTITY: + debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed SG_ERR_UNTRUSTED_IDENTITY"); +// return; + break; + default: + debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed with unknown error"); +// return; + break; + } } } - if (pm) - { - int ret = session_cipher_decrypt_pre_key_signal_message((*(std::map >*)m_omemo.sessions_internal)[hContact][sender_dev_id_int].cipher, pm, 0, &decrypted_key); + if (!decrypted) + { //try to decrypt as signal message + signal_message *sm = nullptr; + + bool deserialized = false; + int ret = signal_message_deserialize(&sm, encrypted_key, encrypted_key_len, omemo::global_context); switch (ret) { case SG_SUCCESS: - break; - case SG_ERR_INVALID_MESSAGE: - debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed SG_ERR_INVALID_MESSAGE"); - return; - break; - case SG_ERR_DUPLICATE_MESSAGE: - debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed SG_ERR_DUPLICATE_MESSAGE"); - return; - break; - case SG_ERR_LEGACY_MESSAGE: - debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed SG_ERR_LEGACY_MESSAGE"); - return; - break; - case SG_ERR_INVALID_KEY_ID: - debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed SG_ERR_INVALID_KEY_ID"); - return; - break; - case SG_ERR_INVALID_KEY: - debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed SG_ERR_INVALID_KEY"); - return; - break; - case SG_ERR_UNTRUSTED_IDENTITY: - debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed SG_ERR_UNTRUSTED_IDENTITY"); - return; + deserialized = true; break; default: - debugLogA("Jabber OMEMO: error: session_cipher_decrypt_pre_key_signal_message failed with unknown error"); - return; + debugLogA("Jabber OMEMO: error: signal_message_deserialize failed with unknown error"); break; } + if (deserialized && sm) + { + ret = session_cipher_decrypt_signal_message((*(std::map >*)m_omemo.sessions_internal)[hContact][sender_dev_id_int].cipher, sm, 0, &decrypted_key); + switch (ret) + { + case SG_SUCCESS: + decrypted = true; + break; + case SG_ERR_INVALID_MESSAGE: + debugLogA("Jabber OMEMO: error: session_cipher_decrypt_signal_message failed SG_ERR_INVALID_MESSAGE"); + break; + case SG_ERR_DUPLICATE_MESSAGE: + debugLogA("Jabber OMEMO: error: session_cipher_decrypt_signal_message failed SG_ERR_DUPLICATE_MESSAGE"); + break; + case SG_ERR_LEGACY_MESSAGE: + debugLogA("Jabber OMEMO: error: session_cipher_decrypt_signal_message failed SG_ERR_LEGACY_MESSAGE"); + break; + case SG_ERR_NO_SESSION: + debugLogA("Jabber OMEMO: error: session_cipher_decrypt_signal_message failed SG_ERR_NO_SESSION"); + break; + default: + debugLogA("Jabber OMEMO: error: session_cipher_decrypt_signal_message failed with unknown error"); + break; + + } + } } - else + if(!decrypted) { - debugLogA("Jabber OMEMO: error: pre_key_signal_message_deserialize failed"); + debugLogA("Jabber OMEMO: error: failed to decrypt incomming message"); + return; //TODO: cleanup } char *out = nullptr; { -- cgit v1.2.3