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