1 /* $OpenBSD: cms_asn1.c,v 1.25 2024/11/01 18:53:35 tb Exp $ */
2 /*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project.
5 */
6 /* ====================================================================
7 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 */
54
55 #include <stddef.h>
56 #include <stdlib.h>
57
58 #include <openssl/asn1.h>
59 #include <openssl/asn1t.h>
60 #include <openssl/cms.h>
61 #include <openssl/evp.h>
62 #include <openssl/x509.h>
63 #include <openssl/x509v3.h>
64
65 #include "cms_local.h"
66
67 static const ASN1_TEMPLATE CMS_IssuerAndSerialNumber_seq_tt[] = {
68 {
69 .flags = 0,
70 .tag = 0,
71 .offset = offsetof(CMS_IssuerAndSerialNumber, issuer),
72 .field_name = "issuer",
73 .item = &X509_NAME_it,
74 },
75 {
76 .flags = 0,
77 .tag = 0,
78 .offset = offsetof(CMS_IssuerAndSerialNumber, serialNumber),
79 .field_name = "serialNumber",
80 .item = &ASN1_INTEGER_it,
81 },
82 };
83
84 const ASN1_ITEM CMS_IssuerAndSerialNumber_it = {
85 .itype = ASN1_ITYPE_SEQUENCE,
86 .utype = V_ASN1_SEQUENCE,
87 .templates = CMS_IssuerAndSerialNumber_seq_tt,
88 .tcount = sizeof(CMS_IssuerAndSerialNumber_seq_tt) / sizeof(ASN1_TEMPLATE),
89 .funcs = NULL,
90 .size = sizeof(CMS_IssuerAndSerialNumber),
91 .sname = "CMS_IssuerAndSerialNumber",
92 };
93
94 static const ASN1_TEMPLATE CMS_OtherCertificateFormat_seq_tt[] = {
95 {
96 .flags = 0,
97 .tag = 0,
98 .offset = offsetof(CMS_OtherCertificateFormat, otherCertFormat),
99 .field_name = "otherCertFormat",
100 .item = &ASN1_OBJECT_it,
101 },
102 {
103 .flags = ASN1_TFLG_OPTIONAL,
104 .tag = 0,
105 .offset = offsetof(CMS_OtherCertificateFormat, otherCert),
106 .field_name = "otherCert",
107 .item = &ASN1_ANY_it,
108 },
109 };
110
111 static const ASN1_ITEM CMS_OtherCertificateFormat_it = {
112 .itype = ASN1_ITYPE_SEQUENCE,
113 .utype = V_ASN1_SEQUENCE,
114 .templates = CMS_OtherCertificateFormat_seq_tt,
115 .tcount = sizeof(CMS_OtherCertificateFormat_seq_tt) / sizeof(ASN1_TEMPLATE),
116 .funcs = NULL,
117 .size = sizeof(CMS_OtherCertificateFormat),
118 .sname = "CMS_OtherCertificateFormat",
119 };
120
121 static const ASN1_TEMPLATE CMS_CertificateChoices_ch_tt[] = {
122 {
123 .flags = 0,
124 .tag = 0,
125 .offset = offsetof(CMS_CertificateChoices, d.certificate),
126 .field_name = "d.certificate",
127 .item = &X509_it,
128 },
129 {
130 .flags = ASN1_TFLG_IMPLICIT,
131 .tag = 0,
132 .offset = offsetof(CMS_CertificateChoices, d.extendedCertificate),
133 .field_name = "d.extendedCertificate",
134 .item = &ASN1_SEQUENCE_it,
135 },
136 {
137 .flags = ASN1_TFLG_IMPLICIT,
138 .tag = 1,
139 .offset = offsetof(CMS_CertificateChoices, d.v1AttrCert),
140 .field_name = "d.v1AttrCert",
141 .item = &ASN1_SEQUENCE_it,
142 },
143 {
144 .flags = ASN1_TFLG_IMPLICIT,
145 .tag = 2,
146 .offset = offsetof(CMS_CertificateChoices, d.v2AttrCert),
147 .field_name = "d.v2AttrCert",
148 .item = &ASN1_SEQUENCE_it,
149 },
150 {
151 .flags = ASN1_TFLG_IMPLICIT,
152 .tag = 3,
153 .offset = offsetof(CMS_CertificateChoices, d.other),
154 .field_name = "d.other",
155 .item = &CMS_OtherCertificateFormat_it,
156 },
157 };
158
159 const ASN1_ITEM CMS_CertificateChoices_it = {
160 .itype = ASN1_ITYPE_CHOICE,
161 .utype = offsetof(CMS_CertificateChoices, type),
162 .templates = CMS_CertificateChoices_ch_tt,
163 .tcount = sizeof(CMS_CertificateChoices_ch_tt) / sizeof(ASN1_TEMPLATE),
164 .funcs = NULL,
165 .size = sizeof(CMS_CertificateChoices),
166 .sname = "CMS_CertificateChoices",
167 };
168
169 static const ASN1_TEMPLATE CMS_SignerIdentifier_ch_tt[] = {
170 {
171 .flags = 0,
172 .tag = 0,
173 .offset = offsetof(CMS_SignerIdentifier, d.issuerAndSerialNumber),
174 .field_name = "d.issuerAndSerialNumber",
175 .item = &CMS_IssuerAndSerialNumber_it,
176 },
177 {
178 .flags = ASN1_TFLG_IMPLICIT,
179 .tag = 0,
180 .offset = offsetof(CMS_SignerIdentifier, d.subjectKeyIdentifier),
181 .field_name = "d.subjectKeyIdentifier",
182 .item = &ASN1_OCTET_STRING_it,
183 },
184 };
185
186 static const ASN1_ITEM CMS_SignerIdentifier_it = {
187 .itype = ASN1_ITYPE_CHOICE,
188 .utype = offsetof(CMS_SignerIdentifier, type),
189 .templates = CMS_SignerIdentifier_ch_tt,
190 .tcount = sizeof(CMS_SignerIdentifier_ch_tt) / sizeof(ASN1_TEMPLATE),
191 .funcs = NULL,
192 .size = sizeof(CMS_SignerIdentifier),
193 .sname = "CMS_SignerIdentifier",
194 };
195
196 static const ASN1_TEMPLATE CMS_EncapsulatedContentInfo_seq_tt[] = {
197 {
198 .flags = 0,
199 .tag = 0,
200 .offset = offsetof(CMS_EncapsulatedContentInfo, eContentType),
201 .field_name = "eContentType",
202 .item = &ASN1_OBJECT_it,
203 },
204 {
205 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL | ASN1_TFLG_NDEF,
206 .tag = 0,
207 .offset = offsetof(CMS_EncapsulatedContentInfo, eContent),
208 .field_name = "eContent",
209 .item = &ASN1_OCTET_STRING_NDEF_it,
210 },
211 };
212
213 static const ASN1_ITEM CMS_EncapsulatedContentInfo_it = {
214 .itype = ASN1_ITYPE_NDEF_SEQUENCE,
215 .utype = V_ASN1_SEQUENCE,
216 .templates = CMS_EncapsulatedContentInfo_seq_tt,
217 .tcount = sizeof(CMS_EncapsulatedContentInfo_seq_tt) / sizeof(ASN1_TEMPLATE),
218 .funcs = NULL,
219 .size = sizeof(CMS_EncapsulatedContentInfo),
220 .sname = "CMS_EncapsulatedContentInfo",
221 };
222
223 /* Minor tweak to operation: free up signer key, cert */
224 static int
cms_si_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)225 cms_si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
226 {
227 if (operation == ASN1_OP_FREE_POST) {
228 CMS_SignerInfo *si = (CMS_SignerInfo *)*pval;
229 EVP_PKEY_free(si->pkey);
230 X509_free(si->signer);
231 EVP_MD_CTX_free(si->mctx);
232 }
233 return 1;
234 }
235
236 static const ASN1_AUX CMS_SignerInfo_aux = {
237 .app_data = NULL,
238 .flags = 0,
239 .ref_offset = 0,
240 .ref_lock = 0,
241 .asn1_cb = cms_si_cb,
242 .enc_offset = 0,
243 };
244 static const ASN1_TEMPLATE CMS_SignerInfo_seq_tt[] = {
245 {
246 .flags = 0,
247 .tag = 0,
248 .offset = offsetof(CMS_SignerInfo, version),
249 .field_name = "version",
250 .item = &LONG_it,
251 },
252 {
253 .flags = 0,
254 .tag = 0,
255 .offset = offsetof(CMS_SignerInfo, sid),
256 .field_name = "sid",
257 .item = &CMS_SignerIdentifier_it,
258 },
259 {
260 .flags = 0,
261 .tag = 0,
262 .offset = offsetof(CMS_SignerInfo, digestAlgorithm),
263 .field_name = "digestAlgorithm",
264 .item = &X509_ALGOR_it,
265 },
266 {
267 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL,
268 .tag = 0,
269 .offset = offsetof(CMS_SignerInfo, signedAttrs),
270 .field_name = "signedAttrs",
271 .item = &X509_ATTRIBUTE_it,
272 },
273 {
274 .flags = 0,
275 .tag = 0,
276 .offset = offsetof(CMS_SignerInfo, signatureAlgorithm),
277 .field_name = "signatureAlgorithm",
278 .item = &X509_ALGOR_it,
279 },
280 {
281 .flags = 0,
282 .tag = 0,
283 .offset = offsetof(CMS_SignerInfo, signature),
284 .field_name = "signature",
285 .item = &ASN1_OCTET_STRING_it,
286 },
287 {
288 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL,
289 .tag = 1,
290 .offset = offsetof(CMS_SignerInfo, unsignedAttrs),
291 .field_name = "unsignedAttrs",
292 .item = &X509_ATTRIBUTE_it,
293 },
294 };
295
296 const ASN1_ITEM CMS_SignerInfo_it = {
297 .itype = ASN1_ITYPE_SEQUENCE,
298 .utype = V_ASN1_SEQUENCE,
299 .templates = CMS_SignerInfo_seq_tt,
300 .tcount = sizeof(CMS_SignerInfo_seq_tt) / sizeof(ASN1_TEMPLATE),
301 .funcs = &CMS_SignerInfo_aux,
302 .size = sizeof(CMS_SignerInfo),
303 .sname = "CMS_SignerInfo",
304 };
305
306 static const ASN1_TEMPLATE CMS_OtherRevocationInfoFormat_seq_tt[] = {
307 {
308 .flags = 0,
309 .tag = 0,
310 .offset = offsetof(CMS_OtherRevocationInfoFormat, otherRevInfoFormat),
311 .field_name = "otherRevInfoFormat",
312 .item = &ASN1_OBJECT_it,
313 },
314 {
315 .flags = ASN1_TFLG_OPTIONAL,
316 .tag = 0,
317 .offset = offsetof(CMS_OtherRevocationInfoFormat, otherRevInfo),
318 .field_name = "otherRevInfo",
319 .item = &ASN1_ANY_it,
320 },
321 };
322
323 static const ASN1_ITEM CMS_OtherRevocationInfoFormat_it = {
324 .itype = ASN1_ITYPE_SEQUENCE,
325 .utype = V_ASN1_SEQUENCE,
326 .templates = CMS_OtherRevocationInfoFormat_seq_tt,
327 .tcount = sizeof(CMS_OtherRevocationInfoFormat_seq_tt) / sizeof(ASN1_TEMPLATE),
328 .funcs = NULL,
329 .size = sizeof(CMS_OtherRevocationInfoFormat),
330 .sname = "CMS_OtherRevocationInfoFormat",
331 };
332
333 static const ASN1_TEMPLATE CMS_RevocationInfoChoice_ch_tt[] = {
334 {
335 .flags = 0,
336 .tag = 0,
337 .offset = offsetof(CMS_RevocationInfoChoice, d.crl),
338 .field_name = "d.crl",
339 .item = &X509_CRL_it,
340 },
341 {
342 .flags = ASN1_TFLG_IMPLICIT,
343 .tag = 1,
344 .offset = offsetof(CMS_RevocationInfoChoice, d.other),
345 .field_name = "d.other",
346 .item = &CMS_OtherRevocationInfoFormat_it,
347 },
348 };
349
350 const ASN1_ITEM CMS_RevocationInfoChoice_it = {
351 .itype = ASN1_ITYPE_CHOICE,
352 .utype = offsetof(CMS_RevocationInfoChoice, type),
353 .templates = CMS_RevocationInfoChoice_ch_tt,
354 .tcount = sizeof(CMS_RevocationInfoChoice_ch_tt) / sizeof(ASN1_TEMPLATE),
355 .funcs = NULL,
356 .size = sizeof(CMS_RevocationInfoChoice),
357 .sname = "CMS_RevocationInfoChoice",
358 };
359
360 static const ASN1_TEMPLATE CMS_SignedData_seq_tt[] = {
361 {
362 .flags = 0,
363 .tag = 0,
364 .offset = offsetof(CMS_SignedData, version),
365 .field_name = "version",
366 .item = &LONG_it,
367 },
368 {
369 .flags = ASN1_TFLG_SET_OF,
370 .tag = 0,
371 .offset = offsetof(CMS_SignedData, digestAlgorithms),
372 .field_name = "digestAlgorithms",
373 .item = &X509_ALGOR_it,
374 },
375 {
376 .flags = 0,
377 .tag = 0,
378 .offset = offsetof(CMS_SignedData, encapContentInfo),
379 .field_name = "encapContentInfo",
380 .item = &CMS_EncapsulatedContentInfo_it,
381 },
382 {
383 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL,
384 .tag = 0,
385 .offset = offsetof(CMS_SignedData, certificates),
386 .field_name = "certificates",
387 .item = &CMS_CertificateChoices_it,
388 },
389 {
390 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL,
391 .tag = 1,
392 .offset = offsetof(CMS_SignedData, crls),
393 .field_name = "crls",
394 .item = &CMS_RevocationInfoChoice_it,
395 },
396 {
397 .flags = ASN1_TFLG_SET_OF,
398 .tag = 0,
399 .offset = offsetof(CMS_SignedData, signerInfos),
400 .field_name = "signerInfos",
401 .item = &CMS_SignerInfo_it,
402 },
403 };
404
405 const ASN1_ITEM CMS_SignedData_it = {
406 .itype = ASN1_ITYPE_NDEF_SEQUENCE,
407 .utype = V_ASN1_SEQUENCE,
408 .templates = CMS_SignedData_seq_tt,
409 .tcount = sizeof(CMS_SignedData_seq_tt) / sizeof(ASN1_TEMPLATE),
410 .funcs = NULL,
411 .size = sizeof(CMS_SignedData),
412 .sname = "CMS_SignedData",
413 };
414
415 static const ASN1_TEMPLATE CMS_OriginatorInfo_seq_tt[] = {
416 {
417 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL,
418 .tag = 0,
419 .offset = offsetof(CMS_OriginatorInfo, certificates),
420 .field_name = "certificates",
421 .item = &CMS_CertificateChoices_it,
422 },
423 {
424 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL,
425 .tag = 1,
426 .offset = offsetof(CMS_OriginatorInfo, crls),
427 .field_name = "crls",
428 .item = &CMS_RevocationInfoChoice_it,
429 },
430 };
431
432 static const ASN1_ITEM CMS_OriginatorInfo_it = {
433 .itype = ASN1_ITYPE_SEQUENCE,
434 .utype = V_ASN1_SEQUENCE,
435 .templates = CMS_OriginatorInfo_seq_tt,
436 .tcount = sizeof(CMS_OriginatorInfo_seq_tt) / sizeof(ASN1_TEMPLATE),
437 .funcs = NULL,
438 .size = sizeof(CMS_OriginatorInfo),
439 .sname = "CMS_OriginatorInfo",
440 };
441
442 static const ASN1_TEMPLATE CMS_EncryptedContentInfo_seq_tt[] = {
443 {
444 .flags = 0,
445 .tag = 0,
446 .offset = offsetof(CMS_EncryptedContentInfo, contentType),
447 .field_name = "contentType",
448 .item = &ASN1_OBJECT_it,
449 },
450 {
451 .flags = 0,
452 .tag = 0,
453 .offset = offsetof(CMS_EncryptedContentInfo, contentEncryptionAlgorithm),
454 .field_name = "contentEncryptionAlgorithm",
455 .item = &X509_ALGOR_it,
456 },
457 {
458 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
459 .tag = 0,
460 .offset = offsetof(CMS_EncryptedContentInfo, encryptedContent),
461 .field_name = "encryptedContent",
462 .item = &ASN1_OCTET_STRING_NDEF_it,
463 },
464 };
465
466 static const ASN1_ITEM CMS_EncryptedContentInfo_it = {
467 .itype = ASN1_ITYPE_NDEF_SEQUENCE,
468 .utype = V_ASN1_SEQUENCE,
469 .templates = CMS_EncryptedContentInfo_seq_tt,
470 .tcount = sizeof(CMS_EncryptedContentInfo_seq_tt) / sizeof(ASN1_TEMPLATE),
471 .funcs = NULL,
472 .size = sizeof(CMS_EncryptedContentInfo),
473 .sname = "CMS_EncryptedContentInfo",
474 };
475
476 static const ASN1_TEMPLATE CMS_KeyTransRecipientInfo_seq_tt[] = {
477 {
478 .flags = 0,
479 .tag = 0,
480 .offset = offsetof(CMS_KeyTransRecipientInfo, version),
481 .field_name = "version",
482 .item = &LONG_it,
483 },
484 {
485 .flags = 0,
486 .tag = 0,
487 .offset = offsetof(CMS_KeyTransRecipientInfo, rid),
488 .field_name = "rid",
489 .item = &CMS_SignerIdentifier_it,
490 },
491 {
492 .flags = 0,
493 .tag = 0,
494 .offset = offsetof(CMS_KeyTransRecipientInfo, keyEncryptionAlgorithm),
495 .field_name = "keyEncryptionAlgorithm",
496 .item = &X509_ALGOR_it,
497 },
498 {
499 .flags = 0,
500 .tag = 0,
501 .offset = offsetof(CMS_KeyTransRecipientInfo, encryptedKey),
502 .field_name = "encryptedKey",
503 .item = &ASN1_OCTET_STRING_it,
504 },
505 };
506
507 const ASN1_ITEM CMS_KeyTransRecipientInfo_it = {
508 .itype = ASN1_ITYPE_SEQUENCE,
509 .utype = V_ASN1_SEQUENCE,
510 .templates = CMS_KeyTransRecipientInfo_seq_tt,
511 .tcount = sizeof(CMS_KeyTransRecipientInfo_seq_tt) / sizeof(ASN1_TEMPLATE),
512 .funcs = NULL,
513 .size = sizeof(CMS_KeyTransRecipientInfo),
514 .sname = "CMS_KeyTransRecipientInfo",
515 };
516
517 static const ASN1_TEMPLATE CMS_OtherKeyAttribute_seq_tt[] = {
518 {
519 .flags = 0,
520 .tag = 0,
521 .offset = offsetof(CMS_OtherKeyAttribute, keyAttrId),
522 .field_name = "keyAttrId",
523 .item = &ASN1_OBJECT_it,
524 },
525 {
526 .flags = ASN1_TFLG_OPTIONAL,
527 .tag = 0,
528 .offset = offsetof(CMS_OtherKeyAttribute, keyAttr),
529 .field_name = "keyAttr",
530 .item = &ASN1_ANY_it,
531 },
532 };
533
534 const ASN1_ITEM CMS_OtherKeyAttribute_it = {
535 .itype = ASN1_ITYPE_SEQUENCE,
536 .utype = V_ASN1_SEQUENCE,
537 .templates = CMS_OtherKeyAttribute_seq_tt,
538 .tcount = sizeof(CMS_OtherKeyAttribute_seq_tt) / sizeof(ASN1_TEMPLATE),
539 .funcs = NULL,
540 .size = sizeof(CMS_OtherKeyAttribute),
541 .sname = "CMS_OtherKeyAttribute",
542 };
543
544 static const ASN1_TEMPLATE CMS_RecipientKeyIdentifier_seq_tt[] = {
545 {
546 .flags = 0,
547 .tag = 0,
548 .offset = offsetof(CMS_RecipientKeyIdentifier, subjectKeyIdentifier),
549 .field_name = "subjectKeyIdentifier",
550 .item = &ASN1_OCTET_STRING_it,
551 },
552 {
553 .flags = ASN1_TFLG_OPTIONAL,
554 .tag = 0,
555 .offset = offsetof(CMS_RecipientKeyIdentifier, date),
556 .field_name = "date",
557 .item = &ASN1_GENERALIZEDTIME_it,
558 },
559 {
560 .flags = ASN1_TFLG_OPTIONAL,
561 .tag = 0,
562 .offset = offsetof(CMS_RecipientKeyIdentifier, other),
563 .field_name = "other",
564 .item = &CMS_OtherKeyAttribute_it,
565 },
566 };
567
568 const ASN1_ITEM CMS_RecipientKeyIdentifier_it = {
569 .itype = ASN1_ITYPE_SEQUENCE,
570 .utype = V_ASN1_SEQUENCE,
571 .templates = CMS_RecipientKeyIdentifier_seq_tt,
572 .tcount = sizeof(CMS_RecipientKeyIdentifier_seq_tt) / sizeof(ASN1_TEMPLATE),
573 .funcs = NULL,
574 .size = sizeof(CMS_RecipientKeyIdentifier),
575 .sname = "CMS_RecipientKeyIdentifier",
576 };
577
578 static const ASN1_TEMPLATE CMS_KeyAgreeRecipientIdentifier_ch_tt[] = {
579 {
580 .flags = 0,
581 .tag = 0,
582 .offset = offsetof(CMS_KeyAgreeRecipientIdentifier, d.issuerAndSerialNumber),
583 .field_name = "d.issuerAndSerialNumber",
584 .item = &CMS_IssuerAndSerialNumber_it,
585 },
586 {
587 .flags = ASN1_TFLG_IMPLICIT,
588 .tag = 0,
589 .offset = offsetof(CMS_KeyAgreeRecipientIdentifier, d.rKeyId),
590 .field_name = "d.rKeyId",
591 .item = &CMS_RecipientKeyIdentifier_it,
592 },
593 };
594
595 static const ASN1_ITEM CMS_KeyAgreeRecipientIdentifier_it = {
596 .itype = ASN1_ITYPE_CHOICE,
597 .utype = offsetof(CMS_KeyAgreeRecipientIdentifier, type),
598 .templates = CMS_KeyAgreeRecipientIdentifier_ch_tt,
599 .tcount = sizeof(CMS_KeyAgreeRecipientIdentifier_ch_tt) / sizeof(ASN1_TEMPLATE),
600 .funcs = NULL,
601 .size = sizeof(CMS_KeyAgreeRecipientIdentifier),
602 .sname = "CMS_KeyAgreeRecipientIdentifier",
603 };
604
605 static int
cms_rek_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)606 cms_rek_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
607 {
608 CMS_RecipientEncryptedKey *rek = (CMS_RecipientEncryptedKey *)*pval;
609 if (operation == ASN1_OP_FREE_POST) {
610 EVP_PKEY_free(rek->pkey);
611 }
612 return 1;
613 }
614
615 static const ASN1_AUX CMS_RecipientEncryptedKey_aux = {
616 .app_data = NULL,
617 .flags = 0,
618 .ref_offset = 0,
619 .ref_lock = 0,
620 .asn1_cb = cms_rek_cb,
621 .enc_offset = 0,
622 };
623 static const ASN1_TEMPLATE CMS_RecipientEncryptedKey_seq_tt[] = {
624 {
625 .flags = 0,
626 .tag = 0,
627 .offset = offsetof(CMS_RecipientEncryptedKey, rid),
628 .field_name = "rid",
629 .item = &CMS_KeyAgreeRecipientIdentifier_it,
630 },
631 {
632 .flags = 0,
633 .tag = 0,
634 .offset = offsetof(CMS_RecipientEncryptedKey, encryptedKey),
635 .field_name = "encryptedKey",
636 .item = &ASN1_OCTET_STRING_it,
637 },
638 };
639
640 const ASN1_ITEM CMS_RecipientEncryptedKey_it = {
641 .itype = ASN1_ITYPE_SEQUENCE,
642 .utype = V_ASN1_SEQUENCE,
643 .templates = CMS_RecipientEncryptedKey_seq_tt,
644 .tcount = sizeof(CMS_RecipientEncryptedKey_seq_tt) / sizeof(ASN1_TEMPLATE),
645 .funcs = &CMS_RecipientEncryptedKey_aux,
646 .size = sizeof(CMS_RecipientEncryptedKey),
647 .sname = "CMS_RecipientEncryptedKey",
648 };
649
650 static const ASN1_TEMPLATE CMS_OriginatorPublicKey_seq_tt[] = {
651 {
652 .flags = 0,
653 .tag = 0,
654 .offset = offsetof(CMS_OriginatorPublicKey, algorithm),
655 .field_name = "algorithm",
656 .item = &X509_ALGOR_it,
657 },
658 {
659 .flags = 0,
660 .tag = 0,
661 .offset = offsetof(CMS_OriginatorPublicKey, publicKey),
662 .field_name = "publicKey",
663 .item = &ASN1_BIT_STRING_it,
664 },
665 };
666
667 const ASN1_ITEM CMS_OriginatorPublicKey_it = {
668 .itype = ASN1_ITYPE_SEQUENCE,
669 .utype = V_ASN1_SEQUENCE,
670 .templates = CMS_OriginatorPublicKey_seq_tt,
671 .tcount = sizeof(CMS_OriginatorPublicKey_seq_tt) / sizeof(ASN1_TEMPLATE),
672 .funcs = NULL,
673 .size = sizeof(CMS_OriginatorPublicKey),
674 .sname = "CMS_OriginatorPublicKey",
675 };
676
677 static const ASN1_TEMPLATE CMS_OriginatorIdentifierOrKey_ch_tt[] = {
678 {
679 .flags = 0,
680 .tag = 0,
681 .offset = offsetof(CMS_OriginatorIdentifierOrKey, d.issuerAndSerialNumber),
682 .field_name = "d.issuerAndSerialNumber",
683 .item = &CMS_IssuerAndSerialNumber_it,
684 },
685 {
686 .flags = ASN1_TFLG_IMPLICIT,
687 .tag = 0,
688 .offset = offsetof(CMS_OriginatorIdentifierOrKey, d.subjectKeyIdentifier),
689 .field_name = "d.subjectKeyIdentifier",
690 .item = &ASN1_OCTET_STRING_it,
691 },
692 {
693 .flags = ASN1_TFLG_IMPLICIT,
694 .tag = 1,
695 .offset = offsetof(CMS_OriginatorIdentifierOrKey, d.originatorKey),
696 .field_name = "d.originatorKey",
697 .item = &CMS_OriginatorPublicKey_it,
698 },
699 };
700
701 static const ASN1_ITEM CMS_OriginatorIdentifierOrKey_it = {
702 .itype = ASN1_ITYPE_CHOICE,
703 .utype = offsetof(CMS_OriginatorIdentifierOrKey, type),
704 .templates = CMS_OriginatorIdentifierOrKey_ch_tt,
705 .tcount = sizeof(CMS_OriginatorIdentifierOrKey_ch_tt) / sizeof(ASN1_TEMPLATE),
706 .funcs = NULL,
707 .size = sizeof(CMS_OriginatorIdentifierOrKey),
708 .sname = "CMS_OriginatorIdentifierOrKey",
709 };
710
711 static int
cms_kari_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)712 cms_kari_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
713 {
714 CMS_KeyAgreeRecipientInfo *kari = (CMS_KeyAgreeRecipientInfo *)*pval;
715 if (operation == ASN1_OP_NEW_POST) {
716 kari->ctx = EVP_CIPHER_CTX_new();
717 if (kari->ctx == NULL)
718 return 0;
719 EVP_CIPHER_CTX_set_flags(kari->ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
720 kari->pctx = NULL;
721 } else if (operation == ASN1_OP_FREE_POST) {
722 EVP_PKEY_CTX_free(kari->pctx);
723 EVP_CIPHER_CTX_free(kari->ctx);
724 }
725 return 1;
726 }
727
728 static const ASN1_AUX CMS_KeyAgreeRecipientInfo_aux = {
729 .app_data = NULL,
730 .flags = 0,
731 .ref_offset = 0,
732 .ref_lock = 0,
733 .asn1_cb = cms_kari_cb,
734 .enc_offset = 0,
735 };
736 static const ASN1_TEMPLATE CMS_KeyAgreeRecipientInfo_seq_tt[] = {
737 {
738 .flags = 0,
739 .tag = 0,
740 .offset = offsetof(CMS_KeyAgreeRecipientInfo, version),
741 .field_name = "version",
742 .item = &LONG_it,
743 },
744 {
745 .flags = ASN1_TFLG_EXPLICIT,
746 .tag = 0,
747 .offset = offsetof(CMS_KeyAgreeRecipientInfo, originator),
748 .field_name = "originator",
749 .item = &CMS_OriginatorIdentifierOrKey_it,
750 },
751 {
752 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL,
753 .tag = 1,
754 .offset = offsetof(CMS_KeyAgreeRecipientInfo, ukm),
755 .field_name = "ukm",
756 .item = &ASN1_OCTET_STRING_it,
757 },
758 {
759 .flags = 0,
760 .tag = 0,
761 .offset = offsetof(CMS_KeyAgreeRecipientInfo, keyEncryptionAlgorithm),
762 .field_name = "keyEncryptionAlgorithm",
763 .item = &X509_ALGOR_it,
764 },
765 {
766 .flags = ASN1_TFLG_SEQUENCE_OF,
767 .tag = 0,
768 .offset = offsetof(CMS_KeyAgreeRecipientInfo, recipientEncryptedKeys),
769 .field_name = "recipientEncryptedKeys",
770 .item = &CMS_RecipientEncryptedKey_it,
771 },
772 };
773
774 const ASN1_ITEM CMS_KeyAgreeRecipientInfo_it = {
775 .itype = ASN1_ITYPE_SEQUENCE,
776 .utype = V_ASN1_SEQUENCE,
777 .templates = CMS_KeyAgreeRecipientInfo_seq_tt,
778 .tcount = sizeof(CMS_KeyAgreeRecipientInfo_seq_tt) / sizeof(ASN1_TEMPLATE),
779 .funcs = &CMS_KeyAgreeRecipientInfo_aux,
780 .size = sizeof(CMS_KeyAgreeRecipientInfo),
781 .sname = "CMS_KeyAgreeRecipientInfo",
782 };
783
784 static const ASN1_TEMPLATE CMS_KEKIdentifier_seq_tt[] = {
785 {
786 .flags = 0,
787 .tag = 0,
788 .offset = offsetof(CMS_KEKIdentifier, keyIdentifier),
789 .field_name = "keyIdentifier",
790 .item = &ASN1_OCTET_STRING_it,
791 },
792 {
793 .flags = ASN1_TFLG_OPTIONAL,
794 .tag = 0,
795 .offset = offsetof(CMS_KEKIdentifier, date),
796 .field_name = "date",
797 .item = &ASN1_GENERALIZEDTIME_it,
798 },
799 {
800 .flags = ASN1_TFLG_OPTIONAL,
801 .tag = 0,
802 .offset = offsetof(CMS_KEKIdentifier, other),
803 .field_name = "other",
804 .item = &CMS_OtherKeyAttribute_it,
805 },
806 };
807
808 static const ASN1_ITEM CMS_KEKIdentifier_it = {
809 .itype = ASN1_ITYPE_SEQUENCE,
810 .utype = V_ASN1_SEQUENCE,
811 .templates = CMS_KEKIdentifier_seq_tt,
812 .tcount = sizeof(CMS_KEKIdentifier_seq_tt) / sizeof(ASN1_TEMPLATE),
813 .funcs = NULL,
814 .size = sizeof(CMS_KEKIdentifier),
815 .sname = "CMS_KEKIdentifier",
816 };
817
818 static const ASN1_TEMPLATE CMS_KEKRecipientInfo_seq_tt[] = {
819 {
820 .flags = 0,
821 .tag = 0,
822 .offset = offsetof(CMS_KEKRecipientInfo, version),
823 .field_name = "version",
824 .item = &LONG_it,
825 },
826 {
827 .flags = 0,
828 .tag = 0,
829 .offset = offsetof(CMS_KEKRecipientInfo, kekid),
830 .field_name = "kekid",
831 .item = &CMS_KEKIdentifier_it,
832 },
833 {
834 .flags = 0,
835 .tag = 0,
836 .offset = offsetof(CMS_KEKRecipientInfo, keyEncryptionAlgorithm),
837 .field_name = "keyEncryptionAlgorithm",
838 .item = &X509_ALGOR_it,
839 },
840 {
841 .flags = 0,
842 .tag = 0,
843 .offset = offsetof(CMS_KEKRecipientInfo, encryptedKey),
844 .field_name = "encryptedKey",
845 .item = &ASN1_OCTET_STRING_it,
846 },
847 };
848
849 const ASN1_ITEM CMS_KEKRecipientInfo_it = {
850 .itype = ASN1_ITYPE_SEQUENCE,
851 .utype = V_ASN1_SEQUENCE,
852 .templates = CMS_KEKRecipientInfo_seq_tt,
853 .tcount = sizeof(CMS_KEKRecipientInfo_seq_tt) / sizeof(ASN1_TEMPLATE),
854 .funcs = NULL,
855 .size = sizeof(CMS_KEKRecipientInfo),
856 .sname = "CMS_KEKRecipientInfo",
857 };
858
859 static const ASN1_TEMPLATE CMS_PasswordRecipientInfo_seq_tt[] = {
860 {
861 .flags = 0,
862 .tag = 0,
863 .offset = offsetof(CMS_PasswordRecipientInfo, version),
864 .field_name = "version",
865 .item = &LONG_it,
866 },
867 {
868 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
869 .tag = 0,
870 .offset = offsetof(CMS_PasswordRecipientInfo, keyDerivationAlgorithm),
871 .field_name = "keyDerivationAlgorithm",
872 .item = &X509_ALGOR_it,
873 },
874 {
875 .flags = 0,
876 .tag = 0,
877 .offset = offsetof(CMS_PasswordRecipientInfo, keyEncryptionAlgorithm),
878 .field_name = "keyEncryptionAlgorithm",
879 .item = &X509_ALGOR_it,
880 },
881 {
882 .flags = 0,
883 .tag = 0,
884 .offset = offsetof(CMS_PasswordRecipientInfo, encryptedKey),
885 .field_name = "encryptedKey",
886 .item = &ASN1_OCTET_STRING_it,
887 },
888 };
889
890 const ASN1_ITEM CMS_PasswordRecipientInfo_it = {
891 .itype = ASN1_ITYPE_SEQUENCE,
892 .utype = V_ASN1_SEQUENCE,
893 .templates = CMS_PasswordRecipientInfo_seq_tt,
894 .tcount = sizeof(CMS_PasswordRecipientInfo_seq_tt) / sizeof(ASN1_TEMPLATE),
895 .funcs = NULL,
896 .size = sizeof(CMS_PasswordRecipientInfo),
897 .sname = "CMS_PasswordRecipientInfo",
898 };
899
900 static const ASN1_TEMPLATE CMS_OtherRecipientInfo_seq_tt[] = {
901 {
902 .flags = 0,
903 .tag = 0,
904 .offset = offsetof(CMS_OtherRecipientInfo, oriType),
905 .field_name = "oriType",
906 .item = &ASN1_OBJECT_it,
907 },
908 {
909 .flags = ASN1_TFLG_OPTIONAL,
910 .tag = 0,
911 .offset = offsetof(CMS_OtherRecipientInfo, oriValue),
912 .field_name = "oriValue",
913 .item = &ASN1_ANY_it,
914 },
915 };
916
917 static const ASN1_ITEM CMS_OtherRecipientInfo_it = {
918 .itype = ASN1_ITYPE_SEQUENCE,
919 .utype = V_ASN1_SEQUENCE,
920 .templates = CMS_OtherRecipientInfo_seq_tt,
921 .tcount = sizeof(CMS_OtherRecipientInfo_seq_tt) / sizeof(ASN1_TEMPLATE),
922 .funcs = NULL,
923 .size = sizeof(CMS_OtherRecipientInfo),
924 .sname = "CMS_OtherRecipientInfo",
925 };
926
927 /* Free up RecipientInfo additional data */
928 static int
cms_ri_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)929 cms_ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
930 {
931 if (operation == ASN1_OP_FREE_PRE) {
932 CMS_RecipientInfo *ri = (CMS_RecipientInfo *)*pval;
933 if (ri->type == CMS_RECIPINFO_TRANS) {
934 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
935 EVP_PKEY_free(ktri->pkey);
936 X509_free(ktri->recip);
937 EVP_PKEY_CTX_free(ktri->pctx);
938 } else if (ri->type == CMS_RECIPINFO_KEK) {
939 CMS_KEKRecipientInfo *kekri = ri->d.kekri;
940 freezero(kekri->key, kekri->keylen);
941 } else if (ri->type == CMS_RECIPINFO_PASS) {
942 CMS_PasswordRecipientInfo *pwri = ri->d.pwri;
943 freezero(pwri->pass, pwri->passlen);
944 }
945 }
946 return 1;
947 }
948
949 static const ASN1_AUX CMS_RecipientInfo_aux = {
950 .app_data = NULL,
951 .flags = 0,
952 .ref_offset = 0,
953 .ref_lock = 0,
954 .asn1_cb = cms_ri_cb,
955 .enc_offset = 0,
956 };
957 static const ASN1_TEMPLATE CMS_RecipientInfo_ch_tt[] = {
958 {
959 .flags = 0,
960 .tag = 0,
961 .offset = offsetof(CMS_RecipientInfo, d.ktri),
962 .field_name = "d.ktri",
963 .item = &CMS_KeyTransRecipientInfo_it,
964 },
965 {
966 .flags = ASN1_TFLG_IMPLICIT,
967 .tag = 1,
968 .offset = offsetof(CMS_RecipientInfo, d.kari),
969 .field_name = "d.kari",
970 .item = &CMS_KeyAgreeRecipientInfo_it,
971 },
972 {
973 .flags = ASN1_TFLG_IMPLICIT,
974 .tag = 2,
975 .offset = offsetof(CMS_RecipientInfo, d.kekri),
976 .field_name = "d.kekri",
977 .item = &CMS_KEKRecipientInfo_it,
978 },
979 {
980 .flags = ASN1_TFLG_IMPLICIT,
981 .tag = 3,
982 .offset = offsetof(CMS_RecipientInfo, d.pwri),
983 .field_name = "d.pwri",
984 .item = &CMS_PasswordRecipientInfo_it,
985 },
986 {
987 .flags = ASN1_TFLG_IMPLICIT,
988 .tag = 4,
989 .offset = offsetof(CMS_RecipientInfo, d.ori),
990 .field_name = "d.ori",
991 .item = &CMS_OtherRecipientInfo_it,
992 },
993 };
994
995 const ASN1_ITEM CMS_RecipientInfo_it = {
996 .itype = ASN1_ITYPE_CHOICE,
997 .utype = offsetof(CMS_RecipientInfo, type),
998 .templates = CMS_RecipientInfo_ch_tt,
999 .tcount = sizeof(CMS_RecipientInfo_ch_tt) / sizeof(ASN1_TEMPLATE),
1000 .funcs = &CMS_RecipientInfo_aux,
1001 .size = sizeof(CMS_RecipientInfo),
1002 .sname = "CMS_RecipientInfo",
1003 };
1004
1005 static const ASN1_TEMPLATE CMS_EnvelopedData_seq_tt[] = {
1006 {
1007 .flags = 0,
1008 .tag = 0,
1009 .offset = offsetof(CMS_EnvelopedData, version),
1010 .field_name = "version",
1011 .item = &LONG_it,
1012 },
1013 {
1014 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
1015 .tag = 0,
1016 .offset = offsetof(CMS_EnvelopedData, originatorInfo),
1017 .field_name = "originatorInfo",
1018 .item = &CMS_OriginatorInfo_it,
1019 },
1020 {
1021 .flags = ASN1_TFLG_SET_OF,
1022 .tag = 0,
1023 .offset = offsetof(CMS_EnvelopedData, recipientInfos),
1024 .field_name = "recipientInfos",
1025 .item = &CMS_RecipientInfo_it,
1026 },
1027 {
1028 .flags = 0,
1029 .tag = 0,
1030 .offset = offsetof(CMS_EnvelopedData, encryptedContentInfo),
1031 .field_name = "encryptedContentInfo",
1032 .item = &CMS_EncryptedContentInfo_it,
1033 },
1034 {
1035 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL,
1036 .tag = 1,
1037 .offset = offsetof(CMS_EnvelopedData, unprotectedAttrs),
1038 .field_name = "unprotectedAttrs",
1039 .item = &X509_ATTRIBUTE_it,
1040 },
1041 };
1042
1043 const ASN1_ITEM CMS_EnvelopedData_it = {
1044 .itype = ASN1_ITYPE_NDEF_SEQUENCE,
1045 .utype = V_ASN1_SEQUENCE,
1046 .templates = CMS_EnvelopedData_seq_tt,
1047 .tcount = sizeof(CMS_EnvelopedData_seq_tt) / sizeof(ASN1_TEMPLATE),
1048 .funcs = NULL,
1049 .size = sizeof(CMS_EnvelopedData),
1050 .sname = "CMS_EnvelopedData",
1051 };
1052
1053 static const ASN1_TEMPLATE CMS_DigestedData_seq_tt[] = {
1054 {
1055 .flags = 0,
1056 .tag = 0,
1057 .offset = offsetof(CMS_DigestedData, version),
1058 .field_name = "version",
1059 .item = &LONG_it,
1060 },
1061 {
1062 .flags = 0,
1063 .tag = 0,
1064 .offset = offsetof(CMS_DigestedData, digestAlgorithm),
1065 .field_name = "digestAlgorithm",
1066 .item = &X509_ALGOR_it,
1067 },
1068 {
1069 .flags = 0,
1070 .tag = 0,
1071 .offset = offsetof(CMS_DigestedData, encapContentInfo),
1072 .field_name = "encapContentInfo",
1073 .item = &CMS_EncapsulatedContentInfo_it,
1074 },
1075 {
1076 .flags = 0,
1077 .tag = 0,
1078 .offset = offsetof(CMS_DigestedData, digest),
1079 .field_name = "digest",
1080 .item = &ASN1_OCTET_STRING_it,
1081 },
1082 };
1083
1084 const ASN1_ITEM CMS_DigestedData_it = {
1085 .itype = ASN1_ITYPE_NDEF_SEQUENCE,
1086 .utype = V_ASN1_SEQUENCE,
1087 .templates = CMS_DigestedData_seq_tt,
1088 .tcount = sizeof(CMS_DigestedData_seq_tt) / sizeof(ASN1_TEMPLATE),
1089 .funcs = NULL,
1090 .size = sizeof(CMS_DigestedData),
1091 .sname = "CMS_DigestedData",
1092 };
1093
1094 static const ASN1_TEMPLATE CMS_EncryptedData_seq_tt[] = {
1095 {
1096 .flags = 0,
1097 .tag = 0,
1098 .offset = offsetof(CMS_EncryptedData, version),
1099 .field_name = "version",
1100 .item = &LONG_it,
1101 },
1102 {
1103 .flags = 0,
1104 .tag = 0,
1105 .offset = offsetof(CMS_EncryptedData, encryptedContentInfo),
1106 .field_name = "encryptedContentInfo",
1107 .item = &CMS_EncryptedContentInfo_it,
1108 },
1109 {
1110 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL,
1111 .tag = 1,
1112 .offset = offsetof(CMS_EncryptedData, unprotectedAttrs),
1113 .field_name = "unprotectedAttrs",
1114 .item = &X509_ATTRIBUTE_it,
1115 },
1116 };
1117
1118 const ASN1_ITEM CMS_EncryptedData_it = {
1119 .itype = ASN1_ITYPE_NDEF_SEQUENCE,
1120 .utype = V_ASN1_SEQUENCE,
1121 .templates = CMS_EncryptedData_seq_tt,
1122 .tcount = sizeof(CMS_EncryptedData_seq_tt) / sizeof(ASN1_TEMPLATE),
1123 .funcs = NULL,
1124 .size = sizeof(CMS_EncryptedData),
1125 .sname = "CMS_EncryptedData",
1126 };
1127
1128 static const ASN1_TEMPLATE CMS_AuthenticatedData_seq_tt[] = {
1129 {
1130 .flags = 0,
1131 .tag = 0,
1132 .offset = offsetof(CMS_AuthenticatedData, version),
1133 .field_name = "version",
1134 .item = &LONG_it,
1135 },
1136 {
1137 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
1138 .tag = 0,
1139 .offset = offsetof(CMS_AuthenticatedData, originatorInfo),
1140 .field_name = "originatorInfo",
1141 .item = &CMS_OriginatorInfo_it,
1142 },
1143 {
1144 .flags = ASN1_TFLG_SET_OF,
1145 .tag = 0,
1146 .offset = offsetof(CMS_AuthenticatedData, recipientInfos),
1147 .field_name = "recipientInfos",
1148 .item = &CMS_RecipientInfo_it,
1149 },
1150 {
1151 .flags = 0,
1152 .tag = 0,
1153 .offset = offsetof(CMS_AuthenticatedData, macAlgorithm),
1154 .field_name = "macAlgorithm",
1155 .item = &X509_ALGOR_it,
1156 },
1157 {
1158 .flags = ASN1_TFLG_IMPLICIT,
1159 .tag = 1,
1160 .offset = offsetof(CMS_AuthenticatedData, digestAlgorithm),
1161 .field_name = "digestAlgorithm",
1162 .item = &X509_ALGOR_it,
1163 },
1164 {
1165 .flags = 0,
1166 .tag = 0,
1167 .offset = offsetof(CMS_AuthenticatedData, encapContentInfo),
1168 .field_name = "encapContentInfo",
1169 .item = &CMS_EncapsulatedContentInfo_it,
1170 },
1171 {
1172 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL,
1173 .tag = 2,
1174 .offset = offsetof(CMS_AuthenticatedData, authAttrs),
1175 .field_name = "authAttrs",
1176 .item = &X509_ALGOR_it,
1177 },
1178 {
1179 .flags = 0,
1180 .tag = 0,
1181 .offset = offsetof(CMS_AuthenticatedData, mac),
1182 .field_name = "mac",
1183 .item = &ASN1_OCTET_STRING_it,
1184 },
1185 {
1186 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL,
1187 .tag = 3,
1188 .offset = offsetof(CMS_AuthenticatedData, unauthAttrs),
1189 .field_name = "unauthAttrs",
1190 .item = &X509_ALGOR_it,
1191 },
1192 };
1193
1194 static const ASN1_ITEM CMS_AuthenticatedData_it = {
1195 .itype = ASN1_ITYPE_NDEF_SEQUENCE,
1196 .utype = V_ASN1_SEQUENCE,
1197 .templates = CMS_AuthenticatedData_seq_tt,
1198 .tcount = sizeof(CMS_AuthenticatedData_seq_tt) / sizeof(ASN1_TEMPLATE),
1199 .funcs = NULL,
1200 .size = sizeof(CMS_AuthenticatedData),
1201 .sname = "CMS_AuthenticatedData",
1202 };
1203
1204 static const ASN1_TEMPLATE CMS_CompressedData_seq_tt[] = {
1205 {
1206 .flags = 0,
1207 .tag = 0,
1208 .offset = offsetof(CMS_CompressedData, version),
1209 .field_name = "version",
1210 .item = &LONG_it,
1211 },
1212 {
1213 .flags = 0,
1214 .tag = 0,
1215 .offset = offsetof(CMS_CompressedData, compressionAlgorithm),
1216 .field_name = "compressionAlgorithm",
1217 .item = &X509_ALGOR_it,
1218 },
1219 {
1220 .flags = 0,
1221 .tag = 0,
1222 .offset = offsetof(CMS_CompressedData, encapContentInfo),
1223 .field_name = "encapContentInfo",
1224 .item = &CMS_EncapsulatedContentInfo_it,
1225 },
1226 };
1227
1228 const ASN1_ITEM CMS_CompressedData_it = {
1229 .itype = ASN1_ITYPE_NDEF_SEQUENCE,
1230 .utype = V_ASN1_SEQUENCE,
1231 .templates = CMS_CompressedData_seq_tt,
1232 .tcount = sizeof(CMS_CompressedData_seq_tt) / sizeof(ASN1_TEMPLATE),
1233 .funcs = NULL,
1234 .size = sizeof(CMS_CompressedData),
1235 .sname = "CMS_CompressedData",
1236 };
1237
1238 /* This is the ANY DEFINED BY table for the top level ContentInfo structure */
1239
1240 static const ASN1_TEMPLATE cms_default_tt = {
1241 .flags = ASN1_TFLG_EXPLICIT,
1242 .tag = 0,
1243 .offset = offsetof(CMS_ContentInfo, d.other),
1244 .field_name = "d.other",
1245 .item = &ASN1_ANY_it,
1246 };
1247
1248 static const ASN1_ADB_TABLE CMS_ContentInfo_adbtbl[] = {
1249 {
1250 .value = NID_pkcs7_data,
1251 .tt = {
1252 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_NDEF,
1253 .tag = 0,
1254 .offset = offsetof(CMS_ContentInfo, d.data),
1255 .field_name = "d.data",
1256 .item = &ASN1_OCTET_STRING_NDEF_it,
1257 },
1258 },
1259 {
1260 .value = NID_pkcs7_signed,
1261 .tt = {
1262 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_NDEF,
1263 .tag = 0,
1264 .offset = offsetof(CMS_ContentInfo, d.signedData),
1265 .field_name = "d.signedData",
1266 .item = &CMS_SignedData_it,
1267 },
1268 },
1269 {
1270 .value = NID_pkcs7_enveloped,
1271 .tt = {
1272 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_NDEF,
1273 .tag = 0,
1274 .offset = offsetof(CMS_ContentInfo, d.envelopedData),
1275 .field_name = "d.envelopedData",
1276 .item = &CMS_EnvelopedData_it,
1277 },
1278 },
1279 {
1280 .value = NID_pkcs7_digest,
1281 .tt = {
1282 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_NDEF,
1283 .tag = 0,
1284 .offset = offsetof(CMS_ContentInfo, d.digestedData),
1285 .field_name = "d.digestedData",
1286 .item = &CMS_DigestedData_it,
1287 },
1288 },
1289 {
1290 .value = NID_pkcs7_encrypted,
1291 .tt = {
1292 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_NDEF,
1293 .tag = 0,
1294 .offset = offsetof(CMS_ContentInfo, d.encryptedData),
1295 .field_name = "d.encryptedData",
1296 .item = &CMS_EncryptedData_it,
1297 },
1298 },
1299 {
1300 .value = NID_id_smime_ct_authData,
1301 .tt = {
1302 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_NDEF,
1303 .tag = 0,
1304 .offset = offsetof(CMS_ContentInfo, d.authenticatedData),
1305 .field_name = "d.authenticatedData",
1306 .item = &CMS_AuthenticatedData_it,
1307 },
1308 },
1309 {
1310 .value = NID_id_smime_ct_compressedData,
1311 .tt = {
1312 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_NDEF,
1313 .tag = 0,
1314 .offset = offsetof(CMS_ContentInfo, d.compressedData),
1315 .field_name = "d.compressedData",
1316 .item = &CMS_CompressedData_it,
1317 },
1318 },
1319 };
1320
1321 static const ASN1_ADB CMS_ContentInfo_adb = {
1322 .flags = 0,
1323 .offset = offsetof(CMS_ContentInfo, contentType),
1324 .tbl = CMS_ContentInfo_adbtbl,
1325 .tblcount = sizeof(CMS_ContentInfo_adbtbl) / sizeof(ASN1_ADB_TABLE),
1326 .default_tt = &cms_default_tt,
1327 .null_tt = NULL,
1328 };
1329
1330 /* CMS streaming support */
1331 static int
cms_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)1332 cms_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
1333 {
1334 ASN1_STREAM_ARG *sarg = exarg;
1335 CMS_ContentInfo *cms = NULL;
1336
1337 if (pval)
1338 cms = (CMS_ContentInfo *)*pval;
1339 else
1340 return 1;
1341
1342 switch (operation) {
1343 case ASN1_OP_STREAM_PRE:
1344 if (CMS_stream(&sarg->boundary, cms) <= 0)
1345 return 0;
1346 /* FALLTHROUGH */
1347
1348 case ASN1_OP_DETACHED_PRE:
1349 sarg->ndef_bio = CMS_dataInit(cms, sarg->out);
1350 if (!sarg->ndef_bio)
1351 return 0;
1352 break;
1353
1354 case ASN1_OP_STREAM_POST:
1355 case ASN1_OP_DETACHED_POST:
1356 if (CMS_dataFinal(cms, sarg->ndef_bio) <= 0)
1357 return 0;
1358 break;
1359 }
1360
1361 return 1;
1362 }
1363
1364 static const ASN1_AUX CMS_ContentInfo_aux = {
1365 .app_data = NULL,
1366 .flags = 0,
1367 .ref_offset = 0,
1368 .ref_lock = 0,
1369 .asn1_cb = cms_cb,
1370 .enc_offset = 0,
1371 };
1372 static const ASN1_TEMPLATE CMS_ContentInfo_seq_tt[] = {
1373 {
1374 .flags = 0,
1375 .tag = 0,
1376 .offset = offsetof(CMS_ContentInfo, contentType),
1377 .field_name = "contentType",
1378 .item = &ASN1_OBJECT_it,
1379 },
1380 {
1381 .flags = ASN1_TFLG_ADB_OID,
1382 .tag = -1,
1383 .offset = 0,
1384 .field_name = "CMS_ContentInfo",
1385 .item = (const ASN1_ITEM *)&CMS_ContentInfo_adb,
1386 },
1387 };
1388
1389 const ASN1_ITEM CMS_ContentInfo_it = {
1390 .itype = ASN1_ITYPE_NDEF_SEQUENCE,
1391 .utype = V_ASN1_SEQUENCE,
1392 .templates = CMS_ContentInfo_seq_tt,
1393 .tcount = sizeof(CMS_ContentInfo_seq_tt) / sizeof(ASN1_TEMPLATE),
1394 .funcs = &CMS_ContentInfo_aux,
1395 .size = sizeof(CMS_ContentInfo),
1396 .sname = "CMS_ContentInfo",
1397 };
1398 LCRYPTO_ALIAS(CMS_ContentInfo_it);
1399
1400 /* Specials for signed attributes */
1401
1402 /*
1403 * When signing attributes we want to reorder them to match the sorted
1404 * encoding.
1405 */
1406
1407 static const ASN1_TEMPLATE CMS_Attributes_Sign_item_tt = {
1408 .flags = ASN1_TFLG_SET_ORDER,
1409 .tag = 0,
1410 .offset = 0,
1411 .field_name = "CMS_ATTRIBUTES",
1412 .item = &X509_ATTRIBUTE_it,
1413 };
1414
1415 const ASN1_ITEM CMS_Attributes_Sign_it = {
1416 .itype = ASN1_ITYPE_PRIMITIVE,
1417 .utype = -1,
1418 .templates = &CMS_Attributes_Sign_item_tt,
1419 .tcount = 0,
1420 .funcs = NULL,
1421 .size = 0,
1422 .sname = "CMS_Attributes_Sign",
1423 };
1424
1425 /*
1426 * When verifying attributes we need to use the received order. So we use
1427 * SEQUENCE OF and tag it to SET OF
1428 */
1429
1430 static const ASN1_TEMPLATE CMS_Attributes_Verify_item_tt = {
1431 .flags = ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL,
1432 .tag = V_ASN1_SET,
1433 .offset = 0,
1434 .field_name = "CMS_ATTRIBUTES",
1435 .item = &X509_ATTRIBUTE_it,
1436 };
1437
1438 const ASN1_ITEM CMS_Attributes_Verify_it = {
1439 .itype = ASN1_ITYPE_PRIMITIVE,
1440 .utype = -1,
1441 .templates = &CMS_Attributes_Verify_item_tt,
1442 .tcount = 0,
1443 .funcs = NULL,
1444 .size = 0,
1445 .sname = "CMS_Attributes_Verify",
1446 };
1447
1448
1449
1450 static const ASN1_TEMPLATE CMS_ReceiptsFrom_ch_tt[] = {
1451 {
1452 .flags = ASN1_TFLG_IMPLICIT,
1453 .tag = 0,
1454 .offset = offsetof(CMS_ReceiptsFrom, d.allOrFirstTier),
1455 .field_name = "d.allOrFirstTier",
1456 .item = &LONG_it,
1457 },
1458 {
1459 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SEQUENCE_OF,
1460 .tag = 1,
1461 .offset = offsetof(CMS_ReceiptsFrom, d.receiptList),
1462 .field_name = "d.receiptList",
1463 .item = &GENERAL_NAMES_it,
1464 },
1465 };
1466
1467 static const ASN1_ITEM CMS_ReceiptsFrom_it = {
1468 .itype = ASN1_ITYPE_CHOICE,
1469 .utype = offsetof(CMS_ReceiptsFrom, type),
1470 .templates = CMS_ReceiptsFrom_ch_tt,
1471 .tcount = sizeof(CMS_ReceiptsFrom_ch_tt) / sizeof(ASN1_TEMPLATE),
1472 .funcs = NULL,
1473 .size = sizeof(CMS_ReceiptsFrom),
1474 .sname = "CMS_ReceiptsFrom",
1475 };
1476
1477 static const ASN1_TEMPLATE CMS_ReceiptRequest_seq_tt[] = {
1478 {
1479 .flags = 0,
1480 .tag = 0,
1481 .offset = offsetof(CMS_ReceiptRequest, signedContentIdentifier),
1482 .field_name = "signedContentIdentifier",
1483 .item = &ASN1_OCTET_STRING_it,
1484 },
1485 {
1486 .flags = 0,
1487 .tag = 0,
1488 .offset = offsetof(CMS_ReceiptRequest, receiptsFrom),
1489 .field_name = "receiptsFrom",
1490 .item = &CMS_ReceiptsFrom_it,
1491 },
1492 {
1493 .flags = ASN1_TFLG_SEQUENCE_OF,
1494 .tag = 0,
1495 .offset = offsetof(CMS_ReceiptRequest, receiptsTo),
1496 .field_name = "receiptsTo",
1497 .item = &GENERAL_NAMES_it,
1498 },
1499 };
1500
1501 const ASN1_ITEM CMS_ReceiptRequest_it = {
1502 .itype = ASN1_ITYPE_SEQUENCE,
1503 .utype = V_ASN1_SEQUENCE,
1504 .templates = CMS_ReceiptRequest_seq_tt,
1505 .tcount = sizeof(CMS_ReceiptRequest_seq_tt) / sizeof(ASN1_TEMPLATE),
1506 .funcs = NULL,
1507 .size = sizeof(CMS_ReceiptRequest),
1508 .sname = "CMS_ReceiptRequest",
1509 };
1510 LCRYPTO_ALIAS(CMS_ReceiptRequest_it);
1511
1512 static const ASN1_TEMPLATE CMS_Receipt_seq_tt[] = {
1513 {
1514 .flags = 0,
1515 .tag = 0,
1516 .offset = offsetof(CMS_Receipt, version),
1517 .field_name = "version",
1518 .item = &LONG_it,
1519 },
1520 {
1521 .flags = 0,
1522 .tag = 0,
1523 .offset = offsetof(CMS_Receipt, contentType),
1524 .field_name = "contentType",
1525 .item = &ASN1_OBJECT_it,
1526 },
1527 {
1528 .flags = 0,
1529 .tag = 0,
1530 .offset = offsetof(CMS_Receipt, signedContentIdentifier),
1531 .field_name = "signedContentIdentifier",
1532 .item = &ASN1_OCTET_STRING_it,
1533 },
1534 {
1535 .flags = 0,
1536 .tag = 0,
1537 .offset = offsetof(CMS_Receipt, originatorSignatureValue),
1538 .field_name = "originatorSignatureValue",
1539 .item = &ASN1_OCTET_STRING_it,
1540 },
1541 };
1542
1543 const ASN1_ITEM CMS_Receipt_it = {
1544 .itype = ASN1_ITYPE_SEQUENCE,
1545 .utype = V_ASN1_SEQUENCE,
1546 .templates = CMS_Receipt_seq_tt,
1547 .tcount = sizeof(CMS_Receipt_seq_tt) / sizeof(ASN1_TEMPLATE),
1548 .funcs = NULL,
1549 .size = sizeof(CMS_Receipt),
1550 .sname = "CMS_Receipt",
1551 };
1552
1553 /*
1554 * Utilities to encode the CMS_SharedInfo structure used during key
1555 * derivation.
1556 */
1557
1558 typedef struct {
1559 X509_ALGOR *keyInfo;
1560 ASN1_OCTET_STRING *entityUInfo;
1561 ASN1_OCTET_STRING *suppPubInfo;
1562 } CMS_SharedInfo;
1563
1564 static const ASN1_TEMPLATE CMS_SharedInfo_seq_tt[] = {
1565 {
1566 .flags = 0,
1567 .tag = 0,
1568 .offset = offsetof(CMS_SharedInfo, keyInfo),
1569 .field_name = "keyInfo",
1570 .item = &X509_ALGOR_it,
1571 },
1572 {
1573 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL,
1574 .tag = 0,
1575 .offset = offsetof(CMS_SharedInfo, entityUInfo),
1576 .field_name = "entityUInfo",
1577 .item = &ASN1_OCTET_STRING_it,
1578 },
1579 {
1580 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL,
1581 .tag = 2,
1582 .offset = offsetof(CMS_SharedInfo, suppPubInfo),
1583 .field_name = "suppPubInfo",
1584 .item = &ASN1_OCTET_STRING_it,
1585 },
1586 };
1587
1588 static const ASN1_ITEM CMS_SharedInfo_it = {
1589 .itype = ASN1_ITYPE_SEQUENCE,
1590 .utype = V_ASN1_SEQUENCE,
1591 .templates = CMS_SharedInfo_seq_tt,
1592 .tcount = sizeof(CMS_SharedInfo_seq_tt) / sizeof(ASN1_TEMPLATE),
1593 .funcs = NULL,
1594 .size = sizeof(CMS_SharedInfo),
1595 .sname = "CMS_SharedInfo",
1596 };
1597
1598 int
CMS_SharedInfo_encode(unsigned char ** pder,X509_ALGOR * kekalg,ASN1_OCTET_STRING * ukm,int keylen)1599 CMS_SharedInfo_encode(unsigned char **pder, X509_ALGOR *kekalg,
1600 ASN1_OCTET_STRING *ukm, int keylen)
1601 {
1602 union {
1603 CMS_SharedInfo *pecsi;
1604 ASN1_VALUE *a;
1605 } intsi = {
1606 NULL
1607 };
1608
1609 ASN1_OCTET_STRING oklen;
1610 unsigned char kl[4];
1611 CMS_SharedInfo ecsi;
1612
1613 keylen <<= 3;
1614 kl[0] = (keylen >> 24) & 0xff;
1615 kl[1] = (keylen >> 16) & 0xff;
1616 kl[2] = (keylen >> 8) & 0xff;
1617 kl[3] = keylen & 0xff;
1618 oklen.length = 4;
1619 oklen.data = kl;
1620 oklen.type = V_ASN1_OCTET_STRING;
1621 oklen.flags = 0;
1622 ecsi.keyInfo = kekalg;
1623 ecsi.entityUInfo = ukm;
1624 ecsi.suppPubInfo = &oklen;
1625 intsi.pecsi = &ecsi;
1626
1627 return ASN1_item_i2d(intsi.a, pder, &CMS_SharedInfo_it);
1628 }
1629 LCRYPTO_ALIAS(CMS_SharedInfo_encode);
1630