This commit was manufactured by cvs2svn to create branch
[oweals/openssl.git] / apps / req.c
1 /* apps/req.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 <time.h>
62 #include <string.h>
63 #ifdef OPENSSL_NO_STDIO
64 #define APPS_WIN16
65 #endif
66 #include "apps.h"
67 #include <openssl/bio.h>
68 #include <openssl/evp.h>
69 #include <openssl/conf.h>
70 #include <openssl/err.h>
71 #include <openssl/asn1.h>
72 #include <openssl/x509.h>
73 #include <openssl/x509v3.h>
74 #include <openssl/objects.h>
75 #include <openssl/pem.h>
76
77 #define SECTION         "req"
78
79 #define BITS            "default_bits"
80 #define KEYFILE         "default_keyfile"
81 #define PROMPT          "prompt"
82 #define DISTINGUISHED_NAME      "distinguished_name"
83 #define ATTRIBUTES      "attributes"
84 #define V3_EXTENSIONS   "x509_extensions"
85 #define REQ_EXTENSIONS  "req_extensions"
86 #define STRING_MASK     "string_mask"
87 #define UTF8_IN         "utf8"
88
89 #define DEFAULT_KEY_LENGTH      512
90 #define MIN_KEY_LENGTH          384
91
92 #undef PROG
93 #define PROG    req_main
94
95 /* -inform arg  - input format - default PEM (DER or PEM)
96  * -outform arg - output format - default PEM
97  * -in arg      - input file - default stdin
98  * -out arg     - output file - default stdout
99  * -verify      - check request signature
100  * -noout       - don't print stuff out.
101  * -text        - print out human readable text.
102  * -nodes       - no des encryption
103  * -config file - Load configuration file.
104  * -key file    - make a request using key in file (or use it for verification).
105  * -keyform arg - key file format.
106  * -rand file(s) - load the file(s) into the PRNG.
107  * -newkey      - make a key and a request.
108  * -modulus     - print RSA modulus.
109  * -pubkey      - output Public Key.
110  * -x509        - output a self signed X509 structure instead.
111  * -asn1-kludge - output new certificate request in a format that some CA's
112  *                require.  This format is wrong
113  */
114
115 static int make_REQ(X509_REQ *req,EVP_PKEY *pkey,char *dn,int attribs,
116                 unsigned long chtype);
117 static int build_subject(X509_REQ *req, char *subj, unsigned long chtype);
118 static int prompt_info(X509_REQ *req,
119                 STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
120                 STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs,
121                 unsigned long chtype);
122 static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *sk,
123                                 STACK_OF(CONF_VALUE) *attr, int attribs,
124                                 unsigned long chtype);
125 static int add_attribute_object(X509_REQ *req, char *text,
126                                 char *def, char *value, int nid, int n_min,
127                                 int n_max, unsigned long chtype);
128 static int add_DN_object(X509_NAME *n, char *text, char *def, char *value,
129         int nid,int n_min,int n_max, unsigned long chtype);
130 #ifndef OPENSSL_NO_RSA
131 static void MS_CALLBACK req_cb(int p,int n,void *arg);
132 #endif
133 static int req_check_len(int len,int n_min,int n_max);
134 static int check_end(char *str, char *end);
135 #ifndef MONOLITH
136 static char *default_config_file=NULL;
137 static CONF *config=NULL;
138 #endif
139 static CONF *req_conf=NULL;
140 static int batch=0;
141
142 #define TYPE_RSA        1
143 #define TYPE_DSA        2
144 #define TYPE_DH         3
145
146 int MAIN(int, char **);
147
148 int MAIN(int argc, char **argv)
149         {
150         ENGINE *e = NULL;
151 #ifndef OPENSSL_NO_DSA
152         DSA *dsa_params=NULL;
153 #endif
154         unsigned long nmflag = 0, reqflag = 0;
155         int ex=1,x509=0,days=30;
156         X509 *x509ss=NULL;
157         X509_REQ *req=NULL;
158         EVP_PKEY *pkey=NULL;
159         int i=0,badops=0,newreq=0,verbose=0,pkey_type=TYPE_RSA;
160         long newkey = -1;
161         BIO *in=NULL,*out=NULL;
162         int informat,outformat,verify=0,noout=0,text=0,keyform=FORMAT_PEM;
163         int nodes=0,kludge=0,newhdr=0,subject=0,pubkey=0;
164         char *infile,*outfile,*prog,*keyfile=NULL,*template=NULL,*keyout=NULL;
165         char *engine=NULL;
166         char *extensions = NULL;
167         char *req_exts = NULL;
168         const EVP_CIPHER *cipher=NULL;
169         ASN1_INTEGER *serial = NULL;
170         int modulus=0;
171         char *inrand=NULL;
172         char *passargin = NULL, *passargout = NULL;
173         char *passin = NULL, *passout = NULL;
174         char *p;
175         char *subj = NULL;
176         const EVP_MD *md_alg=NULL,*digest=EVP_md5();
177         unsigned long chtype = MBSTRING_ASC;
178 #ifndef MONOLITH
179         MS_STATIC char config_name[256];
180         long errline;
181 #endif
182
183         req_conf = NULL;
184 #ifndef OPENSSL_NO_DES
185         cipher=EVP_des_ede3_cbc();
186 #endif
187         apps_startup();
188
189         if (bio_err == NULL)
190                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
191                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
192
193         infile=NULL;
194         outfile=NULL;
195         informat=FORMAT_PEM;
196         outformat=FORMAT_PEM;
197
198         prog=argv[0];
199         argc--;
200         argv++;
201         while (argc >= 1)
202                 {
203                 if      (strcmp(*argv,"-inform") == 0)
204                         {
205                         if (--argc < 1) goto bad;
206                         informat=str2fmt(*(++argv));
207                         }
208                 else if (strcmp(*argv,"-outform") == 0)
209                         {
210                         if (--argc < 1) goto bad;
211                         outformat=str2fmt(*(++argv));
212                         }
213                 else if (strcmp(*argv,"-engine") == 0)
214                         {
215                         if (--argc < 1) goto bad;
216                         engine= *(++argv);
217                         }
218                 else if (strcmp(*argv,"-key") == 0)
219                         {
220                         if (--argc < 1) goto bad;
221                         keyfile= *(++argv);
222                         }
223                 else if (strcmp(*argv,"-pubkey") == 0)
224                         {
225                         pubkey=1;
226                         }
227                 else if (strcmp(*argv,"-new") == 0)
228                         {
229                         newreq=1;
230                         }
231                 else if (strcmp(*argv,"-config") == 0)
232                         {       
233                         if (--argc < 1) goto bad;
234                         template= *(++argv);
235                         }
236                 else if (strcmp(*argv,"-keyform") == 0)
237                         {
238                         if (--argc < 1) goto bad;
239                         keyform=str2fmt(*(++argv));
240                         }
241                 else if (strcmp(*argv,"-in") == 0)
242                         {
243                         if (--argc < 1) goto bad;
244                         infile= *(++argv);
245                         }
246                 else if (strcmp(*argv,"-out") == 0)
247                         {
248                         if (--argc < 1) goto bad;
249                         outfile= *(++argv);
250                         }
251                 else if (strcmp(*argv,"-keyout") == 0)
252                         {
253                         if (--argc < 1) goto bad;
254                         keyout= *(++argv);
255                         }
256                 else if (strcmp(*argv,"-passin") == 0)
257                         {
258                         if (--argc < 1) goto bad;
259                         passargin= *(++argv);
260                         }
261                 else if (strcmp(*argv,"-passout") == 0)
262                         {
263                         if (--argc < 1) goto bad;
264                         passargout= *(++argv);
265                         }
266                 else if (strcmp(*argv,"-rand") == 0)
267                         {
268                         if (--argc < 1) goto bad;
269                         inrand= *(++argv);
270                         }
271                 else if (strcmp(*argv,"-newkey") == 0)
272                         {
273                         int is_numeric;
274
275                         if (--argc < 1) goto bad;
276                         p= *(++argv);
277                         is_numeric = p[0] >= '0' && p[0] <= '9';
278                         if (strncmp("rsa:",p,4) == 0 || is_numeric)
279                                 {
280                                 pkey_type=TYPE_RSA;
281                                 if(!is_numeric)
282                                     p+=4;
283                                 newkey= atoi(p);
284                                 }
285                         else
286 #ifndef OPENSSL_NO_DSA
287                                 if (strncmp("dsa:",p,4) == 0)
288                                 {
289                                 X509 *xtmp=NULL;
290                                 EVP_PKEY *dtmp;
291
292                                 pkey_type=TYPE_DSA;
293                                 p+=4;
294                                 if ((in=BIO_new_file(p,"r")) == NULL)
295                                         {
296                                         perror(p);
297                                         goto end;
298                                         }
299                                 if ((dsa_params=PEM_read_bio_DSAparams(in,NULL,NULL,NULL)) == NULL)
300                                         {
301                                         ERR_clear_error();
302                                         (void)BIO_reset(in);
303                                         if ((xtmp=PEM_read_bio_X509(in,NULL,NULL,NULL)) == NULL)
304                                                 {
305                                                 BIO_printf(bio_err,"unable to load DSA parameters from file\n");
306                                                 goto end;
307                                                 }
308
309                                         if ((dtmp=X509_get_pubkey(xtmp)) == NULL) goto end;
310                                         if (dtmp->type == EVP_PKEY_DSA)
311                                                 dsa_params=DSAparams_dup(dtmp->pkey.dsa);
312                                         EVP_PKEY_free(dtmp);
313                                         X509_free(xtmp);
314                                         if (dsa_params == NULL)
315                                                 {
316                                                 BIO_printf(bio_err,"Certificate does not contain DSA parameters\n");
317                                                 goto end;
318                                                 }
319                                         }
320                                 BIO_free(in);
321                                 newkey=BN_num_bits(dsa_params->p);
322                                 in=NULL;
323                                 }
324                         else 
325 #endif
326 #ifndef OPENSSL_NO_DH
327                                 if (strncmp("dh:",p,4) == 0)
328                                 {
329                                 pkey_type=TYPE_DH;
330                                 p+=3;
331                                 }
332                         else
333 #endif
334                                 pkey_type=TYPE_RSA;
335
336                         newreq=1;
337                         }
338                 else if (strcmp(*argv,"-batch") == 0)
339                         batch=1;
340                 else if (strcmp(*argv,"-newhdr") == 0)
341                         newhdr=1;
342                 else if (strcmp(*argv,"-modulus") == 0)
343                         modulus=1;
344                 else if (strcmp(*argv,"-verify") == 0)
345                         verify=1;
346                 else if (strcmp(*argv,"-nodes") == 0)
347                         nodes=1;
348                 else if (strcmp(*argv,"-noout") == 0)
349                         noout=1;
350                 else if (strcmp(*argv,"-verbose") == 0)
351                         verbose=1;
352                 else if (strcmp(*argv,"-utf8") == 0)
353                         chtype = MBSTRING_UTF8;
354                 else if (strcmp(*argv,"-nameopt") == 0)
355                         {
356                         if (--argc < 1) goto bad;
357                         if (!set_name_ex(&nmflag, *(++argv))) goto bad;
358                         }
359                 else if (strcmp(*argv,"-reqopt") == 0)
360                         {
361                         if (--argc < 1) goto bad;
362                         if (!set_cert_ex(&reqflag, *(++argv))) goto bad;
363                         }
364                 else if (strcmp(*argv,"-subject") == 0)
365                         subject=1;
366                 else if (strcmp(*argv,"-text") == 0)
367                         text=1;
368                 else if (strcmp(*argv,"-x509") == 0)
369                         x509=1;
370                 else if (strcmp(*argv,"-asn1-kludge") == 0)
371                         kludge=1;
372                 else if (strcmp(*argv,"-no-asn1-kludge") == 0)
373                         kludge=0;
374                 else if (strcmp(*argv,"-subj") == 0)
375                         {
376                         if (--argc < 1) goto bad;
377                         subj= *(++argv);
378                         }
379                 else if (strcmp(*argv,"-days") == 0)
380                         {
381                         if (--argc < 1) goto bad;
382                         days= atoi(*(++argv));
383                         if (days == 0) days=30;
384                         }
385                 else if (strcmp(*argv,"-set_serial") == 0)
386                         {
387                         if (--argc < 1) goto bad;
388                         serial = s2i_ASN1_INTEGER(NULL, *(++argv));
389                         if (!serial) goto bad;
390                         }
391                 else if ((md_alg=EVP_get_digestbyname(&((*argv)[1]))) != NULL)
392                         {
393                         /* ok */
394                         digest=md_alg;
395                         }
396                 else if (strcmp(*argv,"-extensions") == 0)
397                         {
398                         if (--argc < 1) goto bad;
399                         extensions = *(++argv);
400                         }
401                 else if (strcmp(*argv,"-reqexts") == 0)
402                         {
403                         if (--argc < 1) goto bad;
404                         req_exts = *(++argv);
405                         }
406                 else
407                         {
408                         BIO_printf(bio_err,"unknown option %s\n",*argv);
409                         badops=1;
410                         break;
411                         }
412                 argc--;
413                 argv++;
414                 }
415
416         if (badops)
417                 {
418 bad:
419                 BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
420                 BIO_printf(bio_err,"where options  are\n");
421                 BIO_printf(bio_err," -inform arg    input format - DER or PEM\n");
422                 BIO_printf(bio_err," -outform arg   output format - DER or PEM\n");
423                 BIO_printf(bio_err," -in arg        input file\n");
424                 BIO_printf(bio_err," -out arg       output file\n");
425                 BIO_printf(bio_err," -text          text form of request\n");
426                 BIO_printf(bio_err," -pubkey        output public key\n");
427                 BIO_printf(bio_err," -noout         do not output REQ\n");
428                 BIO_printf(bio_err," -verify        verify signature on REQ\n");
429                 BIO_printf(bio_err," -modulus       RSA modulus\n");
430                 BIO_printf(bio_err," -nodes         don't encrypt the output key\n");
431                 BIO_printf(bio_err," -engine e      use engine e, possibly a hardware device\n");
432                 BIO_printf(bio_err," -subject       output the request's subject\n");
433                 BIO_printf(bio_err," -passin        private key password source\n");
434                 BIO_printf(bio_err," -key file      use the private key contained in file\n");
435                 BIO_printf(bio_err," -keyform arg   key file format\n");
436                 BIO_printf(bio_err," -keyout arg    file to send the key to\n");
437                 BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
438                 BIO_printf(bio_err,"                load the file (or the files in the directory) into\n");
439                 BIO_printf(bio_err,"                the random number generator\n");
440                 BIO_printf(bio_err," -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
441                 BIO_printf(bio_err," -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
442                 BIO_printf(bio_err," -[digest]      Digest to sign with (md5, sha1, md2, mdc2, md4)\n");
443                 BIO_printf(bio_err," -config file   request template file.\n");
444                 BIO_printf(bio_err," -subj arg      set or modify request subject\n");
445                 BIO_printf(bio_err," -new           new request.\n");
446                 BIO_printf(bio_err," -batch         do not ask anything during request generation\n");
447                 BIO_printf(bio_err," -x509          output a x509 structure instead of a cert. req.\n");
448                 BIO_printf(bio_err," -days          number of days a certificate generated by -x509 is valid for.\n");
449                 BIO_printf(bio_err," -set_serial    serial number to use for a certificate generated by -x509.\n");
450                 BIO_printf(bio_err," -newhdr        output \"NEW\" in the header lines\n");
451                 BIO_printf(bio_err," -asn1-kludge   Output the 'request' in a format that is wrong but some CA's\n");
452                 BIO_printf(bio_err,"                have been reported as requiring\n");
453                 BIO_printf(bio_err," -extensions .. specify certificate extension section (override value in config file)\n");
454                 BIO_printf(bio_err," -reqexts ..    specify request extension section (override value in config file)\n");
455                 BIO_printf(bio_err," -utf8          input characters are UTF8 (default ASCII)\n");
456                 BIO_printf(bio_err," -nameopt arg   - various certificate name options\n");
457                 BIO_printf(bio_err," -reqopt arg    - various request text options\n\n");
458                 goto end;
459                 }
460
461         ERR_load_crypto_strings();
462         if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
463                 BIO_printf(bio_err, "Error getting passwords\n");
464                 goto end;
465         }
466
467 #ifndef MONOLITH /* else this has happened in openssl.c (global `config') */
468         /* Lets load up our environment a little */
469         p=getenv("OPENSSL_CONF");
470         if (p == NULL)
471                 p=getenv("SSLEAY_CONF");
472         if (p == NULL)
473                 {
474                 strcpy(config_name,X509_get_default_cert_area());
475 #ifndef OPENSSL_SYS_VMS
476                 strcat(config_name,"/");
477 #endif
478                 strcat(config_name,OPENSSL_CONF);
479                 p=config_name;
480                 }
481         default_config_file=p;
482         config=NCONF_new(NULL);
483         i=NCONF_load(config, p, &errline);
484 #endif
485
486         if (template != NULL)
487                 {
488                 long errline;
489
490                 if( verbose )
491                         BIO_printf(bio_err,"Using configuration from %s\n",template);
492                 req_conf=NCONF_new(NULL);
493                 i=NCONF_load(req_conf,template,&errline);
494                 if (i == 0)
495                         {
496                         BIO_printf(bio_err,"error on line %ld of %s\n",errline,template);
497                         goto end;
498                         }
499                 }
500         else
501                 {
502                 req_conf=config;
503                 if( verbose )
504                         BIO_printf(bio_err,"Using configuration from %s\n",
505                         default_config_file);
506                 if (req_conf == NULL)
507                         {
508                         BIO_printf(bio_err,"Unable to load config info\n");
509                         }
510                 }
511
512         if (req_conf != NULL)
513                 {
514                 if (!load_config(bio_err, req_conf))
515                         goto end;
516                 p=NCONF_get_string(req_conf,NULL,"oid_file");
517                 if (p == NULL)
518                         ERR_clear_error();
519                 if (p != NULL)
520                         {
521                         BIO *oid_bio;
522
523                         oid_bio=BIO_new_file(p,"r");
524                         if (oid_bio == NULL) 
525                                 {
526                                 /*
527                                 BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
528                                 ERR_print_errors(bio_err);
529                                 */
530                                 }
531                         else
532                                 {
533                                 OBJ_create_objects(oid_bio);
534                                 BIO_free(oid_bio);
535                                 }
536                         }
537                 }
538         if(!add_oid_section(bio_err, req_conf)) goto end;
539
540         if (md_alg == NULL)
541                 {
542                 p=NCONF_get_string(req_conf,SECTION,"default_md");
543                 if (p == NULL)
544                         ERR_clear_error();
545                 if (p != NULL)
546                         {
547                         if ((md_alg=EVP_get_digestbyname(p)) != NULL)
548                                 digest=md_alg;
549                         }
550                 }
551
552         if (!extensions)
553                 {
554                 extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
555                 if (!extensions)
556                         ERR_clear_error();
557                 }
558         if (extensions) {
559                 /* Check syntax of file */
560                 X509V3_CTX ctx;
561                 X509V3_set_ctx_test(&ctx);
562                 X509V3_set_nconf(&ctx, req_conf);
563                 if(!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) {
564                         BIO_printf(bio_err,
565                          "Error Loading extension section %s\n", extensions);
566                         goto end;
567                 }
568         }
569
570         if(!passin)
571                 {
572                 passin = NCONF_get_string(req_conf, SECTION, "input_password");
573                 if (!passin)
574                         ERR_clear_error();
575                 }
576         
577         if(!passout)
578                 {
579                 passout = NCONF_get_string(req_conf, SECTION, "output_password");
580                 if (!passout)
581                         ERR_clear_error();
582                 }
583
584         p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
585         if (!p)
586                 ERR_clear_error();
587
588         if(p && !ASN1_STRING_set_default_mask_asc(p)) {
589                 BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
590                 goto end;
591         }
592
593         if (chtype != MBSTRING_UTF8)
594                 {
595                 p = NCONF_get_string(req_conf, SECTION, UTF8_IN);
596                 if (!p)
597                         ERR_clear_error();
598                 else if (!strcmp(p, "yes"))
599                         chtype = MBSTRING_UTF8;
600                 }
601
602
603         if(!req_exts)
604                 {
605                 req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
606                 if (!req_exts)
607                         ERR_clear_error();
608                 }
609         if(req_exts) {
610                 /* Check syntax of file */
611                 X509V3_CTX ctx;
612                 X509V3_set_ctx_test(&ctx);
613                 X509V3_set_nconf(&ctx, req_conf);
614                 if(!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) {
615                         BIO_printf(bio_err,
616                          "Error Loading request extension section %s\n",
617                                                                 req_exts);
618                         goto end;
619                 }
620         }
621
622         in=BIO_new(BIO_s_file());
623         out=BIO_new(BIO_s_file());
624         if ((in == NULL) || (out == NULL))
625                 goto end;
626
627         e = setup_engine(bio_err, engine, 0);
628
629         if (keyfile != NULL)
630                 {
631                 pkey = load_key(bio_err, keyfile, keyform, 0, passin, e,
632                         "Private Key");
633                 if (!pkey)
634                         {
635                         /* load_key() has already printed an appropriate
636                            message */
637                         goto end;
638                         }
639                 if (EVP_PKEY_type(pkey->type) == EVP_PKEY_DSA)
640                         {
641                         char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE");
642                         if (randfile == NULL)
643                                 ERR_clear_error();
644                         app_RAND_load_file(randfile, bio_err, 0);
645                         }
646                 }
647
648         if (newreq && (pkey == NULL))
649                 {
650                 char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE");
651                 if (randfile == NULL)
652                         ERR_clear_error();
653                 app_RAND_load_file(randfile, bio_err, 0);
654                 if (inrand)
655                         app_RAND_load_files(inrand);
656         
657                 if (newkey <= 0)
658                         {
659                         if (!NCONF_get_number(req_conf,SECTION,BITS, &newkey))
660                                 newkey=DEFAULT_KEY_LENGTH;
661                         }
662
663                 if (newkey < MIN_KEY_LENGTH)
664                         {
665                         BIO_printf(bio_err,"private key length is too short,\n");
666                         BIO_printf(bio_err,"it needs to be at least %d bits, not %d\n",MIN_KEY_LENGTH,newkey);
667                         goto end;
668                         }
669                 BIO_printf(bio_err,"Generating a %d bit %s private key\n",
670                         newkey,(pkey_type == TYPE_RSA)?"RSA":"DSA");
671
672                 if ((pkey=EVP_PKEY_new()) == NULL) goto end;
673
674 #ifndef OPENSSL_NO_RSA
675                 if (pkey_type == TYPE_RSA)
676                         {
677                         if (!EVP_PKEY_assign_RSA(pkey,
678                                 RSA_generate_key(newkey,0x10001,
679                                         req_cb,bio_err)))
680                                 goto end;
681                         }
682                 else
683 #endif
684 #ifndef OPENSSL_NO_DSA
685                         if (pkey_type == TYPE_DSA)
686                         {
687                         if (!DSA_generate_key(dsa_params)) goto end;
688                         if (!EVP_PKEY_assign_DSA(pkey,dsa_params)) goto end;
689                         dsa_params=NULL;
690                         }
691 #endif
692
693                 app_RAND_write_file(randfile, bio_err);
694
695                 if (pkey == NULL) goto end;
696
697                 if (keyout == NULL)
698                         {
699                         keyout=NCONF_get_string(req_conf,SECTION,KEYFILE);
700                         if (keyout == NULL)
701                                 ERR_clear_error();
702                         }
703                 
704                 if (keyout == NULL)
705                         {
706                         BIO_printf(bio_err,"writing new private key to stdout\n");
707                         BIO_set_fp(out,stdout,BIO_NOCLOSE);
708 #ifdef OPENSSL_SYS_VMS
709                         {
710                         BIO *tmpbio = BIO_new(BIO_f_linebuffer());
711                         out = BIO_push(tmpbio, out);
712                         }
713 #endif
714                         }
715                 else
716                         {
717                         BIO_printf(bio_err,"writing new private key to '%s'\n",keyout);
718                         if (BIO_write_filename(out,keyout) <= 0)
719                                 {
720                                 perror(keyout);
721                                 goto end;
722                                 }
723                         }
724
725                 p=NCONF_get_string(req_conf,SECTION,"encrypt_rsa_key");
726                 if (p == NULL)
727                         {
728                         ERR_clear_error();
729                         p=NCONF_get_string(req_conf,SECTION,"encrypt_key");
730                         if (p == NULL)
731                                 ERR_clear_error();
732                         }
733                 if ((p != NULL) && (strcmp(p,"no") == 0))
734                         cipher=NULL;
735                 if (nodes) cipher=NULL;
736                 
737                 i=0;
738 loop:
739                 if (!PEM_write_bio_PrivateKey(out,pkey,cipher,
740                         NULL,0,NULL,passout))
741                         {
742                         if ((ERR_GET_REASON(ERR_peek_error()) ==
743                                 PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3))
744                                 {
745                                 ERR_clear_error();
746                                 i++;
747                                 goto loop;
748                                 }
749                         goto end;
750                         }
751                 BIO_printf(bio_err,"-----\n");
752                 }
753
754         if (!newreq)
755                 {
756                 /* Since we are using a pre-existing certificate
757                  * request, the kludge 'format' info should not be
758                  * changed. */
759                 kludge= -1;
760                 if (infile == NULL)
761                         BIO_set_fp(in,stdin,BIO_NOCLOSE);
762                 else
763                         {
764                         if (BIO_read_filename(in,infile) <= 0)
765                                 {
766                                 perror(infile);
767                                 goto end;
768                                 }
769                         }
770
771                 if      (informat == FORMAT_ASN1)
772                         req=d2i_X509_REQ_bio(in,NULL);
773                 else if (informat == FORMAT_PEM)
774                         req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL);
775                 else
776                         {
777                         BIO_printf(bio_err,"bad input format specified for X509 request\n");
778                         goto end;
779                         }
780                 if (req == NULL)
781                         {
782                         BIO_printf(bio_err,"unable to load X509 request\n");
783                         goto end;
784                         }
785                 }
786
787         if (newreq || x509)
788                 {
789                 if (pkey == NULL)
790                         {
791                         BIO_printf(bio_err,"you need to specify a private key\n");
792                         goto end;
793                         }
794 #ifndef OPENSSL_NO_DSA
795                 if (pkey->type == EVP_PKEY_DSA)
796                         digest=EVP_dss1();
797 #endif
798                 if (req == NULL)
799                         {
800                         req=X509_REQ_new();
801                         if (req == NULL)
802                                 {
803                                 goto end;
804                                 }
805
806                         i=make_REQ(req,pkey,subj,!x509, chtype);
807                         subj=NULL; /* done processing '-subj' option */
808                         if ((kludge > 0) && !sk_X509_ATTRIBUTE_num(req->req_info->attributes))
809                                 {
810                                 sk_X509_ATTRIBUTE_free(req->req_info->attributes);
811                                 req->req_info->attributes = NULL;
812                                 }
813                         if (!i)
814                                 {
815                                 BIO_printf(bio_err,"problems making Certificate Request\n");
816                                 goto end;
817                                 }
818                         }
819                 if (x509)
820                         {
821                         EVP_PKEY *tmppkey;
822                         X509V3_CTX ext_ctx;
823                         if ((x509ss=X509_new()) == NULL) goto end;
824
825                         /* Set version to V3 */
826                         if(!X509_set_version(x509ss, 2)) goto end;
827                         if (serial)
828                                 {
829                                 if (!X509_set_serialNumber(x509ss, serial)) goto end;
830                                 }
831                         else
832                                 {
833                                 if (!ASN1_INTEGER_set(X509_get_serialNumber(x509ss),0L)) goto end;
834                                 }
835
836                         if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req))) goto end;
837                         if (!X509_gmtime_adj(X509_get_notBefore(x509ss),0)) goto end;
838                         if (!X509_gmtime_adj(X509_get_notAfter(x509ss), (long)60*60*24*days)) goto end;
839                         if (!X509_set_subject_name(x509ss, X509_REQ_get_subject_name(req))) goto end;
840                         tmppkey = X509_REQ_get_pubkey(req);
841                         if (!tmppkey || !X509_set_pubkey(x509ss,tmppkey)) goto end;
842                         EVP_PKEY_free(tmppkey);
843
844                         /* Set up V3 context struct */
845
846                         X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
847                         X509V3_set_nconf(&ext_ctx, req_conf);
848
849                         /* Add extensions */
850                         if(extensions && !X509V3_EXT_add_nconf(req_conf, 
851                                         &ext_ctx, extensions, x509ss))
852                                 {
853                                 BIO_printf(bio_err,
854                                         "Error Loading extension section %s\n",
855                                         extensions);
856                                 goto end;
857                                 }
858                         
859                         if (!(i=X509_sign(x509ss,pkey,digest)))
860                                 goto end;
861                         }
862                 else
863                         {
864                         X509V3_CTX ext_ctx;
865
866                         /* Set up V3 context struct */
867
868                         X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
869                         X509V3_set_nconf(&ext_ctx, req_conf);
870
871                         /* Add extensions */
872                         if(req_exts && !X509V3_EXT_REQ_add_nconf(req_conf, 
873                                         &ext_ctx, req_exts, req))
874                                 {
875                                 BIO_printf(bio_err,
876                                         "Error Loading extension section %s\n",
877                                         req_exts);
878                                 goto end;
879                                 }
880                         if (!(i=X509_REQ_sign(req,pkey,digest)))
881                                 goto end;
882                         }
883                 }
884
885         if (subj && x509)
886                 {
887                 BIO_printf(bio_err, "Cannot modifiy certificate subject\n");
888                 goto end;
889                 }
890
891         if (subj && !x509)
892                 {
893                 if (verbose)
894                         {
895                         BIO_printf(bio_err, "Modifying Request's Subject\n");
896                         print_name(bio_err, "old subject=", X509_REQ_get_subject_name(req), nmflag);
897                         }
898
899                 if (build_subject(req, subj, chtype) == 0)
900                         {
901                         BIO_printf(bio_err, "ERROR: cannot modify subject\n");
902                         ex=1;
903                         goto end;
904                         }
905
906                 req->req_info->enc.modified = 1;
907
908                 if (verbose)
909                         {
910                         print_name(bio_err, "new subject=", X509_REQ_get_subject_name(req), nmflag);
911                         }
912                 }
913
914         if (verify && !x509)
915                 {
916                 int tmp=0;
917
918                 if (pkey == NULL)
919                         {
920                         pkey=X509_REQ_get_pubkey(req);
921                         tmp=1;
922                         if (pkey == NULL) goto end;
923                         }
924
925                 i=X509_REQ_verify(req,pkey);
926                 if (tmp) {
927                         EVP_PKEY_free(pkey);
928                         pkey=NULL;
929                 }
930
931                 if (i < 0)
932                         {
933                         goto end;
934                         }
935                 else if (i == 0)
936                         {
937                         BIO_printf(bio_err,"verify failure\n");
938                         ERR_print_errors(bio_err);
939                         }
940                 else /* if (i > 0) */
941                         BIO_printf(bio_err,"verify OK\n");
942                 }
943
944         if (noout && !text && !modulus && !subject && !pubkey)
945                 {
946                 ex=0;
947                 goto end;
948                 }
949
950         if (outfile == NULL)
951                 {
952                 BIO_set_fp(out,stdout,BIO_NOCLOSE);
953 #ifdef OPENSSL_SYS_VMS
954                 {
955                 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
956                 out = BIO_push(tmpbio, out);
957                 }
958 #endif
959                 }
960         else
961                 {
962                 if ((keyout != NULL) && (strcmp(outfile,keyout) == 0))
963                         i=(int)BIO_append_filename(out,outfile);
964                 else
965                         i=(int)BIO_write_filename(out,outfile);
966                 if (!i)
967                         {
968                         perror(outfile);
969                         goto end;
970                         }
971                 }
972
973         if (pubkey)
974                 {
975                 EVP_PKEY *tpubkey; 
976                 tpubkey=X509_REQ_get_pubkey(req);
977                 if (tpubkey == NULL)
978                         {
979                         BIO_printf(bio_err,"Error getting public key\n");
980                         ERR_print_errors(bio_err);
981                         goto end;
982                         }
983                 PEM_write_bio_PUBKEY(out, tpubkey);
984                 EVP_PKEY_free(tpubkey);
985                 }
986
987         if (text)
988                 {
989                 if (x509)
990                         X509_print_ex(out, x509ss, nmflag, reqflag);
991                 else    
992                         X509_REQ_print_ex(out, req, nmflag, reqflag);
993                 }
994
995         if(subject) 
996                 {
997                 if(x509)
998                         print_name(out, "subject=", X509_get_subject_name(x509ss), nmflag);
999                 else
1000                         print_name(out, "subject=", X509_REQ_get_subject_name(req), nmflag);
1001                 }
1002
1003         if (modulus)
1004                 {
1005                 EVP_PKEY *tpubkey;
1006
1007                 if (x509)
1008                         tpubkey=X509_get_pubkey(x509ss);
1009                 else
1010                         tpubkey=X509_REQ_get_pubkey(req);
1011                 if (tpubkey == NULL)
1012                         {
1013                         fprintf(stdout,"Modulus=unavailable\n");
1014                         goto end; 
1015                         }
1016                 fprintf(stdout,"Modulus=");
1017 #ifndef OPENSSL_NO_RSA
1018                 if (tpubkey->type == EVP_PKEY_RSA)
1019                         BN_print(out,tpubkey->pkey.rsa->n);
1020                 else
1021 #endif
1022                         fprintf(stdout,"Wrong Algorithm type");
1023                 EVP_PKEY_free(tpubkey);
1024                 fprintf(stdout,"\n");
1025                 }
1026
1027         if (!noout && !x509)
1028                 {
1029                 if      (outformat == FORMAT_ASN1)
1030                         i=i2d_X509_REQ_bio(out,req);
1031                 else if (outformat == FORMAT_PEM) {
1032                         if(newhdr) i=PEM_write_bio_X509_REQ_NEW(out,req);
1033                         else i=PEM_write_bio_X509_REQ(out,req);
1034                 } else {
1035                         BIO_printf(bio_err,"bad output format specified for outfile\n");
1036                         goto end;
1037                         }
1038                 if (!i)
1039                         {
1040                         BIO_printf(bio_err,"unable to write X509 request\n");
1041                         goto end;
1042                         }
1043                 }
1044         if (!noout && x509 && (x509ss != NULL))
1045                 {
1046                 if      (outformat == FORMAT_ASN1)
1047                         i=i2d_X509_bio(out,x509ss);
1048                 else if (outformat == FORMAT_PEM)
1049                         i=PEM_write_bio_X509(out,x509ss);
1050                 else    {
1051                         BIO_printf(bio_err,"bad output format specified for outfile\n");
1052                         goto end;
1053                         }
1054                 if (!i)
1055                         {
1056                         BIO_printf(bio_err,"unable to write X509 certificate\n");
1057                         goto end;
1058                         }
1059                 }
1060         ex=0;
1061 end:
1062         if (ex)
1063                 {
1064                 ERR_print_errors(bio_err);
1065                 }
1066         if ((req_conf != NULL) && (req_conf != config)) NCONF_free(req_conf);
1067         BIO_free(in);
1068         BIO_free_all(out);
1069         EVP_PKEY_free(pkey);
1070         X509_REQ_free(req);
1071         X509_free(x509ss);
1072         ASN1_INTEGER_free(serial);
1073         if(passargin && passin) OPENSSL_free(passin);
1074         if(passargout && passout) OPENSSL_free(passout);
1075         OBJ_cleanup();
1076 #ifndef OPENSSL_NO_DSA
1077         if (dsa_params != NULL) DSA_free(dsa_params);
1078 #endif
1079         apps_shutdown();
1080         EXIT(ex);
1081         }
1082
1083 static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int attribs,
1084                         unsigned long chtype)
1085         {
1086         int ret=0,i;
1087         char no_prompt = 0;
1088         STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL;
1089         char *tmp, *dn_sect,*attr_sect;
1090
1091         tmp=NCONF_get_string(req_conf,SECTION,PROMPT);
1092         if (tmp == NULL)
1093                 ERR_clear_error();
1094         if((tmp != NULL) && !strcmp(tmp, "no")) no_prompt = 1;
1095
1096         dn_sect=NCONF_get_string(req_conf,SECTION,DISTINGUISHED_NAME);
1097         if (dn_sect == NULL)
1098                 {
1099                 BIO_printf(bio_err,"unable to find '%s' in config\n",
1100                         DISTINGUISHED_NAME);
1101                 goto err;
1102                 }
1103         dn_sk=NCONF_get_section(req_conf,dn_sect);
1104         if (dn_sk == NULL)
1105                 {
1106                 BIO_printf(bio_err,"unable to get '%s' section\n",dn_sect);
1107                 goto err;
1108                 }
1109
1110         attr_sect=NCONF_get_string(req_conf,SECTION,ATTRIBUTES);
1111         if (attr_sect == NULL)
1112                 {
1113                 ERR_clear_error();              
1114                 attr_sk=NULL;
1115                 }
1116         else
1117                 {
1118                 attr_sk=NCONF_get_section(req_conf,attr_sect);
1119                 if (attr_sk == NULL)
1120                         {
1121                         BIO_printf(bio_err,"unable to get '%s' section\n",attr_sect);
1122                         goto err;
1123                         }
1124                 }
1125
1126         /* setup version number */
1127         if (!X509_REQ_set_version(req,0L)) goto err; /* version 1 */
1128
1129         if (no_prompt) 
1130                 i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
1131         else 
1132                 {
1133                 if (subj)
1134                         i = build_subject(req, subj, chtype);
1135                 else
1136                         i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs, chtype);
1137                 }
1138         if(!i) goto err;
1139
1140         if (!X509_REQ_set_pubkey(req,pkey)) goto err;
1141
1142         ret=1;
1143 err:
1144         return(ret);
1145         }
1146
1147 /*
1148  * subject is expected to be in the format /type0=value0/type1=value1/type2=...
1149  * where characters may be escaped by \
1150  */
1151 static int build_subject(X509_REQ *req, char *subject, unsigned long chtype)
1152         {
1153         X509_NAME *n;
1154
1155         if (!(n = do_subject(subject, chtype)))
1156                 return 0;
1157
1158         if (!X509_REQ_set_subject_name(req, n))
1159                 {
1160                 X509_NAME_free(n);
1161                 return 0;
1162                 }
1163         X509_NAME_free(n);
1164         return 1;
1165 }
1166
1167
1168 static int prompt_info(X509_REQ *req,
1169                 STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
1170                 STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs,
1171                 unsigned long chtype)
1172         {
1173         int i;
1174         char *p,*q;
1175         char buf[100];
1176         int nid;
1177         long n_min,n_max;
1178         char *type,*def,*value;
1179         CONF_VALUE *v;
1180         X509_NAME *subj;
1181         subj = X509_REQ_get_subject_name(req);
1182
1183         if(!batch)
1184                 {
1185                 BIO_printf(bio_err,"You are about to be asked to enter information that will be incorporated\n");
1186                 BIO_printf(bio_err,"into your certificate request.\n");
1187                 BIO_printf(bio_err,"What you are about to enter is what is called a Distinguished Name or a DN.\n");
1188                 BIO_printf(bio_err,"There are quite a few fields but you can leave some blank\n");
1189                 BIO_printf(bio_err,"For some fields there will be a default value,\n");
1190                 BIO_printf(bio_err,"If you enter '.', the field will be left blank.\n");
1191                 BIO_printf(bio_err,"-----\n");
1192                 }
1193
1194
1195         if (sk_CONF_VALUE_num(dn_sk))
1196                 {
1197                 i= -1;
1198 start:          for (;;)
1199                         {
1200                         i++;
1201                         if (sk_CONF_VALUE_num(dn_sk) <= i) break;
1202
1203                         v=sk_CONF_VALUE_value(dn_sk,i);
1204                         p=q=NULL;
1205                         type=v->name;
1206                         if(!check_end(type,"_min") || !check_end(type,"_max") ||
1207                                 !check_end(type,"_default") ||
1208                                          !check_end(type,"_value")) continue;
1209                         /* Skip past any leading X. X: X, etc to allow for
1210                          * multiple instances 
1211                          */
1212                         for(p = v->name; *p ; p++) 
1213                                 if ((*p == ':') || (*p == ',') ||
1214                                                          (*p == '.')) {
1215                                         p++;
1216                                         if(*p) type = p;
1217                                         break;
1218                                 }
1219                         /* If OBJ not recognised ignore it */
1220                         if ((nid=OBJ_txt2nid(type)) == NID_undef) goto start;
1221                         sprintf(buf,"%s_default",v->name);
1222                         if ((def=NCONF_get_string(req_conf,dn_sect,buf)) == NULL)
1223                                 {
1224                                 ERR_clear_error();
1225                                 def="";
1226                                 }
1227                                 
1228                         sprintf(buf,"%s_value",v->name);
1229                         if ((value=NCONF_get_string(req_conf,dn_sect,buf)) == NULL)
1230                                 {
1231                                 ERR_clear_error();
1232                                 value=NULL;
1233                                 }
1234
1235                         sprintf(buf,"%s_min",v->name);
1236                         if (!NCONF_get_number(req_conf,dn_sect,buf, &n_min))
1237                                 n_min = -1;
1238
1239                         sprintf(buf,"%s_max",v->name);
1240                         if (!NCONF_get_number(req_conf,dn_sect,buf, &n_max))
1241                                 n_max = -1;
1242
1243                         if (!add_DN_object(subj,v->value,def,value,nid,
1244                                 n_min,n_max, chtype))
1245                                 return 0;
1246                         }
1247                 if (X509_NAME_entry_count(subj) == 0)
1248                         {
1249                         BIO_printf(bio_err,"error, no objects specified in config file\n");
1250                         return 0;
1251                         }
1252
1253                 if (attribs)
1254                         {
1255                         if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0) && (!batch))
1256                                 {
1257                                 BIO_printf(bio_err,"\nPlease enter the following 'extra' attributes\n");
1258                                 BIO_printf(bio_err,"to be sent with your certificate request\n");
1259                                 }
1260
1261                         i= -1;
1262 start2:                 for (;;)
1263                                 {
1264                                 i++;
1265                                 if ((attr_sk == NULL) ||
1266                                             (sk_CONF_VALUE_num(attr_sk) <= i))
1267                                         break;
1268
1269                                 v=sk_CONF_VALUE_value(attr_sk,i);
1270                                 type=v->name;
1271                                 if ((nid=OBJ_txt2nid(type)) == NID_undef)
1272                                         goto start2;
1273
1274                                 sprintf(buf,"%s_default",type);
1275                                 if ((def=NCONF_get_string(req_conf,attr_sect,buf))
1276                                         == NULL)
1277                                         {
1278                                         ERR_clear_error();
1279                                         def="";
1280                                         }
1281                                 
1282                                 
1283                                 sprintf(buf,"%s_value",type);
1284                                 if ((value=NCONF_get_string(req_conf,attr_sect,buf))
1285                                         == NULL)
1286                                         {
1287                                         ERR_clear_error();
1288                                         value=NULL;
1289                                         }
1290
1291                                 sprintf(buf,"%s_min",type);
1292                                 if (!NCONF_get_number(req_conf,attr_sect,buf, &n_min))
1293                                         n_min = -1;
1294
1295                                 sprintf(buf,"%s_max",type);
1296                                 if (!NCONF_get_number(req_conf,attr_sect,buf, &n_max))
1297                                         n_max = -1;
1298
1299                                 if (!add_attribute_object(req,
1300                                         v->value,def,value,nid,n_min,n_max, chtype))
1301                                         return 0;
1302                                 }
1303                         }
1304                 }
1305         else
1306                 {
1307                 BIO_printf(bio_err,"No template, please set one up.\n");
1308                 return 0;
1309                 }
1310
1311         return 1;
1312
1313         }
1314
1315 static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
1316                         STACK_OF(CONF_VALUE) *attr_sk, int attribs, unsigned long chtype)
1317         {
1318         int i;
1319         char *p,*q;
1320         char *type;
1321         CONF_VALUE *v;
1322         X509_NAME *subj;
1323
1324         subj = X509_REQ_get_subject_name(req);
1325
1326         for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++)
1327                 {
1328                 v=sk_CONF_VALUE_value(dn_sk,i);
1329                 p=q=NULL;
1330                 type=v->name;
1331                 /* Skip past any leading X. X: X, etc to allow for
1332                  * multiple instances 
1333                  */
1334                 for(p = v->name; *p ; p++) 
1335 #ifndef CHARSET_EBCDIC
1336                         if ((*p == ':') || (*p == ',') || (*p == '.')) {
1337 #else
1338                         if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.'])) {
1339 #endif
1340                                 p++;
1341                                 if(*p) type = p;
1342                                 break;
1343                         }
1344                 if (!X509_NAME_add_entry_by_txt(subj,type, chtype,
1345                                 (unsigned char *) v->value,-1,-1,0)) return 0;
1346
1347                 }
1348
1349                 if (!X509_NAME_entry_count(subj))
1350                         {
1351                         BIO_printf(bio_err,"error, no objects specified in config file\n");
1352                         return 0;
1353                         }
1354                 if (attribs)
1355                         {
1356                         for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++)
1357                                 {
1358                                 v=sk_CONF_VALUE_value(attr_sk,i);
1359                                 if(!X509_REQ_add1_attr_by_txt(req, v->name, chtype,
1360                                         (unsigned char *)v->value, -1)) return 0;
1361                                 }
1362                         }
1363         return 1;
1364         }
1365
1366
1367 static int add_DN_object(X509_NAME *n, char *text, char *def, char *value,
1368              int nid, int n_min, int n_max, unsigned long chtype)
1369         {
1370         int i,ret=0;
1371         MS_STATIC char buf[1024];
1372 start:
1373         if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def);
1374         (void)BIO_flush(bio_err);
1375         if(value != NULL)
1376                 {
1377                 strcpy(buf,value);
1378                 strcat(buf,"\n");
1379                 BIO_printf(bio_err,"%s\n",value);
1380                 }
1381         else
1382                 {
1383                 buf[0]='\0';
1384                 if (!batch)
1385                         {
1386                         fgets(buf,1024,stdin);
1387                         }
1388                 else
1389                         {
1390                         buf[0] = '\n';
1391                         buf[1] = '\0';
1392                         }
1393                 }
1394
1395         if (buf[0] == '\0') return(0);
1396         else if (buf[0] == '\n')
1397                 {
1398                 if ((def == NULL) || (def[0] == '\0'))
1399                         return(1);
1400                 strcpy(buf,def);
1401                 strcat(buf,"\n");
1402                 }
1403         else if ((buf[0] == '.') && (buf[1] == '\n')) return(1);
1404
1405         i=strlen(buf);
1406         if (buf[i-1] != '\n')
1407                 {
1408                 BIO_printf(bio_err,"weird input :-(\n");
1409                 return(0);
1410                 }
1411         buf[--i]='\0';
1412 #ifdef CHARSET_EBCDIC
1413         ebcdic2ascii(buf, buf, i);
1414 #endif
1415         if(!req_check_len(i, n_min, n_max)) goto start;
1416         if (!X509_NAME_add_entry_by_NID(n,nid, chtype,
1417                                 (unsigned char *) buf, -1,-1,0)) goto err;
1418         ret=1;
1419 err:
1420         return(ret);
1421         }
1422
1423 static int add_attribute_object(X509_REQ *req, char *text,
1424                                 char *def, char *value, int nid, int n_min,
1425                                 int n_max, unsigned long chtype)
1426         {
1427         int i;
1428         static char buf[1024];
1429
1430 start:
1431         if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def);
1432         (void)BIO_flush(bio_err);
1433         if (value != NULL)
1434                 {
1435                 strcpy(buf,value);
1436                 strcat(buf,"\n");
1437                 BIO_printf(bio_err,"%s\n",value);
1438                 }
1439         else
1440                 {
1441                 buf[0]='\0';
1442                 if (!batch)
1443                         {
1444                         fgets(buf,1024,stdin);
1445                         }
1446                 else
1447                         {
1448                         buf[0] = '\n';
1449                         buf[1] = '\0';
1450                         }
1451                 }
1452
1453         if (buf[0] == '\0') return(0);
1454         else if (buf[0] == '\n')
1455                 {
1456                 if ((def == NULL) || (def[0] == '\0'))
1457                         return(1);
1458                 strcpy(buf,def);
1459                 strcat(buf,"\n");
1460                 }
1461         else if ((buf[0] == '.') && (buf[1] == '\n')) return(1);
1462
1463         i=strlen(buf);
1464         if (buf[i-1] != '\n')
1465                 {
1466                 BIO_printf(bio_err,"weird input :-(\n");
1467                 return(0);
1468                 }
1469         buf[--i]='\0';
1470 #ifdef CHARSET_EBCDIC
1471         ebcdic2ascii(buf, buf, i);
1472 #endif
1473         if(!req_check_len(i, n_min, n_max)) goto start;
1474
1475         if(!X509_REQ_add1_attr_by_NID(req, nid, chtype,
1476                                         (unsigned char *)buf, -1)) {
1477                 BIO_printf(bio_err, "Error adding attribute\n");
1478                 ERR_print_errors(bio_err);
1479                 goto err;
1480         }
1481
1482         return(1);
1483 err:
1484         return(0);
1485         }
1486
1487 #ifndef OPENSSL_NO_RSA
1488 static void MS_CALLBACK req_cb(int p, int n, void *arg)
1489         {
1490         char c='*';
1491
1492         if (p == 0) c='.';
1493         if (p == 1) c='+';
1494         if (p == 2) c='*';
1495         if (p == 3) c='\n';
1496         BIO_write((BIO *)arg,&c,1);
1497         (void)BIO_flush((BIO *)arg);
1498 #ifdef LINT
1499         p=n;
1500 #endif
1501         }
1502 #endif
1503
1504 static int req_check_len(int len, int n_min, int n_max)
1505         {
1506         if ((n_min > 0) && (len < n_min))
1507                 {
1508                 BIO_printf(bio_err,"string is too short, it needs to be at least %d bytes long\n",n_min);
1509                 return(0);
1510                 }
1511         if ((n_max >= 0) && (len > n_max))
1512                 {
1513                 BIO_printf(bio_err,"string is too long, it needs to be less than  %d bytes long\n",n_max);
1514                 return(0);
1515                 }
1516         return(1);
1517         }
1518
1519 /* Check if the end of a string matches 'end' */
1520 static int check_end(char *str, char *end)
1521 {
1522         int elen, slen; 
1523         char *tmp;
1524         elen = strlen(end);
1525         slen = strlen(str);
1526         if(elen > slen) return 1;
1527         tmp = str + slen - elen;
1528         return strcmp(tmp, end);
1529 }