Skip to content

Commit c3f8702

Browse files
committed
Fix NULL for the key parameter
1 parent 2f4cd41 commit c3f8702

1 file changed

Lines changed: 58 additions & 7 deletions

File tree

src/tpm2_wrap.c

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7517,26 +7517,65 @@ static int CSR_MakeAndSign_Cb(WOLFTPM2_DEV* dev, WOLFTPM2_CSR* csr,
75177517
{
75187518
int rc = 0;
75197519
TpmSignCbCtx signCtx;
7520+
union {
7521+
#ifndef NO_RSA
7522+
RsaKey rsa;
7523+
#endif
7524+
#ifdef HAVE_ECC
7525+
ecc_key ecc;
7526+
#endif
7527+
} wolfKey;
75207528

75217529
if (dev == NULL || csr == NULL || key == NULL || out == NULL) {
75227530
return BAD_FUNC_ARG;
75237531
}
75247532

7533+
XMEMSET(&wolfKey, 0, sizeof(wolfKey));
7534+
7535+
/* Extract public key from TPM key into wolfCrypt key structure */
7536+
if (keyType == ECC_TYPE) {
7537+
#ifdef HAVE_ECC
7538+
rc = wc_ecc_init(&wolfKey.ecc);
7539+
if (rc == 0) {
7540+
/* load public portion of key into wolf ECC Key */
7541+
rc = wolfTPM2_EccKey_TpmToWolf(dev, key, &wolfKey.ecc);
7542+
}
7543+
#else
7544+
rc = NOT_COMPILED_IN;
7545+
#endif
7546+
}
7547+
else if (keyType == RSA_TYPE) {
7548+
#ifndef NO_RSA
7549+
rc = wc_InitRsaKey(&wolfKey.rsa, NULL);
7550+
if (rc == 0) {
7551+
/* load public portion of key into wolf RSA Key */
7552+
rc = wolfTPM2_RsaKey_TpmToWolf(dev, key, &wolfKey.rsa);
7553+
}
7554+
#else
7555+
rc = NOT_COMPILED_IN;
7556+
#endif
7557+
}
7558+
else {
7559+
rc = BAD_FUNC_ARG;
7560+
}
7561+
75257562
/* Setup signing context */
7526-
signCtx.dev = dev;
7527-
signCtx.key = key;
7563+
if (rc == 0) {
7564+
signCtx.dev = dev;
7565+
signCtx.key = key;
7566+
}
75287567

7529-
/* Create certificate body */
7530-
if (selfSignCert) {
7568+
/* Create certificate body with public key */
7569+
if (rc == 0 && selfSignCert) {
75317570
#ifdef WOLFSSL_CERT_GEN
7532-
rc = wc_MakeCert_ex(&csr->req, out, outSz, keyType, NULL,
7571+
rc = wc_MakeCert_ex(&csr->req, out, outSz, keyType, &wolfKey,
75337572
wolfTPM2_GetRng(dev));
75347573
#else
75357574
rc = NOT_COMPILED_IN;
75367575
#endif
75377576
}
7538-
else {
7539-
rc = wc_MakeCertReq_ex(&csr->req, out, outSz, keyType, NULL);
7577+
if (rc == 0 && !selfSignCert) {
7578+
rc = wc_MakeCertReq_ex(&csr->req, out, outSz, keyType, &wolfKey);
75407579
}
75417580

75427581
/* Sign using TPM via callback */
@@ -7567,6 +7606,18 @@ static int CSR_MakeAndSign_Cb(WOLFTPM2_DEV* dev, WOLFTPM2_CSR* csr,
75677606
#endif
75687607
}
75697608

7609+
/* Cleanup wolfCrypt key structure */
7610+
if (keyType == ECC_TYPE) {
7611+
#ifdef HAVE_ECC
7612+
wc_ecc_free(&wolfKey.ecc);
7613+
#endif
7614+
}
7615+
else if (keyType == RSA_TYPE) {
7616+
#ifndef NO_RSA
7617+
wc_FreeRsaKey(&wolfKey.rsa);
7618+
#endif
7619+
}
7620+
75707621
return rc;
75717622
}
75727623

0 commit comments

Comments
 (0)