Configure: Better detection of '-static' in @{$config{LDFLAGS}}
[openssl.git] / apps / genpkey.c
1 /*
2  * Copyright 2006-2018 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 #ifndef OPENSSL_NO_ENGINE
18 # include <openssl/engine.h>
19 #endif
20
21 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e);
22 static int genpkey_cb(EVP_PKEY_CTX *ctx);
23
24 typedef enum OPTION_choice {
25     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
26     OPT_ENGINE, OPT_OUTFORM, OPT_OUT, OPT_PASS, OPT_PARAMFILE,
27     OPT_ALGORITHM, OPT_PKEYOPT, OPT_GENPARAM, OPT_TEXT, OPT_CIPHER
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
41     OPT_SECTION("Output"),
42     {"out", OPT_OUT, '>', "Output file"},
43     {"outform", OPT_OUTFORM, 'F', "output format (DER or PEM)"},
44     {"pass", OPT_PASS, 's', "Output file pass phrase source"},
45     {"genparam", OPT_GENPARAM, '-', "Generate parameters, not key"},
46     {"text", OPT_TEXT, '-', "Print the in text"},
47     {"", OPT_CIPHER, '-', "Cipher to use to encrypt the key"},
48
49     /* This is deliberately last. */
50     {OPT_HELP_STR, 1, 1,
51      "Order of options may be important!  See the documentation.\n"},
52     {NULL}
53 };
54
55 int genpkey_main(int argc, char **argv)
56 {
57     BIO *in = NULL, *out = NULL;
58     ENGINE *e = NULL;
59     EVP_PKEY *pkey = NULL;
60     EVP_PKEY_CTX *ctx = NULL;
61     char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog;
62     const EVP_CIPHER *cipher = NULL;
63     OPTION_CHOICE o;
64     int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0;
65     int private = 0;
66
67     prog = opt_init(argc, argv, genpkey_options);
68     while ((o = opt_next()) != OPT_EOF) {
69         switch (o) {
70         case OPT_EOF:
71         case OPT_ERR:
72  opthelp:
73             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
74             goto end;
75         case OPT_HELP:
76             ret = 0;
77             opt_help(genpkey_options);
78             goto end;
79         case OPT_OUTFORM:
80             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
81                 goto opthelp;
82             break;
83         case OPT_OUT:
84             outfile = opt_arg();
85             break;
86         case OPT_PASS:
87             passarg = opt_arg();
88             break;
89         case OPT_ENGINE:
90             e = setup_engine(opt_arg(), 0);
91             break;
92         case OPT_PARAMFILE:
93             if (do_param == 1)
94                 goto opthelp;
95             if (!init_keygen_file(&ctx, opt_arg(), e))
96                 goto end;
97             break;
98         case OPT_ALGORITHM:
99             if (!init_gen_str(&ctx, opt_arg(), e, do_param))
100                 goto end;
101             break;
102         case OPT_PKEYOPT:
103             if (ctx == NULL) {
104                 BIO_printf(bio_err, "%s: No keytype specified.\n", prog);
105                 goto opthelp;
106             }
107             if (pkey_ctrl_string(ctx, opt_arg()) <= 0) {
108                 BIO_printf(bio_err,
109                            "%s: Error setting %s parameter:\n",
110                            prog, opt_arg());
111                 ERR_print_errors(bio_err);
112                 goto end;
113             }
114             break;
115         case OPT_GENPARAM:
116             if (ctx != NULL)
117                 goto opthelp;
118             do_param = 1;
119             break;
120         case OPT_TEXT:
121             text = 1;
122             break;
123         case OPT_CIPHER:
124             if (!opt_cipher(opt_unknown(), &cipher)
125                 || do_param == 1)
126                 goto opthelp;
127             if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE ||
128                 EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE ||
129                 EVP_CIPHER_mode(cipher) == EVP_CIPH_XTS_MODE ||
130                 EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE) {
131                 BIO_printf(bio_err, "%s: cipher mode not supported\n", prog);
132                 goto end;
133             }
134         }
135     }
136     argc = opt_num_rest();
137     if (argc != 0)
138         goto opthelp;
139
140     private = do_param ? 0 : 1;
141
142     if (ctx == NULL)
143         goto opthelp;
144
145     if (!app_passwd(passarg, NULL, &pass, NULL)) {
146         BIO_puts(bio_err, "Error getting password\n");
147         goto end;
148     }
149
150     out = bio_open_owner(outfile, outformat, private);
151     if (out == NULL)
152         goto end;
153
154     EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
155     EVP_PKEY_CTX_set_app_data(ctx, bio_err);
156
157     if (do_param) {
158         if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
159             BIO_puts(bio_err, "Error generating parameters\n");
160             ERR_print_errors(bio_err);
161             goto end;
162         }
163     } else {
164         if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
165             BIO_puts(bio_err, "Error generating key\n");
166             ERR_print_errors(bio_err);
167             goto end;
168         }
169     }
170
171     if (do_param) {
172         rv = PEM_write_bio_Parameters(out, pkey);
173     } else if (outformat == FORMAT_PEM) {
174         assert(private);
175         rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
176     } else if (outformat == FORMAT_ASN1) {
177         assert(private);
178         rv = i2d_PrivateKey_bio(out, pkey);
179     } else {
180         BIO_printf(bio_err, "Bad format specified for key\n");
181         goto end;
182     }
183
184     if (rv <= 0) {
185         BIO_puts(bio_err, "Error writing key\n");
186         ERR_print_errors(bio_err);
187     }
188
189     if (text) {
190         if (do_param)
191             rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
192         else
193             rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
194
195         if (rv <= 0) {
196             BIO_puts(bio_err, "Error printing key\n");
197             ERR_print_errors(bio_err);
198         }
199     }
200
201     ret = 0;
202
203  end:
204     EVP_PKEY_free(pkey);
205     EVP_PKEY_CTX_free(ctx);
206     BIO_free_all(out);
207     BIO_free(in);
208     release_engine(e);
209     OPENSSL_free(pass);
210     return ret;
211 }
212
213 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e)
214 {
215     BIO *pbio;
216     EVP_PKEY *pkey = NULL;
217     EVP_PKEY_CTX *ctx = NULL;
218     if (*pctx) {
219         BIO_puts(bio_err, "Parameters already set!\n");
220         return 0;
221     }
222
223     pbio = BIO_new_file(file, "r");
224     if (pbio == NULL) {
225         BIO_printf(bio_err, "Can't open parameter file %s\n", file);
226         return 0;
227     }
228
229     pkey = PEM_read_bio_Parameters(pbio, NULL);
230     BIO_free(pbio);
231
232     if (pkey == NULL) {
233         BIO_printf(bio_err, "Error reading parameter file %s\n", file);
234         return 0;
235     }
236
237     ctx = EVP_PKEY_CTX_new(pkey, e);
238     if (ctx == NULL)
239         goto err;
240     if (EVP_PKEY_keygen_init(ctx) <= 0)
241         goto err;
242     EVP_PKEY_free(pkey);
243     *pctx = ctx;
244     return 1;
245
246  err:
247     BIO_puts(bio_err, "Error initializing context\n");
248     ERR_print_errors(bio_err);
249     EVP_PKEY_CTX_free(ctx);
250     EVP_PKEY_free(pkey);
251     return 0;
252
253 }
254
255 int init_gen_str(EVP_PKEY_CTX **pctx,
256                  const char *algname, ENGINE *e, int do_param)
257 {
258     EVP_PKEY_CTX *ctx = NULL;
259     const EVP_PKEY_ASN1_METHOD *ameth;
260     ENGINE *tmpeng = NULL;
261     int pkey_id;
262
263     if (*pctx) {
264         BIO_puts(bio_err, "Algorithm already set!\n");
265         return 0;
266     }
267
268     ameth = EVP_PKEY_asn1_find_str(&tmpeng, algname, -1);
269
270 #ifndef OPENSSL_NO_ENGINE
271     if (!ameth && e)
272         ameth = ENGINE_get_pkey_asn1_meth_str(e, algname, -1);
273 #endif
274
275     if (!ameth) {
276         BIO_printf(bio_err, "Algorithm %s not found\n", algname);
277         return 0;
278     }
279
280     ERR_clear_error();
281
282     EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
283 #ifndef OPENSSL_NO_ENGINE
284     ENGINE_finish(tmpeng);
285 #endif
286     ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
287
288     if (!ctx)
289         goto err;
290     if (do_param) {
291         if (EVP_PKEY_paramgen_init(ctx) <= 0)
292             goto err;
293     } else {
294         if (EVP_PKEY_keygen_init(ctx) <= 0)
295             goto err;
296     }
297
298     *pctx = ctx;
299     return 1;
300
301  err:
302     BIO_printf(bio_err, "Error initializing %s context\n", algname);
303     ERR_print_errors(bio_err);
304     EVP_PKEY_CTX_free(ctx);
305     return 0;
306
307 }
308
309 static int genpkey_cb(EVP_PKEY_CTX *ctx)
310 {
311     char c = '*';
312     BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
313     int p;
314     p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
315     if (p == 0)
316         c = '.';
317     if (p == 1)
318         c = '+';
319     if (p == 2)
320         c = '*';
321     if (p == 3)
322         c = '\n';
323     BIO_write(b, &c, 1);
324     (void)BIO_flush(b);
325     return 1;
326 }