ecparam.c 15 KB

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