Page MenuHomePhorge

D7383.1768453211.diff
No OneTemporary

Size
8 KB
Referenced Files
None
Subscribers
None

D7383.1768453211.diff

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
@@ -1,4 +1,5 @@
#include "CryptoModule.h"
+#include "Logger.h"
#include "PlatformSpecificTools.h"
#include "olm/account.hh"
#include "olm/session.hh"
@@ -224,9 +225,9 @@
const OlmBuffer &oneTimeKeys,
size_t keyIndex) {
if (this->hasSessionFor(targetUserId)) {
- throw std::runtime_error{
- "error initializeOutboundForSendingSession => session already "
- "initialized"};
+ Logger::log(
+ "olm session overwritten for the user with id: " + targetUserId);
+ this->sessions.erase(this->sessions.find(targetUserId));
}
std::unique_ptr<Session> newSession = Session::createSessionAsInitializer(
this->account,
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
@@ -903,7 +903,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
@@ -8,6 +8,8 @@
class NotificationsCryptoModule {
const static std::string secureStoreNotificationsAccountDataKey;
const static std::string notificationsCryptoAccountID;
+ const static std::string keyserverHostedNotificationsID;
+ const static std::string initialEncryptedMessageContent;
static void serializeAndFlushCryptoModule(
crypto::CryptoModule &cryptoModule,
@@ -17,11 +19,25 @@
static crypto::CryptoModule deserializeCryptoModule(
const std::string &path,
const std::string &picklingKey);
+ static void callCryptoModule(
+ std::function<void(crypto::CryptoModule &cryptoModule)> 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 void initializeNotificationsSession(
+ const std::string &identityKeys,
+ const std::string &prekey,
+ const std::string &prekeySignature,
+ const std::string &oneTimeKeys,
+ const std::string &callingProcessName);
+ static bool
+ isNotificationsSessionInitialized(const std::string &callingProcessName);
+ static crypto::EncryptedData
+ generateInitialEncryptedMessage(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
@@ -19,6 +19,10 @@
"notificationsCryptoAccountDataKey";
const std::string NotificationsCryptoModule::notificationsCryptoAccountID =
"notificationsCryptoAccountDataID";
+const std::string NotificationsCryptoModule::keyserverHostedNotificationsID =
+ "keyserverHostedNotificationsID";
+const std::string NotificationsCryptoModule::initialEncryptedMessageContent =
+ "initialMessage";
crypto::CryptoModule NotificationsCryptoModule::deserializeCryptoModule(
const std::string &path,
@@ -126,6 +130,28 @@
}
}
+void NotificationsCryptoModule::callCryptoModule(
+ std::function<void(crypto::CryptoModule &cryptoModule)> caller,
+ const std::string &callingProcessName) {
+ CommSecureStore secureStore{};
+ folly::Optional<std::string> 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 +181,55 @@
callingProcessName);
}
-std::string NotificationsCryptoModule::getNotificationsIdentityKeys() {
- CommSecureStore secureStore{};
- folly::Optional<std::string> 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();
+void NotificationsCryptoModule::initializeNotificationsSession(
+ const std::string &identityKeys,
+ const std::string &prekey,
+ const std::string &prekeySignature,
+ const std::string &oneTimeKeys,
+ const std::string &callingProcessName) {
+ auto caller = [&](crypto::CryptoModule &cryptoModule) {
+ cryptoModule.initializeOutboundForSendingSession(
+ NotificationsCryptoModule::keyserverHostedNotificationsID,
+ std::vector<uint8_t>(identityKeys.begin(), identityKeys.end()),
+ std::vector<uint8_t>(prekey.begin(), prekey.end()),
+ std::vector<uint8_t>(prekeySignature.begin(), prekeySignature.end()),
+ std::vector<uint8_t>(oneTimeKeys.begin(), oneTimeKeys.end()));
+ };
+ NotificationsCryptoModule::callCryptoModule(caller, callingProcessName);
+}
+
+bool NotificationsCryptoModule::isNotificationsSessionInitialized(
+ const std::string &callingProcessName) {
+ bool sessionInitialized;
+ auto caller = [&sessionInitialized](crypto::CryptoModule &cryptoModule) {
+ sessionInitialized = cryptoModule.hasSessionFor(
+ NotificationsCryptoModule::keyserverHostedNotificationsID);
+ };
+ NotificationsCryptoModule::callCryptoModule(caller, callingProcessName);
+ return sessionInitialized;
+}
+
+crypto::EncryptedData
+NotificationsCryptoModule::generateInitialEncryptedMessage(
+ const std::string &callingProcessName) {
+ crypto::EncryptedData initialEncryptedMessage;
+ auto caller = [&initialEncryptedMessage](crypto::CryptoModule &cryptoModule) {
+ initialEncryptedMessage = cryptoModule.encrypt(
+ NotificationsCryptoModule::keyserverHostedNotificationsID,
+ NotificationsCryptoModule::initialEncryptedMessageContent);
+ };
+ NotificationsCryptoModule::callCryptoModule(caller, callingProcessName);
+ return initialEncryptedMessage;
}
void NotificationsCryptoModule::clearSensitiveData() {

File Metadata

Mime Type
text/plain
Expires
Thu, Jan 15, 5:00 AM (7 h, 53 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5936023
Default Alt Text
D7383.1768453211.diff (8 KB)

Event Timeline