]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/ecparam.c
Deprecate EC_KEY + Update ec apps to use EVP_PKEY
[thirdparty/openssl.git] / apps / ecparam.c
1 /*
2 * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11 #include <openssl/opensslconf.h>
12 #include <openssl/evp.h>
13 #include <openssl/encoder.h>
14 #include <openssl/decoder.h>
15 #include <openssl/core_names.h>
16 #include <openssl/core_dispatch.h>
17 #include <openssl/params.h>
18 #include <openssl/err.h>
19 #include "apps.h"
20 #include "progs.h"
21 #include "ec_common.h"
22
23 typedef enum OPTION_choice {
24 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
25 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT,
26 OPT_CHECK, OPT_LIST_CURVES, OPT_NO_SEED, OPT_NOOUT, OPT_NAME,
27 OPT_CONV_FORM, OPT_PARAM_ENC, OPT_GENKEY, OPT_ENGINE, OPT_CHECK_NAMED,
28 OPT_R_ENUM, OPT_PROV_ENUM
29 } OPTION_CHOICE;
30
31 const OPTIONS ecparam_options[] = {
32 OPT_SECTION("General"),
33 {"help", OPT_HELP, '-', "Display this summary"},
34 {"list_curves", OPT_LIST_CURVES, '-',
35 "Prints a list of all curve 'short names'"},
36 #ifndef OPENSSL_NO_ENGINE
37 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
38 #endif
39
40 {"genkey", OPT_GENKEY, '-', "Generate ec key"},
41 {"in", OPT_IN, '<', "Input file - default stdin"},
42 {"inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)"},
43 {"out", OPT_OUT, '>', "Output file - default stdout"},
44 {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
45
46 OPT_SECTION("Output"),
47 {"text", OPT_TEXT, '-', "Print the ec parameters in text form"},
48 {"noout", OPT_NOOUT, '-', "Do not print the ec parameter"},
49 {"param_enc", OPT_PARAM_ENC, 's',
50 "Specifies the way the ec parameters are encoded"},
51
52 OPT_SECTION("Parameter"),
53 {"check", OPT_CHECK, '-', "Validate the ec parameters"},
54 {"check_named", OPT_CHECK_NAMED, '-',
55 "Check that named EC curve parameters have not been modified"},
56 {"no_seed", OPT_NO_SEED, '-',
57 "If 'explicit' parameters are chosen do not use the seed"},
58 {"name", OPT_NAME, 's',
59 "Use the ec parameters with specified 'short name'"},
60 {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
61
62 OPT_R_OPTIONS,
63 OPT_PROV_OPTIONS,
64 {NULL}
65 };
66
67 static int list_builtin_curves(BIO *out)
68 {
69 int ret = 0;
70 EC_builtin_curve *curves = NULL;
71 size_t n, crv_len = EC_get_builtin_curves(NULL, 0);
72
73 curves = app_malloc((int)sizeof(*curves) * crv_len, "list curves");
74 if (!EC_get_builtin_curves(curves, crv_len))
75 goto end;
76
77 for (n = 0; n < crv_len; n++) {
78 const char *comment = curves[n].comment;
79 const char *sname = OBJ_nid2sn(curves[n].nid);
80
81 if (comment == NULL)
82 comment = "CURVE DESCRIPTION NOT AVAILABLE";
83 if (sname == NULL)
84 sname = "";
85
86 BIO_printf(out, " %-10s: ", sname);
87 BIO_printf(out, "%s\n", comment);
88 }
89 ret = 1;
90 end:
91 OPENSSL_free(curves);
92 return ret;
93 }
94
95 int ecparam_main(int argc, char **argv)
96 {
97 EVP_PKEY_CTX *gctx_params = NULL, *gctx_key = NULL, *pctx = NULL;
98 EVP_PKEY *params_key = NULL, *key = NULL;
99 OSSL_ENCODER_CTX *ectx_key = NULL, *ectx_params = NULL;
100 OSSL_DECODER_CTX *dctx_params = NULL;
101 ENGINE *e = NULL;
102 BIO *in = NULL, *out = NULL;
103 char *curve_name = NULL;
104 char *asn1_encoding = NULL;
105 char *point_format = NULL;
106 char *infile = NULL, *outfile = NULL, *prog;
107 OPTION_CHOICE o;
108 int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0;
109 int ret = 1, private = 0;
110 int no_seed = 0, check = 0, check_named = 0, text = 0, genkey = 0;
111 int list_curves = 0;
112
113 prog = opt_init(argc, argv, ecparam_options);
114 while ((o = opt_next()) != OPT_EOF) {
115 switch (o) {
116 case OPT_EOF:
117 case OPT_ERR:
118 opthelp:
119 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
120 goto end;
121 case OPT_HELP:
122 opt_help(ecparam_options);
123 ret = 0;
124 goto end;
125 case OPT_INFORM:
126 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
127 goto opthelp;
128 break;
129 case OPT_IN:
130 infile = opt_arg();
131 break;
132 case OPT_OUTFORM:
133 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
134 goto opthelp;
135 break;
136 case OPT_OUT:
137 outfile = opt_arg();
138 break;
139 case OPT_TEXT:
140 text = 1;
141 break;
142 case OPT_CHECK:
143 check = 1;
144 break;
145 case OPT_CHECK_NAMED:
146 check_named = 1;
147 break;
148 case OPT_LIST_CURVES:
149 list_curves = 1;
150 break;
151 case OPT_NO_SEED:
152 no_seed = 1;
153 break;
154 case OPT_NOOUT:
155 noout = 1;
156 break;
157 case OPT_NAME:
158 curve_name = opt_arg();
159 break;
160 case OPT_CONV_FORM:
161 point_format = opt_arg();
162 if (!opt_string(point_format, point_format_options))
163 goto opthelp;
164 break;
165 case OPT_PARAM_ENC:
166 asn1_encoding = opt_arg();
167 if (!opt_string(asn1_encoding, asn1_encoding_options))
168 goto opthelp;
169 break;
170 case OPT_GENKEY:
171 genkey = 1;
172 break;
173 case OPT_R_CASES:
174 if (!opt_rand(o))
175 goto end;
176 break;
177 case OPT_PROV_CASES:
178 if (!opt_provider(o))
179 goto end;
180 break;
181 case OPT_ENGINE:
182 e = setup_engine(opt_arg(), 0);
183 break;
184 }
185 }
186
187 /* No extra args. */
188 argc = opt_num_rest();
189 if (argc != 0)
190 goto opthelp;
191
192 private = genkey ? 1 : 0;
193
194 in = bio_open_default(infile, 'r', informat);
195 if (in == NULL)
196 goto end;
197 out = bio_open_owner(outfile, outformat, private);
198 if (out == NULL)
199 goto end;
200
201 if (list_curves) {
202 if (list_builtin_curves(out))
203 ret = 0;
204 goto end;
205 }
206
207 if (curve_name != NULL) {
208 OSSL_PARAM params[4];
209 OSSL_PARAM *p = params;
210
211 if (strcmp(curve_name, "secp192r1") == 0) {
212 BIO_printf(bio_err,
213 "using curve name prime192v1 instead of secp192r1\n");
214 curve_name = SN_X9_62_prime192v1;
215 } else if (strcmp(curve_name, "secp256r1") == 0) {
216 BIO_printf(bio_err,
217 "using curve name prime256v1 instead of secp256r1\n");
218 curve_name = SN_X9_62_prime256v1;
219 }
220 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
221 curve_name, 0);
222 if (asn1_encoding != NULL)
223 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING,
224 asn1_encoding, 0);
225 if (point_format != NULL)
226 *p++ = OSSL_PARAM_construct_utf8_string(
227 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
228 point_format, 0);
229 *p = OSSL_PARAM_construct_end();
230 gctx_params = EVP_PKEY_CTX_new_from_name(NULL, "ec", NULL);
231 if (gctx_params == NULL
232 || EVP_PKEY_keygen_init(gctx_params) <= 0
233 || EVP_PKEY_CTX_set_params(gctx_params, params) <= 0
234 || EVP_PKEY_keygen(gctx_params, &params_key) <= 0) {
235 BIO_printf(bio_err, "unable to generate key\n");
236 goto end;
237 }
238 } else {
239 params_key = load_keyparams(infile, 1, "EC", "EC parameters");
240 if (!EVP_PKEY_is_a(params_key, "EC"))
241 goto end;
242 if (point_format
243 && !EVP_PKEY_set_utf8_string_param(
244 params_key, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
245 point_format)) {
246 BIO_printf(bio_err, "unable to set point conversion format\n");
247 goto end;
248 }
249
250 if (asn1_encoding != NULL
251 && !EVP_PKEY_set_utf8_string_param(
252 params_key, OSSL_PKEY_PARAM_EC_ENCODING, asn1_encoding)) {
253 BIO_printf(bio_err, "unable to set asn1 encoding format\n");
254 goto end;
255 }
256 }
257
258 if (no_seed
259 && !EVP_PKEY_set_octet_string_param(params_key, OSSL_PKEY_PARAM_EC_SEED,
260 NULL, 0)) {
261 BIO_printf(bio_err, "unable to clear seed\n");
262 goto end;
263 }
264
265 if (text
266 && !EVP_PKEY_print_params(out, params_key, 0, NULL)) {
267 BIO_printf(bio_err, "unable to print params\n");
268 goto end;
269 }
270
271 if (check || check_named) {
272 BIO_printf(bio_err, "checking elliptic curve parameters: ");
273
274 if (check_named
275 && !EVP_PKEY_set_utf8_string_param(params_key,
276 OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE,
277 OSSL_PKEY_EC_GROUP_CHECK_NAMED)) {
278 BIO_printf(bio_err, "unable to set check_type\n");
279 goto end;
280 }
281 pctx = EVP_PKEY_CTX_new_from_pkey(NULL, params_key, NULL);
282 if (pctx == NULL || !EVP_PKEY_param_check(pctx)) {
283 BIO_printf(bio_err, "failed\n");
284 goto end;
285 }
286 BIO_printf(bio_err, "ok\n");
287 }
288
289 if (outformat == FORMAT_ASN1 && genkey)
290 noout = 1;
291
292 if (!noout) {
293 ectx_params = OSSL_ENCODER_CTX_new_by_EVP_PKEY(
294 params_key, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
295 outformat == FORMAT_ASN1 ? "DER" : "PEM", NULL, NULL);
296 if (!OSSL_ENCODER_to_bio(ectx_params, out)) {
297 BIO_printf(bio_err, "unable to write elliptic curve parameters\n");
298 goto end;
299 }
300 }
301
302 if (genkey) {
303 /*
304 * NOTE: EC keygen does not normally need to pass in the param_key
305 * for named curves. This can be achieved using:
306 * gctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
307 * EVP_PKEY_keygen_init(gctx);
308 * EVP_PKEY_CTX_set_group_name(gctx, curvename);
309 * EVP_PKEY_keygen(gctx, &key) <= 0)
310 */
311 gctx_key = EVP_PKEY_CTX_new_from_pkey(NULL, params_key, NULL);
312 if (EVP_PKEY_keygen_init(gctx_key) <= 0
313 || EVP_PKEY_keygen(gctx_key, &key) <= 0) {
314 BIO_printf(bio_err, "unable to generate key\n");
315 goto end;
316 }
317 assert(private);
318 ectx_key = OSSL_ENCODER_CTX_new_by_EVP_PKEY(
319 key, OSSL_KEYMGMT_SELECT_ALL,
320 outformat == FORMAT_ASN1 ? "DER" : "PEM", NULL, NULL);
321 if (!OSSL_ENCODER_to_bio(ectx_key, out)) {
322 BIO_printf(bio_err, "unable to write elliptic "
323 "curve parameters\n");
324 goto end;
325 }
326 }
327
328 ret = 0;
329 end:
330 if (ret != 0)
331 ERR_print_errors(bio_err);
332 release_engine(e);
333 EVP_PKEY_free(params_key);
334 EVP_PKEY_free(key);
335 EVP_PKEY_CTX_free(pctx);
336 EVP_PKEY_CTX_free(gctx_params);
337 EVP_PKEY_CTX_free(gctx_key);
338 OSSL_DECODER_CTX_free(dctx_params);
339 OSSL_ENCODER_CTX_free(ectx_params);
340 OSSL_ENCODER_CTX_free(ectx_key);
341 BIO_free(in);
342 BIO_free_all(out);
343 return ret;
344 }