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