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