Reorganise encrypted content info code to avoid duplication and be more
[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(flags & CMS_TEXT)
69                 {
70                 tmpout = BIO_new(BIO_s_mem());
71                 if(!tmpout)
72                         {
73                         CMSerr(CMS_F_CMS_COPY_CONTENT,ERR_R_MALLOC_FAILURE);
74                         goto err;
75                         }
76                 }
77         else
78                 tmpout = out;
79
80         /* Read all content through chain to determine content digests */
81         for (;;)
82         {
83                 i=BIO_read(in,buf,sizeof(buf));
84                 if (i <= 0)
85                         break;
86                 if (tmpout)
87                         BIO_write(tmpout, buf, i);
88         }
89
90         if(flags & CMS_TEXT)
91                 {
92                 if(!SMIME_text(tmpout, out))
93                         {
94                         CMSerr(CMS_F_CMS_COPY_CONTENT,CMS_R_SMIME_TEXT_ERROR);
95                         goto err;
96                         }
97                 }
98
99         r = 1;
100
101         err:
102         if (tmpout && (tmpout != out))
103                 BIO_free(tmpout);
104         return r;
105
106         }
107
108 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
109         {
110         BIO *cont;
111         int r;
112         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data)
113                 {
114                 CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
115                 return 0;
116                 }
117         cont = CMS_dataInit(cms, NULL);
118         if (!cont)
119                 return 0;
120         r = cms_copy_content(out, cont, flags);
121         BIO_free_all(cont);
122         return r;
123         }
124
125 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
126         {
127         CMS_ContentInfo *cms;
128         cms = cms_Data_create();
129         if (!cms)
130                 return NULL;
131
132         if ((flags & CMS_STREAM) || CMS_final(cms, in, flags))
133                 return cms;
134
135         CMS_ContentInfo_free(cms);
136
137         return NULL;
138         }
139
140 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
141                                                         unsigned int flags)
142         {
143         BIO *cont;
144         int r;
145         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest)
146                 {
147                 CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
148                 return 0;
149                 }
150
151         if (!dcont)
152                 {
153                 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
154                 if (!pos || !*pos)
155                         {
156                         CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_NO_CONTENT);
157                         return 0;
158                         }
159                 }
160
161         cont = CMS_dataInit(cms, dcont);
162         if (!cont)
163                 return 0;
164         r = cms_copy_content(out, cont, flags);
165         if (r)
166                 r = cms_DigestedData_do_final(cms, cont, 1);
167         BIO_free_all(cont);
168         return r;
169         }
170
171 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
172                                         unsigned int flags)
173         {
174         CMS_ContentInfo *cms;
175         if (!md)
176                 md = EVP_sha1();
177         cms = cms_DigestedData_create(md);
178         if (!cms)
179                 return NULL;
180
181         if(!(flags & CMS_DETACHED))
182                 CMS_set_detached(cms, 0);
183
184         if ((flags & CMS_STREAM) || CMS_final(cms, in, flags))
185                 return cms;
186
187         CMS_ContentInfo_free(cms);
188         return NULL;
189         }
190
191 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
192                                 const unsigned char *key, size_t keylen,
193                                 BIO *dcont, BIO *out, unsigned int flags)
194         {
195         BIO *cont;
196         int r;
197         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted)
198                 {
199                 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
200                                         CMS_R_TYPE_NOT_ENCRYPTED_DATA);
201                 return 0;
202                 }
203
204         if (!dcont)
205                 {
206                 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
207                 if (!pos || !*pos)
208                         {
209                         CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
210                                         CMS_R_NO_CONTENT);
211                         return 0;
212                         }
213                 }
214
215         if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
216                 return 0;
217         cont = CMS_dataInit(cms, dcont);
218         if (!cont)
219                 return 0;
220         r = cms_copy_content(out, cont, flags);
221         BIO_free_all(cont);
222         return r;
223         }
224
225 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
226                                         const unsigned char *key, size_t keylen,
227                                         unsigned int flags)
228         {
229         CMS_ContentInfo *cms;
230         cms = CMS_ContentInfo_new();
231         if (!cms)
232                 return NULL;
233         if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
234                 return NULL;
235
236         if(!(flags & CMS_DETACHED))
237                 CMS_set_detached(cms, 0);
238
239         if ((flags & (CMS_STREAM|CMS_PARTIAL)) || CMS_final(cms, in, flags))
240                 return cms;
241
242         CMS_ContentInfo_free(cms);
243         return NULL;
244         }
245
246 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
247                                         X509_STORE *store,
248                                         STACK_OF(X509) *certs,
249                                         STACK_OF(X509_CRL) *crls,
250                                         unsigned int flags)
251         {
252         X509_STORE_CTX ctx;
253         X509 *signer;
254         int i, j, r = 0;
255         CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
256         if (!X509_STORE_CTX_init(&ctx, store, signer, certs))
257                 {
258                 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
259                                                 CMS_R_STORE_INIT_ERROR);
260                 goto err;
261                 }
262         X509_STORE_CTX_set_purpose(&ctx, X509_PURPOSE_SMIME_SIGN);
263         if (crls)
264                 X509_STORE_CTX_set0_crls(&ctx, crls);
265
266         i = X509_verify_cert(&ctx);
267         if (i <= 0)
268                 {
269                 j = X509_STORE_CTX_get_error(&ctx);
270                 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
271                                                 CMS_R_CERTIFICATE_VERIFY_ERROR);
272                 ERR_add_error_data(2, "Verify error:",
273                                          X509_verify_cert_error_string(j));
274                 goto err;
275                 }
276         r = 1;
277         err:
278         X509_STORE_CTX_cleanup(&ctx);
279         return r;
280
281         }
282
283 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
284                  X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
285         {
286         CMS_SignerInfo *si;
287         STACK_OF(CMS_SignerInfo) *sinfos;
288         STACK_OF(X509) *cms_certs = NULL;
289         STACK_OF(X509_CRL) *crls = NULL;
290         X509 *signer;
291         int i, scount = 0, ret = 0;
292         BIO *cmsbio = NULL, *tmpin = NULL;
293
294         if (!dcont)
295                 {
296                 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
297                 if (!pos || !*pos)
298                         {
299                         CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_CONTENT);
300                         return 0;
301                         }
302                 }
303
304         /* Attempt to find all signer certificates */
305
306         sinfos = CMS_get0_SignerInfos(cms);
307
308         if (sk_CMS_SignerInfo_num(sinfos) <= 0)
309                 {
310                 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
311                 goto err;
312                 }
313
314         for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
315                 {
316                 si = sk_CMS_SignerInfo_value(sinfos, i);
317                 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
318                 if (signer)
319                         scount++;
320                 }
321
322         if (scount != sk_CMS_SignerInfo_num(sinfos))
323                 scount += CMS_set1_signers_certs(cms, certs, flags);
324
325         if (scount != sk_CMS_SignerInfo_num(sinfos))
326                 {
327                 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
328                 goto err;
329                 }
330
331         /* Attempt to verify all signers certs */
332
333         if (!(flags & CMS_NO_SIGNER_CERT_VERIFY))
334                 {
335                 cms_certs = CMS_get1_certs(cms);
336                 crls = CMS_get1_crls(cms);
337                 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
338                         {
339                         si = sk_CMS_SignerInfo_value(sinfos, i);
340                         if (!cms_signerinfo_verify_cert(si, store,
341                                                         cms_certs, crls, flags))
342                                 goto err;
343                         }
344                 }
345
346         /* Attempt to verify all SignerInfo signed attribute signatures */
347
348         if (!(flags & CMS_NO_ATTR_VERIFY))
349                 {
350                 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
351                         {
352                         si = sk_CMS_SignerInfo_value(sinfos, i);
353                         if (CMS_signed_get_attr_count(si) < 0)
354                                 continue;
355                         if (CMS_SignerInfo_verify(si) <= 0)
356                                 goto err;
357                         }
358                 }
359
360         /* Performance optimization: if the content is a memory BIO then
361          * store its contents in a temporary read only memory BIO. This
362          * avoids potentially large numbers of slow copies of data which will
363          * occur when reading from a read write memory BIO when signatures
364          * are calculated.
365          */
366
367         if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM))
368                 {
369                 char *ptr;
370                 long len;
371                 len = BIO_get_mem_data(dcont, &ptr);
372                 tmpin = BIO_new_mem_buf(ptr, len);
373                 if (tmpin == NULL)
374                         {
375                         CMSerr(CMS_F_CMS_VERIFY,ERR_R_MALLOC_FAILURE);
376                         return 0;
377                         }
378                 }
379         else
380                 tmpin = dcont;
381                 
382
383         cmsbio=CMS_dataInit(cms, tmpin);
384         if (!cmsbio)
385                 goto err;
386
387         if (!cms_copy_content(out, cmsbio, flags))
388                 goto err;
389
390         if (!(flags & CMS_NO_CONTENT_VERIFY))
391                 {
392                 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
393                         {
394                         si = sk_CMS_SignerInfo_value(sinfos, i);
395                         if (!CMS_SignerInfo_verify_content(si, cmsbio))
396                                 {
397                                 CMSerr(CMS_F_CMS_VERIFY,
398                                         CMS_R_CONTENT_VERIFY_ERROR);
399                                 goto err;
400                                 }
401                         }
402                 }
403
404         ret = 1;
405
406         err:
407         
408         if (dcont && (tmpin == dcont))
409                 BIO_pop(cmsbio);
410         BIO_free_all(cmsbio);
411
412         if (cms_certs)
413                 sk_X509_pop_free(cms_certs, X509_free);
414         if (crls)
415                 sk_X509_CRL_pop_free(crls, X509_CRL_free);
416
417         return ret;
418         }
419
420 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
421                                                 BIO *data, unsigned int flags)
422         {
423         CMS_ContentInfo *cms;
424         int i;
425         cms = CMS_ContentInfo_new();
426         if (!cms)
427                 goto merr;
428         if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags))
429                 {
430                 CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
431                 goto err;
432                 }
433         for (i = 0; i < sk_X509_num(certs); i++)
434                 {
435                 X509 *x = sk_X509_value(certs, i);
436                 if (!CMS_add1_cert(cms, x))
437                         goto merr;
438                 }
439         /* If no signer or certs initialize signedData */
440         if (!pkey && !i && !CMS_SignedData_init(cms))
441                 goto merr;
442
443         if(!(flags & CMS_DETACHED))
444                 CMS_set_detached(cms, 0);
445
446         if ((flags & (CMS_STREAM|CMS_PARTIAL)) || CMS_final(cms, data, flags))
447                 return cms;
448
449         return cms;
450
451         merr:
452         CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
453
454         err:
455         if (cms)
456                 CMS_ContentInfo_free(cms);
457         return NULL;
458         }
459
460 /* Placeholders for now... */
461
462 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *in,
463                                 const EVP_CIPHER *cipher, unsigned int flags)
464         {
465         return NULL;
466         }
467         
468 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pkey, X509 *cert, BIO *data,
469                                 unsigned int flags)
470         {
471         return 0;
472         }
473
474 int CMS_final(CMS_ContentInfo *cms, BIO *data, int flags)
475         {
476         BIO *cmsbio;
477         int ret = 0;
478         if (!(cmsbio = CMS_dataInit(cms, NULL)))
479                 {
480                 CMSerr(CMS_F_CMS_FINAL,ERR_R_MALLOC_FAILURE);
481                 return 0;
482                 }
483
484         SMIME_crlf_copy(data, cmsbio, flags);
485
486         (void)BIO_flush(cmsbio);
487
488
489         if (!CMS_dataFinal(cms, cmsbio))
490                 {
491                 CMSerr(CMS_F_CMS_FINAL,CMS_R_CMS_DATAFINAL_ERROR);
492                 goto err;
493                 }
494
495         ret = 1;
496
497         err:
498         BIO_free_all(cmsbio);
499
500         return ret;
501
502         }
503
504 #ifdef ZLIB
505
506 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
507                                                         unsigned int flags)
508         {
509         BIO *cont;
510         int r;
511         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData)
512                 {
513                 CMSerr(CMS_F_CMS_UNCOMPRESS,
514                                         CMS_R_TYPE_NOT_COMPRESSED_DATA);
515                 return 0;
516                 }
517
518         if (!dcont)
519                 {
520                 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
521                 if (!pos || !*pos)
522                         {
523                         CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_NO_CONTENT);
524                         return 0;
525                         }
526                 }
527
528         cont = CMS_dataInit(cms, dcont);
529         if (!cont)
530                 return 0;
531         r = cms_copy_content(out, cont, flags);
532         BIO_free_all(cont);
533         return r;
534         }
535
536 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
537         {
538         CMS_ContentInfo *cms;
539         if (comp_nid <= 0)
540                 comp_nid = NID_zlib_compression;
541         cms = cms_CompressedData_create(comp_nid);
542         if (!cms)
543                 return NULL;
544
545         if(!(flags & CMS_DETACHED))
546                 CMS_set_detached(cms, 0);
547
548         if ((flags & CMS_STREAM) || CMS_final(cms, in, flags))
549                 return cms;
550
551         CMS_ContentInfo_free(cms);
552         return NULL;
553         }
554
555 #else
556
557 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
558                                                         unsigned int flags)
559         {
560         CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
561         return 0;
562         }
563
564 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
565         {
566         CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
567         return NULL;
568         }
569
570 #endif