diff --git a/native/cpp/CommonCpp/CryptoTools/CryptoModule.h b/native/cpp/CommonCpp/CryptoTools/CryptoModule.h --- a/native/cpp/CommonCpp/CryptoTools/CryptoModule.h +++ b/native/cpp/CommonCpp/CryptoTools/CryptoModule.h @@ -71,7 +71,7 @@ EncryptedData encrypt(const std::string &targetUserId, const std::string &content); std::string - decrypt(const std::string &targetUserId, EncryptedData encryptedData); + decrypt(const std::string &targetUserId, EncryptedData &encryptedData); std::string signMessage(const std::string &message); static void verifySignature( diff --git a/native/cpp/CommonCpp/CryptoTools/CryptoModule.cpp b/native/cpp/CommonCpp/CryptoTools/CryptoModule.cpp --- a/native/cpp/CommonCpp/CryptoTools/CryptoModule.cpp +++ b/native/cpp/CommonCpp/CryptoTools/CryptoModule.cpp @@ -340,19 +340,37 @@ std::string CryptoModule::decrypt( const std::string &targetUserId, - EncryptedData encryptedData) { + EncryptedData &encryptedData) { if (!this->hasSessionFor(targetUserId)) { throw std::runtime_error{"error decrypt => uninitialized session"}; } OlmSession *session = this->sessions.at(targetUserId)->getOlmSession(); - OlmBuffer tmpEncryptedMessage(encryptedData.message); + OlmBuffer utilityBuffer(::olm_utility_size()); + OlmUtility *olmUtility = ::olm_utility(utilityBuffer.data()); + + OlmBuffer messageHashBuffer(::olm_sha256_length(olmUtility)); + ::olm_sha256( + olmUtility, + encryptedData.message.data(), + encryptedData.message.size(), + messageHashBuffer.data(), + messageHashBuffer.size()); + OlmBuffer tmpEncryptedMessage(encryptedData.message); size_t maxSize = ::olm_decrypt_max_plaintext_length( session, encryptedData.messageType, tmpEncryptedMessage.data(), tmpEncryptedMessage.size()); + + if (maxSize == -1) { + throw std::runtime_error{ + "error decrypt_max_plaintext_length => " + + std::string{::olm_session_last_error(session)} + ". Hash: " + + std::string{messageHashBuffer.begin(), messageHashBuffer.end()}}; + } + OlmBuffer decryptedMessage(maxSize); size_t decryptedSize = ::olm_decrypt( session, @@ -363,7 +381,9 @@ decryptedMessage.size()); if (decryptedSize == -1) { throw std::runtime_error{ - "error decrypt => " + std::string{::olm_session_last_error(session)}}; + "error decrypt => " + std::string{::olm_session_last_error(session)} + + ". Hash: " + + std::string{messageHashBuffer.begin(), messageHashBuffer.end()}}; } return std::string{(char *)decryptedMessage.data(), decryptedSize}; } diff --git a/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp b/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp --- a/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp +++ b/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp @@ -274,9 +274,11 @@ const std::string &callingProcessName) { std::string decryptedData; auto caller = [&](crypto::CryptoModule &cryptoModule) { + crypto::EncryptedData encryptedData{ + std::vector(data.begin(), data.end()), messageType}; decryptedData = cryptoModule.decrypt( NotificationsCryptoModule::keyserverHostedNotificationsID, - {std::vector(data.begin(), data.end()), messageType}); + encryptedData); }; NotificationsCryptoModule::callCryptoModule(caller, callingProcessName); return decryptedData; diff --git a/native/ios/NotificationService/NotificationService.mm b/native/ios/NotificationService/NotificationService.mm --- a/native/ios/NotificationService/NotificationService.mm +++ b/native/ios/NotificationService/NotificationService.mm @@ -60,18 +60,19 @@ // Step 1: notification decryption. if ([self shouldBeDecrypted:content.userInfo]) { std::string decryptErrorMessage; + std::string notifID = std::string([content.userInfo[@"id"] UTF8String]); try { @try { [self decryptContentInPlace:content]; } @catch (NSException *e) { decryptErrorMessage = "NSE: Received Obj-C exception: " + std::string([e.name UTF8String]) + - " during notification decryption."; + " during notification decryption. Notif ID: " + notifID; } } catch (const std::exception &e) { decryptErrorMessage = "NSE: Received C++ exception: " + std::string(e.what()) + - " during notification decryption."; + " during notification decryption. Notif ID: " + notifID; } if (decryptErrorMessage.size()) {