1 /* $OpenBSD: genpkey.c,v 1.13 2019/07/14 03:30:46 guenther Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
64 #include <openssl/err.h>
65 #include <openssl/evp.h>
66 #include <openssl/pem.h>
68 static int init_keygen_file(BIO * err, EVP_PKEY_CTX **pctx, const char *file);
69 static int genpkey_cb(EVP_PKEY_CTX * ctx);
72 const EVP_CIPHER *cipher;
82 genpkey_opt_algorithm(char *arg)
84 if (!init_gen_str(bio_err, genpkey_config.ctx, arg,
85 genpkey_config.do_param))
92 genpkey_opt_cipher(int argc, char **argv, int *argsused)
99 if (genpkey_config.do_param == 1)
102 if (strcmp(name, "none") == 0) {
103 genpkey_config.cipher = NULL;
108 if ((genpkey_config.cipher = EVP_get_cipherbyname(name)) != NULL) {
117 genpkey_opt_paramfile(char *arg)
119 if (genpkey_config.do_param == 1)
121 if (!init_keygen_file(bio_err, genpkey_config.ctx, arg))
128 genpkey_opt_pkeyopt(char *arg)
130 if (*genpkey_config.ctx == NULL) {
131 BIO_puts(bio_err, "No keytype specified\n");
135 if (pkey_ctrl_string(*genpkey_config.ctx, arg) <= 0) {
136 BIO_puts(bio_err, "parameter setting error\n");
137 ERR_print_errors(bio_err);
144 static const struct option genpkey_options[] = {
148 .desc = "Public key algorithm to use (must precede -pkeyopt)",
149 .type = OPTION_ARG_FUNC,
150 .opt.argfunc = genpkey_opt_algorithm,
154 .desc = "Generate a set of parameters instead of a private key",
156 .opt.flag = &genpkey_config.do_param,
161 .desc = "Output file to write to (default stdout)",
163 .opt.arg = &genpkey_config.outfile,
168 .desc = "Output format (DER or PEM)",
169 .type = OPTION_ARG_FORMAT,
170 .opt.value = &genpkey_config.outformat,
175 .desc = "File to load public key algorithm parameters from\n"
176 "(must precede -pkeyopt)",
177 .type = OPTION_ARG_FUNC,
178 .opt.argfunc = genpkey_opt_paramfile,
183 .desc = "Output file password source",
185 .opt.arg = &genpkey_config.passarg,
189 .argname = "opt:value",
190 .desc = "Set public key algorithm option to the given value",
191 .type = OPTION_ARG_FUNC,
192 .opt.argfunc = genpkey_opt_pkeyopt,
196 .desc = "Print the private/public key in human readable form",
198 .opt.flag = &genpkey_config.text,
202 .type = OPTION_ARGV_FUNC,
203 .opt.argvfunc = genpkey_opt_cipher,
212 "usage: genpkey [-algorithm alg] [cipher] [-genparam] [-out file]\n"
213 " [-outform der | pem] [-paramfile file] [-pass arg]\n"
214 " [-pkeyopt opt:value] [-text]\n\n");
215 options_usage(genpkey_options);
219 genpkey_main(int argc, char **argv)
221 BIO *in = NULL, *out = NULL;
222 EVP_PKEY_CTX *ctx = NULL;
223 EVP_PKEY *pkey = NULL;
227 if (single_execution) {
228 if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
234 memset(&genpkey_config, 0, sizeof(genpkey_config));
235 genpkey_config.ctx = &ctx;
236 genpkey_config.outformat = FORMAT_PEM;
238 if (options_parse(argc, argv, genpkey_options, NULL, NULL) != 0) {
248 if (!app_passwd(bio_err, genpkey_config.passarg, NULL, &pass, NULL)) {
249 BIO_puts(bio_err, "Error getting password\n");
252 if (genpkey_config.outfile != NULL) {
253 if ((out = BIO_new_file(genpkey_config.outfile, "wb")) ==
255 BIO_printf(bio_err, "Can't open output file %s\n",
256 genpkey_config.outfile);
260 out = BIO_new_fp(stdout, BIO_NOCLOSE);
263 EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
264 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
266 if (genpkey_config.do_param) {
267 if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
268 BIO_puts(bio_err, "Error generating parameters\n");
269 ERR_print_errors(bio_err);
273 if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
274 BIO_puts(bio_err, "Error generating key\n");
275 ERR_print_errors(bio_err);
280 if (genpkey_config.do_param)
281 rv = PEM_write_bio_Parameters(out, pkey);
282 else if (genpkey_config.outformat == FORMAT_PEM)
283 rv = PEM_write_bio_PrivateKey(out, pkey, genpkey_config.cipher,
284 NULL, 0, NULL, pass);
285 else if (genpkey_config.outformat == FORMAT_ASN1)
286 rv = i2d_PrivateKey_bio(out, pkey);
288 BIO_printf(bio_err, "Bad format specified for key\n");
293 BIO_puts(bio_err, "Error writing key\n");
294 ERR_print_errors(bio_err);
296 if (genpkey_config.text) {
297 if (genpkey_config.do_param)
298 rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
300 rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
303 BIO_puts(bio_err, "Error printing key\n");
304 ERR_print_errors(bio_err);
311 EVP_PKEY_CTX_free(ctx);
320 init_keygen_file(BIO * err, EVP_PKEY_CTX ** pctx, const char *file)
323 EVP_PKEY *pkey = NULL;
324 EVP_PKEY_CTX *ctx = NULL;
326 BIO_puts(err, "Parameters already set!\n");
329 pbio = BIO_new_file(file, "r");
331 BIO_printf(err, "Can't open parameter file %s\n", file);
334 pkey = PEM_read_bio_Parameters(pbio, NULL);
338 BIO_printf(bio_err, "Error reading parameter file %s\n", file);
341 ctx = EVP_PKEY_CTX_new(pkey, NULL);
344 if (EVP_PKEY_keygen_init(ctx) <= 0)
351 BIO_puts(err, "Error initializing context\n");
352 ERR_print_errors(err);
353 EVP_PKEY_CTX_free(ctx);
360 init_gen_str(BIO * err, EVP_PKEY_CTX ** pctx, const char *algname, int do_param)
362 const EVP_PKEY_ASN1_METHOD *ameth;
363 EVP_PKEY_CTX *ctx = NULL;
367 BIO_puts(err, "Algorithm already set!\n");
370 ameth = EVP_PKEY_asn1_find_str(NULL, algname, -1);
373 BIO_printf(bio_err, "Algorithm %s not found\n", algname);
378 EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
379 ctx = EVP_PKEY_CTX_new_id(pkey_id, NULL);
384 if (EVP_PKEY_paramgen_init(ctx) <= 0)
387 if (EVP_PKEY_keygen_init(ctx) <= 0)
395 BIO_printf(err, "Error initializing %s context\n", algname);
396 ERR_print_errors(err);
397 EVP_PKEY_CTX_free(ctx);
403 genpkey_cb(EVP_PKEY_CTX * ctx)
406 BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
408 p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);