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