Add compatibility with OpenSSL 1.1

OpenSSL 1.1 has a new API. This commit allows to compile against it, as well
as it keeps compatibility with previous versions of OpenSSL.

Fixes #89.
preferencesAboutTextFull
Antoine Belvire 7 years ago
parent b228a5c972
commit 393c9faaad
  1. 35
      src/frameworks/UBCryptoUtils.cpp
  2. 5
      src/frameworks/UBCryptoUtils.h

@ -63,6 +63,10 @@ UBCryptoUtils::UBCryptoUtils(QObject * pParent)
UBCryptoUtils::~UBCryptoUtils() UBCryptoUtils::~UBCryptoUtils()
{ {
// TODO UB 4.x aes destroy // TODO UB 4.x aes destroy
#if OPENSSL_VERSION_NUMBER >= 10100000L
EVP_CIPHER_CTX_free(mAesEncryptContext);
EVP_CIPHER_CTX_free(mAesDecryptContext);
#endif
} }
@ -74,18 +78,30 @@ QString UBCryptoUtils::symetricEncrypt(const QString& clear)
int paddingLength = 0; int paddingLength = 0;
unsigned char *ciphertext = (unsigned char *)malloc(cipheredLength); unsigned char *ciphertext = (unsigned char *)malloc(cipheredLength);
#if OPENSSL_VERSION_NUMBER >= 10100000L
if(!EVP_EncryptInit_ex(mAesEncryptContext, NULL, NULL, NULL, NULL)){
#else
if(!EVP_EncryptInit_ex(&mAesEncryptContext, NULL, NULL, NULL, NULL)){ if(!EVP_EncryptInit_ex(&mAesEncryptContext, NULL, NULL, NULL, NULL)){
#endif
free(ciphertext); free(ciphertext);
return QString(); return QString();
} }
#if OPENSSL_VERSION_NUMBER >= 10100000L
if(!EVP_EncryptUpdate(mAesEncryptContext, ciphertext, &cipheredLength, (unsigned char *)clearData.data(), clearData.length())){
#else
if(!EVP_EncryptUpdate(&mAesEncryptContext, ciphertext, &cipheredLength, (unsigned char *)clearData.data(), clearData.length())){ if(!EVP_EncryptUpdate(&mAesEncryptContext, ciphertext, &cipheredLength, (unsigned char *)clearData.data(), clearData.length())){
#endif
free(ciphertext); free(ciphertext);
return QString(); return QString();
} }
/* update ciphertext with the final remaining bytes */ /* update ciphertext with the final remaining bytes */
#if OPENSSL_VERSION_NUMBER >= 10100000L
if(!EVP_EncryptFinal_ex(mAesEncryptContext, ciphertext + cipheredLength, &paddingLength)){
#else
if(!EVP_EncryptFinal_ex(&mAesEncryptContext, ciphertext + cipheredLength, &paddingLength)){ if(!EVP_EncryptFinal_ex(&mAesEncryptContext, ciphertext + cipheredLength, &paddingLength)){
#endif
free(ciphertext); free(ciphertext);
return QString(); return QString();
} }
@ -106,17 +122,29 @@ QString UBCryptoUtils::symetricDecrypt(const QString& encrypted)
int paddingLength = 0; int paddingLength = 0;
unsigned char *plaintext = (unsigned char *)malloc(encryptedLength); unsigned char *plaintext = (unsigned char *)malloc(encryptedLength);
#if OPENSSL_VERSION_NUMBER >= 10100000L
if(!EVP_DecryptInit_ex(mAesDecryptContext, NULL, NULL, NULL, NULL)){
#else
if(!EVP_DecryptInit_ex(&mAesDecryptContext, NULL, NULL, NULL, NULL)){ if(!EVP_DecryptInit_ex(&mAesDecryptContext, NULL, NULL, NULL, NULL)){
#endif
free(plaintext); free(plaintext);
return QString(); return QString();
} }
#if OPENSSL_VERSION_NUMBER >= 10100000L
if(!EVP_DecryptUpdate(mAesDecryptContext, plaintext, &encryptedLength, (const unsigned char *)encryptedData.data(), encryptedData.length())){
#else
if(!EVP_DecryptUpdate(&mAesDecryptContext, plaintext, &encryptedLength, (const unsigned char *)encryptedData.data(), encryptedData.length())){ if(!EVP_DecryptUpdate(&mAesDecryptContext, plaintext, &encryptedLength, (const unsigned char *)encryptedData.data(), encryptedData.length())){
#endif
free(plaintext); free(plaintext);
return QString(); return QString();
} }
#if OPENSSL_VERSION_NUMBER >= 10100000L
if(!EVP_DecryptFinal_ex(mAesDecryptContext, plaintext + encryptedLength, &paddingLength)){
#else
if(!EVP_DecryptFinal_ex(&mAesDecryptContext, plaintext + encryptedLength, &paddingLength)){ if(!EVP_DecryptFinal_ex(&mAesDecryptContext, plaintext + encryptedLength, &paddingLength)){
#endif
free(plaintext); free(plaintext);
return QString(); return QString();
} }
@ -146,8 +174,15 @@ void UBCryptoUtils::aesInit()
return; return;
} }
#if OPENSSL_VERSION_NUMBER >= 10100000L
mAesEncryptContext = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(mAesEncryptContext, EVP_aes_256_cbc(), NULL, key, iv);
mAesDecryptContext = EVP_CIPHER_CTX_new();
EVP_DecryptInit_ex(mAesDecryptContext, EVP_aes_256_cbc(), NULL, key, iv);
#else
EVP_CIPHER_CTX_init(&mAesEncryptContext); EVP_CIPHER_CTX_init(&mAesEncryptContext);
EVP_EncryptInit_ex(&mAesEncryptContext, EVP_aes_256_cbc(), NULL, key, iv); EVP_EncryptInit_ex(&mAesEncryptContext, EVP_aes_256_cbc(), NULL, key, iv);
EVP_CIPHER_CTX_init(&mAesDecryptContext); EVP_CIPHER_CTX_init(&mAesDecryptContext);
EVP_DecryptInit_ex(&mAesDecryptContext, EVP_aes_256_cbc(), NULL, key, iv); EVP_DecryptInit_ex(&mAesDecryptContext, EVP_aes_256_cbc(), NULL, key, iv);
#endif
} }

@ -60,8 +60,13 @@ class UBCryptoUtils : public QObject
void aesInit(); void aesInit();
#if OPENSSL_VERSION_NUMBER >= 10100000L
EVP_CIPHER_CTX *mAesEncryptContext;
EVP_CIPHER_CTX *mAesDecryptContext;
#else
EVP_CIPHER_CTX mAesEncryptContext; EVP_CIPHER_CTX mAesEncryptContext;
EVP_CIPHER_CTX mAesDecryptContext; EVP_CIPHER_CTX mAesDecryptContext;
#endif
}; };

Loading…
Cancel
Save