/src/openssl/crypto/pkcs7/pk7_asn1.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/asn1t.h> |
13 | | #include <openssl/pkcs7.h> |
14 | | #include <openssl/x509.h> |
15 | | #include "pk7_local.h" |
16 | | |
17 | | /* PKCS#7 ASN1 module */ |
18 | | |
19 | | /* This is the ANY DEFINED BY table for the top level PKCS#7 structure */ |
20 | | |
21 | | ASN1_ADB_TEMPLATE(p7default) = ASN1_EXP_OPT(PKCS7, d.other, ASN1_ANY, 0); |
22 | | |
23 | | ASN1_ADB(PKCS7) = { |
24 | | ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP_OPT(PKCS7, d.data, ASN1_OCTET_STRING_NDEF, 0)), |
25 | | ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP_OPT(PKCS7, d.sign, PKCS7_SIGNED, 0)), |
26 | | ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.enveloped, PKCS7_ENVELOPE, 0)), |
27 | | ADB_ENTRY(NID_pkcs7_signedAndEnveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.signed_and_enveloped, PKCS7_SIGN_ENVELOPE, 0)), |
28 | | ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP_OPT(PKCS7, d.digest, PKCS7_DIGEST, 0)), |
29 | | ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP_OPT(PKCS7, d.encrypted, PKCS7_ENCRYPT, 0)) |
30 | | } ASN1_ADB_END(PKCS7, 0, type, 0, &p7default_tt, NULL); |
31 | | |
32 | | /* PKCS#7 streaming support */ |
33 | | static int pk7_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
34 | | void *exarg) |
35 | 0 | { |
36 | 0 | ASN1_STREAM_ARG *sarg = exarg; |
37 | 0 | PKCS7 **pp7 = (PKCS7 **)pval; |
38 | |
|
39 | 0 | switch (operation) { |
40 | | |
41 | 0 | case ASN1_OP_STREAM_PRE: |
42 | 0 | if (PKCS7_stream(&sarg->boundary, *pp7) <= 0) |
43 | 0 | return 0; |
44 | | /* fall through */ |
45 | 0 | case ASN1_OP_DETACHED_PRE: |
46 | 0 | sarg->ndef_bio = PKCS7_dataInit(*pp7, sarg->out); |
47 | 0 | if (!sarg->ndef_bio) |
48 | 0 | return 0; |
49 | 0 | break; |
50 | | |
51 | 0 | case ASN1_OP_STREAM_POST: |
52 | 0 | case ASN1_OP_DETACHED_POST: |
53 | 0 | if (PKCS7_dataFinal(*pp7, sarg->ndef_bio) <= 0) |
54 | 0 | return 0; |
55 | 0 | break; |
56 | |
|
57 | 0 | } |
58 | 0 | return 1; |
59 | 0 | } |
60 | | |
61 | | ASN1_NDEF_SEQUENCE_cb(PKCS7, pk7_cb) = { |
62 | | ASN1_SIMPLE(PKCS7, type, ASN1_OBJECT), |
63 | | ASN1_ADB_OBJECT(PKCS7) |
64 | | }ASN1_NDEF_SEQUENCE_END_cb(PKCS7, PKCS7) |
65 | | |
66 | | PKCS7 *d2i_PKCS7(PKCS7 **a, const unsigned char **in, long len) |
67 | 0 | { |
68 | 0 | PKCS7 *ret; |
69 | 0 | OSSL_LIB_CTX *libctx = NULL; |
70 | 0 | const char *propq = NULL; |
71 | |
|
72 | 0 | if (a != NULL && *a != NULL) { |
73 | 0 | libctx = (*a)->ctx.libctx; |
74 | 0 | propq = (*a)->ctx.propq; |
75 | 0 | } |
76 | |
|
77 | 0 | ret = (PKCS7 *)ASN1_item_d2i_ex((ASN1_VALUE **)a, in, len, (PKCS7_it()), |
78 | 0 | libctx, propq); |
79 | 0 | if (ret != NULL) |
80 | 0 | ossl_pkcs7_resolve_libctx(ret); |
81 | 0 | return ret; |
82 | 0 | } |
83 | | |
84 | | int i2d_PKCS7(const PKCS7 *a, unsigned char **out) |
85 | 0 | { |
86 | 0 | return ASN1_item_i2d((const ASN1_VALUE *)a, out, (PKCS7_it()));\ |
87 | 0 | } |
88 | | |
89 | | PKCS7 *PKCS7_new(void) |
90 | 0 | { |
91 | 0 | return (PKCS7 *)ASN1_item_new(ASN1_ITEM_rptr(PKCS7)); |
92 | 0 | } |
93 | | |
94 | | PKCS7 *PKCS7_new_ex(OSSL_LIB_CTX *libctx, const char *propq) |
95 | 0 | { |
96 | 0 | PKCS7 *pkcs7 = (PKCS7 *)ASN1_item_new_ex(ASN1_ITEM_rptr(PKCS7), libctx, |
97 | 0 | propq); |
98 | |
|
99 | 0 | if (pkcs7 != NULL) { |
100 | 0 | pkcs7->ctx.libctx = libctx; |
101 | 0 | pkcs7->ctx.propq = NULL; |
102 | 0 | if (propq != NULL) { |
103 | 0 | pkcs7->ctx.propq = OPENSSL_strdup(propq); |
104 | 0 | if (pkcs7->ctx.propq == NULL) { |
105 | 0 | PKCS7_free(pkcs7); |
106 | 0 | pkcs7 = NULL; |
107 | 0 | } |
108 | 0 | } |
109 | 0 | } |
110 | 0 | return pkcs7; |
111 | 0 | } |
112 | | |
113 | | void PKCS7_free(PKCS7 *p7) |
114 | 0 | { |
115 | 0 | if (p7 != NULL) { |
116 | 0 | OPENSSL_free(p7->ctx.propq); |
117 | 0 | ASN1_item_free((ASN1_VALUE *)p7, ASN1_ITEM_rptr(PKCS7)); |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | | IMPLEMENT_ASN1_NDEF_FUNCTION(PKCS7) |
122 | | |
123 | | IMPLEMENT_ASN1_DUP_FUNCTION(PKCS7) |
124 | | |
125 | | ASN1_NDEF_SEQUENCE(PKCS7_SIGNED) = { |
126 | | ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER), |
127 | | ASN1_SET_OF(PKCS7_SIGNED, md_algs, X509_ALGOR), |
128 | | ASN1_SIMPLE(PKCS7_SIGNED, contents, PKCS7), |
129 | | ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNED, cert, X509, 0), |
130 | | ASN1_IMP_SET_OF_OPT(PKCS7_SIGNED, crl, X509_CRL, 1), |
131 | | ASN1_SET_OF(PKCS7_SIGNED, signer_info, PKCS7_SIGNER_INFO) |
132 | | } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGNED) |
133 | | |
134 | | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNED) |
135 | | |
136 | | /* Minor tweak to operation: free up EVP_PKEY */ |
137 | | static int si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
138 | | void *exarg) |
139 | 0 | { |
140 | 0 | if (operation == ASN1_OP_FREE_POST) { |
141 | 0 | PKCS7_SIGNER_INFO *si = (PKCS7_SIGNER_INFO *)*pval; |
142 | 0 | EVP_PKEY_free(si->pkey); |
143 | 0 | } |
144 | 0 | return 1; |
145 | 0 | } |
146 | | |
147 | | ASN1_SEQUENCE_cb(PKCS7_SIGNER_INFO, si_cb) = { |
148 | | ASN1_SIMPLE(PKCS7_SIGNER_INFO, version, ASN1_INTEGER), |
149 | | ASN1_SIMPLE(PKCS7_SIGNER_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL), |
150 | | ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_alg, X509_ALGOR), |
151 | | /* NB this should be a SET OF but we use a SEQUENCE OF so the |
152 | | * original order * is retained when the structure is reencoded. |
153 | | * Since the attributes are implicitly tagged this will not affect |
154 | | * the encoding. |
155 | | */ |
156 | | ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNER_INFO, auth_attr, X509_ATTRIBUTE, 0), |
157 | | ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_enc_alg, X509_ALGOR), |
158 | | ASN1_SIMPLE(PKCS7_SIGNER_INFO, enc_digest, ASN1_OCTET_STRING), |
159 | | ASN1_IMP_SET_OF_OPT(PKCS7_SIGNER_INFO, unauth_attr, X509_ATTRIBUTE, 1) |
160 | | } ASN1_SEQUENCE_END_cb(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO) |
161 | | |
162 | | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO) |
163 | | |
164 | | ASN1_SEQUENCE(PKCS7_ISSUER_AND_SERIAL) = { |
165 | | ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, issuer, X509_NAME), |
166 | | ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, serial, ASN1_INTEGER) |
167 | | } ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SERIAL) |
168 | | |
169 | | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL) |
170 | | |
171 | | ASN1_NDEF_SEQUENCE(PKCS7_ENVELOPE) = { |
172 | | ASN1_SIMPLE(PKCS7_ENVELOPE, version, ASN1_INTEGER), |
173 | | ASN1_SET_OF(PKCS7_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO), |
174 | | ASN1_SIMPLE(PKCS7_ENVELOPE, enc_data, PKCS7_ENC_CONTENT) |
175 | | } ASN1_NDEF_SEQUENCE_END(PKCS7_ENVELOPE) |
176 | | |
177 | | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENVELOPE) |
178 | | |
179 | | /* Minor tweak to operation: free up X509 */ |
180 | | static int ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
181 | | void *exarg) |
182 | 0 | { |
183 | 0 | if (operation == ASN1_OP_FREE_POST) { |
184 | 0 | PKCS7_RECIP_INFO *ri = (PKCS7_RECIP_INFO *)*pval; |
185 | 0 | X509_free(ri->cert); |
186 | 0 | } |
187 | 0 | return 1; |
188 | 0 | } |
189 | | |
190 | | ASN1_SEQUENCE_cb(PKCS7_RECIP_INFO, ri_cb) = { |
191 | | ASN1_SIMPLE(PKCS7_RECIP_INFO, version, ASN1_INTEGER), |
192 | | ASN1_SIMPLE(PKCS7_RECIP_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL), |
193 | | ASN1_SIMPLE(PKCS7_RECIP_INFO, key_enc_algor, X509_ALGOR), |
194 | | ASN1_SIMPLE(PKCS7_RECIP_INFO, enc_key, ASN1_OCTET_STRING) |
195 | | } ASN1_SEQUENCE_END_cb(PKCS7_RECIP_INFO, PKCS7_RECIP_INFO) |
196 | | |
197 | | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_RECIP_INFO) |
198 | | |
199 | | ASN1_NDEF_SEQUENCE(PKCS7_ENC_CONTENT) = { |
200 | | ASN1_SIMPLE(PKCS7_ENC_CONTENT, content_type, ASN1_OBJECT), |
201 | | ASN1_SIMPLE(PKCS7_ENC_CONTENT, algorithm, X509_ALGOR), |
202 | | ASN1_IMP_OPT(PKCS7_ENC_CONTENT, enc_data, ASN1_OCTET_STRING_NDEF, 0) |
203 | | } ASN1_NDEF_SEQUENCE_END(PKCS7_ENC_CONTENT) |
204 | | |
205 | | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT) |
206 | | |
207 | | ASN1_NDEF_SEQUENCE(PKCS7_SIGN_ENVELOPE) = { |
208 | | ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, version, ASN1_INTEGER), |
209 | | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO), |
210 | | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, md_algs, X509_ALGOR), |
211 | | ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, enc_data, PKCS7_ENC_CONTENT), |
212 | | ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, cert, X509, 0), |
213 | | ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, crl, X509_CRL, 1), |
214 | | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, signer_info, PKCS7_SIGNER_INFO) |
215 | | } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGN_ENVELOPE) |
216 | | |
217 | | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE) |
218 | | |
219 | | ASN1_NDEF_SEQUENCE(PKCS7_ENCRYPT) = { |
220 | | ASN1_SIMPLE(PKCS7_ENCRYPT, version, ASN1_INTEGER), |
221 | | ASN1_SIMPLE(PKCS7_ENCRYPT, enc_data, PKCS7_ENC_CONTENT) |
222 | | } ASN1_NDEF_SEQUENCE_END(PKCS7_ENCRYPT) |
223 | | |
224 | | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENCRYPT) |
225 | | |
226 | | ASN1_NDEF_SEQUENCE(PKCS7_DIGEST) = { |
227 | | ASN1_SIMPLE(PKCS7_DIGEST, version, ASN1_INTEGER), |
228 | | ASN1_SIMPLE(PKCS7_DIGEST, md, X509_ALGOR), |
229 | | ASN1_SIMPLE(PKCS7_DIGEST, contents, PKCS7), |
230 | | ASN1_SIMPLE(PKCS7_DIGEST, digest, ASN1_OCTET_STRING) |
231 | | } ASN1_NDEF_SEQUENCE_END(PKCS7_DIGEST) |
232 | | |
233 | | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_DIGEST) |
234 | | |
235 | | /* Specials for authenticated attributes */ |
236 | | |
237 | | /* |
238 | | * When signing attributes we want to reorder them to match the sorted |
239 | | * encoding. |
240 | | */ |
241 | | |
242 | | ASN1_ITEM_TEMPLATE(PKCS7_ATTR_SIGN) = |
243 | | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, PKCS7_ATTRIBUTES, X509_ATTRIBUTE) |
244 | | ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_SIGN) |
245 | | |
246 | | /* |
247 | | * When verifying attributes we need to use the received order. So we use |
248 | | * SEQUENCE OF and tag it to SET OF |
249 | | */ |
250 | | |
251 | | ASN1_ITEM_TEMPLATE(PKCS7_ATTR_VERIFY) = |
252 | | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL, |
253 | | V_ASN1_SET, PKCS7_ATTRIBUTES, X509_ATTRIBUTE) |
254 | | ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_VERIFY) |
255 | | |
256 | | IMPLEMENT_ASN1_PRINT_FUNCTION(PKCS7) |