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