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