-
Notifications
You must be signed in to change notification settings - Fork 922
Add PKCS7 ECC raw sign callback support #9656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1794,6 +1794,16 @@ static int wc_PKCS7_ImportRSA(wc_PKCS7* pkcs7, RsaKey* privKey) | |
| } | ||
| #endif | ||
| } | ||
| #ifdef HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK | ||
| else if (pkcs7->rsaSignRawDigestCb != NULL && pkcs7->publicKeySz > 0) { | ||
| /* When using raw sign callback (e.g., HSM/secure element), private | ||
| * key may not be available. Use public key from signer certificate | ||
| * for signature size calculation. */ | ||
| idx = 0; | ||
| ret = wc_RsaPublicKeyDecode(pkcs7->publicKey, &idx, privKey, | ||
| pkcs7->publicKeySz); | ||
| } | ||
| #endif | ||
| else if (pkcs7->devId == INVALID_DEVID) { | ||
| ret = BAD_FUNC_ARG; | ||
| } | ||
|
|
@@ -1874,6 +1884,16 @@ static int wc_PKCS7_ImportECC(wc_PKCS7* pkcs7, ecc_key* privKey) | |
| } | ||
| #endif | ||
| } | ||
| #ifdef HAVE_PKCS7_ECC_RAW_SIGN_CALLBACK | ||
| else if (pkcs7->eccSignRawDigestCb != NULL && pkcs7->publicKeySz > 0) { | ||
| /* When using raw sign callback (e.g., HSM/secure element), private | ||
| * key may not be available. Use public key from signer certificate | ||
| * for signature size calculation. */ | ||
| idx = 0; | ||
| ret = wc_EccPublicKeyDecode(pkcs7->publicKey, &idx, privKey, | ||
| pkcs7->publicKeySz); | ||
| } | ||
| #endif | ||
| else if (pkcs7->devId == INVALID_DEVID) { | ||
| ret = BAD_FUNC_ARG; | ||
| } | ||
|
|
@@ -2398,6 +2418,20 @@ static int wc_PKCS7_SignedDataBuildSignature(wc_PKCS7* pkcs7, | |
|
|
||
| #ifdef HAVE_ECC | ||
| case ECDSAk: | ||
| #ifdef HAVE_PKCS7_ECC_RAW_SIGN_CALLBACK | ||
| if (pkcs7->eccSignRawDigestCb != NULL) { | ||
| /* get hash OID */ | ||
| int eccHashOID = wc_HashGetOID(esd->hashType); | ||
|
|
||
| /* user signing plain digest */ | ||
| ret = pkcs7->eccSignRawDigestCb(pkcs7, | ||
| esd->contentAttribsDigest, hashSz, | ||
| esd->encContentDigest, sizeof(esd->encContentDigest), | ||
| pkcs7->privateKey, pkcs7->privateKeySz, pkcs7->devId, | ||
| eccHashOID); | ||
| break; | ||
| } | ||
|
Comment on lines
+2426
to
+2433
|
||
| #endif | ||
| /* CMS with ECDSA does not sign DigestInfo structure | ||
| * like PKCS#7 with RSA does */ | ||
| ret = wc_PKCS7_EcdsaSign(pkcs7, esd->contentAttribsDigest, | ||
|
|
@@ -3986,6 +4020,30 @@ int wc_PKCS7_SetRsaSignRawDigestCb(wc_PKCS7* pkcs7, CallbackRsaSignRawDigest cb) | |
| } | ||
| #endif | ||
|
|
||
| #endif /* NO_RSA */ | ||
|
|
||
|
|
||
| #ifdef HAVE_ECC | ||
|
|
||
| #ifdef HAVE_PKCS7_ECC_RAW_SIGN_CALLBACK | ||
| /* register raw ECC sign digest callback */ | ||
| int wc_PKCS7_SetEccSignRawDigestCb(wc_PKCS7* pkcs7, CallbackEccSignRawDigest cb) | ||
| { | ||
| if (pkcs7 == NULL || cb == NULL) { | ||
| return BAD_FUNC_ARG; | ||
| } | ||
|
|
||
| pkcs7->eccSignRawDigestCb = cb; | ||
|
|
||
| return 0; | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* HAVE_ECC */ | ||
|
|
||
|
|
||
| #ifndef NO_RSA | ||
|
|
||
| /* returns size of signature put into out, negative on error */ | ||
| static int wc_PKCS7_RsaVerify(wc_PKCS7* pkcs7, byte* sig, int sigSz, | ||
| byte* hash, word32 hashSz) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ECC raw-sign example callback comment says it assumes SHA-256, but the implementation explicitly ignores
hashOID(and doesn’t validatedigestSzfor SHA-256). Either validatehashOID == SHA256h(and expected digest size) to match the documented assumption, or update the comment to indicate the callback is hash-agnostic.