Skip to content

Commit cb49532

Browse files
committed
Zeroize DER buffer in der_to_enc_pem_alloc before free
F-2139 Previously the plaintext private key DER buffer was freed via XFREE without a preceding ForceZero when no password encryption was requested. Track the actual allocation size and zeroize the buffer before release.
1 parent fb64844 commit cb49532

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/pk.c

Lines changed: 6 additions & 1 deletion
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
}

0 commit comments

Comments
 (0)