diff --git a/crypto/bn/convert.c b/crypto/bn/convert.c index 542f523f..9125bf84 100644 --- a/crypto/bn/convert.c +++ b/crypto/bn/convert.c @@ -577,12 +577,14 @@ BIGNUM *BN_mpi2bn(const uint8_t *in, size_t len, BIGNUM *out) { return NULL; } + int out_is_alloced = 0; if (out == NULL) { out = BN_new(); - } - if (out == NULL) { - OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE); - return NULL; + if (out == NULL) { + OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE); + return NULL; + } + out_is_alloced = 1; } if (in_len == 0) { @@ -592,6 +594,9 @@ BIGNUM *BN_mpi2bn(const uint8_t *in, size_t len, BIGNUM *out) { in += 4; if (BN_bin2bn(in, in_len, out) == NULL) { + if (out_is_alloced) { + BN_free(out); + } return NULL; } out->neg = ((*in) & 0x80) != 0;