]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/genpkey.c
Copyright year updates
[thirdparty/openssl.git] / apps / genpkey.c
CommitLineData
0f113f3e 1/*
0ce7d1f3 2 * Copyright 2006-2024 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
a414fd67 18static int verbose = 1;
7d72dc78 19
7c9a7cf1 20static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
b4250010 21 OSSL_LIB_CTX *libctx, const char *propq);
7e1b7485 22typedef enum OPTION_choice {
b0f96018 23 OPT_COMMON,
7e1b7485 24 OPT_ENGINE, OPT_OUTFORM, OPT_OUT, OPT_PASS, OPT_PARAMFILE,
6bd4e3f2 25 OPT_ALGORITHM, OPT_PKEYOPT, OPT_GENPARAM, OPT_TEXT, OPT_CIPHER,
6c03fa21 26 OPT_VERBOSE, OPT_QUIET, OPT_CONFIG, OPT_OUTPUBKEY,
7698f80a 27 OPT_PROV_ENUM, OPT_R_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"},
a414fd67 38 {"verbose", OPT_VERBOSE, '-', "Output status while generating keys"},
dbd0244a 39 {"quiet", OPT_QUIET, '-', "Do not output status while generating keys"},
7e1b7485
RS
40 {"pkeyopt", OPT_PKEYOPT, 's',
41 "Set the public key algorithm option as opt:value"},
7c9a7cf1 42 OPT_CONFIG_OPTION,
5388f986
RS
43
44 OPT_SECTION("Output"),
6c03fa21
MB
45 {"out", OPT_OUT, '>', "Output (private key) file"},
46 {"outpubkey", OPT_OUTPUBKEY, '>', "Output public key file"},
5388f986
RS
47 {"outform", OPT_OUTFORM, 'F', "output format (DER or PEM)"},
48 {"pass", OPT_PASS, 's', "Output file pass phrase source"},
7e1b7485 49 {"genparam", OPT_GENPARAM, '-', "Generate parameters, not key"},
6c03fa21 50 {"text", OPT_TEXT, '-', "Print the private key in text"},
7e1b7485 51 {"", OPT_CIPHER, '-', "Cipher to use to encrypt the key"},
5388f986 52
6bd4e3f2 53 OPT_PROV_OPTIONS,
7698f80a 54 OPT_R_OPTIONS,
6bd4e3f2 55
9c3bcfa0 56 /* This is deliberately last. */
7e1b7485
RS
57 {OPT_HELP_STR, 1, 1,
58 "Order of options may be important! See the documentation.\n"},
59 {NULL}
60};
f5cda4cb 61
2c1ec72a 62static const char *param_datatype_2name(unsigned int type, int *ishex)
63{
64 *ishex = 0;
65
66 switch (type) {
67 case OSSL_PARAM_INTEGER: return "int";
68 case OSSL_PARAM_UNSIGNED_INTEGER: return "uint";
69 case OSSL_PARAM_REAL: return "float";
70 case OSSL_PARAM_OCTET_STRING: *ishex = 1; return "string";
71 case OSSL_PARAM_UTF8_STRING: return "string";
72 default:
73 return NULL;
74 }
75}
76
77static void show_gen_pkeyopt(const char *algname, OSSL_LIB_CTX *libctx, const char *propq)
78{
79 EVP_PKEY_CTX *ctx = NULL;
80 const OSSL_PARAM *params;
81 int i, ishex = 0;
82
83 if (algname == NULL)
84 return;
85 ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq);
86 if (ctx == NULL)
87 return;
88
89 if (EVP_PKEY_keygen_init(ctx) <= 0)
90 goto cleanup;
91 params = EVP_PKEY_CTX_settable_params(ctx);
92 if (params == NULL)
93 goto cleanup;
94
95 BIO_printf(bio_err, "\nThe possible -pkeyopt arguments are:\n");
96 for (i = 0; params[i].key != NULL; ++i) {
97 const char *name = param_datatype_2name(params[i].data_type, &ishex);
98
99 if (name != NULL)
100 BIO_printf(bio_err, " %s%s:%s\n", ishex ? "hex" : "", params[i].key, name);
101 }
102cleanup:
103 EVP_PKEY_CTX_free(ctx);
104}
105
7e1b7485 106int genpkey_main(int argc, char **argv)
0f113f3e 107{
7c9a7cf1 108 CONF *conf = NULL;
6c03fa21 109 BIO *in = NULL, *out = NULL, *outpubkey = NULL;
7e1b7485 110 ENGINE *e = NULL;
0f113f3e
MC
111 EVP_PKEY *pkey = NULL;
112 EVP_PKEY_CTX *ctx = NULL;
182717bd 113 char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog, *p;
6c03fa21 114 char *outpubkeyfile = NULL;
182717bd 115 const char *ciphername = NULL, *paramfile = NULL, *algname = NULL;
606a417f 116 EVP_CIPHER *cipher = NULL;
7e1b7485
RS
117 OPTION_CHOICE o;
118 int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0;
f7d2427a 119 int private = 0, i;
b4250010 120 OSSL_LIB_CTX *libctx = app_get0_libctx();
182717bd 121 STACK_OF(OPENSSL_STRING) *keyopt = NULL;
7e1b7485 122
2c272447 123 opt_set_unknown_name("cipher");
7e1b7485 124 prog = opt_init(argc, argv, genpkey_options);
182717bd
RS
125 keyopt = sk_OPENSSL_STRING_new_null();
126 if (keyopt == NULL)
127 goto end;
7e1b7485
RS
128 while ((o = opt_next()) != OPT_EOF) {
129 switch (o) {
130 case OPT_EOF:
131 case OPT_ERR:
132 opthelp:
133 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
134 goto end;
135 case OPT_HELP:
136 ret = 0;
137 opt_help(genpkey_options);
2c1ec72a 138 show_gen_pkeyopt(algname, libctx, app_get0_propq());
7e1b7485
RS
139 goto end;
140 case OPT_OUTFORM:
141 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
142 goto opthelp;
143 break;
144 case OPT_OUT:
145 outfile = opt_arg();
146 break;
6c03fa21
MB
147 case OPT_OUTPUBKEY:
148 outpubkeyfile = opt_arg();
149 break;
7e1b7485
RS
150 case OPT_PASS:
151 passarg = opt_arg();
152 break;
7e1b7485
RS
153 case OPT_ENGINE:
154 e = setup_engine(opt_arg(), 0);
155 break;
7e1b7485 156 case OPT_PARAMFILE:
0f113f3e 157 if (do_param == 1)
7e1b7485 158 goto opthelp;
182717bd 159 paramfile = opt_arg();
7e1b7485
RS
160 break;
161 case OPT_ALGORITHM:
182717bd 162 algname = opt_arg();
7e1b7485
RS
163 break;
164 case OPT_PKEYOPT:
182717bd 165 if (!sk_OPENSSL_STRING_push(keyopt, opt_arg()))
0f113f3e 166 goto end;
7e1b7485 167 break;
7d72dc78 168 case OPT_QUIET:
a414fd67
PP
169 verbose = 0;
170 break;
171 case OPT_VERBOSE:
172 verbose = 1;
7d72dc78 173 break;
7e1b7485 174 case OPT_GENPARAM:
0f113f3e 175 do_param = 1;
7e1b7485
RS
176 break;
177 case OPT_TEXT:
0f113f3e 178 text = 1;
7e1b7485
RS
179 break;
180 case OPT_CIPHER:
182717bd 181 ciphername = opt_unknown();
6bd4e3f2 182 break;
7c9a7cf1
SL
183 case OPT_CONFIG:
184 conf = app_load_config_modules(opt_arg());
185 if (conf == NULL)
186 goto end;
187 break;
6bd4e3f2
P
188 case OPT_PROV_CASES:
189 if (!opt_provider(o))
190 goto end;
191 break;
7698f80a
VD
192 case OPT_R_CASES:
193 if (!opt_rand(o))
194 goto end;
195 break;
0f113f3e 196 }
0f113f3e 197 }
021410ea
RS
198
199 /* No extra arguments. */
d9f07357 200 if (!opt_check_rest_arg(NULL))
03358517
KR
201 goto opthelp;
202
7698f80a
VD
203 if (!app_RAND_load())
204 goto end;
205
182717bd
RS
206 /* Fetch cipher, etc. */
207 if (paramfile != NULL) {
208 if (!init_keygen_file(&ctx, paramfile, e, libctx, app_get0_propq()))
209 goto end;
210 }
211 if (algname != NULL) {
212 if (!init_gen_str(&ctx, algname, e, do_param, libctx, app_get0_propq()))
213 goto end;
214 }
7e1b7485
RS
215 if (ctx == NULL)
216 goto opthelp;
0f113f3e 217
182717bd
RS
218 for (i = 0; i < sk_OPENSSL_STRING_num(keyopt); i++) {
219 p = sk_OPENSSL_STRING_value(keyopt, i);
220 if (pkey_ctrl_string(ctx, p) <= 0) {
221 BIO_printf(bio_err, "%s: Error setting %s parameter:\n", prog, p);
222 ERR_print_errors(bio_err);
223 goto end;
224 }
225 }
d9f07357
DDO
226 if (!opt_cipher(ciphername, &cipher))
227 goto opthelp;
228 if (ciphername != NULL && do_param == 1) {
229 BIO_printf(bio_err, "Cannot use cipher with -genparam option\n");
230 goto opthelp;
231 }
182717bd
RS
232
233 private = do_param ? 0 : 1;
234
7e1b7485 235 if (!app_passwd(passarg, NULL, &pass, NULL)) {
0f113f3e
MC
236 BIO_puts(bio_err, "Error getting password\n");
237 goto end;
238 }
239
bdd58d98 240 out = bio_open_owner(outfile, outformat, private);
7e1b7485
RS
241 if (out == NULL)
242 goto end;
0f113f3e 243
6c03fa21
MB
244 if (outpubkeyfile != NULL) {
245 outpubkey = bio_open_owner(outpubkeyfile, outformat, private);
246 if (outpubkey == NULL)
247 goto end;
248 }
249
a414fd67 250 if (verbose)
e1cd94f2 251 EVP_PKEY_CTX_set_cb(ctx, progress_cb);
0f113f3e
MC
252 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
253
a7e4ca5b
DDO
254 pkey = do_param ? app_paramgen(ctx, algname)
255 : app_keygen(ctx, algname, 0, 0 /* not verbose */);
8c040c08
BE
256 if (pkey == NULL)
257 goto end;
0f113f3e 258
2234212c 259 if (do_param) {
0f113f3e 260 rv = PEM_write_bio_Parameters(out, pkey);
2234212c 261 } else if (outformat == FORMAT_PEM) {
3b061a00 262 assert(private);
0f113f3e 263 rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
6c03fa21
MB
264 if (rv > 0 && outpubkey != NULL)
265 rv = PEM_write_bio_PUBKEY(outpubkey, pkey);
3b061a00
RS
266 } else if (outformat == FORMAT_ASN1) {
267 assert(private);
0f113f3e 268 rv = i2d_PrivateKey_bio(out, pkey);
6c03fa21
MB
269 if (rv > 0 && outpubkey != NULL)
270 rv = i2d_PUBKEY_bio(outpubkey, pkey);
3b061a00 271 } else {
0f113f3e
MC
272 BIO_printf(bio_err, "Bad format specified for key\n");
273 goto end;
274 }
275
466d30c0
NT
276 ret = 0;
277
0f113f3e 278 if (rv <= 0) {
6c03fa21 279 BIO_puts(bio_err, "Error writing key(s)\n");
466d30c0 280 ret = 1;
0f113f3e
MC
281 }
282
283 if (text) {
284 if (do_param)
285 rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
286 else
287 rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
288
289 if (rv <= 0) {
290 BIO_puts(bio_err, "Error printing key\n");
466d30c0 291 ret = 1;
0f113f3e
MC
292 }
293 }
294
0f113f3e 295 end:
182717bd 296 sk_OPENSSL_STRING_free(keyopt);
a7e4ca5b
DDO
297 if (ret != 0)
298 ERR_print_errors(bio_err);
c5ba2d99
RS
299 EVP_PKEY_free(pkey);
300 EVP_PKEY_CTX_free(ctx);
606a417f 301 EVP_CIPHER_free(cipher);
ca3a82c3 302 BIO_free_all(out);
6c03fa21 303 BIO_free_all(outpubkey);
0f113f3e 304 BIO_free(in);
dd1abd44 305 release_engine(e);
b548a1f1 306 OPENSSL_free(pass);
7c9a7cf1 307 NCONF_free(conf);
0f113f3e
MC
308 return ret;
309}
f5cda4cb 310
7c9a7cf1 311static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
b4250010 312 OSSL_LIB_CTX *libctx, const char *propq)
0f113f3e
MC
313{
314 BIO *pbio;
315 EVP_PKEY *pkey = NULL;
316 EVP_PKEY_CTX *ctx = NULL;
317 if (*pctx) {
7e1b7485 318 BIO_puts(bio_err, "Parameters already set!\n");
0f113f3e
MC
319 return 0;
320 }
321
322 pbio = BIO_new_file(file, "r");
12a765a5 323 if (pbio == NULL) {
7e1b7485 324 BIO_printf(bio_err, "Can't open parameter file %s\n", file);
0f113f3e
MC
325 return 0;
326 }
327
3d63348a 328 pkey = PEM_read_bio_Parameters_ex(pbio, NULL, libctx, propq);
0f113f3e
MC
329 BIO_free(pbio);
330
12a765a5 331 if (pkey == NULL) {
0f113f3e
MC
332 BIO_printf(bio_err, "Error reading parameter file %s\n", file);
333 return 0;
334 }
335
7c9a7cf1
SL
336 if (e != NULL)
337 ctx = EVP_PKEY_CTX_new(pkey, e);
338 else
339 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
96487cdd 340 if (ctx == NULL)
0f113f3e
MC
341 goto err;
342 if (EVP_PKEY_keygen_init(ctx) <= 0)
343 goto err;
344 EVP_PKEY_free(pkey);
345 *pctx = ctx;
346 return 1;
347
348 err:
7e1b7485
RS
349 BIO_puts(bio_err, "Error initializing context\n");
350 ERR_print_errors(bio_err);
c5ba2d99
RS
351 EVP_PKEY_CTX_free(ctx);
352 EVP_PKEY_free(pkey);
0f113f3e
MC
353 return 0;
354
355}
f5cda4cb 356
7e1b7485 357int init_gen_str(EVP_PKEY_CTX **pctx,
7c9a7cf1 358 const char *algname, ENGINE *e, int do_param,
b4250010 359 OSSL_LIB_CTX *libctx, const char *propq)
0f113f3e
MC
360{
361 EVP_PKEY_CTX *ctx = NULL;
0f113f3e 362 int pkey_id;
01b8b3c7 363
0f113f3e 364 if (*pctx) {
7e1b7485 365 BIO_puts(bio_err, "Algorithm already set!\n");
0f113f3e
MC
366 return 0;
367 }
b3c6a331 368
0f386f2e
MC
369 pkey_id = get_legacy_pkey_id(libctx, algname, e);
370 if (pkey_id != NID_undef)
7c9a7cf1 371 ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
0f386f2e 372 else
7c9a7cf1 373 ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq);
0f113f3e 374
0f386f2e 375 if (ctx == NULL)
0f113f3e
MC
376 goto err;
377 if (do_param) {
378 if (EVP_PKEY_paramgen_init(ctx) <= 0)
379 goto err;
380 } else {
381 if (EVP_PKEY_keygen_init(ctx) <= 0)
382 goto err;
383 }
384
385 *pctx = ctx;
386 return 1;
387
388 err:
7e1b7485
RS
389 BIO_printf(bio_err, "Error initializing %s context\n", algname);
390 ERR_print_errors(bio_err);
c5ba2d99 391 EVP_PKEY_CTX_free(ctx);
0f113f3e
MC
392 return 0;
393
394}
f5cda4cb 395