Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/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
123k
} ASN1_SEQUENCE_END(CMS_IssuerAndSerialNumber)
22
123k
23
123k
ASN1_SEQUENCE(CMS_OtherCertificateFormat) = {
24
123k
    ASN1_SIMPLE(CMS_OtherCertificateFormat, otherCertFormat, ASN1_OBJECT),
25
123k
    ASN1_OPT(CMS_OtherCertificateFormat, otherCert, ASN1_ANY)
26
123k
} static_ASN1_SEQUENCE_END(CMS_OtherCertificateFormat)
27
12.9k
28
12.9k
ASN1_CHOICE(CMS_CertificateChoices)
29
4.29M
    = { 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
4.29M
31
4.29M
ASN1_CHOICE(CMS_SignerIdentifier) = {
32
4.29M
    ASN1_SIMPLE(CMS_SignerIdentifier, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber),
33
4.29M
    ASN1_IMP(CMS_SignerIdentifier, d.subjectKeyIdentifier, ASN1_OCTET_STRING, 0)
34
4.29M
} static_ASN1_CHOICE_END(CMS_SignerIdentifier)
35
383k
36
383k
ASN1_NDEF_SEQUENCE(CMS_EncapsulatedContentInfo)
37
383k
    = { 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
187k
{
42
187k
    if (operation == ASN1_OP_FREE_POST) {
43
20.9k
        CMS_SignerInfo *si = (CMS_SignerInfo *)*pval;
44
20.9k
        EVP_PKEY_free(si->pkey);
45
20.9k
        X509_free(si->signer);
46
20.9k
        EVP_MD_CTX_free(si->mctx);
47
20.9k
        EVP_PKEY_CTX_free(si->pctx);
48
20.9k
    }
49
187k
    return 1;
50
187k
}
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
5.24k
} static_ASN1_SEQUENCE_END(CMS_OtherRevocationInfoFormat)
66
5.24k
67
5.24k
ASN1_CHOICE(CMS_RevocationInfoChoice)
68
445k
    = { ASN1_SIMPLE(CMS_RevocationInfoChoice, d.crl, X509_CRL), ASN1_IMP(CMS_RevocationInfoChoice, d.other, CMS_OtherRevocationInfoFormat, 1) } ASN1_CHOICE_END(CMS_RevocationInfoChoice)
69
445k
70
445k
ASN1_NDEF_SEQUENCE(CMS_SignedData) = {
71
445k
    ASN1_EMBED(CMS_SignedData, version, INT32),
72
445k
    ASN1_SET_OF(CMS_SignedData, digestAlgorithms, X509_ALGOR),
73
445k
    ASN1_SIMPLE(CMS_SignedData, encapContentInfo, CMS_EncapsulatedContentInfo),
74
445k
    ASN1_IMP_SET_OF_OPT(CMS_SignedData, certificates, CMS_CertificateChoices, 0),
75
445k
    ASN1_IMP_SET_OF_OPT(CMS_SignedData, crls, CMS_RevocationInfoChoice, 1),
76
445k
    ASN1_SET_OF(CMS_SignedData, signerInfos, CMS_SignerInfo)
77
445k
} 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
124k
} static_ASN1_SEQUENCE_END(CMS_OriginatorInfo)
84
124k
85
124k
static int cms_ec_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
86
124k
{
87
49.7k
    CMS_EncryptedContentInfo *ec = (CMS_EncryptedContentInfo *)*pval;
88
89
49.7k
    if (operation == ASN1_OP_FREE_POST)
90
7.19k
        OPENSSL_clear_free(ec->key, ec->keylen);
91
49.7k
    return 1;
92
49.7k
}
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
123k
} ASN1_SEQUENCE_END(CMS_KeyTransRecipientInfo)
106
123k
107
123k
ASN1_SEQUENCE(CMS_OtherKeyAttribute) = {
108
123k
    ASN1_SIMPLE(CMS_OtherKeyAttribute, keyAttrId, ASN1_OBJECT),
109
123k
    ASN1_OPT(CMS_OtherKeyAttribute, keyAttr, ASN1_ANY)
110
123k
} ASN1_SEQUENCE_END(CMS_OtherKeyAttribute)
111
75.7k
112
75.7k
ASN1_SEQUENCE(CMS_RecipientKeyIdentifier) = {
113
75.7k
    ASN1_SIMPLE(CMS_RecipientKeyIdentifier, subjectKeyIdentifier, ASN1_OCTET_STRING),
114
75.7k
    ASN1_OPT(CMS_RecipientKeyIdentifier, date, ASN1_GENERALIZEDTIME),
115
75.7k
    ASN1_OPT(CMS_RecipientKeyIdentifier, other, CMS_OtherKeyAttribute)
116
75.7k
} ASN1_SEQUENCE_END(CMS_RecipientKeyIdentifier)
117
30.6k
118
30.6k
ASN1_CHOICE(CMS_KeyAgreeRecipientIdentifier) = {
119
30.6k
    ASN1_SIMPLE(CMS_KeyAgreeRecipientIdentifier, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber),
120
30.6k
    ASN1_IMP(CMS_KeyAgreeRecipientIdentifier, d.rKeyId, CMS_RecipientKeyIdentifier, 0)
121
103k
} static_ASN1_CHOICE_END(CMS_KeyAgreeRecipientIdentifier)
122
103k
123
103k
static int cms_rek_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
124
103k
{
125
83.9k
    CMS_RecipientEncryptedKey *rek = (CMS_RecipientEncryptedKey *)*pval;
126
83.9k
    if (operation == ASN1_OP_FREE_POST) {
127
10.7k
        EVP_PKEY_free(rek->pkey);
128
10.7k
    }
129
83.9k
    return 1;
130
83.9k
}
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
42
} ASN1_SEQUENCE_END(CMS_OriginatorPublicKey)
141
42
142
42
ASN1_CHOICE(CMS_OriginatorIdentifierOrKey) = {
143
42
    ASN1_SIMPLE(CMS_OriginatorIdentifierOrKey, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber),
144
42
    ASN1_IMP(CMS_OriginatorIdentifierOrKey, d.subjectKeyIdentifier, ASN1_OCTET_STRING, 0),
145
42
    ASN1_IMP(CMS_OriginatorIdentifierOrKey, d.originatorKey, CMS_OriginatorPublicKey, 1)
146
246k
} static_ASN1_CHOICE_END(CMS_OriginatorIdentifierOrKey)
147
246k
148
246k
static int cms_kari_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
149
246k
{
150
158k
    CMS_KeyAgreeRecipientInfo *kari = (CMS_KeyAgreeRecipientInfo *)*pval;
151
158k
    if (operation == ASN1_OP_NEW_POST) {
152
15.1k
        kari->ctx = EVP_CIPHER_CTX_new();
153
15.1k
        if (kari->ctx == NULL)
154
0
            return 0;
155
15.1k
        EVP_CIPHER_CTX_set_flags(kari->ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
156
15.1k
        kari->pctx = NULL;
157
143k
    } else if (operation == ASN1_OP_FREE_POST) {
158
15.1k
        EVP_PKEY_CTX_free(kari->pctx);
159
15.1k
        EVP_CIPHER_CTX_free(kari->ctx);
160
15.1k
    }
161
158k
    return 1;
162
158k
}
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
21.2k
} static_ASN1_SEQUENCE_END(CMS_KEKIdentifier)
177
21.2k
178
21.2k
ASN1_SEQUENCE(CMS_KEKRecipientInfo)
179
69.2k
    = { 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
69.2k
181
69.2k
ASN1_SEQUENCE(CMS_PasswordRecipientInfo) = {
182
69.2k
    ASN1_EMBED(CMS_PasswordRecipientInfo, version, INT32),
183
69.2k
    ASN1_IMP_OPT(CMS_PasswordRecipientInfo, keyDerivationAlgorithm, X509_ALGOR, 0),
184
69.2k
    ASN1_SIMPLE(CMS_PasswordRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR),
185
69.2k
    ASN1_SIMPLE(CMS_PasswordRecipientInfo, encryptedKey, ASN1_OCTET_STRING)
186
163k
} ASN1_SEQUENCE_END(CMS_PasswordRecipientInfo)
187
163k
188
163k
static int cms_kemri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
189
163k
    void *exarg)
