4cb2a72f455d40e5f2f5f999412df484a5b9453a
[openssl.git] / apps / apps.c
1 /* apps/apps.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <strings.h>
63 #include <sys/types.h>
64 #include <sys/stat.h>
65 #define NON_MAIN
66 #include "apps.h"
67 #undef NON_MAIN
68 #include <openssl/err.h>
69 #include <openssl/x509.h>
70 #include <openssl/pem.h>
71 #include <openssl/pkcs12.h>
72 #include <openssl/safestack.h>
73
74 #ifdef OPENSSL_SYS_WINDOWS
75 #  include "bss_file.c"
76 #endif
77
78 typedef struct {
79         char *name;
80         unsigned long flag;
81         unsigned long mask;
82 } NAME_EX_TBL;
83
84 static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
85
86 int app_init(long mesgwin);
87 #ifdef undef /* never finished - probably never will be :-) */
88 int args_from_file(char *file, int *argc, char **argv[])
89         {
90         FILE *fp;
91         int num,i;
92         unsigned int len;
93         static char *buf=NULL;
94         static char **arg=NULL;
95         char *p;
96         struct stat stbuf;
97
98         if (stat(file,&stbuf) < 0) return(0);
99
100         fp=fopen(file,"r");
101         if (fp == NULL)
102                 return(0);
103
104         *argc=0;
105         *argv=NULL;
106
107         len=(unsigned int)stbuf.st_size;
108         if (buf != NULL) OPENSSL_free(buf);
109         buf=(char *)OPENSSL_malloc(len+1);
110         if (buf == NULL) return(0);
111
112         len=fread(buf,1,len,fp);
113         if (len <= 1) return(0);
114         buf[len]='\0';
115
116         i=0;
117         for (p=buf; *p; p++)
118                 if (*p == '\n') i++;
119         if (arg != NULL) OPENSSL_free(arg);
120         arg=(char **)OPENSSL_malloc(sizeof(char *)*(i*2));
121
122         *argv=arg;
123         num=0;
124         p=buf;
125         for (;;)
126                 {
127                 if (!*p) break;
128                 if (*p == '#') /* comment line */
129                         {
130                         while (*p && (*p != '\n')) p++;
131                         continue;
132                         }
133                 /* else we have a line */
134                 *(arg++)=p;
135                 num++;
136                 while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n')))
137                         p++;
138                 if (!*p) break;
139                 if (*p == '\n')
140                         {
141                         *(p++)='\0';
142                         continue;
143                         }
144                 /* else it is a tab or space */
145                 p++;
146                 while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
147                         p++;
148                 if (!*p) break;
149                 if (*p == '\n')
150                         {
151                         p++;
152                         continue;
153                         }
154                 *(arg++)=p++;
155                 num++;
156                 while (*p && (*p != '\n')) p++;
157                 if (!*p) break;
158                 /* else *p == '\n' */
159                 *(p++)='\0';
160                 }
161         *argc=num;
162         return(1);
163         }
164 #endif
165
166 int str2fmt(char *s)
167         {
168         if      ((*s == 'D') || (*s == 'd'))
169                 return(FORMAT_ASN1);
170         else if ((*s == 'T') || (*s == 't'))
171                 return(FORMAT_TEXT);
172         else if ((*s == 'P') || (*s == 'p'))
173                 return(FORMAT_PEM);
174         else if ((*s == 'N') || (*s == 'n'))
175                 return(FORMAT_NETSCAPE);
176         else if ((*s == 'S') || (*s == 's'))
177                 return(FORMAT_SMIME);
178         else if ((*s == '1')
179                 || (strcmp(s,"PKCS12") == 0) || (strcmp(s,"pkcs12") == 0)
180                 || (strcmp(s,"P12") == 0) || (strcmp(s,"p12") == 0))
181                 return(FORMAT_PKCS12);
182         else if ((*s == 'E') || (*s == 'e'))
183                 return(FORMAT_ENGINE);
184         else
185                 return(FORMAT_UNDEF);
186         }
187
188 #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
189 void program_name(char *in, char *out, int size)
190         {
191         int i,n;
192         char *p=NULL;
193
194         n=strlen(in);
195         /* find the last '/', '\' or ':' */
196         for (i=n-1; i>0; i--)
197                 {
198                 if ((in[i] == '/') || (in[i] == '\\') || (in[i] == ':'))
199                         {
200                         p= &(in[i+1]);
201                         break;
202                         }
203                 }
204         if (p == NULL)
205                 p=in;
206         n=strlen(p);
207         /* strip off trailing .exe if present. */
208         if ((n > 4) && (p[n-4] == '.') &&
209                 ((p[n-3] == 'e') || (p[n-3] == 'E')) &&
210                 ((p[n-2] == 'x') || (p[n-2] == 'X')) &&
211                 ((p[n-1] == 'e') || (p[n-1] == 'E')))
212                 n-=4;
213         if (n > size-1)
214                 n=size-1;
215
216         for (i=0; i<n; i++)
217                 {
218                 if ((p[i] >= 'A') && (p[i] <= 'Z'))
219                         out[i]=p[i]-'A'+'a';
220                 else
221                         out[i]=p[i];
222                 }
223         out[n]='\0';
224         }
225 #else
226 #ifdef OPENSSL_SYS_VMS
227 void program_name(char *in, char *out, int size)
228         {
229         char *p=in, *q;
230         char *chars=":]>";
231
232         while(*chars != '\0')
233                 {
234                 q=strrchr(p,*chars);
235                 if (q > p)
236                         p = q + 1;
237                 chars++;
238                 }
239
240         q=strrchr(p,'.');
241         if (q == NULL)
242                 q = in+size;
243         strncpy(out,p,q-p);
244         out[q-p]='\0';
245         }
246 #else
247 void program_name(char *in, char *out, int size)
248         {
249         char *p;
250
251         p=strrchr(in,'/');
252         if (p != NULL)
253                 p++;
254         else
255                 p=in;
256         strncpy(out,p,size-1);
257         out[size-1]='\0';
258         }
259 #endif
260 #endif
261
262 #ifdef OPENSSL_SYS_WIN32
263 int WIN32_rename(char *from, char *to)
264         {
265 #ifdef OPENSSL_SYS_WINNT
266         int ret;
267 /* Note: MoveFileEx() doesn't work under Win95, Win98 */
268
269         ret=MoveFileEx(from,to,MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED);
270         return(ret?0:-1);
271 #else
272         unlink(to);
273         return MoveFile(from, to);
274 #endif
275         }
276 #endif
277
278 int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[])
279         {
280         int num,len,i;
281         char *p;
282
283         *argc=0;
284         *argv=NULL;
285
286         len=strlen(buf);
287         i=0;
288         if (arg->count == 0)
289                 {
290                 arg->count=20;
291                 arg->data=(char **)OPENSSL_malloc(sizeof(char *)*arg->count);
292                 }
293         for (i=0; i<arg->count; i++)
294                 arg->data[i]=NULL;
295
296         num=0;
297         p=buf;
298         for (;;)
299                 {
300                 /* first scan over white space */
301                 if (!*p) break;
302                 while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
303                         p++;
304                 if (!*p) break;
305
306                 /* The start of something good :-) */
307                 if (num >= arg->count)
308                         {
309                         arg->count+=20;
310                         arg->data=(char **)OPENSSL_realloc(arg->data,
311                                 sizeof(char *)*arg->count);
312                         if (argc == 0) return(0);
313                         }
314                 arg->data[num++]=p;
315
316                 /* now look for the end of this */
317                 if ((*p == '\'') || (*p == '\"')) /* scan for closing quote */
318                         {
319                         i= *(p++);
320                         arg->data[num-1]++; /* jump over quote */
321                         while (*p && (*p != i))
322                                 p++;
323                         *p='\0';
324                         }
325                 else
326                         {
327                         while (*p && ((*p != ' ') &&
328                                 (*p != '\t') && (*p != '\n')))
329                                 p++;
330
331                         if (*p == '\0')
332                                 p--;
333                         else
334                                 *p='\0';
335                         }
336                 p++;
337                 }
338         *argc=num;
339         *argv=arg->data;
340         return(1);
341         }
342
343 #ifndef APP_INIT
344 int app_init(long mesgwin)
345         {
346         return(1);
347         }
348 #endif
349
350
351 int dump_cert_text (BIO *out, X509 *x)
352 {
353         char buf[256];
354         X509_NAME_oneline(X509_get_subject_name(x),buf,256);
355         BIO_puts(out,"subject=");
356         BIO_puts(out,buf);
357
358         X509_NAME_oneline(X509_get_issuer_name(x),buf,256);
359         BIO_puts(out,"\nissuer= ");
360         BIO_puts(out,buf);
361         BIO_puts(out,"\n");
362         return 0;
363 }
364
365 static char *app_get_pass(BIO *err, char *arg, int keepbio);
366
367 int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2)
368 {
369         int same;
370         if(!arg2 || !arg1 || strcmp(arg1, arg2)) same = 0;
371         else same = 1;
372         if(arg1) {
373                 *pass1 = app_get_pass(err, arg1, same);
374                 if(!*pass1) return 0;
375         } else if(pass1) *pass1 = NULL;
376         if(arg2) {
377                 *pass2 = app_get_pass(err, arg2, same ? 2 : 0);
378                 if(!*pass2) return 0;
379         } else if(pass2) *pass2 = NULL;
380         return 1;
381 }
382
383 static char *app_get_pass(BIO *err, char *arg, int keepbio)
384 {
385         char *tmp, tpass[APP_PASS_LEN];
386         static BIO *pwdbio = NULL;
387         int i;
388         if(!strncmp(arg, "pass:", 5)) return BUF_strdup(arg + 5);
389         if(!strncmp(arg, "env:", 4)) {
390                 tmp = getenv(arg + 4);
391                 if(!tmp) {
392                         BIO_printf(err, "Can't read environment variable %s\n", arg + 4);
393                         return NULL;
394                 }
395                 return BUF_strdup(tmp);
396         }
397         if(!keepbio || !pwdbio) {
398                 if(!strncmp(arg, "file:", 5)) {
399                         pwdbio = BIO_new_file(arg + 5, "r");
400                         if(!pwdbio) {
401                                 BIO_printf(err, "Can't open file %s\n", arg + 5);
402                                 return NULL;
403                         }
404                 } else if(!strncmp(arg, "fd:", 3)) {
405                         BIO *btmp;
406                         i = atoi(arg + 3);
407                         if(i >= 0) pwdbio = BIO_new_fd(i, BIO_NOCLOSE);
408                         if((i < 0) || !pwdbio) {
409                                 BIO_printf(err, "Can't access file descriptor %s\n", arg + 3);
410                                 return NULL;
411                         }
412                         /* Can't do BIO_gets on an fd BIO so add a buffering BIO */
413                         btmp = BIO_new(BIO_f_buffer());
414                         pwdbio = BIO_push(btmp, pwdbio);
415                 } else if(!strcmp(arg, "stdin")) {
416                         pwdbio = BIO_new_fp(stdin, BIO_NOCLOSE);
417                         if(!pwdbio) {
418                                 BIO_printf(err, "Can't open BIO for stdin\n");
419                                 return NULL;
420                         }
421                 } else {
422                         BIO_printf(err, "Invalid password argument \"%s\"\n", arg);
423                         return NULL;
424                 }
425         }
426         i = BIO_gets(pwdbio, tpass, APP_PASS_LEN);
427         if(keepbio != 1) {
428                 BIO_free_all(pwdbio);
429                 pwdbio = NULL;
430         }
431         if(i <= 0) {
432                 BIO_printf(err, "Error reading password from BIO\n");
433                 return NULL;
434         }
435         tmp = strchr(tpass, '\n');
436         if(tmp) *tmp = 0;
437         return BUF_strdup(tpass);
438 }
439
440 int add_oid_section(BIO *err, LHASH *conf)
441 {       
442         char *p;
443         STACK_OF(CONF_VALUE) *sktmp;
444         CONF_VALUE *cnf;
445         int i;
446         if(!(p=CONF_get_string(conf,NULL,"oid_section")))
447                 {
448                 ERR_clear_error();
449                 return 1;
450                 }
451         if(!(sktmp = CONF_get_section(conf, p))) {
452                 BIO_printf(err, "problem loading oid section %s\n", p);
453                 return 0;
454         }
455         for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
456                 cnf = sk_CONF_VALUE_value(sktmp, i);
457                 if(OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
458                         BIO_printf(err, "problem creating object %s=%s\n",
459                                                          cnf->name, cnf->value);
460                         return 0;
461                 }
462         }
463         return 1;
464 }
465
466 X509 *load_cert(BIO *err, char *file, int format)
467         {
468         ASN1_HEADER *ah=NULL;
469         BUF_MEM *buf=NULL;
470         X509 *x=NULL;
471         BIO *cert;
472
473         if ((cert=BIO_new(BIO_s_file())) == NULL)
474                 {
475                 ERR_print_errors(err);
476                 goto end;
477                 }
478
479         if (file == NULL)
480                 {
481                 setvbuf(stdin, NULL, _IONBF, 0);
482                 BIO_set_fp(cert,stdin,BIO_NOCLOSE);
483                 }
484         else
485                 {
486                 if (BIO_read_filename(cert,file) <= 0)
487                         {
488                         perror(file);
489                         goto end;
490                         }
491                 }
492
493         if      (format == FORMAT_ASN1)
494                 x=d2i_X509_bio(cert,NULL);
495         else if (format == FORMAT_NETSCAPE)
496                 {
497                 unsigned char *p,*op;
498                 int size=0,i;
499
500                 /* We sort of have to do it this way because it is sort of nice
501                  * to read the header first and check it, then
502                  * try to read the certificate */
503                 buf=BUF_MEM_new();
504                 for (;;)
505                         {
506                         if ((buf == NULL) || (!BUF_MEM_grow(buf,size+1024*10)))
507                                 goto end;
508                         i=BIO_read(cert,&(buf->data[size]),1024*10);
509                         size+=i;
510                         if (i == 0) break;
511                         if (i < 0)
512                                 {
513                                 perror("reading certificate");
514                                 goto end;
515                                 }
516                         }
517                 p=(unsigned char *)buf->data;
518                 op=p;
519
520                 /* First load the header */
521                 if ((ah=d2i_ASN1_HEADER(NULL,&p,(long)size)) == NULL)
522                         goto end;
523                 if ((ah->header == NULL) || (ah->header->data == NULL) ||
524                         (strncmp(NETSCAPE_CERT_HDR,(char *)ah->header->data,
525                         ah->header->length) != 0))
526                         {
527                         BIO_printf(err,"Error reading header on certificate\n");
528                         goto end;
529                         }
530                 /* header is ok, so now read the object */
531                 p=op;
532                 ah->meth=X509_asn1_meth();
533                 if ((ah=d2i_ASN1_HEADER(&ah,&p,(long)size)) == NULL)
534                         goto end;
535                 x=(X509 *)ah->data;
536                 ah->data=NULL;
537                 }
538         else if (format == FORMAT_PEM)
539                 x=PEM_read_bio_X509_AUX(cert,NULL,NULL,NULL);
540         else if (format == FORMAT_PKCS12)
541                 {
542                 PKCS12 *p12 = d2i_PKCS12_bio(cert, NULL);
543
544                 PKCS12_parse(p12, NULL, NULL, &x, NULL);
545                 PKCS12_free(p12);
546                 p12 = NULL;
547                 }
548         else    {
549                 BIO_printf(err,"bad input format specified for input cert\n");
550                 goto end;
551                 }
552 end:
553         if (x == NULL)
554                 {
555                 BIO_printf(err,"unable to load certificate\n");
556                 ERR_print_errors(err);
557                 }
558         if (ah != NULL) ASN1_HEADER_free(ah);
559         if (cert != NULL) BIO_free(cert);
560         if (buf != NULL) BUF_MEM_free(buf);
561         return(x);
562         }
563
564 EVP_PKEY *load_key(BIO *err, char *file, int format, char *pass, ENGINE *e)
565         {
566         BIO *key=NULL;
567         EVP_PKEY *pkey=NULL;
568
569         if (file == NULL)
570                 {
571                 BIO_printf(err,"no keyfile specified\n");
572                 goto end;
573                 }
574         if (format == FORMAT_ENGINE)
575                 {
576                 if (!e)
577                         BIO_printf(bio_err,"no engine specified\n");
578                 else
579                         pkey = ENGINE_load_private_key(e, file, pass);
580                 goto end;
581                 }
582         key=BIO_new(BIO_s_file());
583         if (key == NULL)
584                 {
585                 ERR_print_errors(err);
586                 goto end;
587                 }
588         if (BIO_read_filename(key,file) <= 0)
589                 {
590                 perror(file);
591                 goto end;
592                 }
593         if (format == FORMAT_ASN1)
594                 {
595                 pkey=d2i_PrivateKey_bio(key, NULL);
596                 }
597         else if (format == FORMAT_PEM)
598                 {
599                 pkey=PEM_read_bio_PrivateKey(key,NULL,NULL,pass);
600                 }
601         else if (format == FORMAT_PKCS12)
602                 {
603                 PKCS12 *p12 = d2i_PKCS12_bio(key, NULL);
604
605                 PKCS12_parse(p12, pass, &pkey, NULL, NULL);
606                 PKCS12_free(p12);
607                 p12 = NULL;
608                 }
609         else
610                 {
611                 BIO_printf(err,"bad input format specified for key\n");
612                 goto end;
613                 }
614  end:
615         if (key != NULL) BIO_free(key);
616         if (pkey == NULL)
617                 BIO_printf(err,"unable to load Private Key\n");
618         return(pkey);
619         }
620
621 EVP_PKEY *load_pubkey(BIO *err, char *file, int format, ENGINE *e)
622         {
623         BIO *key=NULL;
624         EVP_PKEY *pkey=NULL;
625
626         if (file == NULL)
627                 {
628                 BIO_printf(err,"no keyfile specified\n");
629                 goto end;
630                 }
631         if (format == FORMAT_ENGINE)
632                 {
633                 if (!e)
634                         BIO_printf(bio_err,"no engine specified\n");
635                 else
636                         pkey = ENGINE_load_public_key(e, file, NULL);
637                 goto end;
638                 }
639         key=BIO_new(BIO_s_file());
640         if (key == NULL)
641                 {
642                 ERR_print_errors(err);
643                 goto end;
644                 }
645         if (BIO_read_filename(key,file) <= 0)
646                 {
647                 perror(file);
648                 goto end;
649                 }
650         if (format == FORMAT_ASN1)
651                 {
652                 pkey=d2i_PUBKEY_bio(key, NULL);
653                 }
654         else if (format == FORMAT_PEM)
655                 {
656                 pkey=PEM_read_bio_PUBKEY(key,NULL,NULL,NULL);
657                 }
658         else
659                 {
660                 BIO_printf(err,"bad input format specified for key\n");
661                 goto end;
662                 }
663  end:
664         if (key != NULL) BIO_free(key);
665         if (pkey == NULL)
666                 BIO_printf(err,"unable to load Public Key\n");
667         return(pkey);
668         }
669
670 STACK_OF(X509) *load_certs(BIO *err, char *file, int format)
671         {
672         BIO *certs;
673         int i;
674         STACK_OF(X509) *othercerts = NULL;
675         STACK_OF(X509_INFO) *allcerts = NULL;
676         X509_INFO *xi;
677
678         if((certs = BIO_new(BIO_s_file())) == NULL)
679                 {
680                 ERR_print_errors(err);
681                 goto end;
682                 }
683
684         if (file == NULL)
685                 BIO_set_fp(certs,stdin,BIO_NOCLOSE);
686         else
687                 {
688                 if (BIO_read_filename(certs,file) <= 0)
689                         {
690                         perror(file);
691                         goto end;
692                         }
693                 }
694
695         if      (format == FORMAT_PEM)
696                 {
697                 othercerts = sk_X509_new_null();
698                 if(!othercerts)
699                         {
700                         sk_X509_free(othercerts);
701                         othercerts = NULL;
702                         goto end;
703                         }
704                 allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
705                 for(i = 0; i < sk_X509_INFO_num(allcerts); i++)
706                         {
707                         xi = sk_X509_INFO_value (allcerts, i);
708                         if (xi->x509)
709                                 {
710                                 sk_X509_push(othercerts, xi->x509);
711                                 xi->x509 = NULL;
712                                 }
713                         }
714                 goto end;
715                 }
716         else    {
717                 BIO_printf(err,"bad input format specified for input cert\n");
718                 goto end;
719                 }
720 end:
721         if (othercerts == NULL)
722                 {
723                 BIO_printf(err,"unable to load certificates\n");
724                 ERR_print_errors(err);
725                 }
726         if (allcerts) sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
727         if (certs != NULL) BIO_free(certs);
728         return(othercerts);
729         }
730
731
732 #define X509V3_EXT_UNKNOWN_MASK         (0xfL << 16)
733 /* Return error for unknown extensions */
734 #define X509V3_EXT_DEFAULT              0
735 /* Print error for unknown extensions */
736 #define X509V3_EXT_ERROR_UNKNOWN        (1L << 16)
737 /* ASN1 parse unknown extensions */
738 #define X509V3_EXT_PARSE_UNKNOWN        (2L << 16)
739 /* BIO_dump unknown extensions */
740 #define X509V3_EXT_DUMP_UNKNOWN         (3L << 16)
741
742 int set_cert_ex(unsigned long *flags, const char *arg)
743 {
744         static const NAME_EX_TBL cert_tbl[] = {
745                 { "compatible", X509_FLAG_COMPAT, 0xffffffffl},
746                 { "no_header", X509_FLAG_NO_HEADER, 0},
747                 { "no_version", X509_FLAG_NO_VERSION, 0},
748                 { "no_serial", X509_FLAG_NO_SERIAL, 0},
749                 { "no_signame", X509_FLAG_NO_SIGNAME, 0},
750                 { "no_validity", X509_FLAG_NO_VALIDITY, 0},
751                 { "no_subject", X509_FLAG_NO_SUBJECT, 0},
752                 { "no_pubkey", X509_FLAG_NO_PUBKEY, 0},
753                 { "no_extensions", X509_FLAG_NO_EXTENSIONS, 0},
754                 { "no_sigdump", X509_FLAG_NO_SIGDUMP, 0},
755                 { "no_aux", X509_FLAG_NO_AUX, 0},
756                 { "ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK},
757                 { "ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
758                 { "ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
759                 { "ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
760                 { NULL, 0, 0}
761         };
762         return set_table_opts(flags, arg, cert_tbl);
763 }
764
765 int set_name_ex(unsigned long *flags, const char *arg)
766 {
767         static const NAME_EX_TBL ex_tbl[] = {
768                 { "esc_2253", ASN1_STRFLGS_ESC_2253, 0},
769                 { "esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0},
770                 { "esc_msb", ASN1_STRFLGS_ESC_MSB, 0},
771                 { "use_quote", ASN1_STRFLGS_ESC_QUOTE, 0},
772                 { "utf8", ASN1_STRFLGS_UTF8_CONVERT, 0},
773                 { "ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0},
774                 { "show_type", ASN1_STRFLGS_SHOW_TYPE, 0},
775                 { "dump_all", ASN1_STRFLGS_DUMP_ALL, 0},
776                 { "dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0},
777                 { "dump_der", ASN1_STRFLGS_DUMP_DER, 0},
778                 { "compat", XN_FLAG_COMPAT, 0xffffffffL},
779                 { "sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK},
780                 { "sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK},
781                 { "sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK},
782                 { "sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK},
783                 { "dn_rev", XN_FLAG_DN_REV, 0},
784                 { "nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK},
785                 { "sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK},
786                 { "lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK},
787                 { "oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK},
788                 { "space_eq", XN_FLAG_SPC_EQ, 0},
789                 { "dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0},
790                 { "RFC2253", XN_FLAG_RFC2253, 0xffffffffL},
791                 { "oneline", XN_FLAG_ONELINE, 0xffffffffL},
792                 { "multiline", XN_FLAG_MULTILINE, 0xffffffffL},
793                 { NULL, 0, 0}
794         };
795         return set_table_opts(flags, arg, ex_tbl);
796 }
797
798 static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
799 {
800         char c;
801         const NAME_EX_TBL *ptbl;
802         c = arg[0];
803
804         if(c == '-') {
805                 c = 0;
806                 arg++;
807         } else if (c == '+') {
808                 c = 1;
809                 arg++;
810         } else c = 1;
811
812         for(ptbl = in_tbl; ptbl->name; ptbl++) {
813                 if(!strcasecmp(arg, ptbl->name)) {
814                         *flags &= ~ptbl->mask;
815                         if(c) *flags |= ptbl->flag;
816                         else *flags &= ~ptbl->flag;
817                         return 1;
818                 }
819         }
820         return 0;
821 }
822
823 void print_name(BIO *out, char *title, X509_NAME *nm, unsigned long lflags)
824 {
825         char buf[256];
826         char mline = 0;
827         int indent = 0;
828         if(title) BIO_puts(out, title);
829         if((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
830                 mline = 1;
831                 indent = 4;
832         }
833         if(lflags == XN_FLAG_COMPAT) {
834                 X509_NAME_oneline(nm,buf,256);
835                 BIO_puts(out,buf);
836                 BIO_puts(out, "\n");
837         } else {
838                 if(mline) BIO_puts(out, "\n");
839                 X509_NAME_print_ex(out, nm, indent, lflags);
840                 BIO_puts(out, "\n");
841         }
842 }
843
844 X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath)
845 {
846         X509_STORE *store;
847         X509_LOOKUP *lookup;
848         if(!(store = X509_STORE_new())) goto end;
849         lookup=X509_STORE_add_lookup(store,X509_LOOKUP_file());
850         if (lookup == NULL) goto end;
851         if (CAfile) {
852                 if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
853                         BIO_printf(bp, "Error loading file %s\n", CAfile);
854                         goto end;
855                 }
856         } else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
857                 
858         lookup=X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
859         if (lookup == NULL) goto end;
860         if (CApath) {
861                 if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
862                         BIO_printf(bp, "Error loading directory %s\n", CApath);
863                         goto end;
864                 }
865         } else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
866
867         ERR_clear_error();
868         return store;
869         end:
870         X509_STORE_free(store);
871         return NULL;
872 }