]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/req.c
Consistent formatting for sizeof(foo)
[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
89a99cd5
RS
336 if (days && !x509)
337 BIO_printf(bio_err, "Ignoring -days; not generating a certificate");
888adbe0
TM
338 if (x509 && infile == NULL)
339 newreq = 1;
340
6b4a77f5 341 /* TODO: simplify this as pkey is still always NULL here */
3b061a00 342 private = newreq && (pkey == NULL) ? 1 : 0;
0f113f3e 343
7e1b7485 344 if (!app_passwd(passargin, passargout, &passin, &passout)) {
0f113f3e
MC
345 BIO_printf(bio_err, "Error getting passwords\n");
346 goto end;
347 }
d02b48c6 348
cc01d217
RS
349 if (verbose)
350 BIO_printf(bio_err, "Using configuration from %s\n", template);
351 req_conf = app_load_config(template);
c821defc 352 if (template != default_config_file && !app_load_modules(req_conf))
296f54ee
RL
353 goto end;
354
0f113f3e 355 if (req_conf != NULL) {
0f113f3e
MC
356 p = NCONF_get_string(req_conf, NULL, "oid_file");
357 if (p == NULL)
358 ERR_clear_error();
359 if (p != NULL) {
360 BIO *oid_bio;
361
362 oid_bio = BIO_new_file(p, "r");
363 if (oid_bio == NULL) {
50e735f9
MC
364 /*-
365 BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
366 ERR_print_errors(bio_err);
367 */
0f113f3e
MC
368 } else {
369 OBJ_create_objects(oid_bio);
370 BIO_free(oid_bio);
371 }
372 }
373 }
7e1b7485 374 if (!add_oid_section(req_conf))
0f113f3e
MC
375 goto end;
376
377 if (md_alg == NULL) {
378 p = NCONF_get_string(req_conf, SECTION, "default_md");
2234212c 379 if (p == NULL) {
0f113f3e 380 ERR_clear_error();
2234212c 381 } else {
7e1b7485
RS
382 if (!opt_md(p, &md_alg))
383 goto opthelp;
384 digest = md_alg;
0f113f3e
MC
385 }
386 }
387
2234212c 388 if (extensions == NULL) {
0f113f3e 389 extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
2234212c 390 if (extensions == NULL)
0f113f3e
MC
391 ERR_clear_error();
392 }
2234212c 393 if (extensions != NULL) {
0f113f3e
MC
394 /* Check syntax of file */
395 X509V3_CTX ctx;
396 X509V3_set_ctx_test(&ctx);
397 X509V3_set_nconf(&ctx, req_conf);
398 if (!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) {
399 BIO_printf(bio_err,
400 "Error Loading extension section %s\n", extensions);
401 goto end;
402 }
403 }
404
ebc4815f
VD
405 if (passin == NULL) {
406 passin = nofree_passin =
407 NCONF_get_string(req_conf, SECTION, "input_password");
408 if (passin == NULL)
0f113f3e
MC
409 ERR_clear_error();
410 }
411
ebc4815f
VD
412 if (passout == NULL) {
413 passout = nofree_passout =
414 NCONF_get_string(req_conf, SECTION, "output_password");
415 if (passout == NULL)
0f113f3e
MC
416 ERR_clear_error();
417 }
418
419 p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
2234212c 420 if (p == NULL)
0f113f3e
MC
421 ERR_clear_error();
422
2234212c 423 if (p != NULL && !ASN1_STRING_set_default_mask_asc(p)) {
0f113f3e
MC
424 BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
425 goto end;
426 }
427
428 if (chtype != MBSTRING_UTF8) {
429 p = NCONF_get_string(req_conf, SECTION, UTF8_IN);
2234212c 430 if (p == NULL)
0f113f3e 431 ERR_clear_error();
86885c28 432 else if (strcmp(p, "yes") == 0)
0f113f3e
MC
433 chtype = MBSTRING_UTF8;
434 }
435
2234212c 436 if (req_exts == NULL) {
0f113f3e 437 req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
2234212c 438 if (req_exts == NULL)
0f113f3e
MC
439 ERR_clear_error();
440 }
2234212c 441 if (req_exts != NULL) {
0f113f3e
MC
442 /* Check syntax of file */
443 X509V3_CTX ctx;
444 X509V3_set_ctx_test(&ctx);
445 X509V3_set_nconf(&ctx, req_conf);
446 if (!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) {
447 BIO_printf(bio_err,
448 "Error Loading request extension section %s\n",
449 req_exts);
450 goto end;
451 }
452 }
d02b48c6 453
0f113f3e 454 if (keyfile != NULL) {
7e1b7485 455 pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
2234212c 456 if (pkey == NULL) {
7e1b7485 457 /* load_key() has already printed an appropriate message */
0f113f3e
MC
458 goto end;
459 } else {
3ee1eac2 460 app_RAND_load_conf(req_conf, SECTION);
0f113f3e
MC
461 }
462 }
463
464 if (newreq && (pkey == NULL)) {
3ee1eac2 465 app_RAND_load_conf(req_conf, SECTION);
0f113f3e
MC
466
467 if (!NCONF_get_number(req_conf, SECTION, BITS, &newkey)) {
468 newkey = DEFAULT_KEY_LENGTH;
469 }
470
2234212c 471 if (keyalg != NULL) {
7e1b7485 472 genctx = set_keygen_ctx(keyalg, &pkey_type, &newkey,
0f113f3e 473 &keyalgstr, gen_eng);
2234212c 474 if (genctx == NULL)
0f113f3e
MC
475 goto end;
476 }
477
478 if (newkey < MIN_KEY_LENGTH
479 && (pkey_type == EVP_PKEY_RSA || pkey_type == EVP_PKEY_DSA)) {
480 BIO_printf(bio_err, "private key length is too short,\n");
481 BIO_printf(bio_err, "it needs to be at least %d bits, not %ld\n",
482 MIN_KEY_LENGTH, newkey);
483 goto end;
484 }
485
2234212c 486 if (genctx == NULL) {
7e1b7485 487 genctx = set_keygen_ctx(NULL, &pkey_type, &newkey,
0f113f3e
MC
488 &keyalgstr, gen_eng);
489 if (!genctx)
490 goto end;
491 }
492
2234212c 493 if (pkeyopts != NULL) {
0f113f3e
MC
494 char *genopt;
495 for (i = 0; i < sk_OPENSSL_STRING_num(pkeyopts); i++) {
496 genopt = sk_OPENSSL_STRING_value(pkeyopts, i);
497 if (pkey_ctrl_string(genctx, genopt) <= 0) {
498 BIO_printf(bio_err, "parameter error \"%s\"\n", genopt);
499 ERR_print_errors(bio_err);
500 goto end;
501 }
502 }
503 }
504
57358a83
MC
505 if (pkey_type == EVP_PKEY_EC) {
506 BIO_printf(bio_err, "Generating an EC private key\n");
507 } else {
508 BIO_printf(bio_err, "Generating a %ld bit %s private key\n",
509 newkey, keyalgstr);
510 }
0f113f3e
MC
511
512 EVP_PKEY_CTX_set_cb(genctx, genpkey_cb);
513 EVP_PKEY_CTX_set_app_data(genctx, bio_err);
514
515 if (EVP_PKEY_keygen(genctx, &pkey) <= 0) {
516 BIO_puts(bio_err, "Error Generating Key\n");
517 goto end;
518 }
519
520 EVP_PKEY_CTX_free(genctx);
521 genctx = NULL;
522
0f113f3e
MC
523 if (keyout == NULL) {
524 keyout = NCONF_get_string(req_conf, SECTION, KEYFILE);
525 if (keyout == NULL)
526 ERR_clear_error();
527 }
528
7e1b7485 529 if (keyout == NULL)
0f113f3e 530 BIO_printf(bio_err, "writing new private key to stdout\n");
7e1b7485 531 else
0f113f3e 532 BIO_printf(bio_err, "writing new private key to '%s'\n", keyout);
bdd58d98 533 out = bio_open_owner(keyout, outformat, private);
7e1b7485
RS
534 if (out == NULL)
535 goto end;
0f113f3e
MC
536
537 p = NCONF_get_string(req_conf, SECTION, "encrypt_rsa_key");
538 if (p == NULL) {
539 ERR_clear_error();
540 p = NCONF_get_string(req_conf, SECTION, "encrypt_key");
541 if (p == NULL)
542 ERR_clear_error();
543 }
544 if ((p != NULL) && (strcmp(p, "no") == 0))
545 cipher = NULL;
546 if (nodes)
547 cipher = NULL;
548
549 i = 0;
550 loop:
3b061a00 551 assert(private);
0f113f3e
MC
552 if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
553 NULL, 0, NULL, passout)) {
554 if ((ERR_GET_REASON(ERR_peek_error()) ==
555 PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) {
556 ERR_clear_error();
557 i++;
558 goto loop;
559 }
560 goto end;
561 }
cf89a80e 562 BIO_free(out);
21425195 563 out = NULL;
0f113f3e
MC
564 BIO_printf(bio_err, "-----\n");
565 }
566
567 if (!newreq) {
bdd58d98 568 in = bio_open_default(infile, 'r', informat);
7e1b7485
RS
569 if (in == NULL)
570 goto end;
0f113f3e
MC
571
572 if (informat == FORMAT_ASN1)
573 req = d2i_X509_REQ_bio(in, NULL);
7e1b7485 574 else
0f113f3e 575 req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
0f113f3e
MC
576 if (req == NULL) {
577 BIO_printf(bio_err, "unable to load X509 request\n");
578 goto end;
579 }
580 }
581
888adbe0 582 if (newreq || x509) {
0f113f3e
MC
583 if (pkey == NULL) {
584 BIO_printf(bio_err, "you need to specify a private key\n");
585 goto end;
586 }
587
588 if (req == NULL) {
589 req = X509_REQ_new();
590 if (req == NULL) {
591 goto end;
592 }
593
594 i = make_REQ(req, pkey, subj, multirdn, !x509, chtype);
595 subj = NULL; /* done processing '-subj' option */
0f113f3e
MC
596 if (!i) {
597 BIO_printf(bio_err, "problems making Certificate Request\n");
598 goto end;
599 }
600 }
601 if (x509) {
602 EVP_PKEY *tmppkey;
603 X509V3_CTX ext_ctx;
604 if ((x509ss = X509_new()) == NULL)
605 goto end;
606
607 /* Set version to V3 */
2234212c 608 if (extensions != NULL && !X509_set_version(x509ss, 2))
0f113f3e 609 goto end;
2234212c 610 if (serial != NULL) {
0f113f3e
MC
611 if (!X509_set_serialNumber(x509ss, serial))
612 goto end;
613 } else {
614 if (!rand_serial(NULL, X509_get_serialNumber(x509ss)))
615 goto end;
616 }
617
618 if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req)))
619 goto end;
dc047d31 620 if (!set_cert_times(x509ss, NULL, NULL, days))
0f113f3e
MC
621 goto end;
622 if (!X509_set_subject_name
623 (x509ss, X509_REQ_get_subject_name(req)))
624 goto end;
c5137473 625 tmppkey = X509_REQ_get0_pubkey(req);
0f113f3e
MC
626 if (!tmppkey || !X509_set_pubkey(x509ss, tmppkey))
627 goto end;
0f113f3e
MC
628
629 /* Set up V3 context struct */
630
631 X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
632 X509V3_set_nconf(&ext_ctx, req_conf);
633
634 /* Add extensions */
2234212c
PY
635 if (extensions != NULL && !X509V3_EXT_add_nconf(req_conf,
636 &ext_ctx, extensions,
637 x509ss)) {
0f113f3e
MC
638 BIO_printf(bio_err, "Error Loading extension section %s\n",
639 extensions);
640 goto end;
641 }
642
b6486bf7
RP
643 /* If a pre-cert was requested, we need to add a poison extension */
644 if (precert) {
645 if (X509_add1_ext_i2d(x509ss, NID_ct_precert_poison, NULL, 1, 0)
646 != 1) {
647 BIO_printf(bio_err, "Error adding poison extension\n");
648 goto end;
649 }
650 }
651
7e1b7485 652 i = do_X509_sign(x509ss, pkey, digest, sigopts);
0f113f3e
MC
653 if (!i) {
654 ERR_print_errors(bio_err);
655 goto end;
656 }
657 } else {
658 X509V3_CTX ext_ctx;
659
660 /* Set up V3 context struct */
661
662 X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
663 X509V3_set_nconf(&ext_ctx, req_conf);
664
665 /* Add extensions */
2234212c
PY
666 if (req_exts != NULL
667 && !X509V3_EXT_REQ_add_nconf(req_conf, &ext_ctx,
668 req_exts, req)) {
0f113f3e
MC
669 BIO_printf(bio_err, "Error Loading extension section %s\n",
670 req_exts);
671 goto end;
672 }
7e1b7485 673 i = do_X509_REQ_sign(req, pkey, digest, sigopts);
0f113f3e
MC
674 if (!i) {
675 ERR_print_errors(bio_err);
676 goto end;
677 }
678 }
679 }
680
681 if (subj && x509) {
682 BIO_printf(bio_err, "Cannot modify certificate subject\n");
683 goto end;
684 }
685
686 if (subj && !x509) {
687 if (verbose) {
688 BIO_printf(bio_err, "Modifying Request's Subject\n");
689 print_name(bio_err, "old subject=",
b5c4209b 690 X509_REQ_get_subject_name(req), get_nameopt());
0f113f3e
MC
691 }
692
693 if (build_subject(req, subj, chtype, multirdn) == 0) {
694 BIO_printf(bio_err, "ERROR: cannot modify subject\n");
7e1b7485 695 ret = 1;
0f113f3e
MC
696 goto end;
697 }
698
0f113f3e
MC
699 if (verbose) {
700 print_name(bio_err, "new subject=",
b5c4209b 701 X509_REQ_get_subject_name(req), get_nameopt());
0f113f3e
MC
702 }
703 }
704
705 if (verify && !x509) {
173f613b 706 EVP_PKEY *tpubkey = pkey;
0f113f3e 707
173f613b
F
708 if (tpubkey == NULL) {
709 tpubkey = X509_REQ_get0_pubkey(req);
710 if (tpubkey == NULL)
0f113f3e
MC
711 goto end;
712 }
713
173f613b 714 i = X509_REQ_verify(req, tpubkey);
0f113f3e
MC
715
716 if (i < 0) {
717 goto end;
718 } else if (i == 0) {
719 BIO_printf(bio_err, "verify failure\n");
720 ERR_print_errors(bio_err);
2234212c 721 } else { /* if (i > 0) */
0f113f3e 722 BIO_printf(bio_err, "verify OK\n");
2234212c 723 }
0f113f3e
MC
724 }
725
726 if (noout && !text && !modulus && !subject && !pubkey) {
7e1b7485 727 ret = 0;
0f113f3e
MC
728 goto end;
729 }
730
7e1b7485
RS
731 out = bio_open_default(outfile,
732 keyout != NULL && outfile != NULL &&
bdd58d98
RL
733 strcmp(keyout, outfile) == 0 ? 'a' : 'w',
734 outformat);
7e1b7485
RS
735 if (out == NULL)
736 goto end;
0f113f3e
MC
737
738 if (pubkey) {
1c72f70d
F
739 EVP_PKEY *tpubkey = X509_REQ_get0_pubkey(req);
740
0f113f3e
MC
741 if (tpubkey == NULL) {
742 BIO_printf(bio_err, "Error getting public key\n");
743 ERR_print_errors(bio_err);
744 goto end;
745 }
746 PEM_write_bio_PUBKEY(out, tpubkey);
0f113f3e
MC
747 }
748
749 if (text) {
750 if (x509)
b5c4209b 751 X509_print_ex(out, x509ss, get_nameopt(), reqflag);
0f113f3e 752 else
b5c4209b 753 X509_REQ_print_ex(out, req, get_nameopt(), reqflag);
0f113f3e
MC
754 }
755
756 if (subject) {
757 if (x509)
758 print_name(out, "subject=", X509_get_subject_name(x509ss),
b5c4209b 759 get_nameopt());
0f113f3e
MC
760 else
761 print_name(out, "subject=", X509_REQ_get_subject_name(req),
b5c4209b 762 get_nameopt());
0f113f3e
MC
763 }
764
765 if (modulus) {
766 EVP_PKEY *tpubkey;
767
768 if (x509)
1c72f70d 769 tpubkey = X509_get0_pubkey(x509ss);
0f113f3e 770 else
1c72f70d 771 tpubkey = X509_REQ_get0_pubkey(req);
0f113f3e
MC
772 if (tpubkey == NULL) {
773 fprintf(stdout, "Modulus=unavailable\n");
774 goto end;
775 }
776 fprintf(stdout, "Modulus=");
cf1b7d96 777#ifndef OPENSSL_NO_RSA
9862e9aa 778 if (EVP_PKEY_base_id(tpubkey) == EVP_PKEY_RSA) {
2ac6115d 779 const BIGNUM *n;
9862e9aa
RL
780 RSA_get0_key(EVP_PKEY_get0_RSA(tpubkey), &n, NULL, NULL);
781 BN_print(out, n);
782 } else
13e91dd3 783#endif
0f113f3e 784 fprintf(stdout, "Wrong Algorithm type");
0f113f3e
MC
785 fprintf(stdout, "\n");
786 }
787
788 if (!noout && !x509) {
789 if (outformat == FORMAT_ASN1)
790 i = i2d_X509_REQ_bio(out, req);
7e1b7485
RS
791 else if (newhdr)
792 i = PEM_write_bio_X509_REQ_NEW(out, req);
793 else
794 i = PEM_write_bio_X509_REQ(out, req);
0f113f3e
MC
795 if (!i) {
796 BIO_printf(bio_err, "unable to write X509 request\n");
797 goto end;
798 }
799 }
800 if (!noout && x509 && (x509ss != NULL)) {
801 if (outformat == FORMAT_ASN1)
802 i = i2d_X509_bio(out, x509ss);
7e1b7485 803 else
0f113f3e 804 i = PEM_write_bio_X509(out, x509ss);
0f113f3e
MC
805 if (!i) {
806 BIO_printf(bio_err, "unable to write X509 certificate\n");
807 goto end;
808 }
809 }
7e1b7485 810 ret = 0;
0f113f3e 811 end:
7e1b7485 812 if (ret) {
0f113f3e
MC
813 ERR_print_errors(bio_err);
814 }
cc01d217 815 NCONF_free(req_conf);
0f113f3e
MC
816 BIO_free(in);
817 BIO_free_all(out);
818 EVP_PKEY_free(pkey);
c5ba2d99 819 EVP_PKEY_CTX_free(genctx);
25aaa98a
RS
820 sk_OPENSSL_STRING_free(pkeyopts);
821 sk_OPENSSL_STRING_free(sigopts);
01b8b3c7 822#ifndef OPENSSL_NO_ENGINE
efa7dd64 823 ENGINE_free(gen_eng);
01b8b3c7 824#endif
b548a1f1 825 OPENSSL_free(keyalgstr);
0f113f3e
MC
826 X509_REQ_free(req);
827 X509_free(x509ss);
828 ASN1_INTEGER_free(serial);
dd1abd44 829 release_engine(e);
ebc4815f
VD
830 if (passin != nofree_passin)
831 OPENSSL_free(passin);
832 if (passout != nofree_passout)
833 OPENSSL_free(passout);
9a0953ed 834 return ret;
0f113f3e 835}
d02b48c6 836
7ce9e425 837static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int multirdn,
0f113f3e
MC
838 int attribs, unsigned long chtype)
839{
840 int ret = 0, i;
841 char no_prompt = 0;
842 STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL;
843 char *tmp, *dn_sect, *attr_sect;
844
845 tmp = NCONF_get_string(req_conf, SECTION, PROMPT);
846 if (tmp == NULL)
847 ERR_clear_error();
86885c28 848 if ((tmp != NULL) && strcmp(tmp, "no") == 0)
0f113f3e
MC
849 no_prompt = 1;
850
851 dn_sect = NCONF_get_string(req_conf, SECTION, DISTINGUISHED_NAME);
852 if (dn_sect == NULL) {
853 BIO_printf(bio_err, "unable to find '%s' in config\n",
854 DISTINGUISHED_NAME);
855 goto err;
856 }
857 dn_sk = NCONF_get_section(req_conf, dn_sect);
858 if (dn_sk == NULL) {
859 BIO_printf(bio_err, "unable to get '%s' section\n", dn_sect);
860 goto err;
861 }
862
863 attr_sect = NCONF_get_string(req_conf, SECTION, ATTRIBUTES);
864 if (attr_sect == NULL) {
865 ERR_clear_error();
866 attr_sk = NULL;
867 } else {
868 attr_sk = NCONF_get_section(req_conf, attr_sect);
869 if (attr_sk == NULL) {
870 BIO_printf(bio_err, "unable to get '%s' section\n", attr_sect);
871 goto err;
872 }
873 }
874
875 /* setup version number */
876 if (!X509_REQ_set_version(req, 0L))
877 goto err; /* version 1 */
878
879 if (subj)
880 i = build_subject(req, subj, chtype, multirdn);
881 else if (no_prompt)
882 i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
883 else
884 i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs,
885 chtype);
886 if (!i)
887 goto err;
888
889 if (!X509_REQ_set_pubkey(req, pkey))
890 goto err;
891
892 ret = 1;
893 err:
9a0953ed 894 return ret;
0f113f3e 895}
d02b48c6 896
c0455cbb
LJ
897/*
898 * subject is expected to be in the format /type0=value0/type1=value1/type2=...
899 * where characters may be escaped by \
900 */
cc696296 901static int build_subject(X509_REQ *req, const char *subject, unsigned long chtype,
0f113f3e
MC
902 int multirdn)
903{
904 X509_NAME *n;
905
75ebbd9a 906 if ((n = parse_name(subject, chtype, multirdn)) == NULL)
0f113f3e 907 return 0;
bad40585 908
0f113f3e
MC
909 if (!X509_REQ_set_subject_name(req, n)) {
910 X509_NAME_free(n);
911 return 0;
912 }
913 X509_NAME_free(n);
914 return 1;
915}
b38f9f66
DSH
916
917static int prompt_info(X509_REQ *req,
cc696296
F
918 STACK_OF(CONF_VALUE) *dn_sk, const char *dn_sect,
919 STACK_OF(CONF_VALUE) *attr_sk, const char *attr_sect,
0f113f3e
MC
920 int attribs, unsigned long chtype)
921{
922 int i;
923 char *p, *q;
924 char buf[100];
925 int nid, mval;
926 long n_min, n_max;
927 char *type, *value;
928 const char *def;
929 CONF_VALUE *v;
930 X509_NAME *subj;
931 subj = X509_REQ_get_subject_name(req);
932
933 if (!batch) {
934 BIO_printf(bio_err,
935 "You are about to be asked to enter information that will be incorporated\n");
936 BIO_printf(bio_err, "into your certificate request.\n");
937 BIO_printf(bio_err,
938 "What you are about to enter is what is called a Distinguished Name or a DN.\n");
939 BIO_printf(bio_err,
940 "There are quite a few fields but you can leave some blank\n");
941 BIO_printf(bio_err,
942 "For some fields there will be a default value,\n");
943 BIO_printf(bio_err,
944 "If you enter '.', the field will be left blank.\n");
945 BIO_printf(bio_err, "-----\n");
946 }
947
948 if (sk_CONF_VALUE_num(dn_sk)) {
949 i = -1;
2234212c
PY
950 start:
951 for ( ; ; ) {
0f113f3e
MC
952 i++;
953 if (sk_CONF_VALUE_num(dn_sk) <= i)
954 break;
955
956 v = sk_CONF_VALUE_value(dn_sk, i);
957 p = q = NULL;
958 type = v->name;
959 if (!check_end(type, "_min") || !check_end(type, "_max") ||
960 !check_end(type, "_default") || !check_end(type, "_value"))
961 continue;
962 /*
963 * Skip past any leading X. X: X, etc to allow for multiple
964 * instances
965 */
966 for (p = v->name; *p; p++)
967 if ((*p == ':') || (*p == ',') || (*p == '.')) {
968 p++;
969 if (*p)
970 type = p;
971 break;
972 }
973 if (*type == '+') {
974 mval = -1;
975 type++;
2234212c 976 } else {
0f113f3e 977 mval = 0;
2234212c 978 }
0f113f3e
MC
979 /* If OBJ not recognised ignore it */
980 if ((nid = OBJ_txt2nid(type)) == NID_undef)
981 goto start;
9a0953ed 982 if (!join(buf, sizeof(buf), v->name, "_default", "Name"))
0f113f3e 983 return 0;
0f113f3e
MC
984 if ((def = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
985 ERR_clear_error();
986 def = "";
987 }
988
9a0953ed
P
989 if (!join(buf, sizeof(buf), v->name, "_value", "Name"))
990 return 0;
0f113f3e
MC
991 if ((value = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
992 ERR_clear_error();
993 value = NULL;
994 }
995
9a0953ed
P
996 if (!join(buf, sizeof(buf), v->name, "_min", "Name"))
997 return 0;
0f113f3e
MC
998 if (!NCONF_get_number(req_conf, dn_sect, buf, &n_min)) {
999 ERR_clear_error();
1000 n_min = -1;
1001 }
1002
9a0953ed
P
1003
1004 if (!join(buf, sizeof(buf), v->name, "_max", "Name"))
1005 return 0;
0f113f3e
MC
1006 if (!NCONF_get_number(req_conf, dn_sect, buf, &n_max)) {
1007 ERR_clear_error();
1008 n_max = -1;
1009 }
1010
1011 if (!add_DN_object(subj, v->value, def, value, nid,
1012 n_min, n_max, chtype, mval))
1013 return 0;
1014 }
1015 if (X509_NAME_entry_count(subj) == 0) {
1016 BIO_printf(bio_err,
1017 "error, no objects specified in config file\n");
1018 return 0;
1019 }
1020
1021 if (attribs) {
1022 if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0)
1023 && (!batch)) {
1024 BIO_printf(bio_err,
1025 "\nPlease enter the following 'extra' attributes\n");
1026 BIO_printf(bio_err,
1027 "to be sent with your certificate request\n");
1028 }
1029
1030 i = -1;
2234212c
PY
1031 start2:
1032 for ( ; ; ) {
0f113f3e
MC
1033 i++;
1034 if ((attr_sk == NULL) || (sk_CONF_VALUE_num(attr_sk) <= i))
1035 break;
1036
1037 v = sk_CONF_VALUE_value(attr_sk, i);
1038 type = v->name;
1039 if ((nid = OBJ_txt2nid(type)) == NID_undef)
1040 goto start2;
1041
9a0953ed 1042 if (!join(buf, sizeof(buf), type, "_default", "Name"))
0f113f3e 1043 return 0;
0f113f3e
MC
1044 if ((def = NCONF_get_string(req_conf, attr_sect, buf))
1045 == NULL) {
1046 ERR_clear_error();
1047 def = "";
1048 }
1049
9a0953ed
P
1050 if (!join(buf, sizeof(buf), type, "_value", "Name"))
1051 return 0;
0f113f3e
MC
1052 if ((value = NCONF_get_string(req_conf, attr_sect, buf))
1053 == NULL) {
1054 ERR_clear_error();
1055 value = NULL;
1056 }
1057
9a0953ed
P
1058 if (!join(buf, sizeof(buf), type,"_min", "Name"))
1059 return 0;
0f113f3e
MC
1060 if (!NCONF_get_number(req_conf, attr_sect, buf, &n_min)) {
1061 ERR_clear_error();
1062 n_min = -1;
1063 }
1064
9a0953ed
P
1065 if (!join(buf, sizeof(buf), type, "_max", "Name"))
1066 return 0;
0f113f3e
MC
1067 if (!NCONF_get_number(req_conf, attr_sect, buf, &n_max)) {
1068 ERR_clear_error();
1069 n_max = -1;
1070 }
1071
1072 if (!add_attribute_object(req,
1073 v->value, def, value, nid, n_min,
1074 n_max, chtype))
1075 return 0;
1076 }
1077 }
1078 } else {
1079 BIO_printf(bio_err, "No template, please set one up.\n");
1080 return 0;
1081 }
1082
1083 return 1;
1084
1085}
b38f9f66
DSH
1086
1087static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
0f113f3e
MC
1088 STACK_OF(CONF_VALUE) *attr_sk, int attribs,
1089 unsigned long chtype)
1090{
b5292f7b 1091 int i, spec_char, plus_char;
0f113f3e
MC
1092 char *p, *q;
1093 char *type;
1094 CONF_VALUE *v;
1095 X509_NAME *subj;
1096
1097 subj = X509_REQ_get_subject_name(req);
1098
1099 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
1100 int mval;
1101 v = sk_CONF_VALUE_value(dn_sk, i);
1102 p = q = NULL;
1103 type = v->name;
1104 /*
1105 * Skip past any leading X. X: X, etc to allow for multiple instances
1106 */
b5292f7b 1107 for (p = v->name; *p; p++) {
97d8e82c 1108#ifndef CHARSET_EBCDIC
b5292f7b 1109 spec_char = ((*p == ':') || (*p == ',') || (*p == '.'));
97d8e82c 1110#else
b5292f7b
FM
1111 spec_char = ((*p == os_toascii[':']) || (*p == os_toascii[','])
1112 || (*p == os_toascii['.']));
97d8e82c 1113#endif
b5292f7b 1114 if (spec_char) {
0f113f3e
MC
1115 p++;
1116 if (*p)
1117 type = p;
1118 break;
1119 }
b5292f7b 1120 }
1a15c899 1121#ifndef CHARSET_EBCDIC
14d3c0dd 1122 plus_char = (*type == '+');
1a15c899 1123#else
14d3c0dd 1124 plus_char = (*type == os_toascii['+']);
1a15c899 1125#endif
b5292f7b 1126 if (plus_char) {
14d3c0dd 1127 type++;
0f113f3e 1128 mval = -1;
2234212c 1129 } else {
0f113f3e 1130 mval = 0;
2234212c 1131 }
0f113f3e
MC
1132 if (!X509_NAME_add_entry_by_txt(subj, type, chtype,
1133 (unsigned char *)v->value, -1, -1,
1134 mval))
1135 return 0;
1136
1137 }
1138
1139 if (!X509_NAME_entry_count(subj)) {
1140 BIO_printf(bio_err, "error, no objects specified in config file\n");
1141 return 0;
1142 }
1143 if (attribs) {
1144 for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) {
1145 v = sk_CONF_VALUE_value(attr_sk, i);
1146 if (!X509_REQ_add1_attr_by_txt(req, v->name, chtype,
1147 (unsigned char *)v->value, -1))
1148 return 0;
1149 }
1150 }
1151 return 1;
1152}
1153
1154static int add_DN_object(X509_NAME *n, char *text, const char *def,
1155 char *value, int nid, int n_min, int n_max,
1156 unsigned long chtype, int mval)
1157{
1158 int i, ret = 0;
68b00c23 1159 char buf[1024];
0f113f3e
MC
1160 start:
1161 if (!batch)
1162 BIO_printf(bio_err, "%s [%s]:", text, def);
1163 (void)BIO_flush(bio_err);
1164 if (value != NULL) {
9a0953ed
P
1165 if (!join(buf, sizeof(buf), value, "\n", "DN value"))
1166 return 0;
0f113f3e
MC
1167 BIO_printf(bio_err, "%s\n", value);
1168 } else {
1169 buf[0] = '\0';
1170 if (!batch) {
cbe29648 1171 if (!fgets(buf, sizeof(buf), stdin))
0f113f3e
MC
1172 return 0;
1173 } else {
1174 buf[0] = '\n';
1175 buf[1] = '\0';
1176 }
1177 }
1178
1179 if (buf[0] == '\0')
2234212c
PY
1180 return 0;
1181 if (buf[0] == '\n') {
0f113f3e 1182 if ((def == NULL) || (def[0] == '\0'))
2234212c 1183 return 1;
9a0953ed
P
1184 if (!join(buf, sizeof(buf), def, "\n", "DN default"))
1185 return 0;
2234212c
PY
1186 } else if ((buf[0] == '.') && (buf[1] == '\n')) {
1187 return 1;
1188 }
0f113f3e
MC
1189
1190 i = strlen(buf);
1191 if (buf[i - 1] != '\n') {
1192 BIO_printf(bio_err, "weird input :-(\n");
2234212c 1193 return 0;
0f113f3e
MC
1194 }
1195 buf[--i] = '\0';
a53955d8 1196#ifdef CHARSET_EBCDIC
0f113f3e 1197 ebcdic2ascii(buf, buf, i);
a53955d8 1198#endif
0f113f3e
MC
1199 if (!req_check_len(i, n_min, n_max)) {
1200 if (batch || value)
1201 return 0;
1202 goto start;
1203 }
1204
1205 if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
1206 (unsigned char *)buf, -1, -1, mval))
1207 goto err;
1208 ret = 1;
1209 err:
9a0953ed 1210 return ret;
0f113f3e 1211}
d02b48c6 1212
7d727231 1213static int add_attribute_object(X509_REQ *req, char *text, const char *def,
0f113f3e
MC
1214 char *value, int nid, int n_min,
1215 int n_max, unsigned long chtype)
1216{
1217 int i;
1218 static char buf[1024];
1219
1220 start:
1221 if (!batch)
1222 BIO_printf(bio_err, "%s [%s]:", text, def);
1223 (void)BIO_flush(bio_err);
1224 if (value != NULL) {
9a0953ed
P
1225 if (!join(buf, sizeof(buf), value, "\n", "Attribute value"))
1226 return 0;
0f113f3e
MC
1227 BIO_printf(bio_err, "%s\n", value);
1228 } else {
1229 buf[0] = '\0';
1230 if (!batch) {
cbe29648 1231 if (!fgets(buf, sizeof(buf), stdin))
0f113f3e
MC
1232 return 0;
1233 } else {
1234 buf[0] = '\n';
1235 buf[1] = '\0';
1236 }
1237 }
1238
1239 if (buf[0] == '\0')
2234212c
PY
1240 return 0;
1241 if (buf[0] == '\n') {
0f113f3e 1242 if ((def == NULL) || (def[0] == '\0'))
2234212c 1243 return 1;
9a0953ed
P
1244 if (!join(buf, sizeof(buf), def, "\n", "Attribute default"))
1245 return 0;
2234212c
PY
1246 } else if ((buf[0] == '.') && (buf[1] == '\n')) {
1247 return 1;
1248 }
0f113f3e
MC
1249
1250 i = strlen(buf);
1251 if (buf[i - 1] != '\n') {
1252 BIO_printf(bio_err, "weird input :-(\n");
2234212c 1253 return 0;
0f113f3e
MC
1254 }
1255 buf[--i] = '\0';
97d8e82c 1256#ifdef CHARSET_EBCDIC
0f113f3e 1257 ebcdic2ascii(buf, buf, i);
97d8e82c 1258#endif
0f113f3e
MC
1259 if (!req_check_len(i, n_min, n_max)) {
1260 if (batch || value)
1261 return 0;
1262 goto start;
1263 }
1264
1265 if (!X509_REQ_add1_attr_by_NID(req, nid, chtype,
1266 (unsigned char *)buf, -1)) {
1267 BIO_printf(bio_err, "Error adding attribute\n");
1268 ERR_print_errors(bio_err);
1269 goto err;
1270 }
1271
9a0953ed 1272 return 1;
0f113f3e 1273 err:
9a0953ed 1274 return 0;
0f113f3e 1275}
d02b48c6 1276
b7a26e6d 1277static int req_check_len(int len, int n_min, int n_max)
0f113f3e
MC
1278{
1279 if ((n_min > 0) && (len < n_min)) {
1280 BIO_printf(bio_err,
1281 "string is too short, it needs to be at least %d bytes long\n",
1282 n_min);
9a0953ed 1283 return 0;
0f113f3e
MC
1284 }
1285 if ((n_max >= 0) && (len > n_max)) {
1286 BIO_printf(bio_err,
0cb8c9d8 1287 "string is too long, it needs to be no more than %d bytes long\n",
0f113f3e 1288 n_max);
9a0953ed 1289 return 0;
0f113f3e 1290 }
9a0953ed 1291 return 1;
0f113f3e 1292}
a43aa73e
DSH
1293
1294/* Check if the end of a string matches 'end' */
7d727231 1295static int check_end(const char *str, const char *end)
a43aa73e 1296{
9a0953ed 1297 size_t elen, slen;
0f113f3e 1298 const char *tmp;
9a0953ed 1299
0f113f3e
MC
1300 elen = strlen(end);
1301 slen = strlen(str);
1302 if (elen > slen)
1303 return 1;
1304 tmp = str + slen - elen;
1305 return strcmp(tmp, end);
a43aa73e 1306}
959e8dfe 1307
9a0953ed
P
1308/*
1309 * Merge the two strings together into the result buffer checking for
44e69951 1310 * overflow and producing an error message if there is.
9a0953ed
P
1311 */
1312static int join(char buf[], size_t buf_size, const char *name,
1313 const char *tail, const char *desc)
1314{
1315 const size_t name_len = strlen(name), tail_len = strlen(tail);
1316
1317 if (name_len + tail_len + 1 > buf_size) {
1318 BIO_printf(bio_err, "%s '%s' too long\n", desc, name);
1319 return 0;
1320 }
1321 memcpy(buf, name, name_len);
1322 memcpy(buf + name_len, tail, tail_len + 1);
1323 return 1;
1324}
1325
7e1b7485 1326static EVP_PKEY_CTX *set_keygen_ctx(const char *gstr,
0f113f3e
MC
1327 int *pkey_type, long *pkeylen,
1328 char **palgnam, ENGINE *keygen_engine)
1329{
1330 EVP_PKEY_CTX *gctx = NULL;
1331 EVP_PKEY *param = NULL;
1332 long keylen = -1;
1333 BIO *pbio = NULL;
1334 const char *paramfile = NULL;
1335
1336 if (gstr == NULL) {
1337 *pkey_type = EVP_PKEY_RSA;
1338 keylen = *pkeylen;
1339 } else if (gstr[0] >= '0' && gstr[0] <= '9') {
1340 *pkey_type = EVP_PKEY_RSA;
1341 keylen = atol(gstr);
1342 *pkeylen = keylen;
2234212c 1343 } else if (strncmp(gstr, "param:", 6) == 0) {
0f113f3e 1344 paramfile = gstr + 6;
2234212c 1345 } else {
0f113f3e
MC
1346 const char *p = strchr(gstr, ':');
1347 int len;
1348 ENGINE *tmpeng;
1349 const EVP_PKEY_ASN1_METHOD *ameth;
1350
2234212c 1351 if (p != NULL)
0f113f3e
MC
1352 len = p - gstr;
1353 else
1354 len = strlen(gstr);
1355 /*
1356 * The lookup of a the string will cover all engines so keep a note
1357 * of the implementation.
1358 */
1359
1360 ameth = EVP_PKEY_asn1_find_str(&tmpeng, gstr, len);
1361
2234212c 1362 if (ameth == NULL) {
7e1b7485 1363 BIO_printf(bio_err, "Unknown algorithm %.*s\n", len, gstr);
0f113f3e
MC
1364 return NULL;
1365 }
1366
1367 EVP_PKEY_asn1_get0_info(NULL, pkey_type, NULL, NULL, NULL, ameth);
01b8b3c7 1368#ifndef OPENSSL_NO_ENGINE
7c96dbcd 1369 ENGINE_finish(tmpeng);
01b8b3c7 1370#endif
0f113f3e 1371 if (*pkey_type == EVP_PKEY_RSA) {
2234212c 1372 if (p != NULL) {
0f113f3e
MC
1373 keylen = atol(p + 1);
1374 *pkeylen = keylen;
2234212c 1375 } else {
0f113f3e 1376 keylen = *pkeylen;
2234212c
PY
1377 }
1378 } else if (p != NULL) {
0f113f3e 1379 paramfile = p + 1;
2234212c 1380 }
0f113f3e
MC
1381 }
1382
2234212c 1383 if (paramfile != NULL) {
0f113f3e 1384 pbio = BIO_new_file(paramfile, "r");
2234212c 1385 if (pbio == NULL) {
7e1b7485 1386 BIO_printf(bio_err, "Can't open parameter file %s\n", paramfile);
0f113f3e
MC
1387 return NULL;
1388 }
1389 param = PEM_read_bio_Parameters(pbio, NULL);
1390
2234212c 1391 if (param == NULL) {
0f113f3e 1392 X509 *x;
2234212c 1393
0f113f3e
MC
1394 (void)BIO_reset(pbio);
1395 x = PEM_read_bio_X509(pbio, NULL, NULL, NULL);
2234212c 1396 if (x != NULL) {
0f113f3e
MC
1397 param = X509_get_pubkey(x);
1398 X509_free(x);
1399 }
1400 }
1401
1402 BIO_free(pbio);
1403
2234212c 1404 if (param == NULL) {
7e1b7485 1405 BIO_printf(bio_err, "Error reading parameter file %s\n", paramfile);
0f113f3e
MC
1406 return NULL;
1407 }
2234212c 1408 if (*pkey_type == -1) {
0f113f3e 1409 *pkey_type = EVP_PKEY_id(param);
2234212c 1410 } else if (*pkey_type != EVP_PKEY_base_id(param)) {
7e1b7485 1411 BIO_printf(bio_err, "Key Type does not match parameters\n");
0f113f3e
MC
1412 EVP_PKEY_free(param);
1413 return NULL;
1414 }
1415 }
1416
2234212c 1417 if (palgnam != NULL) {
0f113f3e
MC
1418 const EVP_PKEY_ASN1_METHOD *ameth;
1419 ENGINE *tmpeng;
1420 const char *anam;
2234212c 1421
0f113f3e 1422 ameth = EVP_PKEY_asn1_find(&tmpeng, *pkey_type);
2234212c 1423 if (ameth == NULL) {
7e1b7485 1424 BIO_puts(bio_err, "Internal error: can't find key algorithm\n");
0f113f3e
MC
1425 return NULL;
1426 }
1427 EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, &anam, ameth);
7644a9ae 1428 *palgnam = OPENSSL_strdup(anam);
01b8b3c7 1429#ifndef OPENSSL_NO_ENGINE
7c96dbcd 1430 ENGINE_finish(tmpeng);
01b8b3c7 1431#endif
0f113f3e
MC
1432 }
1433
2234212c 1434 if (param != NULL) {
0f113f3e
MC
1435 gctx = EVP_PKEY_CTX_new(param, keygen_engine);
1436 *pkeylen = EVP_PKEY_bits(param);
1437 EVP_PKEY_free(param);
2234212c 1438 } else {
0f113f3e 1439 gctx = EVP_PKEY_CTX_new_id(*pkey_type, keygen_engine);
2234212c 1440 }
0f113f3e 1441
96487cdd 1442 if (gctx == NULL) {
7e1b7485
RS
1443 BIO_puts(bio_err, "Error allocating keygen context\n");
1444 ERR_print_errors(bio_err);
0f113f3e
MC
1445 return NULL;
1446 }
1447
1448 if (EVP_PKEY_keygen_init(gctx) <= 0) {
7e1b7485
RS
1449 BIO_puts(bio_err, "Error initializing keygen context\n");
1450 ERR_print_errors(bio_err);
69ac182d 1451 EVP_PKEY_CTX_free(gctx);
0f113f3e
MC
1452 return NULL;
1453 }
d4f0339c 1454#ifndef OPENSSL_NO_RSA
0f113f3e
MC
1455 if ((*pkey_type == EVP_PKEY_RSA) && (keylen != -1)) {
1456 if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx, keylen) <= 0) {
7e1b7485
RS
1457 BIO_puts(bio_err, "Error setting RSA keysize\n");
1458 ERR_print_errors(bio_err);
0f113f3e
MC
1459 EVP_PKEY_CTX_free(gctx);
1460 return NULL;
1461 }
1462 }
d4f0339c 1463#endif
959e8dfe 1464
0f113f3e
MC
1465 return gctx;
1466}
959e8dfe
DSH
1467
1468static int genpkey_cb(EVP_PKEY_CTX *ctx)
0f113f3e
MC
1469{
1470 char c = '*';
1471 BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
1472 int p;
1473 p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
1474 if (p == 0)
1475 c = '.';
1476 if (p == 1)
1477 c = '+';
1478 if (p == 2)
1479 c = '*';
1480 if (p == 3)
1481 c = '\n';
1482 BIO_write(b, &c, 1);
1483 (void)BIO_flush(b);
1484 return 1;
1485}
cdb182b5 1486
7e1b7485 1487static int do_sign_init(EVP_MD_CTX *ctx, EVP_PKEY *pkey,
0f113f3e
MC
1488 const EVP_MD *md, STACK_OF(OPENSSL_STRING) *sigopts)
1489{
1490 EVP_PKEY_CTX *pkctx = NULL;
1491 int i;
7e1b7485 1492
6e59a892
RL
1493 if (ctx == NULL)
1494 return 0;
0f113f3e
MC
1495 if (!EVP_DigestSignInit(ctx, &pkctx, md, NULL, pkey))
1496 return 0;
1497 for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
1498 char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
1499 if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
7e1b7485 1500 BIO_printf(bio_err, "parameter error \"%s\"\n", sigopt);
0f113f3e
MC
1501 ERR_print_errors(bio_err);
1502 return 0;
1503 }
1504 }
1505 return 1;
1506}
cdb182b5 1507
7e1b7485 1508int do_X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md,
0f113f3e
MC
1509 STACK_OF(OPENSSL_STRING) *sigopts)
1510{
1511 int rv;
bfb0641f 1512 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
7e1b7485 1513
6e59a892 1514 rv = do_sign_init(mctx, pkey, md, sigopts);
0f113f3e 1515 if (rv > 0)
6e59a892 1516 rv = X509_sign_ctx(x, mctx);
c6aca19b 1517 EVP_MD_CTX_free(mctx);
0f113f3e
MC
1518 return rv > 0 ? 1 : 0;
1519}
cdb182b5 1520
7e1b7485 1521int do_X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md,
0f113f3e
MC
1522 STACK_OF(OPENSSL_STRING) *sigopts)
1523{
1524 int rv;
bfb0641f 1525 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
6e59a892 1526 rv = do_sign_init(mctx, pkey, md, sigopts);
0f113f3e 1527 if (rv > 0)
6e59a892 1528 rv = X509_REQ_sign_ctx(x, mctx);
c6aca19b 1529 EVP_MD_CTX_free(mctx);
0f113f3e
MC
1530 return rv > 0 ? 1 : 0;
1531}
4c623cdd 1532
7e1b7485 1533int do_X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md,
0f113f3e
MC
1534 STACK_OF(OPENSSL_STRING) *sigopts)
1535{
1536 int rv;
bfb0641f 1537 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
6e59a892 1538 rv = do_sign_init(mctx, pkey, md, sigopts);
0f113f3e 1539 if (rv > 0)
6e59a892 1540 rv = X509_CRL_sign_ctx(x, mctx);
c6aca19b 1541 EVP_MD_CTX_free(mctx);
0f113f3e
MC
1542 return rv > 0 ? 1 : 0;
1543}