12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454 |
- //
- // NCEndToEndEncryption.m
- // Nextcloud
- //
- // Created by Marino Faggiana on 19/09/17.
- // Copyright © 2017 Marino Faggiana. All rights reserved.
- //
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- #import "NCEndToEndEncryption.h"
- #import "NCBridgeSwift.h"
- #import "CCUtility.h"
- #import <CommonCrypto/CommonDigest.h>
- #import <CommonCrypto/CommonKeyDerivation.h>
- #import <OpenSSL/OpenSSL.h>
- #define addName(field, value) X509_NAME_add_entry_by_txt(name, field, MBSTRING_ASC, (unsigned char *)value, -1, -1, 0); NSLog(@"%s: %s", field, value);
- #define IV_DELIMITER_ENCODED_OLD @"fA=="
- #define IV_DELIMITER_ENCODED @"|"
- #define PBKDF2_KEY_LENGTH 256
- //#define PBKDF2_SALT @"$4$YmBjm3hk$Qb74D5IUYwghUmzsMqeNFx5z0/8$"
- #define ASYMMETRIC_STRING_TEST @"Nextcloud a safe home for all your data"
- #define fileNameCertificate @"cert.pem"
- #define fileNameCSR @"csr.pem"
- #define fileNamePrivateKey @"privateKey.pem"
- #define fileNamePubliceKey @"publicKey.pem"
- #define streamBuffer 1024
- #define AES_KEY_128_LENGTH 16
- #define AES_KEY_256_LENGTH 32
- #define AES_IVEC_LENGTH 16
- #define AES_GCM_TAG_LENGTH 16
- #define AES_SALT_LENGTH 40
- @interface NCEndToEndEncryption ()
- {
- NSData *_privateKeyData;
- NSData *_publicKeyData;
- NSData *_csrData;
- }
- @end
- @implementation NCEndToEndEncryption
- //Singleton
- + (instancetype)sharedManager {
- static NCEndToEndEncryption *NCEndToEndEncryption = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- NCEndToEndEncryption = [self new];
- });
- return NCEndToEndEncryption;
- }
- #
- #pragma mark - Generate Certificate X509 - CSR - Private Key
- #
- - (BOOL)generateCertificateX509WithUserId:(NSString *)userId directory:(NSString *)directory
- {
- OPENSSL_init();
-
- int ret;
- EVP_PKEY * pkey;
- pkey = EVP_PKEY_new();
- RSA * rsa;
- BIGNUM *bignum = BN_new();
- ret = BN_set_word(bignum, RSA_F4);
- if (ret != 1) {
- return NO;
- }
- rsa = RSA_new();
- ret = RSA_generate_key_ex(rsa, 2048, bignum, NULL);
- if (ret != 1) {
- return NO;
- }
-
- EVP_PKEY_assign_RSA(pkey, rsa);
-
- X509 * x509;
- x509 = X509_new();
-
- ASN1_INTEGER_set(X509_get_serialNumber(x509), 1);
-
- long notBefore = [[NSDate date] timeIntervalSinceDate:[NSDate date]];
- long notAfter = [[[NSDate date] dateByAddingTimeInterval:60*60*24*365*10] timeIntervalSinceDate:[NSDate date]]; // 10 year
- X509_gmtime_adj(X509_get_notBefore(x509), notBefore);
- X509_gmtime_adj(X509_get_notAfter(x509), notAfter);
-
- X509_set_pubkey(x509, pkey);
-
- X509_NAME * name;
- name = X509_get_subject_name(x509);
-
- // Now to add the subject name fields to the certificate
- // I use a macro here to make it cleaner.
-
- const unsigned char *cUserId = (const unsigned char *) [userId cStringUsingEncoding:NSUTF8StringEncoding];
- // Common Name = UserID.
- addName("CN", cUserId);
-
- // The organizational unit for the cert. Usually this is a department.
- addName("OU", "Certificate Authority");
-
- // The organization of the cert.
- addName("O", "Nextcloud");
-
- // The city of the organization.
- addName("L", "Vicenza");
-
- // The state/province of the organization.
- addName("S", "Italy");
-
- // The country (ISO 3166) of the organization
- addName("C", "IT");
-
- X509_set_issuer_name(x509, name);
-
- /*
- for (SANObject * san in self.options.sans) {
- if (!san.value || san.value.length <= 0) {
- continue;
- }
-
- NSString * prefix = san.type == SANObjectTypeIP ? @"IP:" : @"DNS:";
- NSString * value = [NSString stringWithFormat:@"%@%@", prefix, san.value];
- NSLog(@"Add subjectAltName %@", value);
-
- X509_EXTENSION * extension = NULL;
- ASN1_STRING * asnValue = ASN1_STRING_new();
- ASN1_STRING_set(asnValue, (const unsigned char *)[value UTF8String], (int)value.length);
- X509_EXTENSION_create_by_NID(&extension, NID_subject_alt_name, 0, asnValue);
- X509_add_ext(x509, extension, -1);
- }
- */
-
- // Specify the encryption algorithm of the signature.
- // SHA256 should suit your needs.
- if (X509_sign(x509, pkey, EVP_sha256()) < 0) {
- return NO;
- }
-
- X509_print_fp(stdout, x509);
-
- // Extract CSR, publicKey, privateKey
- int len;
- char *keyBytes;
-
- // CSR
- BIO *csrBIO = BIO_new(BIO_s_mem());
- X509_REQ *certReq = X509_to_X509_REQ(x509, pkey, EVP_sha256());
- PEM_write_bio_X509_REQ(csrBIO, certReq);
-
- len = BIO_pending(csrBIO);
- keyBytes = malloc(len);
-
- BIO_read(csrBIO, keyBytes, len);
- _csrData = [NSData dataWithBytes:keyBytes length:len];
- NSLog(@"[LOG] \n%@", [[NSString alloc] initWithData:_csrData encoding:NSUTF8StringEncoding]);
-
- // PublicKey
- 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);
- _publicKeyData = [NSData dataWithBytes:keyBytes length:len];
- self.generatedPublicKey = [[NSString alloc] initWithData:_publicKeyData encoding:NSUTF8StringEncoding];
- NSLog(@"[LOG] \n%@", self.generatedPublicKey);
-
- // PrivateKey
- 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);
- _privateKeyData = [NSData dataWithBytes:keyBytes length:len];
- self.generatedPrivateKey = [[NSString alloc] initWithData:_privateKeyData encoding:NSUTF8StringEncoding];
- NSLog(@"[LOG] \n%@", self.generatedPrivateKey);
-
- if(keyBytes)
- free(keyBytes);
-
- #ifdef DEBUG
- // Save to disk [DEBUG MODE]
- [self saveToDiskPEMWithCert:x509 key:pkey directory:directory];
- #endif
-
- return YES;
- }
- - (NSString *)extractPublicKeyFromCertificate:(NSString *)pemCertificate
- {
- const char *ptrCert = [pemCertificate cStringUsingEncoding:NSUTF8StringEncoding];
-
- BIO *certBio = BIO_new(BIO_s_mem());
- BIO_write(certBio, ptrCert,(unsigned int)strlen(ptrCert));
-
- X509 *certX509 = PEM_read_bio_X509(certBio, NULL, NULL, NULL);
- if (!certX509) {
- fprintf(stderr, "unable to parse certificate in memory\n");
- return nil;
- }
-
- EVP_PKEY *pkey;
- pkey = X509_get_pubkey(certX509);
- NSString *publicKey = [self pubKeyToString:pkey];
-
- EVP_PKEY_free(pkey);
- BIO_free(certBio);
- X509_free(certX509);
-
- NSLog(@"[LOG] \n%@", publicKey);
- return publicKey;
- }
- - (BOOL)saveToDiskPEMWithCert:(X509 *)x509 key:(EVP_PKEY *)pkey directory:(NSString *)directory
- {
- FILE *f;
-
- // Certificate
- NSString *certificatePath = [NSString stringWithFormat:@"%@/%@", directory, fileNameCertificate];
- f = fopen([certificatePath fileSystemRepresentation], "wb");
- if (PEM_write_X509(f, x509) < 0) {
- // Error writing to disk.
- fclose(f);
- return NO;
- }
- NSLog(@"[LOG] Saved cert to %@", certificatePath);
- fclose(f);
-
- // PublicKey
- NSString *publicKeyPath = [NSString stringWithFormat:@"%@/%@", directory, fileNamePubliceKey];
- f = fopen([publicKeyPath fileSystemRepresentation], "wb");
- if (PEM_write_PUBKEY(f, pkey) < 0) {
- // Error
- fclose(f);
- return NO;
- }
- NSLog(@"[LOG] Saved publicKey to %@", publicKeyPath);
- fclose(f);
-
- // Here you write the private key (pkey) to disk. OpenSSL will encrypt the
- // file using the password and cipher you provide.
- //if (PEM_write_PrivateKey(f, pkey, EVP_des_ede3_cbc(), (unsigned char *)[password UTF8String], (int)password.length, NULL, NULL) < 0) {
-
- // PrivateKey
- NSString *privatekeyPath = [NSString stringWithFormat:@"%@/%@", directory, fileNamePrivateKey];
- f = fopen([privatekeyPath fileSystemRepresentation], "wb");
- if (PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL) < 0) {
- // Error
- fclose(f);
- return NO;
- }
- NSLog(@"[LOG] Saved privatekey to %@", privatekeyPath);
- fclose(f);
-
- // CSR Request sha256
- NSString *csrPath = [NSString stringWithFormat:@"%@/%@", directory, fileNameCSR];
- f = fopen([csrPath fileSystemRepresentation], "wb");
- X509_REQ *certreq = X509_to_X509_REQ(x509, pkey, EVP_sha256());
- if (PEM_write_X509_REQ(f, certreq) < 0) {
- // Error
- fclose(f);
- return NO;
- }
- NSLog(@"[LOG] Saved csr to %@", csrPath);
- fclose(f);
-
- return YES;
- }
- - (BOOL)saveP12WithCert:(X509 *)x509 key:(EVP_PKEY *)pkey directory:(NSString *)directory finished:(void (^)(NSError *))finished
- {
- //PKCS12 * p12 = PKCS12_create([password UTF8String], NULL, pkey, x509, NULL, 0, 0, PKCS12_DEFAULT_ITER, 1, NID_key_usage);
- PKCS12 *p12 = PKCS12_create(NULL, NULL, pkey, x509, NULL, 0, 0, PKCS12_DEFAULT_ITER, 1, NID_key_usage);
-
- NSString *path = [NSString stringWithFormat:@"%@/certificate.p12", directory];
-
- FILE *f = fopen([path fileSystemRepresentation], "wb");
-
- if (i2d_PKCS12_fp(f, p12) != 1) {
- fclose(f);
- return NO;
- }
- NSLog(@"[LOG] Saved p12 to %@", path);
- fclose(f);
-
- return YES;
- }
- #
- #pragma mark - Create CSR & Encrypt/Decrypt Private Key
- #
- - (NSString *)createCSR:(NSString *)userId directory:(NSString *)directory
- {
- // Create Certificate, if do not exists
- if (!_csrData) {
- if (![self generateCertificateX509WithUserId:userId directory:directory])
- return nil;
- }
-
- NSString *csr = [[NSString alloc] initWithData:_csrData encoding:NSUTF8StringEncoding];
-
- return csr;
- }
- - (NSString *)encryptPrivateKey:(NSString *)userId directory:(NSString *)directory passphrase:(NSString *)passphrase privateKey:(NSString **)privateKey iterationCount:(unsigned int)iterationCount
- {
- NSMutableData *cipher = [NSMutableData new];
- if (!_privateKeyData) {
- if (![self generateCertificateX509WithUserId:userId directory:directory])
- return nil;
- }
-
- NSMutableData *key = [NSMutableData dataWithLength:PBKDF2_KEY_LENGTH/8];
- NSData *salt = [self generateSalt:AES_SALT_LENGTH];
-
- // Remove all whitespaces from passphrase
- passphrase = [passphrase stringByReplacingOccurrencesOfString:@" " withString:@""];
-
- CCKeyDerivationPBKDF(kCCPBKDF2, passphrase.UTF8String, passphrase.length, salt.bytes, salt.length, kCCPRFHmacAlgSHA1, iterationCount, key.mutableBytes, key.length);
-
- NSData *initializationVector = [self generateIV:AES_IVEC_LENGTH];
- NSData *authenticationTag = [NSData new];
-
- NSString *pkEncoded = [_privateKeyData base64EncodedStringWithOptions:0];
- NSData *pkEncodedData = [pkEncoded dataUsingEncoding:NSUTF8StringEncoding];
- BOOL result = [self encryptData:pkEncodedData cipher:&cipher key:key keyLen:AES_KEY_256_LENGTH initializationVector:initializationVector authenticationTag:&authenticationTag];
-
- if (result && cipher) {
-
- NSString *cipherString = [cipher base64EncodedStringWithOptions:0];
- NSString *initializationVectorString = [initializationVector base64EncodedStringWithOptions:0];
- NSString *saltString = [salt base64EncodedStringWithOptions:0];
- NSString *encryptPrivateKey = [NSString stringWithFormat:@"%@%@%@%@%@", cipherString, IV_DELIMITER_ENCODED, initializationVectorString, IV_DELIMITER_ENCODED, saltString];
-
- *privateKey = [[NSString alloc] initWithData:_privateKeyData encoding:NSUTF8StringEncoding];
- return encryptPrivateKey;
-
- } else {
-
- return nil;
- }
- }
- - (NSData *)decryptPrivateKey:(NSString *)privateKey passphrase:(NSString *)passphrase publicKey:(NSString *)publicKey iterationCount:(unsigned int)iterationCount
- {
- NSMutableData *plain = [NSMutableData new];
- // Key
- NSMutableData *key = [NSMutableData dataWithLength:PBKDF2_KEY_LENGTH/8];
-
- // Split
- NSArray *cipherArray = [privateKey componentsSeparatedByString:IV_DELIMITER_ENCODED];
- if (cipherArray.count != 3) {
- cipherArray = [privateKey componentsSeparatedByString:IV_DELIMITER_ENCODED_OLD];
- if (cipherArray.count != 3) {
- return nil;
- }
- }
-
- NSData *cipher = [[NSData alloc] initWithBase64EncodedString:cipherArray[0] options:0];
- NSString *authenticationTagString = [privateKey substringWithRange:NSMakeRange([(NSString *)cipherArray[0] length] - AES_GCM_TAG_LENGTH, AES_GCM_TAG_LENGTH)];
- NSData *authenticationTag = [[NSData alloc] initWithBase64EncodedString:authenticationTagString options:0];
- NSData *initializationVector = [[NSData alloc] initWithBase64EncodedString:cipherArray[1] options:0];
- NSData *salt = [[NSData alloc] initWithBase64EncodedString:cipherArray[2] options:0];
- // Remove Authentication Tag
- cipher = [cipher subdataWithRange:NSMakeRange(0, cipher.length - AES_GCM_TAG_LENGTH)];
- // Remove all whitespaces from passphrase
- passphrase = [passphrase stringByReplacingOccurrencesOfString:@" " withString:@""];
-
- CCKeyDerivationPBKDF(kCCPBKDF2, passphrase.UTF8String, passphrase.length, salt.bytes, salt.length, kCCPRFHmacAlgSHA1, iterationCount, key.mutableBytes, key.length);
-
- BOOL result = [self decryptData:cipher plain:&plain key:key keyLen:AES_KEY_256_LENGTH initializationVector:initializationVector authenticationTag:authenticationTag];
-
- if (result && plain) {
- return plain;
- }
- return nil;
- }
- #
- #pragma mark - Encrypt / Decrypt file material
- #
- - (NSString *)encryptPayloadFile:(NSData *)encrypted key:(NSString *)key
- {
- NSMutableData *cipher;
- NSData *authenticationTag = [NSData new];
- // Key
- NSData *keyData = [[NSData alloc] initWithBase64EncodedString:key options:0];
- // Initialization Vector
- NSData *initializationVector = [self generateIV:AES_IVEC_LENGTH];
- BOOL result = [self encryptData:encrypted cipher:&cipher key:keyData keyLen:AES_KEY_128_LENGTH initializationVector:initializationVector authenticationTag:&authenticationTag];
- if (cipher != nil && result) {
- NSString *cipherString = [cipher base64EncodedStringWithOptions:0];
- NSString *initializationVectorString = [initializationVector base64EncodedStringWithOptions:0];
- NSString *payload = [NSString stringWithFormat:@"%@%@%@", cipherString, IV_DELIMITER_ENCODED, initializationVectorString];
- return payload;
- }
- return nil;
- }
- - (NSString *)encryptPayloadFile:(NSData *)encrypted key:(NSString *)key initializationVector:(NSString **)initializationVector authenticationTag:(NSString **)authenticationTag
- {
- NSMutableData *cipher;
- NSData *authenticationTagData = [NSData new];
- // Key
- NSData *keyData = [[NSData alloc] initWithBase64EncodedString:key options:0];
- // Initialization Vector
- NSData *initializationVectorData = [self generateIV:AES_IVEC_LENGTH];
- BOOL result = [self encryptData:encrypted cipher:&cipher key:keyData keyLen:AES_KEY_128_LENGTH initializationVector:initializationVectorData authenticationTag:&authenticationTagData];
- if (cipher != nil && result) {
- *initializationVector = [initializationVectorData base64EncodedStringWithOptions:0];
- *authenticationTag = [authenticationTagData base64EncodedStringWithOptions:0];
- NSString *payload = [cipher base64EncodedStringWithOptions:0];
-
- return payload;
- }
- return nil;
- }
- - (NSData *)decryptPayloadFile:(NSString *)encrypted key:(NSString *)key
- {
- NSMutableData *plain;
- NSRange range = [encrypted rangeOfString:IV_DELIMITER_ENCODED];
- if (range.location == NSNotFound) {
- range = [encrypted rangeOfString:IV_DELIMITER_ENCODED_OLD];
- if (range.location == NSNotFound) {
- return nil;
- }
- }
- // Cipher
- NSString *cipher = [encrypted substringToIndex:(range.location)];
- NSData *cipherData = [[NSData alloc] initWithBase64EncodedString:cipher options:0];
- // Key
- NSData *keyData = [[NSData alloc] initWithBase64EncodedString:key options:0];
- // Initialization Vector
- NSString *initializationVector = [encrypted substringWithRange:NSMakeRange(range.location + range.length, encrypted.length - (range.location + range.length))];
- NSData *initializationVectorData = [[NSData alloc] initWithBase64EncodedString:initializationVector options:0];
- // Authentication Tag
- NSString *authenticationTag = [cipher substringWithRange:NSMakeRange(cipher.length - AES_GCM_TAG_LENGTH, AES_GCM_TAG_LENGTH)];
- NSData *authenticationTagData = [[NSData alloc] initWithBase64EncodedString:authenticationTag options:0];
- // Remove Authentication Tag
- cipherData = [cipherData subdataWithRange:NSMakeRange(0, cipherData.length - AES_GCM_TAG_LENGTH)];
- BOOL result = [self decryptData:cipherData plain:&plain key:keyData keyLen:AES_KEY_128_LENGTH initializationVector:initializationVectorData authenticationTag:authenticationTagData];
- if (plain != nil && result) {
- return plain;
- }
- return nil;
- }
- - (NSData *)decryptPayloadFile:(NSString *)encrypted key:(NSString *)key initializationVector:(NSString *)initializationVector authenticationTag:(NSString *)authenticationTag
- {
- NSMutableData *plain;
- // Remove initializationVector Tag if exists [ANDROID]
- NSString *android = [@"|" stringByAppendingString: initializationVector];
- encrypted = [encrypted stringByReplacingOccurrencesOfString:android withString:@""];
- NSData *cipher = [[NSData alloc] initWithBase64EncodedString:encrypted options:0];
- NSData *keyData = [[NSData alloc] initWithBase64EncodedString:key options:0];
- NSData *initializationVectorData = [[NSData alloc] initWithBase64EncodedString:initializationVector options:0];
- NSData *authenticationTagData = [[NSData alloc] initWithBase64EncodedString:authenticationTag options:0];
- // Remove Authentication Tag
- cipher = [cipher subdataWithRange:NSMakeRange(0, cipher.length - AES_GCM_TAG_LENGTH)];
- BOOL result = [self decryptData:cipher plain:&plain key:keyData keyLen:AES_KEY_128_LENGTH initializationVector:initializationVectorData authenticationTag:authenticationTagData];
- if (plain != nil && result && plain.length > 0) {
- return plain;
- }
- return nil;
- }
- #
- #pragma mark - Encrypt / Decrypt file
- #
- - (BOOL)encryptFile:(NSString *)fileName fileNameIdentifier:(NSString *)fileNameIdentifier directory:(NSString *)directory key:(NSString **)key initializationVector:(NSString **)initializationVector authenticationTag:(NSString **)authenticationTag
- {
- NSData *authenticationTagData;
- NSData *keyData = [self generateKey:AES_KEY_128_LENGTH];
- NSData *initializationVectorData = [self generateIV:AES_IVEC_LENGTH];
- BOOL result = [self encryptFile:[NSString stringWithFormat:@"%@/%@", directory, fileName] fileNameCipher:[NSString stringWithFormat:@"%@/%@", directory, fileNameIdentifier] key:keyData keyLen:AES_KEY_128_LENGTH initializationVector:initializationVectorData authenticationTag:&authenticationTagData];
- if (result) {
- *key = [keyData base64EncodedStringWithOptions:0];
- *initializationVector = [initializationVectorData base64EncodedStringWithOptions:0];
- *authenticationTag = [authenticationTagData base64EncodedStringWithOptions:0];
- if (key == nil || initializationVector == nil || authenticationTag == nil) {
- return false;
- } else {
- return true;
- }
- }
-
- return false;
- }
- - (BOOL)decryptFile:(NSString *)fileName fileNameView:(NSString *)fileNameView ocId:(NSString *)ocId key:(NSString *)key initializationVector:(NSString *)initializationVector authenticationTag:(NSString *)authenticationTag
- {
- NSData *keyData = [[NSData alloc] initWithBase64EncodedString:key options:0];
- NSData *initializationVectorData = [[NSData alloc] initWithBase64EncodedString:initializationVector options:0];
- NSData *authenticationTagData = [[NSData alloc] initWithBase64EncodedString:authenticationTag options:0];
- return [self decryptFile:[CCUtility getDirectoryProviderStorageOcId:ocId fileNameView:fileName] fileNamePlain:[CCUtility getDirectoryProviderStorageOcId:ocId fileNameView:fileNameView] key:keyData keyLen:AES_KEY_128_LENGTH initializationVector:initializationVectorData authenticationTag:authenticationTagData];
- }
- // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
- // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
- #
- #pragma mark - OPENSSL ENCRYPT/DECRYPT
- #
- #
- #pragma mark - Encrypt/Decrypt asymmetric
- #
- - (NSData *)encryptAsymmetricData:(NSData *)plainData certificate:(NSString *)certificate
- {
- EVP_PKEY *key = NULL;
- int status = 0;
- unsigned char *pKey = (unsigned char *)[certificate UTF8String];
- // Extract real publicKey
- BIO *bio = BIO_new_mem_buf(pKey, -1);
- if (!bio)
- return nil;
- X509 *x509 = PEM_read_bio_X509(bio, NULL, 0, NULL);
- if (!x509)
- return nil;
- key = X509_get_pubkey(x509);
- if (!key)
- return nil;
- EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(key, NULL);
- if (!ctx)
- return nil;
- status = EVP_PKEY_encrypt_init(ctx);
- if (status <= 0)
- return nil;
- status = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING);
- if (status <= 0)
- return nil;
- status = EVP_PKEY_CTX_set_rsa_oaep_md(ctx, EVP_sha256());
- if (status <= 0)
- return nil;
- status = EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, EVP_sha256());
- if (status <= 0)
- return nil;
- unsigned long outLen = 0;
- status = EVP_PKEY_encrypt(ctx, NULL, &outLen, [plainData bytes], (int)[plainData length]);
- if (status <= 0 || outLen == 0)
- return nil;
- unsigned char *out = (unsigned char *) malloc(outLen);
- status = EVP_PKEY_encrypt(ctx, out, &outLen, [plainData bytes], (int)[plainData length]);
- if (status <= 0)
- return nil;
- NSData *outData = [[NSData alloc] initWithBytes:out length:outLen];
- if (out)
- free(out);
- return outData;
- }
- - (NSData *)encryptAsymmetricData:(NSData *)plainData privateKey:(NSString *)privateKey
- {
- EVP_PKEY *key = NULL;
- int status = 0;
- unsigned char *pKey = (unsigned char *)[privateKey UTF8String];
- BIO *bio = BIO_new_mem_buf(pKey, -1);
- if (!bio)
- return nil;
- key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
- if (!key)
- return nil;
- EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(key, NULL);
- if (!ctx)
- return nil;
- status = EVP_PKEY_encrypt_init(ctx);
- if (status <= 0)
- return nil;
- status = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING);
- if (status <= 0)
- return nil;
- status = EVP_PKEY_CTX_set_rsa_oaep_md(ctx, EVP_sha256());
- if (status <= 0)
- return nil;
- status = EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, EVP_sha256());
- if (status <= 0)
- return nil;
- unsigned long outLen = 0;
- status = EVP_PKEY_encrypt(ctx, NULL, &outLen, [plainData bytes], (int)[plainData length]);
- if (status <= 0 || outLen == 0)
- return nil;
- unsigned char *out = (unsigned char *) malloc(outLen);
- status = EVP_PKEY_encrypt(ctx, out, &outLen, [plainData bytes], (int)[plainData length]);
- if (status <= 0)
- return nil;
- NSData *outData = [[NSData alloc] initWithBytes:out length:outLen];
- if (out)
- free(out);
- return outData;
- }
- - (NSData *)decryptAsymmetricData:(NSData *)cipherData privateKey:(NSString *)privateKey
- {
- unsigned char *pKey = (unsigned char *)[privateKey UTF8String];
- int status = 0;
-
- BIO *bio = BIO_new_mem_buf(pKey, -1);
- if (!bio)
- return nil;
-
- EVP_PKEY *key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
- if (!key)
- return nil;
-
- EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(key, NULL);
- if (!ctx)
- return nil;
-
- status = EVP_PKEY_decrypt_init(ctx);
- if (status <= 0)
- return nil;
-
- status = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING);
- if (status <= 0)
- return nil;
-
- status = EVP_PKEY_CTX_set_rsa_oaep_md(ctx, EVP_sha256());
- if (status <= 0)
- return nil;
-
- status = EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, EVP_sha256());
- if (status <= 0)
- return nil;
-
- unsigned long outLen = 0;
- status = EVP_PKEY_decrypt(ctx, NULL, &outLen, [cipherData bytes], (int)[cipherData length]);
- if (status <= 0 || outLen == 0)
- return nil;
-
- unsigned char *out = (unsigned char *) malloc(outLen);
- status = EVP_PKEY_decrypt(ctx, out, &outLen, [cipherData bytes], (int)[cipherData length]);
- if (status <= 0)
- return nil;
- NSData *outData = [[NSData alloc] initWithBytes:out length:outLen];
- if (out)
- free(out);
-
- return outData;
- }
- #
- #pragma mark - AES/GCM/NoPadding
- #
- // Encryption data using GCM mode
- - (BOOL)encryptData:(NSData *)plain cipher:(NSMutableData **)cipher key:(NSData *)key keyLen:(int)keyLen initializationVector:(NSData *)initializationVector authenticationTag:(NSData **)authenticationTag
- {
- int status = 0;
- int len = 0;
-
- // set up key
- len = keyLen;
- unsigned char cKey[len];
- bzero(cKey, sizeof(cKey));
- [key getBytes:cKey length:len];
- // set up ivec
- len = AES_IVEC_LENGTH;
- unsigned char cIV[len];
- bzero(cIV, sizeof(cIV));
- [initializationVector getBytes:cIV length:len];
-
- // set up tag
- len = AES_GCM_TAG_LENGTH;
- unsigned char cTag[len];
- bzero(cTag, sizeof(cTag));
-
- // Create and initialise the context
- EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
- if (!ctx)
- return NO;
-
- // Initialise the encryption operation
- if (keyLen == AES_KEY_128_LENGTH)
- status = EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
- else if (keyLen == AES_KEY_256_LENGTH)
- status = EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
-
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
-
- // Set IV length. Not necessary if this is 12 bytes (96 bits)
- status = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int)sizeof(cIV), NULL);
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
-
- // Initialise key and IV
- status = EVP_EncryptInit_ex (ctx, NULL, NULL, cKey, cIV);
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
-
- // Provide the message to be encrypted, and obtain the encrypted output
- *cipher = [NSMutableData dataWithLength:[plain length]];
- unsigned char * cCipher = [*cipher mutableBytes];
- int cCipherLen = 0;
- status = EVP_EncryptUpdate(ctx, cCipher, &cCipherLen, [plain bytes], (int)[plain length]);
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
-
- // Finalise the encryption
- status = EVP_EncryptFinal_ex(ctx, cCipher, &cCipherLen);
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
-
- // Get the tag
- status = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, (int)sizeof(cTag), cTag);
- *authenticationTag = [NSData dataWithBytes:cTag length:sizeof(cTag)];
- // Append TAG
- [*cipher appendData:*authenticationTag];
- EVP_CIPHER_CTX_free(ctx);
-
- return status; // OpenSSL uses 1 for success
- }
- // Encryption file using GCM mode
- - (BOOL)encryptFile:(NSString *)fileName fileNameCipher:(NSString *)fileNameCipher key:(NSData *)key keyLen:(int)keyLen initializationVector:(NSData *)initializationVector authenticationTag:(NSData **)authenticationTag
- {
- int status = 0;
- int len = 0;
- // set up key
- len = keyLen;
- unsigned char cKey[len];
- bzero(cKey, sizeof(cKey));
- [key getBytes:cKey length:len];
- // set up ivec
- len = AES_IVEC_LENGTH;
- unsigned char cIV[len];
- bzero(cIV, sizeof(cIV));
- [initializationVector getBytes:cIV length:len];
- // set up tag
- len = AES_GCM_TAG_LENGTH;
- unsigned char cTag[len];
- bzero(cTag, sizeof(cTag));
- // Create and initialise the context
- EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
- if (!ctx) {
- return NO;
- }
- // Initialise the encryption operation
- if (keyLen == AES_KEY_128_LENGTH)
- status = EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
- else if (keyLen == AES_KEY_256_LENGTH)
- status = EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
- // Set IV length. Not necessary if this is 12 bytes (96 bits)
- status = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int)sizeof(cIV), NULL);
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
- // Initialise key and IV
- status = EVP_EncryptInit_ex (ctx, NULL, NULL, cKey, cIV);
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
- NSInputStream *inStream = [NSInputStream inputStreamWithFileAtPath:fileName];
- [inStream open];
- NSOutputStream *outStream = [NSOutputStream outputStreamToFileAtPath:fileNameCipher append:false];
- [outStream open];
- Byte buffer[streamBuffer];
- NSInteger totalNumberOfBytesWritten = 0;
- int cCipherLen = 0;
- unsigned char *cCipher;
- while ([inStream hasBytesAvailable]) {
- @autoreleasepool {
- NSInteger bytesRead = [inStream read:buffer maxLength:streamBuffer];
- if (bytesRead > 0) {
- cCipher = [[NSMutableData dataWithLength:bytesRead] mutableBytes];
- status = EVP_EncryptUpdate(ctx, cCipher, &cCipherLen, [[NSData dataWithBytes:buffer length:bytesRead] bytes], (int)bytesRead);
- if (status <= 0) {
- [inStream close];
- [outStream close];
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
- if ([outStream hasSpaceAvailable]) {
- totalNumberOfBytesWritten = [outStream write:cCipher maxLength:cCipherLen];
- if (totalNumberOfBytesWritten != cCipherLen) {
- [inStream close];
- [outStream close];
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
- }
- }
- }
- }
- [inStream close];
- status = EVP_EncryptFinal_ex(ctx, cCipher, &cCipherLen);
- if (status <= 0) {
- [outStream close];
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
- // Get the tag
- status = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, (int)sizeof(cTag), cTag);
- if (status <= 0) {
- [outStream close];
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
- *authenticationTag = [NSData dataWithBytes:cTag length:sizeof(cTag)];
- // Append TAG
- if ([outStream hasSpaceAvailable]) {
- totalNumberOfBytesWritten = [outStream write:cTag maxLength:sizeof(cTag)];
- if (totalNumberOfBytesWritten != sizeof(cTag)) {
- status = NO;
- }
- } else {
- status = NO;
- }
- [outStream close];
- EVP_CIPHER_CTX_free(ctx);
- return status; // OpenSSL uses 1 for success
- }
- // Decryption data using GCM mode
- - (BOOL)decryptData:(NSData *)cipher plain:(NSMutableData **)plain key:(NSData *)key keyLen:(int)keyLen initializationVector:(NSData *)initializationVector authenticationTag:(NSData *)authenticationTag
- {
- int status = 0;
- int len = 0;
-
- // set up key
- len = keyLen;
- unsigned char cKey[len];
- bzero(cKey, sizeof(cKey));
- [key getBytes:cKey length:len];
-
- // set up ivec
- len = (int)[initializationVector length];
- unsigned char cIV[len];
- bzero(cIV, sizeof(cIV));
- [initializationVector getBytes:cIV length:len];
-
- // set up tag
- len = (int)[authenticationTag length];;
- unsigned char cTag[len];
- bzero(cTag, sizeof(cTag));
- [authenticationTag getBytes:cTag length:len];
- // Create and initialise the context
- EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
- if (!ctx)
- return NO;
-
- // Initialise the decryption operation
- if (keyLen == AES_KEY_128_LENGTH)
- status = EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
- else if (keyLen == AES_KEY_256_LENGTH)
- status = EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
-
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
-
- // Set IV length. Not necessary if this is 12 bytes (96 bits)
- status = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int)sizeof(cIV), NULL);
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
-
- // Initialise key and IV
- status = EVP_DecryptInit_ex(ctx, NULL, NULL, cKey, cIV);
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
-
- // Provide the message to be decrypted, and obtain the plaintext output
- *plain = [NSMutableData dataWithLength:([cipher length])];
- int cPlainLen = 0;
- unsigned char * cPlain = [*plain mutableBytes];
- status = EVP_DecryptUpdate(ctx, cPlain, &cPlainLen, [cipher bytes], (int)([cipher length]));
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
-
- // Tag is the last 16 bytes
- status = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, (int)sizeof(cTag), cTag);
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
-
- // Finalise the encryption
- EVP_DecryptFinal_ex(ctx,NULL, &cPlainLen);
- // Free
- EVP_CIPHER_CTX_free(ctx);
-
- return status; // OpenSSL uses 1 for success
- }
- // Decryption file using GCM mode
- - (BOOL)decryptFile:(NSString *)fileName fileNamePlain:(NSString *)fileNamePlain key:(NSData *)key keyLen:(int)keyLen initializationVector:(NSData *)initializationVector authenticationTag:(NSData *)authenticationTag
- {
- int status = 0;
- int len = 0;
- // set up key
- len = keyLen;
- unsigned char cKey[len];
- bzero(cKey, sizeof(cKey));
- [key getBytes:cKey length:len];
- // set up ivec
- len = (int)[initializationVector length];
- unsigned char cIV[len];
- bzero(cIV, sizeof(cIV));
- [initializationVector getBytes:cIV length:len];
- // set up tag
- len = (int)[authenticationTag length];;
- unsigned char cTag[len];
- bzero(cTag, sizeof(cTag));
- [authenticationTag getBytes:cTag length:len];
- // Create and initialise the context
- EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
- if (!ctx)
- return NO;
- // Initialise the decryption operation
- if (keyLen == AES_KEY_128_LENGTH)
- status = EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
- else if (keyLen == AES_KEY_256_LENGTH)
- status = EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
- // Set IV length. Not necessary if this is 12 bytes (96 bits)
- status = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int)sizeof(cIV), NULL);
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
- // Initialise key and IV
- status = EVP_DecryptInit_ex(ctx, NULL, NULL, cKey, cIV);
- if (status <= 0) {
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
- NSInputStream *inStream = [NSInputStream inputStreamWithFileAtPath:fileName];
- [inStream open];
- NSOutputStream *outStream = [NSOutputStream outputStreamToFileAtPath:fileNamePlain append:false];
- [outStream open];
- Byte buffer[streamBuffer];
- NSInteger totalNumberOfBytesWritten = 0;
- int cPlainLen = 0;
- unsigned char *cPlain;
- while ([inStream hasBytesAvailable]) {
- @autoreleasepool {
- NSInteger bytesRead = [inStream read:buffer maxLength:streamBuffer];
- if (bytesRead > 0) {
- cPlain = [[NSMutableData dataWithLength:bytesRead] mutableBytes];
- status = EVP_DecryptUpdate(ctx, cPlain, &cPlainLen, [[NSData dataWithBytes:buffer length:bytesRead] bytes], (int)bytesRead);
- if (status <= 0) {
- [inStream close];
- [outStream close];
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
- if ([outStream hasSpaceAvailable]) {
- totalNumberOfBytesWritten = [outStream write:cPlain maxLength:cPlainLen];
- if (totalNumberOfBytesWritten != cPlainLen) {
- [inStream close];
- [outStream close];
- EVP_CIPHER_CTX_free(ctx);
- return NO;
- }
- }
- }
- }
- }
- [inStream close];
- [outStream close];
- // Tag is the last 16 bytes
- status = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, (int)sizeof(cTag), cTag);
- if (status <= 0)
- return NO;
- // Finalise the encryption
- EVP_DecryptFinal_ex(ctx,NULL, &cPlainLen);
- // Free
- EVP_CIPHER_CTX_free(ctx);
- return status; // OpenSSL uses 1 for success
- }
- #
- #pragma mark - CMS
- #
- - (NSData *)generateSignatureCMS:(NSData *)data certificate:(NSString *)certificate privateKey:(NSString *)privateKey userId:(NSString *)userId
- {
- unsigned char *pKey = (unsigned char *)[privateKey UTF8String];
- unsigned char *certKey = (unsigned char *)[certificate UTF8String];
- BIO *printBIO = BIO_new_fp(stdout, BIO_NOCLOSE);
- BIO *certKeyBIO = BIO_new_mem_buf(certKey, -1);
- if (!certKeyBIO)
- return nil;
- X509 *x509 = PEM_read_bio_X509(certKeyBIO, NULL, 0, NULL);
- if (!x509)
- return nil;
- BIO *pkeyBIO = BIO_new_mem_buf(pKey, -1);
- EVP_PKEY *key = PEM_read_bio_PrivateKey(pkeyBIO, NULL, NULL, NULL);
- if (!key)
- return nil;
- BIO *dataBIO = BIO_new_mem_buf((void*)data.bytes, (int)data.length);
- CMS_ContentInfo *contentInfo = CMS_sign(x509, key, NULL, dataBIO, CMS_DETACHED);
- if (contentInfo == nil)
- return nil;
- CMS_ContentInfo_print_ctx(printBIO, contentInfo, 0, NULL);
- PEM_write_bio_CMS(printBIO, contentInfo);
- BIO *i2dCmsBioOut = BIO_new(BIO_s_mem());
- if (i2d_CMS_bio(i2dCmsBioOut, contentInfo) != 1)
- return nil;
- int len = BIO_pending(i2dCmsBioOut);
- char *keyBytes = malloc(len);
- BIO_read(i2dCmsBioOut, keyBytes, len);
- NSData *i2dCmsData = [NSData dataWithBytes:keyBytes length:len];
- BIO_free(printBIO);
- BIO_free(certKeyBIO);
- BIO_free(pkeyBIO);
- BIO_free(dataBIO);
- BIO_free(i2dCmsBioOut);
- return i2dCmsData;
- }
- /*
- - (BOOL)verifySignatureCMS:(NSData *)cmsContent data:(NSData *)data publicKey:(NSString *)publicKey userId:(NSString *)userId
- {
- BIO *dataBIO = BIO_new_mem_buf((void*)data.bytes, (int)data.length);
- BIO *printBIO = BIO_new_fp(stdout, BIO_NOCLOSE);
- BIO *cmsBIO = BIO_new_mem_buf(cmsContent.bytes, (int)cmsContent.length);
- CMS_ContentInfo *contentInfo = d2i_CMS_bio(cmsBIO, NULL);
- unsigned char *publicKeyUTF8 = (unsigned char *)[publicKey UTF8String];
- BIO *publicKeyBIO = BIO_new_mem_buf(publicKeyUTF8, -1);
- EVP_PKEY *pkey = PEM_read_bio_PUBKEY(publicKeyBIO, NULL, NULL, NULL);
- CMS_ContentInfo_print_ctx(printBIO, contentInfo, 0, NULL);
- BOOL verifyResult = CMS_verify(contentInfo, NULL, NULL, dataBIO, NULL, CMS_DETACHED | CMS_NO_SIGNER_CERT_VERIFY);
- if (verifyResult) {
- STACK_OF(X509) *signers = CMS_get0_signers(contentInfo);
- int numSigners = sk_X509_num(signers);
- for (int i = 0; i < numSigners; ++i) {
- X509 *signer = sk_X509_value(signers, i);
- int result = X509_verify(signer, pkey);
- if (result <= 0) {
- verifyResult = false;
- break;
- }
- int cnDataLength = X509_NAME_get_text_by_NID(X509_get_subject_name(signer), NID_commonName, 0, 0);
- cnDataLength += 1;
- NSMutableData* cnData = [NSMutableData dataWithLength:cnDataLength];
- X509_NAME_get_text_by_NID(X509_get_subject_name(signer), NID_commonName, [cnData mutableBytes], cnDataLength);
- NSString *cn = [[NSString alloc] initWithCString:[cnData mutableBytes] encoding:NSUTF8StringEncoding];
- if ([userId isEqualToString:cn]) {
- verifyResult = true;
- break;
- } else {
- verifyResult = false;
- }
- }
- if (signers) {
- sk_X509_free(signers);
- }
- signers = NULL;
- }
- BIO_free(dataBIO);
- BIO_free(printBIO);
- BIO_free(cmsBIO);
- BIO_free(publicKeyBIO);
- return verifyResult;
- }
- */
- - (BOOL)verifySignatureCMS:(NSData *)cmsContent data:(NSData *)data certificates:(NSArray*)certificates
- {
- BIO *dataBIO = BIO_new_mem_buf((void*)data.bytes, (int)data.length);
- BIO *printBIO = BIO_new_fp(stdout, BIO_NOCLOSE);
- BIO *cmsBIO = BIO_new_mem_buf(cmsContent.bytes, (int)cmsContent.length);
- CMS_ContentInfo *contentInfo = d2i_CMS_bio(cmsBIO, NULL);
- CMS_ContentInfo_print_ctx(printBIO, contentInfo, 0, NULL);
- BOOL verifyResult = CMS_verify(contentInfo, NULL, NULL, dataBIO, NULL, CMS_DETACHED | CMS_NO_SIGNER_CERT_VERIFY);
- struct stack_st_CMS_SignerInfo* signerInfos = CMS_get0_SignerInfos(contentInfo);
- if (verifyResult) {
- STACK_OF(X509) *signers = CMS_get0_signers(contentInfo);
- int numSigners = sk_X509_num(signers);
- for (NSString *certificate in certificates) {
- const char *ptrCertificate = [certificate cStringUsingEncoding:NSUTF8StringEncoding];
- BIO *certBio = BIO_new(BIO_s_mem());
- BIO_write(certBio, ptrCertificate,(unsigned int)strlen(ptrCertificate));
- X509 *certX509 = PEM_read_bio_X509(certBio, NULL, NULL, NULL);
- if (!certX509) {
- continue;
- }
- for (int i = 0; i < numSigners; ++i) {
- struct CMS_SignerInfo_st *signerInfo = sk_CMS_SignerInfo_value(signerInfos, i);
- if (CMS_SignerInfo_cert_cmp(signerInfo, certX509) == 0) {
- return true;
- }
- }
- }
- }
- BIO_free(dataBIO);
- BIO_free(printBIO);
- BIO_free(cmsBIO);
- return verifyResult;
- }
- #
- #pragma mark - Utility
- #
- - (void)Encodedkey:(NSString **)key initializationVector:(NSString **)initializationVector
- {
- NSData *keyData = [self generateKey:AES_KEY_128_LENGTH];
- NSData *ivData = [self generateIV:AES_IVEC_LENGTH];
- *key = [keyData base64EncodedStringWithOptions:0];
- *initializationVector = [ivData base64EncodedStringWithOptions:0];
- }
- - (NSString *)createSHA256:(NSData *)data
- {
- uint8_t digest[CC_SHA256_DIGEST_LENGTH];
- CC_SHA256(data.bytes, (unsigned int)data.length, digest);
- NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
- for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++)
- [output appendFormat:@"%02x", digest[i]];
- return output;
- }
- - (NSString *)createSHA512:(NSString *)string
- {
- const char *cstr = [string cStringUsingEncoding:NSUTF8StringEncoding];
- NSData *data = [NSData dataWithBytes:cstr length:string.length];
- uint8_t digest[CC_SHA512_DIGEST_LENGTH];
- CC_SHA512(data.bytes, (unsigned int)data.length, digest);
- NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
- for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
- [output appendFormat:@"%02x", digest[i]];
- return output;
- }
- - (NSData *)generateIV:(int)length
- {
- NSMutableData *ivData = [NSMutableData dataWithLength:length];
- (void)SecRandomCopyBytes(kSecRandomDefault, length, ivData.mutableBytes);
- return ivData;
- }
- - (NSData *)generateSalt:(int)length
- {
- NSMutableData *saltData = [NSMutableData dataWithLength:length];
- (void)SecRandomCopyBytes(kSecRandomDefault, length, saltData.mutableBytes);
-
- return saltData;
- }
- - (NSData *)generateKey:(int)length
- {
- NSMutableData *keyData = [NSMutableData dataWithLength:length];
- unsigned char *pKeyData = [keyData mutableBytes];
- RAND_bytes(pKeyData, length);
- return keyData;
- }
- - (NSData *)generateKey
- {
- NSMutableData *keyData = [NSMutableData dataWithLength:AES_KEY_128_LENGTH];
- unsigned char *pKeyData = [keyData mutableBytes];
- RAND_bytes(pKeyData, AES_KEY_128_LENGTH);
- return keyData;
- }
- - (NSString *)getSHA1:(NSString *)input
- {
- const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
- NSData *data = [NSData dataWithBytes:cstr length:input.length];
-
- uint8_t digest[CC_SHA1_DIGEST_LENGTH];
-
- CC_SHA1(data.bytes, (unsigned int)data.length, digest);
-
- NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
-
- for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
- [output appendFormat:@"%02x", digest[i]];
-
- return output;
- }
- - (NSData *)hashValueMD5OfData:(NSData *)data
- {
- MD5_CTX md5Ctx;
- unsigned char hashValue[MD5_DIGEST_LENGTH];
- if(!MD5_Init(&md5Ctx)) {
- return nil;
- }
- if (!MD5_Update(&md5Ctx, data.bytes, data.length)) {
- return nil;
- }
- if (!MD5_Final(hashValue, &md5Ctx)) {
- return nil;
- }
- return [NSData dataWithBytes:hashValue length:MD5_DIGEST_LENGTH];
- }
- - (NSString *)hexadecimalString:(NSData *)input
- {
- const unsigned char *dataBuffer = (const unsigned char *) [input bytes];
-
- if (!dataBuffer) {
- return [NSString string];
- }
-
- NSUInteger dataLength = [input length];
- NSMutableString *hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
-
- for (int i = 0; i < dataLength; ++i) {
- [hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long) dataBuffer[i]]];
- }
-
- return [NSString stringWithString:hexString];
- }
- - (NSString *)derToPemPrivateKey:(NSString *)input
- {
- NSInteger substringLength = 65;
- NSMutableString *result = [NSMutableString stringWithString: input];
- for(long i=substringLength;i<=input.length;i++) {
- [result insertString: @"\n" atIndex: i];
- i+=substringLength;
- }
-
- [result insertString: @"-----BEGIN PRIVATE KEY-----\n" atIndex: 0];
- [result appendString:@"\n-----END PRIVATE KEY-----\n"];
- return result;
- }
- - (NSString *)pubKeyToString:(EVP_PKEY *)pubkey
- {
- char *buf[256];
- FILE *pFile;
- NSString *pkey_string;
-
- pFile = fmemopen(buf, sizeof(buf), "w");
- PEM_write_PUBKEY(pFile,pubkey);
- fputc('\0', pFile);
- fclose(pFile);
-
- pkey_string = [NSString stringWithUTF8String:(char *)buf];
-
- return pkey_string;
- }
- @end
|