Skip to content

Commit a05dd20

Browse files
committed
Zeroize DSA DER buffer in PEM write before free
F-2140 wolfSSL_PEM_write_mem_DSAPrivateKey serializes the DSA private key to a heap DER buffer and freed it on five paths without ForceZero. Zeroize the buffer before each XFREE.
1 parent cb49532 commit a05dd20

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

src/pk.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,6 +2109,7 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
21092109
derSz = wc_DsaKeyToDer((DsaKey*)dsa->internal, derBuf, (word32)der_max_len);
21102110
if (derSz < 0) {
21112111
WOLFSSL_MSG("wc_DsaKeyToDer failed");
2112+
ForceZero(derBuf, (word32)der_max_len);
21122113
XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
21132114
return 0;
21142115
}
@@ -2121,6 +2122,7 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
21212122
&cipherInfo, der_max_len, WC_MD5);
21222123
if (ret != 1) {
21232124
WOLFSSL_MSG("EncryptDerKey failed");
2125+
ForceZero(derBuf, (word32)der_max_len);
21242126
XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
21252127
return ret;
21262128
}
@@ -2136,6 +2138,7 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
21362138
tmp = (byte*)XMALLOC((size_t)*pLen, NULL, DYNAMIC_TYPE_PEM);
21372139
if (tmp == NULL) {
21382140
WOLFSSL_MSG("malloc failed");
2141+
ForceZero(derBuf, (word32)der_max_len);
21392142
XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
21402143
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
21412144
return 0;
@@ -2146,11 +2149,13 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
21462149
type);
21472150
if (*pLen <= 0) {
21482151
WOLFSSL_MSG("wc_DerToPemEx failed");
2152+
ForceZero(derBuf, (word32)der_max_len);
21492153
XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
21502154
XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
21512155
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
21522156
return 0;
21532157
}
2158+
ForceZero(derBuf, (word32)der_max_len);
21542159
XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
21552160
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
21562161

0 commit comments

Comments
 (0)