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