Assert that new OpenSSL allocators succeed

This commit is contained in:
Jack Grigg 2016-09-29 16:25:12 +13:00
parent 4c2b2541c3
commit 017abaf694
3 changed files with 12 additions and 4 deletions

View File

@ -178,11 +178,13 @@ bool CECKey::Recover(const uint256 &hash, const unsigned char *p64, int rec)
{ {
if (rec<0 || rec>=3) if (rec<0 || rec>=3)
return false; return false;
ECDSA_SIG *sig = ECDSA_SIG_new(); ECDSA_SIG *sig = nullptr;
BIGNUM *sig_r, *sig_s; BIGNUM *sig_r = nullptr, *sig_s = nullptr;
if (!(sig_r = BN_bin2bn(&p64[0], 32, nullptr)) || if (!(sig = ECDSA_SIG_new()) ||
!(sig_r = BN_bin2bn(&p64[0], 32, nullptr)) ||
!(sig_s = BN_bin2bn(&p64[32], 32, nullptr)) || !(sig_s = BN_bin2bn(&p64[32], 32, nullptr)) ||
!ECDSA_SIG_set0(sig, sig_r, sig_s)) { !ECDSA_SIG_set0(sig, sig_r, sig_s)) {
ECDSA_SIG_free(sig);
BN_free(sig_r); BN_free(sig_r);
BN_free(sig_s); BN_free(sig_s);
return false; return false;

View File

@ -7,6 +7,7 @@
#define BITCOIN_TEST_BIGNUM_H #define BITCOIN_TEST_BIGNUM_H
#include <algorithm> #include <algorithm>
#include <cassert>
#include <limits> #include <limits>
#include <stdexcept> #include <stdexcept>
#include <stdint.h> #include <stdint.h>
@ -30,11 +31,13 @@ public:
CBigNum() CBigNum()
{ {
bn = BN_new(); bn = BN_new();
assert(bn);
} }
CBigNum(const CBigNum& b) CBigNum(const CBigNum& b)
{ {
bn = BN_new(); bn = BN_new();
assert(bn);
if (!BN_copy(bn, b.bn)) if (!BN_copy(bn, b.bn))
{ {
BN_clear_free(bn); BN_clear_free(bn);
@ -54,11 +57,12 @@ public:
BN_clear_free(bn); BN_clear_free(bn);
} }
CBigNum(long long n) { bn = BN_new(); setint64(n); } CBigNum(long long n) { bn = BN_new(); assert(bn); setint64(n); }
explicit CBigNum(const std::vector<unsigned char>& vch) explicit CBigNum(const std::vector<unsigned char>& vch)
{ {
bn = BN_new(); bn = BN_new();
assert(bn);
setvch(vch); setvch(vch);
} }

View File

@ -61,6 +61,7 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
bool fOk = true; bool fOk = true;
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
assert(ctx);
if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0; 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_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0; if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
@ -86,6 +87,7 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
bool fOk = true; bool fOk = true;
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
assert(ctx);
if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0; 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_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0; if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;