From 7a69415e79b67a761b629faf6ebe19972e507040 Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 23 Sep 2022 13:53:16 -0700 Subject: [PATCH] Fix for possible memory leak on failure in RSA/DH public key decode with heap based math. --- wolfcrypt/src/asn.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index eaadde27b..e3e30ad98 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -9499,6 +9499,9 @@ int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key, (int)(rsaPublicKeyASN_Length - RSAPUBLICKEYASN_IDX_PUBKEY_RSA_SEQ), 0, input, inOutIdx, inSz); if (ret != 0) { + mp_free(&key->n); + mp_free(&key->e); + /* Didn't work - try whole SubjectKeyInfo instead. */ /* Set the OID to expect. */ GetASN_ExpBuffer(&dataASN[RSAPUBLICKEYASN_IDX_ALGOID_OID], @@ -9849,6 +9852,9 @@ int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz) inOutIdx, inSz); #ifdef WOLFSSL_DH_EXTRA if (ret != 0) { + mp_free(&key->p); + mp_free(&key->g); + /* Initialize data and set mp_ints to hold p, g, q, priv and pub. */ XMEMSET(dataASN, 0, sizeof(*dataASN) * dhKeyPkcs8ASN_Length); GetASN_ExpBuffer(&dataASN[DHKEYPKCS8ASN_IDX_PKEYALGO_OID],