ecparam.c 12 KB

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