]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/genpkey.c
Fetch algorithm after loading providers
[thirdparty/openssl.git] / apps / genpkey.c
CommitLineData
0f113f3e 1/*
4333b89f 2 * Copyright 2006-2021 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>
17
7c9a7cf1 18static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
b4250010 19 OSSL_LIB_CTX *libctx, const char *propq);
f5cda4cb
DSH
20static int genpkey_cb(EVP_PKEY_CTX *ctx);
21
7e1b7485
RS
22typedef enum OPTION_choice {
23 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
24 OPT_ENGINE, OPT_OUTFORM, OPT_OUT, OPT_PASS, OPT_PARAMFILE,
6bd4e3f2 25 OPT_ALGORITHM, OPT_PKEYOPT, OPT_GENPARAM, OPT_TEXT, OPT_CIPHER,
7c9a7cf1 26 OPT_CONFIG,
6bd4e3f2 27 OPT_PROV_ENUM
7e1b7485
RS
28} OPTION_CHOICE;
29
44c83ebd 30const 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"},
7c9a7cf1 40 OPT_CONFIG_OPTION,
5388f986
RS
41
42 OPT_SECTION("Output"),
43 {"out", OPT_OUT, '>', "Output file"},
44 {"outform", OPT_OUTFORM, 'F', "output format (DER or PEM)"},
45 {"pass", OPT_PASS, 's', "Output file pass phrase source"},
7e1b7485
RS
46 {"genparam", OPT_GENPARAM, '-', "Generate parameters, not key"},
47 {"text", OPT_TEXT, '-', "Print the in text"},
48 {"", OPT_CIPHER, '-', "Cipher to use to encrypt the key"},
5388f986 49
6bd4e3f2
P
50 OPT_PROV_OPTIONS,
51
9c3bcfa0 52 /* This is deliberately last. */
7e1b7485
RS
53 {OPT_HELP_STR, 1, 1,
54 "Order of options may be important! See the documentation.\n"},
55 {NULL}
56};
f5cda4cb 57
7e1b7485 58int genpkey_main(int argc, char **argv)
0f113f3e 59{
7c9a7cf1 60 CONF *conf = NULL;
0f113f3e 61 BIO *in = NULL, *out = NULL;
7e1b7485 62 ENGINE *e = NULL;
0f113f3e
MC
63 EVP_PKEY *pkey = NULL;
64 EVP_PKEY_CTX *ctx = NULL;
7e1b7485
RS
65 char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog;
66 const EVP_CIPHER *cipher = NULL;
67 OPTION_CHOICE o;
68 int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0;
3b061a00 69 int private = 0;
b4250010 70 OSSL_LIB_CTX *libctx = app_get0_libctx();
7e1b7485
RS
71
72 prog = opt_init(argc, argv, genpkey_options);
73 while ((o = opt_next()) != OPT_EOF) {
74 switch (o) {
75 case OPT_EOF:
76 case OPT_ERR:
77 opthelp:
78 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
79 goto end;
80 case OPT_HELP:
81 ret = 0;
82 opt_help(genpkey_options);
83 goto end;
84 case OPT_OUTFORM:
85 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
86 goto opthelp;
87 break;
88 case OPT_OUT:
89 outfile = opt_arg();
90 break;
7e1b7485
RS
91 case OPT_PASS:
92 passarg = opt_arg();
93 break;
7e1b7485
RS
94 case OPT_ENGINE:
95 e = setup_engine(opt_arg(), 0);
96 break;
7e1b7485 97 case OPT_PARAMFILE:
0f113f3e 98 if (do_param == 1)
7e1b7485 99 goto opthelp;
7dc67708 100 if (!init_keygen_file(&ctx, opt_arg(), e, libctx, app_get0_propq()))
0f113f3e 101 goto end;
7e1b7485
RS
102 break;
103 case OPT_ALGORITHM:
7dc67708 104 if (!init_gen_str(&ctx, opt_arg(), e, do_param, libctx, app_get0_propq()))
0f113f3e 105 goto end;
7e1b7485
RS
106 break;
107 case OPT_PKEYOPT:
108 if (ctx == NULL) {
109 BIO_printf(bio_err, "%s: No keytype specified.\n", prog);
110 goto opthelp;
111 }
112 if (pkey_ctrl_string(ctx, opt_arg()) <= 0) {
113 BIO_printf(bio_err,
114 "%s: Error setting %s parameter:\n",
115 prog, opt_arg());
0f113f3e
MC
116 ERR_print_errors(bio_err);
117 goto end;
118 }
7e1b7485
RS
119 break;
120 case OPT_GENPARAM:
f85a9d26
SL
121 if (ctx != NULL) {
122 BIO_printf(bio_err,
123 "%s: '-genparam' option must be set before"
124 " the '-algorithm' option.\n", prog);
7e1b7485 125 goto opthelp;
f85a9d26 126 }
0f113f3e 127 do_param = 1;
7e1b7485
RS
128 break;
129 case OPT_TEXT:
0f113f3e 130 text = 1;
7e1b7485
RS
131 break;
132 case OPT_CIPHER:
133 if (!opt_cipher(opt_unknown(), &cipher)
134 || do_param == 1)
135 goto opthelp;
49c9c1b3
DO
136 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE ||
137 EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE ||
138 EVP_CIPHER_mode(cipher) == EVP_CIPH_XTS_MODE ||
139 EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE) {
140 BIO_printf(bio_err, "%s: cipher mode not supported\n", prog);
141 goto end;
142 }
6bd4e3f2 143 break;
7c9a7cf1
SL
144 case OPT_CONFIG:
145 conf = app_load_config_modules(opt_arg());
146 if (conf == NULL)
147 goto end;
148 break;
6bd4e3f2
P
149 case OPT_PROV_CASES:
150 if (!opt_provider(o))
151 goto end;
152 break;
0f113f3e 153 }
0f113f3e 154 }
021410ea
RS
155
156 /* No extra arguments. */
7e1b7485 157 argc = opt_num_rest();
03358517
KR
158 if (argc != 0)
159 goto opthelp;
160
3b061a00 161 private = do_param ? 0 : 1;
0f113f3e 162
7e1b7485
RS
163 if (ctx == NULL)
164 goto opthelp;
0f113f3e 165
7e1b7485 166 if (!app_passwd(passarg, NULL, &pass, NULL)) {
0f113f3e
MC
167 BIO_puts(bio_err, "Error getting password\n");
168 goto end;
169 }
170
bdd58d98 171 out = bio_open_owner(outfile, outformat, private);
7e1b7485
RS
172 if (out == NULL)
173 goto end;
0f113f3e
MC
174
175 EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
176 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
177
178 if (do_param) {
179 if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
180 BIO_puts(bio_err, "Error generating parameters\n");
181 ERR_print_errors(bio_err);
182 goto end;
183 }
184 } else {
185 if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
186 BIO_puts(bio_err, "Error generating key\n");
187 ERR_print_errors(bio_err);
188 goto end;
189 }
190 }
191
2234212c 192 if (do_param) {
0f113f3e 193 rv = PEM_write_bio_Parameters(out, pkey);
2234212c 194 } else if (outformat == FORMAT_PEM) {
3b061a00 195 assert(private);
0f113f3e 196 rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
3b061a00
RS
197 } else if (outformat == FORMAT_ASN1) {
198 assert(private);
0f113f3e 199 rv = i2d_PrivateKey_bio(out, pkey);
3b061a00 200 } else {
0f113f3e
MC
201 BIO_printf(bio_err, "Bad format specified for key\n");
202 goto end;
203 }
204
466d30c0
NT
205 ret = 0;
206
0f113f3e
MC
207 if (rv <= 0) {
208 BIO_puts(bio_err, "Error writing key\n");
209 ERR_print_errors(bio_err);
466d30c0 210 ret = 1;
0f113f3e
MC
211 }
212
213 if (text) {
214 if (do_param)
215 rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
216 else
217 rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
218
219 if (rv <= 0) {
220 BIO_puts(bio_err, "Error printing key\n");
221 ERR_print_errors(bio_err);
466d30c0 222 ret = 1;
0f113f3e
MC
223 }
224 }
225
0f113f3e 226 end:
c5ba2d99
RS
227 EVP_PKEY_free(pkey);
228 EVP_PKEY_CTX_free(ctx);
ca3a82c3 229 BIO_free_all(out);
0f113f3e 230 BIO_free(in);
dd1abd44 231 release_engine(e);
b548a1f1 232 OPENSSL_free(pass);
7c9a7cf1 233 NCONF_free(conf);
0f113f3e
MC
234 return ret;
235}
f5cda4cb 236
7c9a7cf1 237static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
b4250010 238 OSSL_LIB_CTX *libctx, const char *propq)
0f113f3e
MC
239{
240 BIO *pbio;
241 EVP_PKEY *pkey = NULL;
242 EVP_PKEY_CTX *ctx = NULL;
243 if (*pctx) {
7e1b7485 244 BIO_puts(bio_err, "Parameters already set!\n");
0f113f3e
MC
245 return 0;
246 }
247
248 pbio = BIO_new_file(file, "r");
12a765a5 249 if (pbio == NULL) {
7e1b7485 250 BIO_printf(bio_err, "Can't open parameter file %s\n", file);
0f113f3e
MC
251 return 0;
252 }
253
3d63348a 254 pkey = PEM_read_bio_Parameters_ex(pbio, NULL, libctx, propq);
0f113f3e
MC
255 BIO_free(pbio);
256
12a765a5 257 if (pkey == NULL) {
0f113f3e
MC
258 BIO_printf(bio_err, "Error reading parameter file %s\n", file);
259 return 0;
260 }
261
7c9a7cf1
SL
262 if (e != NULL)
263 ctx = EVP_PKEY_CTX_new(pkey, e);
264 else
265 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
96487cdd 266 if (ctx == NULL)
0f113f3e
MC
267 goto err;
268 if (EVP_PKEY_keygen_init(ctx) <= 0)
269 goto err;
270 EVP_PKEY_free(pkey);
271 *pctx = ctx;
272 return 1;
273
274 err:
7e1b7485
RS
275 BIO_puts(bio_err, "Error initializing context\n");
276 ERR_print_errors(bio_err);
c5ba2d99
RS
277 EVP_PKEY_CTX_free(ctx);
278 EVP_PKEY_free(pkey);
0f113f3e
MC
279 return 0;
280
281}
f5cda4cb 282
7e1b7485 283int init_gen_str(EVP_PKEY_CTX **pctx,
7c9a7cf1 284 const char *algname, ENGINE *e, int do_param,
b4250010 285 OSSL_LIB_CTX *libctx, const char *propq)
0f113f3e
MC
286{
287 EVP_PKEY_CTX *ctx = NULL;
0f113f3e 288 int pkey_id;
01b8b3c7 289
0f113f3e 290 if (*pctx) {
7e1b7485 291 BIO_puts(bio_err, "Algorithm already set!\n");
0f113f3e
MC
292 return 0;
293 }
b3c6a331 294
0f386f2e
MC
295 pkey_id = get_legacy_pkey_id(libctx, algname, e);
296 if (pkey_id != NID_undef)
7c9a7cf1 297 ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
0f386f2e 298 else
7c9a7cf1 299 ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq);
0f113f3e 300
0f386f2e 301 if (ctx == NULL)
0f113f3e
MC
302 goto err;
303 if (do_param) {
304 if (EVP_PKEY_paramgen_init(ctx) <= 0)
305 goto err;
306 } else {
307 if (EVP_PKEY_keygen_init(ctx) <= 0)
308 goto err;
309 }
310
311 *pctx = ctx;
312 return 1;
313
314 err:
7e1b7485
RS
315 BIO_printf(bio_err, "Error initializing %s context\n", algname);
316 ERR_print_errors(bio_err);
c5ba2d99 317 EVP_PKEY_CTX_free(ctx);
0f113f3e
MC
318 return 0;
319
320}
f5cda4cb
DSH
321
322static int genpkey_cb(EVP_PKEY_CTX *ctx)
0f113f3e
MC
323{
324 char c = '*';
325 BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
326 int p;
327 p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
328 if (p == 0)
329 c = '.';
330 if (p == 1)
331 c = '+';
332 if (p == 2)
333 c = '*';
334 if (p == 3)
335 c = '\n';
336 BIO_write(b, &c, 1);
337 (void)BIO_flush(b);
338 return 1;
339}