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