2 * Copyright 2002-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
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
12 #include <openssl/opensslconf.h>
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>
22 #include "ec_common.h"
24 typedef enum OPTION_choice
{
26 OPT_INFORM
, OPT_OUTFORM
, OPT_IN
, OPT_OUT
, OPT_TEXT
,
27 OPT_CHECK
, OPT_LIST_CURVES
, OPT_NO_SEED
, OPT_NOOUT
, OPT_NAME
,
28 OPT_CONV_FORM
, OPT_PARAM_ENC
, OPT_GENKEY
, OPT_ENGINE
, OPT_CHECK_NAMED
,
29 OPT_R_ENUM
, OPT_PROV_ENUM
32 const OPTIONS ecparam_options
[] = {
33 OPT_SECTION("General"),
34 {"help", OPT_HELP
, '-', "Display this summary"},
35 {"list_curves", OPT_LIST_CURVES
, '-',
36 "Prints a list of all curve 'short names'"},
37 #ifndef OPENSSL_NO_ENGINE
38 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
41 {"genkey", OPT_GENKEY
, '-', "Generate ec key"},
42 {"in", OPT_IN
, '<', "Input file - default stdin"},
43 {"inform", OPT_INFORM
, 'F', "Input format - default PEM (DER or PEM)"},
44 {"out", OPT_OUT
, '>', "Output file - default stdout"},
45 {"outform", OPT_OUTFORM
, 'F', "Output format - default PEM"},
47 OPT_SECTION("Output"),
48 {"text", OPT_TEXT
, '-', "Print the ec parameters in text form"},
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"},
53 OPT_SECTION("Parameter"),
54 {"check", OPT_CHECK
, '-', "Validate the ec parameters"},
55 {"check_named", OPT_CHECK_NAMED
, '-',
56 "Check that named EC curve parameters have not been modified"},
57 {"no_seed", OPT_NO_SEED
, '-',
58 "If 'explicit' parameters are chosen do not use the seed"},
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 "},
68 static int list_builtin_curves(BIO
*out
)
71 EC_builtin_curve
*curves
= NULL
;
72 size_t n
, crv_len
= EC_get_builtin_curves(NULL
, 0);
74 curves
= app_malloc((int)sizeof(*curves
) * crv_len
, "list curves");
75 if (!EC_get_builtin_curves(curves
, crv_len
))
78 for (n
= 0; n
< crv_len
; n
++) {
79 const char *comment
= curves
[n
].comment
;
80 const char *sname
= OBJ_nid2sn(curves
[n
].nid
);
83 comment
= "CURVE DESCRIPTION NOT AVAILABLE";
87 BIO_printf(out
, " %-10s: ", sname
);
88 BIO_printf(out
, "%s\n", comment
);
96 int ecparam_main(int argc
, char **argv
)
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
;
104 char *curve_name
= NULL
;
105 char *asn1_encoding
= NULL
;
106 char *point_format
= NULL
;
107 char *infile
= NULL
, *outfile
= NULL
, *prog
;
109 int informat
= FORMAT_PEM
, outformat
= FORMAT_PEM
, noout
= 0;
110 int ret
= 1, private = 0;
111 int no_seed
= 0, check
= 0, check_named
= 0, text
= 0, genkey
= 0;
114 prog
= opt_init(argc
, argv
, ecparam_options
);
115 while ((o
= opt_next()) != OPT_EOF
) {
120 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
123 opt_help(ecparam_options
);
127 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &informat
))
134 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &outformat
))
146 case OPT_CHECK_NAMED
:
149 case OPT_LIST_CURVES
:
159 curve_name
= opt_arg();
162 point_format
= opt_arg();
163 if (!opt_string(point_format
, point_format_options
))
167 asn1_encoding
= opt_arg();
168 if (!opt_string(asn1_encoding
, asn1_encoding_options
))
179 if (!opt_provider(o
))
183 e
= setup_engine(opt_arg(), 0);
189 argc
= opt_num_rest();
193 if (!app_RAND_load())
196 private = genkey
? 1 : 0;
198 out
= bio_open_owner(outfile
, outformat
, private);
203 if (list_builtin_curves(out
))
208 if (curve_name
!= NULL
) {
209 OSSL_PARAM params
[4];
210 OSSL_PARAM
*p
= params
;
212 if (strcmp(curve_name
, "secp192r1") == 0) {
214 "using curve name prime192v1 instead of secp192r1\n");
215 curve_name
= SN_X9_62_prime192v1
;
216 } else if (strcmp(curve_name
, "secp256r1") == 0) {
218 "using curve name prime256v1 instead of secp256r1\n");
219 curve_name
= SN_X9_62_prime256v1
;
221 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
,
223 if (asn1_encoding
!= NULL
)
224 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING
,
226 if (point_format
!= NULL
)
227 *p
++ = OSSL_PARAM_construct_utf8_string(
228 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
230 *p
= OSSL_PARAM_construct_end();
232 if (OPENSSL_strcasecmp(curve_name
, "SM2") == 0)
233 gctx_params
= EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "sm2",
236 gctx_params
= EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "ec",
238 if (gctx_params
== NULL
239 || EVP_PKEY_keygen_init(gctx_params
) <= 0
240 || EVP_PKEY_CTX_set_params(gctx_params
, params
) <= 0
241 || EVP_PKEY_keygen(gctx_params
, ¶ms_key
) <= 0) {
242 BIO_printf(bio_err
, "unable to generate key\n");
246 params_key
= load_keyparams(infile
, informat
, 1, "EC", "EC parameters");
247 if (params_key
== NULL
|| !EVP_PKEY_is_a(params_key
, "EC"))
250 && !EVP_PKEY_set_utf8_string_param(
251 params_key
, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
253 BIO_printf(bio_err
, "unable to set point conversion format\n");
257 if (asn1_encoding
!= NULL
258 && !EVP_PKEY_set_utf8_string_param(
259 params_key
, OSSL_PKEY_PARAM_EC_ENCODING
, asn1_encoding
)) {
260 BIO_printf(bio_err
, "unable to set asn1 encoding format\n");
266 && !EVP_PKEY_set_octet_string_param(params_key
, OSSL_PKEY_PARAM_EC_SEED
,
268 BIO_printf(bio_err
, "unable to clear seed\n");
273 && !EVP_PKEY_print_params(out
, params_key
, 0, NULL
)) {
274 BIO_printf(bio_err
, "unable to print params\n");
278 if (check
|| check_named
) {
279 BIO_printf(bio_err
, "checking elliptic curve parameters: ");
282 && !EVP_PKEY_set_utf8_string_param(params_key
,
283 OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE
,
284 OSSL_PKEY_EC_GROUP_CHECK_NAMED
)) {
285 BIO_printf(bio_err
, "unable to set check_type\n");
288 pctx
= EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params_key
,
290 if (pctx
== NULL
|| EVP_PKEY_param_check(pctx
) <= 0) {
291 BIO_printf(bio_err
, "failed\n");
294 BIO_printf(bio_err
, "ok\n");
297 if (outformat
== FORMAT_ASN1
&& genkey
)
301 ectx_params
= OSSL_ENCODER_CTX_new_for_pkey(
302 params_key
, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
303 outformat
== FORMAT_ASN1
? "DER" : "PEM", NULL
, NULL
);
304 if (!OSSL_ENCODER_to_bio(ectx_params
, out
)) {
305 BIO_printf(bio_err
, "unable to write elliptic curve parameters\n");
312 * NOTE: EC keygen does not normally need to pass in the param_key
313 * for named curves. This can be achieved using:
314 * gctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
315 * EVP_PKEY_keygen_init(gctx);
316 * EVP_PKEY_CTX_set_group_name(gctx, curvename);
317 * EVP_PKEY_keygen(gctx, &key) <= 0)
319 gctx_key
= EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params_key
,
321 if (EVP_PKEY_keygen_init(gctx_key
) <= 0
322 || EVP_PKEY_keygen(gctx_key
, &key
) <= 0) {
323 BIO_printf(bio_err
, "unable to generate key\n");
327 ectx_key
= OSSL_ENCODER_CTX_new_for_pkey(
328 key
, OSSL_KEYMGMT_SELECT_ALL
,
329 outformat
== FORMAT_ASN1
? "DER" : "PEM", NULL
, NULL
);
330 if (!OSSL_ENCODER_to_bio(ectx_key
, out
)) {
331 BIO_printf(bio_err
, "unable to write elliptic "
332 "curve parameters\n");
340 ERR_print_errors(bio_err
);
342 EVP_PKEY_free(params_key
);
344 EVP_PKEY_CTX_free(pctx
);
345 EVP_PKEY_CTX_free(gctx_params
);
346 EVP_PKEY_CTX_free(gctx_key
);
347 OSSL_DECODER_CTX_free(dctx_params
);
348 OSSL_ENCODER_CTX_free(ectx_params
);
349 OSSL_ENCODER_CTX_free(ectx_key
);