diff --git a/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.h b/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.h --- a/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.h +++ b/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.h @@ -13,15 +13,16 @@ static std::string getPicklingKey(); static void serializeAndFlushCryptoModule( - crypto::CryptoModule &cryptoModule, + std::unique_ptr cryptoModule, const std::string &path, const std::string &picklingKey, const std::string &callingProcessName); - static crypto::CryptoModule deserializeCryptoModule( + static std::unique_ptr deserializeCryptoModule( const std::string &path, const std::string &picklingKey); static void callCryptoModule( - std::function caller, + std::function &cryptoModule)> caller, const std::string &callingProcessName); public: @@ -53,7 +54,7 @@ class StatefulDecryptResult { StatefulDecryptResult( - crypto::CryptoModule cryptoModule, + std::unique_ptr cryptoModule, std::string decryptedData); std::unique_ptr cryptoModuleState; std::string decryptedData; 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 @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace comm { @@ -26,7 +27,8 @@ const int NotificationsCryptoModule::olmEncryptedTypeMessage = 1; const int temporaryFilePathRandomSuffixLength = 32; -crypto::CryptoModule NotificationsCryptoModule::deserializeCryptoModule( +std::unique_ptr +NotificationsCryptoModule::deserializeCryptoModule( const std::string &path, const std::string &picklingKey) { std::ifstream pickledPersistStream(path, std::ifstream::in); @@ -55,8 +57,10 @@ std::unordered_map sessions; if (persistJSON["sessions"].isNull()) { - return crypto::CryptoModule{ - notificationsCryptoAccountID, picklingKey, {account, sessions}}; + return std::make_unique( + notificationsCryptoAccountID, + picklingKey, + crypto::Persist({account, sessions})); } for (auto &sessionKeyValuePair : persistJSON["sessions"].items()) { std::string targetUserID = sessionKeyValuePair.first.asString(); @@ -64,16 +68,18 @@ sessions[targetUserID] = std::vector(sessionData.begin(), sessionData.end()); } - return crypto::CryptoModule{ - notificationsCryptoAccountID, picklingKey, {account, sessions}}; + return std::make_unique( + notificationsCryptoAccountID, + picklingKey, + crypto::Persist({account, sessions})); } void NotificationsCryptoModule::serializeAndFlushCryptoModule( - crypto::CryptoModule &cryptoModule, + std::unique_ptr cryptoModule, const std::string &path, const std::string &picklingKey, const std::string &callingProcessName) { - crypto::Persist persist = cryptoModule.storeAsB64(picklingKey); + crypto::Persist persist = cryptoModule->storeAsB64(picklingKey); folly::dynamic sessions = folly::dynamic::object; for (auto &sessionKeyValuePair : persist.sessions) { @@ -145,16 +151,17 @@ } void NotificationsCryptoModule::callCryptoModule( - std::function caller, + std::function< + void(const std::unique_ptr &cryptoModule)> caller, const std::string &callingProcessName) { const std::string picklingKey = NotificationsCryptoModule::getPicklingKey(); const std::string path = PlatformSpecificTools::getNotificationsCryptoAccountPath(); - crypto::CryptoModule cryptoModule = + std::unique_ptr cryptoModule = NotificationsCryptoModule::deserializeCryptoModule(path, picklingKey); caller(cryptoModule); NotificationsCryptoModule::serializeAndFlushCryptoModule( - cryptoModule, path, picklingKey, callingProcessName); + std::move(cryptoModule), path, picklingKey, callingProcessName); } void NotificationsCryptoModule::initializeNotificationsCryptoAccount( @@ -177,10 +184,11 @@ NotificationsCryptoModule::secureStoreNotificationsAccountDataKey, picklingKey); - crypto::CryptoModule cryptoModule{ - NotificationsCryptoModule::notificationsCryptoAccountID}; + std::unique_ptr cryptoModule = + std::make_unique( + NotificationsCryptoModule::notificationsCryptoAccountID); NotificationsCryptoModule::serializeAndFlushCryptoModule( - cryptoModule, + std::move(cryptoModule), notificationsCryptoAccountPath, picklingKey, callingProcessName); @@ -189,8 +197,9 @@ std::string NotificationsCryptoModule::getNotificationsIdentityKeys( const std::string &callingProcessName) { std::string identityKeys; - auto caller = [&identityKeys](crypto::CryptoModule cryptoModule) { - identityKeys = cryptoModule.getIdentityKeys(); + auto caller = [&identityKeys]( + const std::unique_ptr &cryptoModule) { + identityKeys = cryptoModule->getIdentityKeys(); }; NotificationsCryptoModule::callCryptoModule(caller, callingProcessName); return identityKeys; @@ -199,9 +208,10 @@ std::string NotificationsCryptoModule::generateAndGetNotificationsPrekey( const std::string &callingProcessName) { std::string prekey; - auto caller = [&prekey](crypto::CryptoModule cryptoModule) { - prekey = cryptoModule.generateAndGetPrekey(); - }; + auto caller = + [&prekey](const std::unique_ptr &cryptoModule) { + prekey = cryptoModule->generateAndGetPrekey(); + }; NotificationsCryptoModule::callCryptoModule(caller, callingProcessName); return prekey; } @@ -209,8 +219,9 @@ std::string NotificationsCryptoModule::getNotificationsPrekeySignature( const std::string &callingProcessName) { std::string prekeySignature; - auto caller = [&prekeySignature](crypto::CryptoModule cryptoModule) { - prekeySignature = cryptoModule.getPrekeySignature(); + auto caller = [&prekeySignature]( + const std::unique_ptr &cryptoModule) { + prekeySignature = cryptoModule->getPrekeySignature(); }; NotificationsCryptoModule::callCryptoModule(caller, callingProcessName); return prekeySignature; @@ -220,9 +231,9 @@ const size_t oneTimeKeysAmount, const std::string &callingProcessName) { std::string oneTimeKeys; - auto caller = [&oneTimeKeys, - oneTimeKeysAmount](crypto::CryptoModule cryptoModule) { - oneTimeKeys = cryptoModule.getOneTimeKeys(oneTimeKeysAmount); + auto caller = [&oneTimeKeys, oneTimeKeysAmount]( + const std::unique_ptr &cryptoModule) { + oneTimeKeys = cryptoModule->getOneTimeKeys(oneTimeKeysAmount); }; NotificationsCryptoModule::callCryptoModule(caller, callingProcessName); return oneTimeKeys; @@ -235,14 +246,14 @@ const std::string &oneTimeKeys, const std::string &callingProcessName) { crypto::EncryptedData initialEncryptedMessage; - auto caller = [&](crypto::CryptoModule &cryptoModule) { - cryptoModule.initializeOutboundForSendingSession( + auto caller = [&](const std::unique_ptr &cryptoModule) { + cryptoModule->initializeOutboundForSendingSession( NotificationsCryptoModule::keyserverHostedNotificationsID, std::vector(identityKeys.begin(), identityKeys.end()), std::vector(prekey.begin(), prekey.end()), std::vector(prekeySignature.begin(), prekeySignature.end()), std::vector(oneTimeKeys.begin(), oneTimeKeys.end())); - initialEncryptedMessage = cryptoModule.encrypt( + initialEncryptedMessage = cryptoModule->encrypt( NotificationsCryptoModule::keyserverHostedNotificationsID, NotificationsCryptoModule::initialEncryptedMessageContent); }; @@ -253,8 +264,9 @@ bool NotificationsCryptoModule::isNotificationsSessionInitialized( const std::string &callingProcessName) { bool sessionInitialized; - auto caller = [&sessionInitialized](crypto::CryptoModule &cryptoModule) { - sessionInitialized = cryptoModule.hasSessionFor( + auto caller = [&sessionInitialized]( + const std::unique_ptr &cryptoModule) { + sessionInitialized = cryptoModule->hasSessionFor( NotificationsCryptoModule::keyserverHostedNotificationsID); }; NotificationsCryptoModule::callCryptoModule(caller, callingProcessName); @@ -276,10 +288,10 @@ const size_t messageType, const std::string &callingProcessName) { std::string decryptedData; - auto caller = [&](crypto::CryptoModule &cryptoModule) { + auto caller = [&](const std::unique_ptr &cryptoModule) { crypto::EncryptedData encryptedData{ std::vector(data.begin(), data.end()), messageType}; - decryptedData = cryptoModule.decrypt( + decryptedData = cryptoModule->decrypt( NotificationsCryptoModule::keyserverHostedNotificationsID, encryptedData); }; @@ -288,10 +300,9 @@ } NotificationsCryptoModule::StatefulDecryptResult::StatefulDecryptResult( - crypto::CryptoModule cryptoModule, + std::unique_ptr cryptoModule, std::string decryptedData) - : cryptoModuleState(std::make_unique(cryptoModule)), - decryptedData(decryptedData) { + : cryptoModuleState(std::move(cryptoModule)), decryptedData(decryptedData) { } std::string @@ -306,13 +317,14 @@ std::string path = PlatformSpecificTools::getNotificationsCryptoAccountPath(); std::string picklingKey = NotificationsCryptoModule::getPicklingKey(); - crypto::CryptoModule cryptoModule = + std::unique_ptr cryptoModule = NotificationsCryptoModule::deserializeCryptoModule(path, picklingKey); crypto::EncryptedData encryptedData{ std::vector(data.begin(), data.end()), messageType}; - std::string decryptedData = cryptoModule.decrypt( + std::string decryptedData = cryptoModule->decrypt( NotificationsCryptoModule::keyserverHostedNotificationsID, encryptedData); - StatefulDecryptResult statefulDecryptResult(cryptoModule, decryptedData); + StatefulDecryptResult statefulDecryptResult( + std::move(cryptoModule), decryptedData); return std::make_unique( std::move(statefulDecryptResult)); @@ -325,9 +337,10 @@ std::string path = PlatformSpecificTools::getNotificationsCryptoAccountPath(); std::string picklingKey = NotificationsCryptoModule::getPicklingKey(); - crypto::CryptoModule cryptoModule = *statefulDecryptResult->cryptoModuleState; - NotificationsCryptoModule::serializeAndFlushCryptoModule( - cryptoModule, path, picklingKey, callingProcessName); + std::move(statefulDecryptResult->cryptoModuleState), + path, + picklingKey, + callingProcessName); } } // namespace comm