Coverage Report

Created: 2026-05-24 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/cms/cms_asn1.c
Line
Count
Source
1
/*
2
 * Copyright 2008-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <openssl/asn1t.h>
11
#include <openssl/pem.h>
12
#include <openssl/x509v3.h>
13
#include <openssl/cms.h>
14
#include "cms_local.h"
15
16
#include <crypto/asn1.h>
17
18
ASN1_SEQUENCE(CMS_IssuerAndSerialNumber) = {
19
    ASN1_SIMPLE(CMS_IssuerAndSerialNumber, issuer, X509_NAME),
20
    ASN1_SIMPLE(CMS_IssuerAndSerialNumber, serialNumber, ASN1_INTEGER)
21
155k
} ASN1_SEQUENCE_END(CMS_IssuerAndSerialNumber)
22
155k
23
155k
ASN1_SEQUENCE(CMS_OtherCertificateFormat) = {
24
155k
    ASN1_SIMPLE(CMS_OtherCertificateFormat, otherCertFormat, ASN1_OBJECT),
25
155k
    ASN1_OPT(CMS_OtherCertificateFormat, otherCert, ASN1_ANY)
26
155k
} static_ASN1_SEQUENCE_END(CMS_OtherCertificateFormat)
27
128k
28
128k
ASN1_CHOICE(CMS_CertificateChoices)
29
13.8M
    = { ASN1_SIMPLE(CMS_CertificateChoices, d.certificate, X509), ASN1_IMP(CMS_CertificateChoices, d.extendedCertificate, ASN1_SEQUENCE, 0), ASN1_IMP(CMS_CertificateChoices, d.v1AttrCert, ASN1_SEQUENCE, 1), ASN1_IMP(CMS_CertificateChoices, d.v2AttrCert, ASN1_SEQUENCE, 2), ASN1_IMP(CMS_CertificateChoices, d.other, CMS_OtherCertificateFormat, 3) } ASN1_CHOICE_END(CMS_CertificateChoices)
30
13.8M
31
13.8M
ASN1_CHOICE(CMS_SignerIdentifier) = {
32
13.8M
    ASN1_SIMPLE(CMS_SignerIdentifier, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber),
33
13.8M
    ASN1_IMP(CMS_SignerIdentifier, d.subjectKeyIdentifier, ASN1_OCTET_STRING, 0)
34
13.8M
} static_ASN1_CHOICE_END(CMS_SignerIdentifier)
35
475k
36
475k
ASN1_NDEF_SEQUENCE(CMS_EncapsulatedContentInfo)
37
475k
    = { ASN1_SIMPLE(CMS_EncapsulatedContentInfo, eContentType, ASN1_OBJECT), ASN1_NDEF_EXP_OPT(CMS_EncapsulatedContentInfo, eContent, ASN1_OCTET_STRING_NDEF, 0) } static_ASN1_NDEF_SEQUENCE_END(CMS_EncapsulatedContentInfo)
38
39
/* Minor tweak to operation: free up signer key, cert */
40
static int cms_si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
41
215k
{
42
215k
    if (operation == ASN1_OP_FREE_POST) {
43
24.3k
        CMS_SignerInfo *si = (CMS_SignerInfo *)*pval;
44
24.3k
        EVP_PKEY_free(si->pkey);
45
24.3k
        X509_free(si->signer);
46
24.3k
        EVP_MD_CTX_free(si->mctx);
47
24.3k
        EVP_PKEY_CTX_free(si->pctx);
48
24.3k
    }
49
215k
    return 1;
50
215k
}
51
52
ASN1_SEQUENCE_cb(CMS_SignerInfo, cms_si_cb) = {
53
    ASN1_EMBED(CMS_SignerInfo, version, INT32),
54
    ASN1_SIMPLE(CMS_SignerInfo, sid, CMS_SignerIdentifier),
55
    ASN1_SIMPLE(CMS_SignerInfo, digestAlgorithm, X509_ALGOR),
56
    ASN1_IMP_SET_OF_OPT(CMS_SignerInfo, signedAttrs, X509_ATTRIBUTE, 0),
57
    ASN1_SIMPLE(CMS_SignerInfo, signatureAlgorithm, X509_ALGOR),
58
    ASN1_SIMPLE(CMS_SignerInfo, signature, ASN1_OCTET_STRING),
59
    ASN1_IMP_SET_OF_OPT(CMS_SignerInfo, unsignedAttrs, X509_ATTRIBUTE, 1)
60
} ASN1_SEQUENCE_END_cb(CMS_SignerInfo, CMS_SignerInfo)
61
62
ASN1_SEQUENCE(CMS_OtherRevocationInfoFormat) = {
63
    ASN1_SIMPLE(CMS_OtherRevocationInfoFormat, otherRevInfoFormat, ASN1_OBJECT),
64
    ASN1_OPT(CMS_OtherRevocationInfoFormat, otherRevInfo, ASN1_ANY)
65
7.18k
} static_ASN1_SEQUENCE_END(CMS_OtherRevocationInfoFormat)
66
7.18k
67
7.18k
ASN1_CHOICE(CMS_RevocationInfoChoice)
68
641k
    = { ASN1_SIMPLE(CMS_RevocationInfoChoice, d.crl, X509_CRL), ASN1_IMP(CMS_RevocationInfoChoice, d.other, CMS_OtherRevocationInfoFormat, 1) } ASN1_CHOICE_END(CMS_RevocationInfoChoice)
