ecparam.c 15 KB

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