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