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