ec_kmgmt.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*
  2. * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * ECDH/ECDSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/core_dispatch.h>
  15. #include <openssl/core_names.h>
  16. #include <openssl/bn.h>
  17. #include <openssl/err.h>
  18. #include <openssl/objects.h>
  19. #include "crypto/bn.h"
  20. #include "crypto/ec.h"
  21. #include "prov/implementations.h"
  22. #include "prov/providercommon.h"
  23. #include "prov/providercommonerr.h"
  24. #include "prov/provider_ctx.h"
  25. #include "internal/param_build_set.h"
  26. static OSSL_FUNC_keymgmt_new_fn ec_newdata;
  27. static OSSL_FUNC_keymgmt_gen_init_fn ec_gen_init;
  28. static OSSL_FUNC_keymgmt_gen_set_template_fn ec_gen_set_template;
  29. static OSSL_FUNC_keymgmt_gen_set_params_fn ec_gen_set_params;
  30. static OSSL_FUNC_keymgmt_gen_settable_params_fn ec_gen_settable_params;
  31. static OSSL_FUNC_keymgmt_gen_fn ec_gen;
  32. static OSSL_FUNC_keymgmt_gen_cleanup_fn ec_gen_cleanup;
  33. static OSSL_FUNC_keymgmt_free_fn ec_freedata;
  34. static OSSL_FUNC_keymgmt_get_params_fn ec_get_params;
  35. static OSSL_FUNC_keymgmt_gettable_params_fn ec_gettable_params;
  36. static OSSL_FUNC_keymgmt_set_params_fn ec_set_params;
  37. static OSSL_FUNC_keymgmt_settable_params_fn ec_settable_params;
  38. static OSSL_FUNC_keymgmt_has_fn ec_has;
  39. static OSSL_FUNC_keymgmt_match_fn ec_match;
  40. static OSSL_FUNC_keymgmt_validate_fn ec_validate;
  41. static OSSL_FUNC_keymgmt_import_fn ec_import;
  42. static OSSL_FUNC_keymgmt_import_types_fn ec_import_types;
  43. static OSSL_FUNC_keymgmt_export_fn ec_export;
  44. static OSSL_FUNC_keymgmt_export_types_fn ec_export_types;
  45. static OSSL_FUNC_keymgmt_query_operation_name_fn ec_query_operation_name;
  46. #define EC_DEFAULT_MD "SHA256"
  47. #define EC_POSSIBLE_SELECTIONS \
  48. (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
  49. static
  50. const char *ec_query_operation_name(int operation_id)
  51. {
  52. switch (operation_id) {
  53. case OSSL_OP_KEYEXCH:
  54. return "ECDH";
  55. case OSSL_OP_SIGNATURE:
  56. return "ECDSA";
  57. }
  58. return NULL;
  59. }
  60. static ossl_inline
  61. int domparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl,
  62. OSSL_PARAM params[])
  63. {
  64. const EC_GROUP *ecg;
  65. int curve_nid;
  66. if (ec == NULL)
  67. return 0;
  68. ecg = EC_KEY_get0_group(ec);
  69. if (ecg == NULL)
  70. return 0;
  71. curve_nid = EC_GROUP_get_curve_name(ecg);
  72. if (curve_nid == NID_undef) {
  73. /* TODO(3.0): should we support explicit parameters curves? */
  74. return 0;
  75. } else {
  76. /* named curve */
  77. const char *curve_name = NULL;
  78. if ((curve_name = ec_curve_nid2name(curve_nid)) == NULL)
  79. return 0;
  80. if (!ossl_param_build_set_utf8_string(tmpl, params,
  81. OSSL_PKEY_PARAM_GROUP_NAME,
  82. curve_name))
  83. return 0;
  84. }
  85. return 1;
  86. }
  87. /*
  88. * Callers of key_to_params MUST make sure that domparams_to_params is also
  89. * called!
  90. *
  91. * This function only exports the bare keypair, domain parameters and other
  92. * parameters are exported separately.
  93. */
  94. static ossl_inline
  95. int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl,
  96. OSSL_PARAM params[], int include_private,
  97. unsigned char **pub_key)
  98. {
  99. BIGNUM *x = NULL, *y = NULL;
  100. const BIGNUM *priv_key = NULL;
  101. const EC_POINT *pub_point = NULL;
  102. const EC_GROUP *ecg = NULL;
  103. size_t pub_key_len = 0;
  104. int ret = 0;
  105. BN_CTX *bnctx = NULL;
  106. if (eckey == NULL
  107. || (ecg = EC_KEY_get0_group(eckey)) == NULL)
  108. return 0;
  109. priv_key = EC_KEY_get0_private_key(eckey);
  110. pub_point = EC_KEY_get0_public_key(eckey);
  111. if (pub_point != NULL) {
  112. OSSL_PARAM *p = NULL, *px = NULL, *py = NULL;
  113. /*
  114. * EC_POINT_point2buf() can generate random numbers in some
  115. * implementations so we need to ensure we use the correct libctx.
  116. */
  117. bnctx = BN_CTX_new_ex(ec_key_get_libctx(eckey));
  118. if (bnctx == NULL)
  119. goto err;
  120. /* If we are doing a get then check first before decoding the point */
  121. if (tmpl == NULL) {
  122. p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PUB_KEY);
  123. px = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_X);
  124. py = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_Y);
  125. }
  126. if (p != NULL || tmpl != NULL) {
  127. /* convert pub_point to a octet string according to the SECG standard */
  128. if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
  129. POINT_CONVERSION_COMPRESSED,
  130. pub_key, bnctx)) == 0
  131. || !ossl_param_build_set_octet_string(tmpl, p,
  132. OSSL_PKEY_PARAM_PUB_KEY,
  133. *pub_key, pub_key_len))
  134. goto err;
  135. }
  136. if (px != NULL || py != NULL) {
  137. if (px != NULL)
  138. x = BN_CTX_get(bnctx);
  139. if (py != NULL)
  140. y = BN_CTX_get(bnctx);
  141. if (!EC_POINT_get_affine_coordinates(ecg, pub_point, x, y, bnctx))
  142. goto err;
  143. if (px != NULL
  144. && !ossl_param_build_set_bn(tmpl, px,
  145. OSSL_PKEY_PARAM_EC_PUB_X, x))
  146. goto err;
  147. if (py != NULL
  148. && !ossl_param_build_set_bn(tmpl, py,
  149. OSSL_PKEY_PARAM_EC_PUB_Y, y))
  150. goto err;
  151. }
  152. }
  153. if (priv_key != NULL && include_private) {
  154. size_t sz;
  155. int ecbits;
  156. /*
  157. * Key import/export should never leak the bit length of the secret
  158. * scalar in the key.
  159. *
  160. * For this reason, on export we use padded BIGNUMs with fixed length.
  161. *
  162. * When importing we also should make sure that, even if short lived,
  163. * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
  164. * soon as possible, so that any processing of this BIGNUM might opt for
  165. * constant time implementations in the backend.
  166. *
  167. * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
  168. * to preallocate the BIGNUM internal buffer to a fixed public size big
  169. * enough that operations performed during the processing never trigger
  170. * a realloc which would leak the size of the scalar through memory
  171. * accesses.
  172. *
  173. * Fixed Length
  174. * ------------
  175. *
  176. * The order of the large prime subgroup of the curve is our choice for
  177. * a fixed public size, as that is generally the upper bound for
  178. * generating a private key in EC cryptosystems and should fit all valid
  179. * secret scalars.
  180. *
  181. * For padding on export we just use the bit length of the order
  182. * converted to bytes (rounding up).
  183. *
  184. * For preallocating the BIGNUM storage we look at the number of "words"
  185. * required for the internal representation of the order, and we
  186. * preallocate 2 extra "words" in case any of the subsequent processing
  187. * might temporarily overflow the order length.
  188. */
  189. ecbits = EC_GROUP_order_bits(ecg);
  190. if (ecbits <= 0)
  191. goto err;
  192. sz = (ecbits + 7 ) / 8;
  193. if (!ossl_param_build_set_bn_pad(tmpl, params,
  194. OSSL_PKEY_PARAM_PRIV_KEY,
  195. priv_key, sz))
  196. goto err;
  197. }
  198. ret = 1;
  199. err:
  200. BN_CTX_free(bnctx);
  201. return ret;
  202. }
  203. static ossl_inline
  204. int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl,
  205. OSSL_PARAM params[])
  206. {
  207. int ecdh_cofactor_mode = 0;
  208. if (ec == NULL)
  209. return 0;
  210. ecdh_cofactor_mode =
  211. (EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
  212. return ossl_param_build_set_int(tmpl, params,
  213. OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
  214. ecdh_cofactor_mode);
  215. }
  216. static
  217. void *ec_newdata(void *provctx)
  218. {
  219. return EC_KEY_new_with_libctx(PROV_LIBRARY_CONTEXT_OF(provctx), NULL);
  220. }
  221. static
  222. void ec_freedata(void *keydata)
  223. {
  224. EC_KEY_free(keydata);
  225. }
  226. static
  227. int ec_has(void *keydata, int selection)
  228. {
  229. EC_KEY *ec = keydata;
  230. int ok = 0;
  231. if (ec != NULL) {
  232. if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
  233. ok = 1;
  234. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  235. ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
  236. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  237. ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
  238. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  239. ok = ok && (EC_KEY_get0_group(ec) != NULL);
  240. /*
  241. * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
  242. * available, so no extra check is needed other than the previous one
  243. * against EC_POSSIBLE_SELECTIONS.
  244. */
  245. }
  246. return ok;
  247. }
  248. static int ec_match(const void *keydata1, const void *keydata2, int selection)
  249. {
  250. const EC_KEY *ec1 = keydata1;
  251. const EC_KEY *ec2 = keydata2;
  252. const EC_GROUP *group_a = EC_KEY_get0_group(ec1);
  253. const EC_GROUP *group_b = EC_KEY_get0_group(ec2);
  254. int ok = 1;
  255. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  256. ok = ok && group_a != NULL && group_b != NULL
  257. && EC_GROUP_cmp(group_a, group_b, NULL) == 0;
  258. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  259. const BIGNUM *pa = EC_KEY_get0_private_key(ec1);
  260. const BIGNUM *pb = EC_KEY_get0_private_key(ec2);
  261. ok = ok && BN_cmp(pa, pb) == 0;
  262. }
  263. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  264. const EC_POINT *pa = EC_KEY_get0_public_key(ec1);
  265. const EC_POINT *pb = EC_KEY_get0_public_key(ec2);
  266. ok = ok && EC_POINT_cmp(group_b, pa, pb, NULL);
  267. }
  268. return ok;
  269. }
  270. static
  271. int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
  272. {
  273. EC_KEY *ec = keydata;
  274. int ok = 1;
  275. if (ec == NULL)
  276. return 0;
  277. /*
  278. * In this implementation, we can export/import only keydata in the
  279. * following combinations:
  280. * - domain parameters only
  281. * - public key with associated domain parameters (+optional other params)
  282. * - private key with associated public key and domain parameters
  283. * (+optional other params)
  284. *
  285. * This means:
  286. * - domain parameters must always be requested
  287. * - private key must be requested alongside public key
  288. * - other parameters must be requested only alongside a key
  289. */
  290. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
  291. return 0;
  292. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
  293. && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
  294. return 0;
  295. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
  296. && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  297. return 0;
  298. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  299. ok = ok && ec_key_domparams_fromdata(ec, params);
  300. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  301. int include_private =
  302. selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
  303. ok = ok && ec_key_fromdata(ec, params, include_private);
  304. }
  305. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
  306. ok = ok && ec_key_otherparams_fromdata(ec, params);
  307. return ok;
  308. }
  309. static
  310. int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
  311. void *cbarg)
  312. {
  313. EC_KEY *ec = keydata;
  314. OSSL_PARAM_BLD *tmpl;
  315. OSSL_PARAM *params = NULL;
  316. unsigned char *pub_key = NULL;
  317. int ok = 1;
  318. if (ec == NULL)
  319. return 0;
  320. /*
  321. * In this implementation, we can export/import only keydata in the
  322. * following combinations:
  323. * - domain parameters only
  324. * - public key with associated domain parameters (+optional other params)
  325. * - private key with associated public key and domain parameters
  326. * (+optional other params)
  327. *
  328. * This means:
  329. * - domain parameters must always be requested
  330. * - private key must be requested alongside public key
  331. * - other parameters must be requested only alongside a key
  332. */
  333. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
  334. return 0;
  335. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
  336. && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
  337. return 0;
  338. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
  339. && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  340. return 0;
  341. tmpl = OSSL_PARAM_BLD_new();
  342. if (tmpl == NULL)
  343. return 0;
  344. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  345. ok = ok && domparams_to_params(ec, tmpl, NULL);
  346. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  347. int include_private =
  348. selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
  349. ok = ok && key_to_params(ec, tmpl, NULL, include_private, &pub_key);
  350. }
  351. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
  352. ok = ok && otherparams_to_params(ec, tmpl, NULL);
  353. if (ok && (params = OSSL_PARAM_BLD_to_param(tmpl)) != NULL)
  354. ok = param_cb(params, cbarg);
  355. OSSL_PARAM_BLD_free_params(params);
  356. OSSL_PARAM_BLD_free(tmpl);
  357. OPENSSL_free(pub_key);
  358. return ok;
  359. }
  360. /* IMEXPORT = IMPORT + EXPORT */
  361. # define EC_IMEXPORTABLE_DOM_PARAMETERS \
  362. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0)
  363. # define EC_IMEXPORTABLE_PUBLIC_KEY \
  364. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
  365. # define EC_IMEXPORTABLE_PRIVATE_KEY \
  366. OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
  367. # define EC_IMEXPORTABLE_OTHER_PARAMETERS \
  368. OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL)
  369. /*
  370. * Include all the possible combinations of OSSL_PARAM arrays for
  371. * ec_imexport_types().
  372. *
  373. * They are in a separate file as it is ~100 lines of unreadable and
  374. * uninteresting machine generated stuff.
  375. *
  376. * TODO(3.0): the generated list looks quite ugly, as to cover all possible
  377. * combinations of the bits in `selection`, it also includes combinations that
  378. * are not really useful: we might want to consider alternatives to this
  379. * solution.
  380. */
  381. #include "ec_kmgmt_imexport.inc"
  382. static ossl_inline
  383. const OSSL_PARAM *ec_imexport_types(int selection)
  384. {
  385. int type_select = 0;
  386. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  387. type_select += 1;
  388. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  389. type_select += 2;
  390. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  391. type_select += 4;
  392. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
  393. type_select += 8;
  394. return ec_types[type_select];
  395. }
  396. static
  397. const OSSL_PARAM *ec_import_types(int selection)
  398. {
  399. return ec_imexport_types(selection);
  400. }
  401. static
  402. const OSSL_PARAM *ec_export_types(int selection)
  403. {
  404. return ec_imexport_types(selection);
  405. }
  406. static
  407. int ec_get_params(void *key, OSSL_PARAM params[])
  408. {
  409. int ret;
  410. EC_KEY *eck = key;
  411. const EC_GROUP *ecg = NULL;
  412. OSSL_PARAM *p;
  413. unsigned char *pub_key = NULL;
  414. ecg = EC_KEY_get0_group(eck);
  415. if (ecg == NULL)
  416. return 0;
  417. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
  418. && !OSSL_PARAM_set_int(p, ECDSA_size(eck)))
  419. return 0;
  420. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
  421. && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg)))
  422. return 0;
  423. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) {
  424. int ecbits, sec_bits;
  425. ecbits = EC_GROUP_order_bits(ecg);
  426. /*
  427. * The following estimates are based on the values published
  428. * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
  429. * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
  430. *
  431. * Note that the above reference explicitly categorizes algorithms in a
  432. * discrete set of values {80, 112, 128, 192, 256}, and that it is
  433. * relevant only for NIST approved Elliptic Curves, while OpenSSL
  434. * applies the same logic also to other curves.
  435. *
  436. * Classifications produced by other standardazing bodies might differ,
  437. * so the results provided for "bits of security" by this provider are
  438. * to be considered merely indicative, and it is the users'
  439. * responsibility to compare these values against the normative
  440. * references that may be relevant for their intent and purposes.
  441. */
  442. if (ecbits >= 512)
  443. sec_bits = 256;
  444. else if (ecbits >= 384)
  445. sec_bits = 192;
  446. else if (ecbits >= 256)
  447. sec_bits = 128;
  448. else if (ecbits >= 224)
  449. sec_bits = 112;
  450. else if (ecbits >= 160)
  451. sec_bits = 80;
  452. else
  453. sec_bits = ecbits / 2;
  454. if (!OSSL_PARAM_set_int(p, sec_bits))
  455. return 0;
  456. }
  457. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
  458. && !OSSL_PARAM_set_utf8_string(p, EC_DEFAULT_MD))
  459. return 0;
  460. p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
  461. if (p != NULL) {
  462. int ecdh_cofactor_mode = 0;
  463. ecdh_cofactor_mode =
  464. (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
  465. if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
  466. return 0;
  467. }
  468. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT)) != NULL) {
  469. BN_CTX *ctx = BN_CTX_new_ex(ec_key_get_libctx(key));
  470. if (ctx == NULL)
  471. return 0;
  472. p->return_size = EC_POINT_point2oct(EC_KEY_get0_group(key),
  473. EC_KEY_get0_public_key(key),
  474. POINT_CONVERSION_UNCOMPRESSED,
  475. p->data, p->return_size, ctx);
  476. BN_CTX_free(ctx);
  477. if (p->return_size == 0)
  478. return 0;
  479. }
  480. ret = domparams_to_params(eck, NULL, params)
  481. && key_to_params(eck, NULL, params, 1, &pub_key)
  482. && otherparams_to_params(eck, NULL, params);
  483. OPENSSL_free(pub_key);
  484. return ret;
  485. }
  486. static const OSSL_PARAM ec_known_gettable_params[] = {
  487. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  488. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  489. OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
  490. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0),
  491. EC_IMEXPORTABLE_DOM_PARAMETERS,
  492. EC_IMEXPORTABLE_PUBLIC_KEY,
  493. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
  494. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
  495. EC_IMEXPORTABLE_PRIVATE_KEY,
  496. EC_IMEXPORTABLE_OTHER_PARAMETERS,
  497. OSSL_PARAM_END
  498. };
  499. static
  500. const OSSL_PARAM *ec_gettable_params(void)
  501. {
  502. return ec_known_gettable_params;
  503. }
  504. static const OSSL_PARAM ec_known_settable_params[] = {
  505. OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
  506. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0),
  507. OSSL_PARAM_END
  508. };
  509. static
  510. const OSSL_PARAM *ec_settable_params(void)
  511. {
  512. return ec_known_settable_params;
  513. }
  514. static
  515. int ec_set_params(void *key, const OSSL_PARAM params[])
  516. {
  517. EC_KEY *eck = key;
  518. const OSSL_PARAM *p;
  519. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT);
  520. if (p != NULL) {
  521. BN_CTX *ctx = BN_CTX_new_ex(ec_key_get_libctx(key));
  522. int ret = 1;
  523. if (ctx == NULL
  524. || p->data_type != OSSL_PARAM_OCTET_STRING
  525. || !EC_KEY_oct2key(key, p->data, p->data_size, ctx))
  526. ret = 0;
  527. BN_CTX_free(ctx);
  528. if (!ret)
  529. return 0;
  530. }
  531. return ec_key_otherparams_fromdata(eck, params);
  532. }
  533. static
  534. int ec_validate(void *keydata, int selection)
  535. {
  536. EC_KEY *eck = keydata;
  537. int ok = 0;
  538. BN_CTX *ctx = BN_CTX_new_ex(ec_key_get_libctx(eck));
  539. if (ctx == NULL)
  540. return 0;
  541. if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
  542. ok = 1;
  543. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  544. ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
  545. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  546. ok = ok && ec_key_public_check(eck, ctx);
  547. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  548. ok = ok && ec_key_private_check(eck);
  549. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
  550. ok = ok && ec_key_pairwise_check(eck, ctx);
  551. BN_CTX_free(ctx);
  552. return ok;
  553. }
  554. struct ec_gen_ctx {
  555. OPENSSL_CTX *libctx;
  556. EC_GROUP *gen_group;
  557. int selection;
  558. int ecdh_mode;
  559. };
  560. static void *ec_gen_init(void *provctx, int selection)
  561. {
  562. OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
  563. struct ec_gen_ctx *gctx = NULL;
  564. if ((selection & (EC_POSSIBLE_SELECTIONS)) == 0)
  565. return NULL;
  566. if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
  567. gctx->libctx = libctx;
  568. gctx->gen_group = NULL;
  569. gctx->selection = selection;
  570. gctx->ecdh_mode = 0;
  571. }
  572. return gctx;
  573. }
  574. static int ec_gen_set_group(void *genctx, int nid)
  575. {
  576. struct ec_gen_ctx *gctx = genctx;
  577. EC_GROUP *group;
  578. group = EC_GROUP_new_by_curve_name_with_libctx(gctx->libctx, NULL, nid);
  579. if (group == NULL) {
  580. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
  581. return 0;
  582. }
  583. EC_GROUP_free(gctx->gen_group);
  584. gctx->gen_group = group;
  585. return 1;
  586. }
  587. static int ec_gen_set_template(void *genctx, void *templ)
  588. {
  589. struct ec_gen_ctx *gctx = genctx;
  590. EC_KEY *ec = templ;
  591. const EC_GROUP *ec_group;
  592. if (gctx == NULL || ec == NULL)
  593. return 0;
  594. if ((ec_group = EC_KEY_get0_group(ec)) == NULL)
  595. return 0;
  596. return ec_gen_set_group(gctx, EC_GROUP_get_curve_name(ec_group));
  597. }
  598. static int ec_gen_set_params(void *genctx, const OSSL_PARAM params[])
  599. {
  600. struct ec_gen_ctx *gctx = genctx;
  601. const OSSL_PARAM *p;
  602. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH))
  603. != NULL) {
  604. if (!OSSL_PARAM_get_int(p, &gctx->ecdh_mode))
  605. return 0;
  606. }
  607. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME))
  608. != NULL) {
  609. const char *curve_name = NULL;
  610. int ret = 0;
  611. switch (p->data_type) {
  612. case OSSL_PARAM_UTF8_STRING:
  613. /* The OSSL_PARAM functions have no support for this */
  614. curve_name = p->data;
  615. ret = (curve_name != NULL);
  616. break;
  617. case OSSL_PARAM_UTF8_PTR:
  618. ret = OSSL_PARAM_get_utf8_ptr(p, &curve_name);
  619. break;
  620. }
  621. if (ret) {
  622. int nid = ec_curve_name2nid(curve_name);
  623. if (nid == NID_undef) {
  624. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
  625. ret = 0;
  626. } else {
  627. ret = ec_gen_set_group(gctx, nid);
  628. }
  629. }
  630. return ret;
  631. }
  632. return 1;
  633. }
  634. static const OSSL_PARAM *ec_gen_settable_params(void *provctx)
  635. {
  636. static OSSL_PARAM settable[] = {
  637. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
  638. OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
  639. OSSL_PARAM_END
  640. };
  641. return settable;
  642. }
  643. static int ec_gen_assign_group(EC_KEY *ec, EC_GROUP *group)
  644. {
  645. if (group == NULL) {
  646. ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
  647. return 0;
  648. }
  649. return EC_KEY_set_group(ec, group) > 0;
  650. }
  651. /*
  652. * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
  653. */
  654. static void *ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  655. {
  656. struct ec_gen_ctx *gctx = genctx;
  657. EC_KEY *ec = NULL;
  658. int ret = 1; /* Start optimistically */
  659. if (gctx == NULL
  660. || (ec = EC_KEY_new_with_libctx(gctx->libctx, NULL)) == NULL)
  661. return NULL;
  662. /* We must always assign a group, no matter what */
  663. ret = ec_gen_assign_group(ec, gctx->gen_group);
  664. /* Whether you want it or not, you get a keypair, not just one half */
  665. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  666. ret = ret && EC_KEY_generate_key(ec);
  667. if (gctx->ecdh_mode != -1)
  668. ret = ret && ec_set_ecdh_cofactor_mode(ec, gctx->ecdh_mode);
  669. if (ret)
  670. return ec;
  671. /* Something went wrong, throw the key away */
  672. EC_KEY_free(ec);
  673. return NULL;
  674. }
  675. static void ec_gen_cleanup(void *genctx)
  676. {
  677. struct ec_gen_ctx *gctx = genctx;
  678. if (gctx == NULL)
  679. return;
  680. EC_GROUP_free(gctx->gen_group);
  681. OPENSSL_free(gctx);
  682. }
  683. const OSSL_DISPATCH ec_keymgmt_functions[] = {
  684. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
  685. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init },
  686. { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
  687. (void (*)(void))ec_gen_set_template },
  688. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
  689. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
  690. (void (*)(void))ec_gen_settable_params },
  691. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ec_gen },
  692. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
  693. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
  694. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params },
  695. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params },
  696. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params },
  697. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params },
  698. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
  699. { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
  700. { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate },
  701. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
  702. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
  703. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
  704. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
  705. { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
  706. (void (*)(void))ec_query_operation_name },
  707. { 0, NULL }
  708. };