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