190
163k
{
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
50.0k
} 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
33.4k
} ASN1_SEQUENCE_END(CMS_OtherRecipientInfo)
231
33.4k
232
33.4k
IMPLEMENT_ASN1_FUNCTIONS(CMS_OtherRecipientInfo)
233
33.4k
234
33.4k
/* Free up RecipientInfo additional data */
235
33.4k
static int cms_ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
236
33.4k
    void *exarg)
237
405k
{
238
405k
    if (operation == ASN1_OP_FREE_PRE) {
239
50.3k
        CMS_RecipientInfo *ri = (CMS_RecipientInfo *)*pval;
240
50.3k
        if (ri->type == CMS_RECIPINFO_TRANS) {
241
6.87k
            CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
242
6.87k
            EVP_PKEY_free(ktri->pkey);
243
6.87k
            X509_free(ktri->recip);
244
6.87k
            EVP_PKEY_CTX_free(ktri->pctx);
245
43.4k
        } else if (ri->type == CMS_RECIPINFO_KEK) {
246
2.96k
            CMS_KEKRecipientInfo *kekri = ri->d.kekri;
247
2.96k
            OPENSSL_clear_free(kekri->key, kekri->keylen);
248
40.4k
        } else if (ri->type == CMS_RECIPINFO_PASS) {
249
22.5k
            CMS_PasswordRecipientInfo *pwri = ri->d.pwri;
250
22.5k
            OPENSSL_clear_free(pwri->pass, pwri->passlen);
251
22.5k
        }
252
354k
    } else if (operation == ASN1_OP_D2I_POST) {
253
47.6k
        CMS_RecipientInfo *ri = (CMS_RecipientInfo *)*pval;
254
255
47.6k
        ri->type = ri->encoded_type;
256
47.6k
        if (ri->type == CMS_RECIPINFO_OTHER) {
257
6.61k
            int nid;
258
259
6.61k
            nid = OBJ_obj2nid(ri->d.ori->oriType);
260
            /* For ORI, map NID to specific type */
261
6.61k
            if (nid == NID_id_smime_ori_kem)
262
0
                ri->type = CMS_RECIPINFO_KEM;
263
            /* Otherwise stay with generic CMS_RECIPINFO_OTHER type */
264
6.61k
        }
265
307k
    } else if (operation == ASN1_OP_NEW_POST) {
266
50.3k
        CMS_RecipientInfo *ri = (CMS_RecipientInfo *)*pval;
267
268
50.3k
        ri->type = ri->encoded_type;
269
50.3k
    }
270
405k
    return 1;
271
405k
}
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
374k
} 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
23.1k
} 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
346
} 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.61k
} 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_ATTRIBUTE, 1),
310
    ASN1_SIMPLE(CMS_AuthEnvelopedData, mac, ASN1_OCTET_STRING),
