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