Remove old versions of OpenSSL.
[dragonfly.git] / crypto / openssl-0.9 / 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 (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                 {
215                 flags &= ~CMS_STREAM;
216                 CMS_set_detached(cms, 0);
217                 }
218
219         if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
220                 return cms;
221
222         CMS_ContentInfo_free(cms);
223         return NULL;
224         }
225
226 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
227                                 const unsigned char *key, size_t keylen,
228                                 BIO *dcont, BIO *out, unsigned int flags)
229         {
230         BIO *cont;
231         int r;
232         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted)
233                 {
234                 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
235                                         CMS_R_TYPE_NOT_ENCRYPTED_DATA);
236                 return 0;
237                 }
238
239         if (!dcont && !check_content(cms))
240                 return 0;
241
242         if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
243                 return 0;
244         cont = CMS_dataInit(cms, dcont);
245         if (!cont)
246                 return 0;
247         r = cms_copy_content(out, cont, flags);
248         do_free_upto(cont, dcont);
249         return r;
250         }
251
252 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
253                                         const unsigned char *key, size_t keylen,
254                                         unsigned int flags)
255         {
256         CMS_ContentInfo *cms;
257         if (!cipher)
258                 {
259                 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
260                 return NULL;
261                 }
262         cms = CMS_ContentInfo_new();
263         if (!cms)
264                 return NULL;
265         if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
266                 return NULL;
267
268         if(!(flags & CMS_DETACHED))
269                 {
270                 flags &= ~CMS_STREAM;
271                 CMS_set_detached(cms, 0);
272                 }
273
274         if ((flags & (CMS_STREAM|CMS_PARTIAL))
275                 || CMS_final(cms, in, NULL, flags))
276                 return cms;
277
278         CMS_ContentInfo_free(cms);
279         return NULL;
280         }
281
282 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
283                                         X509_STORE *store,
284                                         STACK_OF(X509) *certs,
285                                         STACK_OF(X509_CRL) *crls,
286                                         unsigned int flags)
287         {
288         X509_STORE_CTX ctx;
289         X509 *signer;
290         int i, j, r = 0;
291         CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
292         if (!X509_STORE_CTX_init(&ctx, store, signer, certs))
293                 {
294                 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
295                                                 CMS_R_STORE_INIT_ERROR);
296                 goto err;
297                 }
298         X509_STORE_CTX_set_purpose(&ctx, X509_PURPOSE_SMIME_SIGN);
299         if (crls)
300                 X509_STORE_CTX_set0_crls(&ctx, crls);
301
302         i = X509_verify_cert(&ctx);
303         if (i <= 0)
304                 {
305                 j = X509_STORE_CTX_get_error(&ctx);
306                 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
307                                                 CMS_R_CERTIFICATE_VERIFY_ERROR);
308                 ERR_add_error_data(2, "Verify error:",
309                                          X509_verify_cert_error_string(j));
310                 goto err;
311                 }
312         r = 1;
313         err:
314         X509_STORE_CTX_cleanup(&ctx);
315         return r;
316
317         }
318
319 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
320                  X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
321         {
322         CMS_SignerInfo *si;
323         STACK_OF(CMS_SignerInfo) *sinfos;
324         STACK_OF(X509) *cms_certs = NULL;
325         STACK_OF(X509_CRL) *crls = NULL;
326         X509 *signer;
327         int i, scount = 0, ret = 0;
328         BIO *cmsbio = NULL, *tmpin = NULL;
329
330         if (!dcont && !check_content(cms))
331                 return 0;
332
333         /* Attempt to find all signer certificates */
334
335         sinfos = CMS_get0_SignerInfos(cms);
336
337         if (sk_CMS_SignerInfo_num(sinfos) <= 0)
338                 {
339                 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
340                 goto err;
341                 }
342
343         for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
344                 {
345                 si = sk_CMS_SignerInfo_value(sinfos, i);
346                 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
347                 if (signer)
348                         scount++;
349                 }
350
351         if (scount != sk_CMS_SignerInfo_num(sinfos))
352                 scount += CMS_set1_signers_certs(cms, certs, flags);
353
354         if (scount != sk_CMS_SignerInfo_num(sinfos))
355                 {
356                 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
357                 goto err;
358                 }
359
360         /* Attempt to verify all signers certs */
361
362         if (!(flags & CMS_NO_SIGNER_CERT_VERIFY))
363                 {
364                 cms_certs = CMS_get1_certs(cms);
365                 if (!(flags & CMS_NOCRL))
366                         crls = CMS_get1_crls(cms);
367                 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
368                         {
369                         si = sk_CMS_SignerInfo_value(sinfos, i);
370                         if (!cms_signerinfo_verify_cert(si, store,
371                                                         cms_certs, crls, flags))
372                                 goto err;
373                         }
374                 }
375
376         /* Attempt to verify all SignerInfo signed attribute signatures */
377
378         if (!(flags & CMS_NO_ATTR_VERIFY))
379                 {
380                 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
381                         {
382                         si = sk_CMS_SignerInfo_value(sinfos, i);
383                         if (CMS_signed_get_attr_count(si) < 0)
384                                 continue;
385                         if (CMS_SignerInfo_verify(si) <= 0)
386                                 goto err;
387                         }
388                 }
389
390         /* Performance optimization: if the content is a memory BIO then
391          * store its contents in a temporary read only memory BIO. This
392          * avoids potentially large numbers of slow copies of data which will
393          * occur when reading from a read write memory BIO when signatures
394          * are calculated.
395          */
396
397         if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM))
398                 {
399                 char *ptr;
400                 long len;
401                 len = BIO_get_mem_data(dcont, &ptr);
402                 tmpin = BIO_new_mem_buf(ptr, len);
403                 if (tmpin == NULL)
404                         {
405                         CMSerr(CMS_F_CMS_VERIFY,ERR_R_MALLOC_FAILURE);
406                         return 0;
407                         }
408                 }
409         else
410                 tmpin = dcont;
411                 
412
413         cmsbio=CMS_dataInit(cms, tmpin);
414         if (!cmsbio)
415                 goto err;
416
417         if (!cms_copy_content(out, cmsbio, flags))
418                 goto err;
419
420         if (!(flags & CMS_NO_CONTENT_VERIFY))
421                 {
422                 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
423                         {
424                         si = sk_CMS_SignerInfo_value(sinfos, i);
425                         if (!CMS_SignerInfo_verify_content(si, cmsbio))
426                                 {
427                                 CMSerr(CMS_F_CMS_VERIFY,
428                                         CMS_R_CONTENT_VERIFY_ERROR);
429                                 goto err;
430                                 }
431                         }
432                 }
433
434         ret = 1;
435
436         err:
437         
438         if (dcont && (tmpin == dcont))
439                 do_free_upto(cmsbio, dcont);
440         else
441                 BIO_free_all(cmsbio);
442
443         if (cms_certs)
444                 sk_X509_pop_free(cms_certs, X509_free);
445         if (crls)
446                 sk_X509_CRL_pop_free(crls, X509_CRL_free);
447
448         return ret;
449         }
450
451 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
452                         STACK_OF(X509) *certs,
453                         X509_STORE *store, unsigned int flags)
454         {
455         int r;
456         r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
457         if (r <= 0)
458                 return r;
459         return cms_Receipt_verify(rcms, ocms);
460         }
461
462 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
463                                                 BIO *data, unsigned int flags)
464         {
465         CMS_ContentInfo *cms;
466         int i;
467
468         cms = CMS_ContentInfo_new();
469         if (!cms || !CMS_SignedData_init(cms))
470                 goto merr;
471
472         if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags))
473                 {
474                 CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
475                 goto err;
476                 }
477
478         for (i = 0; i < sk_X509_num(certs); i++)
479                 {
480                 X509 *x = sk_X509_value(certs, i);
481                 if (!CMS_add1_cert(cms, x))
482                         goto merr;
483                 }
484
485         if(!(flags & CMS_DETACHED))
486                 {
487                 flags &= ~CMS_STREAM;
488                 CMS_set_detached(cms, 0);
489                 }
490
491         if ((flags & (CMS_STREAM|CMS_PARTIAL))
492                 || CMS_final(cms, data, NULL, flags))
493                 return cms;
494         else
495                 goto err;
496
497         merr:
498         CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
499
500         err:
501         if (cms)
502                 CMS_ContentInfo_free(cms);
503         return NULL;
504         }
505
506 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
507                                         X509 *signcert, EVP_PKEY *pkey,
508                                         STACK_OF(X509) *certs,
509                                         unsigned int flags)
510         {
511         CMS_SignerInfo *rct_si;
512         CMS_ContentInfo *cms = NULL;
513         ASN1_OCTET_STRING **pos, *os;
514         BIO *rct_cont = NULL;
515         int r = 0;
516
517         flags &= ~CMS_STREAM;
518         /* Not really detached but avoids content being allocated */
519         flags |= CMS_PARTIAL|CMS_BINARY|CMS_DETACHED;
520         if (!pkey || !signcert)
521                 {
522                 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
523                 return NULL;
524                 }
525
526         /* Initialize signed data */
527
528         cms = CMS_sign(NULL, NULL, certs, NULL, flags);
529         if (!cms)
530                 goto err;
531
532         /* Set inner content type to signed receipt */
533         if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
534                 goto err;
535
536         rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
537         if (!rct_si)
538                 {
539                 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
540                 goto err;
541                 }
542
543         os = cms_encode_Receipt(si);
544
545         if (!os)
546                 goto err;
547
548         /* Set content to digest */
549         rct_cont = BIO_new_mem_buf(os->data, os->length);
550         if (!rct_cont)
551                 goto err;
552
553         /* Add msgSigDigest attribute */
554
555         if (!cms_msgSigDigest_add1(rct_si, si))
556                 goto err;
557
558         /* Finalize structure */
559         if (!CMS_final(cms, rct_cont, NULL, flags))
560                 goto err;
561
562         /* Set embedded content */
563         pos = CMS_get0_content(cms);
564         *pos = os;
565
566         r = 1;
567
568         err:
569         if (rct_cont)
570                 BIO_free(rct_cont);
571         if (r)
572                 return cms;
573         CMS_ContentInfo_free(cms);
574         return NULL;
575
576         }
577
578 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
579                                 const EVP_CIPHER *cipher, unsigned int flags)
580         {
581         CMS_ContentInfo *cms;
582         int i;
583         X509 *recip;
584         cms = CMS_EnvelopedData_create(cipher);
585         if (!cms)
586                 goto merr;
587         for (i = 0; i < sk_X509_num(certs); i++)
588                 {
589                 recip = sk_X509_value(certs, i);
590                 if (!CMS_add1_recipient_cert(cms, recip, flags))
591                         {
592                         CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
593                         goto err;
594                         }
595                 }
596
597         if(!(flags & CMS_DETACHED))
598                 {
599                 flags &= ~CMS_STREAM;
600                 CMS_set_detached(cms, 0);
601                 }
602
603         if ((flags & (CMS_STREAM|CMS_PARTIAL))
604                 || CMS_final(cms, data, NULL, flags))
605                 return cms;
606         else
607                 goto err;
608
609         merr:
610         CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
611         err:
612         if (cms)
613                 CMS_ContentInfo_free(cms);
614         return NULL;
615         }
616
617 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
618         {
619         STACK_OF(CMS_RecipientInfo) *ris;
620         CMS_RecipientInfo *ri;
621         int i, r;
622         ris = CMS_get0_RecipientInfos(cms);
623         for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++)
624                 {
625                 ri = sk_CMS_RecipientInfo_value(ris, i);
626                 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_TRANS)
627                                 continue;
628                 /* If we have a cert try matching RecipientInfo
629                  * otherwise try them all.
630                  */
631                 if (!cert || (CMS_RecipientInfo_ktri_cert_cmp(ri, cert) == 0))
632                         {
633                         CMS_RecipientInfo_set0_pkey(ri, pk);
634                         r = CMS_RecipientInfo_decrypt(cms, ri);
635                         CMS_RecipientInfo_set0_pkey(ri, NULL);
636                         if (r > 0)
637                                 return 1;
638                         if (cert)
639                                 {
640                                 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY,
641                                                 CMS_R_DECRYPT_ERROR);
642                                 return 0;
643                                 }
644                         ERR_clear_error();
645                         }
646                 }
647
648         CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);
649         return 0;
650
651         }
652
653 int CMS_decrypt_set1_key(CMS_ContentInfo *cms, 
654                                 unsigned char *key, size_t keylen,
655                                 unsigned char *id, size_t idlen)
656         {
657         STACK_OF(CMS_RecipientInfo) *ris;
658         CMS_RecipientInfo *ri;
659         int i, r;
660         ris = CMS_get0_RecipientInfos(cms);
661         for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++)
662                 {
663                 ri = sk_CMS_RecipientInfo_value(ris, i);
664                 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
665                                 continue;
666
667                 /* If we have an id try matching RecipientInfo
668                  * otherwise try them all.
669                  */
670                 if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0))
671                         {
672                         CMS_RecipientInfo_set0_key(ri, key, keylen);
673                         r = CMS_RecipientInfo_decrypt(cms, ri);
674                         CMS_RecipientInfo_set0_key(ri, NULL, 0);
675                         if (r > 0)
676                                 return 1;
677                         if (id)
678                                 {
679                                 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY,
680                                                 CMS_R_DECRYPT_ERROR);
681                                 return 0;
682                                 }
683                         ERR_clear_error();
684                         }
685                 }
686
687         CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
688         return 0;
689
690         }
691         
692 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
693                                 BIO *dcont, BIO *out,
694                                 unsigned int flags)
695         {
696         int r;
697         BIO *cont;
698         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped)
699                 {
700                 CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
701                 return 0;
702                 }
703         if (!dcont && !check_content(cms))
704                 return 0;
705         if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
706                 return 0;
707
708         cont = CMS_dataInit(cms, dcont);
709         if (!cont)
710                 return 0;
711         r = cms_copy_content(out, cont, flags);
712         do_free_upto(cont, dcont);
713         return r;
714         }
715
716 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
717         {
718         BIO *cmsbio;
719         int ret = 0;
720         if (!(cmsbio = CMS_dataInit(cms, dcont)))
721                 {
722                 CMSerr(CMS_F_CMS_FINAL,ERR_R_MALLOC_FAILURE);
723                 return 0;
724                 }
725
726         SMIME_crlf_copy(data, cmsbio, flags);
727
728         (void)BIO_flush(cmsbio);
729
730
731         if (!CMS_dataFinal(cms, cmsbio))
732                 {
733                 CMSerr(CMS_F_CMS_FINAL,CMS_R_CMS_DATAFINAL_ERROR);
734                 goto err;
735                 }
736
737         ret = 1;
738
739         err:
740         do_free_upto(cmsbio, dcont);
741
742         return ret;
743
744         }
745
746 #ifdef ZLIB
747
748 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
749                                                         unsigned int flags)
750         {
751         BIO *cont;
752         int r;
753         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData)
754                 {
755                 CMSerr(CMS_F_CMS_UNCOMPRESS,
756                                         CMS_R_TYPE_NOT_COMPRESSED_DATA);
757                 return 0;
758                 }
759
760         if (!dcont && !check_content(cms))
761                 return 0;
762
763         cont = CMS_dataInit(cms, dcont);
764         if (!cont)
765                 return 0;
766         r = cms_copy_content(out, cont, flags);
767         do_free_upto(cont, dcont);
768         return r;
769         }
770
771 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
772         {
773         CMS_ContentInfo *cms;
774         if (comp_nid <= 0)
775                 comp_nid = NID_zlib_compression;
776         cms = cms_CompressedData_create(comp_nid);
777         if (!cms)
778                 return NULL;
779
780         if(!(flags & CMS_DETACHED))
781                 {
782                 flags &= ~CMS_STREAM;
783                 CMS_set_detached(cms, 0);
784                 }
785
786         if (CMS_final(cms, in, NULL, flags))
787                 return cms;
788
789         CMS_ContentInfo_free(cms);
790         return NULL;
791         }
792
793 #else
794
795 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
796                                                         unsigned int flags)
797         {
798         CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
799         return 0;
800         }
801
802 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
803         {
804         CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
805         return NULL;
806         }
807
808 #endif