2
0

ecparam.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. argc = opt_num_rest();
  164. if (argc != 0)
  165. goto opthelp;
  166. private = genkey ? 1 : 0;
  167. in = bio_open_default(infile, 'r', informat);
  168. if (in == NULL)
  169. goto end;
  170. out = bio_open_owner(outfile, outformat, private);
  171. if (out == NULL)
  172. goto end;
  173. if (list_curves) {
  174. EC_builtin_curve *curves = NULL;
  175. size_t crv_len = EC_get_builtin_curves(NULL, 0);
  176. size_t n;
  177. curves = app_malloc((int)sizeof(*curves) * crv_len, "list curves");
  178. if (!EC_get_builtin_curves(curves, crv_len)) {
  179. OPENSSL_free(curves);
  180. goto end;
  181. }
  182. for (n = 0; n < crv_len; n++) {
  183. const char *comment;
  184. const char *sname;
  185. comment = curves[n].comment;
  186. sname = OBJ_nid2sn(curves[n].nid);
  187. if (comment == NULL)
  188. comment = "CURVE DESCRIPTION NOT AVAILABLE";
  189. if (sname == NULL)
  190. sname = "";
  191. BIO_printf(out, " %-10s: ", sname);
  192. BIO_printf(out, "%s\n", comment);
  193. }
  194. OPENSSL_free(curves);
  195. ret = 0;
  196. goto end;
  197. }
  198. if (curve_name != NULL) {
  199. int nid;
  200. /*
  201. * workaround for the SECG curve names secp192r1 and secp256r1 (which
  202. * are the same as the curves prime192v1 and prime256v1 defined in
  203. * X9.62)
  204. */
  205. if (strcmp(curve_name, "secp192r1") == 0) {
  206. BIO_printf(bio_err, "using curve name prime192v1 "
  207. "instead of secp192r1\n");
  208. nid = NID_X9_62_prime192v1;
  209. } else if (strcmp(curve_name, "secp256r1") == 0) {
  210. BIO_printf(bio_err, "using curve name prime256v1 "
  211. "instead of secp256r1\n");
  212. nid = NID_X9_62_prime256v1;
  213. } else {
  214. nid = OBJ_sn2nid(curve_name);
  215. }
  216. if (nid == 0)
  217. nid = EC_curve_nist2nid(curve_name);
  218. if (nid == 0) {
  219. BIO_printf(bio_err, "unknown curve name (%s)\n", curve_name);
  220. goto end;
  221. }
  222. group = EC_GROUP_new_by_curve_name(nid);
  223. if (group == NULL) {
  224. BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name);
  225. goto end;
  226. }
  227. EC_GROUP_set_asn1_flag(group, asn1_flag);
  228. EC_GROUP_set_point_conversion_form(group, form);
  229. } else if (informat == FORMAT_ASN1) {
  230. group = d2i_ECPKParameters_bio(in, NULL);
  231. } else {
  232. group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
  233. }
  234. if (group == NULL) {
  235. BIO_printf(bio_err, "unable to load elliptic curve parameters\n");
  236. ERR_print_errors(bio_err);
  237. goto end;
  238. }
  239. if (new_form)
  240. EC_GROUP_set_point_conversion_form(group, form);
  241. if (new_asn1_flag)
  242. EC_GROUP_set_asn1_flag(group, asn1_flag);
  243. if (no_seed) {
  244. EC_GROUP_set_seed(group, NULL, 0);
  245. }
  246. if (text) {
  247. if (!ECPKParameters_print(out, group, 0))
  248. goto end;
  249. }
  250. if (check_named) {
  251. BIO_printf(bio_err, "validating named elliptic curve parameters: ");
  252. if (EC_GROUP_check_named_curve(group, 0, NULL) <= 0) {
  253. BIO_printf(bio_err, "failed\n");
  254. ERR_print_errors(bio_err);
  255. goto end;
  256. }
  257. BIO_printf(bio_err, "ok\n");
  258. }
  259. if (check) {
  260. BIO_printf(bio_err, "checking elliptic curve parameters: ");
  261. if (!EC_GROUP_check(group, NULL)) {
  262. BIO_printf(bio_err, "failed\n");
  263. ERR_print_errors(bio_err);
  264. goto end;
  265. }
  266. BIO_printf(bio_err, "ok\n");
  267. }
  268. if (outformat == FORMAT_ASN1 && genkey)
  269. noout = 1;
  270. if (!noout) {
  271. if (outformat == FORMAT_ASN1)
  272. i = i2d_ECPKParameters_bio(out, group);
  273. else
  274. i = PEM_write_bio_ECPKParameters(out, group);
  275. if (!i) {
  276. BIO_printf(bio_err, "unable to write elliptic "
  277. "curve parameters\n");
  278. ERR_print_errors(bio_err);
  279. goto end;
  280. }
  281. }
  282. if (genkey) {
  283. EC_KEY *eckey = EC_KEY_new();
  284. if (eckey == NULL)
  285. goto end;
  286. if (EC_KEY_set_group(eckey, group) == 0) {
  287. BIO_printf(bio_err, "unable to set group when generating key\n");
  288. EC_KEY_free(eckey);
  289. ERR_print_errors(bio_err);
  290. goto end;
  291. }
  292. if (new_form)
  293. EC_KEY_set_conv_form(eckey, form);
  294. if (!EC_KEY_generate_key(eckey)) {
  295. BIO_printf(bio_err, "unable to generate key\n");
  296. EC_KEY_free(eckey);
  297. ERR_print_errors(bio_err);
  298. goto end;
  299. }
  300. assert(private);
  301. if (outformat == FORMAT_ASN1)
  302. i = i2d_ECPrivateKey_bio(out, eckey);
  303. else
  304. i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
  305. NULL, 0, NULL, NULL);
  306. EC_KEY_free(eckey);
  307. }
  308. ret = 0;
  309. end:
  310. BN_free(ec_p);
  311. BN_free(ec_a);
  312. BN_free(ec_b);
  313. BN_free(ec_gen);
  314. BN_free(ec_order);
  315. BN_free(ec_cofactor);
  316. OPENSSL_free(buffer);
  317. EC_GROUP_free(group);
  318. release_engine(e);
  319. BIO_free(in);
  320. BIO_free_all(out);
  321. return ret;
  322. }