2
0

ecparam.c 12 KB

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