NISTZ256: owur'ize.
[openssl.git] / crypto / cms / cms_smime.c
1 /* crypto/cms/cms_smime.c */
2 /*
3  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4  * project.
5  */
6 /* ====================================================================
7  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
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
19  *    distribution.
20  *
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/)"
25  *
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.
30  *
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.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
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  * ====================================================================
53  */
54
55 #include "cryptlib.h"
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>
61 #include "cms_lcl.h"
62 #include "internal/asn1_int.h"
63
64 static BIO *cms_get_text_bio(BIO *out, unsigned int flags)
65 {
66     BIO *rbio;
67     if (out == NULL)
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);
72     } else
73         rbio = out;
74     return rbio;
75 }
76
77 static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
78 {
79     unsigned char buf[4096];
80     int r = 0, i;
81     BIO *tmpout;
82
83     tmpout = cms_get_text_bio(out, flags);
84
85     if (!tmpout) {
86         CMSerr(CMS_F_CMS_COPY_CONTENT, ERR_R_MALLOC_FAILURE);
87         goto err;
88     }
89
90     /* Read all content through chain to process digest, decrypt etc */
91     for (;;) {
92         i = BIO_read(in, buf, sizeof(buf));
93         if (i <= 0) {
94             if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
95                 if (!BIO_get_cipher_status(in))
96                     goto err;
97             }
98             if (i < 0)
99                 goto err;
100             break;
101         }
102
103         if (tmpout && (BIO_write(tmpout, buf, i) != i))
104             goto err;
105     }
106
107     if (flags & CMS_TEXT) {
108         if (!SMIME_text(tmpout, out)) {
109             CMSerr(CMS_F_CMS_COPY_CONTENT, CMS_R_SMIME_TEXT_ERROR);
110             goto err;
111         }
112     }
113
114     r = 1;
115
116  err:
117     if (tmpout != out)
118         BIO_free(tmpout);
119     return r;
120
121 }
122
123 static int check_content(CMS_ContentInfo *cms)
124 {
125     ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
126     if (!pos || !*pos) {
127         CMSerr(CMS_F_CHECK_CONTENT, CMS_R_NO_CONTENT);
128         return 0;
129     }
130     return 1;
131 }
132
133 static void do_free_upto(BIO *f, BIO *upto)
134 {
135     if (upto) {
136         BIO *tbio;
137         do {
138             tbio = BIO_pop(f);
139             BIO_free(f);
140             f = tbio;
141         }
142         while (f && f != upto);
143     } else
144         BIO_free_all(f);
145 }
146
147 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
148 {
149     BIO *cont;
150     int r;
151     if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
152         CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
153         return 0;
154     }
155     cont = CMS_dataInit(cms, NULL);
156     if (!cont)
157         return 0;
158     r = cms_copy_content(out, cont, flags);
159     BIO_free_all(cont);
160     return r;
161 }
162
163 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
164 {
165     CMS_ContentInfo *cms;
166     cms = cms_Data_create();
167     if (!cms)
168         return NULL;
169
170     if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
171         return cms;
172
173     CMS_ContentInfo_free(cms);
174
175     return NULL;
176 }
177
178 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
179                       unsigned int flags)
180 {
181     BIO *cont;
182     int r;
183     if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
184         CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
185         return 0;
186     }
187
188     if (!dcont && !check_content(cms))
189         return 0;
190
191     cont = CMS_dataInit(cms, dcont);
192     if (!cont)
193         return 0;
194     r = cms_copy_content(out, cont, flags);
195     if (r)
196         r = cms_DigestedData_do_final(cms, cont, 1);
197     do_free_upto(cont, dcont);
198     return r;
199 }
200
201 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
202                                    unsigned int flags)
203 {
204     CMS_ContentInfo *cms;
205     if (!md)
206         md = EVP_sha1();
207     cms = cms_DigestedData_create(md);
208     if (!cms)
209         return NULL;
210
211     if (!(flags & CMS_DETACHED))
212         CMS_set_detached(cms, 0);
213
214     if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
215         return cms;
216
217     CMS_ContentInfo_free(cms);
218     return NULL;
219 }
220
221 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
222                               const unsigned char *key, size_t keylen,
223                               BIO *dcont, BIO *out, unsigned int flags)
224 {
225     BIO *cont;
226     int r;
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);
230         return 0;
231     }
232
233     if (!dcont && !check_content(cms))
234         return 0;
235
236     if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
237         return 0;
238     cont = CMS_dataInit(cms, dcont);
239     if (!cont)
240         return 0;
241     r = cms_copy_content(out, cont, flags);
242     do_free_upto(cont, dcont);
243     return r;
244 }
245
246 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
247                                            const unsigned char *key,
248                                            size_t keylen, unsigned int flags)
249 {
250     CMS_ContentInfo *cms;
251     if (!cipher) {
252         CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
253         return NULL;
254     }
255     cms = CMS_ContentInfo_new();
256     if (!cms)
257         return NULL;
258     if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
259         return NULL;
260
261     if (!(flags & CMS_DETACHED))
262         CMS_set_detached(cms, 0);
263
264     if ((flags & (CMS_STREAM | CMS_PARTIAL))
265         || CMS_final(cms, in, NULL, flags))
266         return cms;
267
268     CMS_ContentInfo_free(cms);
269     return NULL;
270 }
271
272 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
273                                       X509_STORE *store,
274                                       STACK_OF(X509) *certs,
275                                       STACK_OF(X509_CRL) *crls,
276                                       unsigned int flags)
277 {
278     X509_STORE_CTX ctx;
279     X509 *signer;
280     int i, j, r = 0;
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);
284         goto err;
285     }
286     X509_STORE_CTX_set_default(&ctx, "smime_sign");
287     if (crls)
288         X509_STORE_CTX_set0_crls(&ctx, crls);
289
290     i = X509_verify_cert(&ctx);
291     if (i <= 0) {
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));
297         goto err;
298     }
299     r = 1;
300  err:
301     X509_STORE_CTX_cleanup(&ctx);
302     return r;
303
304 }
305
306 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
307                X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
308 {
309     CMS_SignerInfo *si;
310     STACK_OF(CMS_SignerInfo) *sinfos;
311     STACK_OF(X509) *cms_certs = NULL;
312     STACK_OF(X509_CRL) *crls = NULL;
313     X509 *signer;
314     int i, scount = 0, ret = 0;
315     BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
316
317     if (!dcont && !check_content(cms))
318         return 0;
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;
323     }
324
325     /* Attempt to find all signer certificates */
326
327     sinfos = CMS_get0_SignerInfos(cms);
328
329     if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
330         CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
331         goto err;
332     }
333
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);
337         if (signer)
338             scount++;
339     }
340
341     if (scount != sk_CMS_SignerInfo_num(sinfos))
342         scount += CMS_set1_signers_certs(cms, certs, flags);
343
344     if (scount != sk_CMS_SignerInfo_num(sinfos)) {
345         CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
346         goto err;
347     }
348
349     /* Attempt to verify all signers certs */
350
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))
359                 goto err;
360         }
361     }
362
363     /* Attempt to verify all SignerInfo signed attribute signatures */
364
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)
369                 continue;
370             if (CMS_SignerInfo_verify(si) <= 0)
371                 goto err;
372         }
373     }
374
375     /*
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.
380      */
381
382     if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
383         char *ptr;
384         long len;
385         len = BIO_get_mem_data(dcont, &ptr);
386         tmpin = BIO_new_mem_buf(ptr, len);
387         if (tmpin == NULL) {
388             CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
389             return 0;
390         }
391     } else
392         tmpin = dcont;
393     /*
394      * If not binary mode and detached generate digests by *writing* through
395      * the BIO. That makes it possible to canonicalise the input.
396      */
397     if (!(flags & SMIME_BINARY) && dcont) {
398         /*
399          * Create output BIO so we can either handle text or to ensure
400          * included content doesn't override detached content.
401          */
402         tmpout = cms_get_text_bio(out, flags);
403         if (!tmpout) {
404             CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
405             goto err;
406         }
407         cmsbio = CMS_dataInit(cms, tmpout);
408         if (!cmsbio)
409             goto err;
410         /*
411          * Don't use SMIME_TEXT for verify: it adds headers and we want to
412          * remove them.
413          */
414         SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT);
415
416         if (flags & CMS_TEXT) {
417             if (!SMIME_text(tmpout, out)) {
418                 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SMIME_TEXT_ERROR);
419                 goto err;
420             }
421         }
422     } else {
423         cmsbio = CMS_dataInit(cms, tmpin);
424         if (!cmsbio)
425             goto err;
426
427         if (!cms_copy_content(out, cmsbio, flags))
428             goto err;
429
430     }
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);
436                 goto err;
437             }
438         }
439     }
440
441     ret = 1;
442
443  err:
444     if (!(flags & SMIME_BINARY) && dcont) {
445         do_free_upto(cmsbio, tmpout);
446         if (tmpin != dcont)
447             BIO_free(tmpin);
448     } else {
449         if (dcont && (tmpin == dcont))
450             do_free_upto(cmsbio, dcont);
451         else
452             BIO_free_all(cmsbio);
453     }
454
455     if (out != tmpout)
456         BIO_free_all(tmpout);
457
458     if (cms_certs)
459         sk_X509_pop_free(cms_certs, X509_free);
460     if (crls)
461         sk_X509_CRL_pop_free(crls, X509_CRL_free);
462
463     return ret;
464 }
465
466 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
467                        STACK_OF(X509) *certs,
468                        X509_STORE *store, unsigned int flags)
469 {
470     int r;
471     flags &= ~(CMS_DETACHED | CMS_TEXT);
472     r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
473     if (r <= 0)
474         return r;
475     return cms_Receipt_verify(rcms, ocms);
476 }
477
478 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey,
479                           STACK_OF(X509) *certs, BIO *data,
480                           unsigned int flags)
481 {
482     CMS_ContentInfo *cms;
483     int i;
484
485     cms = CMS_ContentInfo_new();
486     if (!cms || !CMS_SignedData_init(cms))
487         goto merr;
488     if (flags & CMS_ASCIICRLF
489         && !CMS_set1_eContentType(cms,
490                                   OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
491         goto err;
492
493     if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
494         CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
495         goto err;
496     }
497
498     for (i = 0; i < sk_X509_num(certs); i++) {
499         X509 *x = sk_X509_value(certs, i);
500         if (!CMS_add1_cert(cms, x))
501             goto merr;
502     }
503
504     if (!(flags & CMS_DETACHED))
505         CMS_set_detached(cms, 0);
506
507     if ((flags & (CMS_STREAM | CMS_PARTIAL))
508         || CMS_final(cms, data, NULL, flags))
509         return cms;
510     else
511         goto err;
512
513  merr:
514     CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
515
516  err:
517     if (cms)
518         CMS_ContentInfo_free(cms);
519     return NULL;
520 }
521
522 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
523                                   X509 *signcert, EVP_PKEY *pkey,
524                                   STACK_OF(X509) *certs, unsigned int flags)
525 {
526     CMS_SignerInfo *rct_si;
527     CMS_ContentInfo *cms = NULL;
528     ASN1_OCTET_STRING **pos, *os;
529     BIO *rct_cont = NULL;
530     int r = 0;
531
532     flags &= ~(CMS_STREAM | CMS_TEXT);
533     /* Not really detached but avoids content being allocated */
534     flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
535     if (!pkey || !signcert) {
536         CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
537         return NULL;
538     }
539
540     /* Initialize signed data */
541
542     cms = CMS_sign(NULL, NULL, certs, NULL, flags);
543     if (!cms)
544         goto err;
545
546     /* Set inner content type to signed receipt */
547     if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
548         goto err;
549
550     rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
551     if (!rct_si) {
552         CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
553         goto err;
554     }
555
556     os = cms_encode_Receipt(si);
557
558     if (!os)
559         goto err;
560
561     /* Set content to digest */
562     rct_cont = BIO_new_mem_buf(os->data, os->length);
563     if (!rct_cont)
564         goto err;
565
566     /* Add msgSigDigest attribute */
567
568     if (!cms_msgSigDigest_add1(rct_si, si))
569         goto err;
570
571     /* Finalize structure */
572     if (!CMS_final(cms, rct_cont, NULL, flags))
573         goto err;
574
575     /* Set embedded content */
576     pos = CMS_get0_content(cms);
577     *pos = os;
578
579     r = 1;
580
581  err:
582     BIO_free(rct_cont);
583     if (r)
584         return cms;
585     CMS_ContentInfo_free(cms);
586     return NULL;
587
588 }
589
590 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
591                              const EVP_CIPHER *cipher, unsigned int flags)
592 {
593     CMS_ContentInfo *cms;
594     int i;
595     X509 *recip;
596     cms = CMS_EnvelopedData_create(cipher);
597     if (!cms)
598         goto merr;
599     for (i = 0; i < sk_X509_num(certs); i++) {
600         recip = sk_X509_value(certs, i);
601         if (!CMS_add1_recipient_cert(cms, recip, flags)) {
602             CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
603             goto err;
604         }
605     }
606
607     if (!(flags & CMS_DETACHED))
608         CMS_set_detached(cms, 0);
609
610     if ((flags & (CMS_STREAM | CMS_PARTIAL))
611         || CMS_final(cms, data, NULL, flags))
612         return cms;
613     else
614         goto err;
615
616  merr:
617     CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
618  err:
619     if (cms)
620         CMS_ContentInfo_free(cms);
621     return NULL;
622 }
623
624 static int cms_kari_set1_pkey(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
625                               EVP_PKEY *pk, X509 *cert)
626 {
627     int i;
628     STACK_OF(CMS_RecipientEncryptedKey) *reks;
629     CMS_RecipientEncryptedKey *rek;
630     reks = CMS_RecipientInfo_kari_get0_reks(ri);
631     if (!cert)
632         return 0;
633     for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
634         int rv;
635         rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
636         if (CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
637             continue;
638         CMS_RecipientInfo_kari_set0_pkey(ri, pk);
639         rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
640         CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
641         if (rv > 0)
642             return 1;
643         return -1;
644     }
645     return 0;
646 }
647
648 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
649 {
650     STACK_OF(CMS_RecipientInfo) *ris;
651     CMS_RecipientInfo *ri;
652     int i, r, ri_type;
653     int debug = 0, match_ri = 0;
654     ris = CMS_get0_RecipientInfos(cms);
655     if (ris)
656         debug = cms->d.envelopedData->encryptedContentInfo->debug;
657     ri_type = cms_pkey_get_ri_type(pk);
658     if (ri_type == CMS_RECIPINFO_NONE) {
659         CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY,
660                CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
661         return 0;
662     }
663
664     for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
665         ri = sk_CMS_RecipientInfo_value(ris, i);
666         if (CMS_RecipientInfo_type(ri) != ri_type)
667             continue;
668         match_ri = 1;
669         if (ri_type == CMS_RECIPINFO_AGREE) {
670             r = cms_kari_set1_pkey(cms, ri, pk, cert);
671             if (r > 0)
672                 return 1;
673             if (r < 0)
674                 return 0;
675         }
676         /*
677          * If we have a cert try matching RecipientInfo otherwise try them
678          * all.
679          */
680         else if (!cert || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
681             CMS_RecipientInfo_set0_pkey(ri, pk);
682             r = CMS_RecipientInfo_decrypt(cms, ri);
683             CMS_RecipientInfo_set0_pkey(ri, NULL);
684             if (cert) {
685                 /*
686                  * If not debugging clear any error and return success to
687                  * avoid leaking of information useful to MMA
688                  */
689                 if (!debug) {
690                     ERR_clear_error();
691                     return 1;
692                 }
693                 if (r > 0)
694                     return 1;
695                 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_DECRYPT_ERROR);
696                 return 0;
697             }
698             /*
699              * If no cert and not debugging don't leave loop after first
700              * successful decrypt. Always attempt to decrypt all recipients
701              * to avoid leaking timing of a successful decrypt.
702              */
703             else if (r > 0 && debug)
704                 return 1;
705         }
706     }
707     /* If no cert and not debugging always return success */
708     if (match_ri && !cert && !debug) {
709         ERR_clear_error();
710         return 1;
711     }
712
713     CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);
714     return 0;
715
716 }
717
718 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
719                          unsigned char *key, size_t keylen,
720                          unsigned char *id, size_t idlen)
721 {
722     STACK_OF(CMS_RecipientInfo) *ris;
723     CMS_RecipientInfo *ri;
724     int i, r;
725     ris = CMS_get0_RecipientInfos(cms);
726     for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
727         ri = sk_CMS_RecipientInfo_value(ris, i);
728         if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
729             continue;
730
731         /*
732          * If we have an id try matching RecipientInfo otherwise try them
733          * all.
734          */
735         if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
736             CMS_RecipientInfo_set0_key(ri, key, keylen);
737             r = CMS_RecipientInfo_decrypt(cms, ri);
738             CMS_RecipientInfo_set0_key(ri, NULL, 0);
739             if (r > 0)
740                 return 1;
741             if (id) {
742                 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_DECRYPT_ERROR);
743                 return 0;
744             }
745             ERR_clear_error();
746         }
747     }
748
749     CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
750     return 0;
751
752 }
753
754 int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
755                               unsigned char *pass, ossl_ssize_t passlen)
756 {
757     STACK_OF(CMS_RecipientInfo) *ris;
758     CMS_RecipientInfo *ri;
759     int i, r;
760     ris = CMS_get0_RecipientInfos(cms);
761     for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
762         ri = sk_CMS_RecipientInfo_value(ris, i);
763         if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
764             continue;
765         CMS_RecipientInfo_set0_password(ri, pass, passlen);
766         r = CMS_RecipientInfo_decrypt(cms, ri);
767         CMS_RecipientInfo_set0_password(ri, NULL, 0);
768         if (r > 0)
769             return 1;
770     }
771
772     CMSerr(CMS_F_CMS_DECRYPT_SET1_PASSWORD, CMS_R_NO_MATCHING_RECIPIENT);
773     return 0;
774
775 }
776
777 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
778                 BIO *dcont, BIO *out, unsigned int flags)
779 {
780     int r;
781     BIO *cont;
782     if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped) {
783         CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
784         return 0;
785     }
786     if (!dcont && !check_content(cms))
787         return 0;
788     if (flags & CMS_DEBUG_DECRYPT)
789         cms->d.envelopedData->encryptedContentInfo->debug = 1;
790     else
791         cms->d.envelopedData->encryptedContentInfo->debug = 0;
792     if (!pk && !cert && !dcont && !out)
793         return 1;
794     if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
795         return 0;
796     cont = CMS_dataInit(cms, dcont);
797     if (!cont)
798         return 0;
799     r = cms_copy_content(out, cont, flags);
800     do_free_upto(cont, dcont);
801     return r;
802 }
803
804 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
805 {
806     BIO *cmsbio;
807     int ret = 0;
808     if (!(cmsbio = CMS_dataInit(cms, dcont))) {
809         CMSerr(CMS_F_CMS_FINAL, ERR_R_MALLOC_FAILURE);
810         return 0;
811     }
812
813     SMIME_crlf_copy(data, cmsbio, flags);
814
815     (void)BIO_flush(cmsbio);
816
817     if (!CMS_dataFinal(cms, cmsbio)) {
818         CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR);
819         goto err;
820     }
821
822     ret = 1;
823
824  err:
825     do_free_upto(cmsbio, dcont);
826
827     return ret;
828
829 }
830
831 #ifdef ZLIB
832
833 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
834                    unsigned int flags)
835 {
836     BIO *cont;
837     int r;
838     if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
839         CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
840         return 0;
841     }
842
843     if (!dcont && !check_content(cms))
844         return 0;
845
846     cont = CMS_dataInit(cms, dcont);
847     if (!cont)
848         return 0;
849     r = cms_copy_content(out, cont, flags);
850     do_free_upto(cont, dcont);
851     return r;
852 }
853
854 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
855 {
856     CMS_ContentInfo *cms;
857     if (comp_nid <= 0)
858         comp_nid = NID_zlib_compression;
859     cms = cms_CompressedData_create(comp_nid);
860     if (!cms)
861         return NULL;
862
863     if (!(flags & CMS_DETACHED))
864         CMS_set_detached(cms, 0);
865
866     if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
867         return cms;
868
869     CMS_ContentInfo_free(cms);
870     return NULL;
871 }
872
873 #else
874
875 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
876                    unsigned int flags)
877 {
878     CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
879     return 0;
880 }
881
882 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
883 {
884     CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
885     return NULL;
886 }
887
888 #endif