Browse Source

PushNotification

Marino Faggiana 1 year ago
parent
commit
d5df5872d9

+ 1 - 1
Notification Service Extension/NotificationService.swift

@@ -38,7 +38,7 @@ class NotificationService: UNNotificationServiceExtension {
                 if let message = bestAttemptContent.userInfo["subject"] as? String {
                     let tableAccounts = NCManageDatabase.shared.getAllAccount()
                     for tableAccount in tableAccounts {
-                        guard let privateKey = CCUtility.getPushNotificationPrivateKey(tableAccount.account),
+                        guard let privateKey = NCKeychain().getPushNotificationPrivateKey(account: tableAccount.account),
                               let decryptedMessage = NCPushNotificationEncryption.shared().decryptPushNotification(message, withDevicePrivateKey: privateKey),
                               let data = decryptedMessage.data(using: .utf8) else {
                             continue

+ 1 - 2
iOSClient/AppDelegate.swift

@@ -139,7 +139,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
 
         } else {
 
-            CCUtility.deleteAllChainStore()
             NCKeychain().removeAll()
             if let bundleID = Bundle.main.bundleIdentifier {
                 UserDefaults.standard.removePersistentDomain(forName: bundleID)
@@ -623,7 +622,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         NCManageDatabase.shared.clearDatabase(account: account, removeAccount: true)
 
         NCKeychain().clearAllKeysEndToEnd(account: account)
-        CCUtility.clearAllKeysPushNotification(account)
+        NCKeychain().clearAllKeysPushNotification(account: account)
         NCKeychain().setPassword(account: account, password: nil)
 
         self.account = ""

+ 19 - 19
iOSClient/PushNotification/NCPushNotification.m

@@ -54,8 +54,8 @@
     
     for (tableAccount *result in [[NCManageDatabase shared] getAllAccount]) {
         
-        NSString *token = [CCUtility getPushNotificationToken:result.account];
-        
+        NSString *token = [[[NCKeychain alloc] init] getPushNotificationTokenWithAccount:result.account];
+
         if (![token isEqualToString:self.pushKitToken]) {
             if (token != nil) {
                 // unsubscribing + subscribing
@@ -73,8 +73,8 @@
     if (message) {
         NSArray *results = [[NCManageDatabase shared] getAllAccount];
         for (tableAccount *result in results) {
-            if ([CCUtility getPushNotificationPrivateKey:result.account]) {
-                NSData *decryptionKey = [CCUtility getPushNotificationPrivateKey:result.account];
+            if ([[[NCKeychain alloc] init] getPushNotificationPrivateKeyWithAccount:result.account]) {
+                NSData *decryptionKey = [[[NCKeychain alloc] init] getPushNotificationPrivateKeyWithAccount:result.account];
                 NSString *decryptedMessage = [[NCPushNotificationEncryption shared] decryptPushNotification:message withDevicePrivateKey:decryptionKey];
                 if (decryptedMessage) {
                     NSData *data = [decryptedMessage dataUsingEncoding:NSUTF8StringEncoding];
@@ -102,7 +102,7 @@
     [[NCPushNotificationEncryption shared] generatePushNotificationsKeyPair:account];
 
     NSString *pushTokenHash = [[NCEndToEndEncryption sharedManager] createSHA512:self.pushKitToken];
-    NSData *pushPublicKey = [CCUtility getPushNotificationPublicKey:account];
+    NSData *pushPublicKey = [[[NCKeychain alloc] init] getPushNotificationPublicKeyWithAccount:account];
     NSString *pushDevicePublicKey = [[NSString alloc] initWithData:pushPublicKey encoding:NSUTF8StringEncoding];
     NSString *proxyServerPath = [NCBrandOptions shared].pushNotificationServerProxy;
     
@@ -114,10 +114,10 @@
 
                     [[[NextcloudKit shared] nkCommonInstance] writeLog:@"[INFO] Subscribed to Push Notification server & proxy successfully"];
 
-                    [CCUtility setPushNotificationToken:account token:self.pushKitToken];
-                    [CCUtility setPushNotificationDeviceIdentifier:account deviceIdentifier:deviceIdentifier];
-                    [CCUtility setPushNotificationDeviceIdentifierSignature:account deviceIdentifierSignature:signature];
-                    [CCUtility setPushNotificationSubscribingPublicKey:account publicKey:publicKey];
+                    [[[NCKeychain alloc] init] setPushNotificationTokenWithAccount:account token:self.pushKitToken];
+                    [[[NCKeychain alloc] init] setPushNotificationDeviceIdentifierWithAccount:account deviceIdentifier:deviceIdentifier];
+                    [[[NCKeychain alloc] init] setPushNotificationDeviceIdentifierSignatureWithAccount:account deviceIdentifierSignature:signature];
+                    [[[NCKeychain alloc] init] setPushNotificationSubscribingPublicKeyWithAccount:account publicKey:publicKey];
                 }
             }];
         }
@@ -128,9 +128,9 @@
 {
     if (appDelegate.account == nil || appDelegate.account.length == 0) { return; }
     
-    NSString *deviceIdentifier = [CCUtility getPushNotificationDeviceIdentifier:account];
-    NSString *signature = [CCUtility getPushNotificationDeviceIdentifierSignature:account];
-    NSString *publicKey = [CCUtility getPushNotificationSubscribingPublicKey:account];
+    NSString *deviceIdentifier = [[[NCKeychain alloc] init] getPushNotificationDeviceIdentifierWithAccount:account];
+    NSString *signature = [[[NCKeychain alloc] init] getPushNotificationDeviceIdentifierSignatureWithAccount:account];
+    NSString *publicKey = [[[NCKeychain alloc] init] getPushNotificationSubscribingPublicKeyWithAccount:account];
 
     [[NextcloudKit shared] unsubscribingPushNotificationWithServerUrl:urlBase account:account user:user password:[[[NCKeychain alloc] init] getPasswordWithAccount:account] customUserAgent:nil addCustomHeaders:nil queue:dispatch_get_main_queue() completion:^(NSString *account, NKError *error) {
         if (error == NKError.success) {
@@ -141,13 +141,13 @@
                 
                     [[[NextcloudKit shared] nkCommonInstance] writeLog:@"[INFO] Unsubscribed to Push Notification server & proxy successfully."];
                     
-                    [CCUtility setPushNotificationPublicKey:account data:nil];
-                    [CCUtility setPushNotificationSubscribingPublicKey:account publicKey:nil];
-                    [CCUtility setPushNotificationPrivateKey:account data:nil];
-                    [CCUtility setPushNotificationToken:account token:nil];
-                    [CCUtility setPushNotificationDeviceIdentifier:account deviceIdentifier:nil];
-                    [CCUtility setPushNotificationDeviceIdentifierSignature:account deviceIdentifierSignature:nil];
-                    
+                    [[[NCKeychain alloc] init] setPushNotificationPublicKeyWithAccount:account data:nil];
+                    [[[NCKeychain alloc] init] setPushNotificationSubscribingPublicKeyWithAccount:account publicKey:nil];
+                    [[[NCKeychain alloc] init] setPushNotificationPrivateKeyWithAccount:account data:nil];
+                    [[[NCKeychain alloc] init] setPushNotificationTokenWithAccount:account token:nil];
+                    [[[NCKeychain alloc] init] setPushNotificationDeviceIdentifierWithAccount:account deviceIdentifier:nil];
+                    [[[NCKeychain alloc] init] setPushNotificationDeviceIdentifierSignatureWithAccount:account deviceIdentifierSignature:nil];
+
                     if (self.pushKitToken != nil && subscribing) {
                         [self subscribingNextcloudServerPushNotification:account urlBase:urlBase user:user];
                     }

+ 1 - 1
iOSClient/PushNotification/NCPushNotificationEncryption.h

@@ -23,7 +23,7 @@
 //  This code derived from : Nextcloud Talk - NCSettingsController Created by Ivan Sein on 26.06.17. Copyright © 2017 struktur AG. All rights reserved.
 //
 
-#import <Foundation/Foundation.h>
+#import <UIKit/UIKit.h>
 
 @interface NCPushNotificationEncryption : NSObject
 

+ 4 - 4
iOSClient/PushNotification/NCPushNotificationEncryption.m

@@ -24,10 +24,9 @@
 //
 
 #import "NCPushNotificationEncryption.h"
-
+#import "NCBridgeSwift.h"
 #import <OpenSSL/OpenSSL.h>
 #import <CommonCrypto/CommonDigest.h>
-
 #import "NCEndToEndEncryption.h"
 #import "CCUtility.h"
 
@@ -75,7 +74,8 @@
     
     BIO_read(publicKeyBIO, keyBytes, len);
     NSData *ncPNPublicKey = [NSData dataWithBytes:keyBytes length:len];
-    [CCUtility setPushNotificationPublicKey:account data:ncPNPublicKey];
+
+    [[[NCKeychain alloc] init] setPushNotificationPublicKeyWithAccount:account data:ncPNPublicKey];
     NSLog(@"Push Notifications Key Pair generated: \n%@", [[NSString alloc] initWithData:ncPNPublicKey encoding:NSUTF8StringEncoding]);
     
     // PrivateKey
@@ -87,7 +87,7 @@
     
     BIO_read(privateKeyBIO, keyBytes, len);
     NSData *ncPNPrivateKey = [NSData dataWithBytes:keyBytes length:len];
-    [CCUtility setPushNotificationPrivateKey:account data:ncPNPrivateKey];
+    [[[NCKeychain alloc] init] setPushNotificationPrivateKeyWithAccount:account data:ncPNPrivateKey];
     
     RSA_free(rsa);
     BN_free(bigNumber);

+ 0 - 1
iOSClient/Settings/CCAdvanced.m

@@ -465,7 +465,6 @@
             [CCUtility removeDocumentsDirectory];
             [CCUtility removeTemporaryDirectory];
 
-            [CCUtility deleteAllChainStore];
             [[[NCKeychain alloc] init] removeAll];
 
             exit(0);

+ 0 - 1
iOSClient/Settings/NCSettingsBundleHelper.swift

@@ -47,7 +47,6 @@ class NCSettingsBundleHelper: NSObject {
             CCUtility.removeDocumentsDirectory()
             CCUtility.removeTemporaryDirectory()
 
-            CCUtility.deleteAllChainStore()
             NCKeychain().removeAll()
             NCManageDatabase.shared.removeDB()
 

+ 0 - 22
iOSClient/Utility/CCUtility.h

@@ -35,28 +35,6 @@
 
 @interface CCUtility : NSObject
 
-// ===== KeyChainStore =====
-
-// GET/SET
-
-+ (void)deleteAllChainStore;
-
-+ (void)setPushNotificationPublicKey:(NSString *)account data:(NSData *)data;
-+ (NSData *)getPushNotificationPublicKey:(NSString *)account;
-+ (void)setPushNotificationSubscribingPublicKey:(NSString *)account publicKey:(NSString *)publicKey;
-+ (NSString *)getPushNotificationSubscribingPublicKey:(NSString *)account;
-+ (void)setPushNotificationPrivateKey:(NSString *)account data:(NSData *)data;
-+ (NSData *)getPushNotificationPrivateKey:(NSString *)account;
-+ (void)setPushNotificationToken:(NSString *)account token:(NSString *)token;
-+ (NSString *)getPushNotificationToken:(NSString *)account;
-+ (void)setPushNotificationDeviceIdentifier:(NSString *)account deviceIdentifier:(NSString *)deviceIdentifier;
-+ (NSString *)getPushNotificationDeviceIdentifier:(NSString *)account;
-+ (void)setPushNotificationDeviceIdentifierSignature:(NSString *)account deviceIdentifierSignature:(NSString *)deviceIdentifierSignature;
-+ (NSString *)getPushNotificationDeviceIdentifierSignature:(NSString *)account;
-+ (void)clearAllKeysPushNotification:(NSString *)account;
-
-// ===== Varius =====
-
 + (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL;
 
 + (NSString *)dateDiff:(NSDate *)convertedDate;

+ 0 - 94
iOSClient/Utility/CCUtility.m

@@ -38,100 +38,6 @@
 
 @implementation CCUtility
 
-#pragma --------------------------------------------------------------------------------------------
-#pragma mark ======================= KeyChainStore ==================================
-#pragma --------------------------------------------------------------------------------------------
-
-+ (void)deleteAllChainStore
-{
-    [UICKeyChainStore removeAllItems];
-    [UICKeyChainStore removeAllItemsForService:NCGlobal.shared.serviceShareKeyChain];
-}
-
-#pragma ------------------------------ GET/SET
-
-+ (void)setPushNotificationPublicKey:(NSString *)account data:(NSData *)data
-{
-    NSString *key = [@"PNPublicKey" stringByAppendingString:account];
-    [UICKeyChainStore setData:data forKey:key service:NCGlobal.shared.serviceShareKeyChain];
-}
-
-+ (NSData *)getPushNotificationPublicKey:(NSString *)account
-{
-    NSString *key = [@"PNPublicKey" stringByAppendingString:account];
-    return [UICKeyChainStore dataForKey:key service:NCGlobal.shared.serviceShareKeyChain];
-}
-
-+ (void)setPushNotificationSubscribingPublicKey:(NSString *)account publicKey:(NSString *)publicKey
-{
-    NSString *key = [@"PNSubscribingPublicKey" stringByAppendingString:account];
-    [UICKeyChainStore setString:publicKey forKey:key service:NCGlobal.shared.serviceShareKeyChain];
-}
-
-+ (NSString *)getPushNotificationSubscribingPublicKey:(NSString *)account
-{
-    NSString *key = [@"PNSubscribingPublicKey" stringByAppendingString:account];
-    return [UICKeyChainStore stringForKey:key service:NCGlobal.shared.serviceShareKeyChain];
-}
-
-+ (void)setPushNotificationPrivateKey:(NSString *)account data:(NSData *)data
-{
-    NSString *key = [@"PNPrivateKey" stringByAppendingString:account];
-    [UICKeyChainStore setData:data forKey:key service:NCGlobal.shared.serviceShareKeyChain];
-}
-
-+ (NSData *)getPushNotificationPrivateKey:(NSString *)account
-{
-    NSString *key = [@"PNPrivateKey" stringByAppendingString:account];
-    return [UICKeyChainStore dataForKey:key service:NCGlobal.shared.serviceShareKeyChain];
-}
-
-+ (void)setPushNotificationToken:(NSString *)account token:(NSString *)token
-{
-    NSString *key = [@"PNToken" stringByAppendingString:account];
-    [UICKeyChainStore setString:token forKey:key service:NCGlobal.shared.serviceShareKeyChain];
-}
-
-+ (NSString *)getPushNotificationToken:(NSString *)account
-{
-    NSString *key = [@"PNToken" stringByAppendingString:account];
-    return [UICKeyChainStore stringForKey:key service:NCGlobal.shared.serviceShareKeyChain];
-}
-
-+ (void)setPushNotificationDeviceIdentifier:(NSString *)account deviceIdentifier:(NSString *)deviceIdentifier
-{
-    NSString *key = [@"PNDeviceIdentifier" stringByAppendingString:account];
-    [UICKeyChainStore setString:deviceIdentifier forKey:key service:NCGlobal.shared.serviceShareKeyChain];
-}
-
-+ (NSString *)getPushNotificationDeviceIdentifier:(NSString *)account
-{
-    NSString *key = [@"PNDeviceIdentifier" stringByAppendingString:account];
-    return [UICKeyChainStore stringForKey:key service:NCGlobal.shared.serviceShareKeyChain];
-}
-
-+ (void)setPushNotificationDeviceIdentifierSignature:(NSString *)account deviceIdentifierSignature:(NSString *)deviceIdentifierSignature
-{
-    NSString *key = [@"PNDeviceIdentifierSignature" stringByAppendingString:account];
-    [UICKeyChainStore setString:deviceIdentifierSignature forKey:key service:NCGlobal.shared.serviceShareKeyChain];
-}
-
-+ (NSString *)getPushNotificationDeviceIdentifierSignature:(NSString *)account
-{
-    NSString *key = [@"PNDeviceIdentifierSignature" stringByAppendingString:account];
-    return [UICKeyChainStore stringForKey:key service:NCGlobal.shared.serviceShareKeyChain];
-}
-
-+ (void)clearAllKeysPushNotification:(NSString *)account
-{
-    [self setPushNotificationPublicKey:account data:nil];
-    [self setPushNotificationSubscribingPublicKey:account publicKey:nil];
-    [self setPushNotificationPrivateKey:account data:nil];
-    [self setPushNotificationToken:account token:nil];
-    [self setPushNotificationDeviceIdentifier:account deviceIdentifier:nil];
-    [self setPushNotificationDeviceIdentifierSignature:account deviceIdentifierSignature:nil];
-}
-
 #pragma --------------------------------------------------------------------------------------------
 #pragma mark ===== Various =====
 #pragma --------------------------------------------------------------------------------------------

+ 71 - 1
iOSClient/Utility/NCKeychain.swift

@@ -428,7 +428,6 @@ import KeychainAccess
     }
 
     func isEndToEndEnabled(account: String) -> Bool {
-
         guard let certificate = getEndToEndCertificate(account: account), !certificate.isEmpty,
               let publicKey = getEndToEndPublicKey(account: account), !publicKey.isEmpty,
               let privateKey = getEndToEndPrivateKey(account: account), !privateKey.isEmpty,
@@ -443,4 +442,75 @@ import KeychainAccess
         setEndToEndPublicKey(account: account, publicKey: nil)
         setEndToEndPassphrase(account: account, passphrase: nil)
     }
+
+    // MARK: - PUSHNOTIFICATION
+
+    @objc func getPushNotificationPublicKey(account: String) -> Data? {
+        let key = "PNPublicKey" + account
+        return try? keychain.getData(key)
+    }
+
+    @objc func setPushNotificationPublicKey(account: String, data: Data?) {
+        let key = "PNPublicKey" + account
+        keychain[data: key] = data
+    }
+
+    @objc func getPushNotificationSubscribingPublicKey(account: String) -> String? {
+        let key = "PNSubscribingPublicKey" + account
+        return try? keychain.get(key)
+    }
+
+    @objc func setPushNotificationSubscribingPublicKey(account: String, publicKey: String?) {
+        let key = "PNSubscribingPublicKey" + account
+        keychain[key] = publicKey
+    }
+
+    @objc func getPushNotificationPrivateKey(account: String) -> Data? {
+        let key = "PNPrivateKey" + account
+        return try? keychain.getData(key)
+    }
+
+    @objc func setPushNotificationPrivateKey(account: String, data: Data?) {
+        let key = "PNPrivateKey" + account
+        keychain[data: key] = data
+    }
+
+    @objc func getPushNotificationToken(account: String) -> String? {
+        let key = "PNToken" + account
+        return try? keychain.get(key)
+    }
+
+    @objc func setPushNotificationToken(account: String, token: String?) {
+        let key = "PNToken" + account
+        keychain[key] = token
+    }
+
+    @objc func getPushNotificationDeviceIdentifier(account: String) -> String? {
+        let key = "PNDeviceIdentifier" + account
+        return try? keychain.get(key)
+    }
+
+    @objc func setPushNotificationDeviceIdentifier(account: String, deviceIdentifier: String?) {
+        let key = "PNDeviceIdentifier" + account
+        keychain[key] = deviceIdentifier
+    }
+
+    @objc func getPushNotificationDeviceIdentifierSignature(account: String) -> String? {
+        let key = "PNDeviceIdentifierSignature" + account
+        return try? keychain.get(key)
+    }
+
+    @objc func setPushNotificationDeviceIdentifierSignature(account: String, deviceIdentifierSignature: String?) {
+        let key = "PNDeviceIdentifierSignature" + account
+        keychain[key] = deviceIdentifierSignature
+    }
+
+    @objc func clearAllKeysPushNotification(account: String) {
+        setPushNotificationPublicKey(account: account, data: nil)
+        setPushNotificationSubscribingPublicKey(account: account, publicKey: nil)
+        setPushNotificationPrivateKey(account: account, data: nil)
+        setPushNotificationToken(account: account, token: nil)
+        setPushNotificationDeviceIdentifier(account: account, deviceIdentifier: nil)
+        setPushNotificationDeviceIdentifierSignature(account: account, deviceIdentifierSignature: nil)
+    }
 }

+ 0 - 1
iOSClient/Utility/NCUtility.swift

@@ -219,7 +219,6 @@ class NCUtility: NSObject {
 
         CCUtility.createDirectoryStandard()
 
-        CCUtility.deleteAllChainStore()
         NCKeychain().removeAll()
     }
 #endif