69
641k
70
641k
ASN1_NDEF_SEQUENCE(CMS_SignedData) = {
71
641k
    ASN1_EMBED(CMS_SignedData, version, INT32),
72
641k
    ASN1_SET_OF(CMS_SignedData, digestAlgorithms, X509_ALGOR),
73
641k
    ASN1_SIMPLE(CMS_SignedData, encapContentInfo, CMS_EncapsulatedContentInfo),
74
641k
    ASN1_IMP_SET_OF_OPT(CMS_SignedData, certificates, CMS_CertificateChoices, 0),
75
641k
    ASN1_IMP_SET_OF_OPT(CMS_SignedData, crls, CMS_RevocationInfoChoice, 1),
76
641k
    ASN1_SET_OF(CMS_SignedData, signerInfos, CMS_SignerInfo)
77
641k
} ASN1_NDEF_SEQUENCE_END(CMS_SignedData)
78
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(CMS_SignedData)
79
80
ASN1_SEQUENCE(CMS_OriginatorInfo) = {
81
    ASN1_IMP_SET_OF_OPT(CMS_OriginatorInfo, certificates, CMS_CertificateChoices, 0),
82
    ASN1_IMP_SET_OF_OPT(CMS_OriginatorInfo, crls, CMS_RevocationInfoChoice, 1)
83
177k
} static_ASN1_SEQUENCE_END(CMS_OriginatorInfo)
84
177k
85
177k
static int cms_ec_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
86
177k
{
87
62.1k
    CMS_EncryptedContentInfo *ec = (CMS_EncryptedContentInfo *)*pval;
88
89
62.1k
    if (operation == ASN1_OP_FREE_POST)
90
9.23k
        OPENSSL_clear_free(ec->key, ec->keylen);
91
62.1k
    return 1;
92
62.1k
}
93
94
ASN1_NDEF_SEQUENCE_cb(CMS_EncryptedContentInfo, cms_ec_cb) = {
95
    ASN1_SIMPLE(CMS_EncryptedContentInfo, contentType, ASN1_OBJECT),
96
    ASN1_SIMPLE(CMS_EncryptedContentInfo, contentEncryptionAlgorithm, X509_ALGOR),
97
    ASN1_IMP_OPT(CMS_EncryptedContentInfo, encryptedContent, ASN1_OCTET_STRING_NDEF, 0)
98
} ASN1_NDEF_SEQUENCE_END_cb(CMS_EncryptedContentInfo, CMS_EncryptedContentInfo)
99
100
ASN1_SEQUENCE(CMS_KeyTransRecipientInfo) = {
101
    ASN1_EMBED(CMS_KeyTransRecipientInfo, version, INT32),
102
    ASN1_SIMPLE(CMS_KeyTransRecipientInfo, rid, CMS_SignerIdentifier),
103
    ASN1_SIMPLE(CMS_KeyTransRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR),
104
    ASN1_SIMPLE(CMS_KeyTransRecipientInfo, encryptedKey, ASN1_OCTET_STRING)
105
161k
} ASN1_SEQUENCE_END(CMS_KeyTransRecipientInfo)
106
161k
107
161k
ASN1_SEQUENCE(CMS_OtherKeyAttribute) = {
108
161k
    ASN1_SIMPLE(CMS_OtherKeyAttribute, keyAttrId, ASN1_OBJECT),
109
161k
    ASN1_OPT(CMS_OtherKeyAttribute, keyAttr, ASN1_ANY)
110
161k
} ASN1_SEQUENCE_END(CMS_OtherKeyAttribute)
111
110k
112
110k
ASN1_SEQUENCE(CMS_RecipientKeyIdentifier) = {
113
110k
    ASN1_SIMPLE(CMS_RecipientKeyIdentifier, subjectKeyIdentifier, ASN1_OCTET_STRING),
114
110k
    ASN1_OPT(CMS_RecipientKeyIdentifier, date, ASN1_GENERALIZEDTIME),
115
110k
    ASN1_OPT(CMS_RecipientKeyIdentifier, other, CMS_OtherKeyAttribute)
116
110k
} ASN1_SEQUENCE_END(CMS_RecipientKeyIdentifier)
117
51.3k
118
51.3k
ASN1_CHOICE(CMS_KeyAgreeRecipientIdentifier) = {
119
51.3k
    ASN1_SIMPLE(CMS_KeyAgreeRecipientIdentifier, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber),
120
51.3k
    ASN1_IMP(CMS_KeyAgreeRecipientIdentifier, d.rKeyId, CMS_RecipientKeyIdentifier, 0)
121
149k
} static_ASN1_CHOICE_END(CMS_KeyAgreeRecipientIdentifier)
122
149k
123
149k
static int cms_rek_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
124
149k
{
125
118k
    CMS_RecipientEncryptedKey *rek = (CMS_RecipientEncryptedKey *)*pval;
126
118k
    if (operation == ASN1_OP_FREE_POST) {
127
14.5k
        EVP_PKEY_free(rek->pkey);
128
14.5k
    }
129
118k
    return 1;
130
118k
}
131
132
ASN1_SEQUENCE_cb(CMS_RecipientEncryptedKey, cms_rek_cb) = {
133
    ASN1_SIMPLE(CMS_RecipientEncryptedKey, rid, CMS_KeyAgreeRecipientIdentifier),
134
    ASN1_SIMPLE(CMS_RecipientEncryptedKey, encryptedKey, ASN1_OCTET_STRING)
135
} ASN1_SEQUENCE_END_cb(CMS_RecipientEncryptedKey, CMS_RecipientEncryptedKey)
136
137
ASN1_SEQUENCE(CMS_OriginatorPublicKey) = {
138
    ASN1_SIMPLE(CMS_OriginatorPublicKey, algorithm, X509_ALGOR),
139
    ASN1_SIMPLE(CMS_OriginatorPublicKey, publicKey, ASN1_BIT_STRING)
140
64
} ASN1_SEQUENCE_END(CMS_OriginatorPublicKey)
141
64
142
64
ASN1_CHOICE(CMS_OriginatorIdentifierOrKey) = {
143
64
    ASN1_SIMPLE(CMS_OriginatorIdentifierOrKey, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber),
144
64
    ASN1_IMP(CMS_OriginatorIdentifierOrKey, d.subjectKeyIdentifier, ASN1_OCTET_STRING, 0),
145
64
    ASN1_IMP(CMS_OriginatorIdentifierOrKey, d.originatorKey, CMS_OriginatorPublicKey, 1)
146
302k
} static_ASN1_CHOICE_END(CMS_OriginatorIdentifierOrKey)
147
302k
148
302k
static int cms_kari_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
149
302k
{
150
194k
    CMS_KeyAgreeRecipientInfo *kari = (CMS_KeyAgreeRecipientInfo *)*pval;
151
194k
    if (operation == ASN1_OP_NEW_POST) {
152
18.3k
        kari->ctx = EVP_CIPHER_CTX_new();
153
18.3k
        if (kari->ctx == NULL)
154
0
            return 0;
155
18.3k
        EVP_CIPHER_CTX_set_flags(kari->ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
156
18.3k
        kari->pctx = NULL;
157
176k
    } else if (operation == ASN1_OP_FREE_POST) {
158
18.3k
        EVP_PKEY_CTX_free(kari->pctx);
159
18.3k
        EVP_CIPHER_CTX_free(kari->ctx);
160
18.3k
    }
161
194k
    return 1;
162
194k
}
163
164
ASN1_SEQUENCE_cb(CMS_KeyAgreeRecipientInfo, cms_kari_cb) = {
165
    ASN1_EMBED(CMS_KeyAgreeRecipientInfo, version, INT32),
166
    ASN1_EXP(CMS_KeyAgreeRecipientInfo, originator, CMS_OriginatorIdentifierOrKey, 0),
167
    ASN1_EXP_OPT(CMS_KeyAgreeRecipientInfo, ukm, ASN1_OCTET_STRING, 1),
168
    ASN1_SIMPLE(CMS_KeyAgreeRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR),
169
    ASN1_SEQUENCE_OF(CMS_KeyAgreeRecipientInfo, recipientEncryptedKeys, CMS_RecipientEncryptedKey)
170
} ASN1_SEQUENCE_END_cb(CMS_KeyAgreeRecipientInfo, CMS_KeyAgreeRecipientInfo)
171
172
ASN1_SEQUENCE(CMS_KEKIdentifier) = {
173
    ASN1_SIMPLE(CMS_KEKIdentifier, keyIdentifier, ASN1_OCTET_STRING),
174
    ASN1_OPT(CMS_KEKIdentifier, date, ASN1_GENERALIZEDTIME),
175
    ASN1_OPT(CMS_KEKIdentifier, other, CMS_OtherKeyAttribute)
176
24.7k
} static_ASN1_SEQUENCE_END(CMS_KEKIdentifier)
177
24.7k
178
24.7k
ASN1_SEQUENCE(CMS_KEKRecipientInfo)
179
84.6k
    = { ASN1_EMBED(CMS_KEKRecipientInfo, version, INT32), ASN1_SIMPLE(CMS_KEKRecipientInfo, kekid, CMS_KEKIdentifier), ASN1_SIMPLE(CMS_KEKRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR), ASN1_SIMPLE(CMS_KEKRecipientInfo, encryptedKey, ASN1_OCTET_STRING) } ASN1_SEQUENCE_END(CMS_KEKRecipientInfo)
180
84.6k
181
84.6k
ASN1_SEQUENCE(CMS_PasswordRecipientInfo) = {
182
84.6k
    ASN1_EMBED(CMS_PasswordRecipientInfo, version, INT32),
183
84.6k
    ASN1_IMP_OPT(CMS_PasswordRecipientInfo, keyDerivationAlgorithm, X509_ALGOR, 0),
184
84.6k
    ASN1_SIMPLE(CMS_PasswordRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR),
185
84.6k
    ASN1_SIMPLE(CMS_PasswordRecipientInfo, encryptedKey, ASN1_OCTET_STRING)
186
201k
} ASN1_SEQUENCE_END(CMS_PasswordRecipientInfo)
187
201k
188
201k
static int cms_kemri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
189
201k
    void *exarg)
