And so it begins...
[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 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
192                                         X509_STORE *store,
193                                         STACK_OF(X509) *certs,
194                                         STACK_OF(X509_CRL) *crls,
195                                         unsigned int flags)
196         {
197         X509_STORE_CTX ctx;
198         X509 *signer;
199         int i, j, r = 0;
200         CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
201         if (!X509_STORE_CTX_init(&ctx, store, signer, certs))
202                 {
203                 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
204                                                 CMS_R_STORE_INIT_ERROR);
205                 goto err;
206                 }
207         X509_STORE_CTX_set_purpose(&ctx, X509_PURPOSE_SMIME_SIGN);
208         if (crls)
209                 X509_STORE_CTX_set0_crls(&ctx, crls);
210
211         i = X509_verify_cert(&ctx);
212         if (i <= 0)
213                 {
214                 j = X509_STORE_CTX_get_error(&ctx);
215                 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
216                                                 CMS_R_CERTIFICATE_VERIFY_ERROR);
217                 ERR_add_error_data(2, "Verify error:",
218                                          X509_verify_cert_error_string(j));
219                 goto err;
220                 }
221         r = 1;
222         err:
223         X509_STORE_CTX_cleanup(&ctx);
224         return r;
225
226         }
227
228 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
229                  X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
230         {
231         CMS_SignerInfo *si;
232         STACK_OF(CMS_SignerInfo) *sinfos;
233         STACK_OF(X509) *cms_certs = NULL;
234         STACK_OF(X509_CRL) *crls = NULL;
235         X509 *signer;
236         int i, scount = 0, ret = 0;
237         BIO *cmsbio = NULL, *tmpin = NULL;
238
239         if (!dcont)
240                 {
241                 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
242                 if (!pos || !*pos)
243                         {
244                         CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_CONTENT);
245                         return 0;
246                         }
247                 }
248
249         /* Attempt to find all signer certificates */
250
251         sinfos = CMS_get0_SignerInfos(cms);
252
253         if (sk_CMS_SignerInfo_num(sinfos) <= 0)
254                 {
255                 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
256                 goto err;
257                 }
258
259         for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
260                 {
261                 si = sk_CMS_SignerInfo_value(sinfos, i);
262                 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
263                 if (signer)
264                         scount++;
265                 }
266
267         if (scount != sk_CMS_SignerInfo_num(sinfos))
268                 scount += CMS_set1_signers_certs(cms, certs, flags);
269
270         if (scount != sk_CMS_SignerInfo_num(sinfos))
271                 {
272                 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
273                 goto err;
274                 }
275
276         /* Attempt to verify all signers certs */
277
278         if (!(flags & CMS_NO_SIGNER_CERT_VERIFY))
279                 {
280                 cms_certs = CMS_get1_certs(cms);
281                 crls = CMS_get1_crls(cms);
282                 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
283                         {
284                         si = sk_CMS_SignerInfo_value(sinfos, i);
285                         if (!cms_signerinfo_verify_cert(si, store,
286                                                         cms_certs, crls, flags))
287                                 goto err;
288                         }
289                 }
290
291         /* Attempt to verify all SignerInfo signed attribute signatures */
292
293         if (!(flags & CMS_NO_ATTR_VERIFY))
294                 {
295                 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
296                         {
297                         si = sk_CMS_SignerInfo_value(sinfos, i);
298                         if (CMS_signed_get_attr_count(si) < 0)
299                                 continue;
300                         if (CMS_SignerInfo_verify(si) <= 0)
301                                 goto err;
302                         }
303                 }
304
305         /* Performance optimization: if the content is a memory BIO then
306          * store its contents in a temporary read only memory BIO. This
307          * avoids potentially large numbers of slow copies of data which will
308          * occur when reading from a read write memory BIO when signatures
309          * are calculated.
310          */
311
312         if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM))
313                 {
314                 char *ptr;
315                 long len;
316                 len = BIO_get_mem_data(dcont, &ptr);
317                 tmpin = BIO_new_mem_buf(ptr, len);
318                 if (tmpin == NULL)
319                         {
320                         CMSerr(CMS_F_CMS_VERIFY,ERR_R_MALLOC_FAILURE);
321                         return 0;
322                         }
323                 }
324         else
325                 tmpin = dcont;
326                 
327
328         cmsbio=CMS_dataInit(cms, tmpin);
329         if (!cmsbio)
330                 goto err;
331
332         if (!cms_copy_content(out, cmsbio, flags))
333                 goto err;
334
335         if (!(flags & CMS_NO_CONTENT_VERIFY))
336                 {
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_content(si, cmsbio))
341                                 {
342                                 CMSerr(CMS_F_CMS_VERIFY,
343                                         CMS_R_CONTENT_VERIFY_ERROR);
344                                 goto err;
345                                 }
346                         }
347                 }
348
349         ret = 1;
350
351         err:
352         
353         if (dcont && (tmpin == dcont))
354                 BIO_pop(cmsbio);
355         BIO_free_all(cmsbio);
356
357         if (cms_certs)
358                 sk_X509_pop_free(cms_certs, X509_free);
359         if (crls)
360                 sk_X509_CRL_pop_free(crls, X509_CRL_free);
361
362         return ret;
363         }
364
365 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
366                                                 BIO *data, unsigned int flags)
367         {
368         CMS_ContentInfo *cms;
369         int i;
370         cms = CMS_ContentInfo_new();
371         if (!cms)
372                 goto merr;
373         if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags))
374                 {
375                 CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
376                 goto err;
377                 }
378         for (i = 0; i < sk_X509_num(certs); i++)
379                 {
380                 X509 *x = sk_X509_value(certs, i);
381                 if (!CMS_add1_cert(cms, x))
382                         goto merr;
383                 }
384         /* If no signer or certs initialize signedData */
385         if (!pkey && !i && !CMS_SignedData_init(cms))
386                 goto merr;
387
388         if(!(flags & CMS_DETACHED))
389                 CMS_set_detached(cms, 0);
390
391         if ((flags & (CMS_STREAM|CMS_PARTIAL)) || CMS_final(cms, data, flags))
392                 return cms;
393
394         return cms;
395
396         merr:
397         CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
398
399         err:
400         if (cms)
401                 CMS_ContentInfo_free(cms);
402         return NULL;
403         }
404
405 /* Placeholders for now... */
406
407 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *in,
408                                 const EVP_CIPHER *cipher, unsigned int flags)
409         {
410         return NULL;
411         }
412         
413 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pkey, X509 *cert, BIO *data,
414                                 unsigned int flags)
415         {
416         return 0;
417         }
418
419 int CMS_final(CMS_ContentInfo *cms, BIO *data, int flags)
420         {
421         BIO *cmsbio;
422         int ret = 0;
423         if (!(cmsbio = CMS_dataInit(cms, NULL)))
424                 {
425                 CMSerr(CMS_F_CMS_FINAL,ERR_R_MALLOC_FAILURE);
426                 return 0;
427                 }
428
429         SMIME_crlf_copy(data, cmsbio, flags);
430
431         (void)BIO_flush(cmsbio);
432
433
434         if (!CMS_dataFinal(cms, cmsbio))
435                 {
436                 CMSerr(CMS_F_CMS_FINAL,CMS_R_CMS_DATAFINAL_ERROR);
437                 goto err;
438                 }
439
440         ret = 1;
441
442         err:
443         BIO_free_all(cmsbio);
444
445         return ret;
446
447         }
448
449 #ifdef ZLIB
450
451 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
452                                                         unsigned int flags)
453         {
454         BIO *cont;
455         int r;
456         if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData)
457                 {
458                 CMSerr(CMS_F_CMS_UNCOMPRESS,
459                                         CMS_R_TYPE_NOT_COMPRESSED_DATA);
460                 return 0;
461                 }
462
463         if (!dcont)
464                 {
465                 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
466                 if (!pos || !*pos)
467                         {
468                         CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_NO_CONTENT);
469                         return 0;
470                         }
471                 }
472
473         cont = CMS_dataInit(cms, dcont);
474         if (!cont)
475                 return 0;
476         r = cms_copy_content(out, cont, flags);
477         BIO_free_all(cont);
478         return r;
479         }
480
481 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
482         {
483         CMS_ContentInfo *cms;
484         if (comp_nid <= 0)
485                 comp_nid = NID_zlib_compression;
486         cms = cms_CompressedData_create(comp_nid);
487         if (!cms)
488                 return NULL;
489
490         if(!(flags & CMS_DETACHED))
491                 CMS_set_detached(cms, 0);
492
493         if ((flags & CMS_STREAM) || CMS_final(cms, in, flags))
494                 return cms;
495
496         CMS_ContentInfo_free(cms);
497         return NULL;
498         }
499
500 #else
501
502 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
503                                                         unsigned int flags)
504         {
505         CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
506         return 0;
507         }
508
509 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
510         {
511         CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
512         return NULL;
513         }
514
515 #endif