@@ -12957,6 +12957,116 @@ int wc_PKCS7_DecodeEnvelopedData(wc_PKCS7* pkcs7, byte* in,
1295712957}
1295812958
1295912959
12960+ int wc_PKCS7_GetEnvelopedDataKariRid(const byte * in, word32 inSz,
12961+ byte * out, word32 * outSz)
12962+ {
12963+ int ret = 0;
12964+ word32 idx = 0;
12965+ int length = 0;
12966+ word32 contentType = 0;
12967+ word32 ridIdx = 0;
12968+ byte ridTag = 0;
12969+
12970+ if (in == NULL || inSz == 0 || out == NULL || outSz == NULL) {
12971+ ret = BAD_FUNC_ARG;
12972+ }
12973+ /* Consume ContentInfo SEQUENCE header. */
12974+ else if (GetSequence(in, &idx, &length, inSz) < 0) {
12975+ ret = ASN_PARSE_E;
12976+ }
12977+ /* Validate the EnvelopedData OBJECT IDENTIFIER. */
12978+ else if (wc_GetContentType(in, &idx, &contentType, inSz) < 0) {
12979+ ret = ASN_PARSE_E;
12980+ }
12981+ else if (contentType != ENVELOPED_DATA) {
12982+ WOLFSSL_MSG("PKCS#7 input not of type EnvelopedData");
12983+ ret = PKCS7_OID_E;
12984+ }
12985+ /* Consume EXPLICIT content [0] header. */
12986+ else if (GetASNHeader(in, ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED, &idx,
12987+ &length, inSz) < 0) {
12988+ ret = ASN_PARSE_E;
12989+ }
12990+ /* Consume EnvelopedData SEQUENCE header. */
12991+ else if (GetSequence(in, &idx, &length, inSz) < 0) {
12992+ ret = ASN_PARSE_E;
12993+ }
12994+ /* Consume version. */
12995+ else if (GetMyVersion(in, &idx, &length, inSz) < 0) {
12996+ ret = ASN_PARSE_E;
12997+ }
12998+ /* Consume originatorInfo if present. */
12999+ else if (GetASNHeader(in, ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED, &idx,
13000+ &length, inSz) >= 0) {
13001+ idx += (word32)length;
13002+ }
13003+ /* Consume recipientInfos SET OF header. */
13004+ if (ret == 0 && GetSet(in, &idx, &length, inSz) < 0) {
13005+ ret = ASN_PARSE_E;
13006+ }
13007+ /* Consume kari [1] header. */
13008+ if (ret == 0 && GetASNHeader(in, ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1,
13009+ &idx, &length, inSz) < 0) {
13010+ ret = ASN_PARSE_E;
13011+ }
13012+ /* Consume KARI version. */
13013+ if (ret == 0 && GetMyVersion(in, &idx, &length, inSz) < 0) {
13014+ ret = ASN_PARSE_E;
13015+ }
13016+ /* Consume KARI originator [0] header. */
13017+ if (ret == 0 && GetASNHeader(in, ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED,
13018+ &idx, &length, inSz) < 0) {
13019+ ret = ASN_PARSE_E;
13020+ }
13021+ /* Skip originator [0] content. */
13022+ if (ret == 0)
13023+ idx += (word32)length;
13024+ /* Consume KARI ukm [1] if present. */
13025+ if (ret == 0 && GetASNHeader(in, ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1,
13026+ &idx, &length, inSz) >= 0) {
13027+ idx += (word32) length;
13028+ }
13029+ /* Consume KARI keyEncryptionAlgorithm. */
13030+ if (ret == 0 && GetSequence(in, &idx, &length, inSz) < 0) {
13031+ ret = ASN_PARSE_E;
13032+ }
13033+ /* Skip keyEncryptionAlgorithm content. */
13034+ if (ret == 0)
13035+ idx += (word32)length;
13036+ /* Consume RecipientEncryptedKeys SEQUENCE OF header. */
13037+ if (ret == 0 && GetSequence(in, &idx, &length, inSz) < 0) {
13038+ ret = ASN_PARSE_E;
13039+ }
13040+ /* Consume RecipientEncryptedKey SEQUENCE header. */
13041+ if (ret == 0 && GetSequence(in, &idx, &length, inSz) < 0) {
13042+ ret = ASN_PARSE_E;
13043+ }
13044+ if (ret == 0)
13045+ ridIdx = idx;
13046+ /* Consume KeyAgreeRecipientIdentifier tag. */
13047+ if (ret == 0 && GetASNTag(in, &idx, &ridTag, inSz) < 0) {
13048+ ret = ASN_PARSE_E;
13049+ }
13050+ /* Consume KeyAgreeRecipientIdentifier length. */
13051+ if (ret == 0 && GetLength(in, &idx, &length, inSz) < 0) {
13052+ ret = ASN_PARSE_E;
13053+ }
13054+ if (ret == 0) {
13055+ word32 ridSz = (idx + (word32)length) - ridIdx;
13056+ if (ridSz > *outSz) {
13057+ /* Not enough room in output buffer. */
13058+ ret = BUFFER_E;
13059+ }
13060+ else {
13061+ /* Copy KeyAgreeRecipientIdentifier to output buffer. */
13062+ XMEMCPY(out, &in[ridIdx], ridSz);
13063+ *outSz = ridSz;
13064+ }
13065+ }
13066+ return ret;
13067+ }
13068+
13069+
1296013070/* build PKCS#7 authEnvelopedData content type, return enveloped size */
1296113071int wc_PKCS7_EncodeAuthEnvelopedData(wc_PKCS7* pkcs7, byte* output,
1296213072 word32 outputSz)
0 commit comments