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