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