ecparam.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Copyright 2002-2020 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 <openssl/opensslconf.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14. #include <string.h>
  15. #include "apps.h"
  16. #include "progs.h"
  17. #include <openssl/bio.h>
  18. #include <openssl/err.h>
  19. #include <openssl/bn.h>
  20. #include <openssl/ec.h>
  21. #include <openssl/x509.h>
  22. #include <openssl/pem.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, OPT_C,
  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
  29. } OPTION_CHOICE;
  30. const OPTIONS ecparam_options[] = {
  31. OPT_SECTION("General"),
  32. {"help", OPT_HELP, '-', "Display this summary"},
  33. {"list_curves", OPT_LIST_CURVES, '-',
  34. "Prints a list of all curve 'short names'"},
  35. #ifndef OPENSSL_NO_ENGINE
  36. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  37. #endif
  38. {"genkey", OPT_GENKEY, '-', "Generate ec key"},
  39. {"in", OPT_IN, '<', "Input file - default stdin"},
  40. {"inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)"},
  41. {"out", OPT_OUT, '>', "Output file - default stdout"},
  42. {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
  43. OPT_SECTION("Output"),
  44. {"text", OPT_TEXT, '-', "Print the ec parameters in text form"},
  45. {"C", OPT_C, '-', "Print a 'C' function creating the parameters"},
  46. {"noout", OPT_NOOUT, '-', "Do not print the ec parameter"},
  47. {"param_enc", OPT_PARAM_ENC, 's',
  48. "Specifies the way the ec parameters are encoded"},
  49. OPT_SECTION("Parameter"),
  50. {"check", OPT_CHECK, '-', "Validate the ec parameters"},
  51. {"check_named", OPT_CHECK_NAMED, '-',
  52. "Check that named EC curve parameters have not been modified"},
  53. {"no_seed", OPT_NO_SEED, '-',
  54. "If 'explicit' parameters are chosen do not use the seed"},
  55. {"name", OPT_NAME, 's',
  56. "Use the ec parameters with specified 'short name'"},
  57. {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
  58. OPT_R_OPTIONS,
  59. OPT_PROV_OPTIONS,
  60. {NULL}
  61. };
  62. static OPT_PAIR forms[] = {
  63. {"compressed", POINT_CONVERSION_COMPRESSED},
  64. {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
  65. {"hybrid", POINT_CONVERSION_HYBRID},
  66. {NULL}
  67. };
  68. static OPT_PAIR encodings[] = {
  69. {"named_curve", OPENSSL_EC_NAMED_CURVE},
  70. {"explicit", 0},
  71. {NULL}
  72. };
  73. int ecparam_main(int argc, char **argv)
  74. {
  75. ENGINE *e = NULL;
  76. BIGNUM *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
  77. BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL;
  78. BIO *in = NULL, *out = NULL;
  79. EC_GROUP *group = NULL;
  80. point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
  81. char *curve_name = NULL;
  82. char *infile = NULL, *outfile = NULL, *prog;
  83. unsigned char *buffer = NULL;
  84. OPTION_CHOICE o;
  85. int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_asn1_flag = 0;
  86. int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
  87. int ret = 1, private = 0;
  88. int list_curves = 0, no_seed = 0, check = 0, new_form = 0;
  89. int text = 0, i, genkey = 0, check_named = 0;
  90. prog = opt_init(argc, argv, ecparam_options);
  91. while ((o = opt_next()) != OPT_EOF) {
  92. switch (o) {
  93. case OPT_EOF:
  94. case OPT_ERR:
  95. opthelp:
  96. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  97. goto end;
  98. case OPT_HELP:
  99. opt_help(ecparam_options);
  100. ret = 0;
  101. goto end;
  102. case OPT_INFORM:
  103. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  104. goto opthelp;
  105. break;
  106. case OPT_IN:
  107. infile = opt_arg();
  108. break;
  109. case OPT_OUTFORM:
  110. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  111. goto opthelp;
  112. break;
  113. case OPT_OUT:
  114. outfile = opt_arg();
  115. break;
  116. case OPT_TEXT:
  117. text = 1;
  118. break;
  119. case OPT_C:
  120. C = 1;
  121. break;
  122. case OPT_CHECK:
  123. check = 1;
  124. break;
  125. case OPT_CHECK_NAMED:
  126. check_named = 1;
  127. break;
  128. case OPT_LIST_CURVES:
  129. list_curves = 1;
  130. break;
  131. case OPT_NO_SEED:
  132. no_seed = 1;
  133. break;
  134. case OPT_NOOUT:
  135. noout = 1;
  136. break;
  137. case OPT_NAME:
  138. curve_name = opt_arg();
  139. break;
  140. case OPT_CONV_FORM:
  141. if (!opt_pair(opt_arg(), forms, &new_form))
  142. goto opthelp;
  143. form = new_form;
  144. new_form = 1;
  145. break;
  146. case OPT_PARAM_ENC:
  147. if (!opt_pair(opt_arg(), encodings, &asn1_flag))
  148. goto opthelp;
  149. new_asn1_flag = 1;
  150. break;
  151. case OPT_GENKEY:
  152. genkey = 1;
  153. break;
  154. case OPT_R_CASES:
  155. if (!opt_rand(o))
  156. goto end;
  157. break;
  158. case OPT_PROV_CASES:
  159. if (!opt_provider(o))
  160. goto end;
  161. break;
  162. case OPT_ENGINE:
  163. e = setup_engine(opt_arg(), 0);
  164. break;
  165. }
  166. }
  167. argc = opt_num_rest();
  168. if (argc != 0)
  169. goto opthelp;
  170. private = genkey ? 1 : 0;
  171. in = bio_open_default(infile, 'r', informat);
  172. if (in == NULL)
  173. goto end;
  174. out = bio_open_owner(outfile, outformat, private);
  175. if (out == NULL)
  176. goto end;
  177. if (list_curves) {
  178. EC_builtin_curve *curves = NULL;
  179. size_t crv_len = EC_get_builtin_curves(NULL, 0);
  180. size_t n;
  181. curves = app_malloc((int)sizeof(*curves) * crv_len, "list curves");
  182. if (!EC_get_builtin_curves(curves, crv_len)) {
  183. OPENSSL_free(curves);
  184. goto end;
  185. }
  186. for (n = 0; n < crv_len; n++) {
  187. const char *comment;
  188. const char *sname;
  189. comment = curves[n].comment;
  190. sname = OBJ_nid2sn(curves[n].nid);
  191. if (comment == NULL)
  192. comment = "CURVE DESCRIPTION NOT AVAILABLE";
  193. if (sname == NULL)
  194. sname = "";
  195. BIO_printf(out, " %-10s: ", sname);
  196. BIO_printf(out, "%s\n", comment);
  197. }
  198. OPENSSL_free(curves);
  199. ret = 0;
  200. goto end;
  201. }
  202. if (curve_name != NULL) {
  203. int nid;
  204. /*
  205. * workaround for the SECG curve names secp192r1 and secp256r1 (which
  206. * are the same as the curves prime192v1 and prime256v1 defined in
  207. * X9.62)
  208. */
  209. if (strcmp(curve_name, "secp192r1") == 0) {
  210. BIO_printf(bio_err, "using curve name prime192v1 "
  211. "instead of secp192r1\n");
  212. nid = NID_X9_62_prime192v1;
  213. } else if (strcmp(curve_name, "secp256r1") == 0) {
  214. BIO_printf(bio_err, "using curve name prime256v1 "
  215. "instead of secp256r1\n");
  216. nid = NID_X9_62_prime256v1;
  217. } else {
  218. nid = OBJ_sn2nid(curve_name);
  219. }
  220. if (nid == 0)
  221. nid = EC_curve_nist2nid(curve_name);
  222. if (nid == 0) {
  223. BIO_printf(bio_err, "unknown curve name (%s)\n", curve_name);
  224. goto end;
  225. }
  226. group = EC_GROUP_new_by_curve_name(nid);
  227. if (group == NULL) {
  228. BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name);
  229. goto end;
  230. }
  231. EC_GROUP_set_asn1_flag(group, asn1_flag);
  232. EC_GROUP_set_point_conversion_form(group, form);
  233. } else if (informat == FORMAT_ASN1) {
  234. group = d2i_ECPKParameters_bio(in, NULL);
  235. } else {
  236. group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
  237. }
  238. if (group == NULL) {
  239. BIO_printf(bio_err, "unable to load elliptic curve parameters\n");
  240. ERR_print_errors(bio_err);
  241. goto end;
  242. }
  243. if (new_form)
  244. EC_GROUP_set_point_conversion_form(group, form);
  245. if (new_asn1_flag)
  246. EC_GROUP_set_asn1_flag(group, asn1_flag);
  247. if (no_seed) {
  248. EC_GROUP_set_seed(group, NULL, 0);
  249. }
  250. if (text) {
  251. if (!ECPKParameters_print(out, group, 0))
  252. goto end;
  253. }
  254. if (check_named) {
  255. BIO_printf(bio_err, "validating named elliptic curve parameters: ");
  256. if (EC_GROUP_check_named_curve(group, 0, NULL) <= 0) {
  257. BIO_printf(bio_err, "failed\n");
  258. ERR_print_errors(bio_err);
  259. goto end;
  260. }
  261. BIO_printf(bio_err, "ok\n");
  262. }
  263. if (check) {
  264. BIO_printf(bio_err, "checking elliptic curve parameters: ");
  265. if (!EC_GROUP_check(group, NULL)) {
  266. BIO_printf(bio_err, "failed\n");
  267. ERR_print_errors(bio_err);
  268. goto end;
  269. }
  270. BIO_printf(bio_err, "ok\n");
  271. }
  272. if (C) {
  273. size_t buf_len = 0, tmp_len = 0;
  274. const EC_POINT *point;
  275. int is_prime, len = 0;
  276. if ((ec_p = BN_new()) == NULL
  277. || (ec_a = BN_new()) == NULL
  278. || (ec_b = BN_new()) == NULL
  279. || (ec_gen = BN_new()) == NULL
  280. || (ec_order = BN_new()) == NULL
  281. || (ec_cofactor = BN_new()) == NULL) {
  282. perror("Can't allocate BN");
  283. goto end;
  284. }
  285. is_prime = (EC_GROUP_get_field_type(group) == NID_X9_62_prime_field);
  286. if (!is_prime) {
  287. BIO_printf(bio_err, "Can only handle X9.62 prime fields\n");
  288. goto end;
  289. }
  290. if (!EC_GROUP_get_curve(group, ec_p, ec_a, ec_b, NULL))
  291. goto end;
  292. if ((point = EC_GROUP_get0_generator(group)) == NULL)
  293. goto end;
  294. if (!EC_POINT_point2bn(group, point,
  295. EC_GROUP_get_point_conversion_form(group),
  296. ec_gen, NULL))
  297. goto end;
  298. if (!EC_GROUP_get_order(group, ec_order, NULL))
  299. goto end;
  300. if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
  301. goto end;
  302. if (!ec_p || !ec_a || !ec_b || !ec_gen || !ec_order || !ec_cofactor)
  303. goto end;
  304. len = BN_num_bits(ec_order);
  305. if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
  306. buf_len = tmp_len;
  307. if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
  308. buf_len = tmp_len;
  309. if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
  310. buf_len = tmp_len;
  311. if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
  312. buf_len = tmp_len;
  313. if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
  314. buf_len = tmp_len;
  315. if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
  316. buf_len = tmp_len;
  317. buffer = app_malloc(buf_len, "BN buffer");
  318. BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n{\n", len);
  319. print_bignum_var(out, ec_p, "ec_p", len, buffer);
  320. print_bignum_var(out, ec_a, "ec_a", len, buffer);
  321. print_bignum_var(out, ec_b, "ec_b", len, buffer);
  322. print_bignum_var(out, ec_gen, "ec_gen", len, buffer);
  323. print_bignum_var(out, ec_order, "ec_order", len, buffer);
  324. print_bignum_var(out, ec_cofactor, "ec_cofactor", len, buffer);
  325. BIO_printf(out, " int ok = 0;\n"
  326. " EC_GROUP *group = NULL;\n"
  327. " EC_POINT *point = NULL;\n"
  328. " BIGNUM *tmp_1 = NULL;\n"
  329. " BIGNUM *tmp_2 = NULL;\n"
  330. " BIGNUM *tmp_3 = NULL;\n"
  331. "\n");
  332. BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_p_%d, sizeof(ec_p_%d), NULL)) == NULL)\n"
  333. " goto err;\n", len, len);
  334. BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_a_%d, sizeof(ec_a_%d), NULL)) == NULL)\n"
  335. " goto err;\n", len, len);
  336. BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_b_%d, sizeof(ec_b_%d), NULL)) == NULL)\n"
  337. " goto err;\n", len, len);
  338. BIO_printf(out, " if ((group = EC_GROUP_new_curve_GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)\n"
  339. " goto err;\n"
  340. "\n");
  341. BIO_printf(out, " /* build generator */\n");
  342. BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_gen_%d, sizeof(ec_gen_%d), tmp_1)) == NULL)\n"
  343. " goto err;\n", len, len);
  344. BIO_printf(out, " point = EC_POINT_bn2point(group, tmp_1, NULL, NULL);\n");
  345. BIO_printf(out, " if (point == NULL)\n"
  346. " goto err;\n");
  347. BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_order_%d, sizeof(ec_order_%d), tmp_2)) == NULL)\n"
  348. " goto err;\n", len, len);
  349. BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_cofactor_%d, sizeof(ec_cofactor_%d), tmp_3)) == NULL)\n"
  350. " goto err;\n", len, len);
  351. BIO_printf(out, " if (!EC_GROUP_set_generator(group, point, tmp_2, tmp_3))\n"
  352. " goto err;\n"
  353. "ok = 1;"
  354. "\n");
  355. BIO_printf(out, "err:\n"
  356. " BN_free(tmp_1);\n"
  357. " BN_free(tmp_2);\n"
  358. " BN_free(tmp_3);\n"
  359. " EC_POINT_free(point);\n"
  360. " if (!ok) {\n"
  361. " EC_GROUP_free(group);\n"
  362. " return NULL;\n"
  363. " }\n"
  364. " return (group);\n"
  365. "}\n");
  366. }
  367. if (outformat == FORMAT_ASN1 && genkey)
  368. noout = 1;
  369. if (!noout) {
  370. if (outformat == FORMAT_ASN1)
  371. i = i2d_ECPKParameters_bio(out, group);
  372. else
  373. i = PEM_write_bio_ECPKParameters(out, group);
  374. if (!i) {
  375. BIO_printf(bio_err, "unable to write elliptic "
  376. "curve parameters\n");
  377. ERR_print_errors(bio_err);
  378. goto end;
  379. }
  380. }
  381. if (genkey) {
  382. EC_KEY *eckey = EC_KEY_new();
  383. if (eckey == NULL)
  384. goto end;
  385. if (EC_KEY_set_group(eckey, group) == 0) {
  386. BIO_printf(bio_err, "unable to set group when generating key\n");
  387. EC_KEY_free(eckey);
  388. ERR_print_errors(bio_err);
  389. goto end;
  390. }
  391. if (new_form)
  392. EC_KEY_set_conv_form(eckey, form);
  393. if (!EC_KEY_generate_key(eckey)) {
  394. BIO_printf(bio_err, "unable to generate key\n");
  395. EC_KEY_free(eckey);
  396. ERR_print_errors(bio_err);
  397. goto end;
  398. }
  399. assert(private);
  400. if (outformat == FORMAT_ASN1)
  401. i = i2d_ECPrivateKey_bio(out, eckey);
  402. else
  403. i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
  404. NULL, 0, NULL, NULL);
  405. EC_KEY_free(eckey);
  406. }
  407. ret = 0;
  408. end:
  409. BN_free(ec_p);
  410. BN_free(ec_a);
  411. BN_free(ec_b);
  412. BN_free(ec_gen);
  413. BN_free(ec_order);
  414. BN_free(ec_cofactor);
  415. OPENSSL_free(buffer);
  416. EC_GROUP_free(group);
  417. release_engine(e);
  418. BIO_free(in);
  419. BIO_free_all(out);
  420. return ret;
  421. }