b18c789f643fc4cf181169d8c8ae8cea4c8529bd
[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         flags &= ~(CMS_DETACHED|CMS_TEXT);
430         r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
431         if (r <= 0)
432                 return r;
433         return cms_Receipt_verify(rcms, ocms);
434         }
435
436 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
437                                                 BIO *data, unsigned int flags)
438         {
439         CMS_ContentInfo *cms;
440         int i;
441         cms = CMS_ContentInfo_new();
442         if (!cms)
443                 goto merr;
444         if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags))
445                 {
446                 CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
447                 goto err;
448                 }
449         for (i = 0; i < sk_X509_num(certs); i++)
450                 {
451                 X509 *x = sk_X509_value(certs, i);
452                 if (!CMS_add1_cert(cms, x))
453                         goto merr;
454                 }
455         /* If no signer or certs initialize signedData */
456         if (!pkey && !i && !CMS_SignedData_init(cms))
457                 goto merr;
458
459         if(!(flags & CMS_DETACHED))
460                 CMS_set_detached(cms, 0);
461
462         if ((flags & (CMS_STREAM|CMS_PARTIAL)) || CMS_final(cms, data, flags))
463                 return cms;
464         else
465                 goto err;
466
467         merr:
468         CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
469
470         err:
471         if (cms)
472                 CMS_ContentInfo_free(cms);
473         return NULL;
474         }
475
476 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
477                                         X509 *signcert, EVP_PKEY *pkey,
478                                         STACK_OF(X509) *certs,
479                                         unsigned int flags)
480         {
481         CMS_SignerInfo *rct_si;
482         CMS_ContentInfo *cms = NULL;
483         ASN1_OCTET_STRING **pos, *os;
484         BIO *rct_cont = NULL;
485         int r = 0;
486
487         flags &= ~(CMS_STREAM|CMS_TEXT);
488         /* Not really detached but avoids content being allocated */
489         flags |= CMS_PARTIAL|CMS_BINARY|CMS_DETACHED;
490         if (!pkey || !signcert)
491                 {
492                 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
493                 return NULL;
494                 }
495
496         /* Initialize signed data */
497
498         cms = CMS_sign(NULL, NULL, certs, NULL, flags);
499         if (!cms)
500                 goto err;
501
502         /* Set inner content type to signed receipt */
503         if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
504                 goto err;
505
506         rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
507         if (!rct_si)
508                 {
509                 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
510                 goto err;
511                 }
512
513         os = cms_encode_Receipt(si);
514
515         if (!os)
516                 goto err;
517
518         /* Set content to digest */
519         rct_cont = BIO_new_mem_buf(os->data, os->length);
520         if (!rct_cont)
521                 goto err;
522
523         /* Add msgSigDigest attribute */
524
525         if (!cms_msgSigDigest_add1(rct_si, si))
526                 goto err;
527
528         /* Finalize structure */
529         if (!CMS_final(cms, rct_cont, flags))
530                 goto err;
531
532         /* Set embedded content */
533         pos = CMS_get0_content(cms);
534         *pos = os;
535
536         r = 1;
537
538         err:
539         if (rct_cont)
540                 BIO_free(rct_cont);
541         if (r)
542                 return cms;
543         CMS_ContentInfo_free(cms);
544         return NULL;
545
546         }
547
548 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
549                                 const EVP_CIPHER *cipher, unsigned int flags)
550         {
551         CMS_ContentInfo *cms;
552         int i;
553         X509 *recip;
554         cms = CMS_EnvelopedData_create(cipher);
555         if (!cms)
556                 goto merr;
557         for (i = 0; i < sk_X509_num(certs); i++)
558                 {
559                 recip = sk_X509_value(certs, i);
560                 if (!CMS_add1_recipient_cert(cms, recip, flags))
561                         {
562                         CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
563                         goto err;
564                         }
565                 }
566
567         if(!(flags & CMS_DETACHED))
568                 CMS_set_detached(cms, 0);
569
570         if ((flags & (CMS_STREAM|CMS_PARTIAL)) || CMS_final(cms, data, flags))
571                 return cms;
572         else
573                 goto err;
574
575         merr:
576         CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
577         err:
578         if (cms)
579                 CMS_ContentInfo_free(cms);
580         return NULL;
581         }
582
583 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
584         {
585         STACK_OF(CMS_RecipientInfo) *ris;
586         CMS_RecipientInfo *ri;
587         int i, r;
588         ris = CMS_get0_RecipientInfos(cms);
589         for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++)
590                 {
591                 ri = sk_CMS_RecipientInfo_value(ris, i);
592                 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_TRANS)
593                                 continue;
594                 /* If we have a cert try matching RecipientInfo
595                  * otherwise try them all.
596                  */
597                 if (!cert || (CMS_RecipientInfo_ktri_cert_cmp(ri, cert) == 0))
598                         {
599                         CMS_RecipientInfo_set0_pkey(ri, pk);
600                         r = CMS_RecipientInfo_decrypt(cms, ri);
601                         CMS_RecipientInfo_set0_pkey(ri, NULL);
602                         if (r > 0)
603                                 return 1;
604                         if (cert)
605                                 {
606                                 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY,
607                                                 CMS_R_DECRYPT_ERROR);
608                                 return 0;
609                                 }
610                         ERR_clear_error();
611                         }
612                 }
613
614         CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);
615         return 0;
616
617         }
618
619 int CMS_decrypt_set1_key(CMS_ContentInfo *cms, 
620                                 unsigned char *key, size_t keylen,
621                                 unsigned char *id, size_t idlen)
622         {
623         STACK_OF(CMS_RecipientInfo) *ris;
624         CMS_RecipientInfo *ri;
625         int i, r;
626         ris = CMS_get0_RecipientInfos(cms);
627         for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++)
628                 {
629                 ri = sk_CMS_RecipientInfo_value(ris, i);
630                 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
631                                 continue;
632
633                 /* If we have an id try matching RecipientInfo
634                  * otherwise try them all.
635                  */
636                 if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0))
637                         {
638                         CMS_RecipientInfo_set0_key(ri, key, keylen);
639                         r = CMS_RecipientInfo_decrypt(cms, ri);
640                         CMS_RecipientInfo_set0_key(ri, NULL, 0);
641                         if (r > 0)
642                                 return 1;
643                         if (id)
644                                 {
645                                 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY,
646                                                 CMS_R_DECRYPT_ERROR);
647                                 return 0;
648                                 }
649                         ERR_clear_error();
650                         }
651                 }
652
653         CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
654         return 0;
655
656         }
657         
658 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
659                                 BIO *dcont, BIO *out,
660                                 unsigned int flags)
661         {
662         int r;
663         BIO *cont;
664         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped)
665                 {
666                 CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
667                 return 0;
668                 }
669         if (!dcont && !check_content(cms))
670                 return 0;
671         if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
672                 return 0;
673
674         cont = CMS_dataInit(cms, dcont);
675         if (!cont)
676                 return 0;
677         r = cms_copy_content(out, cont, flags);
678         BIO_free_all(cont);
679         return r;
680         }
681
682 int CMS_final(CMS_ContentInfo *cms, BIO *data, int flags)
683         {
684         BIO *cmsbio;
685         int ret = 0;
686         if (!(cmsbio = CMS_dataInit(cms, NULL)))
687                 {
688                 CMSerr(CMS_F_CMS_FINAL,ERR_R_MALLOC_FAILURE);
689                 return 0;
690                 }
691
692         SMIME_crlf_copy(data, cmsbio, flags);
693
694         (void)BIO_flush(cmsbio);
695
696
697         if (!CMS_dataFinal(cms, cmsbio))
698                 {
699                 CMSerr(CMS_F_CMS_FINAL,CMS_R_CMS_DATAFINAL_ERROR);
700                 goto err;
701                 }
702
703         ret = 1;
704
705         err:
706         BIO_free_all(cmsbio);
707
708         return ret;
709
710         }
711
712 #ifdef ZLIB
713
714 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
715                                                         unsigned int flags)
716         {
717         BIO *cont;
718         int r;
719         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData)
720                 {
721                 CMSerr(CMS_F_CMS_UNCOMPRESS,
722                                         CMS_R_TYPE_NOT_COMPRESSED_DATA);
723                 return 0;
724                 }
725
726         if (!dcont && !check_content(cms))
727                 return 0;
728
729         cont = CMS_dataInit(cms, dcont);
730         if (!cont)
731                 return 0;
732         r = cms_copy_content(out, cont, flags);
733         BIO_free_all(cont);
734         return r;
735         }
736
737 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
738         {
739         CMS_ContentInfo *cms;
740         if (comp_nid <= 0)
741                 comp_nid = NID_zlib_compression;
742         cms = cms_CompressedData_create(comp_nid);
743         if (!cms)
744                 return NULL;
745
746         if(!(flags & CMS_DETACHED))
747                 CMS_set_detached(cms, 0);
748
749         if ((flags & CMS_STREAM) || CMS_final(cms, in, flags))
750                 return cms;
751
752         CMS_ContentInfo_free(cms);
753         return NULL;
754         }
755
756 #else
757
758 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
759                                                         unsigned int flags)
760         {
761         CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
762         return 0;
763         }
764
765 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
766         {
767         CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
768         return NULL;
769         }
770
771 #endif