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