190
201k
{
191
10
    CMS_KEMRecipientInfo *kemri = (CMS_KEMRecipientInfo *)*pval;
192
193
10
    if (operation == ASN1_OP_NEW_POST) {
194
2
        kemri->ctx = EVP_CIPHER_CTX_new();
195
2
        if (kemri->ctx == NULL)
196
0
            return 0;
197
2
        EVP_CIPHER_CTX_set_flags(kemri->ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
198
2
        kemri->pctx = NULL;
199
8
    } else if (operation == ASN1_OP_FREE_POST) {
200
2
        EVP_PKEY_CTX_free(kemri->pctx);
201
2
        EVP_CIPHER_CTX_free(kemri->ctx);
202
2
        ASN1_OCTET_STRING_free(kemri->ukm);
203
2
    }
204
10
    return 1;
205
10
}
206
207
ASN1_SEQUENCE_cb(CMS_KEMRecipientInfo, cms_kemri_cb) = {
208
    ASN1_EMBED(CMS_KEMRecipientInfo, version, INT32),
209
    ASN1_SIMPLE(CMS_KEMRecipientInfo, rid, CMS_SignerIdentifier),
210
    ASN1_SIMPLE(CMS_KEMRecipientInfo, kem, X509_ALGOR),
211
    ASN1_SIMPLE(CMS_KEMRecipientInfo, kemct, ASN1_OCTET_STRING),
212
    ASN1_SIMPLE(CMS_KEMRecipientInfo, kdf, X509_ALGOR),
213
    ASN1_EMBED(CMS_KEMRecipientInfo, kekLength, INT32),
214
    ASN1_EXP_OPT(CMS_KEMRecipientInfo, ukm, ASN1_OCTET_STRING, 0),
215
    ASN1_SIMPLE(CMS_KEMRecipientInfo, wrap, X509_ALGOR),
216
    ASN1_SIMPLE(CMS_KEMRecipientInfo, encryptedKey, ASN1_OCTET_STRING)
217
} ASN1_SEQUENCE_END_cb(CMS_KEMRecipientInfo, CMS_KEMRecipientInfo)
218
219
ASN1_ADB_TEMPLATE(ori_def) = ASN1_SIMPLE(CMS_OtherRecipientInfo, d.other, ASN1_ANY);
220
221
ASN1_ADB(CMS_OtherRecipientInfo) = {
222
    ADB_ENTRY(NID_id_smime_ori_kem, ASN1_SIMPLE(CMS_OtherRecipientInfo, d.kemri, CMS_KEMRecipientInfo))
223
36.1k
} ASN1_ADB_END(CMS_OtherRecipientInfo, 0, oriType, 0, &ori_def_tt, NULL);
224
225
DECLARE_ASN1_FUNCTIONS(CMS_OtherRecipientInfo)
226
227
ASN1_SEQUENCE(CMS_OtherRecipientInfo) = {
228
    ASN1_SIMPLE(CMS_OtherRecipientInfo, oriType, ASN1_OBJECT),
229
    ASN1_ADB_OBJECT(CMS_OtherRecipientInfo)
230
24.3k
} ASN1_SEQUENCE_END(CMS_OtherRecipientInfo)
231
24.3k
232
24.3k
IMPLEMENT_ASN1_FUNCTIONS(CMS_OtherRecipientInfo)
233
24.3k
234
24.3k
/* Free up RecipientInfo additional data */
235
24.3k
static int cms_ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
236
24.3k
    void *exarg)
