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 */
pk7_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)33 static int pk7_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
34 void *exarg)
35 {
36 ASN1_STREAM_ARG *sarg = exarg;
37 PKCS7 **pp7 = (PKCS7 **)pval;
38
39 switch (operation) {
40
41 case ASN1_OP_STREAM_PRE:
42 if (PKCS7_stream(&sarg->boundary, *pp7) <= 0)
43 return 0;
44 /* fall thru */
45 case ASN1_OP_DETACHED_PRE:
46 sarg->ndef_bio = PKCS7_dataInit(*pp7, sarg->out);
47 if (!sarg->ndef_bio)
48 return 0;
49 break;
50
51 case ASN1_OP_STREAM_POST:
52 case ASN1_OP_DETACHED_POST:
53 if (PKCS7_dataFinal(*pp7, sarg->ndef_bio) <= 0)
54 return 0;
55 break;
56
57 }
58 return 1;
59 }
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 {
68 PKCS7 *ret;
69 OSSL_LIB_CTX *libctx = NULL;
70 const char *propq = NULL;
71
72 if (a != NULL && *a != NULL) {
73 libctx = (*a)->ctx.libctx;
74 propq = (*a)->ctx.propq;
75 }
76
77 ret = (PKCS7 *)ASN1_item_d2i_ex((ASN1_VALUE **)a, in, len, (PKCS7_it()),
78 libctx, propq);
79 if (ret != NULL)
80 ossl_pkcs7_resolve_libctx(ret);
81 return ret;
82 }
83
i2d_PKCS7(const PKCS7 * a,unsigned char ** out)84 int i2d_PKCS7(const PKCS7 *a, unsigned char **out)
85 {
86 return ASN1_item_i2d((const ASN1_VALUE *)a, out, (PKCS7_it()));\
87 }
88
PKCS7_new(void)89 PKCS7 *PKCS7_new(void)
90 {
91 return (PKCS7 *)ASN1_item_new(ASN1_ITEM_rptr(PKCS7));
92 }
93
PKCS7_new_ex(OSSL_LIB_CTX * libctx,const char * propq)94 PKCS7 *PKCS7_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
95 {
96 PKCS7 *pkcs7 = (PKCS7 *)ASN1_item_new_ex(ASN1_ITEM_rptr(PKCS7), libctx,
97 propq);
98
99 if (pkcs7 != NULL) {
100 pkcs7->ctx.libctx = libctx;
101 pkcs7->ctx.propq = NULL;
102 if (propq != NULL) {
103 pkcs7->ctx.propq = OPENSSL_strdup(propq);
104 if (pkcs7->ctx.propq == NULL) {
105 PKCS7_free(pkcs7);
106 pkcs7 = NULL;
107 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
108 }
109 }
110 }
111 return pkcs7;
112 }
113
PKCS7_free(PKCS7 * p7)114 void PKCS7_free(PKCS7 *p7)
115 {
116 if (p7 != NULL) {
117 OPENSSL_free(p7->ctx.propq);
118 ASN1_item_free((ASN1_VALUE *)p7, ASN1_ITEM_rptr(PKCS7));
119 }
120 }
121
122 IMPLEMENT_ASN1_NDEF_FUNCTION(PKCS7)
123
124 IMPLEMENT_ASN1_DUP_FUNCTION(PKCS7)
125
126 ASN1_NDEF_SEQUENCE(PKCS7_SIGNED) = {
127 ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER),
128 ASN1_SET_OF(PKCS7_SIGNED, md_algs, X509_ALGOR),
129 ASN1_SIMPLE(PKCS7_SIGNED, contents, PKCS7),
130 ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNED, cert, X509, 0),
131 ASN1_IMP_SET_OF_OPT(PKCS7_SIGNED, crl, X509_CRL, 1),
132 ASN1_SET_OF(PKCS7_SIGNED, signer_info, PKCS7_SIGNER_INFO)
133 } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGNED)
134
135 IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNED)
136
137 /* Minor tweak to operation: free up EVP_PKEY */
138 static int si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
139 void *exarg)
140 {
141 if (operation == ASN1_OP_FREE_POST) {
142 PKCS7_SIGNER_INFO *si = (PKCS7_SIGNER_INFO *)*pval;
143 EVP_PKEY_free(si->pkey);
144 }
145 return 1;
146 }
147
148 ASN1_SEQUENCE_cb(PKCS7_SIGNER_INFO, si_cb) = {
149 ASN1_SIMPLE(PKCS7_SIGNER_INFO, version, ASN1_INTEGER),
150 ASN1_SIMPLE(PKCS7_SIGNER_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
151 ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_alg, X509_ALGOR),
152 /* NB this should be a SET OF but we use a SEQUENCE OF so the
153 * original order * is retained when the structure is reencoded.
154 * Since the attributes are implicitly tagged this will not affect
155 * the encoding.
156 */
157 ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNER_INFO, auth_attr, X509_ATTRIBUTE, 0),
158 ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_enc_alg, X509_ALGOR),
159 ASN1_SIMPLE(PKCS7_SIGNER_INFO, enc_digest, ASN1_OCTET_STRING),
160 ASN1_IMP_SET_OF_OPT(PKCS7_SIGNER_INFO, unauth_attr, X509_ATTRIBUTE, 1)
161 } ASN1_SEQUENCE_END_cb(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO)
162
163 IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO)
164
165 ASN1_SEQUENCE(PKCS7_ISSUER_AND_SERIAL) = {
166 ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, issuer, X509_NAME),
167 ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, serial, ASN1_INTEGER)
168 } ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SERIAL)
169
170 IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL)
171
172 ASN1_NDEF_SEQUENCE(PKCS7_ENVELOPE) = {
173 ASN1_SIMPLE(PKCS7_ENVELOPE, version, ASN1_INTEGER),
174 ASN1_SET_OF(PKCS7_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
175 ASN1_SIMPLE(PKCS7_ENVELOPE, enc_data, PKCS7_ENC_CONTENT)
176 } ASN1_NDEF_SEQUENCE_END(PKCS7_ENVELOPE)
177
178 IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENVELOPE)
179
180 /* Minor tweak to operation: free up X509 */
181 static int ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
182 void *exarg)
183 {
184 if (operation == ASN1_OP_FREE_POST) {
185 PKCS7_RECIP_INFO *ri = (PKCS7_RECIP_INFO *)*pval;
186 X509_free(ri->cert);
187 }
188 return 1;
189 }
190
191 ASN1_SEQUENCE_cb(PKCS7_RECIP_INFO, ri_cb) = {
192 ASN1_SIMPLE(PKCS7_RECIP_INFO, version, ASN1_INTEGER),
193 ASN1_SIMPLE(PKCS7_RECIP_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
194 ASN1_SIMPLE(PKCS7_RECIP_INFO, key_enc_algor, X509_ALGOR),
195 ASN1_SIMPLE(PKCS7_RECIP_INFO, enc_key, ASN1_OCTET_STRING)
196 } ASN1_SEQUENCE_END_cb(PKCS7_RECIP_INFO, PKCS7_RECIP_INFO)
197
198 IMPLEMENT_ASN1_FUNCTIONS(PKCS7_RECIP_INFO)
199
200 ASN1_NDEF_SEQUENCE(PKCS7_ENC_CONTENT) = {
201 ASN1_SIMPLE(PKCS7_ENC_CONTENT, content_type, ASN1_OBJECT),
202 ASN1_SIMPLE(PKCS7_ENC_CONTENT, algorithm, X509_ALGOR),
203 ASN1_IMP_OPT(PKCS7_ENC_CONTENT, enc_data, ASN1_OCTET_STRING_NDEF, 0)
204 } ASN1_NDEF_SEQUENCE_END(PKCS7_ENC_CONTENT)
205
206 IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT)
207
208 ASN1_NDEF_SEQUENCE(PKCS7_SIGN_ENVELOPE) = {
209 ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, version, ASN1_INTEGER),
210 ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
211 ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, md_algs, X509_ALGOR),
212 ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, enc_data, PKCS7_ENC_CONTENT),
213 ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, cert, X509, 0),
214 ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, crl, X509_CRL, 1),
215 ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, signer_info, PKCS7_SIGNER_INFO)
216 } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGN_ENVELOPE)
217
218 IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE)
219
220 ASN1_NDEF_SEQUENCE(PKCS7_ENCRYPT) = {
221 ASN1_SIMPLE(PKCS7_ENCRYPT, version, ASN1_INTEGER),
222 ASN1_SIMPLE(PKCS7_ENCRYPT, enc_data, PKCS7_ENC_CONTENT)
223 } ASN1_NDEF_SEQUENCE_END(PKCS7_ENCRYPT)
224
225 IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENCRYPT)
226
227 ASN1_NDEF_SEQUENCE(PKCS7_DIGEST) = {
228 ASN1_SIMPLE(PKCS7_DIGEST, version, ASN1_INTEGER),
229 ASN1_SIMPLE(PKCS7_DIGEST, md, X509_ALGOR),
230 ASN1_SIMPLE(PKCS7_DIGEST, contents, PKCS7),
231 ASN1_SIMPLE(PKCS7_DIGEST, digest, ASN1_OCTET_STRING)
232 } ASN1_NDEF_SEQUENCE_END(PKCS7_DIGEST)
233
234 IMPLEMENT_ASN1_FUNCTIONS(PKCS7_DIGEST)
235
236 /* Specials for authenticated attributes */
237
238 /*
239 * When signing attributes we want to reorder them to match the sorted
240 * encoding.
241 */
242
243 ASN1_ITEM_TEMPLATE(PKCS7_ATTR_SIGN) =
244 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
245 ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_SIGN)
246
247 /*
248 * When verifying attributes we need to use the received order. So we use
249 * SEQUENCE OF and tag it to SET OF
250 */
251
252 ASN1_ITEM_TEMPLATE(PKCS7_ATTR_VERIFY) =
253 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL,
254 V_ASN1_SET, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
255 ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_VERIFY)
256
257 IMPLEMENT_ASN1_PRINT_FUNCTION(PKCS7)
258