Skip to content

Commit 4c8adc5

Browse files
committed
Zeroize RSA DER buffer in To_Der error path
F-2142 wolfSSL_RSA_To_Der could free a buffer holding RSA private key material when the DER encoding step failed. Record the allocation size and ForceZero the buffer before XFREE on the private key path.
1 parent dfd37f4 commit 4c8adc5

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

src/pk_rsa.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ static int wolfSSL_RSA_To_Der_ex(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey,
779779
{
780780
int ret = 1;
781781
int derSz = 0;
782+
int derAllocSz = 0;
782783
byte* derBuf = NULL;
783784

784785
WOLFSSL_ENTER("wolfSSL_RSA_To_Der");
@@ -820,6 +821,7 @@ static int wolfSSL_RSA_To_Der_ex(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey,
820821
}
821822
}
822823

824+
derAllocSz = derSz;
823825
if ((ret == 1) && (outBuf != NULL)) {
824826
derBuf = *outBuf;
825827
if (derBuf == NULL) {
@@ -863,6 +865,9 @@ static int wolfSSL_RSA_To_Der_ex(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey,
863865

864866
if ((outBuf != NULL) && (*outBuf != derBuf)) {
865867
/* Not returning buffer, needs to be disposed of. */
868+
if ((derBuf != NULL) && (publicKey == 0)) {
869+
ForceZero(derBuf, (word32)derAllocSz);
870+
}
866871
XFREE(derBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
867872
}
868873
WOLFSSL_LEAVE("wolfSSL_RSA_To_Der", ret);

0 commit comments

Comments
 (0)