@@ -5771,29 +5771,30 @@ static int wc_PKCS7_KariParseRecipCert(WC_PKCS7_KARI* kari, const byte* cert,
57715771 int ret ;
57725772 word32 idx ;
57735773
5774- if (kari == NULL || kari -> decoded == NULL ||
5775- cert == NULL || certSz == 0 )
5774+ if (kari == NULL || kari -> decoded == NULL ) {
57765775 return BAD_FUNC_ARG ;
5776+ }
57775777
57785778 /* decode certificate */
5779- InitDecodedCert (kari -> decoded , (byte * )cert , certSz , kari -> heap );
5780- kari -> decodedInit = 1 ;
5781- ret = ParseCert (kari -> decoded , CA_TYPE , NO_VERIFY , 0 );
5782- if (ret < 0 )
5783- return ret ;
5779+ if (cert != NULL ) {
5780+ InitDecodedCert (kari -> decoded , (byte * )cert , certSz , kari -> heap );
5781+ kari -> decodedInit = 1 ;
5782+ ret = ParseCert (kari -> decoded , CA_TYPE , NO_VERIFY , 0 );
5783+ if (ret < 0 )
5784+ return ret ;
57845785
5785- /* only supports ECDSA for now */
5786- if (kari -> decoded -> keyOID != ECDSAk ) {
5787- WOLFSSL_MSG ("CMS KARI only supports ECDSA key types" );
5788- return BAD_FUNC_ARG ;
5789- }
5786+ /* only supports ECDSA for now */
5787+ if (kari -> decoded -> keyOID != ECDSAk ) {
5788+ WOLFSSL_MSG ("CMS KARI only supports ECDSA key types" );
5789+ return BAD_FUNC_ARG ;
5790+ }
57905791
5791- /* make sure subject key id was read from cert */
5792- if (kari -> decoded -> extSubjKeyIdSet == 0 ) {
5793- WOLFSSL_MSG ("Failed to read subject key ID from recipient cert" );
5794- return BAD_FUNC_ARG ;
5792+ /* make sure subject key id was read from cert */
5793+ if (kari -> decoded -> extSubjKeyIdSet == 0 ) {
5794+ WOLFSSL_MSG ("Failed to read subject key ID from recipient cert" );
5795+ return BAD_FUNC_ARG ;
5796+ }
57955797 }
5796-
57975798 ret = wc_ecc_init_ex (kari -> recipKey , kari -> heap , kari -> devId );
57985799 if (ret != 0 )
57995800 return ret ;
@@ -5802,6 +5803,10 @@ static int wc_PKCS7_KariParseRecipCert(WC_PKCS7_KARI* kari, const byte* cert,
58025803
58035804 /* get recip public key */
58045805 if (kari -> direction == WC_PKCS7_ENCODE ) {
5806+ if (cert == NULL ) {
5807+ WOLFSSL_MSG ("Error recipient cert can not be null with encode" );
5808+ return BAD_FUNC_ARG ;
5809+ }
58055810
58065811 idx = 0 ;
58075812 ret = wc_EccPublicKeyDecode (kari -> decoded -> publicKey , & idx ,
@@ -9270,7 +9275,14 @@ static int wc_PKCS7_KariGetIssuerAndSerialNumber(WC_PKCS7_KARI* kari,
92709275 }
92719276
92729277 /* if we found correct recipient, issuer hashes will match */
9273- if (XMEMCMP (rid , kari -> decoded -> issuerHash , keyIdSize ) == 0 ) {
9278+ if (kari -> decodedInit == 1 ) {
9279+ if (XMEMCMP (rid , kari -> decoded -> issuerHash , keyIdSize ) == 0 ) {
9280+ * recipFound = 1 ;
9281+ }
9282+ }
9283+ else {
9284+ /* can not confirm recipient serial number with no cert provided */
9285+ WOLFSSL_MSG ("No recipient cert loaded to match with CMS serial number" );
92749286 * recipFound = 1 ;
92759287 }
92769288
@@ -9308,7 +9320,8 @@ static int wc_PKCS7_KariGetIssuerAndSerialNumber(WC_PKCS7_KARI* kari,
93089320 return ret ;
93099321 }
93109322
9311- if (mp_cmp (recipSerial , serial ) != MP_EQ ) {
9323+ if (kari -> decodedInit == 1 &&
9324+ mp_cmp (recipSerial , serial ) != MP_EQ ) {
93129325 mp_clear (serial );
93139326 mp_clear (recipSerial );
93149327 WOLFSSL_MSG ("CMS serial number does not match recipient" );
@@ -9944,8 +9957,6 @@ static int wc_PKCS7_DecryptKari(PKCS7* pkcs7, byte* in, word32 inSz,
99449957
99459958 WOLFSSL_ENTER ("wc_PKCS7_DecryptKari" );
99469959 if (pkcs7 == NULL || pkiMsg == NULL ||
9947- ((pkcs7 -> singleCert == NULL || pkcs7 -> singleCertSz == 0 ) &&
9948- pkcs7 -> wrapCEKCb == NULL ) ||
99499960 idx == NULL || decryptedKey == NULL || decryptedKeySz == NULL ) {
99509961 return BAD_FUNC_ARG ;
99519962 }
@@ -9986,17 +9997,15 @@ static int wc_PKCS7_DecryptKari(PKCS7* pkcs7, byte* in, word32 inSz,
99869997 encryptedKeySz = MAX_ENCRYPTED_KEY_SZ ;
99879998
99889999 /* parse cert and key */
9989- if (pkcs7 -> singleCert != NULL ) {
9990- ret = wc_PKCS7_KariParseRecipCert (kari , (byte * )pkcs7 -> singleCert ,
9991- pkcs7 -> singleCertSz , pkcs7 -> privateKey ,
9992- pkcs7 -> privateKeySz );
9993- if (ret != 0 ) {
9994- wc_PKCS7_KariFree (kari );
9995- #ifdef WOLFSSL_SMALL_STACK
9996- XFREE (encryptedKey , pkcs7 -> heap , DYNAMIC_TYPE_PKCS7 );
9997- #endif
9998- return ret ;
9999- }
10000+ ret = wc_PKCS7_KariParseRecipCert (kari , (byte * )pkcs7 -> singleCert ,
10001+ pkcs7 -> singleCertSz , pkcs7 -> privateKey ,
10002+ pkcs7 -> privateKeySz );
10003+ if (ret != 0 ) {
10004+ wc_PKCS7_KariFree (kari );
10005+ #ifdef WOLFSSL_SMALL_STACK
10006+ XFREE (encryptedKey , pkcs7 -> heap , DYNAMIC_TYPE_PKCS7 );
10007+ #endif
10008+ return ret ;
1000010009 }
1000110010
1000210011 /* remove OriginatorIdentifierOrKey */
0 commit comments