Changeset View
Changeset View
Standalone View
Standalone View
native/cpp/CommonCpp/CryptoTools/CryptoModule.cpp
#include "CryptoModule.h" | #include "CryptoModule.h" | ||||
#include "PlatformSpecificTools.h" | #include "PlatformSpecificTools.h" | ||||
#include "olm/account.hh" | |||||
#include "olm/session.hh" | #include "olm/session.hh" | ||||
#include <stdexcept> | #include <stdexcept> | ||||
namespace comm { | namespace comm { | ||||
namespace crypto { | namespace crypto { | ||||
CryptoModule::CryptoModule(std::string id) : id{id} { | CryptoModule::CryptoModule(std::string id) : id{id} { | ||||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | throw std::runtime_error{ | ||||
std::to_string(oneTimeKeysAmount) + ", got " + | std::to_string(oneTimeKeysAmount) + ", got " + | ||||
std::to_string(publishedOneTimeKeys)}; | std::to_string(publishedOneTimeKeys)}; | ||||
} | } | ||||
return std::string{ | return std::string{ | ||||
this->keys.oneTimeKeys.begin(), this->keys.oneTimeKeys.end()}; | this->keys.oneTimeKeys.begin(), this->keys.oneTimeKeys.end()}; | ||||
} | } | ||||
std::uint8_t CryptoModule::getNumPrekeys() { | |||||
return reinterpret_cast<olm::Account *>(this->account)->num_prekeys; | |||||
} | |||||
std::string CryptoModule::getPrekey() { | |||||
OlmBuffer prekey; | |||||
prekey.resize(::olm_account_prekey_length(this->account)); | |||||
if (-1 == ::olm_account_prekey(this->account, prekey.data(), prekey.size())) { | |||||
throw std::runtime_error{ | |||||
"error getPrekey => " + | |||||
std::string{::olm_account_last_error(this->account)}}; | |||||
} | |||||
return std::string{std::string{prekey.begin(), prekey.end()}}; | |||||
} | |||||
folly::Optional<std::string> CryptoModule::getUnpublishedPrekey() { | |||||
OlmBuffer prekey; | |||||
prekey.resize(::olm_account_prekey_length(this->account)); | |||||
std::size_t retval = ::olm_account_unpublished_prekey( | |||||
this->account, prekey.data(), prekey.size()); | |||||
if (0 == retval) { | |||||
return folly::none; | |||||
} else if (-1 == retval) { | |||||
throw std::runtime_error{ | |||||
"error getUnpublishedPrekey => " + | |||||
std::string{::olm_account_last_error(this->account)}}; | |||||
} | |||||
return std::string{prekey.begin(), prekey.end()}; | |||||
} | |||||
std::string CryptoModule::generateAndGetPrekey() { | |||||
size_t prekeySize = | |||||
::olm_account_generate_prekey_random_length(this->account); | |||||
OlmBuffer random; | |||||
PlatformSpecificTools::generateSecureRandomBytes(random, prekeySize); | |||||
if (-1 == | |||||
::olm_account_generate_prekey( | |||||
this->account, random.data(), random.size())) { | |||||
throw std::runtime_error{ | |||||
"error generateAndGetPrekey => " + | |||||
std::string{::olm_account_last_error(this->account)}}; | |||||
} | |||||
OlmBuffer prekey; | |||||
prekey.resize(::olm_account_prekey_length(this->account)); | |||||
if (-1 == ::olm_account_prekey(this->account, prekey.data(), prekey.size())) { | |||||
throw std::runtime_error{ | |||||
"error generateAndGetPrekey => " + | |||||
std::string{::olm_account_last_error(this->account)}}; | |||||
} | |||||
return std::string{prekey.begin(), prekey.end()}; | |||||
} | |||||
void CryptoModule::markPrekeyAsPublished() { | |||||
::olm_account_mark_prekey_as_published(this->account); | |||||
} | |||||
void CryptoModule::forgetOldPrekey() { | |||||
::olm_account_forget_old_prekey(this->account); | |||||
} | |||||
void CryptoModule::initializeInboundForReceivingSession( | void CryptoModule::initializeInboundForReceivingSession( | ||||
const std::string &targetUserId, | const std::string &targetUserId, | ||||
const OlmBuffer &encryptedMessage, | const OlmBuffer &encryptedMessage, | ||||
const OlmBuffer &idKeys, | const OlmBuffer &idKeys, | ||||
const bool overwrite) { | const bool overwrite) { | ||||
if (this->hasSessionFor(targetUserId)) { | if (this->hasSessionFor(targetUserId)) { | ||||
if (overwrite) { | if (overwrite) { | ||||
this->sessions.erase(this->sessions.find(targetUserId)); | this->sessions.erase(this->sessions.find(targetUserId)); | ||||
▲ Show 20 Lines • Show All 218 Lines • Show Last 20 Lines |