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