Skip to content

Commit cfcd384

Browse files
committed
Address copilot feedback
1 parent 6d6d0ab commit cfcd384

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

tests/api/test_pkcs7.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,9 @@ static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
490490
/* ECC sign raw digest callback
491491
* This callback demonstrates HSM/secure element use case where the private
492492
* key is not passed through PKCS7 structure but obtained independently.
493+
* Note: This example callback is hash-agnostic and will work with any
494+
* hash algorithm. The hashOID parameter can be used to validate or select
495+
* different signing behavior if needed.
493496
*/
494497
static int eccSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
495498
byte* out, word32 outSz, byte* privateKey,
@@ -498,7 +501,11 @@ static int eccSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
498501
int ret;
499502
word32 idx = 0;
500503
word32 sigSz = outSz;
501-
ecc_key ecc;
504+
#ifdef WOLFSSL_SMALL_STACK
505+
ecc_key* ecc = NULL;
506+
#else
507+
ecc_key ecc[1];
508+
#endif
502509

503510
/* privateKey may be NULL in HSM/secure element use case - we load it
504511
* independently in this callback to simulate that scenario */
@@ -510,15 +517,25 @@ static int eccSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
510517
return -1;
511518
}
512519

520+
#ifdef WOLFSSL_SMALL_STACK
521+
ecc = (ecc_key*)XMALLOC(sizeof(ecc_key), pkcs7->heap, DYNAMIC_TYPE_ECC);
522+
if (ecc == NULL) {
523+
return MEMORY_E;
524+
}
525+
#endif
526+
513527
/* set up ECC key */
514-
ret = wc_ecc_init_ex(&ecc, pkcs7->heap, devid);
528+
ret = wc_ecc_init_ex(ecc, pkcs7->heap, devid);
515529
if (ret != 0) {
530+
#ifdef WOLFSSL_SMALL_STACK
531+
XFREE(ecc, pkcs7->heap, DYNAMIC_TYPE_ECC);
532+
#endif
516533
return ret;
517534
}
518535

519536
/* Load key from test buffer - simulates HSM/secure element access */
520537
#if defined(USE_CERT_BUFFERS_256)
521-
ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, &ecc,
538+
ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, ecc,
522539
sizeof_ecc_clikey_der_256);
523540
#else
524541
{
@@ -528,28 +545,37 @@ static int eccSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
528545

529546
fp = XFOPEN("./certs/client-ecc-key.der", "rb");
530547
if (fp == XBADFILE) {
531-
wc_ecc_free(&ecc);
548+
wc_ecc_free(ecc);
549+
#ifdef WOLFSSL_SMALL_STACK
550+
XFREE(ecc, pkcs7->heap, DYNAMIC_TYPE_ECC);
551+
#endif
532552
return -1;
533553
}
534554
keySz = (int)XFREAD(keyBuf, 1, sizeof(keyBuf), fp);
535555
XFCLOSE(fp);
536556
if (keySz <= 0) {
537-
wc_ecc_free(&ecc);
557+
wc_ecc_free(ecc);
558+
#ifdef WOLFSSL_SMALL_STACK
559+
XFREE(ecc, pkcs7->heap, DYNAMIC_TYPE_ECC);
560+
#endif
538561
return -1;
539562
}
540-
ret = wc_EccPrivateKeyDecode(keyBuf, &idx, &ecc, (word32)keySz);
563+
ret = wc_EccPrivateKeyDecode(keyBuf, &idx, ecc, (word32)keySz);
541564
}
542565
#endif
543566

544567
/* sign digest */
545568
if (ret == 0) {
546-
ret = wc_ecc_sign_hash(digest, digestSz, out, &sigSz, pkcs7->rng, &ecc);
569+
ret = wc_ecc_sign_hash(digest, digestSz, out, &sigSz, pkcs7->rng, ecc);
547570
if (ret == 0) {
548571
ret = (int)sigSz;
549572
}
550573
}
551574

552-
wc_ecc_free(&ecc);
575+
wc_ecc_free(ecc);
576+
#ifdef WOLFSSL_SMALL_STACK
577+
XFREE(ecc, pkcs7->heap, DYNAMIC_TYPE_ECC);
578+
#endif
553579

554580
return ret;
555581
}

wolfcrypt/src/pkcs7.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,13 +2422,21 @@ static int wc_PKCS7_SignedDataBuildSignature(wc_PKCS7* pkcs7,
24222422
if (pkcs7->eccSignRawDigestCb != NULL) {
24232423
/* get hash OID */
24242424
int eccHashOID = wc_HashGetOID(esd->hashType);
2425+
if (eccHashOID < 0) {
2426+
ret = eccHashOID;
2427+
break;
2428+
}
24252429

24262430
/* user signing plain digest */
24272431
ret = pkcs7->eccSignRawDigestCb(pkcs7,
24282432
esd->contentAttribsDigest, hashSz,
24292433
esd->encContentDigest, sizeof(esd->encContentDigest),
24302434
pkcs7->privateKey, pkcs7->privateKeySz, pkcs7->devId,
24312435
eccHashOID);
2436+
/* validate return value doesn't exceed buffer size */
2437+
if (ret > 0 && (word32)ret > sizeof(esd->encContentDigest)) {
2438+
ret = BUFFER_E;
2439+
}
24322440
break;
24332441
}
24342442
#endif

0 commit comments

Comments
 (0)