Skip to content

Commit d1b6037

Browse files
barrystyletohsnoom
authored andcommitted
allow openssl1.0/1.1 to coexist
1 parent 209609b commit d1b6037

File tree

2 files changed

+78
-9
lines changed

2 files changed

+78
-9
lines changed

src/util.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ class CInit
183183
~CInit()
184184
{
185185
// Securely erase the memory used by the PRNG
186+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
187+
#else
186188
RAND_cleanup();
189+
#endif
187190
// Shutdown OpenSSL library multithreading support
188191
CRYPTO_set_locking_callback(NULL);
189192
for (int i = 0; i < CRYPTO_num_locks(); i++)

src/wallet/crypter.cpp

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,32 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
5858
int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
5959
vchCiphertext = std::vector<unsigned char>(nCLen);
6060

61+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
62+
EVP_CIPHER_CTX* ctx;
63+
#else
64+
EVP_CIPHER_CTX ctx;
65+
#endif
66+
6167
bool fOk = true;
6268

63-
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
69+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
70+
ctx = EVP_CIPHER_CTX_new();
71+
EVP_CIPHER_CTX_init(ctx);
72+
#else
73+
EVP_CIPHER_CTX_init (&ctx);
74+
#endif
75+
76+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
6477
if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
6578
if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
6679
if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
67-
EVP_CIPHER_CTX_free(ctx);
80+
EVP_CIPHER_CTX_cleanup(ctx);
81+
#else
82+
if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
83+
if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
84+
if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
85+
EVP_CIPHER_CTX_cleanup(&ctx);
86+
#endif
6887

6988
if (!fOk) return false;
7089

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

84103
vchPlaintext = CKeyingMaterial(nPLen);
85104

105+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
106+
EVP_CIPHER_CTX* ctx;
107+
#else
108+
EVP_CIPHER_CTX ctx;
109+
#endif
110+
86111
bool fOk = true;
87112

88-
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
113+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
114+
ctx = EVP_CIPHER_CTX_new();
115+
EVP_CIPHER_CTX_init(ctx);
116+
#else
117+
EVP_CIPHER_CTX_init (&ctx);
118+
#endif
119+
120+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
89121
if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
90122
if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
91123
if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
92-
EVP_CIPHER_CTX_free(ctx);
124+
EVP_CIPHER_CTX_cleanup(ctx);
125+
#else
126+
if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
127+
if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
128+
if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
129+
EVP_CIPHER_CTX_cleanup(&ctx);
130+
#endif
93131

94132
if (!fOk) return false;
95133

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

130168
// Perform the encryption
169+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
131170
EVP_CIPHER_CTX* ctx;
171+
#else
172+
EVP_CIPHER_CTX ctx;
173+
#endif
132174

133175
bool fOk = true;
134176

135-
ctx = EVP_CIPHER_CTX_new();
177+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
178+
EVP_CIPHER_CTX_init(ctx);
136179
if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
137180
if (fOk) fOk = EVP_EncryptUpdate(ctx, (unsigned char*)&sCiphertext[0], &nCLen, (const unsigned char*)&sPlaintext[0], nLen);
138181
if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (unsigned char*)(&sCiphertext[0]) + nCLen, &nFLen);
139-
EVP_CIPHER_CTX_free(ctx);
182+
EVP_CIPHER_CTX_cleanup(ctx);
183+
#else
184+
EVP_CIPHER_CTX_init(&ctx);
185+
if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
186+
if (fOk) fOk = EVP_EncryptUpdate(&ctx, (unsigned char*)&sCiphertext[0], &nCLen, (const unsigned char*)&sPlaintext[0], nLen);
187+
if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (unsigned char*)(&sCiphertext[0]) + nCLen, &nFLen);
188+
EVP_CIPHER_CTX_cleanup(&ctx);
189+
#endif
140190

141191
if (!fOk) return false;
142192

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

170220
sPlaintext.resize(nPLen);
171221

222+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
172223
EVP_CIPHER_CTX* ctx;
224+
#else
225+
EVP_CIPHER_CTX ctx;
226+
#endif
173227

174228
bool fOk = true;
175229

230+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
176231
ctx = EVP_CIPHER_CTX_new();
232+
EVP_CIPHER_CTX_init(ctx);
233+
#else
234+
EVP_CIPHER_CTX_init (&ctx);
235+
#endif
236+
237+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
177238
if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
178239
if (fOk) fOk = EVP_DecryptUpdate(ctx, (unsigned char*)&sPlaintext[0], &nPLen, (const unsigned char*)&sCiphertext[0], nLen);
179240
if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (unsigned char*)(&sPlaintext[0]) + nPLen, &nFLen);
180-
EVP_CIPHER_CTX_free(ctx);
241+
EVP_CIPHER_CTX_cleanup(ctx);
242+
#else
243+
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]);
244+
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);
245+
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);
246+
EVP_CIPHER_CTX_cleanup(&ctx); EVP_CIPHER_CTX_cleanup(&ctx);
247+
#endif
181248

182249
if (!fOk) return false;
183250

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

200267
bool CCryptoKeyStore::Lock()
201268
{
202-
if (!SetCrypted())
203269
return false;
204270

205271
{
@@ -600,4 +666,4 @@ bool CCryptoKeyStore::GetDeterministicSeed(const uint256& hashSeed, uint256& see
600666

601667

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

0 commit comments

Comments
 (0)