237
286k
{
238
286k
    if (operation == ASN1_OP_FREE_PRE) {
239
35.0k
        CMS_RecipientInfo *ri = (CMS_RecipientInfo *)*pval;
240
35.0k
        if (ri->type == CMS_RECIPINFO_TRANS) {
241
6.45k
            CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
242
6.45k
            EVP_PKEY_free(ktri->pkey);
243
6.45k
            X509_free(ktri->recip);
244
6.45k
            EVP_PKEY_CTX_free(ktri->pctx);
245
28.5k
        } else if (ri->type == CMS_RECIPINFO_KEK) {
246
1.85k
            CMS_KEKRecipientInfo *kekri = ri->d.kekri;
247
1.85k
            OPENSSL_clear_free(kekri->key, kekri->keylen);
248
26.6k
        } else if (ri->type == CMS_RECIPINFO_PASS) {
249
14.5k
            CMS_PasswordRecipientInfo *pwri = ri->d.pwri;
250
14.5k
            OPENSSL_clear_free(pwri->pass, pwri->passlen);
251
14.5k
        }
252
251k
    } else if (operation == ASN1_OP_D2I_POST) {
253
33.1k
        CMS_RecipientInfo *ri = (CMS_RecipientInfo *)*pval;
254
255
33.1k
        ri->type = ri->encoded_type;
256
33.1k
        if (ri->type == CMS_RECIPINFO_OTHER) {
257
4.64k
            int nid;
258
259
4.64k
            nid = OBJ_obj2nid(ri->d.ori->oriType);
260
            /* For ORI, map NID to specific type */
261
4.64k
            if (nid == NID_id_smime_ori_kem)
262
0
                ri->type = CMS_RECIPINFO_KEM;
263
            /* Otherwise stay with generic CMS_RECIPINFO_OTHER type */
264
4.64k
        }
265
218k
    } else if (operation == ASN1_OP_NEW_POST) {
266
35.0k
        CMS_RecipientInfo *ri = (CMS_RecipientInfo *)*pval;
267
268
35.0k
        ri->type = ri->encoded_type;
269
35.0k
    }
270
286k
    return 1;
271
286k
}
272
273
ASN1_CHOICE_cb(CMS_RecipientInfo, cms_ri_cb) = {
274
    ASN1_SIMPLE(CMS_RecipientInfo, d.ktri, CMS_KeyTransRecipientInfo),
275
    ASN1_IMP(CMS_RecipientInfo, d.kari, CMS_KeyAgreeRecipientInfo, 1),
276
    ASN1_IMP(CMS_RecipientInfo, d.kekri, CMS_KEKRecipientInfo, 2),
277
    ASN1_IMP(CMS_RecipientInfo, d.pwri, CMS_PasswordRecipientInfo, 3),
278
    ASN1_IMP(CMS_RecipientInfo, d.ori, CMS_OtherRecipientInfo, 4)
279
486k
} ASN1_CHOICE_END_cb(CMS_RecipientInfo, CMS_RecipientInfo, encoded_type)
280
281
ASN1_NDEF_SEQUENCE(CMS_EnvelopedData) = {
282
    ASN1_EMBED(CMS_EnvelopedData, version, INT32),
283
    ASN1_IMP_OPT(CMS_EnvelopedData, originatorInfo, CMS_OriginatorInfo, 0),
284
    ASN1_SET_OF(CMS_EnvelopedData, recipientInfos, CMS_RecipientInfo),
285
    ASN1_SIMPLE(CMS_EnvelopedData, encryptedContentInfo, CMS_EncryptedContentInfo),
286
    ASN1_IMP_SET_OF_OPT(CMS_EnvelopedData, unprotectedAttrs, X509_ATTRIBUTE, 1)
287
28.9k
} ASN1_NDEF_SEQUENCE_END(CMS_EnvelopedData)
288
IMPLEMENT_ASN1_DUP_FUNCTION(CMS_EnvelopedData)
289
290
ASN1_NDEF_SEQUENCE(CMS_DigestedData) = {
291
    ASN1_EMBED(CMS_DigestedData, version, INT32),
292
    ASN1_SIMPLE(CMS_DigestedData, digestAlgorithm, X509_ALGOR),
293
    ASN1_SIMPLE(CMS_DigestedData, encapContentInfo, CMS_EncapsulatedContentInfo),
294
    ASN1_SIMPLE(CMS_DigestedData, digest, ASN1_OCTET_STRING)
295
635
} ASN1_NDEF_SEQUENCE_END(CMS_DigestedData)
296
297
ASN1_NDEF_SEQUENCE(CMS_EncryptedData) = {
298
    ASN1_EMBED(CMS_EncryptedData, version, INT32),
299
    ASN1_SIMPLE(CMS_EncryptedData, encryptedContentInfo, CMS_EncryptedContentInfo),
300
    ASN1_IMP_SET_OF_OPT(CMS_EncryptedData, unprotectedAttrs, X509_ATTRIBUTE, 1)
301
1.92k
} ASN1_NDEF_SEQUENCE_END(CMS_EncryptedData)
302
303
/* Defined in RFC 5083 - Section 2.1. AuthEnvelopedData Type */
304
ASN1_NDEF_SEQUENCE(CMS_AuthEnvelopedData) = {
305
    ASN1_EMBED(CMS_AuthEnvelopedData, version, INT32),
306
    ASN1_IMP_OPT(CMS_AuthEnvelopedData, originatorInfo, CMS_OriginatorInfo, 0),
307
    ASN1_SET_OF(CMS_AuthEnvelopedData, recipientInfos, CMS_RecipientInfo),
308
    ASN1_SIMPLE(CMS_AuthEnvelopedData, authEncryptedContentInfo, CMS_EncryptedContentInfo),
309
    ASN1_IMP_SET_OF_OPT(CMS_AuthEnvelopedData, authAttrs, X509_ALGOR, 2),
310
    ASN1_SIMPLE(CMS_AuthEnvelopedData, mac, ASN1_OCTET_STRING),
311
    ASN1_IMP_SET_OF_OPT(CMS_AuthEnvelopedData, unauthAttrs, X509_ALGOR, 3)
312
166
} ASN1_NDEF_SEQUENCE_END(CMS_AuthEnvelopedData)
313
314
ASN1_NDEF_SEQUENCE(CMS_AuthenticatedData) = {
315
    ASN1_EMBED(CMS_AuthenticatedData, version, INT32),
316
    ASN1_IMP_OPT(CMS_AuthenticatedData, originatorInfo, CMS_OriginatorInfo, 0),
317
    ASN1_SET_OF(CMS_AuthenticatedData, recipientInfos, CMS_RecipientInfo),
318
    ASN1_SIMPLE(CMS_AuthenticatedData, macAlgorithm, X509_ALGOR),
319
    ASN1_IMP(CMS_AuthenticatedData, digestAlgorithm, X509_ALGOR, 1),
320
    ASN1_SIMPLE(CMS_AuthenticatedData, encapContentInfo, CMS_EncapsulatedContentInfo),
321
    ASN1_IMP_SET_OF_OPT(CMS_AuthenticatedData, authAttrs, X509_ALGOR, 2),
322
    ASN1_SIMPLE(CMS_AuthenticatedData, mac, ASN1_OCTET_STRING),
323
    ASN1_IMP_SET_OF_OPT(CMS_AuthenticatedData, unauthAttrs, X509_ALGOR, 3)
324
61.1k
} static_ASN1_NDEF_SEQUENCE_END(CMS_AuthenticatedData)
325
326
ASN1_NDEF_SEQUENCE(CMS_CompressedData)
327
    = {
328
          ASN1_EMBED(CMS_CompressedData, version, INT32),
329
          ASN1_SIMPLE(CMS_CompressedData, compressionAlgorithm, X509_ALGOR),
330
          ASN1_SIMPLE(CMS_CompressedData, encapContentInfo, CMS_EncapsulatedContentInfo),
331
125
      } ASN1_NDEF_SEQUENCE_END(CMS_CompressedData)
