]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/cms/cms_smime.c
cf12c5b78580ea88e0aaa162716dad16e3d2a4d0
[thirdparty/openssl.git] / crypto / cms / cms_smime.c
1 /*
2 * Copyright 2008-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 #include "internal/cryptlib.h"
11 #include <openssl/asn1t.h>
12 #include <openssl/x509.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/err.h>
15 #include <openssl/cms.h>
16 #include "cms_local.h"
17 #include "crypto/asn1.h"
18
19 static BIO *cms_get_text_bio(BIO *out, unsigned int flags)
20 {
21 BIO *rbio;
22
23 if (out == NULL)
24 rbio = BIO_new(BIO_s_null());
25 else if (flags & CMS_TEXT) {
26 rbio = BIO_new(BIO_s_mem());
27 BIO_set_mem_eof_return(rbio, 0);
28 } else
29 rbio = out;
30 return rbio;
31 }
32
33 static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
34 {
35 unsigned char buf[4096];
36 int r = 0, i;
37 BIO *tmpout;
38
39 tmpout = cms_get_text_bio(out, flags);
40
41 if (tmpout == NULL) {
42 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
43 goto err;
44 }
45
46 /* Read all content through chain to process digest, decrypt etc */
47 for (;;) {
48 i = BIO_read(in, buf, sizeof(buf));
49 if (i <= 0) {
50 if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
51 if (BIO_get_cipher_status(in) <= 0)
52 goto err;
53 }
54 if (i < 0)
55 goto err;
56 break;
57 }
58
59 if (tmpout != NULL && (BIO_write(tmpout, buf, i) != i))
60 goto err;
61 }
62
63 if (flags & CMS_TEXT) {
64 if (!SMIME_text(tmpout, out)) {
65 ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR);
66 goto err;
67 }
68 }
69
70 r = 1;
71 err:
72 if (tmpout != out)
73 BIO_free(tmpout);
74 return r;
75
76 }
77
78 static int check_content(CMS_ContentInfo *cms)
79 {
80 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
81
82 if (pos == NULL || *pos == NULL) {
83 ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
84 return 0;
85 }
86 return 1;
87 }
88
89 static void do_free_upto(BIO *f, BIO *upto)
90 {
91 if (upto != NULL) {
92 BIO *tbio;
93
94 do {
95 tbio = BIO_pop(f);
96 BIO_free(f);
97 f = tbio;
98 } while (f != NULL && f != upto);
99 } else {
100 BIO_free_all(f);
101 }
102 }
103
104 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
105 {
106 BIO *cont;
107 int r;
108
109 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
110 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_DATA);
111 return 0;
112 }
113 cont = CMS_dataInit(cms, NULL);
114 if (cont == NULL)
115 return 0;
116 r = cms_copy_content(out, cont, flags);
117 BIO_free_all(cont);
118 return r;
119 }
120
121 CMS_ContentInfo *CMS_data_create_ex(BIO *in, unsigned int flags,
122 OSSL_LIB_CTX *libctx, const char *propq)
123 {
124 CMS_ContentInfo *cms = ossl_cms_Data_create(libctx, propq);
125
126 if (cms == NULL)
127 return NULL;
128
129 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
130 return cms;
131
132 CMS_ContentInfo_free(cms);
133 return NULL;
134 }
135
136 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
137 {
138 return CMS_data_create_ex(in, flags, NULL, NULL);
139 }
140
141 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
142 unsigned int flags)
143 {
144 BIO *cont;
145 int r;
146
147 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
148 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_DIGESTED_DATA);
149 return 0;
150 }
151
152 if (dcont == NULL && !check_content(cms))
153 return 0;
154
155 cont = CMS_dataInit(cms, dcont);
156 if (cont == NULL)
157 return 0;
158
159 r = cms_copy_content(out, cont, flags);
160 if (r)
161 r = ossl_cms_DigestedData_do_final(cms, cont, 1);
162 do_free_upto(cont, dcont);
163 return r;
164 }
165
166 CMS_ContentInfo *CMS_digest_create_ex(BIO *in, const EVP_MD *md,
167 unsigned int flags, OSSL_LIB_CTX *ctx,
168 const char *propq)
169 {
170 CMS_ContentInfo *cms;
171
172 /*
173 * Because the EVP_MD is cached and can be a legacy algorithm, we
174 * cannot fetch the algorithm if it isn't supplied.
175 */
176 if (md == NULL)
177 md = EVP_sha1();
178 cms = ossl_cms_DigestedData_create(md, ctx, propq);
179 if (cms == NULL)
180 return NULL;
181
182 if (!(flags & CMS_DETACHED))
183 CMS_set_detached(cms, 0);
184
185 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
186 return cms;
187
188 CMS_ContentInfo_free(cms);
189 return NULL;
190 }
191
192 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
193 unsigned int flags)
194 {
195 return CMS_digest_create_ex(in, md, flags, NULL, NULL);
196 }
197
198 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
199 const unsigned char *key, size_t keylen,
200 BIO *dcont, BIO *out, unsigned int flags)
201 {
202 BIO *cont;
203 int r;
204
205 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
206 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENCRYPTED_DATA);
207 return 0;
208 }
209
210 if (dcont == NULL && !check_content(cms))
211 return 0;
212
213 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
214 return 0;
215 cont = CMS_dataInit(cms, dcont);
216 if (cont == NULL)
217 return 0;
218 r = cms_copy_content(out, cont, flags);
219 do_free_upto(cont, dcont);
220 return r;
221 }
222
223 CMS_ContentInfo *CMS_EncryptedData_encrypt_ex(BIO *in, const EVP_CIPHER *cipher,
224 const unsigned char *key,
225 size_t keylen, unsigned int flags,
226 OSSL_LIB_CTX *libctx,
227 const char *propq)
228 {
229 CMS_ContentInfo *cms;
230
231 if (cipher == NULL) {
232 ERR_raise(ERR_LIB_CMS, CMS_R_NO_CIPHER);
233 return NULL;
234 }
235 cms = CMS_ContentInfo_new_ex(libctx, propq);
236 if (cms == NULL)
237 return NULL;
238 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
239 return NULL;
240
241 if (!(flags & CMS_DETACHED))
242 CMS_set_detached(cms, 0);
243
244 if ((flags & (CMS_STREAM | CMS_PARTIAL))
245 || CMS_final(cms, in, NULL, flags))
246 return cms;
247
248 CMS_ContentInfo_free(cms);
249 return NULL;
250 }
251
252 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
253 const unsigned char *key,
254 size_t keylen, unsigned int flags)
255 {
256 return CMS_EncryptedData_encrypt_ex(in, cipher, key, keylen, flags, NULL,
257 NULL);
258 }
259
260 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
261 X509_STORE *store,
262 STACK_OF(X509) *untrusted,
263 STACK_OF(X509_CRL) *crls,
264 STACK_OF(X509) **chain,
265 const CMS_CTX *cms_ctx)
266 {
267 X509_STORE_CTX *ctx;
268 X509 *signer;
269 int i, j, r = 0;
270
271 ctx = X509_STORE_CTX_new_ex(ossl_cms_ctx_get0_libctx(cms_ctx),
272 ossl_cms_ctx_get0_propq(cms_ctx));
273 if (ctx == NULL) {
274 ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
275 goto err;
276 }
277 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
278 if (!X509_STORE_CTX_init(ctx, store, signer, untrusted)) {
279 ERR_raise(ERR_LIB_CMS, CMS_R_STORE_INIT_ERROR);
280 goto err;
281 }
282 X509_STORE_CTX_set_default(ctx, "smime_sign");
283 if (crls != NULL)
284 X509_STORE_CTX_set0_crls(ctx, crls);
285
286 i = X509_verify_cert(ctx);
287 if (i <= 0) {
288 j = X509_STORE_CTX_get_error(ctx);
289 ERR_raise_data(ERR_LIB_CMS, CMS_R_CERTIFICATE_VERIFY_ERROR,
290 "Verify error: %s", X509_verify_cert_error_string(j));
291 goto err;
292 }
293 r = 1;
294
295 /* also send back the trust chain when required */
296 if (chain != NULL)
297 *chain = X509_STORE_CTX_get1_chain(ctx);
298 err:
299 X509_STORE_CTX_free(ctx);
300 return r;
301
302 }
303
304 /* This strongly overlaps with PKCS7_verify() */
305 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
306 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
307 {
308 CMS_SignerInfo *si;
309 STACK_OF(CMS_SignerInfo) *sinfos;
310 STACK_OF(X509) *cms_certs = NULL;
311 STACK_OF(X509_CRL) *crls = NULL;
312 STACK_OF(X509) **si_chains = NULL;
313 X509 *signer;
314 int i, scount = 0, ret = 0;
315 BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
316 int cadesVerify = (flags & CMS_CADES) != 0;
317 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
318
319 if (dcont == NULL && !check_content(cms))
320 return 0;
321 if (dcont != NULL && !(flags & CMS_BINARY)) {
322 const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
323
324 if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
325 flags |= CMS_ASCIICRLF;
326 }
327
328 /* Attempt to find all signer certificates */
329
330 sinfos = CMS_get0_SignerInfos(cms);
331
332 if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
333 ERR_raise(ERR_LIB_CMS, CMS_R_NO_SIGNERS);
334 goto err;
335 }
336
337 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
338 si = sk_CMS_SignerInfo_value(sinfos, i);
339 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
340 if (signer != NULL)
341 scount++;
342 }
343
344 if (scount != sk_CMS_SignerInfo_num(sinfos))
345 scount += CMS_set1_signers_certs(cms, certs, flags);
346
347 if (scount != sk_CMS_SignerInfo_num(sinfos)) {
348 ERR_raise(ERR_LIB_CMS, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
349 goto err;
350 }
351
352 /* Attempt to verify all signers certs */
353 /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
354
355 if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
356 if (cadesVerify) {
357 /* Certificate trust chain is required to check CAdES signature */
358 si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
359 if (si_chains == NULL)
360 goto err;
361 }
362 cms_certs = CMS_get1_certs(cms);
363 if (!(flags & CMS_NOCRL))
364 crls = CMS_get1_crls(cms);
365 for (i = 0; i < scount; i++) {
366 si = sk_CMS_SignerInfo_value(sinfos, i);
367
368 if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls,
369 si_chains ? &si_chains[i] : NULL,
370 ctx))
371 goto err;
372 }
373 }
374
375 /* Attempt to verify all SignerInfo signed attribute signatures */
376
377 if ((flags & CMS_NO_ATTR_VERIFY) == 0 || cadesVerify) {
378 for (i = 0; i < scount; i++) {
379 si = sk_CMS_SignerInfo_value(sinfos, i);
380 if (CMS_signed_get_attr_count(si) < 0)
381 continue;
382 if (CMS_SignerInfo_verify(si) <= 0)
383 goto err;
384 if (cadesVerify) {
385 STACK_OF(X509) *si_chain = si_chains ? si_chains[i] : NULL;
386
387 if (ossl_cms_check_signing_certs(si, si_chain) <= 0)
388 goto err;
389 }
390 }
391 }
392
393 /*
394 * Performance optimization: if the content is a memory BIO then store
395 * its contents in a temporary read only memory BIO. This avoids
396 * potentially large numbers of slow copies of data which will occur when
397 * reading from a read write memory BIO when signatures are calculated.
398 */
399
400 if (dcont != NULL && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
401 char *ptr;
402 long len;
403
404 len = BIO_get_mem_data(dcont, &ptr);
405 tmpin = (len == 0) ? dcont : BIO_new_mem_buf(ptr, len);
406 if (tmpin == NULL) {
407 ERR_raise(ERR_LIB_CMS, ERR_R_BIO_LIB);
408 goto err2;
409 }
410 } else {
411 tmpin = dcont;
412 }
413 /*
414 * If not binary mode and detached generate digests by *writing* through
415 * the BIO. That makes it possible to canonicalise the input.
416 */
417 if (!(flags & SMIME_BINARY) && dcont) {
418 /*
419 * Create output BIO so we can either handle text or to ensure
420 * included content doesn't override detached content.
421 */
422 tmpout = cms_get_text_bio(out, flags);
423 if (tmpout == NULL) {
424 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
425 goto err;
426 }
427 cmsbio = CMS_dataInit(cms, tmpout);
428 if (cmsbio == NULL)
429 goto err;
430 /*
431 * Don't use SMIME_TEXT for verify: it adds headers and we want to
432 * remove them.
433 */
434 if (!SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT))
435 goto err;
436
437 if (flags & CMS_TEXT) {
438 if (!SMIME_text(tmpout, out)) {
439 ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR);
440 goto err;
441 }
442 }
443 } else {
444 cmsbio = CMS_dataInit(cms, tmpin);
445 if (cmsbio == NULL)
446 goto err;
447
448 if (!cms_copy_content(out, cmsbio, flags))
449 goto err;
450
451 }
452 if (!(flags & CMS_NO_CONTENT_VERIFY)) {
453 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
454 si = sk_CMS_SignerInfo_value(sinfos, i);
455 if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
456 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_VERIFY_ERROR);
457 goto err;
458 }
459 }
460 }
461
462 ret = 1;
463 err:
464 if (!(flags & SMIME_BINARY) && dcont) {
465 do_free_upto(cmsbio, tmpout);
466 if (tmpin != dcont)
467 BIO_free(tmpin);
468 } else {
469 if (dcont && (tmpin == dcont))
470 do_free_upto(cmsbio, dcont);
471 else
472 BIO_free_all(cmsbio);
473 }
474
475 if (out != tmpout)
476 BIO_free_all(tmpout);
477
478 err2:
479 if (si_chains != NULL) {
480 for (i = 0; i < scount; ++i)
481 OSSL_STACK_OF_X509_free(si_chains[i]);
482 OPENSSL_free(si_chains);
483 }
484 OSSL_STACK_OF_X509_free(cms_certs);
485 sk_X509_CRL_pop_free(crls, X509_CRL_free);
486
487 return ret;
488 }
489
490 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
491 STACK_OF(X509) *certs,
492 X509_STORE *store, unsigned int flags)
493 {
494 int r;
495
496 flags &= ~(CMS_DETACHED | CMS_TEXT);
497 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
498 if (r <= 0)
499 return r;
500 return ossl_cms_Receipt_verify(rcms, ocms);
501 }
502
503 CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
504 STACK_OF(X509) *certs, BIO *data,
505 unsigned int flags, OSSL_LIB_CTX *libctx,
506 const char *propq)
507 {
508 CMS_ContentInfo *cms;
509 int i;
510
511 cms = CMS_ContentInfo_new_ex(libctx, propq);
512 if (cms == NULL || !CMS_SignedData_init(cms)) {
513 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
514 goto err;
515 }
516 if (flags & CMS_ASCIICRLF
517 && !CMS_set1_eContentType(cms,
518 OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF))) {
519 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
520 goto err;
521 }
522
523 if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
524 ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
525 goto err;
526 }
527
528 for (i = 0; i < sk_X509_num(certs); i++) {
529 X509 *x = sk_X509_value(certs, i);
530
531 if (!CMS_add1_cert(cms, x)) {
532 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
533 goto err;
534 }
535 }
536
537 if (!(flags & CMS_DETACHED))
538 CMS_set_detached(cms, 0);
539
540 if ((flags & (CMS_STREAM | CMS_PARTIAL))
541 || CMS_final(cms, data, NULL, flags))
542 return cms;
543 else
544 goto err;
545
546 err:
547 CMS_ContentInfo_free(cms);
548 return NULL;
549 }
550
551 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
552 BIO *data, unsigned int flags)
553 {
554 return CMS_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
555 }
556
557 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
558 X509 *signcert, EVP_PKEY *pkey,
559 STACK_OF(X509) *certs, unsigned int flags)
560 {
561 CMS_SignerInfo *rct_si;
562 CMS_ContentInfo *cms = NULL;
563 ASN1_OCTET_STRING **pos, *os;
564 BIO *rct_cont = NULL;
565 int r = 0;
566 const CMS_CTX *ctx = si->cms_ctx;
567
568 flags &= ~(CMS_STREAM | CMS_TEXT);
569 /* Not really detached but avoids content being allocated */
570 flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
571 if (pkey == NULL || signcert == NULL) {
572 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY_OR_CERT);
573 return NULL;
574 }
575
576 /* Initialize signed data */
577
578 cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags,
579 ossl_cms_ctx_get0_libctx(ctx),
580 ossl_cms_ctx_get0_propq(ctx));
581 if (cms == NULL)
582 goto err;
583
584 /* Set inner content type to signed receipt */
585 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
586 goto err;
587
588 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
589 if (!rct_si) {
590 ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
591 goto err;
592 }
593
594 os = ossl_cms_encode_Receipt(si);
595 if (os == NULL)
596 goto err;
597
598 /* Set content to digest */
599 rct_cont = BIO_new_mem_buf(os->data, os->length);
600 if (rct_cont == NULL)
601 goto err;
602
603 /* Add msgSigDigest attribute */
604
605 if (!ossl_cms_msgSigDigest_add1(rct_si, si))
606 goto err;
607
608 /* Finalize structure */
609 if (!CMS_final(cms, rct_cont, NULL, flags))
610 goto err;
611
612 /* Set embedded content */
613 pos = CMS_get0_content(cms);
614 if (pos == NULL)
615 goto err;
616 *pos = os;
617
618 r = 1;
619
620 err:
621 BIO_free(rct_cont);
622 if (r)
623 return cms;
624 CMS_ContentInfo_free(cms);
625 return NULL;
626
627 }
628
629 CMS_ContentInfo *CMS_encrypt_ex(STACK_OF(X509) *certs, BIO *data,
630 const EVP_CIPHER *cipher, unsigned int flags,
631 OSSL_LIB_CTX *libctx, const char *propq)
632 {
633 CMS_ContentInfo *cms;
634 int i;
635 X509 *recip;
636
637
638 cms = (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
639 ? CMS_AuthEnvelopedData_create_ex(cipher, libctx, propq)
640 : CMS_EnvelopedData_create_ex(cipher, libctx, propq);
641 if (cms == NULL) {
642 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
643 goto err;
644 }
645 for (i = 0; i < sk_X509_num(certs); i++) {
646 recip = sk_X509_value(certs, i);
647 if (!CMS_add1_recipient_cert(cms, recip, flags)) {
648 ERR_raise(ERR_LIB_CMS, CMS_R_RECIPIENT_ERROR);
649 goto err;
650 }
651 }
652
653 if (!(flags & CMS_DETACHED))
654 CMS_set_detached(cms, 0);
655
656 if ((flags & (CMS_STREAM | CMS_PARTIAL))
657 || CMS_final(cms, data, NULL, flags))
658 return cms;
659 else
660 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
661
662 err:
663 CMS_ContentInfo_free(cms);
664 return NULL;
665 }
666
667 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
668 const EVP_CIPHER *cipher, unsigned int flags)
669 {
670 return CMS_encrypt_ex(certs, data, cipher, flags, NULL, NULL);
671 }
672
673 static int cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms,
674 CMS_RecipientInfo *ri,
675 EVP_PKEY *pk, X509 *cert, X509 *peer)
676 {
677 int i;
678 STACK_OF(CMS_RecipientEncryptedKey) *reks;
679 CMS_RecipientEncryptedKey *rek;
680
681 reks = CMS_RecipientInfo_kari_get0_reks(ri);
682 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
683 int rv;
684
685 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
686 if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
687 continue;
688 CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, peer);
689 rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
690 CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
691 if (rv > 0)
692 return 1;
693 return cert == NULL ? 0 : -1;
694 }
695 return 0;
696 }
697
698 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
699 {
700 return CMS_decrypt_set1_pkey_and_peer(cms, pk, cert, NULL);
701 }
702
703 int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk,
704 X509 *cert, X509 *peer)
705 {
706 STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
707 CMS_RecipientInfo *ri;
708 int i, r, cms_pkey_ri_type;
709 int debug = 0, match_ri = 0;
710 CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
711
712 /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
713 if (ec != NULL) {
714 OPENSSL_clear_free(ec->key, ec->keylen);
715 ec->key = NULL;
716 ec->keylen = 0;
717 }
718
719 if (ris != NULL && ec != NULL)
720 debug = ec->debug;
721
722 cms_pkey_ri_type = ossl_cms_pkey_get_ri_type(pk);
723 if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
724 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
725 return 0;
726 }
727
728 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
729 int ri_type;
730
731 ri = sk_CMS_RecipientInfo_value(ris, i);
732 ri_type = CMS_RecipientInfo_type(ri);
733 if (!ossl_cms_pkey_is_ri_type_supported(pk, ri_type))
734 continue;
735 match_ri = 1;
736 if (ri_type == CMS_RECIPINFO_AGREE) {
737 r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
738 if (r > 0)
739 return 1;
740 if (r < 0)
741 return 0;
742 }
743 /* If we have a cert, try matching RecipientInfo, else try them all */
744 else if (cert == NULL || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
745 EVP_PKEY_up_ref(pk);
746 CMS_RecipientInfo_set0_pkey(ri, pk);
747 r = CMS_RecipientInfo_decrypt(cms, ri);
748 CMS_RecipientInfo_set0_pkey(ri, NULL);
749 if (cert != NULL) {
750 /*
751 * If not debugging clear any error and return success to
752 * avoid leaking of information useful to MMA
753 */
754 if (!debug) {
755 ERR_clear_error();
756 return 1;
757 }
758 if (r > 0)
759 return 1;
760 ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
761 return 0;
762 }
763 /*
764 * If no cert and not debugging don't leave loop after first
765 * successful decrypt. Always attempt to decrypt all recipients
766 * to avoid leaking timing of a successful decrypt.
767 */
768 else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
769 return 1;
770 }
771 }
772 /* If no cert, key transport and not debugging always return success */
773 if (cert == NULL
774 && cms_pkey_ri_type == CMS_RECIPINFO_TRANS
775 && match_ri
776 && !debug) {
777 ERR_clear_error();
778 return 1;
779 }
780
781 if (!match_ri)
782 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
783 return 0;
784
785 }
786
787 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
788 unsigned char *key, size_t keylen,
789 const unsigned char *id, size_t idlen)
790 {
791 STACK_OF(CMS_RecipientInfo) *ris;
792 CMS_RecipientInfo *ri;
793 int i, r, match_ri = 0;
794
795 ris = CMS_get0_RecipientInfos(cms);
796 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
797 ri = sk_CMS_RecipientInfo_value(ris, i);
798 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
799 continue;
800
801 /* If we have an id, try matching RecipientInfo, else try them all */
802 if (id == NULL
803 || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
804 match_ri = 1;
805 CMS_RecipientInfo_set0_key(ri, key, keylen);
806 r = CMS_RecipientInfo_decrypt(cms, ri);
807 CMS_RecipientInfo_set0_key(ri, NULL, 0);
808 if (r > 0)
809 return 1;
810 if (id != NULL) {
811 ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
812 return 0;
813 }
814 ERR_clear_error();
815 }
816 }
817
818 if (!match_ri)
819 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
820 return 0;
821
822 }
823
824 int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
825 unsigned char *pass, ossl_ssize_t passlen)
826 {
827 STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
828 CMS_RecipientInfo *ri;
829 int i, r, match_ri = 0;
830 CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
831
832 /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
833 if (ec != NULL) {
834 OPENSSL_clear_free(ec->key, ec->keylen);
835 ec->key = NULL;
836 ec->keylen = 0;
837 }
838
839 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
840 ri = sk_CMS_RecipientInfo_value(ris, i);
841 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
842 continue;
843
844 /* Must try each PasswordRecipientInfo */
845 match_ri = 1;
846 CMS_RecipientInfo_set0_password(ri, pass, passlen);
847 r = CMS_RecipientInfo_decrypt(cms, ri);
848 CMS_RecipientInfo_set0_password(ri, NULL, 0);
849 if (r > 0)
850 return 1;
851 }
852
853 if (!match_ri)
854 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
855 return 0;
856
857 }
858
859 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
860 BIO *dcont, BIO *out, unsigned int flags)
861 {
862 int r;
863 BIO *cont;
864 CMS_EncryptedContentInfo *ec;
865 int nid = OBJ_obj2nid(CMS_get0_type(cms));
866
867 if (nid != NID_pkcs7_enveloped
868 && nid != NID_id_smime_ct_authEnvelopedData) {
869 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENVELOPED_DATA);
870 return 0;
871 }
872 if (dcont == NULL && !check_content(cms))
873 return 0;
874 ec = ossl_cms_get0_env_enc_content(cms);
875 ec->debug = (flags & CMS_DEBUG_DECRYPT) != 0;
876 ec->havenocert = cert == NULL;
877 if (pk == NULL && cert == NULL && dcont == NULL && out == NULL)
878 return 1;
879 if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert))
880 return 0;
881 cont = CMS_dataInit(cms, dcont);
882 if (cont == NULL)
883 return 0;
884 r = cms_copy_content(out, cont, flags);
885 do_free_upto(cont, dcont);
886 return r;
887 }
888
889 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
890 {
891 BIO *cmsbio;
892 int ret = 0;
893
894 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
895 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
896 return 0;
897 }
898
899 if (!SMIME_crlf_copy(data, cmsbio, flags)) {
900 goto err;
901 }
902
903 (void)BIO_flush(cmsbio);
904
905 if (!CMS_dataFinal(cms, cmsbio)) {
906 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
907 goto err;
908 }
909
910 ret = 1;
911
912 err:
913 do_free_upto(cmsbio, dcont);
914
915 return ret;
916
917 }
918
919 int CMS_final_digest(CMS_ContentInfo *cms,
920 const unsigned char *md, unsigned int mdlen,
921 BIO *dcont, unsigned int flags)
922 {
923 BIO *cmsbio;
924 int ret = 0;
925
926 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
927 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
928 return 0;
929 }
930
931 (void)BIO_flush(cmsbio);
932
933 if (!ossl_cms_DataFinal(cms, cmsbio, md, mdlen)) {
934 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
935 goto err;
936 }
937 ret = 1;
938
939 err:
940 do_free_upto(cmsbio, dcont);
941 return ret;
942 }
943
944 #ifndef OPENSSL_NO_ZLIB
945
946 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
947 unsigned int flags)
948 {
949 BIO *cont;
950 int r;
951
952 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
953 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
954 return 0;
955 }
956
957 if (dcont == NULL && !check_content(cms))
958 return 0;
959
960 cont = CMS_dataInit(cms, dcont);
961 if (cont == NULL)
962 return 0;
963 r = cms_copy_content(out, cont, flags);
964 do_free_upto(cont, dcont);
965 return r;
966 }
967
968 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
969 {
970 CMS_ContentInfo *cms;
971
972 if (comp_nid <= 0)
973 comp_nid = NID_zlib_compression;
974 cms = ossl_cms_CompressedData_create(comp_nid, NULL, NULL);
975 if (cms == NULL)
976 return NULL;
977
978 if (!(flags & CMS_DETACHED))
979 CMS_set_detached(cms, 0);
980
981 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
982 return cms;
983
984 CMS_ContentInfo_free(cms);
985 return NULL;
986 }
987
988 #else
989
990 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
991 unsigned int flags)
992 {
993 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
994 return 0;
995 }
996
997 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
998 {
999 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1000 return NULL;
1001 }
1002
1003 #endif