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