332
333
/* This is the ANY DEFINED BY table for the top level ContentInfo structure */
334
335
ASN1_ADB_TEMPLATE(cms_default) = ASN1_EXP(CMS_ContentInfo, d.other, ASN1_ANY, 0);
336
337
ASN1_ADB(CMS_ContentInfo) = {
338
    ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP(CMS_ContentInfo, d.data, ASN1_OCTET_STRING_NDEF, 0)),
339
    ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP(CMS_ContentInfo, d.signedData, CMS_SignedData, 0)),
340
    ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP(CMS_ContentInfo, d.envelopedData, CMS_EnvelopedData, 0)),
341
    ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP(CMS_ContentInfo, d.digestedData, CMS_DigestedData, 0)),
342
    ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP(CMS_ContentInfo, d.encryptedData, CMS_EncryptedData, 0)),
343
    ADB_ENTRY(NID_id_smime_ct_authEnvelopedData, ASN1_NDEF_EXP(CMS_ContentInfo, d.authEnvelopedData, CMS_AuthEnvelopedData, 0)),
344
    ADB_ENTRY(NID_id_smime_ct_authData, ASN1_NDEF_EXP(CMS_ContentInfo, d.authenticatedData, CMS_AuthenticatedData, 0)),
345
    ADB_ENTRY(NID_id_smime_ct_compressedData, ASN1_NDEF_EXP(CMS_ContentInfo, d.compressedData, CMS_CompressedData, 0)),