311
    ASN1_IMP_SET_OF_OPT(CMS_AuthEnvelopedData, unauthAttrs, X509_ATTRIBUTE, 2)
312
72
} 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_ATTRIBUTE, 2),
322
    ASN1_SIMPLE(CMS_AuthenticatedData, mac, ASN1_OCTET_STRING),
323
    ASN1_IMP_SET_OF_OPT(CMS_AuthenticatedData, unauthAttrs, X509_ATTRIBUTE, 3)
324
40.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
100
      } 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
287k
} 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
83.5k
{
352
83.5k
    ASN1_STREAM_ARG *sarg = exarg;
353
83.5k
    CMS_ContentInfo *cms = NULL;
354
83.5k
    if (pval)
355
83.5k
        cms = (CMS_ContentInfo *)*pval;
356
0
    else
357
0
        return 1;
358
83.5k
    switch (operation) {
359
360
0
    case ASN1_OP_STREAM_PRE:
361
0
        if (ossl_cms_stream(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
0
    case ASN1_OP_GET0_STREAM_CONTENT: {
377
0
        ASN1_STRING **content = exarg;
378
0
        ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
379
380
0
        *content = pos == NULL ? NULL : *pos;
381
0
    } break;
382
383
15.4k
    case ASN1_OP_FREE_POST:
384
15.4k
        OPENSSL_free(cms->ctx.propq);
385
15.4k
        break;
386
83.5k
    }
387
83.5k
    return 1;
388
83.5k
}
389
390
ASN1_NDEF_SEQUENCE_cb(CMS_ContentInfo, cms_cb) = {
391
    ASN1_SIMPLE(CMS_ContentInfo, contentType, ASN1_OBJECT),
392
    ASN1_ADB_OBJECT(CMS_ContentInfo)
393
} ASN1_NDEF_SEQUENCE_END_cb(CMS_ContentInfo, CMS_ContentInfo)
394
395
/* Specials for signed attributes */
396
397
/*
398
 * When signing attributes we want to reorder them to match the sorted
399
 * encoding.
400
 */
401
402
ASN1_ITEM_TEMPLATE(CMS_Attributes_Sign) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, CMS_ATTRIBUTES, X509_ATTRIBUTE)
403
0
ASN1_ITEM_TEMPLATE_END(CMS_Attributes_Sign)
404
405
/*
406
 * When verifying attributes we need to use the received order. So we use
407
 * SEQUENCE OF and tag it to SET OF
408
 */
409
410
ASN1_ITEM_TEMPLATE(CMS_Attributes_Verify) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL,
411
    V_ASN1_SET, CMS_ATTRIBUTES, X509_ATTRIBUTE)
412
0
ASN1_ITEM_TEMPLATE_END(CMS_Attributes_Verify)
413
414
ASN1_CHOICE(CMS_ReceiptsFrom) = {
415
    ASN1_IMP_EMBED(CMS_ReceiptsFrom, d.allOrFirstTier, INT32, 0),
416
    ASN1_IMP_SEQUENCE_OF(CMS_ReceiptsFrom, d.receiptList, GENERAL_NAMES, 1)
417
167k
} static_ASN1_CHOICE_END(CMS_ReceiptsFrom)
418
167k
419
167k
ASN1_SEQUENCE(CMS_ReceiptRequest)
420
167k
    = { 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)
