0d87960d69e315cd4f686e032de2fc54f7158c9e
[openssl.git] / apps / smime.c
1 /* smime.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 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 /* S/MIME utility function */
60
61 #include <stdio.h>
62 #include <string.h>
63 #include "apps.h"
64 #include <openssl/crypto.h>
65 #include <openssl/pem.h>
66 #include <openssl/err.h>
67
68 #undef PROG
69 #define PROG smime_main
70 static X509 *load_cert(char *file);
71 static EVP_PKEY *load_key(char *file, char *pass);
72 static STACK_OF(X509) *load_certs(char *file);
73 static X509_STORE *setup_verify(char *CAfile, char *CApath);
74 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
75
76 #define SMIME_OP        0x10
77 #define SMIME_ENCRYPT   (1 | SMIME_OP)
78 #define SMIME_DECRYPT   2
79 #define SMIME_SIGN      (3 | SMIME_OP)
80 #define SMIME_VERIFY    4
81 #define SMIME_PK7OUT    5
82
83 int MAIN(int, char **);
84
85 int MAIN(int argc, char **argv)
86 {
87         int operation = 0;
88         int ret = 0;
89         char **args;
90         char *inmode = "r", *outmode = "w";
91         char *infile = NULL, *outfile = NULL;
92         char *signerfile = NULL, *recipfile = NULL;
93         char *certfile = NULL, *keyfile = NULL;
94         EVP_CIPHER *cipher = NULL;
95         PKCS7 *p7 = NULL;
96         X509_STORE *store = NULL;
97         X509 *cert = NULL, *recip = NULL, *signer = NULL;
98         EVP_PKEY *key = NULL;
99         STACK_OF(X509) *encerts = NULL, *other = NULL;
100         BIO *in = NULL, *out = NULL, *indata = NULL;
101         int badarg = 0;
102         int flags = PKCS7_DETACHED;
103         char *to = NULL, *from = NULL, *subject = NULL;
104         char *CAfile = NULL, *CApath = NULL, *passin = NULL;
105         char *inrand = NULL;
106         int need_rand = 0;
107         args = argv + 1;
108
109         ret = 1;
110
111         while (!badarg && *args && *args[0] == '-') {
112                 if (!strcmp (*args, "-encrypt")) operation = SMIME_ENCRYPT;
113                 else if (!strcmp (*args, "-decrypt")) operation = SMIME_DECRYPT;
114                 else if (!strcmp (*args, "-sign")) operation = SMIME_SIGN;
115                 else if (!strcmp (*args, "-verify")) operation = SMIME_VERIFY;
116                 else if (!strcmp (*args, "-pk7out")) operation = SMIME_PK7OUT;
117 #ifndef NO_DES
118                 else if (!strcmp (*args, "-des3")) 
119                                 cipher = EVP_des_ede3_cbc();
120                 else if (!strcmp (*args, "-des")) 
121                                 cipher = EVP_des_cbc();
122 #endif
123 #ifndef NO_RC2
124                 else if (!strcmp (*args, "-rc2-40")) 
125                                 cipher = EVP_rc2_40_cbc();
126                 else if (!strcmp (*args, "-rc2-128")) 
127                                 cipher = EVP_rc2_cbc();
128                 else if (!strcmp (*args, "-rc2-64")) 
129                                 cipher = EVP_rc2_64_cbc();
130 #endif
131                 else if (!strcmp (*args, "-text")) 
132                                 flags |= PKCS7_TEXT;
133                 else if (!strcmp (*args, "-nointern")) 
134                                 flags |= PKCS7_NOINTERN;
135                 else if (!strcmp (*args, "-noverify")) 
136                                 flags |= PKCS7_NOVERIFY;
137                 else if (!strcmp (*args, "-nochain")) 
138                                 flags |= PKCS7_NOCHAIN;
139                 else if (!strcmp (*args, "-nocerts")) 
140                                 flags |= PKCS7_NOCERTS;
141                 else if (!strcmp (*args, "-noattr")) 
142                                 flags |= PKCS7_NOATTR;
143                 else if (!strcmp (*args, "-nodetach")) 
144                                 flags &= ~PKCS7_DETACHED;
145                 else if (!strcmp (*args, "-binary"))
146                                 flags |= PKCS7_BINARY;
147                 else if (!strcmp (*args, "-nosigs"))
148                                 flags |= PKCS7_NOSIGS;
149                 else if (!strcmp(*args,"-rand")) {
150                         if (args[1]) {
151                                 args++;
152                                 inrand = *args;
153                         } else badarg = 1;
154                         need_rand = 1;
155                 } else if (!strcmp(*args,"-passin")) {
156                         if (args[1]) {
157                                 args++;
158                                 passin = *args;
159                         } else badarg = 1;
160                 } else if (!strcmp(*argv,"-envpassin")) {
161                         if (args[1]) {
162                                 args++;
163                                 if(!(passin= getenv(*args))) {
164                                         BIO_printf(bio_err,
165                                          "Can't read environment variable %s\n",
166                                                                 *args);
167                                         badarg = 1;
168                                 }
169                         } else badarg = 1;
170                 } else if (!strcmp (*args, "-to")) {
171                         if (args[1]) {
172                                 args++;
173                                 to = *args;
174                         } else badarg = 1;
175                 } else if (!strcmp (*args, "-from")) {
176                         if (args[1]) {
177                                 args++;
178                                 from = *args;
179                         } else badarg = 1;
180                 } else if (!strcmp (*args, "-subject")) {
181                         if (args[1]) {
182                                 args++;
183                                 subject = *args;
184                         } else badarg = 1;
185                 } else if (!strcmp (*args, "-signer")) {
186                         if (args[1]) {
187                                 args++;
188                                 signerfile = *args;
189                         } else badarg = 1;
190                 } else if (!strcmp (*args, "-recip")) {
191                         if (args[1]) {
192                                 args++;
193                                 recipfile = *args;
194                         } else badarg = 1;
195                 } else if (!strcmp (*args, "-inkey")) {
196                         if (args[1]) {
197                                 args++;
198                                 keyfile = *args;
199                         } else badarg = 1;
200                 } else if (!strcmp (*args, "-certfile")) {
201                         if (args[1]) {
202                                 args++;
203                                 certfile = *args;
204                         } else badarg = 1;
205                 } else if (!strcmp (*args, "-CAfile")) {
206                         if (args[1]) {
207                                 args++;
208                                 CAfile = *args;
209                         } else badarg = 1;
210                 } else if (!strcmp (*args, "-CApath")) {
211                         if (args[1]) {
212                                 args++;
213                                 CApath = *args;
214                         } else badarg = 1;
215                 } else if (!strcmp (*args, "-in")) {
216                         if (args[1]) {
217                                 args++;
218                                 infile = *args;
219                         } else badarg = 1;
220                 } else if (!strcmp (*args, "-out")) {
221                         if (args[1]) {
222                                 args++;
223                                 outfile = *args;
224                         } else badarg = 1;
225                 } else badarg = 1;
226                 args++;
227         }
228
229         if(operation == SMIME_SIGN) {
230                 if(!signerfile) {
231                         BIO_printf(bio_err, "No signer certificate specified\n");
232                         badarg = 1;
233                 }
234                 need_rand = 1;
235         } else if(operation == SMIME_DECRYPT) {
236                 if(!recipfile) {
237                         BIO_printf(bio_err, "No recipient certificate and key specified\n");
238                         badarg = 1;
239                 }
240         } else if(operation == SMIME_ENCRYPT) {
241                 if(!*args) {
242                         BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
243                         badarg = 1;
244                 }
245                 need_rand = 1;
246         } else if(!operation) badarg = 1;
247
248         if (badarg) {
249                 BIO_printf (bio_err, "Usage smime [options] cert.pem ...\n");
250                 BIO_printf (bio_err, "where options are\n");
251                 BIO_printf (bio_err, "-encrypt       encrypt message\n");
252                 BIO_printf (bio_err, "-decrypt       decrypt encrypted message\n");
253                 BIO_printf (bio_err, "-sign          sign message\n");
254                 BIO_printf (bio_err, "-verify        verify signed message\n");
255                 BIO_printf (bio_err, "-pk7out        output PKCS#7 structure\n");
256 #ifndef NO_DES
257                 BIO_printf (bio_err, "-des3          encrypt with triple DES\n");
258                 BIO_printf (bio_err, "-des           encrypt with DES\n");
259 #endif
260 #ifndef NO_RC2
261                 BIO_printf (bio_err, "-rc2-40        encrypt with RC2-40 (default)\n");
262                 BIO_printf (bio_err, "-rc2-64        encrypt with RC2-64\n");
263                 BIO_printf (bio_err, "-rc2-128       encrypt with RC2-128\n");
264 #endif
265                 BIO_printf (bio_err, "-nointern      don't search certificates in message for signer\n");
266                 BIO_printf (bio_err, "-nosigs        don't verify message signature\n");
267                 BIO_printf (bio_err, "-noverify      don't verify signers certificate\n");
268                 BIO_printf (bio_err, "-nocerts       don't include signers certificate when signing\n");
269                 BIO_printf (bio_err, "-nodetach      use opaque signing\n");
270                 BIO_printf (bio_err, "-noattr        don't include any signed attributes\n");
271                 BIO_printf (bio_err, "-binary        don't translate message to text\n");
272                 BIO_printf (bio_err, "-certfile file other certificates file\n");
273                 BIO_printf (bio_err, "-signer file   signer certificate file\n");
274                 BIO_printf (bio_err, "-recip  file   recipient certificate file for decryption\n");
275                 BIO_printf (bio_err, "-in file       input file\n");
276                 BIO_printf (bio_err, "-inkey file    input private key (if not signer or recipient)\n");
277                 BIO_printf (bio_err, "-out file      output file\n");
278                 BIO_printf (bio_err, "-to addr       to address\n");
279                 BIO_printf (bio_err, "-from ad       from address\n");
280                 BIO_printf (bio_err, "-subject s     subject\n");
281                 BIO_printf (bio_err, "-text          include or delete text MIME headers\n");
282                 BIO_printf (bio_err, "-CApath dir    trusted certificates directory\n");
283                 BIO_printf (bio_err, "-CAfile file   trusted certificates file\n");
284                 BIO_printf(bio_err,  "-rand file:file:...\n");
285                 BIO_printf(bio_err,  "               load the file (or the files in the directory) into\n");
286                 BIO_printf(bio_err,  "               the random number generator\n");
287                 BIO_printf (bio_err, "cert.pem       recipient certificate(s) for encryption\n");
288                 goto end;
289         }
290
291         if (need_rand) {
292                 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
293                 if (inrand != NULL)
294                         BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
295                                 app_RAND_load_files(inrand));
296         }
297
298         ret = 2;
299
300         if(operation != SMIME_SIGN) flags &= ~PKCS7_DETACHED;
301
302         if(flags & PKCS7_BINARY) {
303                 if(operation & SMIME_OP) inmode = "rb";
304                 else outmode = "rb";
305         }
306
307         if(operation == SMIME_ENCRYPT) {
308                 if (!cipher) {
309 #ifndef NO_RC2                  
310                         cipher = EVP_rc2_40_cbc();
311 #else
312                         BIO_printf(bio_err, "No cipher selected\n");
313                         goto end;
314 #endif
315                 }
316 #ifdef CRYPTO_MDEBUG
317                 CRYPTO_push_info("load encryption certificates");
318 #endif          
319                 encerts = sk_X509_new_null();
320                 while (*args) {
321                         if(!(cert = load_cert(*args))) {
322                                 BIO_printf(bio_err, "Can't read recipient certificate file %s\n", *args);
323                                 goto end;
324                         }
325                         sk_X509_push(encerts, cert);
326                         cert = NULL;
327                         args++;
328                 }
329 #ifdef CRYPTO_MDEBUG
330                 CRYPTO_pop_info();
331 #endif          
332         }
333
334         if(signerfile && (operation == SMIME_SIGN)) {
335 #ifdef CRYPTO_MDEBUG
336                 CRYPTO_push_info("load signer certificate");
337 #endif          
338                 if(!(signer = load_cert(signerfile))) {
339                         BIO_printf(bio_err, "Can't read signer certificate file %s\n", signerfile);
340                         goto end;
341                 }
342 #ifdef CRYPTO_MDEBUG
343                 CRYPTO_pop_info();
344 #endif          
345         }
346
347         if(certfile) {
348 #ifdef CRYPTO_MDEBUG
349                 CRYPTO_push_info("load other certfiles");
350 #endif          
351                 if(!(other = load_certs(certfile))) {
352                         BIO_printf(bio_err, "Can't read certificate file %s\n", certfile);
353                         ERR_print_errors(bio_err);
354                         goto end;
355                 }
356 #ifdef CRYPTO_MDEBUG
357                 CRYPTO_pop_info();
358 #endif          
359         }
360
361         if(recipfile && (operation == SMIME_DECRYPT)) {
362 #ifdef CRYPTO_MDEBUG
363                 CRYPTO_push_info("load recipient certificate");
364 #endif          
365                 if(!(recip = load_cert(recipfile))) {
366                         BIO_printf(bio_err, "Can't read recipient certificate file %s\n", recipfile);
367                         ERR_print_errors(bio_err);
368                         goto end;
369                 }
370 #ifdef CRYPTO_MDEBUG
371                 CRYPTO_pop_info();
372 #endif          
373         }
374
375         if(operation == SMIME_DECRYPT) {
376                 if(!keyfile) keyfile = recipfile;
377         } else if(operation == SMIME_SIGN) {
378                 if(!keyfile) keyfile = signerfile;
379         } else keyfile = NULL;
380
381         if(keyfile) {
382 #ifdef CRYPTO_MDEBUG
383                 CRYPTO_push_info("load keyfile");
384 #endif          
385                 if(!(key = load_key(keyfile, passin))) {
386                         BIO_printf(bio_err, "Can't read recipient certificate file %s\n", keyfile);
387                         ERR_print_errors(bio_err);
388                         goto end;
389                 }
390 #ifdef CRYPTO_MDEBUG
391                 CRYPTO_pop_info();
392 #endif          
393         }
394
395 #ifdef CRYPTO_MDEBUG
396         CRYPTO_push_info("open input files");
397 #endif          
398         if (infile) {
399                 if (!(in = BIO_new_file(infile, inmode))) {
400                         BIO_printf (bio_err,
401                                  "Can't open input file %s\n", infile);
402                         goto end;
403                 }
404         } else in = BIO_new_fp(stdin, BIO_NOCLOSE);
405 #ifdef CRYPTO_MDEBUG
406         CRYPTO_pop_info();
407 #endif          
408
409 #ifdef CRYPTO_MDEBUG
410         CRYPTO_push_info("open output files");
411 #endif          
412         if (outfile) {
413                 if (!(out = BIO_new_file(outfile, outmode))) {
414                         BIO_printf (bio_err,
415                                  "Can't open output file %s\n", outfile);
416                         goto end;
417                 }
418         } else out = BIO_new_fp(stdout, BIO_NOCLOSE);
419 #ifdef CRYPTO_MDEBUG
420         CRYPTO_pop_info();
421 #endif          
422
423         if(operation == SMIME_VERIFY) {
424 #ifdef CRYPTO_MDEBUG
425                 CRYPTO_push_info("setup_verify");
426 #endif          
427                 if(!(store = setup_verify(CAfile, CApath))) goto end;
428 #ifdef CRYPTO_MDEBUG
429                 CRYPTO_pop_info();
430 #endif          
431         }
432
433         ret = 3;
434
435         if(operation == SMIME_ENCRYPT) {
436 #ifdef CRYPTO_MDEBUG
437                 CRYPTO_push_info("PKCS7_encrypt");
438 #endif          
439                 p7 = PKCS7_encrypt(encerts, in, cipher, flags);
440 #ifdef CRYPTO_MDEBUG
441                 CRYPTO_pop_info();
442 #endif          
443         } else if(operation == SMIME_SIGN) {
444 #ifdef CRYPTO_MDEBUG
445                 CRYPTO_push_info("PKCS7_sign");
446 #endif          
447                 p7 = PKCS7_sign(signer, key, other, in, flags);
448                 BIO_reset(in);
449 #ifdef CRYPTO_MDEBUG
450                 CRYPTO_pop_info();
451 #endif          
452         } else {
453 #ifdef CRYPTO_MDEBUG
454                 CRYPTO_push_info("SMIME_read_PKCS7");
455 #endif          
456                 if(!(p7 = SMIME_read_PKCS7(in, &indata))) {
457                         BIO_printf(bio_err, "Error reading S/MIME message\n");
458                         goto end;
459                 }
460 #ifdef CRYPTO_MDEBUG
461                 CRYPTO_pop_info();
462 #endif          
463         }
464
465         if(!p7) {
466                 BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
467                 goto end;
468         }
469
470         ret = 4;
471         if(operation == SMIME_DECRYPT) {
472 #ifdef CRYPTO_MDEBUG
473                 CRYPTO_push_info("PKCS7_decrypt");
474 #endif          
475                 if(!PKCS7_decrypt(p7, key, recip, out, flags)) {
476                         BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
477                         goto end;
478                 }
479 #ifdef CRYPTO_MDEBUG
480                 CRYPTO_pop_info();
481 #endif          
482         } else if(operation == SMIME_VERIFY) {
483                 STACK_OF(X509) *signers;
484 #ifdef CRYPTO_MDEBUG
485                 CRYPTO_push_info("PKCS7_verify");
486 #endif          
487                 if(PKCS7_verify(p7, other, store, indata, out, flags)) {
488                         BIO_printf(bio_err, "Verification Successful\n");
489                 } else {
490                         BIO_printf(bio_err, "Verification Failure\n");
491                         goto end;
492                 }
493 #ifdef CRYPTO_MDEBUG
494                 CRYPTO_pop_info();
495                 CRYPTO_push_info("PKCS7_iget_signers");
496 #endif          
497                 signers = PKCS7_iget_signers(p7, other, flags);
498 #ifdef CRYPTO_MDEBUG
499                 CRYPTO_pop_info();
500                 CRYPTO_push_info("save_certs");
501 #endif          
502                 if(!save_certs(signerfile, signers)) {
503                         BIO_printf(bio_err, "Error writing signers to %s\n",
504                                                                 signerfile);
505                         ret = 5;
506                         goto end;
507                 }
508 #ifdef CRYPTO_MDEBUG
509                 CRYPTO_pop_info();
510 #endif          
511                 sk_X509_free(signers);
512         } else if(operation == SMIME_PK7OUT) {
513                 PEM_write_bio_PKCS7(out, p7);
514         } else {
515                 if(to) BIO_printf(out, "To: %s\n", to);
516                 if(from) BIO_printf(out, "From: %s\n", from);
517                 if(subject) BIO_printf(out, "Subject: %s\n", subject);
518                 SMIME_write_PKCS7(out, p7, in, flags);
519         }
520         ret = 0;
521 end:
522 #ifdef CRYPTO_MDEBUG
523         CRYPTO_remove_all_info();
524 #endif
525         if (need_rand)
526                 app_RAND_write_file(NULL, bio_err);
527         if(ret) ERR_print_errors(bio_err);
528         sk_X509_pop_free(encerts, X509_free);
529         sk_X509_pop_free(other, X509_free);
530         X509_STORE_free(store);
531         X509_free(cert);
532         X509_free(recip);
533         X509_free(signer);
534         EVP_PKEY_free(key);
535         PKCS7_free(p7);
536         BIO_free(in);
537         BIO_free(indata);
538         BIO_free(out);
539         return (ret);
540 }
541
542 static X509 *load_cert(char *file)
543 {
544         BIO *in;
545         X509 *cert;
546         if(!(in = BIO_new_file(file, "r"))) return NULL;
547         cert = PEM_read_bio_X509(in, NULL, NULL,NULL);
548         BIO_free(in);
549         return cert;
550 }
551
552 static EVP_PKEY *load_key(char *file, char *pass)
553 {
554         BIO *in;
555         EVP_PKEY *key;
556         if(!(in = BIO_new_file(file, "r"))) return NULL;
557         key = PEM_read_bio_PrivateKey(in, NULL,PEM_cb,pass);
558         BIO_free(in);
559         return key;
560 }
561
562 static STACK_OF(X509) *load_certs(char *file)
563 {
564         BIO *in;
565         int i;
566         STACK_OF(X509) *othercerts;
567         STACK_OF(X509_INFO) *allcerts;
568         X509_INFO *xi;
569         if(!(in = BIO_new_file(file, "r"))) return NULL;
570         othercerts = sk_X509_new(NULL);
571         if(!othercerts) return NULL;
572         allcerts = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
573         for(i = 0; i < sk_X509_INFO_num(allcerts); i++) {
574                 xi = sk_X509_INFO_value (allcerts, i);
575                 if (xi->x509) {
576                         sk_X509_push(othercerts, xi->x509);
577                         xi->x509 = NULL;
578                 }
579         }
580         sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
581         BIO_free(in);
582         return othercerts;
583 }
584
585 static X509_STORE *setup_verify(char *CAfile, char *CApath)
586 {
587         X509_STORE *store;
588         X509_LOOKUP *lookup;
589 #ifdef CRYPTO_MDEBUG
590         CRYPTO_push_info("X509_STORE_new");
591 #endif  
592         if(!(store = X509_STORE_new())) goto end;
593 #ifdef CRYPTO_MDEBUG
594         CRYPTO_pop_info();
595         CRYPTO_push_info("X509_STORE_add_lookup(...file)");
596 #endif  
597         lookup=X509_STORE_add_lookup(store,X509_LOOKUP_file());
598         if (lookup == NULL) goto end;
599 #ifdef CRYPTO_MDEBUG
600         CRYPTO_pop_info();
601         CRYPTO_push_info("X509_LOOKUP_load_file");
602 #endif  
603         if (CAfile) {
604                 if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
605                         BIO_printf(bio_err, "Error loading file %s\n", CAfile);
606                         goto end;
607                 }
608         } else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
609                 
610 #ifdef CRYPTO_MDEBUG
611         CRYPTO_pop_info();
612         CRYPTO_push_info("X509_STORE_add_lookup(...hash_dir)");
613 #endif  
614         lookup=X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
615         if (lookup == NULL) goto end;
616 #ifdef CRYPTO_MDEBUG
617         CRYPTO_pop_info();
618         CRYPTO_push_info("X509_LOOKUP_add_dir");
619 #endif  
620         if (CApath) {
621                 if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
622                         BIO_printf(bio_err, "Error loading directory %s\n", CApath);
623                         goto end;
624                 }
625         } else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
626 #ifdef CRYPTO_MDEBUG
627         CRYPTO_pop_info();
628 #endif  
629
630         ERR_clear_error();
631         return store;
632         end:
633         X509_STORE_free(store);
634         return NULL;
635 }
636
637 int save_certs(char *signerfile, STACK_OF(X509) *signers)
638 {
639         int i;
640         BIO *tmp;
641         if(!signerfile) return 1;
642         tmp = BIO_new_file(signerfile, "w");
643         if(!tmp) return 0;
644         for(i = 0; i < sk_X509_num(signers); i++)
645                 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
646         BIO_free(tmp);
647         return 1;
648 }
649