]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/req.c
Fix bogus use of BIO_sock_should_retry.
[thirdparty/openssl.git] / apps / req.c
CommitLineData
846e33c7 1/*
2234212c 2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
846e33c7
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
d02b48c6
RE
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <time.h>
13#include <string.h>
d02b48c6 14#include "apps.h"
ec577822
BM
15#include <openssl/bio.h>
16#include <openssl/evp.h>
ec577822
BM
17#include <openssl/conf.h>
18#include <openssl/err.h>
19#include <openssl/asn1.h>
20#include <openssl/x509.h>
21#include <openssl/x509v3.h>
22#include <openssl/objects.h>
23#include <openssl/pem.h>
3eeaab4b
NL
24#include <openssl/bn.h>
25#ifndef OPENSSL_NO_RSA
0f113f3e 26# include <openssl/rsa.h>
3eeaab4b
NL
27#endif
28#ifndef OPENSSL_NO_DSA
0f113f3e 29# include <openssl/dsa.h>
3eeaab4b 30#endif
d02b48c6 31
0f113f3e 32#define SECTION "req"
d02b48c6 33
0f113f3e
MC
34#define BITS "default_bits"
35#define KEYFILE "default_keyfile"
36#define PROMPT "prompt"
37#define DISTINGUISHED_NAME "distinguished_name"
38#define ATTRIBUTES "attributes"
39#define V3_EXTENSIONS "x509_extensions"
40#define REQ_EXTENSIONS "req_extensions"
41#define STRING_MASK "string_mask"
42#define UTF8_IN "utf8"
d02b48c6 43
a7626557
EK
44#define DEFAULT_KEY_LENGTH 2048
45#define MIN_KEY_LENGTH 512
d02b48c6 46
0f113f3e
MC
47static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *dn, int mutlirdn,
48 int attribs, unsigned long chtype);
cc696296 49static int build_subject(X509_REQ *req, const char *subj, unsigned long chtype,
0f113f3e 50 int multirdn);
b38f9f66 51static int prompt_info(X509_REQ *req,
cc696296
F
52 STACK_OF(CONF_VALUE) *dn_sk, const char *dn_sect,
53 STACK_OF(CONF_VALUE) *attr_sk, const char *attr_sect,
0f113f3e 54 int attribs, unsigned long chtype);
b38f9f66 55static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *sk,
0f113f3e
MC
56 STACK_OF(CONF_VALUE) *attr, int attribs,
57 unsigned long chtype);
7d727231 58static int add_attribute_object(X509_REQ *req, char *text, const char *def,
0f113f3e
MC
59 char *value, int nid, int n_min, int n_max,
60 unsigned long chtype);
61static int add_DN_object(X509_NAME *n, char *text, const char *def,
62 char *value, int nid, int n_min, int n_max,
63 unsigned long chtype, int mval);
959e8dfe 64static int genpkey_cb(EVP_PKEY_CTX *ctx);
0f113f3e 65static int req_check_len(int len, int n_min, int n_max);
7d727231 66static int check_end(const char *str, const char *end);
9a0953ed
P
67static int join(char buf[], size_t buf_size, const char *name,
68 const char *tail, const char *desc);
7e1b7485 69static EVP_PKEY_CTX *set_keygen_ctx(const char *gstr,
0f113f3e
MC
70 int *pkey_type, long *pkeylen,
71 char **palgnam, ENGINE *keygen_engine);
0f113f3e
MC
72static CONF *req_conf = NULL;
73static int batch = 0;
d02b48c6 74
7e1b7485
RS
75typedef enum OPTION_choice {
76 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
77 OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_KEYGEN_ENGINE, OPT_KEY,
78 OPT_PUBKEY, OPT_NEW, OPT_CONFIG, OPT_KEYFORM, OPT_IN, OPT_OUT,
3ee1eac2 79 OPT_KEYOUT, OPT_PASSIN, OPT_PASSOUT, OPT_NEWKEY,
7e1b7485
RS
80 OPT_PKEYOPT, OPT_SIGOPT, OPT_BATCH, OPT_NEWHDR, OPT_MODULUS,
81 OPT_VERIFY, OPT_NODES, OPT_NOOUT, OPT_VERBOSE, OPT_UTF8,
29eca1c0 82 OPT_NAMEOPT, OPT_REQOPT, OPT_SUBJ, OPT_SUBJECT, OPT_TEXT, OPT_X509,
d8c054f2 83 OPT_MULTIVALUE_RDN, OPT_DAYS, OPT_SET_SERIAL, OPT_EXTENSIONS,
3ee1eac2
RS
84 OPT_REQEXTS, OPT_PRECERT, OPT_MD,
85 OPT_R_ENUM
7e1b7485
RS
86} OPTION_CHOICE;
87
44c83ebd 88const OPTIONS req_options[] = {
7e1b7485
RS
89 {"help", OPT_HELP, '-', "Display this summary"},
90 {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
91 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
92 {"in", OPT_IN, '<', "Input file"},
93 {"out", OPT_OUT, '>', "Output file"},
43db7aa2
DSH
94 {"key", OPT_KEY, 's', "Private key to use"},
95 {"keyform", OPT_KEYFORM, 'f', "Key file format"},
7e1b7485
RS
96 {"pubkey", OPT_PUBKEY, '-', "Output public key"},
97 {"new", OPT_NEW, '-', "New request"},
98 {"config", OPT_CONFIG, '<', "Request template file"},
99 {"keyout", OPT_KEYOUT, '>', "File to send the key to"},
100 {"passin", OPT_PASSIN, 's', "Private key password source"},
9a13bb38 101 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
3ee1eac2 102 OPT_R_OPTIONS,
7e1b7485 103 {"newkey", OPT_NEWKEY, 's', "Specify as type:bits"},
9a13bb38
RS
104 {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"},
105 {"sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form"},
7e1b7485
RS
106 {"batch", OPT_BATCH, '-',
107 "Do not ask anything during request generation"},
108 {"newhdr", OPT_NEWHDR, '-', "Output \"NEW\" in the header lines"},
109 {"modulus", OPT_MODULUS, '-', "RSA modulus"},
110 {"verify", OPT_VERIFY, '-', "Verify signature on REQ"},
111 {"nodes", OPT_NODES, '-', "Don't encrypt the output key"},
112 {"noout", OPT_NOOUT, '-', "Do not output REQ"},
9a13bb38 113 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
7e1b7485
RS
114 {"utf8", OPT_UTF8, '-', "Input characters are UTF8 (default ASCII)"},
115 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
116 {"reqopt", OPT_REQOPT, 's', "Various request text options"},
117 {"text", OPT_TEXT, '-', "Text form of request"},
118 {"x509", OPT_X509, '-',
119 "Output a x509 structure instead of a cert request"},
7e1b7485 120 {OPT_MORE_STR, 1, 1, "(Required by some CA's)"},
29eca1c0
TH
121 {"subj", OPT_SUBJ, 's', "Set or modify request subject"},
122 {"subject", OPT_SUBJECT, '-', "Output the request's subject"},
7e1b7485
RS
123 {"multivalue-rdn", OPT_MULTIVALUE_RDN, '-',
124 "Enable support for multivalued RDNs"},
125 {"days", OPT_DAYS, 'p', "Number of days cert is valid for"},
be4c82aa 126 {"set_serial", OPT_SET_SERIAL, 's', "Serial number to use"},
7e1b7485
RS
127 {"extensions", OPT_EXTENSIONS, 's',
128 "Cert extension section (override value in config file)"},
129 {"reqexts", OPT_REQEXTS, 's',
130 "Request extension section (override value in config file)"},
65b3dff7 131 {"precert", OPT_PRECERT, '-', "Add a poison extension (implies -new)"},
9c3bcfa0 132 {"", OPT_MD, '-', "Any supported digest"},
7e1b7485
RS
133#ifndef OPENSSL_NO_ENGINE
134 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
9a13bb38
RS
135 {"keygen_engine", OPT_KEYGEN_ENGINE, 's',
136 "Specify engine to be used for key generation operations"},
7e1b7485 137#endif
7e1b7485
RS
138 {NULL}
139};
667ac4ec 140
7e1b7485 141int req_main(int argc, char **argv)
0f113f3e 142{
7e1b7485
RS
143 ASN1_INTEGER *serial = NULL;
144 BIO *in = NULL, *out = NULL;
0f113f3e 145 ENGINE *e = NULL, *gen_eng = NULL;
7e1b7485 146 EVP_PKEY *pkey = NULL;
0f113f3e 147 EVP_PKEY_CTX *genctx = NULL;
0f113f3e 148 STACK_OF(OPENSSL_STRING) *pkeyopts = NULL, *sigopts = NULL;
7e1b7485
RS
149 X509 *x509ss = NULL;
150 X509_REQ *req = NULL;
0f113f3e 151 const EVP_CIPHER *cipher = NULL;
0f113f3e 152 const EVP_MD *md_alg = NULL, *digest = NULL;
333b070e 153 char *extensions = NULL, *infile = NULL;
3ee1eac2 154 char *outfile = NULL, *keyfile = NULL;
7e1b7485 155 char *keyalgstr = NULL, *p, *prog, *passargin = NULL, *passargout = NULL;
ebc4815f
VD
156 char *passin = NULL, *passout = NULL;
157 char *nofree_passin = NULL, *nofree_passout = NULL;
158 char *req_exts = NULL, *subj = NULL;
cc01d217 159 char *template = default_config_file, *keyout = NULL;
7e1b7485
RS
160 const char *keyalg = NULL;
161 OPTION_CHOICE o;
3b061a00
RS
162 int ret = 1, x509 = 0, days = 30, i = 0, newreq = 0, verbose = 0;
163 int pkey_type = -1, private = 0;
7e1b7485
RS
164 int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyform = FORMAT_PEM;
165 int modulus = 0, multirdn = 0, verify = 0, noout = 0, text = 0;
65b3dff7 166 int nodes = 0, newhdr = 0, subject = 0, pubkey = 0, precert = 0;
7e1b7485 167 long newkey = -1;
b5c4209b 168 unsigned long chtype = MBSTRING_ASC, reqflag = 0;
d02b48c6 169
cf1b7d96 170#ifndef OPENSSL_NO_DES
0f113f3e 171 cipher = EVP_des_ede3_cbc();
d02b48c6 172#endif
7e1b7485
RS
173
174 prog = opt_init(argc, argv, req_options);
175 while ((o = opt_next()) != OPT_EOF) {
176 switch (o) {
177 case OPT_EOF:
178 case OPT_ERR:
179 opthelp:
180 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
181 goto end;
182 case OPT_HELP:
183 opt_help(req_options);
184 ret = 0;
185 goto end;
186 case OPT_INFORM:
187 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
188 goto opthelp;
189 break;
190 case OPT_OUTFORM:
191 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
192 goto opthelp;
193 break;
7e1b7485 194 case OPT_ENGINE:
43db7aa2 195 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
196 break;
197 case OPT_KEYGEN_ENGINE:
333b070e 198#ifndef OPENSSL_NO_ENGINE
7e1b7485 199 gen_eng = ENGINE_by_id(opt_arg());
0f113f3e
MC
200 if (gen_eng == NULL) {
201 BIO_printf(bio_err, "Can't find keygen engine %s\n", *argv);
333b070e 202 goto opthelp;
0f113f3e 203 }
0b13e9f0 204#endif
333b070e 205 break;
7e1b7485
RS
206 case OPT_KEY:
207 keyfile = opt_arg();
208 break;
209 case OPT_PUBKEY:
0f113f3e 210 pubkey = 1;
7e1b7485
RS
211 break;
212 case OPT_NEW:
0f113f3e 213 newreq = 1;
7e1b7485
RS
214 break;
215 case OPT_CONFIG:
216 template = opt_arg();
217 break;
218 case OPT_KEYFORM:
43db7aa2 219 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
7e1b7485
RS
220 goto opthelp;
221 break;
222 case OPT_IN:
223 infile = opt_arg();
224 break;
225 case OPT_OUT:
226 outfile = opt_arg();
227 break;
228 case OPT_KEYOUT:
229 keyout = opt_arg();
230 break;
231 case OPT_PASSIN:
232 passargin = opt_arg();
233 break;
234 case OPT_PASSOUT:
235 passargout = opt_arg();
236 break;
3ee1eac2
RS
237 case OPT_R_CASES:
238 if (!opt_rand(o))
239 goto end;
7e1b7485
RS
240 break;
241 case OPT_NEWKEY:
242 keyalg = opt_arg();
0f113f3e 243 newreq = 1;
7e1b7485
RS
244 break;
245 case OPT_PKEYOPT:
0f113f3e
MC
246 if (!pkeyopts)
247 pkeyopts = sk_OPENSSL_STRING_new_null();
7e1b7485
RS
248 if (!pkeyopts || !sk_OPENSSL_STRING_push(pkeyopts, opt_arg()))
249 goto opthelp;
250 break;
251 case OPT_SIGOPT:
0f113f3e
MC
252 if (!sigopts)
253 sigopts = sk_OPENSSL_STRING_new_null();
7e1b7485
RS
254 if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
255 goto opthelp;
256 break;
257 case OPT_BATCH:
0f113f3e 258 batch = 1;
7e1b7485
RS
259 break;
260 case OPT_NEWHDR:
0f113f3e 261 newhdr = 1;
7e1b7485
RS
262 break;
263 case OPT_MODULUS:
0f113f3e 264 modulus = 1;
7e1b7485
RS
265 break;
266 case OPT_VERIFY:
0f113f3e 267 verify = 1;
7e1b7485
RS
268 break;
269 case OPT_NODES:
0f113f3e 270 nodes = 1;
7e1b7485
RS
271 break;
272 case OPT_NOOUT:
0f113f3e 273 noout = 1;
7e1b7485
RS
274 break;
275 case OPT_VERBOSE:
0f113f3e 276 verbose = 1;
7e1b7485
RS
277 break;
278 case OPT_UTF8:
0f113f3e 279 chtype = MBSTRING_UTF8;
7e1b7485
RS
280 break;
281 case OPT_NAMEOPT:
b5c4209b 282 if (!set_nameopt(opt_arg()))
7e1b7485
RS
283 goto opthelp;
284 break;
285 case OPT_REQOPT:
286 if (!set_cert_ex(&reqflag, opt_arg()))
287 goto opthelp;
288 break;
289 case OPT_TEXT:
0f113f3e 290 text = 1;
7e1b7485
RS
291 break;
292 case OPT_X509:
0f113f3e 293 x509 = 1;
7e1b7485 294 break;
7e1b7485
RS
295 case OPT_DAYS:
296 days = atoi(opt_arg());
297 break;
298 case OPT_SET_SERIAL:
08f6ae5b 299 if (serial != NULL) {
efba7787 300 BIO_printf(bio_err, "Serial number supplied twice\n");
08f6ae5b
MC
301 goto opthelp;
302 }
7e1b7485
RS
303 serial = s2i_ASN1_INTEGER(NULL, opt_arg());
304 if (serial == NULL)
305 goto opthelp;
306 break;
307 case OPT_SUBJECT:
f1cece55 308 subject = 1;
29eca1c0
TH
309 break;
310 case OPT_SUBJ:
7e1b7485
RS
311 subj = opt_arg();
312 break;
313 case OPT_MULTIVALUE_RDN:
0f113f3e 314 multirdn = 1;
7e1b7485
RS
315 break;
316 case OPT_EXTENSIONS:
317 extensions = opt_arg();
318 break;
319 case OPT_REQEXTS:
320 req_exts = opt_arg();
321 break;
b6486bf7 322 case OPT_PRECERT:
65b3dff7 323 newreq = precert = 1;
b6486bf7 324 break;
7e1b7485
RS
325 case OPT_MD:
326 if (!opt_md(opt_unknown(), &md_alg))
327 goto opthelp;
0f113f3e 328 digest = md_alg;
0f113f3e
MC
329 break;
330 }
0f113f3e 331 }
7e1b7485 332 argc = opt_num_rest();
03358517
KR
333 if (argc != 0)
334 goto opthelp;
f1cece55 335
888adbe0
TM
336 if (x509 && infile == NULL)
337 newreq = 1;
338
6b4a77f5 339 /* TODO: simplify this as pkey is still always NULL here */
3b061a00 340 private = newreq && (pkey == NULL) ? 1 : 0;
0f113f3e 341
7e1b7485 342 if (!app_passwd(passargin, passargout, &passin, &passout)) {
0f113f3e
MC
343 BIO_printf(bio_err, "Error getting passwords\n");
344 goto end;
345 }
d02b48c6 346
cc01d217
RS
347 if (verbose)
348 BIO_printf(bio_err, "Using configuration from %s\n", template);
349 req_conf = app_load_config(template);
c821defc 350 if (template != default_config_file && !app_load_modules(req_conf))
296f54ee
RL
351 goto end;
352
0f113f3e 353 if (req_conf != NULL) {
0f113f3e
MC
354 p = NCONF_get_string(req_conf, NULL, "oid_file");
355 if (p == NULL)
356 ERR_clear_error();
357 if (p != NULL) {
358 BIO *oid_bio;
359
360 oid_bio = BIO_new_file(p, "r");
361 if (oid_bio == NULL) {
50e735f9
MC
362 /*-
363 BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
364 ERR_print_errors(bio_err);
365 */
0f113f3e
MC
366 } else {
367 OBJ_create_objects(oid_bio);
368 BIO_free(oid_bio);
369 }
370 }
371 }
7e1b7485 372 if (!add_oid_section(req_conf))
0f113f3e
MC
373 goto end;
374
375 if (md_alg == NULL) {
376 p = NCONF_get_string(req_conf, SECTION, "default_md");
2234212c 377 if (p == NULL) {
0f113f3e 378 ERR_clear_error();
2234212c 379 } else {
7e1b7485
RS
380 if (!opt_md(p, &md_alg))
381 goto opthelp;
382 digest = md_alg;
0f113f3e
MC
383 }
384 }
385
2234212c 386 if (extensions == NULL) {
0f113f3e 387 extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
2234212c 388 if (extensions == NULL)
0f113f3e
MC
389 ERR_clear_error();
390 }
2234212c 391 if (extensions != NULL) {
0f113f3e
MC
392 /* Check syntax of file */
393 X509V3_CTX ctx;
394 X509V3_set_ctx_test(&ctx);
395 X509V3_set_nconf(&ctx, req_conf);
396 if (!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) {
397 BIO_printf(bio_err,
398 "Error Loading extension section %s\n", extensions);
399 goto end;
400 }
401 }
402
ebc4815f
VD
403 if (passin == NULL) {
404 passin = nofree_passin =
405 NCONF_get_string(req_conf, SECTION, "input_password");
406 if (passin == NULL)
0f113f3e
MC
407 ERR_clear_error();
408 }
409
ebc4815f
VD
410 if (passout == NULL) {
411 passout = nofree_passout =
412 NCONF_get_string(req_conf, SECTION, "output_password");
413 if (passout == NULL)
0f113f3e
MC
414 ERR_clear_error();
415 }
416
417 p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
2234212c 418 if (p == NULL)
0f113f3e
MC
419 ERR_clear_error();
420
2234212c 421 if (p != NULL && !ASN1_STRING_set_default_mask_asc(p)) {
0f113f3e
MC
422 BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
423 goto end;
424 }
425
426 if (chtype != MBSTRING_UTF8) {
427 p = NCONF_get_string(req_conf, SECTION, UTF8_IN);
2234212c 428 if (p == NULL)
0f113f3e 429 ERR_clear_error();
86885c28 430 else if (strcmp(p, "yes") == 0)
0f113f3e
MC
431 chtype = MBSTRING_UTF8;
432 }
433
2234212c 434 if (req_exts == NULL) {
0f113f3e 435 req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
2234212c 436 if (req_exts == NULL)
0f113f3e
MC
437 ERR_clear_error();
438 }
2234212c 439 if (req_exts != NULL) {
0f113f3e
MC
440 /* Check syntax of file */
441 X509V3_CTX ctx;
442 X509V3_set_ctx_test(&ctx);
443 X509V3_set_nconf(&ctx, req_conf);
444 if (!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) {
445 BIO_printf(bio_err,
446 "Error Loading request extension section %s\n",
447 req_exts);
448 goto end;
449 }
450 }
d02b48c6 451
0f113f3e 452 if (keyfile != NULL) {
7e1b7485 453 pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
2234212c 454 if (pkey == NULL) {
7e1b7485 455 /* load_key() has already printed an appropriate message */
0f113f3e
MC
456 goto end;
457 } else {
3ee1eac2 458 app_RAND_load_conf(req_conf, SECTION);
0f113f3e
MC
459 }
460 }
461
462 if (newreq && (pkey == NULL)) {
3ee1eac2 463 app_RAND_load_conf(req_conf, SECTION);
0f113f3e
MC
464
465 if (!NCONF_get_number(req_conf, SECTION, BITS, &newkey)) {
466 newkey = DEFAULT_KEY_LENGTH;
467 }
468
2234212c 469 if (keyalg != NULL) {
7e1b7485 470 genctx = set_keygen_ctx(keyalg, &pkey_type, &newkey,
0f113f3e 471 &keyalgstr, gen_eng);
2234212c 472 if (genctx == NULL)
0f113f3e
MC
473 goto end;
474 }
475
476 if (newkey < MIN_KEY_LENGTH
477 && (pkey_type == EVP_PKEY_RSA || pkey_type == EVP_PKEY_DSA)) {
478 BIO_printf(bio_err, "private key length is too short,\n");
479 BIO_printf(bio_err, "it needs to be at least %d bits, not %ld\n",
480 MIN_KEY_LENGTH, newkey);
481 goto end;
482 }
483
2234212c 484 if (genctx == NULL) {
7e1b7485 485 genctx = set_keygen_ctx(NULL, &pkey_type, &newkey,
0f113f3e
MC
486 &keyalgstr, gen_eng);
487 if (!genctx)
488 goto end;
489 }
490
2234212c 491 if (pkeyopts != NULL) {
0f113f3e
MC
492 char *genopt;
493 for (i = 0; i < sk_OPENSSL_STRING_num(pkeyopts); i++) {
494 genopt = sk_OPENSSL_STRING_value(pkeyopts, i);
495 if (pkey_ctrl_string(genctx, genopt) <= 0) {
496 BIO_printf(bio_err, "parameter error \"%s\"\n", genopt);
497 ERR_print_errors(bio_err);
498 goto end;
499 }
500 }
501 }
502
57358a83
MC
503 if (pkey_type == EVP_PKEY_EC) {
504 BIO_printf(bio_err, "Generating an EC private key\n");
505 } else {
506 BIO_printf(bio_err, "Generating a %ld bit %s private key\n",
507 newkey, keyalgstr);
508 }
0f113f3e
MC
509
510 EVP_PKEY_CTX_set_cb(genctx, genpkey_cb);
511 EVP_PKEY_CTX_set_app_data(genctx, bio_err);
512
513 if (EVP_PKEY_keygen(genctx, &pkey) <= 0) {
514 BIO_puts(bio_err, "Error Generating Key\n");
515 goto end;
516 }
517
518 EVP_PKEY_CTX_free(genctx);
519 genctx = NULL;
520
0f113f3e
MC
521 if (keyout == NULL) {
522 keyout = NCONF_get_string(req_conf, SECTION, KEYFILE);
523 if (keyout == NULL)
524 ERR_clear_error();
525 }
526
7e1b7485 527 if (keyout == NULL)
0f113f3e 528 BIO_printf(bio_err, "writing new private key to stdout\n");
7e1b7485 529 else
0f113f3e 530 BIO_printf(bio_err, "writing new private key to '%s'\n", keyout);
bdd58d98 531 out = bio_open_owner(keyout, outformat, private);
7e1b7485
RS
532 if (out == NULL)
533 goto end;
0f113f3e
MC
534
535 p = NCONF_get_string(req_conf, SECTION, "encrypt_rsa_key");
536 if (p == NULL) {
537 ERR_clear_error();
538 p = NCONF_get_string(req_conf, SECTION, "encrypt_key");
539 if (p == NULL)
540 ERR_clear_error();
541 }
542 if ((p != NULL) && (strcmp(p, "no") == 0))
543 cipher = NULL;
544 if (nodes)
545 cipher = NULL;
546
547 i = 0;
548 loop:
3b061a00 549 assert(private);
0f113f3e
MC
550 if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
551 NULL, 0, NULL, passout)) {
552 if ((ERR_GET_REASON(ERR_peek_error()) ==
553 PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) {
554 ERR_clear_error();
555 i++;
556 goto loop;
557 }
558 goto end;
559 }
cf89a80e 560 BIO_free(out);
21425195 561 out = NULL;
0f113f3e
MC
562 BIO_printf(bio_err, "-----\n");
563 }
564
565 if (!newreq) {
bdd58d98 566 in = bio_open_default(infile, 'r', informat);
7e1b7485
RS
567 if (in == NULL)
568 goto end;
0f113f3e
MC
569
570 if (informat == FORMAT_ASN1)
571 req = d2i_X509_REQ_bio(in, NULL);
7e1b7485 572 else
0f113f3e 573 req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
0f113f3e
MC
574 if (req == NULL) {
575 BIO_printf(bio_err, "unable to load X509 request\n");
576 goto end;
577 }
578 }
579
888adbe0 580 if (newreq || x509) {
0f113f3e
MC
581 if (pkey == NULL) {
582 BIO_printf(bio_err, "you need to specify a private key\n");
583 goto end;
584 }
585
586 if (req == NULL) {
587 req = X509_REQ_new();
588 if (req == NULL) {
589 goto end;
590 }
591
592 i = make_REQ(req, pkey, subj, multirdn, !x509, chtype);
593 subj = NULL; /* done processing '-subj' option */
0f113f3e
MC
594 if (!i) {
595 BIO_printf(bio_err, "problems making Certificate Request\n");
596 goto end;
597 }
598 }
599 if (x509) {
600 EVP_PKEY *tmppkey;
601 X509V3_CTX ext_ctx;
602 if ((x509ss = X509_new()) == NULL)
603 goto end;
604
605 /* Set version to V3 */
2234212c 606 if (extensions != NULL && !X509_set_version(x509ss, 2))
0f113f3e 607 goto end;
2234212c 608 if (serial != NULL) {
0f113f3e
MC
609 if (!X509_set_serialNumber(x509ss, serial))
610 goto end;
611 } else {
612 if (!rand_serial(NULL, X509_get_serialNumber(x509ss)))
613 goto end;
614 }
615
616 if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req)))
617 goto end;
dc047d31 618 if (!set_cert_times(x509ss, NULL, NULL, days))
0f113f3e
MC
619 goto end;
620 if (!X509_set_subject_name
621 (x509ss, X509_REQ_get_subject_name(req)))
622 goto end;
c5137473 623 tmppkey = X509_REQ_get0_pubkey(req);
0f113f3e
MC
624 if (!tmppkey || !X509_set_pubkey(x509ss, tmppkey))
625 goto end;
0f113f3e
MC
626
627 /* Set up V3 context struct */
628
629 X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
630 X509V3_set_nconf(&ext_ctx, req_conf);
631
632 /* Add extensions */
2234212c
PY
633 if (extensions != NULL && !X509V3_EXT_add_nconf(req_conf,
634 &ext_ctx, extensions,
635 x509ss)) {
0f113f3e
MC
636 BIO_printf(bio_err, "Error Loading extension section %s\n",
637 extensions);
638 goto end;
639 }
640
b6486bf7
RP
641 /* If a pre-cert was requested, we need to add a poison extension */
642 if (precert) {
643 if (X509_add1_ext_i2d(x509ss, NID_ct_precert_poison, NULL, 1, 0)
644 != 1) {
645 BIO_printf(bio_err, "Error adding poison extension\n");
646 goto end;
647 }
648 }
649
7e1b7485 650 i = do_X509_sign(x509ss, pkey, digest, sigopts);
0f113f3e
MC
651 if (!i) {
652 ERR_print_errors(bio_err);
653 goto end;
654 }
655 } else {
656 X509V3_CTX ext_ctx;
657
658 /* Set up V3 context struct */
659
660 X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
661 X509V3_set_nconf(&ext_ctx, req_conf);
662
663 /* Add extensions */
2234212c
PY
664 if (req_exts != NULL
665 && !X509V3_EXT_REQ_add_nconf(req_conf, &ext_ctx,
666 req_exts, req)) {
0f113f3e
MC
667 BIO_printf(bio_err, "Error Loading extension section %s\n",
668 req_exts);
669 goto end;
670 }
7e1b7485 671 i = do_X509_REQ_sign(req, pkey, digest, sigopts);
0f113f3e
MC
672 if (!i) {
673 ERR_print_errors(bio_err);
674 goto end;
675 }
676 }
677 }
678
679 if (subj && x509) {
680 BIO_printf(bio_err, "Cannot modify certificate subject\n");
681 goto end;
682 }
683
684 if (subj && !x509) {
685 if (verbose) {
686 BIO_printf(bio_err, "Modifying Request's Subject\n");
687 print_name(bio_err, "old subject=",
b5c4209b 688 X509_REQ_get_subject_name(req), get_nameopt());
0f113f3e
MC
689 }
690
691 if (build_subject(req, subj, chtype, multirdn) == 0) {
692 BIO_printf(bio_err, "ERROR: cannot modify subject\n");
7e1b7485 693 ret = 1;
0f113f3e
MC
694 goto end;
695 }
696
0f113f3e
MC
697 if (verbose) {
698 print_name(bio_err, "new subject=",
b5c4209b 699 X509_REQ_get_subject_name(req), get_nameopt());
0f113f3e
MC
700 }
701 }
702
703 if (verify && !x509) {
173f613b 704 EVP_PKEY *tpubkey = pkey;
0f113f3e 705
173f613b
F
706 if (tpubkey == NULL) {
707 tpubkey = X509_REQ_get0_pubkey(req);
708 if (tpubkey == NULL)
0f113f3e
MC
709 goto end;
710 }
711
173f613b 712 i = X509_REQ_verify(req, tpubkey);
0f113f3e
MC
713
714 if (i < 0) {
715 goto end;
716 } else if (i == 0) {
717 BIO_printf(bio_err, "verify failure\n");
718 ERR_print_errors(bio_err);
2234212c 719 } else { /* if (i > 0) */
0f113f3e 720 BIO_printf(bio_err, "verify OK\n");
2234212c 721 }
0f113f3e
MC
722 }
723
724 if (noout && !text && !modulus && !subject && !pubkey) {
7e1b7485 725 ret = 0;
0f113f3e
MC
726 goto end;
727 }
728
7e1b7485
RS
729 out = bio_open_default(outfile,
730 keyout != NULL && outfile != NULL &&
bdd58d98
RL
731 strcmp(keyout, outfile) == 0 ? 'a' : 'w',
732 outformat);
7e1b7485
RS
733 if (out == NULL)
734 goto end;
0f113f3e
MC
735
736 if (pubkey) {
1c72f70d
F
737 EVP_PKEY *tpubkey = X509_REQ_get0_pubkey(req);
738
0f113f3e
MC
739 if (tpubkey == NULL) {
740 BIO_printf(bio_err, "Error getting public key\n");
741 ERR_print_errors(bio_err);
742 goto end;
743 }
744 PEM_write_bio_PUBKEY(out, tpubkey);
0f113f3e
MC
745 }
746
747 if (text) {
748 if (x509)
b5c4209b 749 X509_print_ex(out, x509ss, get_nameopt(), reqflag);
0f113f3e 750 else
b5c4209b 751 X509_REQ_print_ex(out, req, get_nameopt(), reqflag);
0f113f3e
MC
752 }
753
754 if (subject) {
755 if (x509)
756 print_name(out, "subject=", X509_get_subject_name(x509ss),
b5c4209b 757 get_nameopt());
0f113f3e
MC
758 else
759 print_name(out, "subject=", X509_REQ_get_subject_name(req),
b5c4209b 760 get_nameopt());
0f113f3e
MC
761 }
762
763 if (modulus) {
764 EVP_PKEY *tpubkey;
765
766 if (x509)
1c72f70d 767 tpubkey = X509_get0_pubkey(x509ss);
0f113f3e 768 else
1c72f70d 769 tpubkey = X509_REQ_get0_pubkey(req);
0f113f3e
MC
770 if (tpubkey == NULL) {
771 fprintf(stdout, "Modulus=unavailable\n");
772 goto end;
773 }
774 fprintf(stdout, "Modulus=");
cf1b7d96 775#ifndef OPENSSL_NO_RSA
9862e9aa 776 if (EVP_PKEY_base_id(tpubkey) == EVP_PKEY_RSA) {
2ac6115d 777 const BIGNUM *n;
9862e9aa
RL
778 RSA_get0_key(EVP_PKEY_get0_RSA(tpubkey), &n, NULL, NULL);
779 BN_print(out, n);
780 } else
13e91dd3 781#endif
0f113f3e 782 fprintf(stdout, "Wrong Algorithm type");
0f113f3e
MC
783 fprintf(stdout, "\n");
784 }
785
786 if (!noout && !x509) {
787 if (outformat == FORMAT_ASN1)
788 i = i2d_X509_REQ_bio(out, req);
7e1b7485
RS
789 else if (newhdr)
790 i = PEM_write_bio_X509_REQ_NEW(out, req);
791 else
792 i = PEM_write_bio_X509_REQ(out, req);
0f113f3e
MC
793 if (!i) {
794 BIO_printf(bio_err, "unable to write X509 request\n");
795 goto end;
796 }
797 }
798 if (!noout && x509 && (x509ss != NULL)) {
799 if (outformat == FORMAT_ASN1)
800 i = i2d_X509_bio(out, x509ss);
7e1b7485 801 else
0f113f3e 802 i = PEM_write_bio_X509(out, x509ss);
0f113f3e
MC
803 if (!i) {
804 BIO_printf(bio_err, "unable to write X509 certificate\n");
805 goto end;
806 }
807 }
7e1b7485 808 ret = 0;
0f113f3e 809 end:
7e1b7485 810 if (ret) {
0f113f3e
MC
811 ERR_print_errors(bio_err);
812 }
cc01d217 813 NCONF_free(req_conf);
0f113f3e
MC
814 BIO_free(in);
815 BIO_free_all(out);
816 EVP_PKEY_free(pkey);
c5ba2d99 817 EVP_PKEY_CTX_free(genctx);
25aaa98a
RS
818 sk_OPENSSL_STRING_free(pkeyopts);
819 sk_OPENSSL_STRING_free(sigopts);
01b8b3c7 820#ifndef OPENSSL_NO_ENGINE
efa7dd64 821 ENGINE_free(gen_eng);
01b8b3c7 822#endif
b548a1f1 823 OPENSSL_free(keyalgstr);
0f113f3e
MC
824 X509_REQ_free(req);
825 X509_free(x509ss);
826 ASN1_INTEGER_free(serial);
dd1abd44 827 release_engine(e);
ebc4815f
VD
828 if (passin != nofree_passin)
829 OPENSSL_free(passin);
830 if (passout != nofree_passout)
831 OPENSSL_free(passout);
9a0953ed 832 return ret;
0f113f3e 833}
d02b48c6 834
7ce9e425 835static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int multirdn,
0f113f3e
MC
836 int attribs, unsigned long chtype)
837{
838 int ret = 0, i;
839 char no_prompt = 0;
840 STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL;
841 char *tmp, *dn_sect, *attr_sect;
842
843 tmp = NCONF_get_string(req_conf, SECTION, PROMPT);
844 if (tmp == NULL)
845 ERR_clear_error();
86885c28 846 if ((tmp != NULL) && strcmp(tmp, "no") == 0)
0f113f3e
MC
847 no_prompt = 1;
848
849 dn_sect = NCONF_get_string(req_conf, SECTION, DISTINGUISHED_NAME);
850 if (dn_sect == NULL) {
851 BIO_printf(bio_err, "unable to find '%s' in config\n",
852 DISTINGUISHED_NAME);
853 goto err;
854 }
855 dn_sk = NCONF_get_section(req_conf, dn_sect);
856 if (dn_sk == NULL) {
857 BIO_printf(bio_err, "unable to get '%s' section\n", dn_sect);
858 goto err;
859 }
860
861 attr_sect = NCONF_get_string(req_conf, SECTION, ATTRIBUTES);
862 if (attr_sect == NULL) {
863 ERR_clear_error();
864 attr_sk = NULL;
865 } else {
866 attr_sk = NCONF_get_section(req_conf, attr_sect);
867 if (attr_sk == NULL) {
868 BIO_printf(bio_err, "unable to get '%s' section\n", attr_sect);
869 goto err;
870 }
871 }
872
873 /* setup version number */
874 if (!X509_REQ_set_version(req, 0L))
875 goto err; /* version 1 */
876
877 if (subj)
878 i = build_subject(req, subj, chtype, multirdn);
879 else if (no_prompt)
880 i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
881 else
882 i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs,
883 chtype);
884 if (!i)
885 goto err;
886
887 if (!X509_REQ_set_pubkey(req, pkey))
888 goto err;
889
890 ret = 1;
891 err:
9a0953ed 892 return ret;
0f113f3e 893}
d02b48c6 894
c0455cbb
LJ
895/*
896 * subject is expected to be in the format /type0=value0/type1=value1/type2=...
897 * where characters may be escaped by \
898 */
cc696296 899static int build_subject(X509_REQ *req, const char *subject, unsigned long chtype,
0f113f3e
MC
900 int multirdn)
901{
902 X509_NAME *n;
903
75ebbd9a 904 if ((n = parse_name(subject, chtype, multirdn)) == NULL)
0f113f3e 905 return 0;
bad40585 906
0f113f3e
MC
907 if (!X509_REQ_set_subject_name(req, n)) {
908 X509_NAME_free(n);
909 return 0;
910 }
911 X509_NAME_free(n);
912 return 1;
913}
b38f9f66
DSH
914
915static int prompt_info(X509_REQ *req,
cc696296
F
916 STACK_OF(CONF_VALUE) *dn_sk, const char *dn_sect,
917 STACK_OF(CONF_VALUE) *attr_sk, const char *attr_sect,
0f113f3e
MC
918 int attribs, unsigned long chtype)
919{
920 int i;
921 char *p, *q;
922 char buf[100];
923 int nid, mval;
924 long n_min, n_max;
925 char *type, *value;
926 const char *def;
927 CONF_VALUE *v;
928 X509_NAME *subj;
929 subj = X509_REQ_get_subject_name(req);
930
931 if (!batch) {
932 BIO_printf(bio_err,
933 "You are about to be asked to enter information that will be incorporated\n");
934 BIO_printf(bio_err, "into your certificate request.\n");
935 BIO_printf(bio_err,
936 "What you are about to enter is what is called a Distinguished Name or a DN.\n");
937 BIO_printf(bio_err,
938 "There are quite a few fields but you can leave some blank\n");
939 BIO_printf(bio_err,
940 "For some fields there will be a default value,\n");
941 BIO_printf(bio_err,
942 "If you enter '.', the field will be left blank.\n");
943 BIO_printf(bio_err, "-----\n");
944 }
945
946 if (sk_CONF_VALUE_num(dn_sk)) {
947 i = -1;
2234212c
PY
948 start:
949 for ( ; ; ) {
0f113f3e
MC
950 i++;
951 if (sk_CONF_VALUE_num(dn_sk) <= i)
952 break;
953
954 v = sk_CONF_VALUE_value(dn_sk, i);
955 p = q = NULL;
956 type = v->name;
957 if (!check_end(type, "_min") || !check_end(type, "_max") ||
958 !check_end(type, "_default") || !check_end(type, "_value"))
959 continue;
960 /*
961 * Skip past any leading X. X: X, etc to allow for multiple
962 * instances
963 */
964 for (p = v->name; *p; p++)
965 if ((*p == ':') || (*p == ',') || (*p == '.')) {
966 p++;
967 if (*p)
968 type = p;
969 break;
970 }
971 if (*type == '+') {
972 mval = -1;
973 type++;
2234212c 974 } else {
0f113f3e 975 mval = 0;
2234212c 976 }
0f113f3e
MC
977 /* If OBJ not recognised ignore it */
978 if ((nid = OBJ_txt2nid(type)) == NID_undef)
979 goto start;
9a0953ed 980 if (!join(buf, sizeof(buf), v->name, "_default", "Name"))
0f113f3e 981 return 0;
0f113f3e
MC
982 if ((def = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
983 ERR_clear_error();
984 def = "";
985 }
986
9a0953ed
P
987 if (!join(buf, sizeof(buf), v->name, "_value", "Name"))
988 return 0;
0f113f3e
MC
989 if ((value = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
990 ERR_clear_error();
991 value = NULL;
992 }
993
9a0953ed
P
994 if (!join(buf, sizeof(buf), v->name, "_min", "Name"))
995 return 0;
0f113f3e
MC
996 if (!NCONF_get_number(req_conf, dn_sect, buf, &n_min)) {
997 ERR_clear_error();
998 n_min = -1;
999 }
1000
9a0953ed
P
1001
1002 if (!join(buf, sizeof(buf), v->name, "_max", "Name"))
1003 return 0;
0f113f3e
MC
1004 if (!NCONF_get_number(req_conf, dn_sect, buf, &n_max)) {
1005 ERR_clear_error();
1006 n_max = -1;
1007 }
1008
1009 if (!add_DN_object(subj, v->value, def, value, nid,
1010 n_min, n_max, chtype, mval))
1011 return 0;
1012 }
1013 if (X509_NAME_entry_count(subj) == 0) {
1014 BIO_printf(bio_err,
1015 "error, no objects specified in config file\n");
1016 return 0;
1017 }
1018
1019 if (attribs) {
1020 if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0)
1021 && (!batch)) {
1022 BIO_printf(bio_err,
1023 "\nPlease enter the following 'extra' attributes\n");
1024 BIO_printf(bio_err,
1025 "to be sent with your certificate request\n");
1026 }
1027
1028 i = -1;
2234212c
PY
1029 start2:
1030 for ( ; ; ) {
0f113f3e
MC
1031 i++;
1032 if ((attr_sk == NULL) || (sk_CONF_VALUE_num(attr_sk) <= i))
1033 break;
1034
1035 v = sk_CONF_VALUE_value(attr_sk, i);
1036 type = v->name;
1037 if ((nid = OBJ_txt2nid(type)) == NID_undef)
1038 goto start2;
1039
9a0953ed 1040 if (!join(buf, sizeof(buf), type, "_default", "Name"))
0f113f3e 1041 return 0;
0f113f3e
MC
1042 if ((def = NCONF_get_string(req_conf, attr_sect, buf))
1043 == NULL) {
1044 ERR_clear_error();
1045 def = "";
1046 }
1047
9a0953ed
P
1048 if (!join(buf, sizeof(buf), type, "_value", "Name"))
1049 return 0;
0f113f3e
MC
1050 if ((value = NCONF_get_string(req_conf, attr_sect, buf))
1051 == NULL) {
1052 ERR_clear_error();
1053 value = NULL;
1054 }
1055
9a0953ed
P
1056 if (!join(buf, sizeof(buf), type,"_min", "Name"))
1057 return 0;
0f113f3e
MC
1058 if (!NCONF_get_number(req_conf, attr_sect, buf, &n_min)) {
1059 ERR_clear_error();
1060 n_min = -1;
1061 }
1062
9a0953ed
P
1063 if (!join(buf, sizeof(buf), type, "_max", "Name"))
1064 return 0;
0f113f3e
MC
1065 if (!NCONF_get_number(req_conf, attr_sect, buf, &n_max)) {
1066 ERR_clear_error();
1067 n_max = -1;
1068 }
1069
1070 if (!add_attribute_object(req,
1071 v->value, def, value, nid, n_min,
1072 n_max, chtype))
1073 return 0;
1074 }
1075 }
1076 } else {
1077 BIO_printf(bio_err, "No template, please set one up.\n");
1078 return 0;
1079 }
1080
1081 return 1;
1082
1083}
b38f9f66
DSH
1084
1085static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
0f113f3e
MC
1086 STACK_OF(CONF_VALUE) *attr_sk, int attribs,
1087 unsigned long chtype)
1088{
b5292f7b 1089 int i, spec_char, plus_char;
0f113f3e
MC
1090 char *p, *q;
1091 char *type;
1092 CONF_VALUE *v;
1093 X509_NAME *subj;
1094
1095 subj = X509_REQ_get_subject_name(req);
1096
1097 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
1098 int mval;
1099 v = sk_CONF_VALUE_value(dn_sk, i);
1100 p = q = NULL;
1101 type = v->name;
1102 /*
1103 * Skip past any leading X. X: X, etc to allow for multiple instances
1104 */
b5292f7b 1105 for (p = v->name; *p; p++) {
97d8e82c 1106#ifndef CHARSET_EBCDIC
b5292f7b 1107 spec_char = ((*p == ':') || (*p == ',') || (*p == '.'));
97d8e82c 1108#else
b5292f7b
FM
1109 spec_char = ((*p == os_toascii[':']) || (*p == os_toascii[','])
1110 || (*p == os_toascii['.']));
97d8e82c 1111#endif
b5292f7b 1112 if (spec_char) {
0f113f3e
MC
1113 p++;
1114 if (*p)
1115 type = p;
1116 break;
1117 }
b5292f7b 1118 }
1a15c899 1119#ifndef CHARSET_EBCDIC
14d3c0dd 1120 plus_char = (*type == '+');
1a15c899 1121#else
14d3c0dd 1122 plus_char = (*type == os_toascii['+']);
1a15c899 1123#endif
b5292f7b 1124 if (plus_char) {
14d3c0dd 1125 type++;
0f113f3e 1126 mval = -1;
2234212c 1127 } else {
0f113f3e 1128 mval = 0;
2234212c 1129 }
0f113f3e
MC
1130 if (!X509_NAME_add_entry_by_txt(subj, type, chtype,
1131 (unsigned char *)v->value, -1, -1,
1132 mval))
1133 return 0;
1134
1135 }
1136
1137 if (!X509_NAME_entry_count(subj)) {
1138 BIO_printf(bio_err, "error, no objects specified in config file\n");
1139 return 0;
1140 }
1141 if (attribs) {
1142 for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) {
1143 v = sk_CONF_VALUE_value(attr_sk, i);
1144 if (!X509_REQ_add1_attr_by_txt(req, v->name, chtype,
1145 (unsigned char *)v->value, -1))
1146 return 0;
1147 }
1148 }
1149 return 1;
1150}
1151
1152static int add_DN_object(X509_NAME *n, char *text, const char *def,
1153 char *value, int nid, int n_min, int n_max,
1154 unsigned long chtype, int mval)
1155{
1156 int i, ret = 0;
68b00c23 1157 char buf[1024];
0f113f3e
MC
1158 start:
1159 if (!batch)
1160 BIO_printf(bio_err, "%s [%s]:", text, def);
1161 (void)BIO_flush(bio_err);
1162 if (value != NULL) {
9a0953ed
P
1163 if (!join(buf, sizeof(buf), value, "\n", "DN value"))
1164 return 0;
0f113f3e
MC
1165 BIO_printf(bio_err, "%s\n", value);
1166 } else {
1167 buf[0] = '\0';
1168 if (!batch) {
1169 if (!fgets(buf, sizeof buf, stdin))
1170 return 0;
1171 } else {
1172 buf[0] = '\n';
1173 buf[1] = '\0';
1174 }
1175 }
1176
1177 if (buf[0] == '\0')
2234212c
PY
1178 return 0;
1179 if (buf[0] == '\n') {
0f113f3e 1180 if ((def == NULL) || (def[0] == '\0'))
2234212c 1181 return 1;
9a0953ed
P
1182 if (!join(buf, sizeof(buf), def, "\n", "DN default"))
1183 return 0;
2234212c
PY
1184 } else if ((buf[0] == '.') && (buf[1] == '\n')) {
1185 return 1;
1186 }
0f113f3e
MC
1187
1188 i = strlen(buf);
1189 if (buf[i - 1] != '\n') {
1190 BIO_printf(bio_err, "weird input :-(\n");
2234212c 1191 return 0;
0f113f3e
MC
1192 }
1193 buf[--i] = '\0';
a53955d8 1194#ifdef CHARSET_EBCDIC
0f113f3e 1195 ebcdic2ascii(buf, buf, i);
a53955d8 1196#endif
0f113f3e
MC
1197 if (!req_check_len(i, n_min, n_max)) {
1198 if (batch || value)
1199 return 0;
1200 goto start;
1201 }
1202
1203 if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
1204 (unsigned char *)buf, -1, -1, mval))
1205 goto err;
1206 ret = 1;
1207 err:
9a0953ed 1208 return ret;
0f113f3e 1209}
d02b48c6 1210
7d727231 1211static int add_attribute_object(X509_REQ *req, char *text, const char *def,
0f113f3e
MC
1212 char *value, int nid, int n_min,
1213 int n_max, unsigned long chtype)
1214{
1215 int i;
1216 static char buf[1024];
1217
1218 start:
1219 if (!batch)
1220 BIO_printf(bio_err, "%s [%s]:", text, def);
1221 (void)BIO_flush(bio_err);
1222 if (value != NULL) {
9a0953ed
P
1223 if (!join(buf, sizeof(buf), value, "\n", "Attribute value"))
1224 return 0;
0f113f3e
MC
1225 BIO_printf(bio_err, "%s\n", value);
1226 } else {
1227 buf[0] = '\0';
1228 if (!batch) {
1229 if (!fgets(buf, sizeof buf, stdin))
1230 return 0;
1231 } else {
1232 buf[0] = '\n';
1233 buf[1] = '\0';
1234 }
1235 }
1236
1237 if (buf[0] == '\0')
2234212c
PY
1238 return 0;
1239 if (buf[0] == '\n') {
0f113f3e 1240 if ((def == NULL) || (def[0] == '\0'))
2234212c 1241 return 1;
9a0953ed
P
1242 if (!join(buf, sizeof(buf), def, "\n", "Attribute default"))
1243 return 0;
2234212c
PY
1244 } else if ((buf[0] == '.') && (buf[1] == '\n')) {
1245 return 1;
1246 }
0f113f3e
MC
1247
1248 i = strlen(buf);
1249 if (buf[i - 1] != '\n') {
1250 BIO_printf(bio_err, "weird input :-(\n");
2234212c 1251 return 0;
0f113f3e
MC
1252 }
1253 buf[--i] = '\0';
97d8e82c 1254#ifdef CHARSET_EBCDIC
0f113f3e 1255 ebcdic2ascii(buf, buf, i);
97d8e82c 1256#endif
0f113f3e
MC
1257 if (!req_check_len(i, n_min, n_max)) {
1258 if (batch || value)
1259 return 0;
1260 goto start;
1261 }
1262
1263 if (!X509_REQ_add1_attr_by_NID(req, nid, chtype,
1264 (unsigned char *)buf, -1)) {
1265 BIO_printf(bio_err, "Error adding attribute\n");
1266 ERR_print_errors(bio_err);
1267 goto err;
1268 }
1269
9a0953ed 1270 return 1;
0f113f3e 1271 err:
9a0953ed 1272 return 0;
0f113f3e 1273}
d02b48c6 1274
b7a26e6d 1275static int req_check_len(int len, int n_min, int n_max)
0f113f3e
MC
1276{
1277 if ((n_min > 0) && (len < n_min)) {
1278 BIO_printf(bio_err,
1279 "string is too short, it needs to be at least %d bytes long\n",
1280 n_min);
9a0953ed 1281 return 0;
0f113f3e
MC
1282 }
1283 if ((n_max >= 0) && (len > n_max)) {
1284 BIO_printf(bio_err,
0cb8c9d8 1285 "string is too long, it needs to be no more than %d bytes long\n",
0f113f3e 1286 n_max);
9a0953ed 1287 return 0;
0f113f3e 1288 }
9a0953ed 1289 return 1;
0f113f3e 1290}
a43aa73e
DSH
1291
1292/* Check if the end of a string matches 'end' */
7d727231 1293static int check_end(const char *str, const char *end)
a43aa73e 1294{
9a0953ed 1295 size_t elen, slen;
0f113f3e 1296 const char *tmp;
9a0953ed 1297
0f113f3e
MC
1298 elen = strlen(end);
1299 slen = strlen(str);
1300 if (elen > slen)
1301 return 1;
1302 tmp = str + slen - elen;
1303 return strcmp(tmp, end);
a43aa73e 1304}
959e8dfe 1305
9a0953ed
P
1306/*
1307 * Merge the two strings together into the result buffer checking for
1308 * overflow and producing an error message is there is.
1309 */
1310static int join(char buf[], size_t buf_size, const char *name,
1311 const char *tail, const char *desc)
1312{
1313 const size_t name_len = strlen(name), tail_len = strlen(tail);
1314
1315 if (name_len + tail_len + 1 > buf_size) {
1316 BIO_printf(bio_err, "%s '%s' too long\n", desc, name);
1317 return 0;
1318 }
1319 memcpy(buf, name, name_len);
1320 memcpy(buf + name_len, tail, tail_len + 1);
1321 return 1;
1322}
1323
7e1b7485 1324static EVP_PKEY_CTX *set_keygen_ctx(const char *gstr,
0f113f3e
MC
1325 int *pkey_type, long *pkeylen,
1326 char **palgnam, ENGINE *keygen_engine)
1327{
1328 EVP_PKEY_CTX *gctx = NULL;
1329 EVP_PKEY *param = NULL;
1330 long keylen = -1;
1331 BIO *pbio = NULL;
1332 const char *paramfile = NULL;
1333
1334 if (gstr == NULL) {
1335 *pkey_type = EVP_PKEY_RSA;
1336 keylen = *pkeylen;
1337 } else if (gstr[0] >= '0' && gstr[0] <= '9') {
1338 *pkey_type = EVP_PKEY_RSA;
1339 keylen = atol(gstr);
1340 *pkeylen = keylen;
2234212c 1341 } else if (strncmp(gstr, "param:", 6) == 0) {
0f113f3e 1342 paramfile = gstr + 6;
2234212c 1343 } else {
0f113f3e
MC
1344 const char *p = strchr(gstr, ':');
1345 int len;
1346 ENGINE *tmpeng;
1347 const EVP_PKEY_ASN1_METHOD *ameth;
1348
2234212c 1349 if (p != NULL)
0f113f3e
MC
1350 len = p - gstr;
1351 else
1352 len = strlen(gstr);
1353 /*
1354 * The lookup of a the string will cover all engines so keep a note
1355 * of the implementation.
1356 */
1357
1358 ameth = EVP_PKEY_asn1_find_str(&tmpeng, gstr, len);
1359
2234212c 1360 if (ameth == NULL) {
7e1b7485 1361 BIO_printf(bio_err, "Unknown algorithm %.*s\n", len, gstr);
0f113f3e
MC
1362 return NULL;
1363 }
1364
1365 EVP_PKEY_asn1_get0_info(NULL, pkey_type, NULL, NULL, NULL, ameth);
01b8b3c7 1366#ifndef OPENSSL_NO_ENGINE
7c96dbcd 1367 ENGINE_finish(tmpeng);
01b8b3c7 1368#endif
0f113f3e 1369 if (*pkey_type == EVP_PKEY_RSA) {
2234212c 1370 if (p != NULL) {
0f113f3e
MC
1371 keylen = atol(p + 1);
1372 *pkeylen = keylen;
2234212c 1373 } else {
0f113f3e 1374 keylen = *pkeylen;
2234212c
PY
1375 }
1376 } else if (p != NULL) {
0f113f3e 1377 paramfile = p + 1;
2234212c 1378 }
0f113f3e
MC
1379 }
1380
2234212c 1381 if (paramfile != NULL) {
0f113f3e 1382 pbio = BIO_new_file(paramfile, "r");
2234212c 1383 if (pbio == NULL) {
7e1b7485 1384 BIO_printf(bio_err, "Can't open parameter file %s\n", paramfile);
0f113f3e
MC
1385 return NULL;
1386 }
1387 param = PEM_read_bio_Parameters(pbio, NULL);
1388
2234212c 1389 if (param == NULL) {
0f113f3e 1390 X509 *x;
2234212c 1391
0f113f3e
MC
1392 (void)BIO_reset(pbio);
1393 x = PEM_read_bio_X509(pbio, NULL, NULL, NULL);
2234212c 1394 if (x != NULL) {
0f113f3e
MC
1395 param = X509_get_pubkey(x);
1396 X509_free(x);
1397 }
1398 }
1399
1400 BIO_free(pbio);
1401
2234212c 1402 if (param == NULL) {
7e1b7485 1403 BIO_printf(bio_err, "Error reading parameter file %s\n", paramfile);
0f113f3e
MC
1404 return NULL;
1405 }
2234212c 1406 if (*pkey_type == -1) {
0f113f3e 1407 *pkey_type = EVP_PKEY_id(param);
2234212c 1408 } else if (*pkey_type != EVP_PKEY_base_id(param)) {
7e1b7485 1409 BIO_printf(bio_err, "Key Type does not match parameters\n");
0f113f3e
MC
1410 EVP_PKEY_free(param);
1411 return NULL;
1412 }
1413 }
1414
2234212c 1415 if (palgnam != NULL) {
0f113f3e
MC
1416 const EVP_PKEY_ASN1_METHOD *ameth;
1417 ENGINE *tmpeng;
1418 const char *anam;
2234212c 1419
0f113f3e 1420 ameth = EVP_PKEY_asn1_find(&tmpeng, *pkey_type);
2234212c 1421 if (ameth == NULL) {
7e1b7485 1422 BIO_puts(bio_err, "Internal error: can't find key algorithm\n");
0f113f3e
MC
1423 return NULL;
1424 }
1425 EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, &anam, ameth);
7644a9ae 1426 *palgnam = OPENSSL_strdup(anam);
01b8b3c7 1427#ifndef OPENSSL_NO_ENGINE
7c96dbcd 1428 ENGINE_finish(tmpeng);
01b8b3c7 1429#endif
0f113f3e
MC
1430 }
1431
2234212c 1432 if (param != NULL) {
0f113f3e
MC
1433 gctx = EVP_PKEY_CTX_new(param, keygen_engine);
1434 *pkeylen = EVP_PKEY_bits(param);
1435 EVP_PKEY_free(param);
2234212c 1436 } else {
0f113f3e 1437 gctx = EVP_PKEY_CTX_new_id(*pkey_type, keygen_engine);
2234212c 1438 }
0f113f3e 1439
96487cdd 1440 if (gctx == NULL) {
7e1b7485
RS
1441 BIO_puts(bio_err, "Error allocating keygen context\n");
1442 ERR_print_errors(bio_err);
0f113f3e
MC
1443 return NULL;
1444 }
1445
1446 if (EVP_PKEY_keygen_init(gctx) <= 0) {
7e1b7485
RS
1447 BIO_puts(bio_err, "Error initializing keygen context\n");
1448 ERR_print_errors(bio_err);
69ac182d 1449 EVP_PKEY_CTX_free(gctx);
0f113f3e
MC
1450 return NULL;
1451 }
d4f0339c 1452#ifndef OPENSSL_NO_RSA
0f113f3e
MC
1453 if ((*pkey_type == EVP_PKEY_RSA) && (keylen != -1)) {
1454 if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx, keylen) <= 0) {
7e1b7485
RS
1455 BIO_puts(bio_err, "Error setting RSA keysize\n");
1456 ERR_print_errors(bio_err);
0f113f3e
MC
1457 EVP_PKEY_CTX_free(gctx);
1458 return NULL;
1459 }
1460 }
d4f0339c 1461#endif
959e8dfe 1462
0f113f3e
MC
1463 return gctx;
1464}
959e8dfe
DSH
1465
1466static int genpkey_cb(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1467{
1468 char c = '*';
1469 BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
1470 int p;
1471 p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
1472 if (p == 0)
1473 c = '.';
1474 if (p == 1)
1475 c = '+';
1476 if (p == 2)
1477 c = '*';
1478 if (p == 3)
1479 c = '\n';
1480 BIO_write(b, &c, 1);
1481 (void)BIO_flush(b);
1482 return 1;
1483}
cdb182b5 1484
7e1b7485 1485static int do_sign_init(EVP_MD_CTX *ctx, EVP_PKEY *pkey,
0f113f3e
MC
1486 const EVP_MD *md, STACK_OF(OPENSSL_STRING) *sigopts)
1487{
1488 EVP_PKEY_CTX *pkctx = NULL;
1489 int i;
7e1b7485 1490
6e59a892
RL
1491 if (ctx == NULL)
1492 return 0;
0f113f3e
MC
1493 if (!EVP_DigestSignInit(ctx, &pkctx, md, NULL, pkey))
1494 return 0;
1495 for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
1496 char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
1497 if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
7e1b7485 1498 BIO_printf(bio_err, "parameter error \"%s\"\n", sigopt);
0f113f3e
MC
1499 ERR_print_errors(bio_err);
1500 return 0;
1501 }
1502 }
1503 return 1;
1504}
cdb182b5 1505
7e1b7485 1506int do_X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md,
0f113f3e
MC
1507 STACK_OF(OPENSSL_STRING) *sigopts)
1508{
1509 int rv;
bfb0641f 1510 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
7e1b7485 1511
6e59a892 1512 rv = do_sign_init(mctx, pkey, md, sigopts);
0f113f3e 1513 if (rv > 0)
6e59a892 1514 rv = X509_sign_ctx(x, mctx);
c6aca19b 1515 EVP_MD_CTX_free(mctx);
0f113f3e
MC
1516 return rv > 0 ? 1 : 0;
1517}
cdb182b5 1518
7e1b7485 1519int do_X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md,
0f113f3e
MC
1520 STACK_OF(OPENSSL_STRING) *sigopts)
1521{
1522 int rv;
bfb0641f 1523 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
6e59a892 1524 rv = do_sign_init(mctx, pkey, md, sigopts);
0f113f3e 1525 if (rv > 0)
6e59a892 1526 rv = X509_REQ_sign_ctx(x, mctx);
c6aca19b 1527 EVP_MD_CTX_free(mctx);
0f113f3e
MC
1528 return rv > 0 ? 1 : 0;
1529}
4c623cdd 1530
7e1b7485 1531int do_X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md,
0f113f3e
MC
1532 STACK_OF(OPENSSL_STRING) *sigopts)
1533{
1534 int rv;
bfb0641f 1535 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
6e59a892 1536 rv = do_sign_init(mctx, pkey, md, sigopts);
0f113f3e 1537 if (rv > 0)
6e59a892 1538 rv = X509_CRL_sign_ctx(x, mctx);
c6aca19b 1539 EVP_MD_CTX_free(mctx);
0f113f3e
MC
1540 return rv > 0 ? 1 : 0;
1541}