diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp @@ -854,7 +854,8 @@ try { if (!error.size()) { notificationsKeysResult = - NotificationsCryptoModule::getNotificationsIdentityKeys(); + NotificationsCryptoModule::getNotificationsIdentityKeys( + "Comm"); } } catch (const std::exception &e) { error = e.what(); 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 @@ -17,11 +17,20 @@ static crypto::CryptoModule deserializeCryptoModule( const std::string &path, const std::string &picklingKey); + static void callCryptoModule( + std::function caller, + const std::string &callingProcessName); public: static void initializeNotificationsCryptoAccount(const std::string &callingProcessName); static void clearSensitiveData(); - static std::string getNotificationsIdentityKeys(); + static std::string + getNotificationsIdentityKeys(const std::string &callingProcessName); + static std::string getNotificationsOneTimeKeys( + size_t oneTimeKeysAmount, + const std::string &callingProcessName); + static std::string + generateNotificationsPrekey(const std::string &callingProcessName); }; } // namespace comm 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 @@ -126,6 +126,28 @@ } } +void NotificationsCryptoModule::callCryptoModule( + std::function caller, + const std::string &callingProcessName) { + CommSecureStore secureStore{}; + folly::Optional picklingKey = secureStore.get( + NotificationsCryptoModule::secureStoreNotificationsAccountDataKey); + if (!picklingKey.hasValue()) { + throw std::runtime_error( + "Attempt to retrieve notifications crypto account before it was " + "correctly initialized."); + } + + const std::string path = + PlatformSpecificTools::getNotificationsCryptoAccountPath(); + crypto::CryptoModule cryptoModule = + NotificationsCryptoModule::deserializeCryptoModule( + path, picklingKey.value()); + caller(cryptoModule); + NotificationsCryptoModule::serializeAndFlushCryptoModule( + cryptoModule, path, picklingKey.value(), callingProcessName); +} + void NotificationsCryptoModule::initializeNotificationsCryptoAccount( const std::string &callingProcessName) { const std::string notificationsCryptoAccountPath = @@ -155,22 +177,36 @@ callingProcessName); } -std::string NotificationsCryptoModule::getNotificationsIdentityKeys() { - CommSecureStore secureStore{}; - folly::Optional picklingKey = secureStore.get( - NotificationsCryptoModule::secureStoreNotificationsAccountDataKey); - if (!picklingKey.hasValue()) { - throw std::runtime_error( - "Attempt to retrieve notifications crypto account before it was " - "correctly initialized."); - } +std::string NotificationsCryptoModule::getNotificationsIdentityKeys( + const std::string &callingProcessName) { + std::string identityKeys; + auto caller = [&identityKeys](crypto::CryptoModule cryptoModule) { + identityKeys = cryptoModule.getIdentityKeys(); + }; + NotificationsCryptoModule::callCryptoModule(caller, callingProcessName); + return identityKeys; +} - const std::string path = - PlatformSpecificTools::getNotificationsCryptoAccountPath(); - crypto::CryptoModule cryptoModule = - NotificationsCryptoModule::deserializeCryptoModule( - path, picklingKey.value()); - return cryptoModule.getIdentityKeys(); +std::string NotificationsCryptoModule::getNotificationsOneTimeKeys( + size_t oneTimeKeysAmount, + const std::string &callingProcessName) { + std::string oneTimeKeys; + auto caller = [&oneTimeKeys, + oneTimeKeysAmount](crypto::CryptoModule cryptoModule) { + oneTimeKeys = cryptoModule.getOneTimeKeys(oneTimeKeysAmount); + }; + NotificationsCryptoModule::callCryptoModule(caller, callingProcessName); + return oneTimeKeys; +} + +std::string NotificationsCryptoModule::generateNotificationsPrekey( + const std::string &callingProcessName) { + std::string preKey; + auto caller = [&preKey](crypto::CryptoModule cryptoModule) { + preKey = cryptoModule.generatePrekey(); + }; + NotificationsCryptoModule::callCryptoModule(caller, callingProcessName); + return preKey; } void NotificationsCryptoModule::clearSensitiveData() {