Skip to content

Commit 3c67abc

Browse files
authored
Merge pull request #7954 from JacobBarthelmeh/pkcs7
add option to set custom SKID with PKCS7 bundle creation
2 parents 554d52b + 8017c81 commit 3c67abc

3 files changed

Lines changed: 99 additions & 2 deletions

File tree

tests/api.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51094,6 +51094,36 @@ static int test_wc_PKCS7_signed_enveloped(void)
5109451094
pkcs7 = NULL;
5109551095
#endif /* !NO_PKCS7_STREAM */
5109651096
#endif
51097+
51098+
{
51099+
/* arbitrary custom SKID */
51100+
const byte customSKID[] = {
51101+
0x40, 0x25, 0x77, 0x56
51102+
};
51103+
51104+
ExpectIntEQ(wc_InitRng(&rng), 0);
51105+
sigSz = FOURK_BUF * 2;
51106+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
51107+
if (pkcs7 != NULL) {
51108+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
51109+
pkcs7->content = cert;
51110+
pkcs7->contentSz = (word32)certSz;
51111+
pkcs7->contentOID = DATA;
51112+
pkcs7->privateKey = key;
51113+
pkcs7->privateKeySz = (word32)keySz;
51114+
pkcs7->encryptOID = RSAk;
51115+
pkcs7->hashOID = SHA256h;
51116+
pkcs7->rng = &rng;
51117+
ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
51118+
ExpectIntEQ(wc_PKCS7_SetCustomSKID(pkcs7, customSKID,
51119+
sizeof(customSKID)), 0);
51120+
ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig,
51121+
(word32)sigSz)), 0);
51122+
}
51123+
wc_PKCS7_Free(pkcs7);
51124+
pkcs7 = NULL;
51125+
wc_FreeRng(&rng);
51126+
}
5109751127
#endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
5109851128
return EXPECT_RESULT();
5109951129
}

wolfcrypt/src/pkcs7.c

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,12 @@ void wc_PKCS7_Free(PKCS7* pkcs7)
13761376
pkcs7->cachedEncryptedContentSz = 0;
13771377
}
13781378

