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