1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- /**
- * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: GPL-3.0-or-later
- */
- #import "NCPushNotificationsUtils.h"
- #import <openssl/rsa.h>
- #import <openssl/pem.h>
- #import <openssl/bio.h>
- #import <openssl/bn.h>
- #import <openssl/sha.h>
- #import <openssl/err.h>
- @implementation NCPushNotificationsUtils
- + (NSString *)decryptPushNotification:(NSString *)message withDevicePrivateKey:(NSData *)privateKey
- {
- NSString *privateKeyString = [[NSString alloc] initWithData:privateKey encoding:NSUTF8StringEncoding];
- NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:message options:0];
- char *privKey = (char *)[privateKeyString UTF8String];
-
- // Get Device Private Key from PEM
- BIO *bio = BIO_new(BIO_s_mem());
- BIO_write(bio, privKey, (int)strlen(privKey));
-
- EVP_PKEY* pkey = 0;
- PEM_read_bio_PrivateKey(bio, &pkey, 0, 0);
-
- RSA* rsa = EVP_PKEY_get1_RSA(pkey);
-
- // Decrypt the message
- unsigned char *decrypted = (unsigned char *) malloc(4096);
-
- int decrypted_length = RSA_private_decrypt((int)[decodedData length], [decodedData bytes], decrypted, rsa, RSA_PKCS1_PADDING);
- if(decrypted_length == -1) {
- char buffer[500];
- ERR_error_string(ERR_get_error(), buffer);
- NSLog(@"%@",[NSString stringWithUTF8String:buffer]);
- return nil;
- }
-
- NSString *decryptString = [[NSString alloc] initWithBytes:decrypted length:decrypted_length encoding:NSUTF8StringEncoding];
-
- if (decrypted)
- free(decrypted);
- free(bio);
- free(rsa);
-
- return decryptString;
- }
- @end
|