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