Skip to content

Commit 69f0463

Browse files
committed
Address PR review feedback: add WOLFSSL_CERT_SIGN_CB guards, input validation, SKID extension, and -signcb example option
1 parent fcc6401 commit 69f0463

3 files changed

Lines changed: 49 additions & 22 deletions

File tree

.github/workflows/make-test-swtpm.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
- name: certsigncb
6868
wolfssl_config: --enable-wolftpm --enable-pkcallbacks --enable-certsigncb
6969
wolftpm_config: --enable-swtpm --enable-certgen
70-
test_command: "make check && WOLFSSL_PATH=./wolfssl ./examples/run_examples.sh"
70+
test_command: "make check && WOLFSSL_PATH=./wolfssl ./examples/run_examples.sh && ./examples/csr/csr -signcb && ./examples/csr/csr -signcb -cert"
7171

7272
# STMicro ST33KTPM2
7373
- name: st33ktpm2

examples/csr/csr.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ static const char* gClientCertEccFile = ECC_CERT_PEM;
8686
* - Maintains backward compatibility
8787
* - Requires crypto callback setup
8888
*
89-
* This example demonstrates the crypto callback approach. To use the new
90-
* callback-based approach, pass INVALID_DEVID instead of tpmDevId when calling
91-
* TPM2_CSR_Generate() or wolfTPM2_CSR_MakeAndSign_ex().
89+
* This example demonstrates both approaches. By default it uses the crypto
90+
* callback approach. Use the -signcb option to use the new callback-based
91+
* approach, which passes INVALID_DEVID to wolfTPM2_CSR_MakeAndSign_ex().
9292
*/
9393

9494
static int TPM2_CSR_Generate(WOLFTPM2_DEV* dev, int keyType, WOLFTPM2_KEY* key,
@@ -173,9 +173,11 @@ static int TPM2_CSR_Generate(WOLFTPM2_DEV* dev, int keyType, WOLFTPM2_KEY* key,
173173
static void usage(void)
174174
{
175175
printf("Expected usage:\n");
176-
printf("./examples/csr/csr [-cert]\n");
176+
printf("./examples/csr/csr [-cert] [-signcb]\n");
177177
printf("\t-cert: Make self signed certificate instead of "
178178
"default CSR (Certificate Signing Request)\n");
179+
printf("\t-signcb: Use wc_SignCert_cb callback-based signing "
180+
"(FIPS compliant, requires WOLFSSL_CERT_SIGN_CB)\n");
179181
}
180182

181183
int TPM2_CSR_Example(void* userCtx)
@@ -192,6 +194,7 @@ int TPM2_CSR_ExampleArgs(void* userCtx, int argc, char *argv[])
192194
int tpmDevId;
193195
TPMT_PUBLIC publicTemplate;
194196
int makeSelfSignedCert = 0;
197+
int useSignCb = 0;
195198

196199
printf("TPM2 CSR Example\n");
197200

@@ -207,6 +210,9 @@ int TPM2_CSR_ExampleArgs(void* userCtx, int argc, char *argv[])
207210
if (XSTRCMP(argv[argc-1], "-cert") == 0) {
208211
makeSelfSignedCert = 1;
209212
}
213+
else if (XSTRCMP(argv[argc-1], "-signcb") == 0) {
214+
useSignCb = 1;
215+
}
210216
else {
211217
printf("Warning: Unrecognized option: %s\n", argv[argc-1]);
212218
}
@@ -245,7 +251,8 @@ int TPM2_CSR_ExampleArgs(void* userCtx, int argc, char *argv[])
245251
if (rc == 0) {
246252
rc = TPM2_CSR_Generate(&dev, RSA_TYPE, &key,
247253
makeSelfSignedCert ? gClientCertRsaFile : gClientCsrRsaFile,
248-
makeSelfSignedCert, tpmDevId, CTC_SHA256wRSA);
254+
makeSelfSignedCert,
255+
useSignCb ? INVALID_DEVID : tpmDevId, CTC_SHA256wRSA);
249256
}
250257
wolfTPM2_UnloadHandle(&dev, &key.handle);
251258
wolfTPM2_UnloadHandle(&dev, &storageKey.handle);
@@ -278,7 +285,8 @@ int TPM2_CSR_ExampleArgs(void* userCtx, int argc, char *argv[])
278285
if (rc == 0) {
279286
rc = TPM2_CSR_Generate(&dev, ECC_TYPE, &key,
280287
makeSelfSignedCert ? gClientCertEccFile : gClientCsrEccFile,
281-
makeSelfSignedCert, tpmDevId, sigType);
288+
makeSelfSignedCert,
289+
useSignCb ? INVALID_DEVID : tpmDevId, sigType);
282290
}
283291
wolfTPM2_UnloadHandle(&dev, &key.handle);
284292
wolfTPM2_UnloadHandle(&dev, &storageKey.handle);

