]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
6738bf14 | 2 | * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved. |
f5cda4cb | 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 | |
f5cda4cb | 8 | */ |
846e33c7 | 9 | |
f5cda4cb DSH |
10 | #include <stdio.h> |
11 | #include <string.h> | |
12 | #include "apps.h" | |
dab2cd68 | 13 | #include "progs.h" |
f5cda4cb DSH |
14 | #include <openssl/pem.h> |
15 | #include <openssl/err.h> | |
16 | #include <openssl/evp.h> | |
01b8b3c7 | 17 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 18 | # include <openssl/engine.h> |
01b8b3c7 | 19 | #endif |
f5cda4cb | 20 | |
7e1b7485 | 21 | static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e); |
f5cda4cb DSH |
22 | static int genpkey_cb(EVP_PKEY_CTX *ctx); |
23 | ||
7e1b7485 RS |
24 | typedef enum OPTION_choice { |
25 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
26 | OPT_ENGINE, OPT_OUTFORM, OPT_OUT, OPT_PASS, OPT_PARAMFILE, | |
27 | OPT_ALGORITHM, OPT_PKEYOPT, OPT_GENPARAM, OPT_TEXT, OPT_CIPHER | |
28 | } OPTION_CHOICE; | |
29 | ||
44c83ebd | 30 | const OPTIONS genpkey_options[] = { |
7e1b7485 RS |
31 | {"help", OPT_HELP, '-', "Display this summary"}, |
32 | {"out", OPT_OUT, '>', "Output file"}, | |
33 | {"outform", OPT_OUTFORM, 'F', "output format (DER or PEM)"}, | |
34 | {"pass", OPT_PASS, 's', "Output file pass phrase source"}, | |
35 | {"paramfile", OPT_PARAMFILE, '<', "Parameters file"}, | |
36 | {"algorithm", OPT_ALGORITHM, 's', "The public key algorithm"}, | |
37 | {"pkeyopt", OPT_PKEYOPT, 's', | |
38 | "Set the public key algorithm option as opt:value"}, | |
39 | {"genparam", OPT_GENPARAM, '-', "Generate parameters, not key"}, | |
40 | {"text", OPT_TEXT, '-', "Print the in text"}, | |
41 | {"", OPT_CIPHER, '-', "Cipher to use to encrypt the key"}, | |
42 | #ifndef OPENSSL_NO_ENGINE | |
43 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
44 | #endif | |
9c3bcfa0 | 45 | /* This is deliberately last. */ |
7e1b7485 RS |
46 | {OPT_HELP_STR, 1, 1, |
47 | "Order of options may be important! See the documentation.\n"}, | |
48 | {NULL} | |
49 | }; | |
f5cda4cb | 50 | |
7e1b7485 | 51 | int genpkey_main(int argc, char **argv) |
0f113f3e | 52 | { |
0f113f3e | 53 | BIO *in = NULL, *out = NULL; |
7e1b7485 | 54 | ENGINE *e = NULL; |
0f113f3e MC |
55 | EVP_PKEY *pkey = NULL; |
56 | EVP_PKEY_CTX *ctx = NULL; | |
7e1b7485 RS |
57 | char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog; |
58 | const EVP_CIPHER *cipher = NULL; | |
59 | OPTION_CHOICE o; | |
60 | int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0; | |
3b061a00 | 61 | int private = 0; |
7e1b7485 RS |
62 | |
63 | prog = opt_init(argc, argv, genpkey_options); | |
64 | while ((o = opt_next()) != OPT_EOF) { | |
65 | switch (o) { | |
66 | case OPT_EOF: | |
67 | case OPT_ERR: | |
68 | opthelp: | |
69 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
70 | goto end; | |
71 | case OPT_HELP: | |
72 | ret = 0; | |
73 | opt_help(genpkey_options); | |
74 | goto end; | |
75 | case OPT_OUTFORM: | |
76 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
77 | goto opthelp; | |
78 | break; | |
79 | case OPT_OUT: | |
80 | outfile = opt_arg(); | |
81 | break; | |
7e1b7485 RS |
82 | case OPT_PASS: |
83 | passarg = opt_arg(); | |
84 | break; | |
7e1b7485 RS |
85 | case OPT_ENGINE: |
86 | e = setup_engine(opt_arg(), 0); | |
87 | break; | |
7e1b7485 | 88 | case OPT_PARAMFILE: |
0f113f3e | 89 | if (do_param == 1) |
7e1b7485 RS |
90 | goto opthelp; |
91 | if (!init_keygen_file(&ctx, opt_arg(), e)) | |
0f113f3e | 92 | goto end; |
7e1b7485 RS |
93 | break; |
94 | case OPT_ALGORITHM: | |
95 | if (!init_gen_str(&ctx, opt_arg(), e, do_param)) | |
0f113f3e | 96 | goto end; |
7e1b7485 RS |
97 | break; |
98 | case OPT_PKEYOPT: | |
99 | if (ctx == NULL) { | |
100 | BIO_printf(bio_err, "%s: No keytype specified.\n", prog); | |
101 | goto opthelp; | |
102 | } | |
103 | if (pkey_ctrl_string(ctx, opt_arg()) <= 0) { | |
104 | BIO_printf(bio_err, | |
105 | "%s: Error setting %s parameter:\n", | |
106 | prog, opt_arg()); | |
0f113f3e MC |
107 | ERR_print_errors(bio_err); |
108 | goto end; | |
109 | } | |
7e1b7485 RS |
110 | break; |
111 | case OPT_GENPARAM: | |
112 | if (ctx != NULL) | |
113 | goto opthelp; | |
0f113f3e | 114 | do_param = 1; |
7e1b7485 RS |
115 | break; |
116 | case OPT_TEXT: | |
0f113f3e | 117 | text = 1; |
7e1b7485 RS |
118 | break; |
119 | case OPT_CIPHER: | |
120 | if (!opt_cipher(opt_unknown(), &cipher) | |
121 | || do_param == 1) | |
122 | goto opthelp; | |
0f113f3e | 123 | } |
0f113f3e | 124 | } |
7e1b7485 | 125 | argc = opt_num_rest(); |
03358517 KR |
126 | if (argc != 0) |
127 | goto opthelp; | |
128 | ||
3b061a00 | 129 | private = do_param ? 0 : 1; |
0f113f3e | 130 | |
7e1b7485 RS |
131 | if (ctx == NULL) |
132 | goto opthelp; | |
0f113f3e | 133 | |
7e1b7485 | 134 | if (!app_passwd(passarg, NULL, &pass, NULL)) { |
0f113f3e MC |
135 | BIO_puts(bio_err, "Error getting password\n"); |
136 | goto end; | |
137 | } | |
138 | ||
bdd58d98 | 139 | out = bio_open_owner(outfile, outformat, private); |
7e1b7485 RS |
140 | if (out == NULL) |
141 | goto end; | |
0f113f3e MC |
142 | |
143 | EVP_PKEY_CTX_set_cb(ctx, genpkey_cb); | |
144 | EVP_PKEY_CTX_set_app_data(ctx, bio_err); | |
145 | ||
146 | if (do_param) { | |
147 | if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) { | |
148 | BIO_puts(bio_err, "Error generating parameters\n"); | |
149 | ERR_print_errors(bio_err); | |
150 | goto end; | |
151 | } | |
152 | } else { | |
153 | if (EVP_PKEY_keygen(ctx, &pkey) <= 0) { | |
154 | BIO_puts(bio_err, "Error generating key\n"); | |
155 | ERR_print_errors(bio_err); | |
156 | goto end; | |
157 | } | |
158 | } | |
159 | ||
2234212c | 160 | if (do_param) { |
0f113f3e | 161 | rv = PEM_write_bio_Parameters(out, pkey); |
2234212c | 162 | } else if (outformat == FORMAT_PEM) { |
3b061a00 | 163 | assert(private); |
0f113f3e | 164 | rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass); |
3b061a00 RS |
165 | } else if (outformat == FORMAT_ASN1) { |
166 | assert(private); | |
0f113f3e | 167 | rv = i2d_PrivateKey_bio(out, pkey); |
3b061a00 | 168 | } else { |
0f113f3e MC |
169 | BIO_printf(bio_err, "Bad format specified for key\n"); |
170 | goto end; | |
171 | } | |
172 | ||
173 | if (rv <= 0) { | |
174 | BIO_puts(bio_err, "Error writing key\n"); | |
175 | ERR_print_errors(bio_err); | |
176 | } | |
177 | ||
178 | if (text) { | |
179 | if (do_param) | |
180 | rv = EVP_PKEY_print_params(out, pkey, 0, NULL); | |
181 | else | |
182 | rv = EVP_PKEY_print_private(out, pkey, 0, NULL); | |
183 | ||
184 | if (rv <= 0) { | |
185 | BIO_puts(bio_err, "Error printing key\n"); | |
186 | ERR_print_errors(bio_err); | |
187 | } | |
188 | } | |
189 | ||
190 | ret = 0; | |
191 | ||
192 | end: | |
c5ba2d99 RS |
193 | EVP_PKEY_free(pkey); |
194 | EVP_PKEY_CTX_free(ctx); | |
ca3a82c3 | 195 | BIO_free_all(out); |
0f113f3e | 196 | BIO_free(in); |
dd1abd44 | 197 | release_engine(e); |
b548a1f1 | 198 | OPENSSL_free(pass); |
0f113f3e MC |
199 | return ret; |
200 | } | |
f5cda4cb | 201 | |
7e1b7485 | 202 | static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e) |
0f113f3e MC |
203 | { |
204 | BIO *pbio; | |
205 | EVP_PKEY *pkey = NULL; | |
206 | EVP_PKEY_CTX *ctx = NULL; | |
207 | if (*pctx) { | |
7e1b7485 | 208 | BIO_puts(bio_err, "Parameters already set!\n"); |
0f113f3e MC |
209 | return 0; |
210 | } | |
211 | ||
212 | pbio = BIO_new_file(file, "r"); | |
213 | if (!pbio) { | |
7e1b7485 | 214 | BIO_printf(bio_err, "Can't open parameter file %s\n", file); |
0f113f3e MC |
215 | return 0; |
216 | } | |
217 | ||
218 | pkey = PEM_read_bio_Parameters(pbio, NULL); | |
219 | BIO_free(pbio); | |
220 | ||
221 | if (!pkey) { | |
222 | BIO_printf(bio_err, "Error reading parameter file %s\n", file); | |
223 | return 0; | |
224 | } | |
225 | ||
226 | ctx = EVP_PKEY_CTX_new(pkey, e); | |
96487cdd | 227 | if (ctx == NULL) |
0f113f3e MC |
228 | goto err; |
229 | if (EVP_PKEY_keygen_init(ctx) <= 0) | |
230 | goto err; | |
231 | EVP_PKEY_free(pkey); | |
232 | *pctx = ctx; | |
233 | return 1; | |
234 | ||
235 | err: | |
7e1b7485 RS |
236 | BIO_puts(bio_err, "Error initializing context\n"); |
237 | ERR_print_errors(bio_err); | |
c5ba2d99 RS |
238 | EVP_PKEY_CTX_free(ctx); |
239 | EVP_PKEY_free(pkey); | |
0f113f3e MC |
240 | return 0; |
241 | ||
242 | } | |
f5cda4cb | 243 | |
7e1b7485 | 244 | int init_gen_str(EVP_PKEY_CTX **pctx, |
0f113f3e MC |
245 | const char *algname, ENGINE *e, int do_param) |
246 | { | |
247 | EVP_PKEY_CTX *ctx = NULL; | |
248 | const EVP_PKEY_ASN1_METHOD *ameth; | |
249 | ENGINE *tmpeng = NULL; | |
250 | int pkey_id; | |
01b8b3c7 | 251 | |
0f113f3e | 252 | if (*pctx) { |
7e1b7485 | 253 | BIO_puts(bio_err, "Algorithm already set!\n"); |
0f113f3e MC |
254 | return 0; |
255 | } | |
b3c6a331 | 256 | |
0f113f3e | 257 | ameth = EVP_PKEY_asn1_find_str(&tmpeng, algname, -1); |
f5cda4cb | 258 | |
70531c14 | 259 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
260 | if (!ameth && e) |
261 | ameth = ENGINE_get_pkey_asn1_meth_str(e, algname, -1); | |
70531c14 | 262 | #endif |
b3c6a331 | 263 | |
0f113f3e MC |
264 | if (!ameth) { |
265 | BIO_printf(bio_err, "Algorithm %s not found\n", algname); | |
266 | return 0; | |
267 | } | |
f5cda4cb | 268 | |
0f113f3e | 269 | ERR_clear_error(); |
b3c6a331 | 270 | |
0f113f3e | 271 | EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth); |
01b8b3c7 | 272 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 273 | ENGINE_finish(tmpeng); |
01b8b3c7 | 274 | #endif |
0f113f3e MC |
275 | ctx = EVP_PKEY_CTX_new_id(pkey_id, e); |
276 | ||
277 | if (!ctx) | |
278 | goto err; | |
279 | if (do_param) { | |
280 | if (EVP_PKEY_paramgen_init(ctx) <= 0) | |
281 | goto err; | |
282 | } else { | |
283 | if (EVP_PKEY_keygen_init(ctx) <= 0) | |
284 | goto err; | |
285 | } | |
286 | ||
287 | *pctx = ctx; | |
288 | return 1; | |
289 | ||
290 | err: | |
7e1b7485 RS |
291 | BIO_printf(bio_err, "Error initializing %s context\n", algname); |
292 | ERR_print_errors(bio_err); | |
c5ba2d99 | 293 | EVP_PKEY_CTX_free(ctx); |
0f113f3e MC |
294 | return 0; |
295 | ||
296 | } | |
f5cda4cb DSH |
297 | |
298 | static int genpkey_cb(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
299 | { |
300 | char c = '*'; | |
301 | BIO *b = EVP_PKEY_CTX_get_app_data(ctx); | |
302 | int p; | |
303 | p = EVP_PKEY_CTX_get_keygen_info(ctx, 0); | |
304 | if (p == 0) | |
305 | c = '.'; | |
306 | if (p == 1) | |
307 | c = '+'; | |
308 | if (p == 2) | |
309 | c = '*'; | |
310 | if (p == 3) | |
311 | c = '\n'; | |
312 | BIO_write(b, &c, 1); | |
313 | (void)BIO_flush(b); | |
314 | return 1; | |
315 | } |