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