346
399k
} ASN1_ADB_END(CMS_ContentInfo, 0, contentType, 0, &cms_default_tt, NULL);
347
348
/* CMS streaming support */
349
static int cms_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
350
    void *exarg)
351
562k
{
352
562k
    ASN1_STREAM_ARG *sarg = exarg;
353
562k
    CMS_ContentInfo *cms = NULL;
354
562k
    if (pval)
355
562k
        cms = (CMS_ContentInfo *)*pval;
356
0
    else
357
0
        return 1;
358
562k
    switch (operation) {
359
360
0
    case ASN1_OP_STREAM_PRE:
361
0
        if (CMS_stream(&sarg->boundary, cms) <= 0)
362
0
            return 0;
363
        /* fall through */
364
0
    case ASN1_OP_DETACHED_PRE:
365
0
        sarg->ndef_bio = CMS_dataInit(cms, sarg->out);
366
0
        if (!sarg->ndef_bio)
367
0
            return 0;
368
0
        break;
369
370
0
    case ASN1_OP_STREAM_POST:
371
0
    case ASN1_OP_DETACHED_POST:
372
0
        if (CMS_dataFinal(cms, sarg->ndef_bio) <= 0)
373
0
            return 0;
374
0
        break;
375
376
104k
    case ASN1_OP_FREE_POST:
377
104k
        OPENSSL_free(cms->ctx.propq);
378
104k
        break;
379
562k
    }
380
562k
    return 1;
381
562k
}
382
383
ASN1_NDEF_SEQUENCE_cb(CMS_ContentInfo, cms_cb) = {
384
    ASN1_SIMPLE(CMS_ContentInfo, contentType, ASN1_OBJECT),
385
    ASN1_ADB_OBJECT(CMS_ContentInfo)
386
} ASN1_NDEF_SEQUENCE_END_cb(CMS_ContentInfo, CMS_ContentInfo)
387
388
/* Specials for signed attributes */
389
390
/*
391
 * When signing attributes we want to reorder them to match the sorted
392
 * encoding.
393
 */
