1635165faSspz /*
2*b0d17251Schristos * Copyright 2008-2020 The OpenSSL Project Authors. All Rights Reserved.
3a89c9211Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5c7da899bSchristos * this file except in compliance with the License. You can obtain a copy
6c7da899bSchristos * in the file LICENSE in the source distribution or at
7c7da899bSchristos * https://www.openssl.org/source/license.html
8a89c9211Schristos */
9a89c9211Schristos
10a89c9211Schristos #include <openssl/asn1t.h>
11a89c9211Schristos #include <openssl/pem.h>
12a89c9211Schristos #include <openssl/x509v3.h>
13c7da899bSchristos #include <openssl/cms.h>
147d004720Schristos #include "cms_local.h"
15a89c9211Schristos
16a89c9211Schristos
17a89c9211Schristos ASN1_SEQUENCE(CMS_IssuerAndSerialNumber) = {
18a89c9211Schristos ASN1_SIMPLE(CMS_IssuerAndSerialNumber, issuer, X509_NAME),
19a89c9211Schristos ASN1_SIMPLE(CMS_IssuerAndSerialNumber, serialNumber, ASN1_INTEGER)
20a89c9211Schristos } ASN1_SEQUENCE_END(CMS_IssuerAndSerialNumber)
21a89c9211Schristos
22a89c9211Schristos ASN1_SEQUENCE(CMS_OtherCertificateFormat) = {
23a89c9211Schristos ASN1_SIMPLE(CMS_OtherCertificateFormat, otherCertFormat, ASN1_OBJECT),
24a89c9211Schristos ASN1_OPT(CMS_OtherCertificateFormat, otherCert, ASN1_ANY)
25c7da899bSchristos } static_ASN1_SEQUENCE_END(CMS_OtherCertificateFormat)
26a89c9211Schristos
27a89c9211Schristos ASN1_CHOICE(CMS_CertificateChoices) = {
28a89c9211Schristos ASN1_SIMPLE(CMS_CertificateChoices, d.certificate, X509),
29a89c9211Schristos ASN1_IMP(CMS_CertificateChoices, d.extendedCertificate, ASN1_SEQUENCE, 0),
30a89c9211Schristos ASN1_IMP(CMS_CertificateChoices, d.v1AttrCert, ASN1_SEQUENCE, 1),
31a89c9211Schristos ASN1_IMP(CMS_CertificateChoices, d.v2AttrCert, ASN1_SEQUENCE, 2),
32a89c9211Schristos ASN1_IMP(CMS_CertificateChoices, d.other, CMS_OtherCertificateFormat, 3)
33a89c9211Schristos } ASN1_CHOICE_END(CMS_CertificateChoices)
34a89c9211Schristos
35a89c9211Schristos ASN1_CHOICE(CMS_SignerIdentifier) = {
36a89c9211Schristos ASN1_SIMPLE(CMS_SignerIdentifier, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber),
37a89c9211Schristos ASN1_IMP(CMS_SignerIdentifier, d.subjectKeyIdentifier, ASN1_OCTET_STRING, 0)
38c7da899bSchristos } static_ASN1_CHOICE_END(CMS_SignerIdentifier)
39a89c9211Schristos
40a89c9211Schristos ASN1_NDEF_SEQUENCE(CMS_EncapsulatedContentInfo) = {
41a89c9211Schristos ASN1_SIMPLE(CMS_EncapsulatedContentInfo, eContentType, ASN1_OBJECT),
42a89c9211Schristos ASN1_NDEF_EXP_OPT(CMS_EncapsulatedContentInfo, eContent, ASN1_OCTET_STRING_NDEF, 0)
43c7da899bSchristos } static_ASN1_NDEF_SEQUENCE_END(CMS_EncapsulatedContentInfo)
44a89c9211Schristos
45a89c9211Schristos /* Minor tweak to operation: free up signer key, cert */
46a89c9211Schristos static int cms_si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
47a89c9211Schristos void *exarg)
48a89c9211Schristos {
49635165faSspz if (operation == ASN1_OP_FREE_POST) {
50a89c9211Schristos CMS_SignerInfo *si = (CMS_SignerInfo *)*pval;
51a89c9211Schristos EVP_PKEY_free(si->pkey);
52a89c9211Schristos X509_free(si->signer);
53c7da899bSchristos EVP_MD_CTX_free(si->mctx);
54a89c9211Schristos }
55a89c9211Schristos return 1;
56a89c9211Schristos }
57a89c9211Schristos
58a89c9211Schristos ASN1_SEQUENCE_cb(CMS_SignerInfo, cms_si_cb) = {
5913d40330Schristos ASN1_EMBED(CMS_SignerInfo, version, INT32),
60a89c9211Schristos ASN1_SIMPLE(CMS_SignerInfo, sid, CMS_SignerIdentifier),
61a89c9211Schristos ASN1_SIMPLE(CMS_SignerInfo, digestAlgorithm, X509_ALGOR),
62a89c9211Schristos ASN1_IMP_SET_OF_OPT(CMS_SignerInfo, signedAttrs, X509_ATTRIBUTE, 0),
63a89c9211Schristos ASN1_SIMPLE(CMS_SignerInfo, signatureAlgorithm, X509_ALGOR),
64a89c9211Schristos ASN1_SIMPLE(CMS_SignerInfo, signature, ASN1_OCTET_STRING),
65a89c9211Schristos ASN1_IMP_SET_OF_OPT(CMS_SignerInfo, unsignedAttrs, X509_ATTRIBUTE, 1)
66a89c9211Schristos } ASN1_SEQUENCE_END_cb(CMS_SignerInfo, CMS_SignerInfo)
67a89c9211Schristos
68a89c9211Schristos ASN1_SEQUENCE(CMS_OtherRevocationInfoFormat) = {
69a89c9211Schristos ASN1_SIMPLE(CMS_OtherRevocationInfoFormat, otherRevInfoFormat, ASN1_OBJECT),
70a89c9211Schristos ASN1_OPT(CMS_OtherRevocationInfoFormat, otherRevInfo, ASN1_ANY)
71c7da899bSchristos } static_ASN1_SEQUENCE_END(CMS_OtherRevocationInfoFormat)
72a89c9211Schristos
73a89c9211Schristos ASN1_CHOICE(CMS_RevocationInfoChoice) = {
74a89c9211Schristos ASN1_SIMPLE(CMS_RevocationInfoChoice, d.crl, X509_CRL),
75a89c9211Schristos ASN1_IMP(CMS_RevocationInfoChoice, d.other, CMS_OtherRevocationInfoFormat, 1)
76a89c9211Schristos } ASN1_CHOICE_END(CMS_RevocationInfoChoice)
77a89c9211Schristos
78a89c9211Schristos ASN1_NDEF_SEQUENCE(CMS_SignedData) = {
7913d40330Schristos ASN1_EMBED(CMS_SignedData, version, INT32),
80a89c9211Schristos ASN1_SET_OF(CMS_SignedData, digestAlgorithms, X509_ALGOR),
81a89c9211Schristos ASN1_SIMPLE(CMS_SignedData, encapContentInfo, CMS_EncapsulatedContentInfo),
82a89c9211Schristos ASN1_IMP_SET_OF_OPT(CMS_SignedData, certificates, CMS_CertificateChoices, 0),
83a89c9211Schristos ASN1_IMP_SET_OF_OPT(CMS_SignedData, crls, CMS_RevocationInfoChoice, 1),
84a89c9211Schristos ASN1_SET_OF(CMS_SignedData, signerInfos, CMS_SignerInfo)
85a89c9211Schristos } ASN1_NDEF_SEQUENCE_END(CMS_SignedData)
86a89c9211Schristos
87a89c9211Schristos ASN1_SEQUENCE(CMS_OriginatorInfo) = {
884e3dcb23Sspz ASN1_IMP_SET_OF_OPT(CMS_OriginatorInfo, certificates, CMS_CertificateChoices, 0),
894e3dcb23Sspz ASN1_IMP_SET_OF_OPT(CMS_OriginatorInfo, crls, CMS_RevocationInfoChoice, 1)
90c7da899bSchristos } static_ASN1_SEQUENCE_END(CMS_OriginatorInfo)
91a89c9211Schristos
92a89c9211Schristos ASN1_NDEF_SEQUENCE(CMS_EncryptedContentInfo) = {
93a89c9211Schristos ASN1_SIMPLE(CMS_EncryptedContentInfo, contentType, ASN1_OBJECT),
94a89c9211Schristos ASN1_SIMPLE(CMS_EncryptedContentInfo, contentEncryptionAlgorithm, X509_ALGOR),
95a89c9211Schristos ASN1_IMP_OPT(CMS_EncryptedContentInfo, encryptedContent, ASN1_OCTET_STRING_NDEF, 0)
96c7da899bSchristos } static_ASN1_NDEF_SEQUENCE_END(CMS_EncryptedContentInfo)
97a89c9211Schristos
98a89c9211Schristos ASN1_SEQUENCE(CMS_KeyTransRecipientInfo) = {
9913d40330Schristos ASN1_EMBED(CMS_KeyTransRecipientInfo, version, INT32),
100a89c9211Schristos ASN1_SIMPLE(CMS_KeyTransRecipientInfo, rid, CMS_SignerIdentifier),
101a89c9211Schristos ASN1_SIMPLE(CMS_KeyTransRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR),
102a89c9211Schristos ASN1_SIMPLE(CMS_KeyTransRecipientInfo, encryptedKey, ASN1_OCTET_STRING)
103a89c9211Schristos } ASN1_SEQUENCE_END(CMS_KeyTransRecipientInfo)
104a89c9211Schristos
105a89c9211Schristos ASN1_SEQUENCE(CMS_OtherKeyAttribute) = {
106a89c9211Schristos ASN1_SIMPLE(CMS_OtherKeyAttribute, keyAttrId, ASN1_OBJECT),
107a89c9211Schristos ASN1_OPT(CMS_OtherKeyAttribute, keyAttr, ASN1_ANY)
108a89c9211Schristos } ASN1_SEQUENCE_END(CMS_OtherKeyAttribute)
109a89c9211Schristos
110a89c9211Schristos ASN1_SEQUENCE(CMS_RecipientKeyIdentifier) = {
111a89c9211Schristos ASN1_SIMPLE(CMS_RecipientKeyIdentifier, subjectKeyIdentifier, ASN1_OCTET_STRING),
112a89c9211Schristos ASN1_OPT(CMS_RecipientKeyIdentifier, date, ASN1_GENERALIZEDTIME),
113a89c9211Schristos ASN1_OPT(CMS_RecipientKeyIdentifier, other, CMS_OtherKeyAttribute)
114a89c9211Schristos } ASN1_SEQUENCE_END(CMS_RecipientKeyIdentifier)
115a89c9211Schristos
116a89c9211Schristos ASN1_CHOICE(CMS_KeyAgreeRecipientIdentifier) = {
117a89c9211Schristos ASN1_SIMPLE(CMS_KeyAgreeRecipientIdentifier, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber),
118a89c9211Schristos ASN1_IMP(CMS_KeyAgreeRecipientIdentifier, d.rKeyId, CMS_RecipientKeyIdentifier, 0)
119c7da899bSchristos } static_ASN1_CHOICE_END(CMS_KeyAgreeRecipientIdentifier)
120a89c9211Schristos
121a6054fbfSspz static int cms_rek_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
122a6054fbfSspz void *exarg)
123a6054fbfSspz {
124a6054fbfSspz CMS_RecipientEncryptedKey *rek = (CMS_RecipientEncryptedKey *)*pval;
125a6054fbfSspz if (operation == ASN1_OP_FREE_POST) {
126a6054fbfSspz EVP_PKEY_free(rek->pkey);
127a6054fbfSspz }
128a6054fbfSspz return 1;
129a6054fbfSspz }
130a6054fbfSspz
131a6054fbfSspz ASN1_SEQUENCE_cb(CMS_RecipientEncryptedKey, cms_rek_cb) = {
132a89c9211Schristos ASN1_SIMPLE(CMS_RecipientEncryptedKey, rid, CMS_KeyAgreeRecipientIdentifier),
133a89c9211Schristos ASN1_SIMPLE(CMS_RecipientEncryptedKey, encryptedKey, ASN1_OCTET_STRING)
134a6054fbfSspz } ASN1_SEQUENCE_END_cb(CMS_RecipientEncryptedKey, CMS_RecipientEncryptedKey)
135a89c9211Schristos
136a89c9211Schristos ASN1_SEQUENCE(CMS_OriginatorPublicKey) = {
137a89c9211Schristos ASN1_SIMPLE(CMS_OriginatorPublicKey, algorithm, X509_ALGOR),
138a89c9211Schristos ASN1_SIMPLE(CMS_OriginatorPublicKey, publicKey, ASN1_BIT_STRING)
139a89c9211Schristos } ASN1_SEQUENCE_END(CMS_OriginatorPublicKey)
140a89c9211Schristos
141a89c9211Schristos ASN1_CHOICE(CMS_OriginatorIdentifierOrKey) = {
142a89c9211Schristos ASN1_SIMPLE(CMS_OriginatorIdentifierOrKey, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber),
143a89c9211Schristos ASN1_IMP(CMS_OriginatorIdentifierOrKey, d.subjectKeyIdentifier, ASN1_OCTET_STRING, 0),
144a89c9211Schristos ASN1_IMP(CMS_OriginatorIdentifierOrKey, d.originatorKey, CMS_OriginatorPublicKey, 1)
145c7da899bSchristos } static_ASN1_CHOICE_END(CMS_OriginatorIdentifierOrKey)
146a89c9211Schristos
147a6054fbfSspz static int cms_kari_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
148a6054fbfSspz void *exarg)
149a6054fbfSspz {
150a6054fbfSspz CMS_KeyAgreeRecipientInfo *kari = (CMS_KeyAgreeRecipientInfo *)*pval;
151a6054fbfSspz if (operation == ASN1_OP_NEW_POST) {
152c7da899bSchristos kari->ctx = EVP_CIPHER_CTX_new();
153c7da899bSchristos if (kari->ctx == NULL)
154c7da899bSchristos return 0;
155c7da899bSchristos EVP_CIPHER_CTX_set_flags(kari->ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
156a6054fbfSspz kari->pctx = NULL;
157a6054fbfSspz } else if (operation == ASN1_OP_FREE_POST) {
158a6054fbfSspz EVP_PKEY_CTX_free(kari->pctx);
159c7da899bSchristos EVP_CIPHER_CTX_free(kari->ctx);
160a6054fbfSspz }
161a6054fbfSspz return 1;
162a6054fbfSspz }
163a6054fbfSspz
164a6054fbfSspz ASN1_SEQUENCE_cb(CMS_KeyAgreeRecipientInfo, cms_kari_cb) = {
16513d40330Schristos ASN1_EMBED(CMS_KeyAgreeRecipientInfo, version, INT32),
166a89c9211Schristos ASN1_EXP(CMS_KeyAgreeRecipientInfo, originator, CMS_OriginatorIdentifierOrKey, 0),
167a89c9211Schristos ASN1_EXP_OPT(CMS_KeyAgreeRecipientInfo, ukm, ASN1_OCTET_STRING, 1),
168a89c9211Schristos ASN1_SIMPLE(CMS_KeyAgreeRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR),
169a89c9211Schristos ASN1_SEQUENCE_OF(CMS_KeyAgreeRecipientInfo, recipientEncryptedKeys, CMS_RecipientEncryptedKey)
170a6054fbfSspz } ASN1_SEQUENCE_END_cb(CMS_KeyAgreeRecipientInfo, CMS_KeyAgreeRecipientInfo)
171a89c9211Schristos
172a89c9211Schristos ASN1_SEQUENCE(CMS_KEKIdentifier) = {
173a89c9211Schristos ASN1_SIMPLE(CMS_KEKIdentifier, keyIdentifier, ASN1_OCTET_STRING),
174a89c9211Schristos ASN1_OPT(CMS_KEKIdentifier, date, ASN1_GENERALIZEDTIME),
175a89c9211Schristos ASN1_OPT(CMS_KEKIdentifier, other, CMS_OtherKeyAttribute)
176c7da899bSchristos } static_ASN1_SEQUENCE_END(CMS_KEKIdentifier)
177a89c9211Schristos
178a89c9211Schristos ASN1_SEQUENCE(CMS_KEKRecipientInfo) = {
17913d40330Schristos ASN1_EMBED(CMS_KEKRecipientInfo, version, INT32),
180a89c9211Schristos ASN1_SIMPLE(CMS_KEKRecipientInfo, kekid, CMS_KEKIdentifier),
181a89c9211Schristos ASN1_SIMPLE(CMS_KEKRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR),
182a89c9211Schristos ASN1_SIMPLE(CMS_KEKRecipientInfo, encryptedKey, ASN1_OCTET_STRING)
183a89c9211Schristos } ASN1_SEQUENCE_END(CMS_KEKRecipientInfo)
184a89c9211Schristos
185a89c9211Schristos ASN1_SEQUENCE(CMS_PasswordRecipientInfo) = {
18613d40330Schristos ASN1_EMBED(CMS_PasswordRecipientInfo, version, INT32),
187a89c9211Schristos ASN1_IMP_OPT(CMS_PasswordRecipientInfo, keyDerivationAlgorithm, X509_ALGOR, 0),
188a89c9211Schristos ASN1_SIMPLE(CMS_PasswordRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR),
189a89c9211Schristos ASN1_SIMPLE(CMS_PasswordRecipientInfo, encryptedKey, ASN1_OCTET_STRING)
190a89c9211Schristos } ASN1_SEQUENCE_END(CMS_PasswordRecipientInfo)
191a89c9211Schristos
192a89c9211Schristos ASN1_SEQUENCE(CMS_OtherRecipientInfo) = {
193a89c9211Schristos ASN1_SIMPLE(CMS_OtherRecipientInfo, oriType, ASN1_OBJECT),
194a89c9211Schristos ASN1_OPT(CMS_OtherRecipientInfo, oriValue, ASN1_ANY)
195c7da899bSchristos } static_ASN1_SEQUENCE_END(CMS_OtherRecipientInfo)
196a89c9211Schristos
197a89c9211Schristos /* Free up RecipientInfo additional data */
198a89c9211Schristos static int cms_ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
199a89c9211Schristos void *exarg)
200a89c9211Schristos {
201635165faSspz if (operation == ASN1_OP_FREE_PRE) {
202a89c9211Schristos CMS_RecipientInfo *ri = (CMS_RecipientInfo *)*pval;
203635165faSspz if (ri->type == CMS_RECIPINFO_TRANS) {
204a89c9211Schristos CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
205a89c9211Schristos EVP_PKEY_free(ktri->pkey);
206a89c9211Schristos X509_free(ktri->recip);
207a6054fbfSspz EVP_PKEY_CTX_free(ktri->pctx);
208635165faSspz } else if (ri->type == CMS_RECIPINFO_KEK) {
209a89c9211Schristos CMS_KEKRecipientInfo *kekri = ri->d.kekri;
210c7da899bSchristos OPENSSL_clear_free(kekri->key, kekri->keylen);
211635165faSspz } else if (ri->type == CMS_RECIPINFO_PASS) {
21232daad53Schristos CMS_PasswordRecipientInfo *pwri = ri->d.pwri;
213c7da899bSchristos OPENSSL_clear_free(pwri->pass, pwri->passlen);
21432daad53Schristos }
215a89c9211Schristos }
216a89c9211Schristos return 1;
217a89c9211Schristos }
218a89c9211Schristos
219a89c9211Schristos ASN1_CHOICE_cb(CMS_RecipientInfo, cms_ri_cb) = {
220a89c9211Schristos ASN1_SIMPLE(CMS_RecipientInfo, d.ktri, CMS_KeyTransRecipientInfo),
221a89c9211Schristos ASN1_IMP(CMS_RecipientInfo, d.kari, CMS_KeyAgreeRecipientInfo, 1),
222a89c9211Schristos ASN1_IMP(CMS_RecipientInfo, d.kekri, CMS_KEKRecipientInfo, 2),
223a89c9211Schristos ASN1_IMP(CMS_RecipientInfo, d.pwri, CMS_PasswordRecipientInfo, 3),
224a89c9211Schristos ASN1_IMP(CMS_RecipientInfo, d.ori, CMS_OtherRecipientInfo, 4)
225a89c9211Schristos } ASN1_CHOICE_END_cb(CMS_RecipientInfo, CMS_RecipientInfo, type)
226a89c9211Schristos
227a89c9211Schristos ASN1_NDEF_SEQUENCE(CMS_EnvelopedData) = {
22813d40330Schristos ASN1_EMBED(CMS_EnvelopedData, version, INT32),
229a89c9211Schristos ASN1_IMP_OPT(CMS_EnvelopedData, originatorInfo, CMS_OriginatorInfo, 0),
230a89c9211Schristos ASN1_SET_OF(CMS_EnvelopedData, recipientInfos, CMS_RecipientInfo),
231a89c9211Schristos ASN1_SIMPLE(CMS_EnvelopedData, encryptedContentInfo, CMS_EncryptedContentInfo),
232a89c9211Schristos ASN1_IMP_SET_OF_OPT(CMS_EnvelopedData, unprotectedAttrs, X509_ATTRIBUTE, 1)
233a89c9211Schristos } ASN1_NDEF_SEQUENCE_END(CMS_EnvelopedData)
234a89c9211Schristos
235a89c9211Schristos ASN1_NDEF_SEQUENCE(CMS_DigestedData) = {
23613d40330Schristos ASN1_EMBED(CMS_DigestedData, version, INT32),
237a89c9211Schristos ASN1_SIMPLE(CMS_DigestedData, digestAlgorithm, X509_ALGOR),
238a89c9211Schristos ASN1_SIMPLE(CMS_DigestedData, encapContentInfo, CMS_EncapsulatedContentInfo),
239a89c9211Schristos ASN1_SIMPLE(CMS_DigestedData, digest, ASN1_OCTET_STRING)
240a89c9211Schristos } ASN1_NDEF_SEQUENCE_END(CMS_DigestedData)
241a89c9211Schristos
242a89c9211Schristos ASN1_NDEF_SEQUENCE(CMS_EncryptedData) = {
24313d40330Schristos ASN1_EMBED(CMS_EncryptedData, version, INT32),
244a89c9211Schristos ASN1_SIMPLE(CMS_EncryptedData, encryptedContentInfo, CMS_EncryptedContentInfo),
245a89c9211Schristos ASN1_IMP_SET_OF_OPT(CMS_EncryptedData, unprotectedAttrs, X509_ATTRIBUTE, 1)
246a89c9211Schristos } ASN1_NDEF_SEQUENCE_END(CMS_EncryptedData)
247a89c9211Schristos
248*b0d17251Schristos /* Defined in RFC 5083 - Section 2.1. AuthEnvelopedData Type */
249*b0d17251Schristos ASN1_NDEF_SEQUENCE(CMS_AuthEnvelopedData) = {
250*b0d17251Schristos ASN1_EMBED(CMS_AuthEnvelopedData, version, INT32),
251*b0d17251Schristos ASN1_IMP_OPT(CMS_AuthEnvelopedData, originatorInfo, CMS_OriginatorInfo, 0),
252*b0d17251Schristos ASN1_SET_OF(CMS_AuthEnvelopedData, recipientInfos, CMS_RecipientInfo),
253*b0d17251Schristos ASN1_SIMPLE(CMS_AuthEnvelopedData, authEncryptedContentInfo, CMS_EncryptedContentInfo),
254*b0d17251Schristos ASN1_IMP_SET_OF_OPT(CMS_AuthEnvelopedData, authAttrs, X509_ALGOR, 2),
255*b0d17251Schristos ASN1_SIMPLE(CMS_AuthEnvelopedData, mac, ASN1_OCTET_STRING),
256*b0d17251Schristos ASN1_IMP_SET_OF_OPT(CMS_AuthEnvelopedData, unauthAttrs, X509_ALGOR, 3)
257*b0d17251Schristos } ASN1_NDEF_SEQUENCE_END(CMS_AuthEnvelopedData)
258*b0d17251Schristos
259a89c9211Schristos ASN1_NDEF_SEQUENCE(CMS_AuthenticatedData) = {
26013d40330Schristos ASN1_EMBED(CMS_AuthenticatedData, version, INT32),
261a89c9211Schristos ASN1_IMP_OPT(CMS_AuthenticatedData, originatorInfo, CMS_OriginatorInfo, 0),
262a89c9211Schristos ASN1_SET_OF(CMS_AuthenticatedData, recipientInfos, CMS_RecipientInfo),
263a89c9211Schristos ASN1_SIMPLE(CMS_AuthenticatedData, macAlgorithm, X509_ALGOR),
264a89c9211Schristos ASN1_IMP(CMS_AuthenticatedData, digestAlgorithm, X509_ALGOR, 1),
265a89c9211Schristos ASN1_SIMPLE(CMS_AuthenticatedData, encapContentInfo, CMS_EncapsulatedContentInfo),
266a89c9211Schristos ASN1_IMP_SET_OF_OPT(CMS_AuthenticatedData, authAttrs, X509_ALGOR, 2),
267a89c9211Schristos ASN1_SIMPLE(CMS_AuthenticatedData, mac, ASN1_OCTET_STRING),
268a89c9211Schristos ASN1_IMP_SET_OF_OPT(CMS_AuthenticatedData, unauthAttrs, X509_ALGOR, 3)
269c7da899bSchristos } static_ASN1_NDEF_SEQUENCE_END(CMS_AuthenticatedData)
270a89c9211Schristos
271a89c9211Schristos ASN1_NDEF_SEQUENCE(CMS_CompressedData) = {
27213d40330Schristos ASN1_EMBED(CMS_CompressedData, version, INT32),
273a89c9211Schristos ASN1_SIMPLE(CMS_CompressedData, compressionAlgorithm, X509_ALGOR),
274a89c9211Schristos ASN1_SIMPLE(CMS_CompressedData, encapContentInfo, CMS_EncapsulatedContentInfo),
275a89c9211Schristos } ASN1_NDEF_SEQUENCE_END(CMS_CompressedData)
276a89c9211Schristos
277a89c9211Schristos /* This is the ANY DEFINED BY table for the top level ContentInfo structure */
278a89c9211Schristos
279a89c9211Schristos ASN1_ADB_TEMPLATE(cms_default) = ASN1_EXP(CMS_ContentInfo, d.other, ASN1_ANY, 0);
280a89c9211Schristos
281a89c9211Schristos ASN1_ADB(CMS_ContentInfo) = {
282a89c9211Schristos ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP(CMS_ContentInfo, d.data, ASN1_OCTET_STRING_NDEF, 0)),
283a89c9211Schristos ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP(CMS_ContentInfo, d.signedData, CMS_SignedData, 0)),
284a89c9211Schristos ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP(CMS_ContentInfo, d.envelopedData, CMS_EnvelopedData, 0)),
285a89c9211Schristos ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP(CMS_ContentInfo, d.digestedData, CMS_DigestedData, 0)),
286a89c9211Schristos ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP(CMS_ContentInfo, d.encryptedData, CMS_EncryptedData, 0)),
287*b0d17251Schristos ADB_ENTRY(NID_id_smime_ct_authEnvelopedData, ASN1_NDEF_EXP(CMS_ContentInfo, d.authEnvelopedData, CMS_AuthEnvelopedData, 0)),
288a89c9211Schristos ADB_ENTRY(NID_id_smime_ct_authData, ASN1_NDEF_EXP(CMS_ContentInfo, d.authenticatedData, CMS_AuthenticatedData, 0)),
289a89c9211Schristos ADB_ENTRY(NID_id_smime_ct_compressedData, ASN1_NDEF_EXP(CMS_ContentInfo, d.compressedData, CMS_CompressedData, 0)),
290a89c9211Schristos } ASN1_ADB_END(CMS_ContentInfo, 0, contentType, 0, &cms_default_tt, NULL);
291a89c9211Schristos
292a89c9211Schristos /* CMS streaming support */
cms_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)293a89c9211Schristos static int cms_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
294a89c9211Schristos void *exarg)
295a89c9211Schristos {
296a89c9211Schristos ASN1_STREAM_ARG *sarg = exarg;
297a89c9211Schristos CMS_ContentInfo *cms = NULL;
298a89c9211Schristos if (pval)
299a89c9211Schristos cms = (CMS_ContentInfo *)*pval;
300a89c9211Schristos else
301a89c9211Schristos return 1;
302635165faSspz switch (operation) {
303a89c9211Schristos
304a89c9211Schristos case ASN1_OP_STREAM_PRE:
305a89c9211Schristos if (CMS_stream(&sarg->boundary, cms) <= 0)
306a89c9211Schristos return 0;
307c7da899bSchristos /* fall thru */
308a89c9211Schristos case ASN1_OP_DETACHED_PRE:
309a89c9211Schristos sarg->ndef_bio = CMS_dataInit(cms, sarg->out);
310a89c9211Schristos if (!sarg->ndef_bio)
311a89c9211Schristos return 0;
312a89c9211Schristos break;
313a89c9211Schristos
314a89c9211Schristos case ASN1_OP_STREAM_POST:
315a89c9211Schristos case ASN1_OP_DETACHED_POST:
316a89c9211Schristos if (CMS_dataFinal(cms, sarg->ndef_bio) <= 0)
317a89c9211Schristos return 0;
318a89c9211Schristos break;
319a89c9211Schristos
320a89c9211Schristos }
321a89c9211Schristos return 1;
322a89c9211Schristos }
323a89c9211Schristos
324a89c9211Schristos ASN1_NDEF_SEQUENCE_cb(CMS_ContentInfo, cms_cb) = {
325a89c9211Schristos ASN1_SIMPLE(CMS_ContentInfo, contentType, ASN1_OBJECT),
326a89c9211Schristos ASN1_ADB_OBJECT(CMS_ContentInfo)
327a89c9211Schristos } ASN1_NDEF_SEQUENCE_END_cb(CMS_ContentInfo, CMS_ContentInfo)
328a89c9211Schristos
329a89c9211Schristos /* Specials for signed attributes */
330a89c9211Schristos
331635165faSspz /*
332635165faSspz * When signing attributes we want to reorder them to match the sorted
333a89c9211Schristos * encoding.
334a89c9211Schristos */
335a89c9211Schristos
336a89c9211Schristos ASN1_ITEM_TEMPLATE(CMS_Attributes_Sign) =
337a89c9211Schristos ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, CMS_ATTRIBUTES, X509_ATTRIBUTE)
338a89c9211Schristos ASN1_ITEM_TEMPLATE_END(CMS_Attributes_Sign)
339a89c9211Schristos
340635165faSspz /*
341635165faSspz * When verifying attributes we need to use the received order. So we use
342635165faSspz * SEQUENCE OF and tag it to SET OF
343a89c9211Schristos */
344a89c9211Schristos
345a89c9211Schristos ASN1_ITEM_TEMPLATE(CMS_Attributes_Verify) =
346a89c9211Schristos ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL,
347a89c9211Schristos V_ASN1_SET, CMS_ATTRIBUTES, X509_ATTRIBUTE)
348a89c9211Schristos ASN1_ITEM_TEMPLATE_END(CMS_Attributes_Verify)
349a89c9211Schristos
350a89c9211Schristos
351a89c9211Schristos
352a89c9211Schristos ASN1_CHOICE(CMS_ReceiptsFrom) = {
35313d40330Schristos ASN1_IMP_EMBED(CMS_ReceiptsFrom, d.allOrFirstTier, INT32, 0),
354a89c9211Schristos ASN1_IMP_SEQUENCE_OF(CMS_ReceiptsFrom, d.receiptList, GENERAL_NAMES, 1)
355c7da899bSchristos } static_ASN1_CHOICE_END(CMS_ReceiptsFrom)
356a89c9211Schristos
357a89c9211Schristos ASN1_SEQUENCE(CMS_ReceiptRequest) = {
358a89c9211Schristos ASN1_SIMPLE(CMS_ReceiptRequest, signedContentIdentifier, ASN1_OCTET_STRING),
359a89c9211Schristos ASN1_SIMPLE(CMS_ReceiptRequest, receiptsFrom, CMS_ReceiptsFrom),
360a89c9211Schristos ASN1_SEQUENCE_OF(CMS_ReceiptRequest, receiptsTo, GENERAL_NAMES)
361a89c9211Schristos } ASN1_SEQUENCE_END(CMS_ReceiptRequest)
362a89c9211Schristos
363a89c9211Schristos ASN1_SEQUENCE(CMS_Receipt) = {
36413d40330Schristos ASN1_EMBED(CMS_Receipt, version, INT32),
365a89c9211Schristos ASN1_SIMPLE(CMS_Receipt, contentType, ASN1_OBJECT),
366a89c9211Schristos ASN1_SIMPLE(CMS_Receipt, signedContentIdentifier, ASN1_OCTET_STRING),
367a89c9211Schristos ASN1_SIMPLE(CMS_Receipt, originatorSignatureValue, ASN1_OCTET_STRING)
368a89c9211Schristos } ASN1_SEQUENCE_END(CMS_Receipt)
369a6054fbfSspz
370a6054fbfSspz /*
371a6054fbfSspz * Utilities to encode the CMS_SharedInfo structure used during key
372a6054fbfSspz * derivation.
373a6054fbfSspz */
374a6054fbfSspz
375a6054fbfSspz typedef struct {
376a6054fbfSspz X509_ALGOR *keyInfo;
377a6054fbfSspz ASN1_OCTET_STRING *entityUInfo;
378a6054fbfSspz ASN1_OCTET_STRING *suppPubInfo;
379a6054fbfSspz } CMS_SharedInfo;
380a6054fbfSspz
381a6054fbfSspz ASN1_SEQUENCE(CMS_SharedInfo) = {
382a6054fbfSspz ASN1_SIMPLE(CMS_SharedInfo, keyInfo, X509_ALGOR),
383a6054fbfSspz ASN1_EXP_OPT(CMS_SharedInfo, entityUInfo, ASN1_OCTET_STRING, 0),
384a6054fbfSspz ASN1_EXP_OPT(CMS_SharedInfo, suppPubInfo, ASN1_OCTET_STRING, 2),
385c7da899bSchristos } static_ASN1_SEQUENCE_END(CMS_SharedInfo)
386a6054fbfSspz
387a6054fbfSspz int CMS_SharedInfo_encode(unsigned char **pder, X509_ALGOR *kekalg,
388a6054fbfSspz ASN1_OCTET_STRING *ukm, int keylen)
389a6054fbfSspz {
390a6054fbfSspz union {
391a6054fbfSspz CMS_SharedInfo *pecsi;
392a6054fbfSspz ASN1_VALUE *a;
393a6054fbfSspz } intsi = {
394a6054fbfSspz NULL
395a6054fbfSspz };
396a6054fbfSspz
397a6054fbfSspz ASN1_OCTET_STRING oklen;
398a6054fbfSspz unsigned char kl[4];
399a6054fbfSspz CMS_SharedInfo ecsi;
400a6054fbfSspz
401a6054fbfSspz keylen <<= 3;
402a6054fbfSspz kl[0] = (keylen >> 24) & 0xff;
403a6054fbfSspz kl[1] = (keylen >> 16) & 0xff;
404a6054fbfSspz kl[2] = (keylen >> 8) & 0xff;
405a6054fbfSspz kl[3] = keylen & 0xff;
406a6054fbfSspz oklen.length = 4;
407a6054fbfSspz oklen.data = kl;
408a6054fbfSspz oklen.type = V_ASN1_OCTET_STRING;
409a6054fbfSspz oklen.flags = 0;
410a6054fbfSspz ecsi.keyInfo = kekalg;
411a6054fbfSspz ecsi.entityUInfo = ukm;
412a6054fbfSspz ecsi.suppPubInfo = &oklen;
413a6054fbfSspz intsi.pecsi = &ecsi;
414a6054fbfSspz return ASN1_item_i2d(intsi.a, pder, ASN1_ITEM_rptr(CMS_SharedInfo));
415a6054fbfSspz }
416