Import OpenSSL 0.9.8c
[dragonfly.git] / crypto / openssl-0.9 / crypto / pkcs7 / pk7_smime.c
1 /* pk7_smime.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2004 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  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 /* Simple PKCS#7 processing functions */
60
61 #include <stdio.h>
62 #include "cryptlib.h"
63 #include <openssl/x509.h>
64 #include <openssl/x509v3.h>
65
66 PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
67                   BIO *data, int flags)
68 {
69         PKCS7 *p7;
70         PKCS7_SIGNER_INFO *si;
71         BIO *p7bio;
72         STACK_OF(X509_ALGOR) *smcap;
73         int i;
74
75         if(!X509_check_private_key(signcert, pkey)) {
76                 PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
77                 return NULL;
78         }
79
80         if(!(p7 = PKCS7_new())) {
81                 PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE);
82                 return NULL;
83         }
84
85         PKCS7_set_type(p7, NID_pkcs7_signed);
86
87         PKCS7_content_new(p7, NID_pkcs7_data);
88
89         if (!(si = PKCS7_add_signature(p7,signcert,pkey,EVP_sha1()))) {
90                 PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
91                 PKCS7_free(p7);
92                 return NULL;
93         }
94
95         if(!(flags & PKCS7_NOCERTS)) {
96                 PKCS7_add_certificate(p7, signcert);
97                 if(certs) for(i = 0; i < sk_X509_num(certs); i++)
98                         PKCS7_add_certificate(p7, sk_X509_value(certs, i));
99         }
100
101         if(!(flags & PKCS7_NOATTR)) {
102                 PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
103                                 V_ASN1_OBJECT, OBJ_nid2obj(NID_pkcs7_data));
104                 /* Add SMIMECapabilities */
105                 if(!(flags & PKCS7_NOSMIMECAP))
106                 {
107                 if(!(smcap = sk_X509_ALGOR_new_null())) {
108                         PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE);
109                         PKCS7_free(p7);
110                         return NULL;
111                 }
112 #ifndef OPENSSL_NO_DES
113                 PKCS7_simple_smimecap (smcap, NID_des_ede3_cbc, -1);
114 #endif
115 #ifndef OPENSSL_NO_RC2
116                 PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 128);
117                 PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 64);
118 #endif
119 #ifndef OPENSSL_NO_DES
120                 PKCS7_simple_smimecap (smcap, NID_des_cbc, -1);
121 #endif
122 #ifndef OPENSSL_NO_RC2
123                 PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 40);
124 #endif
125                 PKCS7_add_attrib_smimecap (si, smcap);
126                 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
127                 }
128         }
129
130         if(flags & PKCS7_DETACHED)PKCS7_set_detached(p7, 1);
131
132         if (flags & PKCS7_STREAM)
133                 return p7;
134
135
136         if (!(p7bio = PKCS7_dataInit(p7, NULL))) {
137                 PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE);
138                 PKCS7_free(p7);
139                 return NULL;
140         }
141
142         SMIME_crlf_copy(data, p7bio, flags);
143
144
145         if (!PKCS7_dataFinal(p7,p7bio)) {
146                 PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PKCS7_DATASIGN);
147                 PKCS7_free(p7);
148                 BIO_free_all(p7bio);
149                 return NULL;
150         }
151
152         BIO_free_all(p7bio);
153         return p7;
154 }
155
156 int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
157                                         BIO *indata, BIO *out, int flags)
158 {
159         STACK_OF(X509) *signers;
160         X509 *signer;
161         STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
162         PKCS7_SIGNER_INFO *si;
163         X509_STORE_CTX cert_ctx;
164         char buf[4096];
165         int i, j=0, k, ret = 0;
166         BIO *p7bio;
167         BIO *tmpin, *tmpout;
168
169         if(!p7) {
170                 PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_INVALID_NULL_POINTER);
171                 return 0;
172         }
173
174         if(!PKCS7_type_is_signed(p7)) {
175                 PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_WRONG_CONTENT_TYPE);
176                 return 0;
177         }
178
179         /* Check for no data and no content: no data to verify signature */
180         if(PKCS7_get_detached(p7) && !indata) {
181                 PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_NO_CONTENT);
182                 return 0;
183         }
184 #if 0
185         /* NB: this test commented out because some versions of Netscape
186          * illegally include zero length content when signing data.
187          */
188
189         /* Check for data and content: two sets of data */
190         if(!PKCS7_get_detached(p7) && indata) {
191                                 PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_CONTENT_AND_DATA_PRESENT);
192                 return 0;
193         }
194 #endif
195
196         sinfos = PKCS7_get_signer_info(p7);
197
198         if(!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
199                 PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_NO_SIGNATURES_ON_DATA);
200                 return 0;
201         }
202
203
204         signers = PKCS7_get0_signers(p7, certs, flags);
205
206         if(!signers) return 0;
207
208         /* Now verify the certificates */
209
210         if (!(flags & PKCS7_NOVERIFY)) for (k = 0; k < sk_X509_num(signers); k++) {
211                 signer = sk_X509_value (signers, k);
212                 if (!(flags & PKCS7_NOCHAIN)) {
213                         if(!X509_STORE_CTX_init(&cert_ctx, store, signer,
214                                                         p7->d.sign->cert))
215                                 {
216                                 PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_X509_LIB);
217                                 sk_X509_free(signers);
218                                 return 0;
219                                 }
220                         X509_STORE_CTX_set_purpose(&cert_ctx,
221                                                 X509_PURPOSE_SMIME_SIGN);
222                 } else if(!X509_STORE_CTX_init (&cert_ctx, store, signer, NULL)) {
223                         PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_X509_LIB);
224                         sk_X509_free(signers);
225                         return 0;
226                 }
227                 if (!(flags & PKCS7_NOCRL))
228                         X509_STORE_CTX_set0_crls(&cert_ctx, p7->d.sign->crl);
229                 i = X509_verify_cert(&cert_ctx);
230                 if (i <= 0) j = X509_STORE_CTX_get_error(&cert_ctx);
231                 X509_STORE_CTX_cleanup(&cert_ctx);
232                 if (i <= 0) {
233                         PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_CERTIFICATE_VERIFY_ERROR);
234                         ERR_add_error_data(2, "Verify error:",
235                                          X509_verify_cert_error_string(j));
236                         sk_X509_free(signers);
237                         return 0;
238                 }
239                 /* Check for revocation status here */
240         }
241
242         /* Performance optimization: if the content is a memory BIO then
243          * store its contents in a temporary read only memory BIO. This
244          * avoids potentially large numbers of slow copies of data which will
245          * occur when reading from a read write memory BIO when signatures
246          * are calculated.
247          */
248
249         if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM))
250                 {
251                 char *ptr;
252                 long len;
253                 len = BIO_get_mem_data(indata, &ptr);
254                 tmpin = BIO_new_mem_buf(ptr, len);
255                 if (tmpin == NULL)
256                         {
257                         PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_MALLOC_FAILURE);
258                         return 0;
259                         }
260                 }
261         else
262                 tmpin = indata;
263                 
264
265         p7bio=PKCS7_dataInit(p7,tmpin);
266
267         if(flags & PKCS7_TEXT) {
268                 if(!(tmpout = BIO_new(BIO_s_mem()))) {
269                         PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_MALLOC_FAILURE);
270                         goto err;
271                 }
272         } else tmpout = out;
273
274         /* We now have to 'read' from p7bio to calculate digests etc. */
275         for (;;)
276         {
277                 i=BIO_read(p7bio,buf,sizeof(buf));
278                 if (i <= 0) break;
279                 if (tmpout) BIO_write(tmpout, buf, i);
280         }
281
282         if(flags & PKCS7_TEXT) {
283                 if(!SMIME_text(tmpout, out)) {
284                         PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_SMIME_TEXT_ERROR);
285                         BIO_free(tmpout);
286                         goto err;
287                 }
288                 BIO_free(tmpout);
289         }
290
291         /* Now Verify All Signatures */
292         if (!(flags & PKCS7_NOSIGS))
293             for (i=0; i<sk_PKCS7_SIGNER_INFO_num(sinfos); i++)
294                 {
295                 si=sk_PKCS7_SIGNER_INFO_value(sinfos,i);
296                 signer = sk_X509_value (signers, i);
297                 j=PKCS7_signatureVerify(p7bio,p7,si, signer);
298                 if (j <= 0) {
299                         PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_SIGNATURE_FAILURE);
300                         goto err;
301                 }
302         }
303
304         ret = 1;
305
306         err:
307         
308         if (tmpin == indata)
309                 {
310                 if (indata) BIO_pop(p7bio);
311                 }
312         BIO_free_all(p7bio);
313
314         sk_X509_free(signers);
315
316         return ret;
317 }
318
319 STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags)
320 {
321         STACK_OF(X509) *signers;
322         STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
323         PKCS7_SIGNER_INFO *si;
324         PKCS7_ISSUER_AND_SERIAL *ias;
325         X509 *signer;
326         int i;
327
328         if(!p7) {
329                 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_INVALID_NULL_POINTER);
330                 return NULL;
331         }
332
333         if(!PKCS7_type_is_signed(p7)) {
334                 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_WRONG_CONTENT_TYPE);
335                 return NULL;
336         }
337
338         /* Collect all the signers together */
339
340         sinfos = PKCS7_get_signer_info(p7);
341
342         if(sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
343                 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_NO_SIGNERS);
344                 return 0;
345         }
346
347         if(!(signers = sk_X509_new_null())) {
348                 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,ERR_R_MALLOC_FAILURE);
349                 return NULL;
350         }
351
352         for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++)
353         {
354             si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
355             ias = si->issuer_and_serial;
356             signer = NULL;
357                 /* If any certificates passed they take priority */
358             if (certs) signer = X509_find_by_issuer_and_serial (certs,
359                                                 ias->issuer, ias->serial);
360             if (!signer && !(flags & PKCS7_NOINTERN)
361                         && p7->d.sign->cert) signer =
362                               X509_find_by_issuer_and_serial (p7->d.sign->cert,
363                                                 ias->issuer, ias->serial);
364             if (!signer) {
365                         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
366                         sk_X509_free(signers);
367                         return 0;
368             }
369
370             sk_X509_push(signers, signer);
371         }
372         return signers;
373 }
374
375
376 /* Build a complete PKCS#7 enveloped data */
377
378 PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
379                                                                 int flags)
380 {
381         PKCS7 *p7;
382         BIO *p7bio = NULL;
383         int i;
384         X509 *x509;
385         if(!(p7 = PKCS7_new())) {
386                 PKCS7err(PKCS7_F_PKCS7_ENCRYPT,ERR_R_MALLOC_FAILURE);
387                 return NULL;
388         }
389
390         PKCS7_set_type(p7, NID_pkcs7_enveloped);
391         if(!PKCS7_set_cipher(p7, cipher)) {
392                 PKCS7err(PKCS7_F_PKCS7_ENCRYPT,PKCS7_R_ERROR_SETTING_CIPHER);
393                 goto err;
394         }
395
396         for(i = 0; i < sk_X509_num(certs); i++) {
397                 x509 = sk_X509_value(certs, i);
398                 if(!PKCS7_add_recipient(p7, x509)) {
399                         PKCS7err(PKCS7_F_PKCS7_ENCRYPT,
400                                         PKCS7_R_ERROR_ADDING_RECIPIENT);
401                         goto err;
402                 }
403         }
404
405         if(!(p7bio = PKCS7_dataInit(p7, NULL))) {
406                 PKCS7err(PKCS7_F_PKCS7_ENCRYPT,ERR_R_MALLOC_FAILURE);
407                 goto err;
408         }
409
410         SMIME_crlf_copy(in, p7bio, flags);
411
412         BIO_flush(p7bio);
413
414         if (!PKCS7_dataFinal(p7,p7bio)) {
415                 PKCS7err(PKCS7_F_PKCS7_ENCRYPT,PKCS7_R_PKCS7_DATAFINAL_ERROR);
416                 goto err;
417         }
418         BIO_free_all(p7bio);
419
420         return p7;
421
422         err:
423
424         BIO_free(p7bio);
425         PKCS7_free(p7);
426         return NULL;
427
428 }
429
430 int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
431 {
432         BIO *tmpmem;
433         int ret, i;
434         char buf[4096];
435
436         if(!p7) {
437                 PKCS7err(PKCS7_F_PKCS7_DECRYPT,PKCS7_R_INVALID_NULL_POINTER);
438                 return 0;
439         }
440
441         if(!PKCS7_type_is_enveloped(p7)) {
442                 PKCS7err(PKCS7_F_PKCS7_DECRYPT,PKCS7_R_WRONG_CONTENT_TYPE);
443                 return 0;
444         }
445
446         if(cert && !X509_check_private_key(cert, pkey)) {
447                 PKCS7err(PKCS7_F_PKCS7_DECRYPT,
448                                 PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
449                 return 0;
450         }
451
452         if(!(tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert))) {
453                 PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR);
454                 return 0;
455         }
456
457         if (flags & PKCS7_TEXT) {
458                 BIO *tmpbuf, *bread;
459                 /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
460                 if(!(tmpbuf = BIO_new(BIO_f_buffer()))) {
461                         PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
462                         return 0;
463                 }
464                 if(!(bread = BIO_push(tmpbuf, tmpmem))) {
465                         PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
466                         return 0;
467                 }
468                 ret = SMIME_text(bread, data);
469                 BIO_free_all(bread);
470                 return ret;
471         } else {
472                 for(;;) {
473                         i = BIO_read(tmpmem, buf, sizeof(buf));
474                         if(i <= 0) break;
475                         BIO_write(data, buf, i);
476                 }
477                 BIO_free_all(tmpmem);
478                 return 1;
479         }
480 }