/src/openssl41/crypto/pkcs7/pk7_doit.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2026 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 <openssl/rand.h> |
12 | | #include <openssl/objects.h> |
13 | | #include <openssl/x509.h> |
14 | | #include <openssl/x509v3.h> |
15 | | #include <openssl/err.h> |
16 | | #include "internal/cryptlib.h" |
17 | | #include "internal/sizes.h" |
18 | | #include "crypto/evp.h" |
19 | | #include "pk7_local.h" |
20 | | |
21 | | #include <crypto/asn1.h> |
22 | | |
23 | | static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype, |
24 | | void *value); |
25 | | static const ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid); |
26 | | |
27 | | int PKCS7_type_is_other(PKCS7 *p7) |
28 | 2.71k | { |
29 | 2.71k | int isOther = 1; |
30 | | |
31 | 2.71k | int nid = OBJ_obj2nid(p7->type); |
32 | | |
33 | 2.71k | switch (nid) { |
34 | 799 | case NID_pkcs7_data: |
35 | 801 | case NID_pkcs7_signed: |
36 | 811 | case NID_pkcs7_enveloped: |
37 | 939 | case NID_pkcs7_signedAndEnveloped: |
38 | 963 | case NID_pkcs7_digest: |
39 | 963 | case NID_pkcs7_encrypted: |
40 | 963 | isOther = 0; |
41 | 963 | break; |
42 | 1.75k | default: |
43 | 1.75k | isOther = 1; |
44 | 2.71k | } |
45 | | |
46 | 2.71k | return isOther; |
47 | 2.71k | } |
48 | | |
49 | | ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7) |
50 | 2.36k | { |
51 | 2.36k | if (PKCS7_type_is_data(p7)) |
52 | 1.39k | return p7->d.data; |
53 | 969 | if (PKCS7_type_is_other(p7) && p7->d.other |
54 | 46 | && (p7->d.other->type == V_ASN1_OCTET_STRING)) |
55 | 21 | return p7->d.other->value.octet_string; |
56 | 948 | return NULL; |
57 | 969 | } |
58 | | |
59 | | static ASN1_OCTET_STRING *pkcs7_get1_data(PKCS7 *p7, int streaming) |
60 | 2.36k | { |
61 | 2.36k | ASN1_OCTET_STRING *os = PKCS7_get_octet_string(p7); |
62 | | |
63 | 2.36k | if (os != NULL) { |
64 | | /* Edge case for MIME content, see RFC 5652 section-5.2.1 */ |
65 | 620 | ASN1_OCTET_STRING *osdup = ASN1_OCTET_STRING_dup(os); |
66 | | |
67 | 620 | if (osdup != NULL && streaming) |
68 | 0 | ASN1_STRING_set0(osdup, NULL, 0); |
69 | 620 | return osdup; |
70 | 620 | } |
71 | | |
72 | | /* General case for PKCS#7 content, see RFC 2315 section-7 */ |
73 | 1.74k | if (PKCS7_type_is_other(p7) && (p7->d.other != NULL) |
74 | 25 | && (p7->d.other->type == V_ASN1_SEQUENCE) |
75 | 17 | && (p7->d.other->value.sequence != NULL) |
76 | 17 | && (ASN1_STRING_get_length(p7->d.other->value.sequence) > 0)) { |
77 | 17 | const unsigned char *data = ASN1_STRING_get0_data(p7->d.other->value.sequence); |
78 | 17 | long len; |
79 | 17 | int inf, tag, class; |
80 | 17 | size_t tmp; |
81 | | |
82 | 17 | tmp = ASN1_STRING_get_length(p7->d.other->value.sequence); |
83 | 17 | if (tmp > INT_MAX) |
84 | 0 | return NULL; |
85 | 17 | os = ASN1_OCTET_STRING_new(); |
86 | 17 | if (os == NULL) |
87 | 0 | return NULL; |
88 | 17 | inf = ASN1_get_object(&data, &len, &tag, &class, (int)tmp); |
89 | 17 | if (inf != V_ASN1_CONSTRUCTED || tag != V_ASN1_SEQUENCE |
90 | 17 | || !ASN1_OCTET_STRING_set(os, data, len)) { |
91 | 0 | ASN1_OCTET_STRING_free(os); |
92 | 0 | os = NULL; |
93 | 0 | } |
94 | 17 | } |
95 | 1.74k | return os; |
96 | 1.74k | } |
97 | | |
98 | | static int pkcs7_bio_add_digest(BIO **pbio, X509_ALGOR *alg, |
99 | | const PKCS7_CTX *ctx) |
100 | 2.36k | { |
101 | 2.36k | BIO *btmp; |
102 | 2.36k | char name[OSSL_MAX_NAME_SIZE]; |
103 | 2.36k | EVP_MD *md = NULL; |
104 | | |
105 | 2.36k | if ((btmp = BIO_new(BIO_f_md())) == NULL) { |
106 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB); |
107 | 0 | goto err; |
108 | 0 | } |
109 | | |
110 | 2.36k | OBJ_obj2txt(name, sizeof(name), alg->algorithm, 0); |
111 | | |
112 | 2.36k | md = EVP_MD_fetch(ossl_pkcs7_ctx_get0_libctx(ctx), name, |
113 | 2.36k | ossl_pkcs7_ctx_get0_propq(ctx)); |
114 | | |
115 | 2.36k | if (md == NULL) { |
116 | 45 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE); |
117 | 45 | goto err; |
118 | 45 | } |
119 | | |
120 | 2.31k | if (BIO_set_md(btmp, md) <= 0) { |
121 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB); |
122 | 0 | EVP_MD_free(md); |
123 | 0 | goto err; |
124 | 0 | } |
125 | 2.31k | EVP_MD_free(md); |
126 | 2.31k | if (*pbio == NULL) |
127 | 2.31k | *pbio = btmp; |
128 | 0 | else if (!BIO_push(*pbio, btmp)) { |
129 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB); |
130 | 0 | goto err; |
131 | 0 | } |
132 | 2.31k | btmp = NULL; |
133 | | |
134 | 2.31k | return 1; |
135 | | |
136 | 45 | err: |
137 | 45 | BIO_free(btmp); |
138 | 45 | return 0; |
139 | 2.31k | } |
140 | | |
141 | | static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri, |
142 | | unsigned char *key, int keylen) |
143 | 0 | { |
144 | 0 | EVP_PKEY_CTX *pctx = NULL; |
145 | 0 | EVP_PKEY *pkey = NULL; |
146 | 0 | unsigned char *ek = NULL; |
147 | 0 | int ret = 0; |
148 | 0 | size_t eklen; |
149 | 0 | const PKCS7_CTX *ctx = ri->ctx; |
150 | |
|
151 | 0 | pkey = X509_get0_pubkey(ri->cert); |
152 | 0 | if (pkey == NULL) |
153 | 0 | return 0; |
154 | | |
155 | 0 | pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey, |
156 | 0 | ossl_pkcs7_ctx_get0_propq(ctx)); |
157 | 0 | if (pctx == NULL) |
158 | 0 | return 0; |
159 | | |
160 | 0 | if (EVP_PKEY_encrypt_init(pctx) <= 0) |
161 | 0 | goto err; |
162 | | |
163 | 0 | if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0) |
164 | 0 | goto err; |
165 | | |
166 | 0 | ek = OPENSSL_malloc(eklen); |
167 | 0 | if (ek == NULL) |
168 | 0 | goto err; |
169 | | |
170 | 0 | if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0) |
171 | 0 | goto err; |
172 | | |
173 | 0 | ASN1_STRING_set0(ri->enc_key, ek, (int)eklen); |
174 | 0 | ek = NULL; |
175 | |
|
176 | 0 | ret = 1; |
177 | |
|
178 | 0 | err: |
179 | 0 | EVP_PKEY_CTX_free(pctx); |
180 | 0 | OPENSSL_free(ek); |
181 | 0 | return ret; |
182 | 0 | } |
183 | | |
184 | | static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen, |
185 | | PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey, |
186 | | size_t fixlen) |
187 | 0 | { |
188 | 0 | EVP_PKEY_CTX *pctx = NULL; |
189 | 0 | unsigned char *ek = NULL; |
190 | 0 | size_t eklen; |
191 | 0 | int ret = -1; |
192 | 0 | const PKCS7_CTX *ctx = ri->ctx; |
193 | |
|
194 | 0 | pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey, |
195 | 0 | ossl_pkcs7_ctx_get0_propq(ctx)); |
196 | 0 | if (pctx == NULL) |
197 | 0 | return -1; |
198 | | |
199 | 0 | if (EVP_PKEY_decrypt_init(pctx) <= 0) |
200 | 0 | goto err; |
201 | | |
202 | 0 | ret = evp_pkey_decrypt_alloc(pctx, &ek, &eklen, fixlen, |
203 | 0 | ASN1_STRING_get0_data(ri->enc_key), ASN1_STRING_get_length(ri->enc_key)); |
204 | 0 | if (ret <= 0) |
205 | 0 | goto err; |
206 | | |
207 | 0 | ret = 1; |
208 | |
|
209 | 0 | OPENSSL_clear_free(*pek, *peklen); |
210 | 0 | *pek = ek; |
211 | 0 | *peklen = (int)eklen; |
212 | |
|
213 | 0 | err: |
214 | 0 | EVP_PKEY_CTX_free(pctx); |
215 | 0 | if (!ret) |
216 | 0 | OPENSSL_free(ek); |
217 | |
|
218 | 0 | return ret; |
219 | 0 | } |
220 | | |
221 | | BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio) |
222 | 2.36k | { |
223 | 2.36k | int i; |
224 | 2.36k | BIO *out = NULL, *btmp = NULL; |
225 | 2.36k | X509_ALGOR *xa = NULL; |
226 | 2.36k | EVP_CIPHER *fetched_cipher = NULL; |
227 | 2.36k | const EVP_CIPHER *cipher; |
228 | 2.36k | const EVP_CIPHER *evp_cipher = NULL; |
229 | 2.36k | STACK_OF(X509_ALGOR) *md_sk = NULL; |
230 | 2.36k | STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL; |
231 | 2.36k | X509_ALGOR *xalg = NULL; |
232 | 2.36k | PKCS7_RECIP_INFO *ri = NULL; |
233 | 2.36k | ASN1_OCTET_STRING *os = NULL; |
234 | 2.36k | const PKCS7_CTX *p7_ctx; |
235 | 2.36k | OSSL_LIB_CTX *libctx; |
236 | 2.36k | const char *propq; |
237 | | |
238 | 2.36k | if (p7 == NULL) { |
239 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER); |
240 | 0 | return NULL; |
241 | 0 | } |
242 | 2.36k | p7_ctx = ossl_pkcs7_get0_ctx(p7); |
243 | 2.36k | libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx); |
244 | 2.36k | propq = ossl_pkcs7_ctx_get0_propq(p7_ctx); |
245 | | |
246 | | /* |
247 | | * The content field in the PKCS7 ContentInfo is optional, but that really |
248 | | * only applies to inner content (precisely, detached signatures). |
249 | | * |
250 | | * When reading content, missing outer content is therefore treated as an |
251 | | * error. |
252 | | * |
253 | | * When creating content, PKCS7_content_new() must be called before |
254 | | * calling this method, so a NULL p7->d is always an error. |
255 | | */ |
256 | 2.36k | if (p7->d.ptr == NULL) { |
257 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT); |
258 | 0 | return NULL; |
259 | 0 | } |
260 | | |
261 | 2.36k | i = OBJ_obj2nid(p7->type); |
262 | | |
263 | 2.36k | switch (i) { |
264 | 2.36k | case NID_pkcs7_signed: |
265 | 2.36k | md_sk = p7->d.sign->md_algs; |
266 | 2.36k | os = pkcs7_get1_data(p7->d.sign->contents, |
267 | 2.36k | (p7->state & PKCS7_STATE_STREAMING) != 0); |
268 | 2.36k | break; |
269 | 0 | case NID_pkcs7_signedAndEnveloped: |
270 | 0 | rsk = p7->d.signed_and_enveloped->recipientinfo; |
271 | 0 | md_sk = p7->d.signed_and_enveloped->md_algs; |
272 | 0 | xalg = p7->d.signed_and_enveloped->enc_data->algorithm; |
273 | 0 | evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher; |
274 | 0 | if (evp_cipher == NULL) { |
275 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED); |
276 | 0 | goto err; |
277 | 0 | } |
278 | 0 | break; |
279 | 0 | case NID_pkcs7_enveloped: |
280 | 0 | rsk = p7->d.enveloped->recipientinfo; |
281 | 0 | xalg = p7->d.enveloped->enc_data->algorithm; |
282 | 0 | evp_cipher = p7->d.enveloped->enc_data->cipher; |
283 | 0 | if (evp_cipher == NULL) { |
284 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED); |
285 | 0 | goto err; |
286 | 0 | } |
287 | 0 | break; |
288 | 0 | case NID_pkcs7_digest: |
289 | 0 | xa = p7->d.digest->md; |
290 | 0 | os = pkcs7_get1_data(p7->d.digest->contents, |
291 | 0 | (p7->state & PKCS7_STATE_STREAMING) != 0); |
292 | 0 | break; |
293 | 0 | case NID_pkcs7_data: |
294 | 0 | break; |
295 | 0 | default: |
296 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); |
297 | 0 | goto err; |
298 | 2.36k | } |
299 | | |
300 | 4.68k | for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) |
301 | 2.36k | if (!pkcs7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i), p7_ctx)) |
302 | 45 | goto err; |
303 | | |
304 | 2.32k | if (xa && !pkcs7_bio_add_digest(&out, xa, p7_ctx)) |
305 | 0 | goto err; |
306 | | |
307 | 2.32k | if (evp_cipher != NULL) { |
308 | 0 | unsigned char key[EVP_MAX_KEY_LENGTH]; |
309 | 0 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
310 | 0 | int keylen, ivlen; |
311 | 0 | EVP_CIPHER_CTX *ctx; |
312 | |
|
313 | 0 | if ((btmp = BIO_new(BIO_f_cipher())) == NULL) { |
314 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB); |
315 | 0 | goto err; |
316 | 0 | } |
317 | 0 | BIO_get_cipher_ctx(btmp, &ctx); |
318 | 0 | keylen = EVP_CIPHER_get_key_length(evp_cipher); |
319 | 0 | ivlen = EVP_CIPHER_get_iv_length(evp_cipher); |
320 | 0 | xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_get_type(evp_cipher)); |
321 | 0 | if (ivlen > 0) |
322 | 0 | if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0) |
323 | 0 | goto err; |
324 | | |
325 | 0 | (void)ERR_set_mark(); |
326 | 0 | fetched_cipher = EVP_CIPHER_fetch(libctx, |
327 | 0 | EVP_CIPHER_get0_name(evp_cipher), |
328 | 0 | propq); |
329 | 0 | (void)ERR_pop_to_mark(); |
330 | 0 | if (fetched_cipher != NULL) |
331 | 0 | cipher = fetched_cipher; |
332 | 0 | else |
333 | 0 | cipher = evp_cipher; |
334 | |
|
335 | 0 | if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) <= 0) |
336 | 0 | goto err; |
337 | | |
338 | 0 | EVP_CIPHER_free(fetched_cipher); |
339 | 0 | fetched_cipher = NULL; |
340 | |
|
341 | 0 | if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0) |
342 | 0 | goto err; |
343 | 0 | if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0) |
344 | 0 | goto err; |
345 | | |
346 | 0 | if (ivlen > 0) { |
347 | 0 | if (xalg->parameter == NULL) { |
348 | 0 | xalg->parameter = ASN1_TYPE_new(); |
349 | 0 | if (xalg->parameter == NULL) |
350 | 0 | goto err; |
351 | 0 | } |
352 | 0 | if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) <= 0) { |
353 | 0 | ASN1_TYPE_free(xalg->parameter); |
354 | 0 | xalg->parameter = NULL; |
355 | 0 | goto err; |
356 | 0 | } |
357 | 0 | } |
358 | | |
359 | | /* Lets do the pub key stuff :-) */ |
360 | 0 | for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) { |
361 | 0 | ri = sk_PKCS7_RECIP_INFO_value(rsk, i); |
362 | 0 | if (pkcs7_encode_rinfo(ri, key, keylen) <= 0) |
363 | 0 | goto err; |
364 | 0 | } |
365 | 0 | OPENSSL_cleanse(key, keylen); |
366 | |
|
367 | 0 | if (out == NULL) |
368 | 0 | out = btmp; |
369 | 0 | else |
370 | 0 | BIO_push(out, btmp); |
371 | 0 | btmp = NULL; |
372 | 0 | } |
373 | | |
374 | 2.32k | if (bio == NULL) { |
375 | 200 | if (PKCS7_is_detached(p7)) { |
376 | 0 | bio = BIO_new(BIO_s_null()); |
377 | 200 | } else if (os != NULL && ASN1_STRING_get_length(os) > 0) { |
378 | | /* |
379 | | * bio needs a copy of os->data instead of a pointer because |
380 | | * the data will be used after os has been freed |
381 | | */ |
382 | 198 | bio = BIO_new(BIO_s_mem()); |
383 | 198 | if (bio != NULL) { |
384 | 198 | BIO_set_mem_eof_return(bio, 0); |
385 | 198 | const unsigned char *os_data = ASN1_STRING_get0_data(os); |
386 | 198 | size_t os_len = ASN1_STRING_get_length(os); |
387 | 198 | if (os_len > INT_MAX || BIO_write(bio, os_data, (int)os_len) != (int)os_len) { |
388 | 0 | BIO_free_all(bio); |
389 | 0 | bio = NULL; |
390 | 0 | } |
391 | 198 | } |
392 | 198 | } else { |
393 | 2 | bio = BIO_new(BIO_s_mem()); |
394 | 2 | if (bio == NULL) |
395 | 0 | goto err; |
396 | 2 | BIO_set_mem_eof_return(bio, 0); |
397 | 2 | } |
398 | 200 | if (bio == NULL) |
399 | 0 | goto err; |
400 | 200 | } |
401 | 2.32k | if (out) |
402 | 2.31k | BIO_push(out, bio); |
403 | 4 | else |
404 | 4 | out = bio; |
405 | | |
406 | 2.32k | ASN1_OCTET_STRING_free(os); |
407 | 2.32k | return out; |
408 | | |
409 | 45 | err: |
410 | 45 | ASN1_OCTET_STRING_free(os); |
411 | 45 | EVP_CIPHER_free(fetched_cipher); |
412 | 45 | BIO_free_all(out); |
413 | 45 | BIO_free_all(btmp); |
414 | 45 | return NULL; |
415 | 2.32k | } |
416 | | |
417 | | static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert) |
418 | 0 | { |
419 | 0 | int ret; |
420 | 0 | ret = X509_NAME_cmp(ri->issuer_and_serial->issuer, |
421 | 0 | X509_get_issuer_name(pcert)); |
422 | 0 | if (ret) |
423 | 0 | return ret; |
424 | 0 | return ASN1_INTEGER_cmp(X509_get0_serialNumber(pcert), |
425 | 0 | ri->issuer_and_serial->serial); |
426 | 0 | } |
427 | | |
428 | | /* int */ |
429 | | BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert) |
430 | 0 | { |
431 | 0 | int i, len; |
432 | 0 | BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL; |
433 | 0 | X509_ALGOR *xa; |
434 | 0 | ASN1_OCTET_STRING *data_body = NULL; |
435 | 0 | EVP_MD *md = NULL; |
436 | 0 | EVP_CIPHER *cipher = NULL; |
437 | 0 | EVP_CIPHER_CTX *evp_ctx = NULL; |
438 | 0 | X509_ALGOR *enc_alg = NULL; |
439 | 0 | STACK_OF(X509_ALGOR) *md_sk = NULL; |
440 | 0 | STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL; |
441 | 0 | PKCS7_RECIP_INFO *ri = NULL; |
442 | 0 | unsigned char *ek = NULL, *tkey = NULL; |
443 | 0 | int eklen = 0, tkeylen = 0; |
444 | 0 | char name[OSSL_MAX_NAME_SIZE]; |
445 | 0 | const PKCS7_CTX *p7_ctx; |
446 | 0 | OSSL_LIB_CTX *libctx; |
447 | 0 | const char *propq; |
448 | |
|
449 | 0 | if (p7 == NULL) { |
450 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER); |
451 | 0 | return NULL; |
452 | 0 | } |
453 | | |
454 | 0 | p7_ctx = ossl_pkcs7_get0_ctx(p7); |
455 | 0 | libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx); |
456 | 0 | propq = ossl_pkcs7_ctx_get0_propq(p7_ctx); |
457 | |
|
458 | 0 | if (p7->d.ptr == NULL) { |
459 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT); |
460 | 0 | return NULL; |
461 | 0 | } |
462 | | |
463 | 0 | i = OBJ_obj2nid(p7->type); |
464 | |
|
465 | 0 | switch (i) { |
466 | 0 | case NID_pkcs7_signed: |
467 | | /* |
468 | | * p7->d.sign->contents is a PKCS7 structure consisting of a contentType |
469 | | * field and optional content. |
470 | | * data_body is NULL if that structure has no (=detached) content |
471 | | * or if the contentType is wrong (i.e., not "data"). |
472 | | */ |
473 | 0 | data_body = PKCS7_get_octet_string(p7->d.sign->contents); |
474 | 0 | if (!PKCS7_is_detached(p7) && data_body == NULL) { |
475 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_SIGNED_DATA_TYPE); |
476 | 0 | goto err; |
477 | 0 | } |
478 | 0 | md_sk = p7->d.sign->md_algs; |
479 | 0 | break; |
480 | 0 | case NID_pkcs7_signedAndEnveloped: |
481 | 0 | rsk = p7->d.signed_and_enveloped->recipientinfo; |
482 | 0 | md_sk = p7->d.signed_and_enveloped->md_algs; |
483 | | /* data_body is NULL if the optional EncryptedContent is missing. */ |
484 | 0 | data_body = p7->d.signed_and_enveloped->enc_data->enc_data; |
485 | 0 | enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm; |
486 | |
|
487 | 0 | OBJ_obj2txt(name, sizeof(name), enc_alg->algorithm, 0); |
488 | |
|
489 | 0 | cipher = EVP_CIPHER_fetch(libctx, name, propq); |
490 | |
|
491 | 0 | if (cipher == NULL) { |
492 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE); |
493 | 0 | goto err; |
494 | 0 | } |
495 | 0 | break; |
496 | 0 | case NID_pkcs7_enveloped: |
497 | 0 | rsk = p7->d.enveloped->recipientinfo; |
498 | 0 | enc_alg = p7->d.enveloped->enc_data->algorithm; |
499 | | /* data_body is NULL if the optional EncryptedContent is missing. */ |
500 | 0 | data_body = p7->d.enveloped->enc_data->enc_data; |
501 | 0 | OBJ_obj2txt(name, sizeof(name), enc_alg->algorithm, 0); |
502 | |
|
503 | 0 | cipher = EVP_CIPHER_fetch(libctx, name, propq); |
504 | |
|
505 | 0 | if (cipher == NULL) { |
506 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE); |
507 | 0 | goto err; |
508 | 0 | } |
509 | 0 | break; |
510 | 0 | default: |
511 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); |
512 | 0 | goto err; |
513 | 0 | } |
514 | | |
515 | | /* Detached content must be supplied via in_bio instead. */ |
516 | 0 | if (data_body == NULL && in_bio == NULL) { |
517 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT); |
518 | 0 | goto err; |
519 | 0 | } |
520 | | |
521 | | /* We will be checking the signature */ |
522 | 0 | if (md_sk != NULL) { |
523 | 0 | for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) { |
524 | 0 | xa = sk_X509_ALGOR_value(md_sk, i); |
525 | 0 | if ((btmp = BIO_new(BIO_f_md())) == NULL) { |
526 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB); |
527 | 0 | goto err; |
528 | 0 | } |
529 | | |
530 | 0 | OBJ_obj2txt(name, sizeof(name), xa->algorithm, 0); |
531 | |
|
532 | 0 | md = EVP_MD_fetch(libctx, name, propq); |
533 | |
|
534 | 0 | if (md == NULL) { |
535 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE); |
536 | 0 | goto err; |
537 | 0 | } |
538 | | |
539 | 0 | if (BIO_set_md(btmp, md) <= 0) { |
540 | 0 | EVP_MD_free(md); |
541 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB); |
542 | 0 | goto err; |
543 | 0 | } |
544 | 0 | EVP_MD_free(md); |
545 | 0 | if (out == NULL) |
546 | 0 | out = btmp; |
547 | 0 | else |
548 | 0 | BIO_push(out, btmp); |
549 | 0 | btmp = NULL; |
550 | 0 | } |
551 | 0 | } |
552 | | |
553 | 0 | if (cipher != NULL) { |
554 | 0 | if ((etmp = BIO_new(BIO_f_cipher())) == NULL) { |
555 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB); |
556 | 0 | goto err; |
557 | 0 | } |
558 | | |
559 | | /* |
560 | | * It was encrypted, we need to decrypt the secret key with the |
561 | | * private key |
562 | | */ |
563 | | |
564 | | /* |
565 | | * Find the recipientInfo which matches the passed certificate (if |
566 | | * any) |
567 | | */ |
568 | | |
569 | 0 | if (pcert) { |
570 | 0 | for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) { |
571 | 0 | ri = sk_PKCS7_RECIP_INFO_value(rsk, i); |
572 | 0 | if (!pkcs7_cmp_ri(ri, pcert)) |
573 | 0 | break; |
574 | 0 | ri = NULL; |
575 | 0 | } |
576 | 0 | if (ri == NULL) { |
577 | 0 | ERR_raise(ERR_LIB_PKCS7, |
578 | 0 | PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE); |
579 | 0 | goto err; |
580 | 0 | } |
581 | 0 | } |
582 | | |
583 | | /* If we haven't got a certificate try each ri in turn */ |
584 | 0 | if (pcert == NULL) { |
585 | | /* |
586 | | * Always attempt to decrypt all rinfo even after success as a |
587 | | * defence against MMA timing attacks. |
588 | | */ |
589 | 0 | for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) { |
590 | 0 | ri = sk_PKCS7_RECIP_INFO_value(rsk, i); |
591 | 0 | ri->ctx = p7_ctx; |
592 | 0 | if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey, |
593 | 0 | EVP_CIPHER_get_key_length(cipher)) |
594 | 0 | < 0) |
595 | 0 | goto err; |
596 | 0 | ERR_clear_error(); |
597 | 0 | } |
598 | 0 | } else { |
599 | 0 | ri->ctx = p7_ctx; |
600 | | /* Only exit on fatal errors, not decrypt failure */ |
601 | 0 | if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey, 0) < 0) |
602 | 0 | goto err; |
603 | 0 | ERR_clear_error(); |
604 | 0 | } |
605 | | |
606 | 0 | evp_ctx = NULL; |
607 | 0 | BIO_get_cipher_ctx(etmp, &evp_ctx); |
608 | 0 | if (EVP_CipherInit_ex(evp_ctx, cipher, NULL, NULL, NULL, 0) <= 0) |
609 | 0 | goto err; |
610 | 0 | if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) <= 0) |
611 | 0 | goto err; |
612 | | /* Generate random key as MMA defence */ |
613 | 0 | len = EVP_CIPHER_CTX_get_key_length(evp_ctx); |
614 | 0 | if (len <= 0) |
615 | 0 | goto err; |
616 | 0 | tkeylen = (size_t)len; |
617 | 0 | tkey = OPENSSL_malloc(tkeylen); |
618 | 0 | if (tkey == NULL) |
619 | 0 | goto err; |
620 | 0 | if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0) |
621 | 0 | goto err; |
622 | 0 | if (ek == NULL) { |
623 | 0 | ek = tkey; |
624 | 0 | eklen = tkeylen; |
625 | 0 | tkey = NULL; |
626 | 0 | } |
627 | |
|
628 | 0 | if (eklen != EVP_CIPHER_CTX_get_key_length(evp_ctx)) { |
629 | | /* |
630 | | * Some S/MIME clients don't use the same key and effective key |
631 | | * length. The key length is determined by the size of the |
632 | | * decrypted RSA key. |
633 | | */ |
634 | 0 | if (EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen) <= 0) { |
635 | | /* Use random key as MMA defence */ |
636 | 0 | OPENSSL_clear_free(ek, eklen); |
637 | 0 | ek = tkey; |
638 | 0 | eklen = tkeylen; |
639 | 0 | tkey = NULL; |
640 | 0 | } |
641 | 0 | } |
642 | | /* Clear errors so we don't leak information useful in MMA */ |
643 | 0 | ERR_clear_error(); |
644 | 0 | if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0) |
645 | 0 | goto err; |
646 | | |
647 | 0 | OPENSSL_clear_free(ek, eklen); |
648 | 0 | ek = NULL; |
649 | 0 | OPENSSL_clear_free(tkey, tkeylen); |
650 | 0 | tkey = NULL; |
651 | |
|
652 | 0 | if (out == NULL) |
653 | 0 | out = etmp; |
654 | 0 | else |
655 | 0 | BIO_push(out, etmp); |
656 | 0 | etmp = NULL; |
657 | 0 | } |
658 | 0 | if (in_bio != NULL) { |
659 | 0 | bio = in_bio; |
660 | 0 | } else { |
661 | 0 | size_t data_body_len = ASN1_STRING_get_length(data_body); |
662 | 0 | if (data_body_len > INT_MAX) |
663 | 0 | goto err; |
664 | 0 | if (data_body_len > 0) |
665 | 0 | bio = BIO_new_mem_buf(ASN1_STRING_get0_data(data_body), |
666 | 0 | (int)data_body_len); |
667 | 0 | else { |
668 | 0 | bio = BIO_new(BIO_s_mem()); |
669 | 0 | if (bio == NULL) |
670 | 0 | goto err; |
671 | 0 | BIO_set_mem_eof_return(bio, 0); |
672 | 0 | } |
673 | 0 | if (bio == NULL) |
674 | 0 | goto err; |
675 | 0 | } |
676 | 0 | BIO_push(out, bio); |
677 | 0 | bio = NULL; |
678 | 0 | EVP_CIPHER_free(cipher); |
679 | 0 | return out; |
680 | | |
681 | 0 | err: |
682 | 0 | EVP_CIPHER_free(cipher); |
683 | 0 | OPENSSL_clear_free(ek, eklen); |
684 | 0 | OPENSSL_clear_free(tkey, tkeylen); |
685 | 0 | BIO_free_all(out); |
686 | 0 | BIO_free_all(btmp); |
687 | 0 | BIO_free_all(etmp); |
688 | 0 | BIO_free_all(bio); |
689 | 0 | return NULL; |
690 | 0 | } |
691 | | |
692 | | static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid) |
693 | 0 | { |
694 | 0 | for (;;) { |
695 | 0 | bio = BIO_find_type(bio, BIO_TYPE_MD); |
696 | 0 | if (bio == NULL) { |
697 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); |
698 | 0 | return NULL; |
699 | 0 | } |
700 | 0 | BIO_get_md_ctx(bio, pmd); |
701 | 0 | if (*pmd == NULL) { |
702 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR); |
703 | 0 | return NULL; |
704 | 0 | } |
705 | 0 | if (EVP_MD_CTX_get_type(*pmd) == nid) |
706 | 0 | return bio; |
707 | 0 | bio = BIO_next(bio); |
708 | 0 | } |
709 | 0 | return NULL; |
710 | 0 | } |
711 | | |
712 | | static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx) |
713 | 0 | { |
714 | 0 | unsigned char md_data[EVP_MAX_MD_SIZE]; |
715 | 0 | unsigned int md_len; |
716 | | |
717 | | /* Add signing time if not already present */ |
718 | 0 | if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) { |
719 | 0 | if (!PKCS7_add0_attrib_signing_time(si, NULL)) { |
720 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB); |
721 | 0 | return 0; |
722 | 0 | } |
723 | 0 | } |
724 | | |
725 | | /* Add digest */ |
726 | 0 | if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) { |
727 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB); |
728 | 0 | return 0; |
729 | 0 | } |
730 | 0 | if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) { |
731 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB); |
732 | 0 | return 0; |
733 | 0 | } |
734 | | |
735 | | /* Now sign the attributes */ |
736 | 0 | if (!PKCS7_SIGNER_INFO_sign(si)) |
737 | 0 | return 0; |
738 | | |
739 | 0 | return 1; |
740 | 0 | } |
741 | | |
742 | | int PKCS7_dataFinal(PKCS7 *p7, BIO *bio) |
743 | 0 | { |
744 | 0 | int ret = 0; |
745 | 0 | int i, j; |
746 | 0 | BIO *btmp; |
747 | 0 | PKCS7_SIGNER_INFO *si; |
748 | 0 | EVP_MD_CTX *mdc, *ctx_tmp; |
749 | 0 | STACK_OF(X509_ATTRIBUTE) *sk; |
750 | 0 | STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL; |
751 | 0 | ASN1_OCTET_STRING *os = NULL; |
752 | 0 | const PKCS7_CTX *p7_ctx; |
753 | |
|
754 | 0 | if (p7 == NULL) { |
755 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER); |
756 | 0 | return 0; |
757 | 0 | } |
758 | | |
759 | 0 | p7_ctx = ossl_pkcs7_get0_ctx(p7); |
760 | |
|
761 | 0 | if (p7->d.ptr == NULL) { |
762 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT); |
763 | 0 | return 0; |
764 | 0 | } |
765 | | |
766 | 0 | ctx_tmp = EVP_MD_CTX_new(); |
767 | 0 | if (ctx_tmp == NULL) { |
768 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB); |
769 | 0 | return 0; |
770 | 0 | } |
771 | | |
772 | 0 | i = OBJ_obj2nid(p7->type); |
773 | |
|
774 | 0 | switch (i) { |
775 | 0 | case NID_pkcs7_data: |
776 | 0 | os = p7->d.data; |
777 | 0 | break; |
778 | 0 | case NID_pkcs7_signedAndEnveloped: |
779 | | /* XXXXXXXXXXXXXXXX */ |
780 | 0 | si_sk = p7->d.signed_and_enveloped->signer_info; |
781 | 0 | os = p7->d.signed_and_enveloped->enc_data->enc_data; |
782 | 0 | if (os == NULL) { |
783 | 0 | os = ASN1_OCTET_STRING_new(); |
784 | 0 | if (os == NULL) { |
785 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB); |
786 | 0 | goto err; |
787 | 0 | } |
788 | 0 | p7->d.signed_and_enveloped->enc_data->enc_data = os; |
789 | 0 | } |
790 | 0 | break; |
791 | 0 | case NID_pkcs7_enveloped: |
792 | | /* XXXXXXXXXXXXXXXX */ |
793 | 0 | os = p7->d.enveloped->enc_data->enc_data; |
794 | 0 | if (os == NULL) { |
795 | 0 | os = ASN1_OCTET_STRING_new(); |
796 | 0 | if (os == NULL) { |
797 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB); |
798 | 0 | goto err; |
799 | 0 | } |
800 | 0 | p7->d.enveloped->enc_data->enc_data = os; |
801 | 0 | } |
802 | 0 | break; |
803 | 0 | case NID_pkcs7_signed: |
804 | 0 | si_sk = p7->d.sign->signer_info; |
805 | 0 | if (p7->d.sign->contents == NULL) { |
806 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT); |
807 | 0 | goto err; |
808 | 0 | } |
809 | 0 | os = PKCS7_get_octet_string(p7->d.sign->contents); |
810 | | /* If detached data then the content is excluded */ |
811 | 0 | if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) { |
812 | 0 | ASN1_OCTET_STRING_free(os); |
813 | 0 | os = NULL; |
814 | 0 | p7->d.sign->contents->d.data = NULL; |
815 | 0 | } |
816 | 0 | break; |
817 | | |
818 | 0 | case NID_pkcs7_digest: |
819 | 0 | if (p7->d.digest->contents == NULL) { |
820 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT); |
821 | 0 | goto err; |
822 | 0 | } |
823 | 0 | os = PKCS7_get_octet_string(p7->d.digest->contents); |
824 | | /* If detached data then the content is excluded */ |
825 | 0 | if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) { |
826 | 0 | ASN1_OCTET_STRING_free(os); |
827 | 0 | os = NULL; |
828 | 0 | p7->d.digest->contents->d.data = NULL; |
829 | 0 | } |
830 | 0 | break; |
831 | | |
832 | 0 | default: |
833 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); |
834 | 0 | goto err; |
835 | 0 | } |
836 | | |
837 | 0 | if (si_sk != NULL) { |
838 | 0 | for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) { |
839 | 0 | si = sk_PKCS7_SIGNER_INFO_value(si_sk, i); |
840 | 0 | if (si->pkey == NULL) |
841 | 0 | continue; |
842 | | |
843 | 0 | j = OBJ_obj2nid(si->digest_alg->algorithm); |
844 | |
|
845 | 0 | btmp = bio; |
846 | |
|
847 | 0 | btmp = PKCS7_find_digest(&mdc, btmp, j); |
848 | |
|
849 | 0 | if (btmp == NULL) |
850 | 0 | goto err; |
851 | | |
852 | | /* |
853 | | * We now have the EVP_MD_CTX, lets do the signing. |
854 | | */ |
855 | 0 | if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc)) |
856 | 0 | goto err; |
857 | | |
858 | 0 | sk = si->auth_attr; |
859 | | |
860 | | /* |
861 | | * If there are attributes, we add the digest attribute and only |
862 | | * sign the attributes |
863 | | */ |
864 | 0 | if (sk_X509_ATTRIBUTE_num(sk) > 0) { |
865 | 0 | if (!do_pkcs7_signed_attrib(si, ctx_tmp)) |
866 | 0 | goto err; |
867 | 0 | } else { |
868 | 0 | unsigned char *abuf = NULL; |
869 | 0 | unsigned int abuflen = EVP_PKEY_get_size(si->pkey); |
870 | |
|
871 | 0 | if (abuflen == 0 || (abuf = OPENSSL_malloc(abuflen)) == NULL) |
872 | 0 | goto err; |
873 | | |
874 | 0 | if (!EVP_SignFinal_ex(ctx_tmp, abuf, &abuflen, si->pkey, |
875 | 0 | ossl_pkcs7_ctx_get0_libctx(p7_ctx), |
876 | 0 | ossl_pkcs7_ctx_get0_propq(p7_ctx))) { |
877 | 0 | OPENSSL_free(abuf); |
878 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB); |
879 | 0 | goto err; |
880 | 0 | } |
881 | 0 | ASN1_STRING_set0(si->enc_digest, abuf, abuflen); |
882 | 0 | } |
883 | 0 | } |
884 | 0 | } else if (i == NID_pkcs7_digest) { |
885 | 0 | unsigned char md_data[EVP_MAX_MD_SIZE]; |
886 | 0 | unsigned int md_len; |
887 | 0 | if (!PKCS7_find_digest(&mdc, bio, |
888 | 0 | OBJ_obj2nid(p7->d.digest->md->algorithm))) |
889 | 0 | goto err; |
890 | 0 | if (!EVP_DigestFinal_ex(mdc, md_data, &md_len)) |
891 | 0 | goto err; |
892 | 0 | if (!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len)) |
893 | 0 | goto err; |
894 | 0 | } |
895 | | |
896 | 0 | if (!PKCS7_is_detached(p7)) { |
897 | | /* |
898 | | * NOTE(emilia): I think we only reach os == NULL here because detached |
899 | | * digested data support is broken. |
900 | | */ |
901 | 0 | if (os == NULL) |
902 | 0 | goto err; |
903 | 0 | if (!(p7->state & PKCS7_STATE_STREAMING)) { |
904 | 0 | char *cont; |
905 | 0 | long contlen; |
906 | 0 | btmp = BIO_find_type(bio, BIO_TYPE_MEM); |
907 | 0 | if (btmp == NULL) { |
908 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MEM_BIO); |
909 | 0 | goto err; |
910 | 0 | } |
911 | 0 | contlen = BIO_get_mem_data(btmp, &cont); |
912 | | /* |
913 | | * Mark the BIO read only then we can use its copy of the data |
914 | | * instead of making an extra copy. |
915 | | */ |
916 | 0 | BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY); |
917 | 0 | BIO_set_mem_eof_return(btmp, 0); |
918 | 0 | ASN1_STRING_set0(os, (unsigned char *)cont, contlen); |
919 | 0 | } |
920 | 0 | } |
921 | 0 | ret = 1; |
922 | 0 | err: |
923 | 0 | EVP_MD_CTX_free(ctx_tmp); |
924 | 0 | return ret; |
925 | 0 | } |
926 | | |
927 | | int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si) |
928 | 0 | { |
929 | 0 | EVP_MD_CTX *mctx; |
930 | 0 | EVP_PKEY_CTX *pctx = NULL; |
931 | 0 | unsigned char *abuf = NULL; |
932 | 0 | int alen; |
933 | 0 | size_t siglen; |
934 | 0 | const EVP_MD *md = NULL; |
935 | 0 | const PKCS7_CTX *ctx = si->ctx; |
936 | |
|
937 | 0 | md = EVP_get_digestbyobj(si->digest_alg->algorithm); |
938 | 0 | if (md == NULL) |
939 | 0 | return 0; |
940 | | |
941 | 0 | mctx = EVP_MD_CTX_new(); |
942 | 0 | if (mctx == NULL) { |
943 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB); |
944 | 0 | goto err; |
945 | 0 | } |
946 | | |
947 | 0 | if (EVP_DigestSignInit_ex(mctx, &pctx, EVP_MD_get0_name(md), |
948 | 0 | ossl_pkcs7_ctx_get0_libctx(ctx), |
949 | 0 | ossl_pkcs7_ctx_get0_propq(ctx), si->pkey, |
950 | 0 | NULL) |
951 | 0 | <= 0) |
952 | 0 | goto err; |
953 | | |
954 | 0 | alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf, |
955 | 0 | ASN1_ITEM_rptr(PKCS7_ATTR_SIGN)); |
956 | 0 | if (alen < 0 || abuf == NULL) |
957 | 0 | goto err; |
958 | 0 | if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0) |
959 | 0 | goto err; |
960 | 0 | OPENSSL_free(abuf); |
961 | 0 | abuf = NULL; |
962 | 0 | if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0) |
963 | 0 | goto err; |
964 | 0 | abuf = OPENSSL_malloc(siglen); |
965 | 0 | if (abuf == NULL) |
966 | 0 | goto err; |
967 | 0 | if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0) |
968 | 0 | goto err; |
969 | | |
970 | 0 | EVP_MD_CTX_free(mctx); |
971 | |
|
972 | 0 | ASN1_STRING_set0(si->enc_digest, abuf, (int)siglen); |
973 | |
|
974 | 0 | return 1; |
975 | | |
976 | 0 | err: |
977 | 0 | OPENSSL_free(abuf); |
978 | 0 | EVP_MD_CTX_free(mctx); |
979 | 0 | return 0; |
980 | 0 | } |
981 | | |
982 | | /* This partly overlaps with PKCS7_verify(). It does not support flags. */ |
983 | | int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio, |
984 | | PKCS7 *p7, PKCS7_SIGNER_INFO *si) |
985 | 0 | { |
986 | 0 | PKCS7_ISSUER_AND_SERIAL *ias; |
987 | 0 | int ret = 0, i; |
988 | 0 | STACK_OF(X509) *untrusted; |
989 | 0 | STACK_OF(X509_CRL) *crls; |
990 | 0 | X509 *signer; |
991 | |
|
992 | 0 | if (p7 == NULL) { |
993 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER); |
994 | 0 | return 0; |
995 | 0 | } |
996 | | |
997 | 0 | if (p7->d.ptr == NULL) { |
998 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT); |
999 | 0 | return 0; |
1000 | 0 | } |
1001 | | |
1002 | 0 | if (PKCS7_type_is_signed(p7)) { |
1003 | 0 | untrusted = p7->d.sign->cert; |
1004 | 0 | crls = p7->d.sign->crl; |
1005 | 0 | } else if (PKCS7_type_is_signedAndEnveloped(p7)) { |
1006 | 0 | untrusted = p7->d.signed_and_enveloped->cert; |
1007 | 0 | crls = p7->d.signed_and_enveloped->crl; |
1008 | 0 | } else { |
1009 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE); |
1010 | 0 | goto err; |
1011 | 0 | } |
1012 | 0 | X509_STORE_CTX_set0_crls(ctx, crls); |
1013 | | |
1014 | | /* XXXXXXXXXXXXXXXXXXXXXXX */ |
1015 | 0 | ias = si->issuer_and_serial; |
1016 | |
|
1017 | 0 | signer = X509_find_by_issuer_and_serial(untrusted, ias->issuer, ias->serial); |
1018 | | |
1019 | | /* Were we able to find the signer certificate in passed to us? */ |
1020 | 0 | if (signer == NULL) { |
1021 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_CERTIFICATE); |
1022 | 0 | goto err; |
1023 | 0 | } |
1024 | | |
1025 | | /* Lets verify */ |
1026 | 0 | if (!X509_STORE_CTX_init(ctx, cert_store, signer, untrusted)) { |
1027 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB); |
1028 | 0 | goto err; |
1029 | 0 | } |
1030 | 0 | X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN); |
1031 | 0 | i = X509_verify_cert(ctx); |
1032 | 0 | if (i <= 0) { |
1033 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB); |
1034 | 0 | goto err; |
1035 | 0 | } |
1036 | | |
1037 | 0 | if (PKCS7_signatureVerify(bio, p7, si, signer) <= 0) |
1038 | 0 | goto err; |
1039 | 0 | ret = 1; |
1040 | 0 | err: |
1041 | 0 | return ret; |
1042 | 0 | } |
1043 | | |
1044 | | int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, |
1045 | | const X509 *signer) |
1046 | 2.38k | { |
1047 | 2.38k | ASN1_OCTET_STRING *os; |
1048 | 2.38k | EVP_MD_CTX *mdc_tmp, *mdc; |
1049 | 2.38k | EVP_MD *md = NULL; |
1050 | 2.38k | int ret = 0, i; |
1051 | 2.38k | int md_type; |
1052 | 2.38k | STACK_OF(X509_ATTRIBUTE) *sk; |
1053 | 2.38k | BIO *btmp; |
1054 | 2.38k | EVP_PKEY *pkey; |
1055 | 2.38k | unsigned char *abuf = NULL; |
1056 | 2.38k | const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7); |
1057 | 2.38k | OSSL_LIB_CTX *libctx = ossl_pkcs7_ctx_get0_libctx(ctx); |
1058 | 2.38k | const char *propq = ossl_pkcs7_ctx_get0_propq(ctx); |
1059 | | |
1060 | 2.38k | mdc_tmp = EVP_MD_CTX_new(); |
1061 | 2.38k | if (mdc_tmp == NULL) { |
1062 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB); |
1063 | 0 | goto err; |
1064 | 0 | } |
1065 | | |
1066 | 2.38k | if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) { |
1067 | 0 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE); |
1068 | 0 | goto err; |
1069 | 0 | } |
1070 | | |
1071 | 2.38k | md_type = OBJ_obj2nid(si->digest_alg->algorithm); |
1072 | | |
1073 | 2.38k | btmp = bio; |
1074 | 2.51k | for (;;) { |
1075 | 2.51k | if ((btmp == NULL) || ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) { |
1076 | 128 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); |
1077 | 128 | goto err; |
1078 | 128 | } |
1079 | 2.38k | BIO_get_md_ctx(btmp, &mdc); |
1080 | 2.38k | if (mdc == NULL) { |
1081 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR); |
1082 | 0 | goto err; |
1083 | 0 | } |
1084 | 2.38k | if (EVP_MD_CTX_get_type(mdc) == md_type) |
1085 | 1.79k | break; |
1086 | | /* |
1087 | | * Workaround for some broken clients that put the signature OID |
1088 | | * instead of the digest OID in digest_alg->algorithm |
1089 | | */ |
1090 | 587 | if (EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mdc)) == md_type) |
1091 | 463 | break; |
1092 | 124 | btmp = BIO_next(btmp); |
1093 | 124 | } |
1094 | | |
1095 | | /* |
1096 | | * mdc is the digest ctx that we want, unless there are attributes, in |
1097 | | * which case the digest is the signed attributes |
1098 | | */ |
1099 | 2.25k | if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc)) |
1100 | 0 | goto err; |
1101 | | |
1102 | 2.25k | sk = si->auth_attr; |
1103 | 2.25k | if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) { |
1104 | 400 | unsigned char md_dat[EVP_MAX_MD_SIZE]; |
1105 | 400 | unsigned int md_len; |
1106 | 400 | int alen; |
1107 | 400 | const ASN1_OCTET_STRING *message_digest; |
1108 | | |
1109 | 400 | if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len)) |
1110 | 17 | goto err; |
1111 | 383 | message_digest = PKCS7_digest_from_attributes(sk); |
1112 | 383 | if (!message_digest) { |
1113 | 21 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); |
1114 | 21 | goto err; |
1115 | 21 | } |
1116 | 362 | if ((ASN1_STRING_get_length(message_digest) != md_len) |
1117 | 297 | || (memcmp(ASN1_STRING_get0_data(message_digest), md_dat, md_len))) { |
1118 | 131 | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DIGEST_FAILURE); |
1119 | 131 | ret = -1; |
1120 | 131 | goto err; |
1121 | 131 | } |
1122 | | |
1123 | 231 | md = EVP_MD_fetch(libctx, OBJ_nid2sn(md_type), propq); |
1124 | | |
1125 | 231 | if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) { |
1126 | 1 | goto err; |
1127 | 1 | } |
1128 | | |
1129 | 230 | alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf, |
1130 | 230 | ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY)); |
1131 | 230 | if (alen <= 0 || abuf == NULL) { |
1132 | 0 | ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB); |
1133 | 0 | ret = -1; |
1134 | 0 | goto err; |
1135 | 0 | } |
1136 | 230 | if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen)) |
1137 | 0 | goto err; |
1138 | 230 | } |
1139 | | |
1140 | 2.08k | os = si->enc_digest; |
1141 | 2.08k | pkey = X509_get0_pubkey(signer); |
1142 | 2.08k | if (pkey == NULL) { |
1143 | 68 | ret = -1; |
1144 | 68 | goto err; |
1145 | 68 | } |
1146 | | |
1147 | 2.02k | const unsigned char *sig_data = ASN1_STRING_get0_data(os); |
1148 | 2.02k | size_t sig_len = ASN1_STRING_get_length(os); |
1149 | 2.02k | if (sig_len > INT_MAX) { |
1150 | 0 | ret = -1; |
1151 | 0 | goto err; |
1152 | 0 | } |
1153 | 2.02k | i = EVP_VerifyFinal_ex(mdc_tmp, sig_data, (int)sig_len, pkey, libctx, propq); |
1154 | 2.02k | if (i <= 0) { |
1155 | 1.89k | ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE); |
1156 | 1.89k | ret = -1; |
1157 | 1.89k | goto err; |
1158 | 1.89k | } |
1159 | 125 | ret = 1; |
1160 | 2.38k | err: |
1161 | 2.38k | OPENSSL_free(abuf); |
1162 | 2.38k | EVP_MD_CTX_free(mdc_tmp); |
1163 | 2.38k | EVP_MD_free(md); |
1164 | 2.38k | return ret; |
1165 | 125 | } |
1166 | | |
1167 | | PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx) |
1168 | 0 | { |
1169 | 0 | STACK_OF(PKCS7_RECIP_INFO) *rsk; |
1170 | 0 | PKCS7_RECIP_INFO *ri; |
1171 | 0 | int i; |
1172 | |
|
1173 | 0 | i = OBJ_obj2nid(p7->type); |
1174 | 0 | if (i != NID_pkcs7_signedAndEnveloped) |
1175 | 0 | return NULL; |
1176 | 0 | if (p7->d.signed_and_enveloped == NULL) |
1177 | 0 | return NULL; |
1178 | 0 | rsk = p7->d.signed_and_enveloped->recipientinfo; |
1179 | 0 | if (rsk == NULL) |
1180 | 0 | return NULL; |
1181 | 0 | if (idx < 0 || sk_PKCS7_RECIP_INFO_num(rsk) <= idx) |
1182 | 0 | return NULL; |
1183 | 0 | ri = sk_PKCS7_RECIP_INFO_value(rsk, idx); |
1184 | 0 | return ri->issuer_and_serial; |
1185 | 0 | } |
1186 | | |
1187 | | const ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid) |
1188 | 225 | { |
1189 | 225 | return get_attribute(si->auth_attr, nid); |
1190 | 225 | } |
1191 | | |
1192 | | const ASN1_TYPE *PKCS7_get_attribute(const PKCS7_SIGNER_INFO *si, int nid) |
1193 | 0 | { |
1194 | 0 | return get_attribute(si->unauth_attr, nid); |
1195 | 0 | } |
1196 | | |
1197 | | static const ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid) |
1198 | 608 | { |
1199 | 608 | int idx = X509at_get_attr_by_NID(sk, nid, -1); |
1200 | | |
1201 | 608 | if (idx < 0) |
1202 | 170 | return NULL; |
1203 | 438 | return X509_ATTRIBUTE_get0_type(X509at_get_attr(sk, idx), 0); |
1204 | 608 | } |
1205 | | |
1206 | | const ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk) |
1207 | 383 | { |
1208 | 383 | const ASN1_TYPE *astype; |
1209 | 383 | if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL) |
1210 | 12 | return NULL; |
1211 | 371 | if (astype->type != V_ASN1_OCTET_STRING) |
1212 | 9 | return NULL; |
1213 | 362 | return astype->value.octet_string; |
1214 | 371 | } |
1215 | | |
1216 | | int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, |
1217 | | STACK_OF(X509_ATTRIBUTE) *sk) |
1218 | 0 | { |
1219 | 0 | sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free); |
1220 | 0 | p7si->auth_attr = sk_X509_ATTRIBUTE_deep_copy(sk, X509_ATTRIBUTE_dup, X509_ATTRIBUTE_free); |
1221 | 0 | if (p7si->auth_attr == NULL) |
1222 | 0 | return 0; |
1223 | 0 | return 1; |
1224 | 0 | } |
1225 | | |
1226 | | int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si, |
1227 | | STACK_OF(X509_ATTRIBUTE) *sk) |
1228 | 0 | { |
1229 | 0 | sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free); |
1230 | 0 | p7si->unauth_attr = sk_X509_ATTRIBUTE_deep_copy(sk, X509_ATTRIBUTE_dup, X509_ATTRIBUTE_free); |
1231 | 0 | if (p7si->unauth_attr == NULL) |
1232 | 0 | return 0; |
1233 | 0 | return 1; |
1234 | 0 | } |
1235 | | |
1236 | | int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, |
1237 | | void *value) |
1238 | 0 | { |
1239 | 0 | return add_attribute(&(p7si->auth_attr), nid, atrtype, value); |
1240 | 0 | } |
1241 | | |
1242 | | int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, |
1243 | | void *value) |
1244 | 0 | { |
1245 | 0 | return add_attribute(&(p7si->unauth_attr), nid, atrtype, value); |
1246 | 0 | } |
1247 | | |
1248 | | static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype, |
1249 | | void *value) |
1250 | 0 | { |
1251 | 0 | X509_ATTRIBUTE *attr = NULL; |
1252 | 0 | int i, n; |
1253 | |
|
1254 | 0 | if (*sk == NULL) { |
1255 | 0 | if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL) |
1256 | 0 | return 0; |
1257 | 0 | } |
1258 | 0 | n = sk_X509_ATTRIBUTE_num(*sk); |
1259 | 0 | for (i = 0; i < n; i++) { |
1260 | 0 | attr = sk_X509_ATTRIBUTE_value(*sk, i); |
1261 | 0 | if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) |
1262 | 0 | goto end; |
1263 | 0 | } |
1264 | 0 | if (!sk_X509_ATTRIBUTE_push(*sk, NULL)) |
1265 | 0 | return 0; |
1266 | | |
1267 | 0 | end: |
1268 | 0 | attr = X509_ATTRIBUTE_create(nid, atrtype, value); |
1269 | 0 | if (attr == NULL) { |
1270 | 0 | if (i == n) |
1271 | 0 | sk_X509_ATTRIBUTE_pop(*sk); |
1272 | 0 | return 0; |
1273 | 0 | } |
1274 | 0 | X509_ATTRIBUTE_free(sk_X509_ATTRIBUTE_value(*sk, i)); |
1275 | 0 | (void)sk_X509_ATTRIBUTE_set(*sk, i, attr); |
1276 | 0 | return 1; |
1277 | 0 | } |