123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- #import "NCPushNotificationEncryption.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>
- #import <openssl/ssl.h>
- #import <CommonCrypto/CommonDigest.h>
- #import "NCEndToEndEncryption.h"
- #import "CCUtility.h"
- @implementation NCPushNotificationEncryption
- + (NCPushNotificationEncryption *)sharedInstance
- {
- static dispatch_once_t once;
- static NCPushNotificationEncryption *sharedInstance;
- dispatch_once(&once, ^{
- sharedInstance = [[self alloc] init];
- });
- return sharedInstance;
- }
- - (id)init
- {
- self = [super init];
- if (self) {
-
- }
- return self;
- }
- - (BOOL)generatePushNotificationsKeyPair:(NSString *)account
- {
- EVP_PKEY *pkey;
- NSError *keyError;
- pkey = [[NCEndToEndEncryption sharedManager] generateRSAKey:&keyError];
- if (keyError) {
- return NO;
- }
-
-
- int len;
- char *keyBytes;
-
-
- BIO *publicKeyBIO = BIO_new(BIO_s_mem());
- PEM_write_bio_PUBKEY(publicKeyBIO, pkey);
-
- len = BIO_pending(publicKeyBIO);
- keyBytes = malloc(len);
-
- BIO_read(publicKeyBIO, keyBytes, len);
- NSData *ncPNPublicKey = [NSData dataWithBytes:keyBytes length:len];
- [CCUtility setPushNotificationPublicKey:account data:ncPNPublicKey];
- NSLog(@"Push Notifications Key Pair generated: \n%@", [[NSString alloc] initWithData:ncPNPublicKey encoding:NSUTF8StringEncoding]);
-
-
- BIO *privateKeyBIO = BIO_new(BIO_s_mem());
- PEM_write_bio_PKCS8PrivateKey(privateKeyBIO, pkey, NULL, NULL, 0, NULL, NULL);
-
- len = BIO_pending(privateKeyBIO);
- keyBytes = malloc(len);
-
- BIO_read(privateKeyBIO, keyBytes, len);
- NSData *ncPNPrivateKey = [NSData dataWithBytes:keyBytes length:len];
- [CCUtility setPushNotificationPrivateKey:account data:ncPNPrivateKey];
-
- EVP_PKEY_free(pkey);
-
- return YES;
- }
- - (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];
-
-
- 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);
-
-
- 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
|