394
395
ASN1_ITEM_TEMPLATE(CMS_Attributes_Sign) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, CMS_ATTRIBUTES, X509_ATTRIBUTE)
396
0
ASN1_ITEM_TEMPLATE_END(CMS_Attributes_Sign)
397
398
/*
399
 * When verifying attributes we need to use the received order. So we use
400
 * SEQUENCE OF and tag it to SET OF
401
 */
402
403
ASN1_ITEM_TEMPLATE(CMS_Attributes_Verify) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL,
404
    V_ASN1_SET, CMS_ATTRIBUTES, X509_ATTRIBUTE)
405
0
ASN1_ITEM_TEMPLATE_END(CMS_Attributes_Verify)
406
407
ASN1_CHOICE(CMS_ReceiptsFrom) = {
408
    ASN1_IMP_EMBED(CMS_ReceiptsFrom, d.allOrFirstTier, INT32, 0),
409
    ASN1_IMP_SEQUENCE_OF(CMS_ReceiptsFrom, d.receiptList, GENERAL_NAMES, 1)
410
232k
} static_ASN1_CHOICE_END(CMS_ReceiptsFrom)
411
232k
412
232k
ASN1_SEQUENCE(CMS_ReceiptRequest)
413
232k
    = { ASN1_SIMPLE(CMS_ReceiptRequest, signedContentIdentifier, ASN1_OCTET_STRING), ASN1_SIMPLE(CMS_ReceiptRequest, receiptsFrom, CMS_ReceiptsFrom), ASN1_SEQUENCE_OF(CMS_ReceiptRequest, receiptsTo, GENERAL_NAMES) } ASN1_SEQUENCE_END(CMS_ReceiptRequest)
