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