Page MenuHomePhorge

D9401.1768876162.diff
No OneTemporary

Size
6 KB
Referenced Files
None
Subscribers
None

D9401.1768876162.diff

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
@@ -11,6 +11,12 @@
const static std::string keyserverHostedNotificationsID;
const static std::string initialEncryptedMessageContent;
+ // Stateful NotificationsCryptoModule is
+ // intended to be used when updated CryptoModule
+ // state persistence needs to be deferred.
+ crypto::CryptoModule statefulCryptoModule;
+
+ static std::string getPicklingKey();
static void serializeAndFlushCryptoModule(
crypto::CryptoModule &cryptoModule,
const std::string &path,
@@ -49,5 +55,11 @@
const std::string &data,
const size_t messageType,
const std::string &callingProcessName);
+
+ // Stateful methods declarations
+ NotificationsCryptoModule();
+ std::string
+ statefulDecrypt(const std::string &data, const size_t messageType);
+ void flushState(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
@@ -132,9 +132,7 @@
remove(temporaryPath.c_str());
}
-void NotificationsCryptoModule::callCryptoModule(
- std::function<void(crypto::CryptoModule &cryptoModule)> caller,
- const std::string &callingProcessName) {
+std::string NotificationsCryptoModule::getPicklingKey() {
CommSecureStore secureStore{};
folly::Optional<std::string> picklingKey = secureStore.get(
NotificationsCryptoModule::secureStoreNotificationsAccountDataKey);
@@ -143,15 +141,20 @@
"Attempt to retrieve notifications crypto account before it was "
"correctly initialized.");
}
+ return picklingKey.value();
+}
+void NotificationsCryptoModule::callCryptoModule(
+ std::function<void(crypto::CryptoModule &cryptoModule)> caller,
+ const std::string &callingProcessName) {
+ const std::string picklingKey = NotificationsCryptoModule::getPicklingKey();
const std::string path =
PlatformSpecificTools::getNotificationsCryptoAccountPath();
crypto::CryptoModule cryptoModule =
- NotificationsCryptoModule::deserializeCryptoModule(
- path, picklingKey.value());
+ NotificationsCryptoModule::deserializeCryptoModule(path, picklingKey);
caller(cryptoModule);
NotificationsCryptoModule::serializeAndFlushCryptoModule(
- cryptoModule, path, picklingKey.value(), callingProcessName);
+ cryptoModule, path, picklingKey, callingProcessName);
}
void NotificationsCryptoModule::initializeNotificationsCryptoAccount(
@@ -283,4 +286,28 @@
NotificationsCryptoModule::callCryptoModule(caller, callingProcessName);
return decryptedData;
}
+
+// Stateful methods implementations
+NotificationsCryptoModule::NotificationsCryptoModule()
+ : statefulCryptoModule(NotificationsCryptoModule::deserializeCryptoModule(
+ std::move(PlatformSpecificTools::getNotificationsCryptoAccountPath()),
+ std::move(NotificationsCryptoModule::getPicklingKey()))) {
+}
+
+std::string NotificationsCryptoModule::statefulDecrypt(
+ const std::string &data,
+ const size_t messageType) {
+ crypto::EncryptedData encryptedData{
+ std::vector<uint8_t>(data.begin(), data.end()), messageType};
+ return this->statefulCryptoModule.decrypt(
+ NotificationsCryptoModule::keyserverHostedNotificationsID, encryptedData);
+}
+
+void NotificationsCryptoModule::flushState(
+ const std::string &callingProcessName) {
+ std::string path = PlatformSpecificTools::getNotificationsCryptoAccountPath();
+ std::string picklingKey = NotificationsCryptoModule::getPicklingKey();
+ NotificationsCryptoModule::serializeAndFlushCryptoModule(
+ this->statefulCryptoModule, path, picklingKey, callingProcessName);
+}
} // namespace comm
diff --git a/native/ios/NotificationService/NotificationService.mm b/native/ios/NotificationService/NotificationService.mm
--- a/native/ios/NotificationService/NotificationService.mm
+++ b/native/ios/NotificationService/NotificationService.mm
@@ -63,6 +63,9 @@
UNNotificationContent *publicUserContent = content;
// Step 1: notification decryption.
+ comm::NotificationsCryptoModule statefulNotificationsCryptoModule{};
+ BOOL decryptionExecuted = NO;
+
if ([self shouldBeDecrypted:content.userInfo]) {
std::optional<std::string> notifID;
NSString *objcNotifID = content.userInfo[@"id"];
@@ -73,7 +76,9 @@
std::string decryptErrorMessage;
try {
@try {
- [self decryptContentInPlace:content];
+ [self decryptContentInPlace:content
+ withCryptoModule:statefulNotificationsCryptoModule];
+ decryptionExecuted = YES;
} @catch (NSException *e) {
decryptErrorMessage = "NSE: Received Obj-C exception: " +
std::string([e.name UTF8String]) +
@@ -215,6 +220,11 @@
withPublicUserContent:publicUserContent];
return;
}
+
+ if (decryptionExecuted) {
+ statefulNotificationsCryptoModule.flushState(callingProcessName);
+ }
+
[self callContentHandlerForKey:contentHandlerKey
withContent:publicUserContent];
}
@@ -456,20 +466,19 @@
[payload[encryptionFailureKey] isEqualToNumber:@(1)];
}
-- (NSString *)singleDecrypt:(NSString *)data {
- std::string encryptedData = std::string([data UTF8String]);
- return [NSString
+- (void)decryptContentInPlace:(UNMutableNotificationContent *)content
+ withCryptoModule:
+ (comm::NotificationsCryptoModule &)statefulNotifCryptoModule {
+ std::string encryptedData =
+ std::string([content.userInfo[encryptedPayloadKey] UTF8String]);
+
+ NSString *decryptedSerializedPayload = [NSString
stringWithUTF8String:
- (comm::NotificationsCryptoModule::decrypt(
+ (statefulNotifCryptoModule.statefulDecrypt(
encryptedData,
- comm::NotificationsCryptoModule::olmEncryptedTypeMessage,
- callingProcessName))
+ comm::NotificationsCryptoModule::olmEncryptedTypeMessage))
.c_str()];
-}
-- (void)decryptContentInPlace:(UNMutableNotificationContent *)content {
- NSString *decryptedSerializedPayload =
- [self singleDecrypt:content.userInfo[encryptedPayloadKey]];
NSDictionary *decryptedPayload = [NSJSONSerialization
JSONObjectWithData:[decryptedSerializedPayload
dataUsingEncoding:NSUTF8StringEncoding]

File Metadata

Mime Type
text/plain
Expires
Tue, Jan 20, 2:29 AM (15 h, 13 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5959120
Default Alt Text
D9401.1768876162.diff (6 KB)

Event Timeline