421
52.5k
422
52.5k
ASN1_SEQUENCE(CMS_Receipt) = {
423
52.5k
    ASN1_EMBED(CMS_Receipt, version, INT32),
424
52.5k
    ASN1_SIMPLE(CMS_Receipt, contentType, ASN1_OBJECT),
425
52.5k
    ASN1_SIMPLE(CMS_Receipt, signedContentIdentifier, ASN1_OCTET_STRING),
426
52.5k
    ASN1_SIMPLE(CMS_Receipt, originatorSignatureValue, ASN1_OCTET_STRING)
427
52.5k
} ASN1_SEQUENCE_END(CMS_Receipt)
428
0
429
0
/*
430
0
 * Utilities to encode the CMS_SharedInfo structure used during key
431
0
 * derivation.
432
0
 */
433
0
434
0
typedef struct {
435
0
    X509_ALGOR *keyInfo;
436
0
    ASN1_OCTET_STRING *entityUInfo;
437
0
    ASN1_OCTET_STRING *suppPubInfo;
438
0
} CMS_SharedInfo;
439
0
440
0
ASN1_SEQUENCE(CMS_SharedInfo) = {
441
0
    ASN1_SIMPLE(CMS_SharedInfo, keyInfo, X509_ALGOR),
442
0
    ASN1_EXP_OPT(CMS_SharedInfo, entityUInfo, ASN1_OCTET_STRING, 0),
443
0
    ASN1_EXP_OPT(CMS_SharedInfo, suppPubInfo, ASN1_OCTET_STRING, 2),
444
0
} static_ASN1_SEQUENCE_END(CMS_SharedInfo)
445
0
446
0
int CMS_SharedInfo_encode(unsigned char **pder, X509_ALGOR *kekalg, ASN1_OCTET_STRING *ukm, int keylen)
447
0
{
448
0
    union {
449
0
        CMS_SharedInfo *pecsi;
450
0
        ASN1_VALUE *a;
451
0
    } intsi = {
452
0
        NULL
453
0
    };
454
455
0
    ASN1_OCTET_STRING oklen;
456
0
    unsigned char kl[4];
457
0
    CMS_SharedInfo ecsi;
458
459
0
    keylen <<= 3;
460
0
    kl[0] = (keylen >> 24) & 0xff;
461
0
    kl[1] = (keylen >> 16) & 0xff;
462
0
    kl[2] = (keylen >> 8) & 0xff;
463
0
    kl[3] = keylen & 0xff;
464
0
    oklen.length = 4;
465
0
    oklen.data = kl;
466
0
    oklen.type = V_ASN1_OCTET_STRING;
467
0
    oklen.flags = 0;
468
0
    ecsi.keyInfo = kekalg;
469
0
    ecsi.entityUInfo = ukm;
470
0
    ecsi.suppPubInfo = &oklen;
471
0
    intsi.pecsi = &ecsi;
472
0
    return ASN1_item_i2d(intsi.a, pder, ASN1_ITEM_rptr(CMS_SharedInfo));
473
0
}
474
475
/*
476
 * Utilities to encode the CMS_CMSORIforKEMOtherInfo structure used during key
477
 * derivation.
478
 */
479
480
typedef struct {
481
    X509_ALGOR *wrap;
482
    uint32_t kekLength;
483
    ASN1_OCTET_STRING *ukm;
484
} CMS_CMSORIforKEMOtherInfo;
485
486
ASN1_SEQUENCE(CMS_CMSORIforKEMOtherInfo) = {
487
    ASN1_SIMPLE(CMS_CMSORIforKEMOtherInfo, wrap, X509_ALGOR),
488
    ASN1_EMBED(CMS_CMSORIforKEMOtherInfo, kekLength, INT32),
489
    ASN1_EXP_OPT(CMS_CMSORIforKEMOtherInfo, ukm, ASN1_OCTET_STRING, 0),
490
0
} static_ASN1_SEQUENCE_END(CMS_CMSORIforKEMOtherInfo)
491
0
492
0
int CMS_CMSORIforKEMOtherInfo_encode(unsigned char **pder, X509_ALGOR *wrap, ASN1_OCTET_STRING *ukm, int keylen)
493
0
{
494
0
    CMS_CMSORIforKEMOtherInfo kem_otherinfo;
495
496
0
    kem_otherinfo.wrap = wrap;
497
0
    kem_otherinfo.kekLength = keylen;
498
0
    kem_otherinfo.ukm = ukm;
499
500
0
    return ASN1_item_i2d((ASN1_VALUE *)&kem_otherinfo, pder,
501
0
        ASN1_ITEM_rptr(CMS_CMSORIforKEMOtherInfo));
502
0
}