Skip to content

Commit 5c421d0

Browse files
authored
Merge pull request #7178 from anhu/OQS_MEM_LEAKS
Fixes that prevent memory leaks when using OQS.
2 parents 851f059 + fe87f16 commit 5c421d0

5 files changed

Lines changed: 35 additions & 5 deletions

File tree

src/tls.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7658,6 +7658,13 @@ static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
76587658
word32 privSz = 0;
76597659
word32 pubSz = 0;
76607660

7661+
/* This gets called twice. Once during parsing of the key share and once
7662+
* during the population of the extension. No need to do work the second
7663+
* time. Just return success if its already been done. */
7664+
if (kse->pubKey != NULL) {
7665+
return ret;
7666+
}
7667+
76617668
findEccPqc(&ecc_group, &oqs_group, kse->group);
76627669
ret = kyber_id2type(oqs_group, &type);
76637670
if (ret == NOT_COMPILED_IN) {
@@ -7735,10 +7742,11 @@ static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
77357742

77367743
/* Note we are saving the OQS private key and ECC private key
77377744
* separately. That's because the ECC private key is not simply a
7738-
* buffer. Its is an ecc_key struct.
7739-
*/
7745+
* buffer. Its is an ecc_key struct. Typically do not need the private
7746+
* key size, but will need to zero it out upon freeing. */
77407747
kse->privKey = privKey;
77417748
privKey = NULL;
7749+
kse->privKeyLen = privSz;
77427750

77437751
kse->key = ecc_kse->key;
77447752
ecc_kse->key = NULL;
@@ -7814,9 +7822,19 @@ static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap)
78147822
#endif
78157823
}
78167824
#ifdef HAVE_PQC
7817-
else if (WOLFSSL_NAMED_GROUP_IS_PQC(current->group) &&
7818-
current->key != NULL) {
7819-
ForceZero((byte*)current->key, current->keyLen);
7825+
else if (WOLFSSL_NAMED_GROUP_IS_PQC(current->group)) {
7826+
if (current->key != NULL) {
7827+
ForceZero((byte*)current->key, current->keyLen);
7828+
}
7829+
if (current->pubKey != NULL) {
7830+
XFREE(current->pubKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
7831+
current->pubKey = NULL;
7832+
}
7833+
if (current->privKey != NULL) {
7834+
ForceZero(current->privKey, current->privKeyLen);
7835+
XFREE(current->privKey, heap, DYNAMIC_TYPE_PRIVATE_KEY);
7836+
current->privKey = NULL;
7837+
}
78207838
}
78217839
#endif
78227840
else {

wolfcrypt/src/port/liboqs/liboqs.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ int wolfSSL_liboqsInit(void)
9999
return ret;
100100
}
101101

102+
void wolfSSL_liboqsClose(void)
103+
{
104+
wc_FreeRng(&liboqsDefaultRNG);
105+
}
106+
102107
int wolfSSL_liboqsRngMutexLock(WC_RNG* rng)
103108
{
104109
int ret = wolfSSL_liboqsInit();

wolfcrypt/src/wc_port.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,10 @@ int wolfCrypt_Cleanup(void)
493493
#endif
494494
}
495495

496+
#if defined(HAVE_LIBOQS)
497+
wolfSSL_liboqsClose();
498+
#endif
499+
496500
return ret;
497501
}
498502

wolfssl/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3365,6 +3365,7 @@ typedef struct KeyShareEntry {
33653365
word32 pubKeyLen; /* Public key length */
33663366
#if !defined(NO_DH) || defined(HAVE_PQC)
33673367
byte* privKey; /* Private key - DH and PQ KEMs only */
3368+
word32 privKeyLen;/* Only for PQ KEMs. */
33683369
#endif
33693370
#ifdef WOLFSSL_ASYNC_CRYPT
33703371
int lastRet;

wolfssl/wolfcrypt/port/liboqs/liboqs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ implementations for Post-Quantum cryptography algorithms.
4747

4848
int wolfSSL_liboqsInit(void);
4949

50+
void wolfSSL_liboqsClose(void);
51+
5052
int wolfSSL_liboqsRngMutexLock(WC_RNG* rng);
5153

5254
int wolfSSL_liboqsRngMutexUnlock(void);

0 commit comments

Comments
 (0)