pk7_asn1.c revision 296341
1250199Sgrehan/* pk7_asn.c */
2322612Ssephe/*
3250199Sgrehan * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4250199Sgrehan * 2000.
5250199Sgrehan */
6250199Sgrehan/* ====================================================================
7250199Sgrehan * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
8250199Sgrehan *
9250199Sgrehan * Redistribution and use in source and binary forms, with or without
10250199Sgrehan * modification, are permitted provided that the following conditions
11250199Sgrehan * are met:
12250199Sgrehan *
13250199Sgrehan * 1. Redistributions of source code must retain the above copyright
14250199Sgrehan *    notice, this list of conditions and the following disclaimer.
15250199Sgrehan *
16250199Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
17250199Sgrehan *    notice, this list of conditions and the following disclaimer in
18250199Sgrehan *    the documentation and/or other materials provided with the
19250199Sgrehan *    distribution.
20250199Sgrehan *
21250199Sgrehan * 3. All advertising materials mentioning features or use of this
22250199Sgrehan *    software must display the following acknowledgment:
23250199Sgrehan *    "This product includes software developed by the OpenSSL Project
24250199Sgrehan *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25250199Sgrehan *
26250199Sgrehan * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27250199Sgrehan *    endorse or promote products derived from this software without
28250199Sgrehan *    prior written permission. For written permission, please contact
29264177Simp *    licensing@OpenSSL.org.
30264177Simp *
31264177Simp * 5. Products derived from this software may not be called "OpenSSL"
32308626Ssephe *    nor may "OpenSSL" appear in their names without prior written
33308626Ssephe *    permission of the OpenSSL Project.
34308626Ssephe *
35250199Sgrehan * 6. Redistributions of any form whatsoever must retain the following
36250199Sgrehan *    acknowledgment:
37308626Ssephe *    "This product includes software developed by the OpenSSL Project
38308624Ssephe *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39308624Ssephe *
40308624Ssephe * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41308624Ssephe * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42308624Ssephe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43307461Ssephe * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44307461Ssephe * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45308624Ssephe * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46307500Ssephe * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47308624Ssephe * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48307613Ssephe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49307613Ssephe * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50308624Ssephe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51250199Sgrehan * OF THE POSSIBILITY OF SUCH DAMAGE.
52250199Sgrehan * ====================================================================
53308624Ssephe *
54308624Ssephe * This product includes cryptographic software written by Eric Young
55307475Ssephe * (eay@cryptsoft.com).  This product includes software written by Tim
56308624Ssephe * Hudson (tjh@cryptsoft.com).
57308624Ssephe *
58308624Ssephe */
59308624Ssephe
60308625Ssephe#include <stdio.h>
61308627Ssephe#include "cryptlib.h"
62250199Sgrehan#include <openssl/asn1t.h>
63307484Ssephe#include <openssl/pkcs7.h>
64307484Ssephe#include <openssl/x509.h>
65307484Ssephe
66307484Ssephe/* PKCS#7 ASN1 module */
67307484Ssephe
68307613Ssephe/* This is the ANY DEFINED BY table for the top level PKCS#7 structure */
69307613Ssephe
70307613SsepheASN1_ADB_TEMPLATE(p7default) = ASN1_EXP_OPT(PKCS7, d.other, ASN1_ANY, 0);
71307613Ssephe
72307613SsepheASN1_ADB(PKCS7) = {
73307613Ssephe        ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP_OPT(PKCS7, d.data, ASN1_OCTET_STRING_NDEF, 0)),
74307613Ssephe        ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP_OPT(PKCS7, d.sign, PKCS7_SIGNED, 0)),
75307613Ssephe        ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.enveloped, PKCS7_ENVELOPE, 0)),
76307613Ssephe        ADB_ENTRY(NID_pkcs7_signedAndEnveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.signed_and_enveloped, PKCS7_SIGN_ENVELOPE, 0)),
77307613Ssephe        ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP_OPT(PKCS7, d.digest, PKCS7_DIGEST, 0)),
78307613Ssephe        ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP_OPT(PKCS7, d.encrypted, PKCS7_ENCRYPT, 0))
79307613Ssephe} ASN1_ADB_END(PKCS7, 0, type, 0, &p7default_tt, NULL);
80308626Ssephe
81308626Ssephe/* PKCS#7 streaming support */
82308626Ssephestatic int pk7_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
83308626Ssephe                  void *exarg)
84308626Ssephe{
85308626Ssephe    ASN1_STREAM_ARG *sarg = exarg;
86308626Ssephe    PKCS7 **pp7 = (PKCS7 **)pval;
87308626Ssephe
88308626Ssephe    switch (operation) {
89308626Ssephe
90308626Ssephe    case ASN1_OP_STREAM_PRE:
91308626Ssephe        if (PKCS7_stream(&sarg->boundary, *pp7) <= 0)
92308626Ssephe            return 0;
93308626Ssephe    case ASN1_OP_DETACHED_PRE:
94308626Ssephe        sarg->ndef_bio = PKCS7_dataInit(*pp7, sarg->out);
95308626Ssephe        if (!sarg->ndef_bio)
96308626Ssephe            return 0;
97307473Ssephe        break;
98307484Ssephe
99307484Ssephe    case ASN1_OP_STREAM_POST:
100307484Ssephe    case ASN1_OP_DETACHED_POST:
101307484Ssephe        if (PKCS7_dataFinal(*pp7, sarg->ndef_bio) <= 0)
102307484Ssephe            return 0;
103307484Ssephe        break;
104307484Ssephe
105307484Ssephe    }
106307484Ssephe    return 1;
107307484Ssephe}
108307484Ssephe
109307484SsepheASN1_NDEF_SEQUENCE_cb(PKCS7, pk7_cb) = {
110307484Ssephe        ASN1_SIMPLE(PKCS7, type, ASN1_OBJECT),
111307484Ssephe        ASN1_ADB_OBJECT(PKCS7)
112307498Ssephe}ASN1_NDEF_SEQUENCE_END_cb(PKCS7, PKCS7)
113308626Ssephe
114250199SgrehanIMPLEMENT_ASN1_FUNCTIONS(PKCS7)
115307484Ssephe
116307498SsepheIMPLEMENT_ASN1_NDEF_FUNCTION(PKCS7)
117250199Sgrehan
118308626SsepheIMPLEMENT_ASN1_DUP_FUNCTION(PKCS7)
119307498Ssephe
120307498SsepheASN1_NDEF_SEQUENCE(PKCS7_SIGNED) = {
121307498Ssephe        ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER),
122250199Sgrehan        ASN1_SET_OF(PKCS7_SIGNED, md_algs, X509_ALGOR),
123250199Sgrehan        ASN1_SIMPLE(PKCS7_SIGNED, contents, PKCS7),
124250199Sgrehan        ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNED, cert, X509, 0),
125307498Ssephe        ASN1_IMP_SET_OF_OPT(PKCS7_SIGNED, crl, X509_CRL, 1),
126307498Ssephe        ASN1_SET_OF(PKCS7_SIGNED, signer_info, PKCS7_SIGNER_INFO)
127308626Ssephe} ASN1_NDEF_SEQUENCE_END(PKCS7_SIGNED)
128307498Ssephe
129307498SsepheIMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNED)
130307484Ssephe
131307498Ssephe/* Minor tweak to operation: free up EVP_PKEY */
132307492Ssephestatic int si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
133307498Ssephe                 void *exarg)
134307492Ssephe{
135250199Sgrehan    if (operation == ASN1_OP_FREE_POST) {
136307482Ssephe        PKCS7_SIGNER_INFO *si = (PKCS7_SIGNER_INFO *)*pval;
137307482Ssephe        EVP_PKEY_free(si->pkey);
138307482Ssephe    }
139307482Ssephe    return 1;
140307482Ssephe}
141307482Ssephe
142307482SsepheASN1_SEQUENCE_cb(PKCS7_SIGNER_INFO, si_cb) = {
143307482Ssephe        ASN1_SIMPLE(PKCS7_SIGNER_INFO, version, ASN1_INTEGER),
144307482Ssephe        ASN1_SIMPLE(PKCS7_SIGNER_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
145308626Ssephe        ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_alg, X509_ALGOR),
146307482Ssephe        /* NB this should be a SET OF but we use a SEQUENCE OF so the
147307482Ssephe         * original order * is retained when the structure is reencoded.
148250199Sgrehan         * Since the attributes are implicitly tagged this will not affect
149308626Ssephe         * the encoding.
150307498Ssephe         */
151250199Sgrehan        ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNER_INFO, auth_attr, X509_ATTRIBUTE, 0),
152250199Sgrehan        ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_enc_alg, X509_ALGOR),
153250199Sgrehan        ASN1_SIMPLE(PKCS7_SIGNER_INFO, enc_digest, ASN1_OCTET_STRING),
154250199Sgrehan        ASN1_IMP_SET_OF_OPT(PKCS7_SIGNER_INFO, unauth_attr, X509_ATTRIBUTE, 1)
155307508Ssephe} ASN1_SEQUENCE_END_cb(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO)
156307508Ssephe
157250199SgrehanIMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO)
158307492Ssephe
159307487SsepheASN1_SEQUENCE(PKCS7_ISSUER_AND_SERIAL) = {
160250199Sgrehan        ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, issuer, X509_NAME),
161307492Ssephe        ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, serial, ASN1_INTEGER)
162307487Ssephe} ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SERIAL)
163307492Ssephe
164307487SsepheIMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL)
165307490Ssephe
166307492SsepheASN1_NDEF_SEQUENCE(PKCS7_ENVELOPE) = {
167307492Ssephe        ASN1_SIMPLE(PKCS7_ENVELOPE, version, ASN1_INTEGER),
168307490Ssephe        ASN1_SET_OF(PKCS7_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
169307487Ssephe        ASN1_SIMPLE(PKCS7_ENVELOPE, enc_data, PKCS7_ENC_CONTENT)
170307490Ssephe} ASN1_NDEF_SEQUENCE_END(PKCS7_ENVELOPE)
171250199Sgrehan
172250199SgrehanIMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENVELOPE)
173307508Ssephe
174307508Ssephe/* Minor tweak to operation: free up X509 */
175250199Sgrehanstatic int ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
176307490Ssephe                 void *exarg)
177307490Ssephe{
178250199Sgrehan    if (operation == ASN1_OP_FREE_POST) {
179307492Ssephe        PKCS7_RECIP_INFO *ri = (PKCS7_RECIP_INFO *)*pval;
180307490Ssephe        X509_free(ri->cert);
181307492Ssephe    }
182307490Ssephe    return 1;
183307490Ssephe}
184307490Ssephe
185307490SsepheASN1_SEQUENCE_cb(PKCS7_RECIP_INFO, ri_cb) = {
186307490Ssephe        ASN1_SIMPLE(PKCS7_RECIP_INFO, version, ASN1_INTEGER),
187307490Ssephe        ASN1_SIMPLE(PKCS7_RECIP_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
188307490Ssephe        ASN1_SIMPLE(PKCS7_RECIP_INFO, key_enc_algor, X509_ALGOR),
189250199Sgrehan        ASN1_SIMPLE(PKCS7_RECIP_INFO, enc_key, ASN1_OCTET_STRING)
190250199Sgrehan} ASN1_SEQUENCE_END_cb(PKCS7_RECIP_INFO, PKCS7_RECIP_INFO)
191324477Ssephe
192324477SsepheIMPLEMENT_ASN1_FUNCTIONS(PKCS7_RECIP_INFO)
193324477Ssephe
194324477SsepheASN1_NDEF_SEQUENCE(PKCS7_ENC_CONTENT) = {
195324477Ssephe        ASN1_SIMPLE(PKCS7_ENC_CONTENT, content_type, ASN1_OBJECT),
196324477Ssephe        ASN1_SIMPLE(PKCS7_ENC_CONTENT, algorithm, X509_ALGOR),
197324477Ssephe        ASN1_IMP_OPT(PKCS7_ENC_CONTENT, enc_data, ASN1_OCTET_STRING_NDEF, 0)
198324477Ssephe} ASN1_NDEF_SEQUENCE_END(PKCS7_ENC_CONTENT)
199324477Ssephe
200324477SsepheIMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT)
201324477Ssephe
202324477SsepheASN1_NDEF_SEQUENCE(PKCS7_SIGN_ENVELOPE) = {
203324477Ssephe        ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, version, ASN1_INTEGER),
204324477Ssephe        ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
205324477Ssephe        ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, md_algs, X509_ALGOR),
206324477Ssephe        ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, enc_data, PKCS7_ENC_CONTENT),
207324477Ssephe        ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, cert, X509, 0),
208324477Ssephe        ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, crl, X509_CRL, 1),
209307484Ssephe        ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, signer_info, PKCS7_SIGNER_INFO)
210307491Ssephe} ASN1_NDEF_SEQUENCE_END(PKCS7_SIGN_ENVELOPE)
211308624Ssephe
212250199SgrehanIMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE)
213307484Ssephe
214307491SsepheASN1_NDEF_SEQUENCE(PKCS7_ENCRYPT) = {
215307484Ssephe        ASN1_SIMPLE(PKCS7_ENCRYPT, version, ASN1_INTEGER),
216250199Sgrehan        ASN1_SIMPLE(PKCS7_ENCRYPT, enc_data, PKCS7_ENC_CONTENT)
217307484Ssephe} ASN1_NDEF_SEQUENCE_END(PKCS7_ENCRYPT)
218307484Ssephe
219307484SsepheIMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENCRYPT)
220307484Ssephe
221307484SsepheASN1_NDEF_SEQUENCE(PKCS7_DIGEST) = {
222307484Ssephe        ASN1_SIMPLE(PKCS7_DIGEST, version, ASN1_INTEGER),
223307484Ssephe        ASN1_SIMPLE(PKCS7_DIGEST, md, X509_ALGOR),
224307484Ssephe        ASN1_SIMPLE(PKCS7_DIGEST, contents, PKCS7),
225307484Ssephe        ASN1_SIMPLE(PKCS7_DIGEST, digest, ASN1_OCTET_STRING)
226307484Ssephe} ASN1_NDEF_SEQUENCE_END(PKCS7_DIGEST)
227307484Ssephe
228307484SsepheIMPLEMENT_ASN1_FUNCTIONS(PKCS7_DIGEST)
229307484Ssephe
230307484Ssephe/* Specials for authenticated attributes */
231307484Ssephe
232307484Ssephe/*
233307484Ssephe * When signing attributes we want to reorder them to match the sorted
234307484Ssephe * encoding.
235307484Ssephe */
236307484Ssephe
237307484SsepheASN1_ITEM_TEMPLATE(PKCS7_ATTR_SIGN) =
238307484Ssephe        ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
239250199SgrehanASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_SIGN)
240307484Ssephe
241250199Sgrehan/*
242250199Sgrehan * When verifying attributes we need to use the received order. So we use
243307484Ssephe * SEQUENCE OF and tag it to SET OF
244307484Ssephe */
245250199Sgrehan
246307484SsepheASN1_ITEM_TEMPLATE(PKCS7_ATTR_VERIFY) =
247308519Ssephe        ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL,
248307484Ssephe                                V_ASN1_SET, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
249307484SsepheASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_VERIFY)
250307484Ssephe
251307484SsepheIMPLEMENT_ASN1_PRINT_FUNCTION(PKCS7)
252307484Ssephe