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