From 64a9e6f7c48b495ef323c8f9d65e0775284c5f80 Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Wed, 16 Oct 2024 14:08:55 +1000 Subject: [PATCH] BN API: fix BN_bin2bn to handle NULL data properly BN_bin2bn was freeing the BN and returning it. Added test for this. --- src/ssl_bn.c | 4 +++- tests/api.c | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ssl_bn.c b/src/ssl_bn.c index e45e19da50..74eadcead2 100644 --- a/src/ssl_bn.c +++ b/src/ssl_bn.c @@ -516,12 +516,14 @@ WOLFSSL_BIGNUM* wolfSSL_BN_bin2bn(const unsigned char* data, int len, ret = NULL; } else { - /* Don't free bn as we may be returning it. */ + /* Don't free bn as we are returning it. */ bn = NULL; } } else if (data == NULL) { wolfSSL_BN_zero(ret); + /* Don't free bn as we are returning it. */ + bn = NULL; } } diff --git a/tests/api.c b/tests/api.c index fc68a44715..eca756cfd5 100644 --- a/tests/api.c +++ b/tests/api.c @@ -61606,6 +61606,11 @@ static int test_wolfSSL_BN_enc_dec(void) ExpectNull(BN_bn2dec(NULL)); ExpectNull(BN_bn2dec(&emptyBN)); + ExpectNotNull(c = BN_bin2bn(NULL, 0, NULL)); + BN_clear(c); + BN_free(c); + c = NULL; + ExpectNotNull(BN_bin2bn(NULL, sizeof(binNum), a)); BN_free(a); ExpectNotNull(a = BN_new());