1 /* $OpenBSD: pk7_asn1.c,v 1.12 2015/07/25 15:33:06 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/asn1t.h>
62 #include <openssl/pkcs7.h>
63 #include <openssl/x509.h>
65 /* PKCS#7 ASN1 module */
67 /* This is the ANY DEFINED BY table for the top level PKCS#7 structure */
69 static const ASN1_TEMPLATE p7default_tt
= {
70 .flags
= ASN1_TFLG_EXPLICIT
| ASN1_TFLG_OPTIONAL
,
72 .offset
= offsetof(PKCS7
, d
.other
),
73 .field_name
= "d.other",
77 static const ASN1_ADB_TABLE PKCS7_adbtbl
[] = {
79 .value
= NID_pkcs7_data
,
81 .flags
= ASN1_TFLG_EXPLICIT
| ASN1_TFLG_OPTIONAL
| ASN1_TFLG_NDEF
,
83 .offset
= offsetof(PKCS7
, d
.data
),
84 .field_name
= "d.data",
85 .item
= &ASN1_OCTET_STRING_NDEF_it
,
90 .value
= NID_pkcs7_signed
,
92 .flags
= ASN1_TFLG_EXPLICIT
| ASN1_TFLG_OPTIONAL
| ASN1_TFLG_NDEF
,
94 .offset
= offsetof(PKCS7
, d
.sign
),
95 .field_name
= "d.sign",
96 .item
= &PKCS7_SIGNED_it
,
101 .value
= NID_pkcs7_enveloped
,
103 .flags
= ASN1_TFLG_EXPLICIT
| ASN1_TFLG_OPTIONAL
| ASN1_TFLG_NDEF
,
105 .offset
= offsetof(PKCS7
, d
.enveloped
),
106 .field_name
= "d.enveloped",
107 .item
= &PKCS7_ENVELOPE_it
,
112 .value
= NID_pkcs7_signedAndEnveloped
,
114 .flags
= ASN1_TFLG_EXPLICIT
| ASN1_TFLG_OPTIONAL
| ASN1_TFLG_NDEF
,
116 .offset
= offsetof(PKCS7
, d
.signed_and_enveloped
),
117 .field_name
= "d.signed_and_enveloped",
118 .item
= &PKCS7_SIGN_ENVELOPE_it
,
123 .value
= NID_pkcs7_digest
,
125 .flags
= ASN1_TFLG_EXPLICIT
| ASN1_TFLG_OPTIONAL
| ASN1_TFLG_NDEF
,
127 .offset
= offsetof(PKCS7
, d
.digest
),
128 .field_name
= "d.digest",
129 .item
= &PKCS7_DIGEST_it
,
134 .value
= NID_pkcs7_encrypted
,
136 .flags
= ASN1_TFLG_EXPLICIT
| ASN1_TFLG_OPTIONAL
| ASN1_TFLG_NDEF
,
138 .offset
= offsetof(PKCS7
, d
.encrypted
),
139 .field_name
= "d.encrypted",
140 .item
= &PKCS7_ENCRYPT_it
,
146 static const ASN1_ADB PKCS7_adb
= {
148 .offset
= offsetof(PKCS7
, type
),
151 .tblcount
= sizeof(PKCS7_adbtbl
) / sizeof(ASN1_ADB_TABLE
),
152 .default_tt
= &p7default_tt
,
156 /* PKCS#7 streaming support */
158 pk7_cb(int operation
, ASN1_VALUE
**pval
, const ASN1_ITEM
*it
, void *exarg
)
160 ASN1_STREAM_ARG
*sarg
= exarg
;
161 PKCS7
**pp7
= (PKCS7
**)pval
;
164 case ASN1_OP_STREAM_PRE
:
165 if (PKCS7_stream(&sarg
->boundary
, *pp7
) <= 0)
168 case ASN1_OP_DETACHED_PRE
:
169 sarg
->ndef_bio
= PKCS7_dataInit(*pp7
, sarg
->out
);
174 case ASN1_OP_STREAM_POST
:
175 case ASN1_OP_DETACHED_POST
:
176 if (PKCS7_dataFinal(*pp7
, sarg
->ndef_bio
) <= 0)
183 static const ASN1_AUX PKCS7_aux
= {
191 static const ASN1_TEMPLATE PKCS7_seq_tt
[] = {
195 .offset
= offsetof(PKCS7
, type
),
196 .field_name
= "type",
197 .item
= &ASN1_OBJECT_it
,
200 .flags
= ASN1_TFLG_ADB_OID
,
203 .field_name
= "PKCS7",
204 .item
= (const ASN1_ITEM
*)&PKCS7_adb
,
208 const ASN1_ITEM PKCS7_it
= {
209 .itype
= ASN1_ITYPE_NDEF_SEQUENCE
,
210 .utype
= V_ASN1_SEQUENCE
,
211 .templates
= PKCS7_seq_tt
,
212 .tcount
= sizeof(PKCS7_seq_tt
) / sizeof(ASN1_TEMPLATE
),
214 .size
= sizeof(PKCS7
),
220 d2i_PKCS7(PKCS7
**a
, const unsigned char **in
, long len
)
222 return (PKCS7
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
227 i2d_PKCS7(PKCS7
*a
, unsigned char **out
)
229 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &PKCS7_it
);
235 return (PKCS7
*)ASN1_item_new(&PKCS7_it
);
241 ASN1_item_free((ASN1_VALUE
*)a
, &PKCS7_it
);
245 i2d_PKCS7_NDEF(PKCS7
*a
, unsigned char **out
)
247 return ASN1_item_ndef_i2d((ASN1_VALUE
*)a
, out
, &PKCS7_it
);
253 return ASN1_item_dup(&PKCS7_it
, x
);
256 static const ASN1_TEMPLATE PKCS7_SIGNED_seq_tt
[] = {
260 .offset
= offsetof(PKCS7_SIGNED
, version
),
261 .field_name
= "version",
262 .item
= &ASN1_INTEGER_it
,
265 .flags
= ASN1_TFLG_SET_OF
,
267 .offset
= offsetof(PKCS7_SIGNED
, md_algs
),
268 .field_name
= "md_algs",
269 .item
= &X509_ALGOR_it
,
274 .offset
= offsetof(PKCS7_SIGNED
, contents
),
275 .field_name
= "contents",
279 .flags
= ASN1_TFLG_IMPLICIT
| ASN1_TFLG_SEQUENCE_OF
| ASN1_TFLG_OPTIONAL
,
281 .offset
= offsetof(PKCS7_SIGNED
, cert
),
282 .field_name
= "cert",
286 .flags
= ASN1_TFLG_IMPLICIT
| ASN1_TFLG_SET_OF
| ASN1_TFLG_OPTIONAL
,
288 .offset
= offsetof(PKCS7_SIGNED
, crl
),
290 .item
= &X509_CRL_it
,
293 .flags
= ASN1_TFLG_SET_OF
,
295 .offset
= offsetof(PKCS7_SIGNED
, signer_info
),
296 .field_name
= "signer_info",
297 .item
= &PKCS7_SIGNER_INFO_it
,
301 const ASN1_ITEM PKCS7_SIGNED_it
= {
302 .itype
= ASN1_ITYPE_NDEF_SEQUENCE
,
303 .utype
= V_ASN1_SEQUENCE
,
304 .templates
= PKCS7_SIGNED_seq_tt
,
305 .tcount
= sizeof(PKCS7_SIGNED_seq_tt
) / sizeof(ASN1_TEMPLATE
),
307 .size
= sizeof(PKCS7_SIGNED
),
308 .sname
= "PKCS7_SIGNED",
313 d2i_PKCS7_SIGNED(PKCS7_SIGNED
**a
, const unsigned char **in
, long len
)
315 return (PKCS7_SIGNED
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
320 i2d_PKCS7_SIGNED(PKCS7_SIGNED
*a
, unsigned char **out
)
322 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &PKCS7_SIGNED_it
);
326 PKCS7_SIGNED_new(void)
328 return (PKCS7_SIGNED
*)ASN1_item_new(&PKCS7_SIGNED_it
);
332 PKCS7_SIGNED_free(PKCS7_SIGNED
*a
)
334 ASN1_item_free((ASN1_VALUE
*)a
, &PKCS7_SIGNED_it
);
337 /* Minor tweak to operation: free up EVP_PKEY */
339 si_cb(int operation
, ASN1_VALUE
**pval
, const ASN1_ITEM
*it
, void *exarg
)
341 if (operation
== ASN1_OP_FREE_POST
) {
342 PKCS7_SIGNER_INFO
*si
= (PKCS7_SIGNER_INFO
*)*pval
;
343 EVP_PKEY_free(si
->pkey
);
348 static const ASN1_AUX PKCS7_SIGNER_INFO_aux
= {
356 static const ASN1_TEMPLATE PKCS7_SIGNER_INFO_seq_tt
[] = {
360 .offset
= offsetof(PKCS7_SIGNER_INFO
, version
),
361 .field_name
= "version",
362 .item
= &ASN1_INTEGER_it
,
367 .offset
= offsetof(PKCS7_SIGNER_INFO
, issuer_and_serial
),
368 .field_name
= "issuer_and_serial",
369 .item
= &PKCS7_ISSUER_AND_SERIAL_it
,
374 .offset
= offsetof(PKCS7_SIGNER_INFO
, digest_alg
),
375 .field_name
= "digest_alg",
376 .item
= &X509_ALGOR_it
,
378 /* NB this should be a SET OF but we use a SEQUENCE OF so the
379 * original order * is retained when the structure is reencoded.
380 * Since the attributes are implicitly tagged this will not affect
384 .flags
= ASN1_TFLG_IMPLICIT
| ASN1_TFLG_SEQUENCE_OF
| ASN1_TFLG_OPTIONAL
,
386 .offset
= offsetof(PKCS7_SIGNER_INFO
, auth_attr
),
387 .field_name
= "auth_attr",
388 .item
= &X509_ATTRIBUTE_it
,
393 .offset
= offsetof(PKCS7_SIGNER_INFO
, digest_enc_alg
),
394 .field_name
= "digest_enc_alg",
395 .item
= &X509_ALGOR_it
,
400 .offset
= offsetof(PKCS7_SIGNER_INFO
, enc_digest
),
401 .field_name
= "enc_digest",
402 .item
= &ASN1_OCTET_STRING_it
,
405 .flags
= ASN1_TFLG_IMPLICIT
| ASN1_TFLG_SET_OF
| ASN1_TFLG_OPTIONAL
,
407 .offset
= offsetof(PKCS7_SIGNER_INFO
, unauth_attr
),
408 .field_name
= "unauth_attr",
409 .item
= &X509_ATTRIBUTE_it
,
413 const ASN1_ITEM PKCS7_SIGNER_INFO_it
= {
414 .itype
= ASN1_ITYPE_SEQUENCE
,
415 .utype
= V_ASN1_SEQUENCE
,
416 .templates
= PKCS7_SIGNER_INFO_seq_tt
,
417 .tcount
= sizeof(PKCS7_SIGNER_INFO_seq_tt
) / sizeof(ASN1_TEMPLATE
),
418 .funcs
= &PKCS7_SIGNER_INFO_aux
,
419 .size
= sizeof(PKCS7_SIGNER_INFO
),
420 .sname
= "PKCS7_SIGNER_INFO",
425 d2i_PKCS7_SIGNER_INFO(PKCS7_SIGNER_INFO
**a
, const unsigned char **in
, long len
)
427 return (PKCS7_SIGNER_INFO
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
428 &PKCS7_SIGNER_INFO_it
);
432 i2d_PKCS7_SIGNER_INFO(PKCS7_SIGNER_INFO
*a
, unsigned char **out
)
434 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &PKCS7_SIGNER_INFO_it
);
438 PKCS7_SIGNER_INFO_new(void)
440 return (PKCS7_SIGNER_INFO
*)ASN1_item_new(&PKCS7_SIGNER_INFO_it
);
444 PKCS7_SIGNER_INFO_free(PKCS7_SIGNER_INFO
*a
)
446 ASN1_item_free((ASN1_VALUE
*)a
, &PKCS7_SIGNER_INFO_it
);
449 static const ASN1_TEMPLATE PKCS7_ISSUER_AND_SERIAL_seq_tt
[] = {
453 .offset
= offsetof(PKCS7_ISSUER_AND_SERIAL
, issuer
),
454 .field_name
= "issuer",
455 .item
= &X509_NAME_it
,
460 .offset
= offsetof(PKCS7_ISSUER_AND_SERIAL
, serial
),
461 .field_name
= "serial",
462 .item
= &ASN1_INTEGER_it
,
466 const ASN1_ITEM PKCS7_ISSUER_AND_SERIAL_it
= {
467 .itype
= ASN1_ITYPE_SEQUENCE
,
468 .utype
= V_ASN1_SEQUENCE
,
469 .templates
= PKCS7_ISSUER_AND_SERIAL_seq_tt
,
470 .tcount
= sizeof(PKCS7_ISSUER_AND_SERIAL_seq_tt
) / sizeof(ASN1_TEMPLATE
),
472 .size
= sizeof(PKCS7_ISSUER_AND_SERIAL
),
473 .sname
= "PKCS7_ISSUER_AND_SERIAL",
477 PKCS7_ISSUER_AND_SERIAL
*
478 d2i_PKCS7_ISSUER_AND_SERIAL(PKCS7_ISSUER_AND_SERIAL
**a
, const unsigned char **in
, long len
)
480 return (PKCS7_ISSUER_AND_SERIAL
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
481 &PKCS7_ISSUER_AND_SERIAL_it
);
485 i2d_PKCS7_ISSUER_AND_SERIAL(PKCS7_ISSUER_AND_SERIAL
*a
, unsigned char **out
)
487 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &PKCS7_ISSUER_AND_SERIAL_it
);
490 PKCS7_ISSUER_AND_SERIAL
*
491 PKCS7_ISSUER_AND_SERIAL_new(void)
493 return (PKCS7_ISSUER_AND_SERIAL
*)ASN1_item_new(&PKCS7_ISSUER_AND_SERIAL_it
);
497 PKCS7_ISSUER_AND_SERIAL_free(PKCS7_ISSUER_AND_SERIAL
*a
)
499 ASN1_item_free((ASN1_VALUE
*)a
, &PKCS7_ISSUER_AND_SERIAL_it
);
502 static const ASN1_TEMPLATE PKCS7_ENVELOPE_seq_tt
[] = {
506 .offset
= offsetof(PKCS7_ENVELOPE
, version
),
507 .field_name
= "version",
508 .item
= &ASN1_INTEGER_it
,
511 .flags
= ASN1_TFLG_SET_OF
,
513 .offset
= offsetof(PKCS7_ENVELOPE
, recipientinfo
),
514 .field_name
= "recipientinfo",
515 .item
= &PKCS7_RECIP_INFO_it
,
520 .offset
= offsetof(PKCS7_ENVELOPE
, enc_data
),
521 .field_name
= "enc_data",
522 .item
= &PKCS7_ENC_CONTENT_it
,
526 const ASN1_ITEM PKCS7_ENVELOPE_it
= {
527 .itype
= ASN1_ITYPE_NDEF_SEQUENCE
,
528 .utype
= V_ASN1_SEQUENCE
,
529 .templates
= PKCS7_ENVELOPE_seq_tt
,
530 .tcount
= sizeof(PKCS7_ENVELOPE_seq_tt
) / sizeof(ASN1_TEMPLATE
),
532 .size
= sizeof(PKCS7_ENVELOPE
),
533 .sname
= "PKCS7_ENVELOPE",
538 d2i_PKCS7_ENVELOPE(PKCS7_ENVELOPE
**a
, const unsigned char **in
, long len
)
540 return (PKCS7_ENVELOPE
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
545 i2d_PKCS7_ENVELOPE(PKCS7_ENVELOPE
*a
, unsigned char **out
)
547 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &PKCS7_ENVELOPE_it
);
551 PKCS7_ENVELOPE_new(void)
553 return (PKCS7_ENVELOPE
*)ASN1_item_new(&PKCS7_ENVELOPE_it
);
557 PKCS7_ENVELOPE_free(PKCS7_ENVELOPE
*a
)
559 ASN1_item_free((ASN1_VALUE
*)a
, &PKCS7_ENVELOPE_it
);
562 /* Minor tweak to operation: free up X509 */
564 ri_cb(int operation
, ASN1_VALUE
**pval
, const ASN1_ITEM
*it
, void *exarg
)
566 if (operation
== ASN1_OP_FREE_POST
) {
567 PKCS7_RECIP_INFO
*ri
= (PKCS7_RECIP_INFO
*)*pval
;
573 static const ASN1_AUX PKCS7_RECIP_INFO_aux
= {
581 static const ASN1_TEMPLATE PKCS7_RECIP_INFO_seq_tt
[] = {
585 .offset
= offsetof(PKCS7_RECIP_INFO
, version
),
586 .field_name
= "version",
587 .item
= &ASN1_INTEGER_it
,
592 .offset
= offsetof(PKCS7_RECIP_INFO
, issuer_and_serial
),
593 .field_name
= "issuer_and_serial",
594 .item
= &PKCS7_ISSUER_AND_SERIAL_it
,
599 .offset
= offsetof(PKCS7_RECIP_INFO
, key_enc_algor
),
600 .field_name
= "key_enc_algor",
601 .item
= &X509_ALGOR_it
,
606 .offset
= offsetof(PKCS7_RECIP_INFO
, enc_key
),
607 .field_name
= "enc_key",
608 .item
= &ASN1_OCTET_STRING_it
,
612 const ASN1_ITEM PKCS7_RECIP_INFO_it
= {
613 .itype
= ASN1_ITYPE_SEQUENCE
,
614 .utype
= V_ASN1_SEQUENCE
,
615 .templates
= PKCS7_RECIP_INFO_seq_tt
,
616 .tcount
= sizeof(PKCS7_RECIP_INFO_seq_tt
) / sizeof(ASN1_TEMPLATE
),
617 .funcs
= &PKCS7_RECIP_INFO_aux
,
618 .size
= sizeof(PKCS7_RECIP_INFO
),
619 .sname
= "PKCS7_RECIP_INFO",
624 d2i_PKCS7_RECIP_INFO(PKCS7_RECIP_INFO
**a
, const unsigned char **in
, long len
)
626 return (PKCS7_RECIP_INFO
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
627 &PKCS7_RECIP_INFO_it
);
631 i2d_PKCS7_RECIP_INFO(PKCS7_RECIP_INFO
*a
, unsigned char **out
)
633 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &PKCS7_RECIP_INFO_it
);
637 PKCS7_RECIP_INFO_new(void)
639 return (PKCS7_RECIP_INFO
*)ASN1_item_new(&PKCS7_RECIP_INFO_it
);
643 PKCS7_RECIP_INFO_free(PKCS7_RECIP_INFO
*a
)
645 ASN1_item_free((ASN1_VALUE
*)a
, &PKCS7_RECIP_INFO_it
);
648 static const ASN1_TEMPLATE PKCS7_ENC_CONTENT_seq_tt
[] = {
652 .offset
= offsetof(PKCS7_ENC_CONTENT
, content_type
),
653 .field_name
= "content_type",
654 .item
= &ASN1_OBJECT_it
,
659 .offset
= offsetof(PKCS7_ENC_CONTENT
, algorithm
),
660 .field_name
= "algorithm",
661 .item
= &X509_ALGOR_it
,
664 .flags
= ASN1_TFLG_IMPLICIT
| ASN1_TFLG_OPTIONAL
,
666 .offset
= offsetof(PKCS7_ENC_CONTENT
, enc_data
),
667 .field_name
= "enc_data",
668 .item
= &ASN1_OCTET_STRING_NDEF_it
,
672 const ASN1_ITEM PKCS7_ENC_CONTENT_it
= {
673 .itype
= ASN1_ITYPE_NDEF_SEQUENCE
,
674 .utype
= V_ASN1_SEQUENCE
,
675 .templates
= PKCS7_ENC_CONTENT_seq_tt
,
676 .tcount
= sizeof(PKCS7_ENC_CONTENT_seq_tt
) / sizeof(ASN1_TEMPLATE
),
678 .size
= sizeof(PKCS7_ENC_CONTENT
),
679 .sname
= "PKCS7_ENC_CONTENT",
684 d2i_PKCS7_ENC_CONTENT(PKCS7_ENC_CONTENT
**a
, const unsigned char **in
, long len
)
686 return (PKCS7_ENC_CONTENT
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
687 &PKCS7_ENC_CONTENT_it
);
691 i2d_PKCS7_ENC_CONTENT(PKCS7_ENC_CONTENT
*a
, unsigned char **out
)
693 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &PKCS7_ENC_CONTENT_it
);
697 PKCS7_ENC_CONTENT_new(void)
699 return (PKCS7_ENC_CONTENT
*)ASN1_item_new(&PKCS7_ENC_CONTENT_it
);
703 PKCS7_ENC_CONTENT_free(PKCS7_ENC_CONTENT
*a
)
705 ASN1_item_free((ASN1_VALUE
*)a
, &PKCS7_ENC_CONTENT_it
);
708 static const ASN1_TEMPLATE PKCS7_SIGN_ENVELOPE_seq_tt
[] = {
712 .offset
= offsetof(PKCS7_SIGN_ENVELOPE
, version
),
713 .field_name
= "version",
714 .item
= &ASN1_INTEGER_it
,
717 .flags
= ASN1_TFLG_SET_OF
,
719 .offset
= offsetof(PKCS7_SIGN_ENVELOPE
, recipientinfo
),
720 .field_name
= "recipientinfo",
721 .item
= &PKCS7_RECIP_INFO_it
,
724 .flags
= ASN1_TFLG_SET_OF
,
726 .offset
= offsetof(PKCS7_SIGN_ENVELOPE
, md_algs
),
727 .field_name
= "md_algs",
728 .item
= &X509_ALGOR_it
,
733 .offset
= offsetof(PKCS7_SIGN_ENVELOPE
, enc_data
),
734 .field_name
= "enc_data",
735 .item
= &PKCS7_ENC_CONTENT_it
,
738 .flags
= ASN1_TFLG_IMPLICIT
| ASN1_TFLG_SET_OF
| ASN1_TFLG_OPTIONAL
,
740 .offset
= offsetof(PKCS7_SIGN_ENVELOPE
, cert
),
741 .field_name
= "cert",
745 .flags
= ASN1_TFLG_IMPLICIT
| ASN1_TFLG_SET_OF
| ASN1_TFLG_OPTIONAL
,
747 .offset
= offsetof(PKCS7_SIGN_ENVELOPE
, crl
),
749 .item
= &X509_CRL_it
,
752 .flags
= ASN1_TFLG_SET_OF
,
754 .offset
= offsetof(PKCS7_SIGN_ENVELOPE
, signer_info
),
755 .field_name
= "signer_info",
756 .item
= &PKCS7_SIGNER_INFO_it
,
760 const ASN1_ITEM PKCS7_SIGN_ENVELOPE_it
= {
761 .itype
= ASN1_ITYPE_NDEF_SEQUENCE
,
762 .utype
= V_ASN1_SEQUENCE
,
763 .templates
= PKCS7_SIGN_ENVELOPE_seq_tt
,
764 .tcount
= sizeof(PKCS7_SIGN_ENVELOPE_seq_tt
) / sizeof(ASN1_TEMPLATE
),
766 .size
= sizeof(PKCS7_SIGN_ENVELOPE
),
767 .sname
= "PKCS7_SIGN_ENVELOPE",
771 PKCS7_SIGN_ENVELOPE
*
772 d2i_PKCS7_SIGN_ENVELOPE(PKCS7_SIGN_ENVELOPE
**a
, const unsigned char **in
, long len
)
774 return (PKCS7_SIGN_ENVELOPE
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
775 &PKCS7_SIGN_ENVELOPE_it
);
779 i2d_PKCS7_SIGN_ENVELOPE(PKCS7_SIGN_ENVELOPE
*a
, unsigned char **out
)
781 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &PKCS7_SIGN_ENVELOPE_it
);
784 PKCS7_SIGN_ENVELOPE
*
785 PKCS7_SIGN_ENVELOPE_new(void)
787 return (PKCS7_SIGN_ENVELOPE
*)ASN1_item_new(&PKCS7_SIGN_ENVELOPE_it
);
791 PKCS7_SIGN_ENVELOPE_free(PKCS7_SIGN_ENVELOPE
*a
)
793 ASN1_item_free((ASN1_VALUE
*)a
, &PKCS7_SIGN_ENVELOPE_it
);
796 static const ASN1_TEMPLATE PKCS7_ENCRYPT_seq_tt
[] = {
800 .offset
= offsetof(PKCS7_ENCRYPT
, version
),
801 .field_name
= "version",
802 .item
= &ASN1_INTEGER_it
,
807 .offset
= offsetof(PKCS7_ENCRYPT
, enc_data
),
808 .field_name
= "enc_data",
809 .item
= &PKCS7_ENC_CONTENT_it
,
813 const ASN1_ITEM PKCS7_ENCRYPT_it
= {
814 .itype
= ASN1_ITYPE_NDEF_SEQUENCE
,
815 .utype
= V_ASN1_SEQUENCE
,
816 .templates
= PKCS7_ENCRYPT_seq_tt
,
817 .tcount
= sizeof(PKCS7_ENCRYPT_seq_tt
) / sizeof(ASN1_TEMPLATE
),
819 .size
= sizeof(PKCS7_ENCRYPT
),
820 .sname
= "PKCS7_ENCRYPT",
825 d2i_PKCS7_ENCRYPT(PKCS7_ENCRYPT
**a
, const unsigned char **in
, long len
)
827 return (PKCS7_ENCRYPT
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
832 i2d_PKCS7_ENCRYPT(PKCS7_ENCRYPT
*a
, unsigned char **out
)
834 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &PKCS7_ENCRYPT_it
);
838 PKCS7_ENCRYPT_new(void)
840 return (PKCS7_ENCRYPT
*)ASN1_item_new(&PKCS7_ENCRYPT_it
);
844 PKCS7_ENCRYPT_free(PKCS7_ENCRYPT
*a
)
846 ASN1_item_free((ASN1_VALUE
*)a
, &PKCS7_ENCRYPT_it
);
849 static const ASN1_TEMPLATE PKCS7_DIGEST_seq_tt
[] = {
853 .offset
= offsetof(PKCS7_DIGEST
, version
),
854 .field_name
= "version",
855 .item
= &ASN1_INTEGER_it
,
860 .offset
= offsetof(PKCS7_DIGEST
, md
),
862 .item
= &X509_ALGOR_it
,
867 .offset
= offsetof(PKCS7_DIGEST
, contents
),
868 .field_name
= "contents",
874 .offset
= offsetof(PKCS7_DIGEST
, digest
),
875 .field_name
= "digest",
876 .item
= &ASN1_OCTET_STRING_it
,
880 const ASN1_ITEM PKCS7_DIGEST_it
= {
881 .itype
= ASN1_ITYPE_NDEF_SEQUENCE
,
882 .utype
= V_ASN1_SEQUENCE
,
883 .templates
= PKCS7_DIGEST_seq_tt
,
884 .tcount
= sizeof(PKCS7_DIGEST_seq_tt
) / sizeof(ASN1_TEMPLATE
),
886 .size
= sizeof(PKCS7_DIGEST
),
887 .sname
= "PKCS7_DIGEST",
892 d2i_PKCS7_DIGEST(PKCS7_DIGEST
**a
, const unsigned char **in
, long len
)
894 return (PKCS7_DIGEST
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
899 i2d_PKCS7_DIGEST(PKCS7_DIGEST
*a
, unsigned char **out
)
901 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &PKCS7_DIGEST_it
);
905 PKCS7_DIGEST_new(void)
907 return (PKCS7_DIGEST
*)ASN1_item_new(&PKCS7_DIGEST_it
);
911 PKCS7_DIGEST_free(PKCS7_DIGEST
*a
)
913 ASN1_item_free((ASN1_VALUE
*)a
, &PKCS7_DIGEST_it
);
916 /* Specials for authenticated attributes */
918 /* When signing attributes we want to reorder them to match the sorted
922 static const ASN1_TEMPLATE PKCS7_ATTR_SIGN_item_tt
= {
923 .flags
= ASN1_TFLG_SET_ORDER
,
926 .field_name
= "PKCS7_ATTRIBUTES",
927 .item
= &X509_ATTRIBUTE_it
,
930 const ASN1_ITEM PKCS7_ATTR_SIGN_it
= {
931 .itype
= ASN1_ITYPE_PRIMITIVE
,
933 .templates
= &PKCS7_ATTR_SIGN_item_tt
,
937 .sname
= "PKCS7_ATTR_SIGN",
940 /* When verifying attributes we need to use the received order. So
941 * we use SEQUENCE OF and tag it to SET OF
944 static const ASN1_TEMPLATE PKCS7_ATTR_VERIFY_item_tt
= {
945 .flags
= ASN1_TFLG_SEQUENCE_OF
| ASN1_TFLG_IMPTAG
| ASN1_TFLG_UNIVERSAL
,
948 .field_name
= "PKCS7_ATTRIBUTES",
949 .item
= &X509_ATTRIBUTE_it
,
952 const ASN1_ITEM PKCS7_ATTR_VERIFY_it
= {
953 .itype
= ASN1_ITYPE_PRIMITIVE
,
955 .templates
= &PKCS7_ATTR_VERIFY_item_tt
,
959 .sname
= "PKCS7_ATTR_VERIFY",
964 PKCS7_print_ctx(BIO
*out
, PKCS7
*x
, int indent
, const ASN1_PCTX
*pctx
)
966 return ASN1_item_print(out
, (ASN1_VALUE
*)x
, indent
,