Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compiler error with OpenSSL 1.1 #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions cocoastack/crypto/OpenSSLCryptoKey.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,32 +141,32 @@ - (BOOL)encrypt:(NSData *)plainData intoBuffer:(NSMutableData *)theOutBuffer err
return YES;
}

EVP_CIPHER_CTX cipherContext;
EVP_CIPHER_CTX_init(&cipherContext);
if (!EVP_EncryptInit(&cipherContext, cipher, evpKey, iv)) {
EVP_CIPHER_CTX *cipherContext = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_init(cipherContext);
if (!EVP_EncryptInit(cipherContext, cipher, evpKey, iv)) {
SETNSERROR([CryptoKey errorDomain], -1, @"EVP_EncryptInit: %@", [OpenSSL errorMessage]);
EVP_CIPHER_CTX_cleanup(&cipherContext);
EVP_CIPHER_CTX_cleanup(cipherContext);
return NO;
}

// Need room for data + cipher block size - 1.
[theOutBuffer setLength:([plainData length] + EVP_CIPHER_CTX_block_size(&cipherContext))];
[theOutBuffer setLength:([plainData length] + EVP_CIPHER_CTX_block_size(cipherContext))];
unsigned char *outbuf = (unsigned char *)[theOutBuffer mutableBytes];

int outlen = 0;
if (!EVP_EncryptUpdate(&cipherContext, outbuf, &outlen, [plainData bytes], (int)[plainData length])) {
if (!EVP_EncryptUpdate(cipherContext, outbuf, &outlen, [plainData bytes], (int)[plainData length])) {
SETNSERROR([CryptoKey errorDomain], -1, @"EVP_EncryptUpdate: %@", [OpenSSL errorMessage]);
EVP_CIPHER_CTX_cleanup(&cipherContext);
EVP_CIPHER_CTX_free(cipherContext);
return NO;
}

int extralen = 0;
if (!EVP_EncryptFinal(&cipherContext, outbuf + outlen, &extralen)) {
if (!EVP_EncryptFinal(cipherContext, outbuf + outlen, &extralen)) {
SETNSERROR([CryptoKey errorDomain], -1, @"EVP_EncryptFinal: %@", [OpenSSL errorMessage]);
EVP_CIPHER_CTX_cleanup(&cipherContext);
EVP_CIPHER_CTX_free(cipherContext);
return NO;
}
EVP_CIPHER_CTX_cleanup(&cipherContext);
EVP_CIPHER_CTX_free(cipherContext);

[theOutBuffer setLength:(outlen + extralen)];
return YES;
Expand All @@ -191,31 +191,31 @@ - (BOOL)decrypt:(NSData *)encrypted intoBuffer:(NSMutableData *)theOutBuffer err
int inlen = (int)[encrypted length];
unsigned char *input = (unsigned char *)[encrypted bytes];

EVP_CIPHER_CTX cipherContext;
EVP_CIPHER_CTX_init(&cipherContext);
if (!EVP_DecryptInit(&cipherContext, cipher, evpKey, iv)) {
EVP_CIPHER_CTX *cipherContext = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_init(cipherContext);
if (!EVP_DecryptInit(cipherContext, cipher, evpKey, iv)) {
SETNSERROR([CryptoKey errorDomain], -1, @"EVP_DecryptInit: %@", [OpenSSL errorMessage]);
EVP_CIPHER_CTX_cleanup(&cipherContext);
EVP_CIPHER_CTX_free(cipherContext);
return NO;
}

[theOutBuffer setLength:(inlen + EVP_CIPHER_CTX_block_size(&cipherContext))];
[theOutBuffer setLength:(inlen + EVP_CIPHER_CTX_block_size(cipherContext))];
unsigned char *outbuf = (unsigned char *)[theOutBuffer mutableBytes];
int outlen = 0;
if (!EVP_DecryptUpdate(&cipherContext, outbuf, &outlen, input, inlen)) {
if (!EVP_DecryptUpdate(cipherContext, outbuf, &outlen, input, inlen)) {
SETNSERROR([CryptoKey errorDomain], -1, @"EVP_DecryptUpdate: %@", [OpenSSL errorMessage]);
EVP_CIPHER_CTX_cleanup(&cipherContext);
EVP_CIPHER_CTX_free(cipherContext);
return NO;
}

int extralen = 0;
if (!EVP_DecryptFinal(&cipherContext, outbuf + outlen, &extralen)) {
if (!EVP_DecryptFinal(cipherContext, outbuf + outlen, &extralen)) {
SETNSERROR([CryptoKey errorDomain], -1, @"EVP_DecryptFinal: %@", [OpenSSL errorMessage]);
EVP_CIPHER_CTX_cleanup(&cipherContext);
EVP_CIPHER_CTX_free(cipherContext);
return NO;
}

EVP_CIPHER_CTX_cleanup(&cipherContext);
EVP_CIPHER_CTX_free(cipherContext);
[theOutBuffer setLength:(outlen + extralen)];
return YES;
}
Expand Down