414
71.6k
415
71.6k
ASN1_SEQUENCE(CMS_Receipt) = {
416
71.6k
    ASN1_EMBED(CMS_Receipt, version, INT32),
417
71.6k
    ASN1_SIMPLE(CMS_Receipt, contentType, ASN1_OBJECT),
418
71.6k
    ASN1_SIMPLE(CMS_Receipt, signedContentIdentifier, ASN1_OCTET_STRING),
419
71.6k
    ASN1_SIMPLE(CMS_Receipt, originatorSignatureValue, ASN1_OCTET_STRING)
420
71.6k
} ASN1_SEQUENCE_END(CMS_Receipt)
421
0
422
0
/*
423
0
 * Utilities to encode the CMS_SharedInfo structure used during key
424
0
 * derivation.
425
0
 */
426
0
427
0
typedef struct {
428
0
    X509_ALGOR *keyInfo;
429
0
    ASN1_OCTET_STRING *entityUInfo;
430
0
    ASN1_OCTET_STRING *suppPubInfo;
431
0
} CMS_SharedInfo;
432
0
433
0
ASN1_SEQUENCE(CMS_SharedInfo) = {
434
0
    ASN1_SIMPLE(CMS_SharedInfo, keyInfo, X509_ALGOR),
435
0
    ASN1_EXP_OPT(CMS_SharedInfo, entityUInfo, ASN1_OCTET_STRING, 0),
436
0
    ASN1_EXP_OPT(CMS_SharedInfo, suppPubInfo, ASN1_OCTET_STRING, 2),
437
0
} static_ASN1_SEQUENCE_END(CMS_SharedInfo)
438
0
439
0
int CMS_SharedInfo_encode(unsigned char **pder, X509_ALGOR *kekalg, ASN1_OCTET_STRING *ukm, int keylen)
440
0
{
441
0
    union {
442
0
        CMS_SharedInfo *pecsi;
443
0
        ASN1_VALUE *a;
444
0
    } intsi = {
445
0
        NULL
446
0
    };
447
448
0
    ASN1_OCTET_STRING oklen;
449
0
    unsigned char kl[4];
450
0
    CMS_SharedInfo ecsi;
451
452
0
    keylen <<= 3;
453
0
    kl[0] = (keylen >> 24) & 0xff;
454
0
    kl[1] = (keylen >> 16) & 0xff;
455
0
    kl[2] = (keylen >> 8) & 0xff;
456
0
    kl[3] = keylen & 0xff;
457
0
    oklen.length = 4;
458
0
    oklen.data = kl;
459
0
    oklen.type = V_ASN1_OCTET_STRING;
460
0
    oklen.flags = 0;
461
0
    ecsi.keyInfo = kekalg;
462
0
    ecsi.entityUInfo = ukm;
463
0
    ecsi.suppPubInfo = &oklen;
464
0
    intsi.pecsi = &ecsi;
465
0
    return ASN1_item_i2d(intsi.a, pder, ASN1_ITEM_rptr(CMS_SharedInfo));
466
0
}
467
468
/*
469
 * Utilities to encode the CMS_CMSORIforKEMOtherInfo structure used during key
470
 * derivation.
471
 */
472
473
typedef struct {
474
    X509_ALGOR *wrap;
475
    uint32_t kekLength;
476
    ASN1_OCTET_STRING *ukm;
477
} CMS_CMSORIforKEMOtherInfo;
478
479
ASN1_SEQUENCE(CMS_CMSORIforKEMOtherInfo) = {
480
    ASN1_SIMPLE(CMS_CMSORIforKEMOtherInfo, wrap, X509_ALGOR),
481
    ASN1_EMBED(CMS_CMSORIforKEMOtherInfo, kekLength, INT32),
482
    ASN1_EXP_OPT(CMS_CMSORIforKEMOtherInfo, ukm, ASN1_OCTET_STRING, 0),
483
0
} static_ASN1_SEQUENCE_END(CMS_CMSORIforKEMOtherInfo)
484
0
485
0
int CMS_CMSORIforKEMOtherInfo_encode(unsigned char **pder, X509_ALGOR *wrap, ASN1_OCTET_STRING *ukm, int keylen)
486
0
{
487
0
    CMS_CMSORIforKEMOtherInfo kem_otherinfo;
488
489
0
    kem_otherinfo.wrap = wrap;
490
0
    kem_otherinfo.kekLength = keylen;
491
0
    kem_otherinfo.ukm = ukm;
492
493
0
    return ASN1_item_i2d((ASN1_VALUE *)&kem_otherinfo, pder,
494
0
        ASN1_ITEM_rptr(CMS_CMSORIforKEMOtherInfo));
495
0
}