Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3332624
D9559.id32295.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
15 KB
Referenced Files
None
Subscribers
None
D9559.id32295.diff
View Options
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
@@ -16,13 +16,13 @@
class CryptoModule {
- OlmAccount *account = nullptr;
OlmBuffer accountBuffer;
std::unordered_map<std::string, std::shared_ptr<Session>> sessions = {};
Keys keys;
+ OlmAccount *getOlmAccount();
void createAccount();
void exposePublicIdentityKeys();
void generateOneTimeKeys(size_t oneTimeKeysAmount);
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
@@ -25,43 +25,49 @@
}
}
+OlmAccount *CryptoModule::getOlmAccount() {
+ return reinterpret_cast<OlmAccount *>(this->accountBuffer.data());
+}
+
void CryptoModule::createAccount() {
this->accountBuffer.resize(::olm_account_size());
- this->account = ::olm_account(this->accountBuffer.data());
+ ::olm_account(this->accountBuffer.data());
- size_t randomSize = ::olm_create_account_random_length(this->account);
+ size_t randomSize = ::olm_create_account_random_length(this->getOlmAccount());
OlmBuffer randomBuffer;
PlatformSpecificTools::generateSecureRandomBytes(randomBuffer, randomSize);
if (-1 ==
- ::olm_create_account(this->account, randomBuffer.data(), randomSize)) {
+ ::olm_create_account(
+ this->getOlmAccount(), randomBuffer.data(), randomSize)) {
throw std::runtime_error{
"error createAccount => " +
- std::string{::olm_account_last_error(this->account)}};
+ std::string{::olm_account_last_error(this->getOlmAccount())}};
};
}
void CryptoModule::exposePublicIdentityKeys() {
- size_t identityKeysSize = ::olm_account_identity_keys_length(this->account);
+ size_t identityKeysSize =
+ ::olm_account_identity_keys_length(this->getOlmAccount());
if (this->keys.identityKeys.size() == identityKeysSize) {
return;
}
this->keys.identityKeys.resize(
- ::olm_account_identity_keys_length(this->account));
+ ::olm_account_identity_keys_length(this->getOlmAccount()));
if (-1 ==
::olm_account_identity_keys(
- this->account,
+ this->getOlmAccount(),
this->keys.identityKeys.data(),
this->keys.identityKeys.size())) {
throw std::runtime_error{
"error generateIdentityKeys => " +
- std::string{::olm_account_last_error(this->account)}};
+ std::string{::olm_account_last_error(this->getOlmAccount())}};
}
}
void CryptoModule::generateOneTimeKeys(size_t oneTimeKeysAmount) {
size_t oneTimeKeysSize = ::olm_account_generate_one_time_keys_random_length(
- this->account, oneTimeKeysAmount);
+ this->getOlmAccount(), oneTimeKeysAmount);
if (this->keys.oneTimeKeys.size() == oneTimeKeysSize) {
return;
}
@@ -70,27 +76,30 @@
if (-1 ==
::olm_account_generate_one_time_keys(
- this->account, oneTimeKeysAmount, random.data(), random.size())) {
+ this->getOlmAccount(),
+ oneTimeKeysAmount,
+ random.data(),
+ random.size())) {
throw std::runtime_error{
"error generateOneTimeKeys => " +
- std::string{::olm_account_last_error(this->account)}};
+ std::string{::olm_account_last_error(this->getOlmAccount())}};
}
}
// returns number of published keys
size_t CryptoModule::publishOneTimeKeys() {
this->keys.oneTimeKeys.resize(
- ::olm_account_one_time_keys_length(this->account));
+ ::olm_account_one_time_keys_length(this->getOlmAccount()));
if (-1 ==
::olm_account_one_time_keys(
- this->account,
+ this->getOlmAccount(),
this->keys.oneTimeKeys.data(),
this->keys.oneTimeKeys.size())) {
throw std::runtime_error{
"error publishOneTimeKeys => " +
- std::string{::olm_account_last_error(this->account)}};
+ std::string{::olm_account_last_error(this->getOlmAccount())}};
}
- return ::olm_account_mark_keys_as_published(this->account);
+ return ::olm_account_mark_keys_as_published(this->getOlmAccount());
}
Keys CryptoModule::keysFromStrings(
@@ -123,33 +132,36 @@
}
std::uint8_t CryptoModule::getNumPrekeys() {
- return reinterpret_cast<olm::Account *>(this->account)->num_prekeys;
+ return reinterpret_cast<olm::Account *>(this->getOlmAccount())->num_prekeys;
}
std::string CryptoModule::getPrekey() {
OlmBuffer prekey;
- prekey.resize(::olm_account_prekey_length(this->account));
+ prekey.resize(::olm_account_prekey_length(this->getOlmAccount()));
- if (-1 == ::olm_account_prekey(this->account, prekey.data(), prekey.size())) {
+ if (-1 ==
+ ::olm_account_prekey(
+ this->getOlmAccount(), prekey.data(), prekey.size())) {
throw std::runtime_error{
"error getPrekey => " +
- std::string{::olm_account_last_error(this->account)}};
+ std::string{::olm_account_last_error(this->getOlmAccount())}};
}
return std::string{std::string{prekey.begin(), prekey.end()}};
}
std::string CryptoModule::getPrekeySignature() {
- size_t signatureSize = ::olm_account_signature_length(this->account);
+ size_t signatureSize = ::olm_account_signature_length(this->getOlmAccount());
OlmBuffer signatureBuffer;
signatureBuffer.resize(signatureSize);
if (-1 ==
- ::olm_account_prekey_signature(this->account, signatureBuffer.data())) {
+ ::olm_account_prekey_signature(
+ this->getOlmAccount(), signatureBuffer.data())) {
throw std::runtime_error{
"error getPrekeySignature => " +
- std::string{::olm_account_last_error(this->account)}};
+ std::string{::olm_account_last_error(this->getOlmAccount())}};
}
return std::string{signatureBuffer.begin(), signatureBuffer.end()};
@@ -157,17 +169,17 @@
folly::Optional<std::string> CryptoModule::getUnpublishedPrekey() {
OlmBuffer prekey;
- prekey.resize(::olm_account_prekey_length(this->account));
+ prekey.resize(::olm_account_prekey_length(this->getOlmAccount()));
std::size_t retval = ::olm_account_unpublished_prekey(
- this->account, prekey.data(), prekey.size());
+ this->getOlmAccount(), 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)}};
+ std::string{::olm_account_last_error(this->getOlmAccount())}};
}
return std::string{prekey.begin(), prekey.end()};
@@ -175,37 +187,39 @@
std::string CryptoModule::generateAndGetPrekey() {
size_t prekeySize =
- ::olm_account_generate_prekey_random_length(this->account);
+ ::olm_account_generate_prekey_random_length(this->getOlmAccount());
OlmBuffer random;
PlatformSpecificTools::generateSecureRandomBytes(random, prekeySize);
if (-1 ==
::olm_account_generate_prekey(
- this->account, random.data(), random.size())) {
+ this->getOlmAccount(), random.data(), random.size())) {
throw std::runtime_error{
"error generateAndGetPrekey => " +
- std::string{::olm_account_last_error(this->account)}};
+ std::string{::olm_account_last_error(this->getOlmAccount())}};
}
OlmBuffer prekey;
- prekey.resize(::olm_account_prekey_length(this->account));
+ prekey.resize(::olm_account_prekey_length(this->getOlmAccount()));
- if (-1 == ::olm_account_prekey(this->account, prekey.data(), prekey.size())) {
+ if (-1 ==
+ ::olm_account_prekey(
+ this->getOlmAccount(), prekey.data(), prekey.size())) {
throw std::runtime_error{
"error generateAndGetPrekey => " +
- std::string{::olm_account_last_error(this->account)}};
+ std::string{::olm_account_last_error(this->getOlmAccount())}};
}
return std::string{prekey.begin(), prekey.end()};
}
void CryptoModule::markPrekeyAsPublished() {
- ::olm_account_mark_prekey_as_published(this->account);
+ ::olm_account_mark_prekey_as_published(this->getOlmAccount());
}
void CryptoModule::forgetOldPrekey() {
- ::olm_account_forget_old_prekey(this->account);
+ ::olm_account_forget_old_prekey(this->getOlmAccount());
}
void CryptoModule::initializeInboundForReceivingSession(
@@ -223,7 +237,10 @@
}
}
std::unique_ptr<Session> newSession = Session::createSessionAsResponder(
- this->account, this->keys.identityKeys.data(), encryptedMessage, idKeys);
+ this->getOlmAccount(),
+ this->keys.identityKeys.data(),
+ encryptedMessage,
+ idKeys);
this->sessions.insert(make_pair(targetUserId, std::move(newSession)));
}
@@ -240,7 +257,7 @@
this->sessions.erase(this->sessions.find(targetUserId));
}
std::unique_ptr<Session> newSession = Session::createSessionAsInitializer(
- this->account,
+ this->getOlmAccount(),
this->keys.identityKeys.data(),
idKeys,
preKeys,
@@ -261,18 +278,19 @@
Persist CryptoModule::storeAsB64(const std::string &secretKey) {
Persist persist;
- size_t accountPickleLength = ::olm_pickle_account_length(this->account);
+ size_t accountPickleLength =
+ ::olm_pickle_account_length(this->getOlmAccount());
OlmBuffer accountPickleBuffer(accountPickleLength);
if (accountPickleLength !=
::olm_pickle_account(
- this->account,
+ this->getOlmAccount(),
secretKey.data(),
secretKey.size(),
accountPickleBuffer.data(),
accountPickleLength)) {
throw std::runtime_error{
"error storeAsB64 => " +
- std::string{::olm_account_last_error(this->account)}};
+ std::string{::olm_account_last_error(this->getOlmAccount())}};
}
persist.account = accountPickleBuffer;
@@ -289,23 +307,26 @@
const std::string &secretKey,
Persist persist) {
this->accountBuffer.resize(::olm_account_size());
- this->account = ::olm_account(this->accountBuffer.data());
+ ::olm_account(this->accountBuffer.data());
if (-1 ==
::olm_unpickle_account(
- this->account,
+ this->getOlmAccount(),
secretKey.data(),
secretKey.size(),
persist.account.data(),
persist.account.size())) {
throw std::runtime_error{
"error restoreFromB64 => " +
- std::string{::olm_account_last_error(this->account)}};
+ std::string{::olm_account_last_error(this->getOlmAccount())}};
}
std::unordered_map<std::string, OlmBuffer>::iterator it;
for (it = persist.sessions.begin(); it != persist.sessions.end(); ++it) {
std::unique_ptr<Session> session = session->restoreFromB64(
- this->account, this->keys.identityKeys.data(), secretKey, it->second);
+ this->getOlmAccount(),
+ this->keys.identityKeys.data(),
+ secretKey,
+ it->second);
this->sessions.insert(make_pair(it->first, move(session)));
}
}
@@ -390,16 +411,17 @@
std::string CryptoModule::signMessage(const std::string &message) {
OlmBuffer signature;
- signature.resize(::olm_account_signature_length(this->account));
+ signature.resize(::olm_account_signature_length(this->getOlmAccount()));
size_t signatureLength = ::olm_account_sign(
- this->account,
+ this->getOlmAccount(),
(uint8_t *)message.data(),
message.length(),
signature.data(),
signature.size());
if (signatureLength == -1) {
throw std::runtime_error{
- "olm error: " + std::string{::olm_account_last_error(this->account)}};
+ "olm error: " +
+ std::string{::olm_account_last_error(this->getOlmAccount())}};
}
return std::string{(char *)signature.data(), signatureLength};
}
diff --git a/native/cpp/CommonCpp/CryptoTools/Session.h b/native/cpp/CommonCpp/CryptoTools/Session.h
--- a/native/cpp/CommonCpp/CryptoTools/Session.h
+++ b/native/cpp/CommonCpp/CryptoTools/Session.h
@@ -14,7 +14,6 @@
OlmAccount *ownerUserAccount;
std::uint8_t *ownerIdentityKeys;
- OlmSession *olmSession = nullptr;
OlmBuffer olmSessionBuffer;
Session(OlmAccount *account, std::uint8_t *ownerIdentityKeys)
diff --git a/native/cpp/CommonCpp/CryptoTools/Session.cpp b/native/cpp/CommonCpp/CryptoTools/Session.cpp
--- a/native/cpp/CommonCpp/CryptoTools/Session.cpp
+++ b/native/cpp/CommonCpp/CryptoTools/Session.cpp
@@ -6,6 +6,10 @@
namespace comm {
namespace crypto {
+OlmSession *Session::getOlmSession() {
+ return reinterpret_cast<OlmSession *>(this->olmSessionBuffer.data());
+}
+
std::unique_ptr<Session> Session::createSessionAsInitializer(
OlmAccount *account,
std::uint8_t *ownerIdentityKeys,
@@ -17,16 +21,16 @@
std::unique_ptr<Session> session(new Session(account, ownerIdentityKeys));
session->olmSessionBuffer.resize(::olm_session_size());
- session->olmSession = ::olm_session(session->olmSessionBuffer.data());
+ ::olm_session(session->olmSessionBuffer.data());
OlmBuffer randomBuffer;
PlatformSpecificTools::generateSecureRandomBytes(
randomBuffer,
- ::olm_create_outbound_session_random_length(session->olmSession));
+ ::olm_create_outbound_session_random_length(session->getOlmSession()));
if (-1 ==
::olm_create_outbound_session(
- session->olmSession,
+ session->getOlmSession(),
session->ownerUserAccount,
idKeys.data() + ID_KEYS_PREFIX_OFFSET,
KEYSIZE,
@@ -43,7 +47,7 @@
randomBuffer.size())) {
throw std::runtime_error(
"error createOutbound => " +
- std::string{::olm_session_last_error(session->olmSession)});
+ std::string{::olm_session_last_error(session->getOlmSession())});
}
return session;
}
@@ -57,25 +61,25 @@
OlmBuffer tmpEncryptedMessage(encryptedMessage);
session->olmSessionBuffer.resize(::olm_session_size());
- session->olmSession = ::olm_session(session->olmSessionBuffer.data());
+ ::olm_session(session->olmSessionBuffer.data());
if (-1 ==
::olm_create_inbound_session(
- session->olmSession,
+ session->getOlmSession(),
session->ownerUserAccount,
tmpEncryptedMessage.data(),
encryptedMessage.size())) {
throw std::runtime_error(
"error createInbound => " +
- std::string{::olm_session_last_error(session->olmSession)});
+ std::string{::olm_session_last_error(session->getOlmSession())});
}
return session;
}
OlmBuffer Session::storeAsB64(const std::string &secretKey) {
- size_t pickleLength = ::olm_pickle_session_length(this->olmSession);
+ size_t pickleLength = ::olm_pickle_session_length(this->getOlmSession());
OlmBuffer pickle(pickleLength);
size_t res = ::olm_pickle_session(
- this->olmSession,
+ this->getOlmSession(),
secretKey.data(),
secretKey.size(),
pickle.data(),
@@ -94,10 +98,10 @@
std::unique_ptr<Session> session(new Session(account, ownerIdentityKeys));
session->olmSessionBuffer.resize(::olm_session_size());
- session->olmSession = ::olm_session(session->olmSessionBuffer.data());
+ ::olm_session(session->olmSessionBuffer.data());
if (-1 ==
::olm_unpickle_session(
- session->olmSession,
+ session->getOlmSession(),
secretKey.data(),
secretKey.size(),
b64.data(),
@@ -107,13 +111,5 @@
return session;
}
-OlmSession *Session::getOlmSession() {
- if (this->olmSession == nullptr) {
- throw std::runtime_error(
- "trying to obtain a session pointer of uninitialized session");
- }
- return this->olmSession;
-}
-
} // namespace crypto
} // namespace comm
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Nov 22, 12:29 AM (10 h, 14 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2559209
Default Alt Text
D9559.id32295.diff (15 KB)
Attached To
Mode
D9559: [native] Avoid pointer to other parts of class
Attached
Detach File
Event Timeline
Log In to Comment