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 @@ -29,6 +29,7 @@ // returns number of published keys size_t publishOneTimeKeys(); bool prekeyExistsAndOlderThan(uint64_t threshold); + OlmBuffer pickleAccount(const std::string &secretKey); public: const std::string id; @@ -72,6 +73,7 @@ Persist storeAsB64(const std::string &secretKey); void restoreFromB64(const std::string &secretKey, Persist persist); + std::string pickleAccountToString(const std::string &secretKey); EncryptedData encrypt(const std::string &targetDeviceId, const std::string &content); 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 @@ -328,23 +328,29 @@ this->sessions.erase(deviceId); } -Persist CryptoModule::storeAsB64(const std::string &secretKey) { - Persist persist; - size_t accountPickleLength = - ::olm_pickle_account_length(this->getOlmAccount()); +OlmBuffer CryptoModule::pickleAccount(const std::string &secretKey) { + OlmAccount *olmAccount = this->getOlmAccount(); + size_t accountPickleLength = ::olm_pickle_account_length(olmAccount); OlmBuffer accountPickleBuffer(accountPickleLength); - if (accountPickleLength != - ::olm_pickle_account( - this->getOlmAccount(), - secretKey.data(), - secretKey.size(), - accountPickleBuffer.data(), - accountPickleLength)) { - throw std::runtime_error{ - "error storeAsB64 => " + - std::string{::olm_account_last_error(this->getOlmAccount())}}; + + size_t result = ::olm_pickle_account( + olmAccount, + secretKey.data(), + secretKey.size(), + accountPickleBuffer.data(), + accountPickleLength); + + if (accountPickleLength != result) { + throw std::runtime_error( + "Error in pickleAccount => " + + std::string(::olm_account_last_error(olmAccount))); } - persist.account = accountPickleBuffer; + return accountPickleBuffer; +} + +Persist CryptoModule::storeAsB64(const std::string &secretKey) { + Persist persist; + persist.account = this->pickleAccount(secretKey); std::unordered_map>::iterator it; for (it = this->sessions.begin(); it != this->sessions.end(); ++it) { @@ -356,6 +362,11 @@ return persist; } +std::string CryptoModule::pickleAccountToString(const std::string &secretKey) { + OlmBuffer pickledAccount = this->pickleAccount(secretKey); + return std::string(pickledAccount.begin(), pickledAccount.end()); +} + void CryptoModule::restoreFromB64( const std::string &secretKey, Persist persist) { 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 @@ -2559,10 +2559,8 @@ if (!error.size()) { try { pickleKey = crypto::Tools::generateRandomString(64); - crypto::Persist persist = - this->contentCryptoModule->storeAsB64(pickleKey); pickledAccount = - std::string(persist.account.begin(), persist.account.end()); + this->contentCryptoModule->pickleAccountToString(pickleKey); } catch (const std::exception &e) { error = "Failed to pickle crypto account"; } @@ -2615,10 +2613,8 @@ if (!error.size()) { try { pickleKey = crypto::Tools::generateRandomString(64); - crypto::Persist persist = - this->contentCryptoModule->storeAsB64(pickleKey); pickledAccount = - std::string(persist.account.begin(), persist.account.end()); + this->contentCryptoModule->pickleAccountToString(pickleKey); } catch (const std::exception &e) { error = "Failed to pickle crypto account"; }