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