More code for X509_print_ex() support.
[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 <sys/types.h>
63 #include <sys/stat.h>
64 #define NON_MAIN
65 #include "apps.h"
66 #undef NON_MAIN
67 #include <openssl/err.h>
68 #include <openssl/x509.h>
69 #include <openssl/pem.h>
70 #include <openssl/pkcs12.h>
71 #include <openssl/safestack.h>
72
73 #ifdef WINDOWS
74 #  include "bss_file.c"
75 #endif
76
77 typedef struct {
78         char *name;
79         unsigned long flag;
80         unsigned long mask;
81 } NAME_EX_TBL;
82
83 static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
84
85 int app_init(long mesgwin);
86 #ifdef undef /* never finished - probably never will be :-) */
87 int args_from_file(char *file, int *argc, char **argv[])
88         {
89         FILE *fp;
90         int num,i;
91         unsigned int len;
92         static char *buf=NULL;
93         static char **arg=NULL;
94         char *p;
95         struct stat stbuf;
96
97         if (stat(file,&stbuf) < 0) return(0);
98
99         fp=fopen(file,"r");
100         if (fp == NULL)
101                 return(0);
102
103         *argc=0;
104         *argv=NULL;
105
106         len=(unsigned int)stbuf.st_size;
107         if (buf != NULL) OPENSSL_free(buf);
108         buf=(char *)OPENSSL_malloc(len+1);
109         if (buf == NULL) return(0);
110
111         len=fread(buf,1,len,fp);
112         if (len <= 1) return(0);
113         buf[len]='\0';
114
115         i=0;
116         for (p=buf; *p; p++)
117                 if (*p == '\n') i++;
118         if (arg != NULL) OPENSSL_free(arg);
119         arg=(char **)OPENSSL_malloc(sizeof(char *)*(i*2));
120
121         *argv=arg;
122         num=0;
123         p=buf;
124         for (;;)
125                 {
126                 if (!*p) break;
127                 if (*p == '#') /* comment line */
128                         {
129                         while (*p && (*p != '\n')) p++;
130                         continue;
131                         }
132                 /* else we have a line */
133                 *(arg++)=p;
134                 num++;
135                 while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n')))
136                         p++;
137                 if (!*p) break;
138                 if (*p == '\n')
139                         {
140                         *(p++)='\0';
141                         continue;
142                         }
143                 /* else it is a tab or space */
144                 p++;
145                 while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
146                         p++;
147                 if (!*p) break;
148                 if (*p == '\n')
149                         {
150                         p++;
151                         continue;
152                         }
153                 *(arg++)=p++;
154                 num++;
155                 while (*p && (*p != '\n')) p++;
156                 if (!*p) break;
157                 /* else *p == '\n' */
158                 *(p++)='\0';
159                 }
160         *argc=num;
161         return(1);
162         }
163 #endif
164
165 int str2fmt(char *s)
166         {
167         if      ((*s == 'D') || (*s == 'd'))
168                 return(FORMAT_ASN1);
169         else if ((*s == 'T') || (*s == 't'))
170                 return(FORMAT_TEXT);
171         else if ((*s == 'P') || (*s == 'p'))
172                 return(FORMAT_PEM);
173         else if ((*s == 'N') || (*s == 'n'))
174                 return(FORMAT_NETSCAPE);
175         else if ((*s == 'S') || (*s == 's'))
176                 return(FORMAT_SMIME);
177         else if ((*s == '1')
178                 || (strcmp(s,"PKCS12") == 0) || (strcmp(s,"pkcs12") == 0)
179                 || (strcmp(s,"P12") == 0) || (strcmp(s,"p12") == 0))
180                 return(FORMAT_PKCS12);
181         else
182                 return(FORMAT_UNDEF);
183         }
184
185 #if defined(MSDOS) || defined(WIN32) || defined(WIN16)
186 void program_name(char *in, char *out, int size)
187         {
188         int i,n;
189         char *p=NULL;
190
191         n=strlen(in);
192         /* find the last '/', '\' or ':' */
193         for (i=n-1; i>0; i--)
194                 {
195                 if ((in[i] == '/') || (in[i] == '\\') || (in[i] == ':'))
196                         {
197                         p= &(in[i+1]);
198                         break;
199                         }
200                 }
201         if (p == NULL)
202                 p=in;
203         n=strlen(p);
204         /* strip off trailing .exe if present. */
205         if ((n > 4) && (p[n-4] == '.') &&
206                 ((p[n-3] == 'e') || (p[n-3] == 'E')) &&
207                 ((p[n-2] == 'x') || (p[n-2] == 'X')) &&
208                 ((p[n-1] == 'e') || (p[n-1] == 'E')))
209                 n-=4;
210         if (n > size-1)
211                 n=size-1;
212
213         for (i=0; i<n; i++)
214                 {
215                 if ((p[i] >= 'A') && (p[i] <= 'Z'))
216                         out[i]=p[i]-'A'+'a';
217                 else
218                         out[i]=p[i];
219                 }
220         out[n]='\0';
221         }
222 #else
223 #ifdef VMS
224 void program_name(char *in, char *out, int size)
225         {
226         char *p=in, *q;
227         char *chars=":]>";
228
229         while(*chars != '\0')
230                 {
231                 q=strrchr(p,*chars);
232                 if (q > p)
233                         p = q + 1;
234                 chars++;
235                 }
236
237         q=strrchr(p,'.');
238         if (q == NULL)
239                 q = in+size;
240         strncpy(out,p,q-p);
241         out[q-p]='\0';
242         }
243 #else
244 void program_name(char *in, char *out, int size)
245         {
246         char *p;
247
248         p=strrchr(in,'/');
249         if (p != NULL)
250                 p++;
251         else
252                 p=in;
253         strncpy(out,p,size-1);
254         out[size-1]='\0';
255         }
256 #endif
257 #endif
258
259 #ifdef WIN32
260 int WIN32_rename(char *from, char *to)
261         {
262 #ifdef WINNT
263         int ret;
264 /* Note: MoveFileEx() doesn't work under Win95, Win98 */
265
266         ret=MoveFileEx(from,to,MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED);
267         return(ret?0:-1);
268 #else
269         unlink(to);
270         return MoveFile(from, to);
271 #endif
272         }
273 #endif
274
275 int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[])
276         {
277         int num,len,i;
278         char *p;
279
280         *argc=0;
281         *argv=NULL;
282
283         len=strlen(buf);
284         i=0;
285         if (arg->count == 0)
286                 {
287                 arg->count=20;
288                 arg->data=(char **)OPENSSL_malloc(sizeof(char *)*arg->count);
289                 }
290         for (i=0; i<arg->count; i++)
291                 arg->data[i]=NULL;
292
293         num=0;
294         p=buf;
295         for (;;)
296                 {
297                 /* first scan over white space */
298                 if (!*p) break;
299                 while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
300                         p++;
301                 if (!*p) break;
302
303                 /* The start of something good :-) */
304                 if (num >= arg->count)
305                         {
306                         arg->count+=20;
307                         arg->data=(char **)OPENSSL_realloc(arg->data,
308                                 sizeof(char *)*arg->count);
309                         if (argc == 0) return(0);
310                         }
311                 arg->data[num++]=p;
312
313                 /* now look for the end of this */
314                 if ((*p == '\'') || (*p == '\"')) /* scan for closing quote */
315                         {
316                         i= *(p++);
317                         arg->data[num-1]++; /* jump over quote */
318                         while (*p && (*p != i))
319                                 p++;
320                         *p='\0';
321                         }
322                 else
323                         {
324                         while (*p && ((*p != ' ') &&
325                                 (*p != '\t') && (*p != '\n')))
326                                 p++;
327
328                         if (*p == '\0')
329                                 p--;
330                         else
331                                 *p='\0';
332                         }
333                 p++;
334                 }
335         *argc=num;
336         *argv=arg->data;
337         return(1);
338         }
339
340 #ifndef APP_INIT
341 int app_init(long mesgwin)
342         {
343         return(1);
344         }
345 #endif
346
347
348 int dump_cert_text (BIO *out, X509 *x)
349 {
350         char buf[256];
351         X509_NAME_oneline(X509_get_subject_name(x),buf,256);
352         BIO_puts(out,"subject=");
353         BIO_puts(out,buf);
354
355         X509_NAME_oneline(X509_get_issuer_name(x),buf,256);
356         BIO_puts(out,"\nissuer= ");
357         BIO_puts(out,buf);
358         BIO_puts(out,"\n");
359         return 0;
360 }
361
362 static char *app_get_pass(BIO *err, char *arg, int keepbio);
363
364 int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2)
365 {
366         int same;
367         if(!arg2 || !arg1 || strcmp(arg1, arg2)) same = 0;
368         else same = 1;
369         if(arg1) {
370                 *pass1 = app_get_pass(err, arg1, same);
371                 if(!*pass1) return 0;
372         } else if(pass1) *pass1 = NULL;
373         if(arg2) {
374                 *pass2 = app_get_pass(err, arg2, same ? 2 : 0);
375                 if(!*pass2) return 0;
376         } else if(pass2) *pass2 = NULL;
377         return 1;
378 }
379
380 static char *app_get_pass(BIO *err, char *arg, int keepbio)
381 {
382         char *tmp, tpass[APP_PASS_LEN];
383         static BIO *pwdbio = NULL;
384         int i;
385         if(!strncmp(arg, "pass:", 5)) return BUF_strdup(arg + 5);
386         if(!strncmp(arg, "env:", 4)) {
387                 tmp = getenv(arg + 4);
388                 if(!tmp) {
389                         BIO_printf(err, "Can't read environment variable %s\n", arg + 4);
390                         return NULL;
391                 }
392                 return BUF_strdup(tmp);
393         }
394         if(!keepbio || !pwdbio) {
395                 if(!strncmp(arg, "file:", 5)) {
396                         pwdbio = BIO_new_file(arg + 5, "r");
397                         if(!pwdbio) {
398                                 BIO_printf(err, "Can't open file %s\n", arg + 5);
399                                 return NULL;
400                         }
401                 } else if(!strncmp(arg, "fd:", 3)) {
402                         BIO *btmp;
403                         i = atoi(arg + 3);
404                         if(i >= 0) pwdbio = BIO_new_fd(i, BIO_NOCLOSE);
405                         if((i < 0) || !pwdbio) {
406                                 BIO_printf(err, "Can't access file descriptor %s\n", arg + 3);
407                                 return NULL;
408                         }
409                         /* Can't do BIO_gets on an fd BIO so add a buffering BIO */
410                         btmp = BIO_new(BIO_f_buffer());
411                         pwdbio = BIO_push(btmp, pwdbio);
412                 } else if(!strcmp(arg, "stdin")) {
413                         pwdbio = BIO_new_fp(stdin, BIO_NOCLOSE);
414                         if(!pwdbio) {
415                                 BIO_printf(err, "Can't open BIO for stdin\n");
416                                 return NULL;
417                         }
418                 } else {
419                         BIO_printf(err, "Invalid password argument \"%s\"\n", arg);
420                         return NULL;
421                 }
422         }
423         i = BIO_gets(pwdbio, tpass, APP_PASS_LEN);
424         if(keepbio != 1) {
425                 BIO_free_all(pwdbio);
426                 pwdbio = NULL;
427         }
428         if(i <= 0) {
429                 BIO_printf(err, "Error reading password from BIO\n");
430                 return NULL;
431         }
432         tmp = strchr(tpass, '\n');
433         if(tmp) *tmp = 0;
434         return BUF_strdup(tpass);
435 }
436
437 int add_oid_section(BIO *err, LHASH *conf)
438 {       
439         char *p;
440         STACK_OF(CONF_VALUE) *sktmp;
441         CONF_VALUE *cnf;
442         int i;
443         if(!(p=CONF_get_string(conf,NULL,"oid_section"))) return 1;
444         if(!(sktmp = CONF_get_section(conf, p))) {
445                 BIO_printf(err, "problem loading oid section %s\n", p);
446                 return 0;
447         }
448         for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
449                 cnf = sk_CONF_VALUE_value(sktmp, i);
450                 if(OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
451                         BIO_printf(err, "problem creating object %s=%s\n",
452                                                          cnf->name, cnf->value);
453                         return 0;
454                 }
455         }
456         return 1;
457 }
458
459 X509 *load_cert(BIO *err, char *file, int format)
460         {
461         ASN1_HEADER *ah=NULL;
462         BUF_MEM *buf=NULL;
463         X509 *x=NULL;
464         BIO *cert;
465
466         if ((cert=BIO_new(BIO_s_file())) == NULL)
467                 {
468                 ERR_print_errors(err);
469                 goto end;
470                 }
471
472         if (file == NULL)
473                 BIO_set_fp(cert,stdin,BIO_NOCLOSE);
474         else
475                 {
476                 if (BIO_read_filename(cert,file) <= 0)
477                         {
478                         perror(file);
479                         goto end;
480                         }
481                 }
482
483         if      (format == FORMAT_ASN1)
484                 x=d2i_X509_bio(cert,NULL);
485         else if (format == FORMAT_NETSCAPE)
486                 {
487                 unsigned char *p,*op;
488                 int size=0,i;
489
490                 /* We sort of have to do it this way because it is sort of nice
491                  * to read the header first and check it, then
492                  * try to read the certificate */
493                 buf=BUF_MEM_new();
494                 for (;;)
495                         {
496                         if ((buf == NULL) || (!BUF_MEM_grow(buf,size+1024*10)))
497                                 goto end;
498                         i=BIO_read(cert,&(buf->data[size]),1024*10);
499                         size+=i;
500                         if (i == 0) break;
501                         if (i < 0)
502                                 {
503                                 perror("reading certificate");
504                                 goto end;
505                                 }
506                         }
507                 p=(unsigned char *)buf->data;
508                 op=p;
509
510                 /* First load the header */
511                 if ((ah=d2i_ASN1_HEADER(NULL,&p,(long)size)) == NULL)
512                         goto end;
513                 if ((ah->header == NULL) || (ah->header->data == NULL) ||
514                         (strncmp(NETSCAPE_CERT_HDR,(char *)ah->header->data,
515                         ah->header->length) != 0))
516                         {
517                         BIO_printf(err,"Error reading header on certificate\n");
518                         goto end;
519                         }
520                 /* header is ok, so now read the object */
521                 p=op;
522                 ah->meth=X509_asn1_meth();
523                 if ((ah=d2i_ASN1_HEADER(&ah,&p,(long)size)) == NULL)
524                         goto end;
525                 x=(X509 *)ah->data;
526                 ah->data=NULL;
527                 }
528         else if (format == FORMAT_PEM)
529                 x=PEM_read_bio_X509_AUX(cert,NULL,NULL,NULL);
530         else if (format == FORMAT_PKCS12)
531                 {
532                 PKCS12 *p12 = d2i_PKCS12_bio(cert, NULL);
533
534                 PKCS12_parse(p12, NULL, NULL, &x, NULL);
535                 PKCS12_free(p12);
536                 p12 = NULL;
537                 }
538         else    {
539                 BIO_printf(err,"bad input format specified for input cert\n");
540                 goto end;
541                 }
542 end:
543         if (x == NULL)
544                 {
545                 BIO_printf(err,"unable to load certificate\n");
546                 ERR_print_errors(err);
547                 }
548         if (ah != NULL) ASN1_HEADER_free(ah);
549         if (cert != NULL) BIO_free(cert);
550         if (buf != NULL) BUF_MEM_free(buf);
551         return(x);
552         }
553
554 EVP_PKEY *load_key(BIO *err, char *file, int format, char *pass)
555         {
556         BIO *key=NULL;
557         EVP_PKEY *pkey=NULL;
558
559         if (file == NULL)
560                 {
561                 BIO_printf(err,"no keyfile specified\n");
562                 goto end;
563                 }
564         key=BIO_new(BIO_s_file());
565         if (key == NULL)
566                 {
567                 ERR_print_errors(err);
568                 goto end;
569                 }
570         if (BIO_read_filename(key,file) <= 0)
571                 {
572                 perror(file);
573                 goto end;
574                 }
575         if (format == FORMAT_ASN1)
576                 {
577                 pkey=d2i_PrivateKey_bio(key, NULL);
578                 }
579         else if (format == FORMAT_PEM)
580                 {
581                 pkey=PEM_read_bio_PrivateKey(key,NULL,NULL,pass);
582                 }
583         else if (format == FORMAT_PKCS12)
584                 {
585                 PKCS12 *p12 = d2i_PKCS12_bio(key, NULL);
586
587                 PKCS12_parse(p12, pass, &pkey, NULL, NULL);
588                 PKCS12_free(p12);
589                 p12 = NULL;
590                 }
591         else
592                 {
593                 BIO_printf(err,"bad input format specified for key\n");
594                 goto end;
595                 }
596  end:
597         if (key != NULL) BIO_free(key);
598         if (pkey == NULL)
599                 BIO_printf(err,"unable to load Private Key\n");
600         return(pkey);
601         }
602
603 EVP_PKEY *load_pubkey(BIO *err, char *file, int format)
604         {
605         BIO *key=NULL;
606         EVP_PKEY *pkey=NULL;
607
608         if (file == NULL)
609                 {
610                 BIO_printf(err,"no keyfile specified\n");
611                 goto end;
612                 }
613         key=BIO_new(BIO_s_file());
614         if (key == NULL)
615                 {
616                 ERR_print_errors(err);
617                 goto end;
618                 }
619         if (BIO_read_filename(key,file) <= 0)
620                 {
621                 perror(file);
622                 goto end;
623                 }
624         if (format == FORMAT_ASN1)
625                 {
626                 pkey=d2i_PUBKEY_bio(key, NULL);
627                 }
628         else if (format == FORMAT_PEM)
629                 {
630                 pkey=PEM_read_bio_PUBKEY(key,NULL,NULL,NULL);
631                 }
632         else
633                 {
634                 BIO_printf(err,"bad input format specified for key\n");
635                 goto end;
636                 }
637  end:
638         if (key != NULL) BIO_free(key);
639         if (pkey == NULL)
640                 BIO_printf(err,"unable to load Public Key\n");
641         return(pkey);
642         }
643
644 STACK_OF(X509) *load_certs(BIO *err, char *file, int format)
645         {
646         BIO *certs;
647         int i;
648         STACK_OF(X509) *othercerts = NULL;
649         STACK_OF(X509_INFO) *allcerts = NULL;
650         X509_INFO *xi;
651
652         if((certs = BIO_new(BIO_s_file())) == NULL)
653                 {
654                 ERR_print_errors(err);
655                 goto end;
656                 }
657
658         if (file == NULL)
659                 BIO_set_fp(certs,stdin,BIO_NOCLOSE);
660         else
661                 {
662                 if (BIO_read_filename(certs,file) <= 0)
663                         {
664                         perror(file);
665                         goto end;
666                         }
667                 }
668
669         if      (format == FORMAT_PEM)
670                 {
671                 othercerts = sk_X509_new_null();
672                 if(!othercerts)
673                         {
674                         sk_X509_free(othercerts);
675                         othercerts = NULL;
676                         goto end;
677                         }
678                 allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
679                 for(i = 0; i < sk_X509_INFO_num(allcerts); i++)
680                         {
681                         xi = sk_X509_INFO_value (allcerts, i);
682                         if (xi->x509)
683                                 {
684                                 sk_X509_push(othercerts, xi->x509);
685                                 xi->x509 = NULL;
686                                 }
687                         }
688                 goto end;
689                 }
690         else    {
691                 BIO_printf(err,"bad input format specified for input cert\n");
692                 goto end;
693                 }
694 end:
695         if (othercerts == NULL)
696                 {
697                 BIO_printf(err,"unable to load certificates\n");
698                 ERR_print_errors(err);
699                 }
700         if (allcerts) sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
701         if (certs != NULL) BIO_free(certs);
702         return(othercerts);
703         }
704
705
706 #define X509V3_EXT_UNKNOWN_MASK         (0xfL << 16)
707 /* Return error for unknown extensions */
708 #define X509V3_EXT_DEFAULT              0
709 /* Print error for unknown extensions */
710 #define X509V3_EXT_ERROR_UNKNOWN        (1L << 16)
711 /* ASN1 parse unknown extensions */
712 #define X509V3_EXT_PARSE_UNKNOWN        (2L << 16)
713 /* BIO_dump unknown extensions */
714 #define X509V3_EXT_DUMP_UNKNOWN         (3L << 16)
715
716 int set_cert_ex(unsigned long *flags, const char *arg)
717 {
718         static const NAME_EX_TBL cert_tbl[] = {
719                 { "compatible", X509_FLAG_COMPAT, 0xffffffffl},
720                 { "no_header", X509_FLAG_NO_HEADER, 0},
721                 { "no_version", X509_FLAG_NO_VERSION, 0},
722                 { "no_serial", X509_FLAG_NO_SERIAL, 0},
723                 { "no_signame", X509_FLAG_NO_SIGNAME, 0},
724                 { "no_validity", X509_FLAG_NO_VALIDITY, 0},
725                 { "no_subject", X509_FLAG_NO_SUBJECT, 0},
726                 { "no_pubkey", X509_FLAG_NO_PUBKEY, 0},
727                 { "no_extensions", X509_FLAG_NO_EXTENSIONS, 0},
728                 { "no_sigdump", X509_FLAG_NO_SIGDUMP, 0},
729                 { "no_aux", X509_FLAG_NO_AUX, 0},
730                 { "ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK},
731                 { "ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
732                 { "ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
733                 { "ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
734                 { NULL, 0, 0}
735         };
736         return set_table_opts(flags, arg, cert_tbl);
737 }
738
739 int set_name_ex(unsigned long *flags, const char *arg)
740 {
741         static const NAME_EX_TBL ex_tbl[] = {
742                 { "esc_2253", ASN1_STRFLGS_ESC_2253, 0},
743                 { "esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0},
744                 { "esc_msb", ASN1_STRFLGS_ESC_MSB, 0},
745                 { "use_quote", ASN1_STRFLGS_ESC_QUOTE, 0},
746                 { "utf8", ASN1_STRFLGS_UTF8_CONVERT, 0},
747                 { "ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0},
748                 { "show_type", ASN1_STRFLGS_SHOW_TYPE, 0},
749                 { "dump_all", ASN1_STRFLGS_DUMP_ALL, 0},
750                 { "dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0},
751                 { "dump_der", ASN1_STRFLGS_DUMP_DER, 0},
752                 { "compat", XN_FLAG_COMPAT, 0xffffffffL},
753                 { "sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK},
754                 { "sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK},
755                 { "sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK},
756                 { "sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK},
757                 { "dn_rev", XN_FLAG_DN_REV, 0},
758                 { "nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK},
759                 { "sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK},
760                 { "lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK},
761                 { "oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK},
762                 { "space_eq", XN_FLAG_SPC_EQ, 0},
763                 { "dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0},
764                 { "RFC2253", XN_FLAG_RFC2253, 0xffffffffL},
765                 { "oneline", XN_FLAG_ONELINE, 0xffffffffL},
766                 { "multiline", XN_FLAG_MULTILINE, 0xffffffffL},
767                 { NULL, 0, 0}
768         };
769         return set_table_opts(flags, arg, ex_tbl);
770 }
771
772 static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
773 {
774         char c;
775         const NAME_EX_TBL *ptbl;
776         c = arg[0];
777
778         if(c == '-') {
779                 c = 0;
780                 arg++;
781         } else if (c == '+') {
782                 c = 1;
783                 arg++;
784         } else c = 1;
785
786         for(ptbl = in_tbl; ptbl->name; ptbl++) {
787                 if(!strcmp(arg, ptbl->name)) {
788                         *flags &= ~ptbl->mask;
789                         if(c) *flags |= ptbl->flag;
790                         else *flags &= ~ptbl->flag;
791                         return 1;
792                 }
793         }
794         return 0;
795 }
796
797 void print_name(BIO *out, char *title, X509_NAME *nm, unsigned long lflags)
798 {
799         char buf[256];
800         char mline = 0;
801         int indent = 0;
802         if(title) BIO_puts(out, title);
803         if((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
804                 mline = 1;
805                 indent = 4;
806         }
807         if(lflags == XN_FLAG_COMPAT) {
808                 X509_NAME_oneline(nm,buf,256);
809                 BIO_puts(out,buf);
810                 BIO_puts(out, "\n");
811         } else {
812                 if(mline) BIO_puts(out, "\n");
813                 X509_NAME_print_ex(out, nm, indent, lflags);
814                 BIO_puts(out, "\n");
815         }
816 }
817