@@ -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 )) {
0 commit comments