ecparam.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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,
  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. {"noout", OPT_NOOUT, '-', "Do not print the ec parameter"},
  46. {"param_enc", OPT_PARAM_ENC, 's',
  47. "Specifies the way the ec parameters are encoded"},
  48. OPT_SECTION("Parameter"),
  49. {"check", OPT_CHECK, '-', "Validate the ec parameters"},
  50. {"check_named", OPT_CHECK_NAMED, '-',
  51. "Check that named EC curve parameters have not been modified"},
  52. {"no_seed", OPT_NO_SEED, '-',
  53. "If 'explicit' parameters are chosen do not use the seed"},
  54. {"name", OPT_NAME, 's',
  55. "Use the ec parameters with specified 'short name'"},
  56. {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
  57. OPT_R_OPTIONS,
  58. OPT_PROV_OPTIONS,
  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;
  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_CHECK:
  119. check = 1;
  120. break;
  121. case OPT_CHECK_NAMED:
  122. check_named = 1;
  123. break;
  124. case OPT_LIST_CURVES:
  125. list_curves = 1;
  126. break;
  127. case OPT_NO_SEED:
  128. no_seed = 1;
  129. break;
  130. case OPT_NOOUT:
  131. noout = 1;
  132. break;
  133. case OPT_NAME:
  134. curve_name = opt_arg();
  135. break;
  136. case OPT_CONV_FORM:
  137. if (!opt_pair(opt_arg(), forms, &new_form))
  138. goto opthelp;
  139. form = new_form;
  140. new_form = 1;
  141. break;
  142. case OPT_PARAM_ENC:
  143. if (!opt_pair(opt_arg(), encodings, &asn1_flag))
  144. goto opthelp;
  145. new_asn1_flag = 1;
  146. break;
  147. case OPT_GENKEY:
  148. genkey = 1;
  149. break;
  150. case OPT_R_CASES:
  151. if (!opt_rand(o))
  152. goto end;
  153. break;
  154. case OPT_PROV_CASES:
  155. if (!opt_provider(o))
  156. goto end;
  157. break;
  158. case OPT_ENGINE:
  159. e = setup_engine(opt_arg(), 0);
  160. break;
  161. }
  162. }
  163. /* No extra args. */
  164. argc = opt_num_rest();
  165. if (argc != 0)
  166. goto opthelp;
  167. private = genkey ? 1 : 0;
  168. in = bio_open_default(infile, 'r', informat);
  169. if (in == NULL)
  170. goto end;
  171. out = bio_open_owner(outfile, outformat, private);
  172. if (out == NULL)
  173. goto end;
  174. if (list_curves) {
  175. EC_builtin_curve *curves = NULL;
  176. size_t crv_len = EC_get_builtin_curves(NULL, 0);
  177. size_t n;
  178. curves = app_malloc((int)sizeof(*curves) * crv_len, "list curves");
  179. if (!EC_get_builtin_curves(curves, crv_len)) {
  180. OPENSSL_free(curves);
  181. goto end;
  182. }
  183. for (n = 0; n < crv_len; n++) {
  184. const char *comment;
  185. const char *sname;
  186. comment = curves[n].comment;
  187. sname = OBJ_nid2sn(curves[n].nid);
  188. if (comment == NULL)
  189. comment = "CURVE DESCRIPTION NOT AVAILABLE";
  190. if (sname == NULL)
  191. sname = "";
  192. BIO_printf(out, " %-10s: ", sname);
  193. BIO_printf(out, "%s\n", comment);
  194. }
  195. OPENSSL_free(curves);
  196. ret = 0;
  197. goto end;
  198. }
  199. if (curve_name != NULL) {
  200. int nid;
  201. /*
  202. * workaround for the SECG curve names secp192r1 and secp256r1 (which
  203. * are the same as the curves prime192v1 and prime256v1 defined in
  204. * X9.62)
  205. */
  206. if (strcmp(curve_name, "secp192r1") == 0) {
  207. BIO_printf(bio_err, "using curve name prime192v1 "
  208. "instead of secp192r1\n");
  209. nid = NID_X9_62_prime192v1;
  210. } else if (strcmp(curve_name, "secp256r1") == 0) {
  211. BIO_printf(bio_err, "using curve name prime256v1 "
  212. "instead of secp256r1\n");
  213. nid = NID_X9_62_prime256v1;
  214. } else {
  215. nid = OBJ_sn2nid(curve_name);
  216. }
  217. if (nid == 0)
  218. nid = EC_curve_nist2nid(curve_name);
  219. if (nid == 0) {
  220. BIO_printf(bio_err, "unknown curve name (%s)\n", curve_name);
  221. goto end;
  222. }
  223. group = EC_GROUP_new_by_curve_name(nid);
  224. if (group == NULL) {
  225. BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name);
  226. goto end;
  227. }
  228. EC_GROUP_set_asn1_flag(group, asn1_flag);
  229. EC_GROUP_set_point_conversion_form(group, form);
  230. } else if (informat == FORMAT_ASN1) {
  231. group = d2i_ECPKParameters_bio(in, NULL);
  232. } else {
  233. group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
  234. }
  235. if (group == NULL) {
  236. BIO_printf(bio_err, "unable to load elliptic curve parameters\n");
  237. ERR_print_errors(bio_err);
  238. goto end;
  239. }
  240. if (new_form)
  241. EC_GROUP_set_point_conversion_form(group, form);
  242. if (new_asn1_flag)
  243. EC_GROUP_set_asn1_flag(group, asn1_flag);
  244. if (no_seed) {
  245. EC_GROUP_set_seed(group, NULL, 0);
  246. }
  247. if (text) {
  248. if (!ECPKParameters_print(out, group, 0))
  249. goto end;
  250. }
  251. if (check_named) {
  252. BIO_printf(bio_err, "validating named elliptic curve parameters: ");
  253. if (EC_GROUP_check_named_curve(group, 0, NULL) <= 0) {
  254. BIO_printf(bio_err, "failed\n");
  255. ERR_print_errors(bio_err);
  256. goto end;
  257. }
  258. BIO_printf(bio_err, "ok\n");
  259. }
  260. if (check) {
  261. BIO_printf(bio_err, "checking elliptic curve parameters: ");
  262. if (!EC_GROUP_check(group, NULL)) {
  263. BIO_printf(bio_err, "failed\n");
  264. ERR_print_errors(bio_err);
  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. if (outformat == FORMAT_ASN1)
  273. i = i2d_ECPKParameters_bio(out, group);
  274. else
  275. i = PEM_write_bio_ECPKParameters(out, group);
  276. if (!i) {
  277. BIO_printf(bio_err, "unable to write elliptic "
  278. "curve parameters\n");
  279. ERR_print_errors(bio_err);
  280. goto end;
  281. }
  282. }
  283. if (genkey) {
  284. EC_KEY *eckey = EC_KEY_new();
  285. if (eckey == NULL)
  286. goto end;
  287. if (EC_KEY_set_group(eckey, group) == 0) {
  288. BIO_printf(bio_err, "unable to set group when generating key\n");
  289. EC_KEY_free(eckey);
  290. ERR_print_errors(bio_err);
  291. goto end;
  292. }
  293. if (new_form)
  294. EC_KEY_set_conv_form(eckey, form);
  295. if (!EC_KEY_generate_key(eckey)) {
  296. BIO_printf(bio_err, "unable to generate key\n");
  297. EC_KEY_free(eckey);
  298. ERR_print_errors(bio_err);
  299. goto end;
  300. }
  301. assert(private);
  302. if (outformat == FORMAT_ASN1)
  303. i = i2d_ECPrivateKey_bio(out, eckey);
  304. else
  305. i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
  306. NULL, 0, NULL, NULL);
  307. EC_KEY_free(eckey);
  308. }
  309. ret = 0;
  310. end:
  311. BN_free(ec_p);
  312. BN_free(ec_a);
  313. BN_free(ec_b);
  314. BN_free(ec_gen);
  315. BN_free(ec_order);
  316. BN_free(ec_cofactor);
  317. OPENSSL_free(buffer);
  318. EC_GROUP_free(group);
  319. release_engine(e);
  320. BIO_free(in);
  321. BIO_free_all(out);
  322. return ret;
  323. }