dec8ef32f87d76df48866807bf01a9150fc4c6d7
[openssl.git] / crypto / cms / cms_smime.c
1 /* crypto/cms/cms_smime.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  */
53
54 #include "cryptlib.h"
55 #include <openssl/asn1t.h>
56 #include <openssl/x509.h>
57 #include <openssl/x509v3.h>
58 #include <openssl/err.h>
59 #include <openssl/cms.h>
60 #include "cms_lcl.h"
61
62 static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
63         {
64         unsigned char buf[4096];
65         int r = 0, i;
66         BIO *tmpout = NULL;
67
68         if (out == NULL)
69                 tmpout = BIO_new(BIO_s_null());
70         else if (flags & CMS_TEXT)
71                 tmpout = BIO_new(BIO_s_mem());
72         else
73                 tmpout = out;
74
75         if(!tmpout)
76                 {
77                 CMSerr(CMS_F_CMS_COPY_CONTENT,ERR_R_MALLOC_FAILURE);
78                 goto err;
79                 }
80
81         /* Read all content through chain to process digest, decrypt etc */
82         for (;;)
83         {
84                 i=BIO_read(in,buf,sizeof(buf));
85                 if (i <= 0)
86                         {
87                         if (BIO_method_type(in) == BIO_TYPE_CIPHER)
88                                 {
89                                 if (!BIO_get_cipher_status(in))
90                                         goto err;
91                                 }
92                         if (i < 0)
93                                 goto err;
94                         break;
95                         }
96                                 
97                 if (tmpout && (BIO_write(tmpout, buf, i) != i))
98                         goto err;
99         }
100
101         if(flags & CMS_TEXT)
102                 {
103                 if(!SMIME_text(tmpout, out))
104                         {
105                         CMSerr(CMS_F_CMS_COPY_CONTENT,CMS_R_SMIME_TEXT_ERROR);
106                         goto err;
107                         }
108                 }
109
110         r = 1;
111
112         err:
113         if (tmpout && (tmpout != out))
114                 BIO_free(tmpout);
115         return r;
116
117         }
118
119 static int check_content(CMS_ContentInfo *cms)
120         {
121         ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
122         if (!pos || !*pos)
123                 {
124                 CMSerr(CMS_F_CHECK_CONTENT, CMS_R_NO_CONTENT);
125                 return 0;
126                 }
127         return 1;
128         }
129
130 static void do_free_upto(BIO *f, BIO *upto)
131         {
132         if (upto)
133                 {
134                 BIO *tbio;
135                 do 
136                         {
137                         tbio = BIO_pop(f);
138                         BIO_free(f);
139                         f = tbio;
140                         }
141                 while (f != upto);
142                 }
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                 {
153                 CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
154                 return 0;
155                 }
156         cont = CMS_dataInit(cms, NULL);
157         if (!cont)
158                 return 0;
159         r = cms_copy_content(out, cont, flags);
160         BIO_free_all(cont);
161         return r;
162         }
163
164 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
165         {
166         CMS_ContentInfo *cms;
167         cms = cms_Data_create();
168         if (!cms)
169                 return NULL;
170
171         if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
172                 return cms;
173
174         CMS_ContentInfo_free(cms);
175
176         return NULL;
177         }
178
179 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
180                                                         unsigned int flags)
181         {
182         BIO *cont;
183         int r;
184         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest)
185                 {
186                 CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
187                 return 0;
188                 }
189
190         if (!dcont && !check_content(cms))
191                 return 0;
192
193         cont = CMS_dataInit(cms, dcont);
194         if (!cont)
195                 return 0;
196         r = cms_copy_content(out, cont, flags);
197         if (r)
198                 r = cms_DigestedData_do_final(cms, cont, 1);
199         do_free_upto(cont, dcont);
200         return r;
201         }
202
203 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
204                                         unsigned int flags)
205         {
206         CMS_ContentInfo *cms;
207         if (!md)
208                 md = EVP_sha1();
209         cms = cms_DigestedData_create(md);
210         if (!cms)
211                 return NULL;
212
213         if(!(flags & CMS_DETACHED))
214                 CMS_set_detached(cms, 0);
215
216         if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
217                 return cms;
218
219         CMS_ContentInfo_free(cms);
220         return NULL;
221         }
222
223 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
224                                 const unsigned char *key, size_t keylen,
225                                 BIO *dcont, BIO *out, unsigned int flags)
226         {
227         BIO *cont;
228         int r;
229         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted)
230                 {
231                 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
232                                         CMS_R_TYPE_NOT_ENCRYPTED_DATA);
233                 return 0;
234                 }
235
236         if (!dcont && !check_content(cms))
237                 return 0;
238
239         if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
240                 return 0;
241         cont = CMS_dataInit(cms, dcont);
242         if (!cont)
243                 return 0;
244         r = cms_copy_content(out, cont, flags);
245         do_free_upto(cont, dcont);
246         return r;
247         }
248
249 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
250                                         const unsigned char *key, size_t keylen,
251                                         unsigned int flags)
252         {
253         CMS_ContentInfo *cms;
254         if (!cipher)
255                 {
256                 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
257                 return NULL;
258                 }
259         cms = CMS_ContentInfo_new();
260         if (!cms)
261                 return NULL;
262         if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
263                 return NULL;
264
265         if(!(flags & CMS_DETACHED))
266                 CMS_set_detached(cms, 0);
267
268         if ((flags & (CMS_STREAM|CMS_PARTIAL))
269                 || CMS_final(cms, in, NULL, flags))
270                 return cms;
271
272         CMS_ContentInfo_free(cms);
273         return NULL;
274         }
275
276 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
277                                         X509_STORE *store,
278                                         STACK_OF(X509) *certs,
279                                         STACK_OF(X509_CRL) *crls,
280                                         unsigned int flags)
281         {
282         X509_STORE_CTX ctx;
283         X509 *signer;
284         int i, j, r = 0;
285         CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
286         if (!X509_STORE_CTX_init(&ctx, store, signer, certs))
287                 {
288                 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
289                                                 CMS_R_STORE_INIT_ERROR);
290                 goto err;
291                 }
292         X509_STORE_CTX_set_purpose(&ctx, X509_PURPOSE_SMIME_SIGN);
293         if (crls)
294                 X509_STORE_CTX_set0_crls(&ctx, crls);
295
296         i = X509_verify_cert(&ctx);
297         if (i <= 0)
298                 {
299                 j = X509_STORE_CTX_get_error(&ctx);
300                 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
301                                                 CMS_R_CERTIFICATE_VERIFY_ERROR);
302                 ERR_add_error_data(2, "Verify error:",
303                                          X509_verify_cert_error_string(j));
304                 goto err;
305                 }
306         r = 1;
307         err:
308         X509_STORE_CTX_cleanup(&ctx);
309         return r;
310
311         }
312
313 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
314                  X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
315         {
316         CMS_SignerInfo *si;
317         STACK_OF(CMS_SignerInfo) *sinfos;
318         STACK_OF(X509) *cms_certs = NULL;
319         STACK_OF(X509_CRL) *crls = NULL;
320         X509 *signer;
321         int i, scount = 0, ret = 0;
322         BIO *cmsbio = NULL, *tmpin = NULL;
323
324         if (!dcont && !check_content(cms))
325                 return 0;
326
327         /* Attempt to find all signer certificates */
328
329         sinfos = CMS_get0_SignerInfos(cms);
330
331         if (sk_CMS_SignerInfo_num(sinfos) <= 0)
332                 {
333                 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
334                 goto err;
335                 }
336
337         for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
338                 {
339                 si = sk_CMS_SignerInfo_value(sinfos, i);
340                 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
341                 if (signer)
342                         scount++;
343                 }
344
345         if (scount != sk_CMS_SignerInfo_num(sinfos))
346                 scount += CMS_set1_signers_certs(cms, certs, flags);
347
348         if (scount != sk_CMS_SignerInfo_num(sinfos))
349                 {
350                 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
351                 goto err;
352                 }
353
354         /* Attempt to verify all signers certs */
355
356         if (!(flags & CMS_NO_SIGNER_CERT_VERIFY))
357                 {
358                 cms_certs = CMS_get1_certs(cms);
359                 if (!(flags & CMS_NOCRL))
360                         crls = CMS_get1_crls(cms);
361                 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
362                         {
363                         si = sk_CMS_SignerInfo_value(sinfos, i);
364                         if (!cms_signerinfo_verify_cert(si, store,
365                                                         cms_certs, crls, flags))
366                                 goto err;
367                         }
368                 }
369
370         /* Attempt to verify all SignerInfo signed attribute signatures */
371
372         if (!(flags & CMS_NO_ATTR_VERIFY))
373                 {
374                 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
375                         {
376                         si = sk_CMS_SignerInfo_value(sinfos, i);
377                         if (CMS_signed_get_attr_count(si) < 0)
378                                 continue;
379                         if (CMS_SignerInfo_verify(si) <= 0)
380                                 goto err;
381                         }
382                 }
383
384         /* Performance optimization: if the content is a memory BIO then
385          * store its contents in a temporary read only memory BIO. This
386          * avoids potentially large numbers of slow copies of data which will
387          * occur when reading from a read write memory BIO when signatures
388          * are calculated.
389          */
390
391         if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM))
392                 {
393                 char *ptr;
394                 long len;
395                 len = BIO_get_mem_data(dcont, &ptr);
396                 tmpin = BIO_new_mem_buf(ptr, len);
397                 if (tmpin == NULL)
398                         {
399                         CMSerr(CMS_F_CMS_VERIFY,ERR_R_MALLOC_FAILURE);
400                         return 0;
401                         }
402                 }
403         else
404                 tmpin = dcont;
405                 
406
407         cmsbio=CMS_dataInit(cms, tmpin);
408         if (!cmsbio)
409                 goto err;
410
411         if (!cms_copy_content(out, cmsbio, flags))
412                 goto err;
413
414         if (!(flags & CMS_NO_CONTENT_VERIFY))
415                 {
416                 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
417                         {
418                         si = sk_CMS_SignerInfo_value(sinfos, i);
419                         if (!CMS_SignerInfo_verify_content(si, cmsbio))
420                                 {
421                                 CMSerr(CMS_F_CMS_VERIFY,
422                                         CMS_R_CONTENT_VERIFY_ERROR);
423                                 goto err;
424                                 }
425                         }
426                 }
427
428         ret = 1;
429
430         err:
431         
432         if (dcont && (tmpin == dcont))
433                 do_free_upto(cmsbio, dcont);
434         else
435                 BIO_free_all(cmsbio);
436
437         if (cms_certs)
438                 sk_X509_pop_free(cms_certs, X509_free);
439         if (crls)
440                 sk_X509_CRL_pop_free(crls, X509_CRL_free);
441
442         return ret;
443         }
444
445 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
446                         STACK_OF(X509) *certs,
447                         X509_STORE *store, unsigned int flags)
448         {
449         int r;
450         flags &= ~(CMS_DETACHED|CMS_TEXT);
451         r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
452         if (r <= 0)
453                 return r;
454         return cms_Receipt_verify(rcms, ocms);
455         }
456
457 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
458                                                 BIO *data, unsigned int flags)
459         {
460         CMS_ContentInfo *cms;
461         int i;
462
463         cms = CMS_ContentInfo_new();
464         if (!cms || !CMS_SignedData_init(cms))
465                 goto merr;
466
467         if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags))
468                 {
469                 CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
470                 goto err;
471                 }
472
473         for (i = 0; i < sk_X509_num(certs); i++)
474                 {
475                 X509 *x = sk_X509_value(certs, i);
476                 if (!CMS_add1_cert(cms, x))
477                         goto merr;
478                 }
479
480         if(!(flags & CMS_DETACHED))
481                 CMS_set_detached(cms, 0);
482
483         if ((flags & (CMS_STREAM|CMS_PARTIAL))
484                 || CMS_final(cms, data, NULL, flags))
485                 return cms;
486         else
487                 goto err;
488
489         merr:
490         CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
491
492         err:
493         if (cms)
494                 CMS_ContentInfo_free(cms);
495         return NULL;
496         }
497
498 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
499                                         X509 *signcert, EVP_PKEY *pkey,
500                                         STACK_OF(X509) *certs,
501                                         unsigned int flags)
502         {
503         CMS_SignerInfo *rct_si;
504         CMS_ContentInfo *cms = NULL;
505         ASN1_OCTET_STRING **pos, *os;
506         BIO *rct_cont = NULL;
507         int r = 0;
508
509         flags &= ~(CMS_STREAM|CMS_TEXT);
510         /* Not really detached but avoids content being allocated */
511         flags |= CMS_PARTIAL|CMS_BINARY|CMS_DETACHED;
512         if (!pkey || !signcert)
513                 {
514                 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
515                 return NULL;
516                 }
517
518         /* Initialize signed data */
519
520         cms = CMS_sign(NULL, NULL, certs, NULL, flags);
521         if (!cms)
522                 goto err;
523
524         /* Set inner content type to signed receipt */
525         if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
526                 goto err;
527
528         rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
529         if (!rct_si)
530                 {
531                 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
532                 goto err;
533                 }
534
535         os = cms_encode_Receipt(si);
536
537         if (!os)
538                 goto err;
539
540         /* Set content to digest */
541         rct_cont = BIO_new_mem_buf(os->data, os->length);
542         if (!rct_cont)
543                 goto err;
544
545         /* Add msgSigDigest attribute */
546
547         if (!cms_msgSigDigest_add1(rct_si, si))
548                 goto err;
549
550         /* Finalize structure */
551         if (!CMS_final(cms, rct_cont, NULL, flags))
552                 goto err;
553
554         /* Set embedded content */
555         pos = CMS_get0_content(cms);
556         *pos = os;
557
558         r = 1;
559
560         err:
561         if (rct_cont)
562                 BIO_free(rct_cont);
563         if (r)
564                 return cms;
565         CMS_ContentInfo_free(cms);
566         return NULL;
567
568         }
569
570 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
571                                 const EVP_CIPHER *cipher, unsigned int flags)
572         {
573         CMS_ContentInfo *cms;
574         int i;
575         X509 *recip;
576         cms = CMS_EnvelopedData_create(cipher);
577         if (!cms)
578                 goto merr;
579         for (i = 0; i < sk_X509_num(certs); i++)
580                 {
581                 recip = sk_X509_value(certs, i);
582                 if (!CMS_add1_recipient_cert(cms, recip, flags))
583                         {
584                         CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
585                         goto err;
586                         }
587                 }
588
589         if(!(flags & CMS_DETACHED))
590                 CMS_set_detached(cms, 0);
591
592         if ((flags & (CMS_STREAM|CMS_PARTIAL))
593                 || CMS_final(cms, data, NULL, flags))
594                 return cms;
595         else
596                 goto err;
597
598         merr:
599         CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
600         err:
601         if (cms)
602                 CMS_ContentInfo_free(cms);
603         return NULL;
604         }
605
606 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
607         {
608         STACK_OF(CMS_RecipientInfo) *ris;
609         CMS_RecipientInfo *ri;
610         int i, r;
611         ris = CMS_get0_RecipientInfos(cms);
612         for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++)
613                 {
614                 ri = sk_CMS_RecipientInfo_value(ris, i);
615                 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_TRANS)
616                                 continue;
617                 /* If we have a cert try matching RecipientInfo
618                  * otherwise try them all.
619                  */
620                 if (!cert || (CMS_RecipientInfo_ktri_cert_cmp(ri, cert) == 0))
621                         {
622                         CMS_RecipientInfo_set0_pkey(ri, pk);
623                         r = CMS_RecipientInfo_decrypt(cms, ri);
624                         CMS_RecipientInfo_set0_pkey(ri, NULL);
625                         if (r > 0)
626                                 return 1;
627                         if (cert)
628                                 {
629                                 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY,
630                                                 CMS_R_DECRYPT_ERROR);
631                                 return 0;
632                                 }
633                         ERR_clear_error();
634                         }
635                 }
636
637         CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);
638         return 0;
639
640         }
641
642 int CMS_decrypt_set1_key(CMS_ContentInfo *cms, 
643                                 unsigned char *key, size_t keylen,
644                                 unsigned char *id, size_t idlen)
645         {
646         STACK_OF(CMS_RecipientInfo) *ris;
647         CMS_RecipientInfo *ri;
648         int i, r;
649         ris = CMS_get0_RecipientInfos(cms);
650         for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++)
651                 {
652                 ri = sk_CMS_RecipientInfo_value(ris, i);
653                 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
654                                 continue;
655
656                 /* If we have an id try matching RecipientInfo
657                  * otherwise try them all.
658                  */
659                 if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0))
660                         {
661                         CMS_RecipientInfo_set0_key(ri, key, keylen);
662                         r = CMS_RecipientInfo_decrypt(cms, ri);
663                         CMS_RecipientInfo_set0_key(ri, NULL, 0);
664                         if (r > 0)
665                                 return 1;
666                         if (id)
667                                 {
668                                 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY,
669                                                 CMS_R_DECRYPT_ERROR);
670                                 return 0;
671                                 }
672                         ERR_clear_error();
673                         }
674                 }
675
676         CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
677         return 0;
678
679         }
680         
681 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
682                                 BIO *dcont, BIO *out,
683                                 unsigned int flags)
684         {
685         int r;
686         BIO *cont;
687         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped)
688                 {
689                 CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
690                 return 0;
691                 }
692         if (!dcont && !check_content(cms))
693                 return 0;
694         if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
695                 return 0;
696
697         cont = CMS_dataInit(cms, dcont);
698         if (!cont)
699                 return 0;
700         r = cms_copy_content(out, cont, flags);
701         do_free_upto(cont, dcont);
702         return r;
703         }
704
705 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
706         {
707         BIO *cmsbio;
708         int ret = 0;
709         if (!(cmsbio = CMS_dataInit(cms, dcont)))
710                 {
711                 CMSerr(CMS_F_CMS_FINAL,ERR_R_MALLOC_FAILURE);
712                 return 0;
713                 }
714
715         SMIME_crlf_copy(data, cmsbio, flags);
716
717         (void)BIO_flush(cmsbio);
718
719
720         if (!CMS_dataFinal(cms, cmsbio))
721                 {
722                 CMSerr(CMS_F_CMS_FINAL,CMS_R_CMS_DATAFINAL_ERROR);
723                 goto err;
724                 }
725
726         ret = 1;
727
728         err:
729         do_free_upto(cmsbio, dcont);
730
731         return ret;
732
733         }
734
735 #ifdef ZLIB
736
737 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
738                                                         unsigned int flags)
739         {
740         BIO *cont;
741         int r;
742         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData)
743                 {
744                 CMSerr(CMS_F_CMS_UNCOMPRESS,
745                                         CMS_R_TYPE_NOT_COMPRESSED_DATA);
746                 return 0;
747                 }
748
749         if (!dcont && !check_content(cms))
750                 return 0;
751
752         cont = CMS_dataInit(cms, dcont);
753         if (!cont)
754                 return 0;
755         r = cms_copy_content(out, cont, flags);
756         do_free_upto(cont, dcont);
757         return r;
758         }
759
760 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
761         {
762         CMS_ContentInfo *cms;
763         if (comp_nid <= 0)
764                 comp_nid = NID_zlib_compression;
765         cms = cms_CompressedData_create(comp_nid);
766         if (!cms)
767                 return NULL;
768
769         if(!(flags & CMS_DETACHED))
770                 CMS_set_detached(cms, 0);
771
772         if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
773                 return cms;
774
775         CMS_ContentInfo_free(cms);
776         return NULL;
777         }
778
779 #else
780
781 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
782                                                         unsigned int flags)
783         {
784         CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
785         return 0;
786         }
787
788 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
789         {
790         CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
791         return NULL;
792         }
793
794 #endif