-
Notifications
You must be signed in to change notification settings - Fork 960
Fenrir fixes #10247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fenrir fixes #10247
Changes from all commits
cb49532
a05dd20
dfd37f4
4c8adc5
9790719
fa3feb7
a82828b
9925f90
87e5c62
00fff0f
1e04092
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -480,6 +480,7 @@ static int der_to_enc_pem_alloc(unsigned char* der, int derSz, | |
| byte* tmp = NULL; | ||
| byte* cipherInfo = NULL; | ||
| int pemSz = 0; | ||
| int derAllocSz = derSz; | ||
| int hashType = WC_HASH_TYPE_NONE; | ||
| #if !defined(NO_MD5) | ||
| hashType = WC_MD5; | ||
|
|
@@ -515,6 +516,7 @@ static int der_to_enc_pem_alloc(unsigned char* der, int derSz, | |
| } | ||
| else { | ||
| der = tmpBuf; | ||
| derAllocSz = derSz + blockSz; | ||
|
|
||
| /* Encrypt DER inline. */ | ||
| ret = EncryptDerKey(der, &derSz, cipher, passwd, passwdSz, | ||
|
|
@@ -562,7 +564,10 @@ static int der_to_enc_pem_alloc(unsigned char* der, int derSz, | |
|
|
||
| XFREE(tmp, NULL, DYNAMIC_TYPE_KEY); | ||
| XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING); | ||
| XFREE(der, heap, DYNAMIC_TYPE_TMP_BUFFER); | ||
| if (der != NULL) { | ||
| ForceZero(der, (word32)derAllocSz); | ||
| XFREE(der, heap, DYNAMIC_TYPE_TMP_BUFFER); | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
@@ -2104,6 +2109,7 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa, | |
| derSz = wc_DsaKeyToDer((DsaKey*)dsa->internal, derBuf, (word32)der_max_len); | ||
| if (derSz < 0) { | ||
| WOLFSSL_MSG("wc_DsaKeyToDer failed"); | ||
| ForceZero(derBuf, (word32)der_max_len); | ||
| XFREE(derBuf, NULL, DYNAMIC_TYPE_DER); | ||
| return 0; | ||
| } | ||
|
|
@@ -2116,6 +2122,7 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa, | |
| &cipherInfo, der_max_len, WC_MD5); | ||
| if (ret != 1) { | ||
| WOLFSSL_MSG("EncryptDerKey failed"); | ||
| ForceZero(derBuf, (word32)der_max_len); | ||
| XFREE(derBuf, NULL, DYNAMIC_TYPE_DER); | ||
| return ret; | ||
| } | ||
|
|
@@ -2131,6 +2138,7 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa, | |
| tmp = (byte*)XMALLOC((size_t)*pLen, NULL, DYNAMIC_TYPE_PEM); | ||
| if (tmp == NULL) { | ||
| WOLFSSL_MSG("malloc failed"); | ||
| ForceZero(derBuf, (word32)der_max_len); | ||
| XFREE(derBuf, NULL, DYNAMIC_TYPE_DER); | ||
| XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING); | ||
| return 0; | ||
|
|
@@ -2141,11 +2149,13 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa, | |
| type); | ||
| if (*pLen <= 0) { | ||
| WOLFSSL_MSG("wc_DerToPemEx failed"); | ||
| ForceZero(derBuf, (word32)der_max_len); | ||
| XFREE(derBuf, NULL, DYNAMIC_TYPE_DER); | ||
| XFREE(tmp, NULL, DYNAMIC_TYPE_PEM); | ||
| XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING); | ||
| return 0; | ||
| } | ||
| ForceZero(derBuf, (word32)der_max_len); | ||
| XFREE(derBuf, NULL, DYNAMIC_TYPE_DER); | ||
| XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING); | ||
|
|
||
|
|
@@ -7107,6 +7117,7 @@ static int pem_write_mem_pkcs8privatekey(byte** pem, int* pemSz, | |
| char password[NAME_SZ]; | ||
| byte* key = NULL; | ||
| word32 keySz = 0; | ||
| word32 allocSz = 0; | ||
| int type = PKCS8_PRIVATEKEY_TYPE; | ||
|
|
||
| /* Validate parameters. */ | ||
|
|
@@ -7145,6 +7156,10 @@ static int pem_write_mem_pkcs8privatekey(byte** pem, int* pemSz, | |
| res = 0; | ||
| } | ||
| else { | ||
| /* Remember the allocation size before *pemSz is updated to the | ||
| * actual PEM output length, so we can zero any unused tail that | ||
| * held the DER staging area. */ | ||
| allocSz = (word32)*pemSz; | ||
|
Comment on lines
+7159
to
+7162
|
||
| /* Use end of PEM buffer for key data. */ | ||
| key = *pem + *pemSz - keySz; | ||
| } | ||
|
|
@@ -7198,6 +7213,20 @@ static int pem_write_mem_pkcs8privatekey(byte** pem, int* pemSz, | |
| } | ||
| } | ||
|
|
||
| /* Zero any remnants of the DER staging area that persist after PEM | ||
| * conversion so plaintext private key material is not left in freed heap | ||
| * memory. On success, only the bytes past the actual PEM output need | ||
| * clearing; on failure, the whole buffer is zeroed since its state is | ||
| * indeterminate. */ | ||
| if (*pem != NULL) { | ||
| if (res == 1 && (word32)*pemSz < allocSz) { | ||
| ForceZero(*pem + *pemSz, allocSz - (word32)*pemSz); | ||
| } | ||
| else if (res != 1) { | ||
| ForceZero(*pem, allocSz); | ||
| } | ||
| } | ||
|
Comment on lines
+7216
to
+7228
|
||
|
|
||
| /* Return appropriate return code. */ | ||
| return (res == 0) ? 0 : ret; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -719,6 +719,9 @@ WOLFSSL_RSA* wolfSSL_d2i_RSAPrivateKey_bio(WOLFSSL_BIO *bio, WOLFSSL_RSA **out) | |||||
| key = NULL; | ||||||
| } | ||||||
| /* Dispose of allocated data. */ | ||||||
| if (der != NULL) { | ||||||
| ForceZero(der, (word32)derLen); | ||||||
| } | ||||||
| XFREE(der, bio ? bio->heap : NULL, DYNAMIC_TYPE_TMP_BUFFER); | ||||||
| return key; | ||||||
| } | ||||||
|
|
@@ -779,6 +782,7 @@ static int wolfSSL_RSA_To_Der_ex(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey, | |||||
| { | ||||||
| int ret = 1; | ||||||
| int derSz = 0; | ||||||
| int derAllocSz = 0; | ||||||
|
||||||
| int derAllocSz = 0; | |
| word32 derAllocSz = 0; |
Copilot
AI
Apr 17, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
derAllocSz is an int that is cast to word32 when passed to ForceZero(). If derSz can ever be negative (or otherwise invalid) on an error path, the cast can wrap to a very large value and cause an out-of-bounds wipe. Make derAllocSz an unsigned size type (e.g., word32/size_t) and only assign/call ForceZero() when the size is known to be > 0 (and ideally set it to the actual allocation size used for derBuf, not just the DER length).
Copilot
AI
Apr 17, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
derAllocSz is an int that is cast to word32 when passed to ForceZero(). If derSz can ever be negative (or otherwise invalid) on an error path, the cast can wrap to a very large value and cause an out-of-bounds wipe. Make derAllocSz an unsigned size type (e.g., word32/size_t) and only assign/call ForceZero() when the size is known to be > 0 (and ideally set it to the actual allocation size used for derBuf, not just the DER length).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new wiping logic depends on
allocSzbeing captured before*pemSzis updated, butallocSzis only set in one branch. If*pemis allocated and a failure occurs before that branch executes,allocSzmay remain 0 and the buffer won’t be cleared even though it may contain sensitive staging data. Consider settingallocSzimmediately after the PEM buffer allocation succeeds (at the point where the allocation size is definitively known) so all subsequent failure paths can reliably wipe the allocation.