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