1379+
if (pkcs7->customSKID) {
1380+
XFREE(pkcs7->customSKID, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
1381+
pkcs7->customSKID = NULL;
1382+
pkcs7->customSKIDSz = 0;
1383+
}
1384+
13791385
if (pkcs7->isDynamic) {
13801386
pkcs7->isDynamic = 0;
13811387
XFREE(pkcs7, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
@@ -2816,6 +2822,15 @@ static int PKCS7_EncodeSigned(PKCS7* pkcs7,
28162822
keyIdSize = KEYID_SIZE;
28172823
#endif
28182824

2825+
/* use custom SKID if set */
2826+
if (pkcs7->customSKIDSz > 0) {
2827+
if (pkcs7->customSKID == NULL) {
2828+
WOLFSSL_MSG("Bad custom SKID setup, size > 0 and was NULL");
2829+
return BAD_FUNC_ARG;
2830+
}
2831+
keyIdSize = pkcs7->customSKIDSz;
2832+
}
2833+
28192834
#ifdef WOLFSSL_SMALL_STACK
28202835
signedDataOid = (byte *)XMALLOC(MAX_OID_SZ, pkcs7->heap, DYNAMIC_TYPE_TMP_BUFFER);
28212836
if (signedDataOid == NULL) {
@@ -3264,8 +3279,15 @@ static int PKCS7_EncodeSigned(PKCS7* pkcs7,
32643279
wc_PKCS7_WriteOut(pkcs7, (output2)? (output2 + idx) : NULL,
32653280
esd->issuerSKID, esd->issuerSKIDSz);
32663281
idx += (int)esd->issuerSKIDSz;
3267-
wc_PKCS7_WriteOut(pkcs7, (output2)? (output2 + idx) : NULL,
3282+
3283+
if (pkcs7->customSKID) {
3284+
wc_PKCS7_WriteOut(pkcs7, (output2)? (output2 + idx) : NULL,
3285+
pkcs7->customSKID, (word32)keyIdSize);
3286+
}
3287+
else {
3288+
wc_PKCS7_WriteOut(pkcs7, (output2)? (output2 + idx) : NULL,
32683289
pkcs7->issuerSubjKeyId, (word32)keyIdSize);
3290+
}
32693291
idx += keyIdSize;
32703292
} else if (pkcs7->sidType == DEGENERATE_SID) {
32713293
/* no signer infos in degenerate case */
@@ -3418,6 +3440,40 @@ int wc_PKCS7_EncodeSignedData_ex(PKCS7* pkcs7, const byte* hashBuf,
34183440
return ret;
34193441
}
34203442

3443+
3444+
/* Sets a custom SKID in PKCS7 struct, used before calling an encode operation
3445+
* Returns 0 on success, negative upon error. */
3446+
int wc_PKCS7_SetCustomSKID(PKCS7* pkcs7, const byte* in, word16 inSz)
3447+
{
3448+
int ret = 0;
3449+
3450+
if (pkcs7 == NULL || (in == NULL && inSz > 0)) {
3451+
return BAD_FUNC_ARG;
3452+
}
3453+
3454+
if (in == NULL) {
3455+
if (pkcs7->customSKID != NULL) {
3456+
XFREE(pkcs7->customSKID, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
3457+
}
3458+
pkcs7->customSKIDSz = 0;
3459+
pkcs7->customSKID = NULL;
3460+
}
3461+
else {
3462+
pkcs7->customSKID = (byte*)XMALLOC(inSz, pkcs7->heap,
3463+
DYNAMIC_TYPE_PKCS7);
3464+
if (pkcs7->customSKID == NULL) {
3465+
ret = MEMORY_E;
3466+
}
3467+
else {
3468+
XMEMCPY(pkcs7->customSKID, in, inSz);
3469+
pkcs7->customSKIDSz = inSz;
3470+
}
3471+
}
3472+
3473+
return ret;
3474+
}
3475+
3476+
34213477
/* Toggle detached signature mode on/off for PKCS#7/CMS SignedData content type.
34223478
* By default wolfCrypt includes the data to be signed in the SignedData
34233479
* bundle. This data can be omitted in the case when a detached signature is
@@ -9589,8 +9645,9 @@ int wc_PKCS7_EncodeEnvelopedData(PKCS7* pkcs7, byte* output, word32 outputSz)
95899645
}
95909646

95919647
#ifndef ASN_BER_TO_DER
9592-
if (output == NULL || outputSz == 0)
9648+
if (output == NULL || outputSz == 0) {
95939649
return BAD_FUNC_ARG;
9650+
}
95949651
#else
95959652
/* if both output and callback are not set then error out */
95969653
if ((output == NULL || outputSz == 0) && (pkcs7->streamOutCb == NULL)) {

wolfssl/wolfcrypt/pkcs7.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,14 @@ struct PKCS7 {
359359
word16 contentCRLF:1; /* have content line endings been converted to CRLF */
360360
word16 contentIsPkcs7Type:1; /* eContent follows PKCS#7 RFC not CMS */
361361
word16 hashParamsAbsent:1;
362+
363+
/* RFC 5280 section-4.2.1.2 lists a possible method for creating the SKID as
364+
* a SHA1 hash of the public key, but leaves it open to other methods as
365+
* long as it is a unique ID. This allows for setting a custom SKID when
366+
* creating PKCS7 bundles*/
367+
byte* customSKID;
368+
word16 customSKIDSz;
369+
362370
/* !! NEW DATA MEMBERS MUST BE ADDED AT END !! */
363371
};
364372

@@ -387,6 +395,8 @@ WOLFSSL_API int wc_PKCS7_EncodeData(PKCS7* pkcs7, byte* output,
387395
word32 outputSz);
388396

389397
/* CMS/PKCS#7 SignedData */
398+
WOLFSSL_API int wc_PKCS7_SetCustomSKID(PKCS7* pkcs7, const byte* in,
399+
word16 inSz);
390400
WOLFSSL_API int wc_PKCS7_SetDetached(PKCS7* pkcs7, word16 flag);
391401
WOLFSSL_API int wc_PKCS7_NoDefaultSignedAttribs(PKCS7* pkcs7);
392402
WOLFSSL_API int wc_PKCS7_SetDefaultSignedAttribs(PKCS7* pkcs7, word16 flag);

0 commit comments

Comments
 (0)