ecparam.c 15 KB

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