APPS: Replace 'OPT_ERR = -1, OPT_EOF = 0, OPT_HELP' by OPT_COMMON macro
[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 init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
19                             OSSL_LIB_CTX *libctx, const char *propq);
20 static int genpkey_cb(EVP_PKEY_CTX *ctx);
21
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_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     {"pkeyopt", OPT_PKEYOPT, 's',
39      "Set the public key algorithm option as opt:value"},
40      OPT_CONFIG_OPTION,
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"},
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"},
49
50     OPT_PROV_OPTIONS,
51
52     /* This is deliberately last. */
53     {OPT_HELP_STR, 1, 1,
54      "Order of options may be important!  See the documentation.\n"},
55     {NULL}
56 };
57
58 int genpkey_main(int argc, char **argv)
59 {
60     CONF *conf = NULL;
61     BIO *in = NULL, *out = NULL;
62     ENGINE *e = NULL;
63     EVP_PKEY *pkey = NULL;
64     EVP_PKEY_CTX *ctx = NULL;
65     char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog, *p;
66     const char *ciphername = NULL, *paramfile = NULL, *algname = NULL;
67     EVP_CIPHER *cipher = NULL;
68     OPTION_CHOICE o;
69     int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0;
70     int private = 0, i, m;
71     OSSL_LIB_CTX *libctx = app_get0_libctx();
72     STACK_OF(OPENSSL_STRING) *keyopt = NULL;
73
74     prog = opt_init(argc, argv, genpkey_options);
75     keyopt = sk_OPENSSL_STRING_new_null();
76     if (keyopt == NULL)
77         goto end;
78     while ((o = opt_next()) != OPT_EOF) {
79         switch (o) {
80         case OPT_EOF:
81         case OPT_ERR:
82  opthelp:
83             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
84             goto end;
85         case OPT_HELP:
86             ret = 0;
87             opt_help(genpkey_options);
88             goto end;
89         case OPT_OUTFORM:
90             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
91                 goto opthelp;
92             break;
93         case OPT_OUT:
94             outfile = opt_arg();
95             break;
96         case OPT_PASS:
97             passarg = opt_arg();
98             break;
99         case OPT_ENGINE:
100             e = setup_engine(opt_arg(), 0);
101             break;
102         case OPT_PARAMFILE:
103             if (do_param == 1)
104                 goto opthelp;
105             paramfile = opt_arg();
106             break;
107         case OPT_ALGORITHM:
108             algname = opt_arg();
109             break;
110         case OPT_PKEYOPT:
111             if (!sk_OPENSSL_STRING_push(keyopt, opt_arg()))
112                 goto end;
113             break;
114         case OPT_GENPARAM:
115             do_param = 1;
116             break;
117         case OPT_TEXT:
118             text = 1;
119             break;
120         case OPT_CIPHER:
121             ciphername = opt_unknown();
122             break;
123         case OPT_CONFIG:
124             conf = app_load_config_modules(opt_arg());
125             if (conf == NULL)
126                 goto end;
127             break;
128         case OPT_PROV_CASES:
129             if (!opt_provider(o))
130                 goto end;
131             break;
132         }
133     }
134
135     /* No extra arguments. */
136     argc = opt_num_rest();
137     if (argc != 0)
138         goto opthelp;
139
140     /* Fetch cipher, etc. */
141     if (paramfile != NULL) {
142         if (!init_keygen_file(&ctx, paramfile, e, libctx, app_get0_propq()))
143             goto end;
144     }
145     if (algname != NULL) {
146         if (!init_gen_str(&ctx, algname, e, do_param, libctx, app_get0_propq()))
147             goto end;
148     }
149     if (ctx == NULL)
150         goto opthelp;
151
152     for (i = 0; i < sk_OPENSSL_STRING_num(keyopt); i++) {
153         p = sk_OPENSSL_STRING_value(keyopt, i);
154         if (pkey_ctrl_string(ctx, p) <= 0) {
155             BIO_printf(bio_err, "%s: Error setting %s parameter:\n", prog, p);
156             ERR_print_errors(bio_err);
157             goto end;
158         }
159     }
160     if (ciphername != NULL) {
161         if (!opt_cipher(ciphername, &cipher) || do_param == 1)
162             goto opthelp;
163         m = EVP_CIPHER_mode(cipher);
164         if (m == EVP_CIPH_GCM_MODE || m == EVP_CIPH_CCM_MODE
165                 || m == EVP_CIPH_XTS_MODE || m == EVP_CIPH_OCB_MODE) {
166             BIO_printf(bio_err, "%s: cipher mode not supported\n", prog);
167             goto end;
168         }
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     EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
183     EVP_PKEY_CTX_set_app_data(ctx, bio_err);
184
185     if (do_param) {
186         if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
187             BIO_puts(bio_err, "Error generating parameters\n");
188             ERR_print_errors(bio_err);
189             goto end;
190         }
191     } else {
192         if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
193             BIO_puts(bio_err, "Error generating key\n");
194             ERR_print_errors(bio_err);
195             goto end;
196         }
197     }
198
199     if (do_param) {
200         rv = PEM_write_bio_Parameters(out, pkey);
201     } else if (outformat == FORMAT_PEM) {
202         assert(private);
203         rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
204     } else if (outformat == FORMAT_ASN1) {
205         assert(private);
206         rv = i2d_PrivateKey_bio(out, pkey);
207     } else {
208         BIO_printf(bio_err, "Bad format specified for key\n");
209         goto end;
210     }
211
212     ret = 0;
213
214     if (rv <= 0) {
215         BIO_puts(bio_err, "Error writing key\n");
216         ERR_print_errors(bio_err);
217         ret = 1;
218     }
219
220     if (text) {
221         if (do_param)
222             rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
223         else
224             rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
225
226         if (rv <= 0) {
227             BIO_puts(bio_err, "Error printing key\n");
228             ERR_print_errors(bio_err);
229             ret = 1;
230         }
231     }
232
233  end:
234     sk_OPENSSL_STRING_free(keyopt);
235     EVP_PKEY_free(pkey);
236     EVP_PKEY_CTX_free(ctx);
237     EVP_CIPHER_free(cipher);
238     BIO_free_all(out);
239     BIO_free(in);
240     release_engine(e);
241     OPENSSL_free(pass);
242     NCONF_free(conf);
243     return ret;
244 }
245
246 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
247                             OSSL_LIB_CTX *libctx, const char *propq)
248 {
249     BIO *pbio;
250     EVP_PKEY *pkey = NULL;
251     EVP_PKEY_CTX *ctx = NULL;
252     if (*pctx) {
253         BIO_puts(bio_err, "Parameters already set!\n");
254         return 0;
255     }
256
257     pbio = BIO_new_file(file, "r");
258     if (pbio == NULL) {
259         BIO_printf(bio_err, "Can't open parameter file %s\n", file);
260         return 0;
261     }
262
263     pkey = PEM_read_bio_Parameters_ex(pbio, NULL, libctx, propq);
264     BIO_free(pbio);
265
266     if (pkey == NULL) {
267         BIO_printf(bio_err, "Error reading parameter file %s\n", file);
268         return 0;
269     }
270
271     if (e != NULL)
272         ctx = EVP_PKEY_CTX_new(pkey, e);
273     else
274         ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
275     if (ctx == NULL)
276         goto err;
277     if (EVP_PKEY_keygen_init(ctx) <= 0)
278         goto err;
279     EVP_PKEY_free(pkey);
280     *pctx = ctx;
281     return 1;
282
283  err:
284     BIO_puts(bio_err, "Error initializing context\n");
285     ERR_print_errors(bio_err);
286     EVP_PKEY_CTX_free(ctx);
287     EVP_PKEY_free(pkey);
288     return 0;
289
290 }
291
292 int init_gen_str(EVP_PKEY_CTX **pctx,
293                  const char *algname, ENGINE *e, int do_param,
294                  OSSL_LIB_CTX *libctx, const char *propq)
295 {
296     EVP_PKEY_CTX *ctx = NULL;
297     int pkey_id;
298
299     if (*pctx) {
300         BIO_puts(bio_err, "Algorithm already set!\n");
301         return 0;
302     }
303
304     pkey_id = get_legacy_pkey_id(libctx, algname, e);
305     if (pkey_id != NID_undef)
306         ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
307     else
308         ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq);
309
310     if (ctx == NULL)
311         goto err;
312     if (do_param) {
313         if (EVP_PKEY_paramgen_init(ctx) <= 0)
314             goto err;
315     } else {
316         if (EVP_PKEY_keygen_init(ctx) <= 0)
317             goto err;
318     }
319
320     *pctx = ctx;
321     return 1;
322
323  err:
324     BIO_printf(bio_err, "Error initializing %s context\n", algname);
325     ERR_print_errors(bio_err);
326     EVP_PKEY_CTX_free(ctx);
327     return 0;
328
329 }
330
331 static int genpkey_cb(EVP_PKEY_CTX *ctx)
332 {
333     char c = '*';
334     BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
335     int p;
336     p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
337     if (p == 0)
338         c = '.';
339     if (p == 1)
340         c = '+';
341     if (p == 2)
342         c = '*';
343     if (p == 3)
344         c = '\n';
345     BIO_write(b, &c, 1);
346     (void)BIO_flush(b);
347     return 1;
348 }