1 /* crypto/cms/cms_smime.c */
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
56 #include <openssl/asn1t.h>
57 #include <openssl/x509.h>
58 #include <openssl/x509v3.h>
59 #include <openssl/err.h>
60 #include <openssl/cms.h>
62 #include "asn1_locl.h"
64 static BIO
*cms_get_text_bio(BIO
*out
, unsigned int flags
)
68 rbio
= BIO_new(BIO_s_null());
69 else if (flags
& CMS_TEXT
) {
70 rbio
= BIO_new(BIO_s_mem());
71 BIO_set_mem_eof_return(rbio
, 0);
77 static int cms_copy_content(BIO
*out
, BIO
*in
, unsigned int flags
)
79 unsigned char buf
[4096];
83 tmpout
= cms_get_text_bio(out
, flags
);
86 CMSerr(CMS_F_CMS_COPY_CONTENT
, ERR_R_MALLOC_FAILURE
);
90 /* Read all content through chain to process digest, decrypt etc */
92 i
= BIO_read(in
, buf
, sizeof(buf
));
94 if (BIO_method_type(in
) == BIO_TYPE_CIPHER
) {
95 if (!BIO_get_cipher_status(in
))
103 if (tmpout
&& (BIO_write(tmpout
, buf
, i
) != i
))
107 if (flags
& CMS_TEXT
) {
108 if (!SMIME_text(tmpout
, out
)) {
109 CMSerr(CMS_F_CMS_COPY_CONTENT
, CMS_R_SMIME_TEXT_ERROR
);
117 if (tmpout
&& (tmpout
!= out
))
123 static int check_content(CMS_ContentInfo
*cms
)
125 ASN1_OCTET_STRING
**pos
= CMS_get0_content(cms
);
127 CMSerr(CMS_F_CHECK_CONTENT
, CMS_R_NO_CONTENT
);
133 static void do_free_upto(BIO
*f
, BIO
*upto
)
142 while (f
&& f
!= upto
);
147 int CMS_data(CMS_ContentInfo
*cms
, BIO
*out
, unsigned int flags
)
151 if (OBJ_obj2nid(CMS_get0_type(cms
)) != NID_pkcs7_data
) {
152 CMSerr(CMS_F_CMS_DATA
, CMS_R_TYPE_NOT_DATA
);
155 cont
= CMS_dataInit(cms
, NULL
);
158 r
= cms_copy_content(out
, cont
, flags
);
163 CMS_ContentInfo
*CMS_data_create(BIO
*in
, unsigned int flags
)
165 CMS_ContentInfo
*cms
;
166 cms
= cms_Data_create();
170 if ((flags
& CMS_STREAM
) || CMS_final(cms
, in
, NULL
, flags
))
173 CMS_ContentInfo_free(cms
);
178 int CMS_digest_verify(CMS_ContentInfo
*cms
, BIO
*dcont
, BIO
*out
,
183 if (OBJ_obj2nid(CMS_get0_type(cms
)) != NID_pkcs7_digest
) {
184 CMSerr(CMS_F_CMS_DIGEST_VERIFY
, CMS_R_TYPE_NOT_DIGESTED_DATA
);
188 if (!dcont
&& !check_content(cms
))
191 cont
= CMS_dataInit(cms
, dcont
);
194 r
= cms_copy_content(out
, cont
, flags
);
196 r
= cms_DigestedData_do_final(cms
, cont
, 1);
197 do_free_upto(cont
, dcont
);
201 CMS_ContentInfo
*CMS_digest_create(BIO
*in
, const EVP_MD
*md
,
204 CMS_ContentInfo
*cms
;
207 cms
= cms_DigestedData_create(md
);
211 if (!(flags
& CMS_DETACHED
))
212 CMS_set_detached(cms
, 0);
214 if ((flags
& CMS_STREAM
) || CMS_final(cms
, in
, NULL
, flags
))
217 CMS_ContentInfo_free(cms
);
221 int CMS_EncryptedData_decrypt(CMS_ContentInfo
*cms
,
222 const unsigned char *key
, size_t keylen
,
223 BIO
*dcont
, BIO
*out
, unsigned int flags
)
227 if (OBJ_obj2nid(CMS_get0_type(cms
)) != NID_pkcs7_encrypted
) {
228 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT
,
229 CMS_R_TYPE_NOT_ENCRYPTED_DATA
);
233 if (!dcont
&& !check_content(cms
))
236 if (CMS_EncryptedData_set1_key(cms
, NULL
, key
, keylen
) <= 0)
238 cont
= CMS_dataInit(cms
, dcont
);
241 r
= cms_copy_content(out
, cont
, flags
);
242 do_free_upto(cont
, dcont
);
246 CMS_ContentInfo
*CMS_EncryptedData_encrypt(BIO
*in
, const EVP_CIPHER
*cipher
,
247 const unsigned char *key
,
248 size_t keylen
, unsigned int flags
)
250 CMS_ContentInfo
*cms
;
252 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT
, CMS_R_NO_CIPHER
);
255 cms
= CMS_ContentInfo_new();
258 if (!CMS_EncryptedData_set1_key(cms
, cipher
, key
, keylen
))
261 if (!(flags
& CMS_DETACHED
))
262 CMS_set_detached(cms
, 0);
264 if ((flags
& (CMS_STREAM
| CMS_PARTIAL
))
265 || CMS_final(cms
, in
, NULL
, flags
))
268 CMS_ContentInfo_free(cms
);
272 static int cms_signerinfo_verify_cert(CMS_SignerInfo
*si
,
274 STACK_OF(X509
) *certs
,
275 STACK_OF(X509_CRL
) *crls
,
281 CMS_SignerInfo_get0_algs(si
, NULL
, &signer
, NULL
, NULL
);
282 if (!X509_STORE_CTX_init(&ctx
, store
, signer
, certs
)) {
283 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT
, CMS_R_STORE_INIT_ERROR
);
286 X509_STORE_CTX_set_default(&ctx
, "smime_sign");
288 X509_STORE_CTX_set0_crls(&ctx
, crls
);
290 i
= X509_verify_cert(&ctx
);
292 j
= X509_STORE_CTX_get_error(&ctx
);
293 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT
,
294 CMS_R_CERTIFICATE_VERIFY_ERROR
);
295 ERR_add_error_data(2, "Verify error:",
296 X509_verify_cert_error_string(j
));
301 X509_STORE_CTX_cleanup(&ctx
);
306 int CMS_verify(CMS_ContentInfo
*cms
, STACK_OF(X509
) *certs
,
307 X509_STORE
*store
, BIO
*dcont
, BIO
*out
, unsigned int flags
)
310 STACK_OF(CMS_SignerInfo
) *sinfos
;
311 STACK_OF(X509
) *cms_certs
= NULL
;
312 STACK_OF(X509_CRL
) *crls
= NULL
;
314 int i
, scount
= 0, ret
= 0;
315 BIO
*cmsbio
= NULL
, *tmpin
= NULL
, *tmpout
= NULL
;
317 if (!dcont
&& !check_content(cms
))
319 if (dcont
&& !(flags
& CMS_BINARY
)) {
320 const ASN1_OBJECT
*coid
= CMS_get0_eContentType(cms
);
321 if (OBJ_obj2nid(coid
) == NID_id_ct_asciiTextWithCRLF
)
322 flags
|= CMS_ASCIICRLF
;
325 /* Attempt to find all signer certificates */
327 sinfos
= CMS_get0_SignerInfos(cms
);
329 if (sk_CMS_SignerInfo_num(sinfos
) <= 0) {
330 CMSerr(CMS_F_CMS_VERIFY
, CMS_R_NO_SIGNERS
);
334 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
335 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
336 CMS_SignerInfo_get0_algs(si
, NULL
, &signer
, NULL
, NULL
);
341 if (scount
!= sk_CMS_SignerInfo_num(sinfos
))
342 scount
+= CMS_set1_signers_certs(cms
, certs
, flags
);
344 if (scount
!= sk_CMS_SignerInfo_num(sinfos
)) {
345 CMSerr(CMS_F_CMS_VERIFY
, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND
);
349 /* Attempt to verify all signers certs */
351 if (!(flags
& CMS_NO_SIGNER_CERT_VERIFY
)) {
352 cms_certs
= CMS_get1_certs(cms
);
353 if (!(flags
& CMS_NOCRL
))
354 crls
= CMS_get1_crls(cms
);
355 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
356 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
357 if (!cms_signerinfo_verify_cert(si
, store
,
358 cms_certs
, crls
, flags
))
363 /* Attempt to verify all SignerInfo signed attribute signatures */
365 if (!(flags
& CMS_NO_ATTR_VERIFY
)) {
366 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
367 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
368 if (CMS_signed_get_attr_count(si
) < 0)
370 if (CMS_SignerInfo_verify(si
) <= 0)
376 * Performance optimization: if the content is a memory BIO then store
377 * its contents in a temporary read only memory BIO. This avoids
378 * potentially large numbers of slow copies of data which will occur when
379 * reading from a read write memory BIO when signatures are calculated.
382 if (dcont
&& (BIO_method_type(dcont
) == BIO_TYPE_MEM
)) {
385 len
= BIO_get_mem_data(dcont
, &ptr
);
386 tmpin
= BIO_new_mem_buf(ptr
, len
);
388 CMSerr(CMS_F_CMS_VERIFY
, ERR_R_MALLOC_FAILURE
);
394 * If not binary mode and detached generate digests by *writing* through
395 * the BIO. That makes it possible to canonicalise the input.
397 if (!(flags
& SMIME_BINARY
) && dcont
) {
399 * Create output BIO so we can either handle text or to ensure
400 * included content doesn't override detached content.
402 tmpout
= cms_get_text_bio(out
, flags
);
404 CMSerr(CMS_F_CMS_VERIFY
, ERR_R_MALLOC_FAILURE
);
407 cmsbio
= CMS_dataInit(cms
, tmpout
);
411 * Don't use SMIME_TEXT for verify: it adds headers and we want to
414 SMIME_crlf_copy(dcont
, cmsbio
, flags
& ~SMIME_TEXT
);
416 if (flags
& CMS_TEXT
) {
417 if (!SMIME_text(tmpout
, out
)) {
418 CMSerr(CMS_F_CMS_VERIFY
, CMS_R_SMIME_TEXT_ERROR
);
423 cmsbio
= CMS_dataInit(cms
, tmpin
);
427 if (!cms_copy_content(out
, cmsbio
, flags
))
431 if (!(flags
& CMS_NO_CONTENT_VERIFY
)) {
432 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
433 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
434 if (CMS_SignerInfo_verify_content(si
, cmsbio
) <= 0) {
435 CMSerr(CMS_F_CMS_VERIFY
, CMS_R_CONTENT_VERIFY_ERROR
);
444 if (!(flags
& SMIME_BINARY
) && dcont
) {
445 do_free_upto(cmsbio
, tmpout
);
450 if (dcont
&& (tmpin
== dcont
))
451 do_free_upto(cmsbio
, dcont
);
453 BIO_free_all(cmsbio
);
456 if (tmpout
&& out
!= tmpout
)
457 BIO_free_all(tmpout
);
460 sk_X509_pop_free(cms_certs
, X509_free
);
462 sk_X509_CRL_pop_free(crls
, X509_CRL_free
);
467 int CMS_verify_receipt(CMS_ContentInfo
*rcms
, CMS_ContentInfo
*ocms
,
468 STACK_OF(X509
) *certs
,
469 X509_STORE
*store
, unsigned int flags
)
472 flags
&= ~(CMS_DETACHED
| CMS_TEXT
);
473 r
= CMS_verify(rcms
, certs
, store
, NULL
, NULL
, flags
);
476 return cms_Receipt_verify(rcms
, ocms
);
479 CMS_ContentInfo
*CMS_sign(X509
*signcert
, EVP_PKEY
*pkey
,
480 STACK_OF(X509
) *certs
, BIO
*data
,
483 CMS_ContentInfo
*cms
;
486 cms
= CMS_ContentInfo_new();
487 if (!cms
|| !CMS_SignedData_init(cms
))
489 if (flags
& CMS_ASCIICRLF
490 && !CMS_set1_eContentType(cms
,
491 OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF
)))
494 if (pkey
&& !CMS_add1_signer(cms
, signcert
, pkey
, NULL
, flags
)) {
495 CMSerr(CMS_F_CMS_SIGN
, CMS_R_ADD_SIGNER_ERROR
);
499 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
500 X509
*x
= sk_X509_value(certs
, i
);
501 if (!CMS_add1_cert(cms
, x
))
505 if (!(flags
& CMS_DETACHED
))
506 CMS_set_detached(cms
, 0);
508 if ((flags
& (CMS_STREAM
| CMS_PARTIAL
))
509 || CMS_final(cms
, data
, NULL
, flags
))
515 CMSerr(CMS_F_CMS_SIGN
, ERR_R_MALLOC_FAILURE
);
519 CMS_ContentInfo_free(cms
);
523 CMS_ContentInfo
*CMS_sign_receipt(CMS_SignerInfo
*si
,
524 X509
*signcert
, EVP_PKEY
*pkey
,
525 STACK_OF(X509
) *certs
, unsigned int flags
)
527 CMS_SignerInfo
*rct_si
;
528 CMS_ContentInfo
*cms
= NULL
;
529 ASN1_OCTET_STRING
**pos
, *os
;
530 BIO
*rct_cont
= NULL
;
533 flags
&= ~(CMS_STREAM
| CMS_TEXT
);
534 /* Not really detached but avoids content being allocated */
535 flags
|= CMS_PARTIAL
| CMS_BINARY
| CMS_DETACHED
;
536 if (!pkey
|| !signcert
) {
537 CMSerr(CMS_F_CMS_SIGN_RECEIPT
, CMS_R_NO_KEY_OR_CERT
);
541 /* Initialize signed data */
543 cms
= CMS_sign(NULL
, NULL
, certs
, NULL
, flags
);
547 /* Set inner content type to signed receipt */
548 if (!CMS_set1_eContentType(cms
, OBJ_nid2obj(NID_id_smime_ct_receipt
)))
551 rct_si
= CMS_add1_signer(cms
, signcert
, pkey
, NULL
, flags
);
553 CMSerr(CMS_F_CMS_SIGN_RECEIPT
, CMS_R_ADD_SIGNER_ERROR
);
557 os
= cms_encode_Receipt(si
);
562 /* Set content to digest */
563 rct_cont
= BIO_new_mem_buf(os
->data
, os
->length
);
567 /* Add msgSigDigest attribute */
569 if (!cms_msgSigDigest_add1(rct_si
, si
))
572 /* Finalize structure */
573 if (!CMS_final(cms
, rct_cont
, NULL
, flags
))
576 /* Set embedded content */
577 pos
= CMS_get0_content(cms
);
587 CMS_ContentInfo_free(cms
);
592 CMS_ContentInfo
*CMS_encrypt(STACK_OF(X509
) *certs
, BIO
*data
,
593 const EVP_CIPHER
*cipher
, unsigned int flags
)
595 CMS_ContentInfo
*cms
;
598 cms
= CMS_EnvelopedData_create(cipher
);
601 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
602 recip
= sk_X509_value(certs
, i
);
603 if (!CMS_add1_recipient_cert(cms
, recip
, flags
)) {
604 CMSerr(CMS_F_CMS_ENCRYPT
, CMS_R_RECIPIENT_ERROR
);
609 if (!(flags
& CMS_DETACHED
))
610 CMS_set_detached(cms
, 0);
612 if ((flags
& (CMS_STREAM
| CMS_PARTIAL
))
613 || CMS_final(cms
, data
, NULL
, flags
))
619 CMSerr(CMS_F_CMS_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
622 CMS_ContentInfo_free(cms
);
626 static int cms_kari_set1_pkey(CMS_ContentInfo
*cms
, CMS_RecipientInfo
*ri
,
627 EVP_PKEY
*pk
, X509
*cert
)
630 STACK_OF(CMS_RecipientEncryptedKey
) *reks
;
631 CMS_RecipientEncryptedKey
*rek
;
632 reks
= CMS_RecipientInfo_kari_get0_reks(ri
);
635 for (i
= 0; i
< sk_CMS_RecipientEncryptedKey_num(reks
); i
++) {
637 rek
= sk_CMS_RecipientEncryptedKey_value(reks
, i
);
638 if (CMS_RecipientEncryptedKey_cert_cmp(rek
, cert
))
640 CMS_RecipientInfo_kari_set0_pkey(ri
, pk
);
641 rv
= CMS_RecipientInfo_kari_decrypt(cms
, ri
, rek
);
642 CMS_RecipientInfo_kari_set0_pkey(ri
, NULL
);
650 int CMS_decrypt_set1_pkey(CMS_ContentInfo
*cms
, EVP_PKEY
*pk
, X509
*cert
)
652 STACK_OF(CMS_RecipientInfo
) *ris
;
653 CMS_RecipientInfo
*ri
;
655 int debug
= 0, match_ri
= 0;
656 ris
= CMS_get0_RecipientInfos(cms
);
658 debug
= cms
->d
.envelopedData
->encryptedContentInfo
->debug
;
659 ri_type
= cms_pkey_get_ri_type(pk
);
660 if (ri_type
== CMS_RECIPINFO_NONE
) {
661 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY
,
662 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);
666 for (i
= 0; i
< sk_CMS_RecipientInfo_num(ris
); i
++) {
667 ri
= sk_CMS_RecipientInfo_value(ris
, i
);
668 if (CMS_RecipientInfo_type(ri
) != ri_type
)
671 if (ri_type
== CMS_RECIPINFO_AGREE
) {
672 r
= cms_kari_set1_pkey(cms
, ri
, pk
, cert
);
679 * If we have a cert try matching RecipientInfo otherwise try them
682 else if (!cert
|| !CMS_RecipientInfo_ktri_cert_cmp(ri
, cert
)) {
683 CMS_RecipientInfo_set0_pkey(ri
, pk
);
684 r
= CMS_RecipientInfo_decrypt(cms
, ri
);
685 CMS_RecipientInfo_set0_pkey(ri
, NULL
);
688 * If not debugging clear any error and return success to
689 * avoid leaking of information useful to MMA
697 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY
, CMS_R_DECRYPT_ERROR
);
701 * If no cert and not debugging don't leave loop after first
702 * successful decrypt. Always attempt to decrypt all recipients
703 * to avoid leaking timing of a successful decrypt.
705 else if (r
> 0 && debug
)
709 /* If no cert and not debugging always return success */
710 if (match_ri
&& !cert
&& !debug
) {
715 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY
, CMS_R_NO_MATCHING_RECIPIENT
);
720 int CMS_decrypt_set1_key(CMS_ContentInfo
*cms
,
721 unsigned char *key
, size_t keylen
,
722 unsigned char *id
, size_t idlen
)
724 STACK_OF(CMS_RecipientInfo
) *ris
;
725 CMS_RecipientInfo
*ri
;
727 ris
= CMS_get0_RecipientInfos(cms
);
728 for (i
= 0; i
< sk_CMS_RecipientInfo_num(ris
); i
++) {
729 ri
= sk_CMS_RecipientInfo_value(ris
, i
);
730 if (CMS_RecipientInfo_type(ri
) != CMS_RECIPINFO_KEK
)
734 * If we have an id try matching RecipientInfo otherwise try them
737 if (!id
|| (CMS_RecipientInfo_kekri_id_cmp(ri
, id
, idlen
) == 0)) {
738 CMS_RecipientInfo_set0_key(ri
, key
, keylen
);
739 r
= CMS_RecipientInfo_decrypt(cms
, ri
);
740 CMS_RecipientInfo_set0_key(ri
, NULL
, 0);
744 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY
, CMS_R_DECRYPT_ERROR
);
751 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY
, CMS_R_NO_MATCHING_RECIPIENT
);
756 int CMS_decrypt_set1_password(CMS_ContentInfo
*cms
,
757 unsigned char *pass
, ossl_ssize_t passlen
)
759 STACK_OF(CMS_RecipientInfo
) *ris
;
760 CMS_RecipientInfo
*ri
;
762 ris
= CMS_get0_RecipientInfos(cms
);
763 for (i
= 0; i
< sk_CMS_RecipientInfo_num(ris
); i
++) {
764 ri
= sk_CMS_RecipientInfo_value(ris
, i
);
765 if (CMS_RecipientInfo_type(ri
) != CMS_RECIPINFO_PASS
)
767 CMS_RecipientInfo_set0_password(ri
, pass
, passlen
);
768 r
= CMS_RecipientInfo_decrypt(cms
, ri
);
769 CMS_RecipientInfo_set0_password(ri
, NULL
, 0);
774 CMSerr(CMS_F_CMS_DECRYPT_SET1_PASSWORD
, CMS_R_NO_MATCHING_RECIPIENT
);
779 int CMS_decrypt(CMS_ContentInfo
*cms
, EVP_PKEY
*pk
, X509
*cert
,
780 BIO
*dcont
, BIO
*out
, unsigned int flags
)
784 if (OBJ_obj2nid(CMS_get0_type(cms
)) != NID_pkcs7_enveloped
) {
785 CMSerr(CMS_F_CMS_DECRYPT
, CMS_R_TYPE_NOT_ENVELOPED_DATA
);
788 if (!dcont
&& !check_content(cms
))
790 if (flags
& CMS_DEBUG_DECRYPT
)
791 cms
->d
.envelopedData
->encryptedContentInfo
->debug
= 1;
793 cms
->d
.envelopedData
->encryptedContentInfo
->debug
= 0;
794 if (!pk
&& !cert
&& !dcont
&& !out
)
796 if (pk
&& !CMS_decrypt_set1_pkey(cms
, pk
, cert
))
798 cont
= CMS_dataInit(cms
, dcont
);
801 r
= cms_copy_content(out
, cont
, flags
);
802 do_free_upto(cont
, dcont
);
806 int CMS_final(CMS_ContentInfo
*cms
, BIO
*data
, BIO
*dcont
, unsigned int flags
)
810 if (!(cmsbio
= CMS_dataInit(cms
, dcont
))) {
811 CMSerr(CMS_F_CMS_FINAL
, ERR_R_MALLOC_FAILURE
);
815 SMIME_crlf_copy(data
, cmsbio
, flags
);
817 (void)BIO_flush(cmsbio
);
819 if (!CMS_dataFinal(cms
, cmsbio
)) {
820 CMSerr(CMS_F_CMS_FINAL
, CMS_R_CMS_DATAFINAL_ERROR
);
827 do_free_upto(cmsbio
, dcont
);
835 int CMS_uncompress(CMS_ContentInfo
*cms
, BIO
*dcont
, BIO
*out
,
840 if (OBJ_obj2nid(CMS_get0_type(cms
)) != NID_id_smime_ct_compressedData
) {
841 CMSerr(CMS_F_CMS_UNCOMPRESS
, CMS_R_TYPE_NOT_COMPRESSED_DATA
);
845 if (!dcont
&& !check_content(cms
))
848 cont
= CMS_dataInit(cms
, dcont
);
851 r
= cms_copy_content(out
, cont
, flags
);
852 do_free_upto(cont
, dcont
);
856 CMS_ContentInfo
*CMS_compress(BIO
*in
, int comp_nid
, unsigned int flags
)
858 CMS_ContentInfo
*cms
;
860 comp_nid
= NID_zlib_compression
;
861 cms
= cms_CompressedData_create(comp_nid
);
865 if (!(flags
& CMS_DETACHED
))
866 CMS_set_detached(cms
, 0);
868 if ((flags
& CMS_STREAM
) || CMS_final(cms
, in
, NULL
, flags
))
871 CMS_ContentInfo_free(cms
);
877 int CMS_uncompress(CMS_ContentInfo
*cms
, BIO
*dcont
, BIO
*out
,
880 CMSerr(CMS_F_CMS_UNCOMPRESS
, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
884 CMS_ContentInfo
*CMS_compress(BIO
*in
, int comp_nid
, unsigned int flags
)
886 CMSerr(CMS_F_CMS_COMPRESS
, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);