Skip to content

Commit c098e53

Browse files
Merge pull request #10247 from julek-wolfssl/fenrir/20260417
Fenrir fixes
2 parents c6f8e84 + a010825 commit c098e53

6 files changed

Lines changed: 51 additions & 2 deletions

File tree

src/pk.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ static int der_to_enc_pem_alloc(unsigned char* der, int derSz,
480480
byte* tmp = NULL;
481481
byte* cipherInfo = NULL;
482482
int pemSz = 0;
483+
int derAllocSz = derSz;
483484
int hashType = WC_HASH_TYPE_NONE;
484485
#if !defined(NO_MD5)
485486
hashType = WC_MD5;
@@ -515,6 +516,7 @@ static int der_to_enc_pem_alloc(unsigned char* der, int derSz,
515516
}
516517
else {
517518
der = tmpBuf;
519+
derAllocSz = derSz + blockSz;
518520

519521
/* Encrypt DER inline. */
520522
ret = EncryptDerKey(der, &derSz, cipher, passwd, passwdSz,
@@ -562,7 +564,10 @@ static int der_to_enc_pem_alloc(unsigned char* der, int derSz,
562564

563565
XFREE(tmp, NULL, DYNAMIC_TYPE_KEY);
564566
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
565-
XFREE(der, heap, DYNAMIC_TYPE_TMP_BUFFER);
567+
if (der != NULL) {
568+
ForceZero(der, (word32)derAllocSz);
569+
XFREE(der, heap, DYNAMIC_TYPE_TMP_BUFFER);
570+
}
566571

567572
return ret;
568573
}
@@ -2104,6 +2109,7 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
21042109
derSz = wc_DsaKeyToDer((DsaKey*)dsa->internal, derBuf, (word32)der_max_len);
21052110
if (derSz < 0) {
21062111
WOLFSSL_MSG("wc_DsaKeyToDer failed");
2112+
ForceZero(derBuf, (word32)der_max_len);
21072113
XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
21082114
return 0;
21092115
}
@@ -2116,6 +2122,7 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
21162122
&cipherInfo, der_max_len, WC_MD5);
21172123
if (ret != 1) {
21182124
WOLFSSL_MSG("EncryptDerKey failed");
2125+
ForceZero(derBuf, (word32)der_max_len);
21192126
XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
21202127
return ret;
21212128
}
@@ -2131,6 +2138,7 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
21312138
tmp = (byte*)XMALLOC((size_t)*pLen, NULL, DYNAMIC_TYPE_PEM);
21322139
if (tmp == NULL) {
21332140
WOLFSSL_MSG("malloc failed");
2141+
ForceZero(derBuf, (word32)der_max_len);
21342142
XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
21352143
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
21362144
return 0;
@@ -2141,11 +2149,13 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
21412149
type);
21422150
if (*pLen <= 0) {
21432151
WOLFSSL_MSG("wc_DerToPemEx failed");
2152+
ForceZero(derBuf, (word32)der_max_len);
21442153
XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
21452154
XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
21462155
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
21472156
return 0;
21482157
}
2158+
ForceZero(derBuf, (word32)der_max_len);
21492159
XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
21502160
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
21512161

@@ -7107,6 +7117,7 @@ static int pem_write_mem_pkcs8privatekey(byte** pem, int* pemSz,
71077117
char password[NAME_SZ];
71087118
byte* key = NULL;
71097119
word32 keySz = 0;
7120+
word32 allocSz = 0;
71107121
int type = PKCS8_PRIVATEKEY_TYPE;
71117122

71127123
/* Validate parameters. */
@@ -7139,9 +7150,11 @@ static int pem_write_mem_pkcs8privatekey(byte** pem, int* pemSz,
71397150
*pemSz += 54;
71407151
}
71417152

7153+
allocSz = (word32)*pemSz;
71427154
/* Allocate enough memory to hold PEM encoded encrypted key. */
7143-
*pem = (byte*)XMALLOC((size_t)*pemSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
7155+
*pem = (byte*)XMALLOC((size_t)allocSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
71447156
if (*pem == NULL) {
7157+
allocSz = 0;
71457158
res = 0;
71467159
}
71477160
else {
@@ -7198,6 +7211,20 @@ static int pem_write_mem_pkcs8privatekey(byte** pem, int* pemSz,
71987211
}
71997212
}
72007213

7214+
/* Zero any remnants of the DER staging area that persist after PEM
7215+
* conversion so plaintext private key material is not left in freed heap
7216+
* memory. On success, only the bytes past the actual PEM output need
7217+
* clearing; on failure, the whole buffer is zeroed since its state is
7218+
* indeterminate. */
7219+
if (*pem != NULL) {
7220+
if (res == 1 && (word32)*pemSz < allocSz) {
7221+
ForceZero(*pem + *pemSz, allocSz - (word32)*pemSz);
7222+
}
7223+
else if (res != 1) {
7224+
ForceZero(*pem, allocSz);
7225+
}
7226+
}
7227+
72017228
/* Return appropriate return code. */
72027229
return (res == 0) ? 0 : ret;
72037230

src/pk_ec.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3524,6 +3524,9 @@ int wolfSSL_i2d_ECPrivateKey(const WOLFSSL_EC_KEY *key, unsigned char **out)
35243524

35253525
/* Dispose of any allocated buffer on error. */
35263526
if (err && (*out == buf)) {
3527+
if (buf != NULL) {
3528+
ForceZero(buf, len);
3529+
}
35273530
XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
35283531
*out = NULL;
35293532
}
@@ -4095,6 +4098,7 @@ int wolfSSL_PEM_write_mem_ECPrivateKey(WOLFSSL_EC_KEY* ec,
40954098
derSz = wc_EccKeyToDer((ecc_key*)ec->internal, derBuf, der_max_len);
40964099
if (derSz < 0) {
40974100
WOLFSSL_MSG("wc_EccKeyToDer failed");
4101+
ForceZero(derBuf, der_max_len);
40984102
XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
40994103
ret = 0;
41004104
}

src/pk_rsa.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,9 @@ WOLFSSL_RSA* wolfSSL_d2i_RSAPrivateKey_bio(WOLFSSL_BIO *bio, WOLFSSL_RSA **out)
719719
key = NULL;
720720
}
721721
/* Dispose of allocated data. */
722+
if (der != NULL) {
723+
ForceZero(der, (word32)derLen);
724+
}
722725
XFREE(der, bio ? bio->heap : NULL, DYNAMIC_TYPE_TMP_BUFFER);
723726
return key;
724727
}
@@ -779,6 +782,7 @@ static int wolfSSL_RSA_To_Der_ex(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey,
779782
{
780783
int ret = 1;
781784
int derSz = 0;
785+
word32 derAllocSz = 0;
782786
byte* derBuf = NULL;
783787

784788
WOLFSSL_ENTER("wolfSSL_RSA_To_Der");
@@ -830,6 +834,9 @@ static int wolfSSL_RSA_To_Der_ex(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey,
830834
WOLFSSL_ERROR_MSG("Memory allocation failed");
831835
ret = MEMORY_ERROR;
832836
}
837+
else {
838+
derAllocSz = (word32)derSz;
839+
}
833840
}
834841
}
835842
if ((ret == 1) && (outBuf != NULL)) {
@@ -863,6 +870,9 @@ static int wolfSSL_RSA_To_Der_ex(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey,
863870

864871
if ((outBuf != NULL) && (*outBuf != derBuf)) {
865872
/* Not returning buffer, needs to be disposed of. */
873+
if ((derBuf != NULL) && (publicKey == 0) && (derAllocSz > 0)) {
874+
ForceZero(derBuf, derAllocSz);
875+
}
866876
XFREE(derBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
867877
}
868878
WOLFSSL_LEAVE("wolfSSL_RSA_To_Der", ret);

src/sniffer.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7242,12 +7242,16 @@ int ssl_SetWatchKey_file(void* vSniffer, const char* keyFile, int keyType,
72427242
ret = LoadKeyFile(&keyBuf, &keyBufSz, keyFile, 0, keyType, password);
72437243
if (ret < 0) {
72447244
SetError(KEY_FILE_STR, error, NULL, 0);
7245+
if (keyBuf != NULL) {
7246+
ForceZero(keyBuf, keyBufSz);
7247+
}
72457248
XFREE(keyBuf, NULL, DYNAMIC_TYPE_X509);
72467249
return WOLFSSL_FATAL_ERROR;
72477250
}
72487251

72497252
ret = ssl_SetWatchKey_buffer(vSniffer, keyBuf, keyBufSz, FILETYPE_DER,
72507253
error);
7254+
ForceZero(keyBuf, keyBufSz);
72517255
XFREE(keyBuf, NULL, DYNAMIC_TYPE_X509);
72527256

72537257
return ret;

src/ssl.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18692,6 +18692,7 @@ static int SetStaticEphemeralKey(WOLFSSL_CTX* ctx,
1869218692
#ifndef NO_FILESYSTEM
1869318693
/* done with keyFile buffer */
1869418694
if (keyFile && keyBuf) {
18695+
ForceZero(keyBuf, keySz);
1869518696
XFREE(keyBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
1869618697
}
1869718698
#endif

src/ssl_load.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5365,6 +5365,9 @@ int wolfSSL_CTX_use_RSAPrivateKey(WOLFSSL_CTX* ctx, WOLFSSL_RSA* rsa)
53655365
}
53665366

53675367
/* Dispos of dynamically allocated data. */
5368+
if (der != NULL) {
5369+
ForceZero(der, (word32)derSize);
5370+
}
53685371
XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
53695372
return ret;
53705373
}

0 commit comments

Comments
 (0)