Skip to content

Commit 043dde1

Browse files
authored
Merge pull request #7048 from anhu/PQ_uninit_key_free
Prevent freeing uninitialized keys
2 parents 00a1c68 + 40015a0 commit 043dde1

1 file changed

Lines changed: 26 additions & 28 deletions

File tree

src/tls.c

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8395,16 +8395,24 @@ static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
83958395
/* I am the client, the ciphertext is in keyShareEntry->ke */
83968396
findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
83978397

8398+
ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
8399+
if (ret != 0) {
8400+
WOLFSSL_MSG("Memory allocation error.");
8401+
return MEMORY_E;
8402+
}
8403+
83988404
ret = kyber_id2type(oqs_group, &type);
83998405
if (ret != 0) {
8406+
wc_ecc_free(&eccpubkey);
84008407
WOLFSSL_MSG("Invalid OQS algorithm specified.");
84018408
return BAD_FUNC_ARG;
84028409
}
8403-
if (ret == 0) {
8404-
ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
8405-
if (ret != 0) {
8406-
WOLFSSL_MSG("Error creating Kyber KEM");
8407-
}
8410+
8411+
ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
8412+
if (ret != 0) {
8413+
wc_ecc_free(&eccpubkey);
8414+
WOLFSSL_MSG("Error creating Kyber KEM");
8415+
return MEMORY_E;
84088416
}
84098417

84108418
if (ret == 0) {
@@ -8428,12 +8436,6 @@ static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
84288436
default:
84298437
break;
84308438
}
8431-
8432-
ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
8433-
if (ret != 0) {
8434-
WOLFSSL_MSG("Memory allocation error.");
8435-
ret = MEMORY_E;
8436-
}
84378439
}
84388440
if (ret == 0) {
84398441
sharedSecret = (byte*)XMALLOC(sharedSecretLen, ssl->heap,
@@ -8892,13 +8894,19 @@ static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
88928894
return BAD_FUNC_ARG;
88938895
}
88948896

8895-
if (ret == 0) {
8896-
ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
8897-
if (ret != 0) {
8898-
WOLFSSL_MSG("Could not do ECC public key initialization.");
8899-
ret = MEMORY_E;
8900-
}
8897+
ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
8898+
if (ret != 0) {
8899+
WOLFSSL_MSG("Could not do ECC public key initialization.");
8900+
return MEMORY_E;
89018901
}
8902+
8903+
ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
8904+
if (ret != 0) {
8905+
wc_ecc_free(&eccpubkey);
8906+
WOLFSSL_MSG("Error creating Kyber KEM");
8907+
return MEMORY_E;
8908+
}
8909+
89028910
if (ret == 0) {
89038911
ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
89048912
DYNAMIC_TYPE_TLSX);
@@ -8915,19 +8923,9 @@ static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
89158923
if (ret == 0 && ecc_group != 0) {
89168924
ecc_kse->group = ecc_group;
89178925
ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
8918-
if (ret != 0) {
8919-
/* No message, TLSX_KeyShare_GenEccKey() will do it. */
8920-
return ret;
8921-
}
8922-
ret = 0;
8926+
/* No message, TLSX_KeyShare_GenEccKey() will do it. */
89238927
}
89248928

8925-
if (ret == 0) {
8926-
ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
8927-
if (ret != 0) {
8928-
WOLFSSL_MSG("Error creating Kyber KEM");
8929-
}
8930-
}
89318929
if (ret == 0) {
89328930
ret = wc_KyberKey_PublicKeySize(kem, &pubSz);
89338931
}

0 commit comments

Comments
 (0)