ecparam.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. argc = opt_num_rest();
  175. if (argc != 0)
  176. goto opthelp;
  177. if (!app_RAND_load())
  178. goto end;
  179. private = genkey ? 1 : 0;
  180. out = bio_open_owner(outfile, outformat, private);
  181. if (out == NULL)
  182. goto end;
  183. if (list_curves) {
  184. if (list_builtin_curves(out))
  185. ret = 0;
  186. goto end;
  187. }
  188. if (curve_name != NULL) {
  189. OSSL_PARAM params[4];
  190. OSSL_PARAM *p = params;
  191. if (strcmp(curve_name, "secp192r1") == 0) {
  192. BIO_printf(bio_err,
  193. "using curve name prime192v1 instead of secp192r1\n");
  194. curve_name = SN_X9_62_prime192v1;
  195. } else if (strcmp(curve_name, "secp256r1") == 0) {
  196. BIO_printf(bio_err,
  197. "using curve name prime256v1 instead of secp256r1\n");
  198. curve_name = SN_X9_62_prime256v1;
  199. }
  200. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  201. curve_name, 0);
  202. if (asn1_encoding != NULL)
  203. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING,
  204. asn1_encoding, 0);
  205. if (point_format != NULL)
  206. *p++ = OSSL_PARAM_construct_utf8_string(
  207. OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
  208. point_format, 0);
  209. *p = OSSL_PARAM_construct_end();
  210. if (OPENSSL_strcasecmp(curve_name, "SM2") == 0)
  211. gctx_params = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "sm2",
  212. app_get0_propq());
  213. else
  214. gctx_params = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "ec",
  215. app_get0_propq());
  216. if (gctx_params == NULL
  217. || EVP_PKEY_keygen_init(gctx_params) <= 0
  218. || EVP_PKEY_CTX_set_params(gctx_params, params) <= 0
  219. || EVP_PKEY_keygen(gctx_params, &params_key) <= 0) {
  220. BIO_printf(bio_err, "unable to generate key\n");
  221. goto end;
  222. }
  223. } else {
  224. params_key = load_keyparams(infile, informat, 1, "EC", "EC parameters");
  225. if (params_key == NULL || !EVP_PKEY_is_a(params_key, "EC"))
  226. goto end;
  227. if (point_format
  228. && !EVP_PKEY_set_utf8_string_param(
  229. params_key, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
  230. point_format)) {
  231. BIO_printf(bio_err, "unable to set point conversion format\n");
  232. goto end;
  233. }
  234. if (asn1_encoding != NULL
  235. && !EVP_PKEY_set_utf8_string_param(
  236. params_key, OSSL_PKEY_PARAM_EC_ENCODING, asn1_encoding)) {
  237. BIO_printf(bio_err, "unable to set asn1 encoding format\n");
  238. goto end;
  239. }
  240. }
  241. if (no_seed
  242. && !EVP_PKEY_set_octet_string_param(params_key, OSSL_PKEY_PARAM_EC_SEED,
  243. NULL, 0)) {
  244. BIO_printf(bio_err, "unable to clear seed\n");
  245. goto end;
  246. }
  247. if (text
  248. && !EVP_PKEY_print_params(out, params_key, 0, NULL)) {
  249. BIO_printf(bio_err, "unable to print params\n");
  250. goto end;
  251. }
  252. if (check || check_named) {
  253. BIO_printf(bio_err, "checking elliptic curve parameters: ");
  254. if (check_named
  255. && !EVP_PKEY_set_utf8_string_param(params_key,
  256. OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE,
  257. OSSL_PKEY_EC_GROUP_CHECK_NAMED)) {
  258. BIO_printf(bio_err, "unable to set check_type\n");
  259. goto end;
  260. }
  261. pctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params_key,
  262. app_get0_propq());
  263. if (pctx == NULL || EVP_PKEY_param_check(pctx) <= 0) {
  264. BIO_printf(bio_err, "failed\n");
  265. goto end;
  266. }
  267. BIO_printf(bio_err, "ok\n");
  268. }
  269. if (outformat == FORMAT_ASN1 && genkey)
  270. noout = 1;
  271. if (!noout) {
  272. ectx_params = OSSL_ENCODER_CTX_new_for_pkey(
  273. params_key, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
  274. outformat == FORMAT_ASN1 ? "DER" : "PEM", NULL, NULL);
  275. if (!OSSL_ENCODER_to_bio(ectx_params, out)) {
  276. BIO_printf(bio_err, "unable to write elliptic curve parameters\n");
  277. goto end;
  278. }
  279. }
  280. if (genkey) {
  281. /*
  282. * NOTE: EC keygen does not normally need to pass in the param_key
  283. * for named curves. This can be achieved using:
  284. * gctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
  285. * EVP_PKEY_keygen_init(gctx);
  286. * EVP_PKEY_CTX_set_group_name(gctx, curvename);
  287. * EVP_PKEY_keygen(gctx, &key) <= 0)
  288. */
  289. gctx_key = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params_key,
  290. app_get0_propq());
  291. if (EVP_PKEY_keygen_init(gctx_key) <= 0
  292. || EVP_PKEY_keygen(gctx_key, &key) <= 0) {
  293. BIO_printf(bio_err, "unable to generate key\n");
  294. goto end;
  295. }
  296. assert(private);
  297. ectx_key = OSSL_ENCODER_CTX_new_for_pkey(
  298. key, OSSL_KEYMGMT_SELECT_ALL,
  299. outformat == FORMAT_ASN1 ? "DER" : "PEM", NULL, NULL);
  300. if (!OSSL_ENCODER_to_bio(ectx_key, out)) {
  301. BIO_printf(bio_err, "unable to write elliptic "
  302. "curve parameters\n");
  303. goto end;
  304. }
  305. }
  306. ret = 0;
  307. end:
  308. if (ret != 0)
  309. ERR_print_errors(bio_err);
  310. release_engine(e);
  311. EVP_PKEY_free(params_key);
  312. EVP_PKEY_free(key);
  313. EVP_PKEY_CTX_free(pctx);
  314. EVP_PKEY_CTX_free(gctx_params);
  315. EVP_PKEY_CTX_free(gctx_key);
  316. OSSL_DECODER_CTX_free(dctx_params);
  317. OSSL_ENCODER_CTX_free(ectx_params);
  318. OSSL_ENCODER_CTX_free(ectx_key);
  319. BIO_free_all(out);
  320. return ret;
  321. }