sl@0
|
1 |
/* pk7_asn.c */
|
sl@0
|
2 |
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
|
sl@0
|
3 |
* project 2000.
|
sl@0
|
4 |
*/
|
sl@0
|
5 |
/* ====================================================================
|
sl@0
|
6 |
* Copyright (c) 2000 The OpenSSL Project. All rights reserved.
|
sl@0
|
7 |
*
|
sl@0
|
8 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
9 |
* modification, are permitted provided that the following conditions
|
sl@0
|
10 |
* are met:
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* 1. Redistributions of source code must retain the above copyright
|
sl@0
|
13 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
14 |
*
|
sl@0
|
15 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
16 |
* notice, this list of conditions and the following disclaimer in
|
sl@0
|
17 |
* the documentation and/or other materials provided with the
|
sl@0
|
18 |
* distribution.
|
sl@0
|
19 |
*
|
sl@0
|
20 |
* 3. All advertising materials mentioning features or use of this
|
sl@0
|
21 |
* software must display the following acknowledgment:
|
sl@0
|
22 |
* "This product includes software developed by the OpenSSL Project
|
sl@0
|
23 |
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
sl@0
|
24 |
*
|
sl@0
|
25 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
sl@0
|
26 |
* endorse or promote products derived from this software without
|
sl@0
|
27 |
* prior written permission. For written permission, please contact
|
sl@0
|
28 |
* licensing@OpenSSL.org.
|
sl@0
|
29 |
*
|
sl@0
|
30 |
* 5. Products derived from this software may not be called "OpenSSL"
|
sl@0
|
31 |
* nor may "OpenSSL" appear in their names without prior written
|
sl@0
|
32 |
* permission of the OpenSSL Project.
|
sl@0
|
33 |
*
|
sl@0
|
34 |
* 6. Redistributions of any form whatsoever must retain the following
|
sl@0
|
35 |
* acknowledgment:
|
sl@0
|
36 |
* "This product includes software developed by the OpenSSL Project
|
sl@0
|
37 |
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
sl@0
|
38 |
*
|
sl@0
|
39 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
sl@0
|
40 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
41 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
sl@0
|
42 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
sl@0
|
43 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
sl@0
|
44 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
sl@0
|
45 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
sl@0
|
46 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
47 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
sl@0
|
48 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
sl@0
|
49 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
sl@0
|
50 |
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
sl@0
|
51 |
* ====================================================================
|
sl@0
|
52 |
*
|
sl@0
|
53 |
* This product includes cryptographic software written by Eric Young
|
sl@0
|
54 |
* (eay@cryptsoft.com). This product includes software written by Tim
|
sl@0
|
55 |
* Hudson (tjh@cryptsoft.com).
|
sl@0
|
56 |
*
|
sl@0
|
57 |
*/
|
sl@0
|
58 |
|
sl@0
|
59 |
#include <stdio.h>
|
sl@0
|
60 |
#include "cryptlib.h"
|
sl@0
|
61 |
#include <openssl/asn1t.h>
|
sl@0
|
62 |
#include <openssl/pkcs7.h>
|
sl@0
|
63 |
#include <openssl/x509.h>
|
sl@0
|
64 |
|
sl@0
|
65 |
/* PKCS#7 ASN1 module */
|
sl@0
|
66 |
|
sl@0
|
67 |
/* This is the ANY DEFINED BY table for the top level PKCS#7 structure */
|
sl@0
|
68 |
|
sl@0
|
69 |
ASN1_ADB_TEMPLATE(p7default) = ASN1_EXP_OPT(PKCS7, d.other, ASN1_ANY, 0);
|
sl@0
|
70 |
|
sl@0
|
71 |
ASN1_ADB(PKCS7) = {
|
sl@0
|
72 |
ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP_OPT(PKCS7, d.data, ASN1_OCTET_STRING_NDEF, 0)),
|
sl@0
|
73 |
ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP_OPT(PKCS7, d.sign, PKCS7_SIGNED, 0)),
|
sl@0
|
74 |
ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.enveloped, PKCS7_ENVELOPE, 0)),
|
sl@0
|
75 |
ADB_ENTRY(NID_pkcs7_signedAndEnveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.signed_and_enveloped, PKCS7_SIGN_ENVELOPE, 0)),
|
sl@0
|
76 |
ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP_OPT(PKCS7, d.digest, PKCS7_DIGEST, 0)),
|
sl@0
|
77 |
ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP_OPT(PKCS7, d.encrypted, PKCS7_ENCRYPT, 0))
|
sl@0
|
78 |
} ASN1_ADB_END(PKCS7, 0, type, 0, &p7default_tt, NULL);
|
sl@0
|
79 |
|
sl@0
|
80 |
ASN1_NDEF_SEQUENCE(PKCS7) = {
|
sl@0
|
81 |
ASN1_SIMPLE(PKCS7, type, ASN1_OBJECT),
|
sl@0
|
82 |
ASN1_ADB_OBJECT(PKCS7)
|
sl@0
|
83 |
}ASN1_NDEF_SEQUENCE_END(PKCS7)
|
sl@0
|
84 |
|
sl@0
|
85 |
IMPLEMENT_ASN1_FUNCTIONS(PKCS7)
|
sl@0
|
86 |
IMPLEMENT_ASN1_NDEF_FUNCTION(PKCS7)
|
sl@0
|
87 |
IMPLEMENT_ASN1_DUP_FUNCTION(PKCS7)
|
sl@0
|
88 |
|
sl@0
|
89 |
ASN1_NDEF_SEQUENCE(PKCS7_SIGNED) = {
|
sl@0
|
90 |
ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER),
|
sl@0
|
91 |
ASN1_SET_OF(PKCS7_SIGNED, md_algs, X509_ALGOR),
|
sl@0
|
92 |
ASN1_SIMPLE(PKCS7_SIGNED, contents, PKCS7),
|
sl@0
|
93 |
ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNED, cert, X509, 0),
|
sl@0
|
94 |
ASN1_IMP_SET_OF_OPT(PKCS7_SIGNED, crl, X509_CRL, 1),
|
sl@0
|
95 |
ASN1_SET_OF(PKCS7_SIGNED, signer_info, PKCS7_SIGNER_INFO)
|
sl@0
|
96 |
} ASN1_NDEF_SEQUENCE_END(PKCS7_SIGNED)
|
sl@0
|
97 |
|
sl@0
|
98 |
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNED)
|
sl@0
|
99 |
|
sl@0
|
100 |
/* Minor tweak to operation: free up EVP_PKEY */
|
sl@0
|
101 |
static int si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it)
|
sl@0
|
102 |
{
|
sl@0
|
103 |
if(operation == ASN1_OP_FREE_POST) {
|
sl@0
|
104 |
PKCS7_SIGNER_INFO *si = (PKCS7_SIGNER_INFO *)*pval;
|
sl@0
|
105 |
EVP_PKEY_free(si->pkey);
|
sl@0
|
106 |
}
|
sl@0
|
107 |
return 1;
|
sl@0
|
108 |
}
|
sl@0
|
109 |
|
sl@0
|
110 |
ASN1_SEQUENCE_cb(PKCS7_SIGNER_INFO, si_cb) = {
|
sl@0
|
111 |
ASN1_SIMPLE(PKCS7_SIGNER_INFO, version, ASN1_INTEGER),
|
sl@0
|
112 |
ASN1_SIMPLE(PKCS7_SIGNER_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
|
sl@0
|
113 |
ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_alg, X509_ALGOR),
|
sl@0
|
114 |
/* NB this should be a SET OF but we use a SEQUENCE OF so the
|
sl@0
|
115 |
* original order * is retained when the structure is reencoded.
|
sl@0
|
116 |
* Since the attributes are implicitly tagged this will not affect
|
sl@0
|
117 |
* the encoding.
|
sl@0
|
118 |
*/
|
sl@0
|
119 |
ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNER_INFO, auth_attr, X509_ATTRIBUTE, 0),
|
sl@0
|
120 |
ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_enc_alg, X509_ALGOR),
|
sl@0
|
121 |
ASN1_SIMPLE(PKCS7_SIGNER_INFO, enc_digest, ASN1_OCTET_STRING),
|
sl@0
|
122 |
ASN1_IMP_SET_OF_OPT(PKCS7_SIGNER_INFO, unauth_attr, X509_ATTRIBUTE, 1)
|
sl@0
|
123 |
} ASN1_SEQUENCE_END_cb(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO)
|
sl@0
|
124 |
|
sl@0
|
125 |
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO)
|
sl@0
|
126 |
|
sl@0
|
127 |
ASN1_SEQUENCE(PKCS7_ISSUER_AND_SERIAL) = {
|
sl@0
|
128 |
ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, issuer, X509_NAME),
|
sl@0
|
129 |
ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, serial, ASN1_INTEGER)
|
sl@0
|
130 |
} ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SERIAL)
|
sl@0
|
131 |
|
sl@0
|
132 |
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL)
|
sl@0
|
133 |
|
sl@0
|
134 |
ASN1_NDEF_SEQUENCE(PKCS7_ENVELOPE) = {
|
sl@0
|
135 |
ASN1_SIMPLE(PKCS7_ENVELOPE, version, ASN1_INTEGER),
|
sl@0
|
136 |
ASN1_SET_OF(PKCS7_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
|
sl@0
|
137 |
ASN1_SIMPLE(PKCS7_ENVELOPE, enc_data, PKCS7_ENC_CONTENT)
|
sl@0
|
138 |
} ASN1_NDEF_SEQUENCE_END(PKCS7_ENVELOPE)
|
sl@0
|
139 |
|
sl@0
|
140 |
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENVELOPE)
|
sl@0
|
141 |
|
sl@0
|
142 |
/* Minor tweak to operation: free up X509 */
|
sl@0
|
143 |
static int ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it)
|
sl@0
|
144 |
{
|
sl@0
|
145 |
if(operation == ASN1_OP_FREE_POST) {
|
sl@0
|
146 |
PKCS7_RECIP_INFO *ri = (PKCS7_RECIP_INFO *)*pval;
|
sl@0
|
147 |
X509_free(ri->cert);
|
sl@0
|
148 |
}
|
sl@0
|
149 |
return 1;
|
sl@0
|
150 |
}
|
sl@0
|
151 |
|
sl@0
|
152 |
ASN1_SEQUENCE_cb(PKCS7_RECIP_INFO, ri_cb) = {
|
sl@0
|
153 |
ASN1_SIMPLE(PKCS7_RECIP_INFO, version, ASN1_INTEGER),
|
sl@0
|
154 |
ASN1_SIMPLE(PKCS7_RECIP_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
|
sl@0
|
155 |
ASN1_SIMPLE(PKCS7_RECIP_INFO, key_enc_algor, X509_ALGOR),
|
sl@0
|
156 |
ASN1_SIMPLE(PKCS7_RECIP_INFO, enc_key, ASN1_OCTET_STRING)
|
sl@0
|
157 |
} ASN1_SEQUENCE_END_cb(PKCS7_RECIP_INFO, PKCS7_RECIP_INFO)
|
sl@0
|
158 |
|
sl@0
|
159 |
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_RECIP_INFO)
|
sl@0
|
160 |
|
sl@0
|
161 |
ASN1_NDEF_SEQUENCE(PKCS7_ENC_CONTENT) = {
|
sl@0
|
162 |
ASN1_SIMPLE(PKCS7_ENC_CONTENT, content_type, ASN1_OBJECT),
|
sl@0
|
163 |
ASN1_SIMPLE(PKCS7_ENC_CONTENT, algorithm, X509_ALGOR),
|
sl@0
|
164 |
ASN1_IMP_OPT(PKCS7_ENC_CONTENT, enc_data, ASN1_OCTET_STRING, 0)
|
sl@0
|
165 |
} ASN1_NDEF_SEQUENCE_END(PKCS7_ENC_CONTENT)
|
sl@0
|
166 |
|
sl@0
|
167 |
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT)
|
sl@0
|
168 |
|
sl@0
|
169 |
ASN1_NDEF_SEQUENCE(PKCS7_SIGN_ENVELOPE) = {
|
sl@0
|
170 |
ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, version, ASN1_INTEGER),
|
sl@0
|
171 |
ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
|
sl@0
|
172 |
ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, md_algs, X509_ALGOR),
|
sl@0
|
173 |
ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, enc_data, PKCS7_ENC_CONTENT),
|
sl@0
|
174 |
ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, cert, X509, 0),
|
sl@0
|
175 |
ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, crl, X509_CRL, 1),
|
sl@0
|
176 |
ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, signer_info, PKCS7_SIGNER_INFO)
|
sl@0
|
177 |
} ASN1_NDEF_SEQUENCE_END(PKCS7_SIGN_ENVELOPE)
|
sl@0
|
178 |
|
sl@0
|
179 |
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE)
|
sl@0
|
180 |
|
sl@0
|
181 |
ASN1_NDEF_SEQUENCE(PKCS7_ENCRYPT) = {
|
sl@0
|
182 |
ASN1_SIMPLE(PKCS7_ENCRYPT, version, ASN1_INTEGER),
|
sl@0
|
183 |
ASN1_SIMPLE(PKCS7_ENCRYPT, enc_data, PKCS7_ENC_CONTENT)
|
sl@0
|
184 |
} ASN1_NDEF_SEQUENCE_END(PKCS7_ENCRYPT)
|
sl@0
|
185 |
|
sl@0
|
186 |
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENCRYPT)
|
sl@0
|
187 |
|
sl@0
|
188 |
ASN1_NDEF_SEQUENCE(PKCS7_DIGEST) = {
|
sl@0
|
189 |
ASN1_SIMPLE(PKCS7_DIGEST, version, ASN1_INTEGER),
|
sl@0
|
190 |
ASN1_SIMPLE(PKCS7_DIGEST, md, X509_ALGOR),
|
sl@0
|
191 |
ASN1_SIMPLE(PKCS7_DIGEST, contents, PKCS7),
|
sl@0
|
192 |
ASN1_SIMPLE(PKCS7_DIGEST, digest, ASN1_OCTET_STRING)
|
sl@0
|
193 |
} ASN1_NDEF_SEQUENCE_END(PKCS7_DIGEST)
|
sl@0
|
194 |
|
sl@0
|
195 |
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_DIGEST)
|
sl@0
|
196 |
|
sl@0
|
197 |
/* Specials for authenticated attributes */
|
sl@0
|
198 |
|
sl@0
|
199 |
/* When signing attributes we want to reorder them to match the sorted
|
sl@0
|
200 |
* encoding.
|
sl@0
|
201 |
*/
|
sl@0
|
202 |
|
sl@0
|
203 |
ASN1_ITEM_TEMPLATE(PKCS7_ATTR_SIGN) =
|
sl@0
|
204 |
ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
|
sl@0
|
205 |
ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_SIGN)
|
sl@0
|
206 |
|
sl@0
|
207 |
/* When verifying attributes we need to use the received order. So
|
sl@0
|
208 |
* we use SEQUENCE OF and tag it to SET OF
|
sl@0
|
209 |
*/
|
sl@0
|
210 |
|
sl@0
|
211 |
ASN1_ITEM_TEMPLATE(PKCS7_ATTR_VERIFY) =
|
sl@0
|
212 |
ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL,
|
sl@0
|
213 |
V_ASN1_SET, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
|
sl@0
|
214 |
ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_VERIFY)
|