Skip to content

Commit 7a23cff

Browse files
add PKCS7 set custom SKID
1 parent d796d8c commit 7a23cff

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

wolfcrypt/src/pkcs7.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,12 @@ void wc_PKCS7_Free(PKCS7* pkcs7)
13801380
pkcs7->isDynamic = 0;
13811381
XFREE(pkcs7, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
13821382
}
1383+
1384+
if (pkcs7->customSKID) {
1385+
XFREE(pkcs7->customSKID, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
1386+
pkcs7->customSKID = NULL;
1387+
pkcs7->customSKIDSz = 0;
1388+
}
13831389
}
13841390

13851391

@@ -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,28 @@ 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, 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+
pkcs7->customSKID = (byte*)XMALLOC(inSz, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
3455+
if (pkcs7->customSKID == NULL) {
3456+
ret = MEMORY_E;
3457+
}
3458+
else {
3459+
XMEMCPY(pkcs7->customSKID, in, inSz);
3460+
}
3461+
return ret;
3462+
}
3463+
3464+
34213465
/* Toggle detached signature mode on/off for PKCS#7/CMS SignedData content type.
34223466
* By default wolfCrypt includes the data to be signed in the SignedData
34233467
* bundle. This data can be omitted in the case when a detached signature is

wolfssl/wolfcrypt/pkcs7.h

Lines changed: 9 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,7 @@ 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, byte* in, word16 inSz);
390399
WOLFSSL_API int wc_PKCS7_SetDetached(PKCS7* pkcs7, word16 flag);
391400
WOLFSSL_API int wc_PKCS7_NoDefaultSignedAttribs(PKCS7* pkcs7);
392401
WOLFSSL_API int wc_PKCS7_SetDefaultSignedAttribs(PKCS7* pkcs7, word16 flag);

0 commit comments

Comments
 (0)