src/tpm2_wrap.c

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7391,13 +7391,19 @@ static int wolfTPM2_SignCertCb(const byte* in, word32 inLen,
73917391
byte *r, *s;
73927392
word32 rLen, sLen;
73937393

7394-
/* Split R and S from concatenated signature */
7395-
rLen = sLen = rsLen / 2;
7396-
r = &sigRS[0];
7397-
s = &sigRS[rLen];
7394+
/* Validate concatenated R||S length before splitting */
7395+
if (rsLen <= 0 || (rsLen & 1) != 0) {
7396+
rc = BAD_FUNC_ARG;
7397+
}
7398+
else {
7399+
/* Split R and S from concatenated signature */
7400+
rLen = sLen = (word32)(rsLen / 2);
7401+
r = &sigRS[0];
7402+
s = &sigRS[rLen];
73987403

7399-
/* Encode as DER ECDSA signature */
7400-
rc = wc_ecc_rs_raw_to_sig(r, rLen, s, sLen, out, outLen);
7404+
/* Encode as DER ECDSA signature */
7405+
rc = wc_ecc_rs_raw_to_sig(r, rLen, s, sLen, out, outLen);
7406+
}
74017407
}
74027408
#else
74037409
rc = NOT_COMPILED_IN;
@@ -7515,6 +7521,7 @@ static int CSR_MakeAndSign_Cb(WOLFTPM2_DEV* dev, WOLFTPM2_CSR* csr,
75157521
int selfSignCert)
75167522
{
75177523
int rc = 0;
7524+
int keyInited = 0;
75187525
TpmSignCbCtx signCtx;
75197526
union {
75207527
#ifndef NO_RSA
@@ -7529,13 +7536,15 @@ static int CSR_MakeAndSign_Cb(WOLFTPM2_DEV* dev, WOLFTPM2_CSR* csr,
75297536
return BAD_FUNC_ARG;
75307537
}
75317538

7539+
XMEMSET(&signCtx, 0, sizeof(signCtx));
75327540
XMEMSET(&wolfKey, 0, sizeof(wolfKey));
75337541

75347542
/* Extract public key from TPM key into wolfCrypt key structure */
75357543
if (keyType == ECC_TYPE) {
75367544
#ifdef HAVE_ECC
75377545
rc = wc_ecc_init(&wolfKey.ecc);
75387546
if (rc == 0) {
7547+
keyInited = 1;
75397548
/* load public portion of key into wolf ECC Key */
75407549
rc = wolfTPM2_EccKey_TpmToWolf(dev, key, &wolfKey.ecc);
75417550
}
@@ -7547,6 +7556,7 @@ static int CSR_MakeAndSign_Cb(WOLFTPM2_DEV* dev, WOLFTPM2_CSR* csr,
75477556
#ifndef NO_RSA
75487557
rc = wc_InitRsaKey(&wolfKey.rsa, NULL);
75497558
if (rc == 0) {
7559+
keyInited = 1;
75507560
/* load public portion of key into wolf RSA Key */
75517561
rc = wolfTPM2_RsaKey_TpmToWolf(dev, key, &wolfKey.rsa);
75527562
}
@@ -7564,6 +7574,13 @@ static int CSR_MakeAndSign_Cb(WOLFTPM2_DEV* dev, WOLFTPM2_CSR* csr,
75647574
signCtx.key = key;
75657575
}
75667576

7577+
#ifdef WOLFSSL_CERT_EXT
7578+
/* add SKID from the Public Key */
7579+
if (rc == 0) {
7580+
rc = wc_SetSubjectKeyIdFromPublicKey_ex(&csr->req, keyType, &wolfKey);
7581+
}
7582+
#endif
7583+
75677584
/* Create certificate body with public key */
75687585
if (rc == 0 && selfSignCert) {
75697586
#ifdef WOLFSSL_CERT_GEN
@@ -7606,15 +7623,17 @@ static int CSR_MakeAndSign_Cb(WOLFTPM2_DEV* dev, WOLFTPM2_CSR* csr,
76067623
}
76077624

76087625
/* Cleanup wolfCrypt key structure */
7609-
if (keyType == ECC_TYPE) {
7610-
#ifdef HAVE_ECC
7611-
wc_ecc_free(&wolfKey.ecc);
7612-
#endif
7613-
}
7614-
else if (keyType == RSA_TYPE) {
7615-
#ifndef NO_RSA
7616-
wc_FreeRsaKey(&wolfKey.rsa);
7617-
#endif
7626+
if (keyInited) {
7627+
if (keyType == ECC_TYPE) {
7628+
#ifdef HAVE_ECC
7629+
wc_ecc_free(&wolfKey.ecc);
7630+
#endif
7631+
}
7632+
else if (keyType == RSA_TYPE) {
7633+
#ifndef NO_RSA
7634+
wc_FreeRsaKey(&wolfKey.rsa);
7635+
#endif
7636+
}
76187637
}
76197638

76207639
return rc;

0 commit comments

Comments
 (0)