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