2 * Copyright 2002-2020 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
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>
21 #include "ec_common.h"
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
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"},
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"},
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"},
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 "},
67 static int list_builtin_curves(BIO
*out
)
70 EC_builtin_curve
*curves
= NULL
;
71 size_t n
, crv_len
= EC_get_builtin_curves(NULL
, 0);
73 curves
= app_malloc((int)sizeof(*curves
) * crv_len
, "list curves");
74 if (!EC_get_builtin_curves(curves
, crv_len
))
77 for (n
= 0; n
< crv_len
; n
++) {
78 const char *comment
= curves
[n
].comment
;
79 const char *sname
= OBJ_nid2sn(curves
[n
].nid
);
82 comment
= "CURVE DESCRIPTION NOT AVAILABLE";
86 BIO_printf(out
, " %-10s: ", sname
);
87 BIO_printf(out
, "%s\n", comment
);
95 int ecparam_main(int argc
, char **argv
)
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
;
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
;
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;
113 prog
= opt_init(argc
, argv
, ecparam_options
);
114 while ((o
= opt_next()) != OPT_EOF
) {
119 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
122 opt_help(ecparam_options
);
126 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &informat
))
133 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &outformat
))
145 case OPT_CHECK_NAMED
:
148 case OPT_LIST_CURVES
:
158 curve_name
= opt_arg();
161 point_format
= opt_arg();
162 if (!opt_string(point_format
, point_format_options
))
166 asn1_encoding
= opt_arg();
167 if (!opt_string(asn1_encoding
, asn1_encoding_options
))
178 if (!opt_provider(o
))
182 e
= setup_engine(opt_arg(), 0);
188 argc
= opt_num_rest();
192 private = genkey
? 1 : 0;
194 in
= bio_open_default(infile
, 'r', informat
);
197 out
= bio_open_owner(outfile
, outformat
, private);
202 if (list_builtin_curves(out
))
207 if (curve_name
!= NULL
) {
208 OSSL_PARAM params
[4];
209 OSSL_PARAM
*p
= params
;
211 if (strcmp(curve_name
, "secp192r1") == 0) {
213 "using curve name prime192v1 instead of secp192r1\n");
214 curve_name
= SN_X9_62_prime192v1
;
215 } else if (strcmp(curve_name
, "secp256r1") == 0) {
217 "using curve name prime256v1 instead of secp256r1\n");
218 curve_name
= SN_X9_62_prime256v1
;
220 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
,
222 if (asn1_encoding
!= NULL
)
223 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING
,
225 if (point_format
!= NULL
)
226 *p
++ = OSSL_PARAM_construct_utf8_string(
227 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
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
, ¶ms_key
) <= 0) {
235 BIO_printf(bio_err
, "unable to generate key\n");
239 params_key
= load_keyparams(infile
, 1, "EC", "EC parameters");
240 if (!EVP_PKEY_is_a(params_key
, "EC"))
243 && !EVP_PKEY_set_utf8_string_param(
244 params_key
, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
246 BIO_printf(bio_err
, "unable to set point conversion format\n");
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");
259 && !EVP_PKEY_set_octet_string_param(params_key
, OSSL_PKEY_PARAM_EC_SEED
,
261 BIO_printf(bio_err
, "unable to clear seed\n");
266 && !EVP_PKEY_print_params(out
, params_key
, 0, NULL
)) {
267 BIO_printf(bio_err
, "unable to print params\n");
271 if (check
|| check_named
) {
272 BIO_printf(bio_err
, "checking elliptic curve parameters: ");
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");
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");
286 BIO_printf(bio_err
, "ok\n");
289 if (outformat
== FORMAT_ASN1
&& genkey
)
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");
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)
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");
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");
331 ERR_print_errors(bio_err
);
333 EVP_PKEY_free(params_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
);