Skip to content

Commit a3baac7

Browse files
committed
zero sensitive material before free
1 parent c61fa7d commit a3baac7

5 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/dtls13.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2393,7 +2393,10 @@ static Dtls13Epoch* Dtls13NewEpochSlot(WOLFSSL* ssl)
23932393
WOLFSSL_MSG_EX("Delete epoch: %d", e->epochNumber);
23942394
#endif /* WOLFSSL_DEBUG_TLS */
23952395

2396-
XMEMSET(e, 0, sizeof(*e));
2396+
/* The slot we are reusing holds the previous epoch's symmetric keys, IVs,
2397+
* and sn-keys; use ForceZero so the wipe cannot be elided by the
2398+
* optimizer when the slot is later overwritten. */
2399+
ForceZero(e, sizeof(*e));
23972400

23982401
return e;
23992402
}

src/internal.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9192,6 +9192,11 @@ void wolfSSL_ResourceFree(WOLFSSL* ssl)
91929192
#ifdef WOLFSSL_DTLS13
91939193
Dtls13FreeFsmResources(ssl);
91949194

9195+
/* Zero per-epoch symmetric keys / IVs / sn-keys so they are not left
9196+
* resident in the heap after FreeSSL releases the SSL struct. Mirrors
9197+
* the existing ForceZero on ssl->keys and ssl->clientSecret/serverSecret. */
9198+
ForceZero(ssl->dtls13Epochs, sizeof(ssl->dtls13Epochs));
9199+
91959200
#ifdef WOLFSSL_RW_THREADED
91969201
wc_FreeMutex(&ssl->dtls13Rtx.mutex);
91979202
#endif

src/keys.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4138,6 +4138,8 @@ static int MakeSslMasterSecret(WOLFSSL* ssl)
41384138
ENCRYPT_LEN + WC_SHA_DIGEST_SIZE);
41394139
wc_MemZero_Add("MakeSslMasterSecret shaInput", shaInput,
41404140
PREFIX + ENCRYPT_LEN + 2 * RAN_LEN);
4141+
wc_MemZero_Add("MakeSslMasterSecret shaOutput", shaOutput,
4142+
WC_SHA_DIGEST_SIZE);
41414143
#endif
41424144

41434145
XMEMSET(shaOutput, 0, WC_SHA_DIGEST_SIZE);
@@ -4200,9 +4202,11 @@ static int MakeSslMasterSecret(WOLFSSL* ssl)
42004202

42014203
ForceZero(md5Input, ENCRYPT_LEN + WC_SHA_DIGEST_SIZE);
42024204
ForceZero(shaInput, PREFIX + ENCRYPT_LEN + 2 * RAN_LEN);
4205+
ForceZero(shaOutput, WC_SHA_DIGEST_SIZE);
42034206
#ifdef WOLFSSL_CHECK_MEM_ZERO
42044207
wc_MemZero_Check(md5Input, ENCRYPT_LEN + WC_SHA_DIGEST_SIZE);
42054208
wc_MemZero_Check(shaInput, PREFIX + ENCRYPT_LEN + 2 * RAN_LEN);
4209+
wc_MemZero_Check(shaOutput, WC_SHA_DIGEST_SIZE);
42064210
#endif
42074211

42084212
WC_FREE_VAR_EX(shaOutput, NULL, DYNAMIC_TYPE_TMP_BUFFER);

src/sniffer.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7591,11 +7591,15 @@ static int parseKeyLogFile(const char* fileName, char* error)
75917591

75927592
if (ret != 0) {
75937593
fclose(file);
7594+
ForceZero(secret, SECRET_LENGTH);
7595+
ForceZero(secretHex, sizeof(secretHex));
75947596
return ret;
75957597
}
75967598
}
75977599
fclose(file);
75987600

7601+
ForceZero(secret, SECRET_LENGTH);
7602+
ForceZero(secretHex, sizeof(secretHex));
75997603
return 0;
76007604
}
76017605

@@ -7613,6 +7617,7 @@ static void freeSecretList(void)
76137617

76147618
while (current != NULL) {
76157619
next = current->next;
7620+
ForceZero(current, sizeof(SecretNode));
76167621
XFREE(current, NULL, DYNAMIC_TYPE_SNIFFER_KEYLOG_NODE);
76177622
current = next;
76187623
}

src/tls13.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,17 +1051,22 @@ int Tls13_Exporter(WOLFSSL* ssl, unsigned char *out, size_t outLen,
10511051
protocol, protocolLen, (byte*)label, (word32)labelLen,
10521052
emptyHash, hashLen, (int)hashType);
10531053
if (ret != 0)
1054-
return ret;
1054+
goto cleanup;
10551055

10561056
/* Hash(context_value) */
10571057
ret = wc_Hash(hashType, context, (word32)contextLen, hashOut, WC_MAX_DIGEST_SIZE);
10581058
if (ret != 0)
1059-
return ret;
1059+
goto cleanup;
10601060

10611061
ret = Tls13HKDFExpandLabel(ssl, out, (word32)outLen, firstExpand, hashLen,
10621062
protocol, protocolLen, exporterLabel, EXPORTER_LABEL_SZ,
10631063
hashOut, hashLen, (int)hashType);
10641064

1065+
cleanup:
1066+
/* firstExpand is the per-label Derive-Secret PRK and hashOut holds
1067+
* Hash(context_value); wipe both before the stack frame is reclaimed. */
1068+
ForceZero(firstExpand, sizeof(firstExpand));
1069+
ForceZero(hashOut, sizeof(hashOut));
10651070
return ret;
10661071
}
10671072
#endif

0 commit comments

Comments
 (0)