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