Skip to content

Commit

Permalink
allow openssl1.0/1.1 to coexist
Browse files Browse the repository at this point in the history
  • Loading branch information
barrystyle authored and tohsnoom committed Nov 11, 2019
1 parent 209609b commit d1b6037
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ class CInit
~CInit()
{
// Securely erase the memory used by the PRNG
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
#else
RAND_cleanup();
#endif
// Shutdown OpenSSL library multithreading support
CRYPTO_set_locking_callback(NULL);
for (int i = 0; i < CRYPTO_num_locks(); i++)
Expand Down
84 changes: 75 additions & 9 deletions src/wallet/crypter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,32 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
vchCiphertext = std::vector<unsigned char>(nCLen);

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
EVP_CIPHER_CTX* ctx;
#else
EVP_CIPHER_CTX ctx;
#endif

bool fOk = true;

EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
ctx = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_init(ctx);
#else
EVP_CIPHER_CTX_init (&ctx);
#endif

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
EVP_CIPHER_CTX_free(ctx);
EVP_CIPHER_CTX_cleanup(ctx);
#else
if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
EVP_CIPHER_CTX_cleanup(&ctx);
#endif

if (!fOk) return false;

Expand All @@ -83,13 +102,32 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM

vchPlaintext = CKeyingMaterial(nPLen);

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
EVP_CIPHER_CTX* ctx;
#else
EVP_CIPHER_CTX ctx;
#endif

bool fOk = true;

EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
ctx = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_init(ctx);
#else
EVP_CIPHER_CTX_init (&ctx);
#endif

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
EVP_CIPHER_CTX_free(ctx);
EVP_CIPHER_CTX_cleanup(ctx);
#else
if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
EVP_CIPHER_CTX_cleanup(&ctx);
#endif

if (!fOk) return false;

Expand Down Expand Up @@ -128,15 +166,27 @@ bool EncryptAES256(const SecureString& sKey, const SecureString& sPlaintext, con
sCiphertext.resize(nCLen);

// Perform the encryption
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
EVP_CIPHER_CTX* ctx;
#else
EVP_CIPHER_CTX ctx;
#endif

bool fOk = true;

ctx = EVP_CIPHER_CTX_new();
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
EVP_CIPHER_CTX_init(ctx);
if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
if (fOk) fOk = EVP_EncryptUpdate(ctx, (unsigned char*)&sCiphertext[0], &nCLen, (const unsigned char*)&sPlaintext[0], nLen);
if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (unsigned char*)(&sCiphertext[0]) + nCLen, &nFLen);
EVP_CIPHER_CTX_free(ctx);
EVP_CIPHER_CTX_cleanup(ctx);
#else
EVP_CIPHER_CTX_init(&ctx);
if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
if (fOk) fOk = EVP_EncryptUpdate(&ctx, (unsigned char*)&sCiphertext[0], &nCLen, (const unsigned char*)&sPlaintext[0], nLen);
if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (unsigned char*)(&sCiphertext[0]) + nCLen, &nFLen);
EVP_CIPHER_CTX_cleanup(&ctx);
#endif

if (!fOk) return false;

Expand Down Expand Up @@ -169,15 +219,32 @@ bool DecryptAES256(const SecureString& sKey, const std::string& sCiphertext, con

sPlaintext.resize(nPLen);

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
EVP_CIPHER_CTX* ctx;
#else
EVP_CIPHER_CTX ctx;
#endif

bool fOk = true;

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
ctx = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_init(ctx);
#else
EVP_CIPHER_CTX_init (&ctx);
#endif

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
if (fOk) fOk = EVP_DecryptUpdate(ctx, (unsigned char*)&sPlaintext[0], &nPLen, (const unsigned char*)&sCiphertext[0], nLen);
if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (unsigned char*)(&sPlaintext[0]) + nPLen, &nFLen);
EVP_CIPHER_CTX_free(ctx);
EVP_CIPHER_CTX_cleanup(ctx);
#else
if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]); if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
if (fOk) fOk = EVP_DecryptUpdate(&ctx, (unsigned char*)&sPlaintext[0], &nPLen, (const unsigned char*)&sCiphertext[0], nLen); if (fOk) fOk = EVP_DecryptUpdate(&ctx, (unsigned char*)&sPlaintext[0], &nPLen, (const unsigned char*)&sCiphertext[0], nLen);
if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (unsigned char*)(&sPlaintext[0]) + nPLen, &nFLen); if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (unsigned char*)(&sPlaintext[0]) + nPLen, &nFLen);
EVP_CIPHER_CTX_cleanup(&ctx); EVP_CIPHER_CTX_cleanup(&ctx);
#endif

if (!fOk) return false;

Expand All @@ -199,7 +266,6 @@ bool CCryptoKeyStore::SetCrypted()

bool CCryptoKeyStore::Lock()
{
if (!SetCrypted())
return false;

{
Expand Down Expand Up @@ -600,4 +666,4 @@ bool CCryptoKeyStore::GetDeterministicSeed(const uint256& hashSeed, uint256& see


// return error("Failed to decrypt deterministic seed %s", IsLocked() ? "Wallet is locked!" : "");
}
}

0 comments on commit d1b6037

Please sign in to comment.