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