]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
454afd98 | 2 | * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved. |
5a9a4b29 | 3 | * |
b7617a3a | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
62867571 RS |
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 | |
5a9a4b29 DSH |
8 | */ |
9 | ||
10 | /* Simple PKCS#7 processing functions */ | |
11 | ||
12 | #include <stdio.h> | |
b39fc560 | 13 | #include "internal/cryptlib.h" |
5a9a4b29 DSH |
14 | #include <openssl/x509.h> |
15 | #include <openssl/x509v3.h> | |
16 | ||
8e704858 RS |
17 | #define BUFFERSIZE 4096 |
18 | ||
852c2ed2 RS |
19 | DEFINE_STACK_OF(X509) |
20 | DEFINE_STACK_OF(X509_ATTRIBUTE) | |
21 | DEFINE_STACK_OF(X509_ALGOR) | |
22 | DEFINE_STACK_OF(PKCS7_SIGNER_INFO) | |
23 | ||
55311921 DSH |
24 | static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si); |
25 | ||
5a9a4b29 | 26 | PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, |
0f113f3e | 27 | BIO *data, int flags) |
5a9a4b29 | 28 | { |
0f113f3e MC |
29 | PKCS7 *p7; |
30 | int i; | |
31 | ||
75ebbd9a | 32 | if ((p7 = PKCS7_new()) == NULL) { |
0f113f3e MC |
33 | PKCS7err(PKCS7_F_PKCS7_SIGN, ERR_R_MALLOC_FAILURE); |
34 | return NULL; | |
35 | } | |
36 | ||
37 | if (!PKCS7_set_type(p7, NID_pkcs7_signed)) | |
38 | goto err; | |
39 | ||
40 | if (!PKCS7_content_new(p7, NID_pkcs7_data)) | |
41 | goto err; | |
42 | ||
43 | if (pkey && !PKCS7_sign_add_signer(p7, signcert, pkey, NULL, flags)) { | |
44 | PKCS7err(PKCS7_F_PKCS7_SIGN, PKCS7_R_PKCS7_ADD_SIGNER_ERROR); | |
45 | goto err; | |
46 | } | |
47 | ||
48 | if (!(flags & PKCS7_NOCERTS)) { | |
49 | for (i = 0; i < sk_X509_num(certs); i++) { | |
50 | if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i))) | |
51 | goto err; | |
52 | } | |
53 | } | |
54 | ||
55 | if (flags & PKCS7_DETACHED) | |
56 | PKCS7_set_detached(p7, 1); | |
57 | ||
58 | if (flags & (PKCS7_STREAM | PKCS7_PARTIAL)) | |
59 | return p7; | |
60 | ||
61 | if (PKCS7_final(p7, data, flags)) | |
62 | return p7; | |
63 | ||
64 | err: | |
65 | PKCS7_free(p7); | |
66 | return NULL; | |
60f20632 DSH |
67 | } |
68 | ||
69 | int PKCS7_final(PKCS7 *p7, BIO *data, int flags) | |
0f113f3e MC |
70 | { |
71 | BIO *p7bio; | |
72 | int ret = 0; | |
8e704858 | 73 | |
75ebbd9a | 74 | if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) { |
0f113f3e MC |
75 | PKCS7err(PKCS7_F_PKCS7_FINAL, ERR_R_MALLOC_FAILURE); |
76 | return 0; | |
77 | } | |
60f20632 | 78 | |
0f113f3e | 79 | SMIME_crlf_copy(data, p7bio, flags); |
994df5a2 | 80 | |
0f113f3e | 81 | (void)BIO_flush(p7bio); |
60f20632 | 82 | |
0f113f3e MC |
83 | if (!PKCS7_dataFinal(p7, p7bio)) { |
84 | PKCS7err(PKCS7_F_PKCS7_FINAL, PKCS7_R_PKCS7_DATASIGN); | |
85 | goto err; | |
86 | } | |
5a9a4b29 | 87 | |
0f113f3e | 88 | ret = 1; |
60f20632 | 89 | |
0f113f3e MC |
90 | err: |
91 | BIO_free_all(p7bio); | |
60f20632 | 92 | |
0f113f3e | 93 | return ret; |
27068df7 | 94 | |
0f113f3e | 95 | } |
27068df7 | 96 | |
60f20632 | 97 | /* Check to see if a cipher exists and if so add S/MIME capabilities */ |
27068df7 | 98 | |
60f20632 | 99 | static int add_cipher_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg) |
0f113f3e MC |
100 | { |
101 | if (EVP_get_cipherbynid(nid)) | |
102 | return PKCS7_simple_smimecap(sk, nid, arg); | |
103 | return 1; | |
104 | } | |
5a9a4b29 | 105 | |
61e5ec4b | 106 | static int add_digest_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg) |
0f113f3e MC |
107 | { |
108 | if (EVP_get_digestbynid(nid)) | |
109 | return PKCS7_simple_smimecap(sk, nid, arg); | |
110 | return 1; | |
111 | } | |
61e5ec4b | 112 | |
60f20632 | 113 | PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert, |
0f113f3e MC |
114 | EVP_PKEY *pkey, const EVP_MD *md, |
115 | int flags) | |
116 | { | |
117 | PKCS7_SIGNER_INFO *si = NULL; | |
118 | STACK_OF(X509_ALGOR) *smcap = NULL; | |
119 | if (!X509_check_private_key(signcert, pkey)) { | |
120 | PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, | |
121 | PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); | |
122 | return NULL; | |
123 | } | |
124 | ||
75ebbd9a | 125 | if ((si = PKCS7_add_signature(p7, signcert, pkey, md)) == NULL) { |
0f113f3e MC |
126 | PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, |
127 | PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR); | |
128 | return NULL; | |
129 | } | |
130 | ||
131 | if (!(flags & PKCS7_NOCERTS)) { | |
132 | if (!PKCS7_add_certificate(p7, signcert)) | |
133 | goto err; | |
134 | } | |
135 | ||
136 | if (!(flags & PKCS7_NOATTR)) { | |
137 | if (!PKCS7_add_attrib_content_type(si, NULL)) | |
138 | goto err; | |
139 | /* Add SMIMECapabilities */ | |
140 | if (!(flags & PKCS7_NOSMIMECAP)) { | |
75ebbd9a | 141 | if ((smcap = sk_X509_ALGOR_new_null()) == NULL) { |
0f113f3e MC |
142 | PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, ERR_R_MALLOC_FAILURE); |
143 | goto err; | |
144 | } | |
145 | if (!add_cipher_smcap(smcap, NID_aes_256_cbc, -1) | |
c58f3e42 MC |
146 | || !add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1) |
147 | || !add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1) | |
0f113f3e MC |
148 | || !add_digest_smcap(smcap, NID_id_GostR3411_94, -1) |
149 | || !add_cipher_smcap(smcap, NID_id_Gost28147_89, -1) | |
150 | || !add_cipher_smcap(smcap, NID_aes_192_cbc, -1) | |
151 | || !add_cipher_smcap(smcap, NID_aes_128_cbc, -1) | |
152 | || !add_cipher_smcap(smcap, NID_des_ede3_cbc, -1) | |
153 | || !add_cipher_smcap(smcap, NID_rc2_cbc, 128) | |
154 | || !add_cipher_smcap(smcap, NID_rc2_cbc, 64) | |
155 | || !add_cipher_smcap(smcap, NID_des_cbc, -1) | |
156 | || !add_cipher_smcap(smcap, NID_rc2_cbc, 40) | |
157 | || !PKCS7_add_attrib_smimecap(si, smcap)) | |
158 | goto err; | |
159 | sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); | |
160 | smcap = NULL; | |
161 | } | |
162 | if (flags & PKCS7_REUSE_DIGEST) { | |
163 | if (!pkcs7_copy_existing_digest(p7, si)) | |
164 | goto err; | |
165 | if (!(flags & PKCS7_PARTIAL) && !PKCS7_SIGNER_INFO_sign(si)) | |
166 | goto err; | |
167 | } | |
168 | } | |
169 | return si; | |
170 | err: | |
222561fe | 171 | sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); |
0f113f3e MC |
172 | return NULL; |
173 | } | |
174 | ||
175 | /* | |
176 | * Search for a digest matching SignerInfo digest type and if found copy | |
177 | * across. | |
55311921 DSH |
178 | */ |
179 | ||
180 | static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si) | |
0f113f3e MC |
181 | { |
182 | int i; | |
183 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | |
184 | PKCS7_SIGNER_INFO *sitmp; | |
185 | ASN1_OCTET_STRING *osdig = NULL; | |
186 | sinfos = PKCS7_get_signer_info(p7); | |
187 | for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) { | |
188 | sitmp = sk_PKCS7_SIGNER_INFO_value(sinfos, i); | |
189 | if (si == sitmp) | |
190 | break; | |
191 | if (sk_X509_ATTRIBUTE_num(sitmp->auth_attr) <= 0) | |
192 | continue; | |
193 | if (!OBJ_cmp(si->digest_alg->algorithm, sitmp->digest_alg->algorithm)) { | |
194 | osdig = PKCS7_digest_from_attributes(sitmp->auth_attr); | |
195 | break; | |
196 | } | |
197 | ||
198 | } | |
199 | ||
200 | if (osdig) | |
201 | return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length); | |
202 | ||
203 | PKCS7err(PKCS7_F_PKCS7_COPY_EXISTING_DIGEST, | |
204 | PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND); | |
205 | return 0; | |
206 | } | |
55311921 | 207 | |
5a9a4b29 | 208 | int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, |
0f113f3e | 209 | BIO *indata, BIO *out, int flags) |
5a9a4b29 | 210 | { |
0f113f3e MC |
211 | STACK_OF(X509) *signers; |
212 | X509 *signer; | |
213 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | |
214 | PKCS7_SIGNER_INFO *si; | |
f0e0fd51 | 215 | X509_STORE_CTX *cert_ctx = NULL; |
8e704858 | 216 | char *buf = NULL; |
0f113f3e | 217 | int i, j = 0, k, ret = 0; |
55500ea7 AG |
218 | BIO *p7bio = NULL; |
219 | BIO *tmpin = NULL, *tmpout = NULL; | |
0f113f3e | 220 | |
12a765a5 | 221 | if (p7 == NULL) { |
0f113f3e MC |
222 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_INVALID_NULL_POINTER); |
223 | return 0; | |
224 | } | |
225 | ||
226 | if (!PKCS7_type_is_signed(p7)) { | |
227 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_WRONG_CONTENT_TYPE); | |
228 | return 0; | |
229 | } | |
230 | ||
231 | /* Check for no data and no content: no data to verify signature */ | |
232 | if (PKCS7_get_detached(p7) && !indata) { | |
233 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_NO_CONTENT); | |
234 | return 0; | |
235 | } | |
02a938c9 | 236 | |
6b2ebe43 RS |
237 | if (flags & PKCS7_NO_DUAL_CONTENT) { |
238 | /* | |
239 | * This was originally "#if 0" because we thought that only old broken | |
240 | * Netscape did this. It turns out that Authenticode uses this kind | |
241 | * of "extended" PKCS7 format, and things like UEFI secure boot and | |
242 | * tools like osslsigncode need it. In Authenticode the verification | |
243 | * process is different, but the existing PKCs7 verification works. | |
244 | */ | |
245 | if (!PKCS7_get_detached(p7) && indata) { | |
246 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_CONTENT_AND_DATA_PRESENT); | |
247 | return 0; | |
248 | } | |
0f113f3e | 249 | } |
5a9a4b29 | 250 | |
0f113f3e MC |
251 | sinfos = PKCS7_get_signer_info(p7); |
252 | ||
253 | if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) { | |
254 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_NO_SIGNATURES_ON_DATA); | |
255 | return 0; | |
256 | } | |
257 | ||
258 | signers = PKCS7_get0_signers(p7, certs, flags); | |
0f113f3e MC |
259 | if (!signers) |
260 | return 0; | |
261 | ||
262 | /* Now verify the certificates */ | |
263 | ||
f0e0fd51 RS |
264 | cert_ctx = X509_STORE_CTX_new(); |
265 | if (cert_ctx == NULL) | |
266 | goto err; | |
0f113f3e MC |
267 | if (!(flags & PKCS7_NOVERIFY)) |
268 | for (k = 0; k < sk_X509_num(signers); k++) { | |
269 | signer = sk_X509_value(signers, k); | |
270 | if (!(flags & PKCS7_NOCHAIN)) { | |
f0e0fd51 | 271 | if (!X509_STORE_CTX_init(cert_ctx, store, signer, |
0f113f3e MC |
272 | p7->d.sign->cert)) { |
273 | PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_X509_LIB); | |
55500ea7 | 274 | goto err; |
0f113f3e | 275 | } |
f0e0fd51 RS |
276 | X509_STORE_CTX_set_default(cert_ctx, "smime_sign"); |
277 | } else if (!X509_STORE_CTX_init(cert_ctx, store, signer, NULL)) { | |
0f113f3e | 278 | PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_X509_LIB); |
55500ea7 | 279 | goto err; |
0f113f3e MC |
280 | } |
281 | if (!(flags & PKCS7_NOCRL)) | |
f0e0fd51 RS |
282 | X509_STORE_CTX_set0_crls(cert_ctx, p7->d.sign->crl); |
283 | i = X509_verify_cert(cert_ctx); | |
0f113f3e | 284 | if (i <= 0) |
f0e0fd51 | 285 | j = X509_STORE_CTX_get_error(cert_ctx); |
36c6f0ad | 286 | X509_STORE_CTX_cleanup(cert_ctx); |
0f113f3e MC |
287 | if (i <= 0) { |
288 | PKCS7err(PKCS7_F_PKCS7_VERIFY, | |
289 | PKCS7_R_CERTIFICATE_VERIFY_ERROR); | |
290 | ERR_add_error_data(2, "Verify error:", | |
291 | X509_verify_cert_error_string(j)); | |
55500ea7 | 292 | goto err; |
0f113f3e MC |
293 | } |
294 | /* Check for revocation status here */ | |
295 | } | |
296 | ||
297 | /* | |
298 | * Performance optimization: if the content is a memory BIO then store | |
299 | * its contents in a temporary read only memory BIO. This avoids | |
300 | * potentially large numbers of slow copies of data which will occur when | |
301 | * reading from a read write memory BIO when signatures are calculated. | |
302 | */ | |
303 | ||
304 | if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM)) { | |
305 | char *ptr; | |
306 | long len; | |
307 | len = BIO_get_mem_data(indata, &ptr); | |
308 | tmpin = BIO_new_mem_buf(ptr, len); | |
309 | if (tmpin == NULL) { | |
310 | PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE); | |
55500ea7 | 311 | goto err; |
0f113f3e MC |
312 | } |
313 | } else | |
314 | tmpin = indata; | |
315 | ||
75ebbd9a | 316 | if ((p7bio = PKCS7_dataInit(p7, tmpin)) == NULL) |
0f113f3e MC |
317 | goto err; |
318 | ||
319 | if (flags & PKCS7_TEXT) { | |
75ebbd9a | 320 | if ((tmpout = BIO_new(BIO_s_mem())) == NULL) { |
0f113f3e MC |
321 | PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE); |
322 | goto err; | |
323 | } | |
324 | BIO_set_mem_eof_return(tmpout, 0); | |
325 | } else | |
326 | tmpout = out; | |
327 | ||
328 | /* We now have to 'read' from p7bio to calculate digests etc. */ | |
8e704858 RS |
329 | if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) { |
330 | PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE); | |
331 | goto err; | |
332 | } | |
0f113f3e | 333 | for (;;) { |
8e704858 | 334 | i = BIO_read(p7bio, buf, BUFFERSIZE); |
0f113f3e MC |
335 | if (i <= 0) |
336 | break; | |
337 | if (tmpout) | |
338 | BIO_write(tmpout, buf, i); | |
339 | } | |
340 | ||
341 | if (flags & PKCS7_TEXT) { | |
342 | if (!SMIME_text(tmpout, out)) { | |
343 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_SMIME_TEXT_ERROR); | |
344 | BIO_free(tmpout); | |
345 | goto err; | |
346 | } | |
347 | BIO_free(tmpout); | |
348 | } | |
349 | ||
350 | /* Now Verify All Signatures */ | |
351 | if (!(flags & PKCS7_NOSIGS)) | |
352 | for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) { | |
353 | si = sk_PKCS7_SIGNER_INFO_value(sinfos, i); | |
354 | signer = sk_X509_value(signers, i); | |
355 | j = PKCS7_signatureVerify(p7bio, p7, si, signer); | |
356 | if (j <= 0) { | |
357 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_SIGNATURE_FAILURE); | |
358 | goto err; | |
359 | } | |
360 | } | |
361 | ||
362 | ret = 1; | |
363 | ||
364 | err: | |
f0e0fd51 | 365 | X509_STORE_CTX_free(cert_ctx); |
8e704858 | 366 | OPENSSL_free(buf); |
0f113f3e MC |
367 | if (tmpin == indata) { |
368 | if (indata) | |
369 | BIO_pop(p7bio); | |
370 | } | |
371 | BIO_free_all(p7bio); | |
0f113f3e | 372 | sk_X509_free(signers); |
0f113f3e | 373 | return ret; |
5a9a4b29 DSH |
374 | } |
375 | ||
0f113f3e MC |
376 | STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, |
377 | int flags) | |
55ec5861 | 378 | { |
0f113f3e MC |
379 | STACK_OF(X509) *signers; |
380 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | |
381 | PKCS7_SIGNER_INFO *si; | |
382 | PKCS7_ISSUER_AND_SERIAL *ias; | |
383 | X509 *signer; | |
384 | int i; | |
385 | ||
12a765a5 | 386 | if (p7 == NULL) { |
0f113f3e MC |
387 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_INVALID_NULL_POINTER); |
388 | return NULL; | |
389 | } | |
390 | ||
391 | if (!PKCS7_type_is_signed(p7)) { | |
392 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_WRONG_CONTENT_TYPE); | |
393 | return NULL; | |
394 | } | |
395 | ||
396 | /* Collect all the signers together */ | |
397 | ||
398 | sinfos = PKCS7_get_signer_info(p7); | |
399 | ||
400 | if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) { | |
401 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_NO_SIGNERS); | |
402 | return 0; | |
403 | } | |
404 | ||
75ebbd9a | 405 | if ((signers = sk_X509_new_null()) == NULL) { |
0f113f3e MC |
406 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, ERR_R_MALLOC_FAILURE); |
407 | return NULL; | |
408 | } | |
409 | ||
410 | for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) { | |
411 | si = sk_PKCS7_SIGNER_INFO_value(sinfos, i); | |
412 | ias = si->issuer_and_serial; | |
413 | signer = NULL; | |
414 | /* If any certificates passed they take priority */ | |
415 | if (certs) | |
416 | signer = X509_find_by_issuer_and_serial(certs, | |
417 | ias->issuer, ias->serial); | |
418 | if (!signer && !(flags & PKCS7_NOINTERN) | |
419 | && p7->d.sign->cert) | |
420 | signer = | |
421 | X509_find_by_issuer_and_serial(p7->d.sign->cert, | |
422 | ias->issuer, ias->serial); | |
423 | if (!signer) { | |
424 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, | |
425 | PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND); | |
426 | sk_X509_free(signers); | |
427 | return 0; | |
428 | } | |
429 | ||
430 | if (!sk_X509_push(signers, signer)) { | |
431 | sk_X509_free(signers); | |
432 | return NULL; | |
433 | } | |
434 | } | |
435 | return signers; | |
55ec5861 DSH |
436 | } |
437 | ||
5a9a4b29 DSH |
438 | /* Build a complete PKCS#7 enveloped data */ |
439 | ||
13588350 | 440 | PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, |
0f113f3e | 441 | int flags) |
5a9a4b29 | 442 | { |
0f113f3e MC |
443 | PKCS7 *p7; |
444 | BIO *p7bio = NULL; | |
445 | int i; | |
446 | X509 *x509; | |
75ebbd9a | 447 | if ((p7 = PKCS7_new()) == NULL) { |
0f113f3e MC |
448 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT, ERR_R_MALLOC_FAILURE); |
449 | return NULL; | |
450 | } | |
451 | ||
452 | if (!PKCS7_set_type(p7, NID_pkcs7_enveloped)) | |
453 | goto err; | |
454 | if (!PKCS7_set_cipher(p7, cipher)) { | |
455 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_ERROR_SETTING_CIPHER); | |
456 | goto err; | |
457 | } | |
458 | ||
459 | for (i = 0; i < sk_X509_num(certs); i++) { | |
460 | x509 = sk_X509_value(certs, i); | |
461 | if (!PKCS7_add_recipient(p7, x509)) { | |
462 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_ERROR_ADDING_RECIPIENT); | |
463 | goto err; | |
464 | } | |
465 | } | |
466 | ||
467 | if (flags & PKCS7_STREAM) | |
468 | return p7; | |
469 | ||
470 | if (PKCS7_final(p7, in, flags)) | |
471 | return p7; | |
472 | ||
473 | err: | |
474 | ||
475 | BIO_free_all(p7bio); | |
476 | PKCS7_free(p7); | |
477 | return NULL; | |
5a9a4b29 DSH |
478 | |
479 | } | |
480 | ||
481 | int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags) | |
482 | { | |
0f113f3e | 483 | BIO *tmpmem; |
29717229 | 484 | int ret = 0, i; |
8e704858 | 485 | char *buf = NULL; |
0f113f3e | 486 | |
12a765a5 | 487 | if (p7 == NULL) { |
0f113f3e MC |
488 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_INVALID_NULL_POINTER); |
489 | return 0; | |
490 | } | |
491 | ||
492 | if (!PKCS7_type_is_enveloped(p7)) { | |
493 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_WRONG_CONTENT_TYPE); | |
494 | return 0; | |
495 | } | |
496 | ||
497 | if (cert && !X509_check_private_key(cert, pkey)) { | |
498 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, | |
499 | PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); | |
500 | return 0; | |
501 | } | |
502 | ||
75ebbd9a | 503 | if ((tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert)) == NULL) { |
0f113f3e MC |
504 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR); |
505 | return 0; | |
506 | } | |
507 | ||
508 | if (flags & PKCS7_TEXT) { | |
509 | BIO *tmpbuf, *bread; | |
510 | /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */ | |
75ebbd9a | 511 | if ((tmpbuf = BIO_new(BIO_f_buffer())) == NULL) { |
0f113f3e MC |
512 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); |
513 | BIO_free_all(tmpmem); | |
514 | return 0; | |
515 | } | |
75ebbd9a | 516 | if ((bread = BIO_push(tmpbuf, tmpmem)) == NULL) { |
0f113f3e MC |
517 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); |
518 | BIO_free_all(tmpbuf); | |
519 | BIO_free_all(tmpmem); | |
520 | return 0; | |
521 | } | |
522 | ret = SMIME_text(bread, data); | |
523 | if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) { | |
524 | if (!BIO_get_cipher_status(tmpmem)) | |
525 | ret = 0; | |
526 | } | |
527 | BIO_free_all(bread); | |
528 | return ret; | |
8e704858 RS |
529 | } |
530 | if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) { | |
531 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); | |
532 | goto err; | |
533 | } | |
534 | for (;;) { | |
535 | i = BIO_read(tmpmem, buf, BUFFERSIZE); | |
536 | if (i <= 0) { | |
537 | ret = 1; | |
538 | if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) { | |
539 | if (!BIO_get_cipher_status(tmpmem)) | |
540 | ret = 0; | |
0f113f3e | 541 | } |
8e704858 RS |
542 | |
543 | break; | |
544 | } | |
545 | if (BIO_write(data, buf, i) != i) { | |
8e704858 | 546 | break; |
0f113f3e | 547 | } |
0f113f3e | 548 | } |
8e704858 RS |
549 | err: |
550 | OPENSSL_free(buf); | |
551 | BIO_free_all(tmpmem); | |
552 | return ret; | |
5a9a4b29 | 553 | } |