ec_backend.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * Copyright 2020-2022 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. * Low level APIs related to EC_KEY are deprecated for public use,
  11. * but still ok for internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/core_names.h>
  15. #include <openssl/objects.h>
  16. #include <openssl/params.h>
  17. #include <openssl/err.h>
  18. #ifndef FIPS_MODULE
  19. # include <openssl/engine.h>
  20. # include <openssl/x509.h>
  21. #endif
  22. #include "crypto/bn.h"
  23. #include "crypto/ec.h"
  24. #include "ec_local.h"
  25. #include "internal/e_os.h"
  26. #include "internal/nelem.h"
  27. #include "internal/param_build_set.h"
  28. /* Mapping between a flag and a name */
  29. static const OSSL_ITEM encoding_nameid_map[] = {
  30. { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT },
  31. { OPENSSL_EC_NAMED_CURVE, OSSL_PKEY_EC_ENCODING_GROUP },
  32. };
  33. static const OSSL_ITEM check_group_type_nameid_map[] = {
  34. { 0, OSSL_PKEY_EC_GROUP_CHECK_DEFAULT },
  35. { EC_FLAG_CHECK_NAMED_GROUP, OSSL_PKEY_EC_GROUP_CHECK_NAMED },
  36. { EC_FLAG_CHECK_NAMED_GROUP_NIST, OSSL_PKEY_EC_GROUP_CHECK_NAMED_NIST },
  37. };
  38. static const OSSL_ITEM format_nameid_map[] = {
  39. { (int)POINT_CONVERSION_UNCOMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_UNCOMPRESSED },
  40. { (int)POINT_CONVERSION_COMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_COMPRESSED },
  41. { (int)POINT_CONVERSION_HYBRID, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_HYBRID },
  42. };
  43. int ossl_ec_encoding_name2id(const char *name)
  44. {
  45. size_t i, sz;
  46. /* Return the default value if there is no name */
  47. if (name == NULL)
  48. return OPENSSL_EC_NAMED_CURVE;
  49. for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
  50. if (OPENSSL_strcasecmp(name, encoding_nameid_map[i].ptr) == 0)
  51. return encoding_nameid_map[i].id;
  52. }
  53. return -1;
  54. }
  55. static char *ec_param_encoding_id2name(int id)
  56. {
  57. size_t i, sz;
  58. for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
  59. if (id == (int)encoding_nameid_map[i].id)
  60. return encoding_nameid_map[i].ptr;
  61. }
  62. return NULL;
  63. }
  64. char *ossl_ec_check_group_type_id2name(int id)
  65. {
  66. size_t i, sz;
  67. for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
  68. if (id == (int)check_group_type_nameid_map[i].id)
  69. return check_group_type_nameid_map[i].ptr;
  70. }
  71. return NULL;
  72. }
  73. static int ec_check_group_type_name2id(const char *name)
  74. {
  75. size_t i, sz;
  76. /* Return the default value if there is no name */
  77. if (name == NULL)
  78. return 0;
  79. for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
  80. if (OPENSSL_strcasecmp(name, check_group_type_nameid_map[i].ptr) == 0)
  81. return check_group_type_nameid_map[i].id;
  82. }
  83. return -1;
  84. }
  85. int ossl_ec_set_check_group_type_from_name(EC_KEY *ec, const char *name)
  86. {
  87. int flags = ec_check_group_type_name2id(name);
  88. if (flags == -1)
  89. return 0;
  90. EC_KEY_clear_flags(ec, EC_FLAG_CHECK_NAMED_GROUP_MASK);
  91. EC_KEY_set_flags(ec, flags);
  92. return 1;
  93. }
  94. static int ec_set_check_group_type_from_param(EC_KEY *ec, const OSSL_PARAM *p)
  95. {
  96. const char *name = NULL;
  97. int status = 0;
  98. switch (p->data_type) {
  99. case OSSL_PARAM_UTF8_STRING:
  100. name = p->data;
  101. status = (name != NULL);
  102. break;
  103. case OSSL_PARAM_UTF8_PTR:
  104. status = OSSL_PARAM_get_utf8_ptr(p, &name);
  105. break;
  106. }
  107. if (status)
  108. return ossl_ec_set_check_group_type_from_name(ec, name);
  109. return 0;
  110. }
  111. int ossl_ec_pt_format_name2id(const char *name)
  112. {
  113. size_t i, sz;
  114. /* Return the default value if there is no name */
  115. if (name == NULL)
  116. return (int)POINT_CONVERSION_UNCOMPRESSED;
  117. for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
  118. if (OPENSSL_strcasecmp(name, format_nameid_map[i].ptr) == 0)
  119. return format_nameid_map[i].id;
  120. }
  121. return -1;
  122. }
  123. char *ossl_ec_pt_format_id2name(int id)
  124. {
  125. size_t i, sz;
  126. for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
  127. if (id == (int)format_nameid_map[i].id)
  128. return format_nameid_map[i].ptr;
  129. }
  130. return NULL;
  131. }
  132. static int ec_group_explicit_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl,
  133. OSSL_PARAM params[], BN_CTX *bnctx,
  134. unsigned char **genbuf)
  135. {
  136. int ret = 0, fid;
  137. const char *field_type;
  138. const OSSL_PARAM *param = NULL;
  139. const OSSL_PARAM *param_p = NULL;
  140. const OSSL_PARAM *param_a = NULL;
  141. const OSSL_PARAM *param_b = NULL;
  142. fid = EC_GROUP_get_field_type(group);
  143. if (fid == NID_X9_62_prime_field) {
  144. field_type = SN_X9_62_prime_field;
  145. } else if (fid == NID_X9_62_characteristic_two_field) {
  146. #ifdef OPENSSL_NO_EC2M
  147. ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
  148. goto err;
  149. #else
  150. field_type = SN_X9_62_characteristic_two_field;
  151. #endif
  152. } else {
  153. ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
  154. return 0;
  155. }
  156. param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
  157. param_a = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
  158. param_b = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
  159. if (tmpl != NULL || param_p != NULL || param_a != NULL || param_b != NULL)
  160. {
  161. BIGNUM *p = BN_CTX_get(bnctx);
  162. BIGNUM *a = BN_CTX_get(bnctx);
  163. BIGNUM *b = BN_CTX_get(bnctx);
  164. if (b == NULL) {
  165. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  166. goto err;
  167. }
  168. if (!EC_GROUP_get_curve(group, p, a, b, bnctx)) {
  169. ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
  170. goto err;
  171. }
  172. if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_P, p)
  173. || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_A, a)
  174. || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_B, b)) {
  175. ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
  176. goto err;
  177. }
  178. }
  179. param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
  180. if (tmpl != NULL || param != NULL) {
  181. const BIGNUM *order = EC_GROUP_get0_order(group);
  182. if (order == NULL) {
  183. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
  184. goto err;
  185. }
  186. if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_ORDER,
  187. order)) {
  188. ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
  189. goto err;
  190. }
  191. }
  192. param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
  193. if (tmpl != NULL || param != NULL) {
  194. if (!ossl_param_build_set_utf8_string(tmpl, params,
  195. OSSL_PKEY_PARAM_EC_FIELD_TYPE,
  196. field_type)) {
  197. ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
  198. goto err;
  199. }
  200. }
  201. param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
  202. if (tmpl != NULL || param != NULL) {
  203. size_t genbuf_len;
  204. const EC_POINT *genpt = EC_GROUP_get0_generator(group);
  205. point_conversion_form_t genform = EC_GROUP_get_point_conversion_form(group);
  206. if (genpt == NULL) {
  207. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
  208. goto err;
  209. }
  210. genbuf_len = EC_POINT_point2buf(group, genpt, genform, genbuf, bnctx);
  211. if (genbuf_len == 0) {
  212. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
  213. goto err;
  214. }
  215. if (!ossl_param_build_set_octet_string(tmpl, params,
  216. OSSL_PKEY_PARAM_EC_GENERATOR,
  217. *genbuf, genbuf_len)) {
  218. ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
  219. goto err;
  220. }
  221. }
  222. param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
  223. if (tmpl != NULL || param != NULL) {
  224. const BIGNUM *cofactor = EC_GROUP_get0_cofactor(group);
  225. if (cofactor != NULL
  226. && !ossl_param_build_set_bn(tmpl, params,
  227. OSSL_PKEY_PARAM_EC_COFACTOR, cofactor)) {
  228. ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
  229. goto err;
  230. }
  231. }
  232. param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
  233. if (tmpl != NULL || param != NULL) {
  234. unsigned char *seed = EC_GROUP_get0_seed(group);
  235. size_t seed_len = EC_GROUP_get_seed_len(group);
  236. if (seed != NULL
  237. && seed_len > 0
  238. && !ossl_param_build_set_octet_string(tmpl, params,
  239. OSSL_PKEY_PARAM_EC_SEED,
  240. seed, seed_len)) {
  241. ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
  242. goto err;
  243. }
  244. }
  245. ret = 1;
  246. err:
  247. return ret;
  248. }
  249. int ossl_ec_group_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl,
  250. OSSL_PARAM params[], OSSL_LIB_CTX *libctx,
  251. const char *propq,
  252. BN_CTX *bnctx, unsigned char **genbuf)
  253. {
  254. int ret = 0, curve_nid, encoding_flag;
  255. const char *encoding_name, *pt_form_name;
  256. point_conversion_form_t genform;
  257. if (group == NULL) {
  258. ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER);
  259. return 0;
  260. }
  261. genform = EC_GROUP_get_point_conversion_form(group);
  262. pt_form_name = ossl_ec_pt_format_id2name(genform);
  263. if (pt_form_name == NULL
  264. || !ossl_param_build_set_utf8_string(
  265. tmpl, params,
  266. OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, pt_form_name)) {
  267. ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
  268. return 0;
  269. }
  270. encoding_flag = EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE;
  271. encoding_name = ec_param_encoding_id2name(encoding_flag);
  272. if (encoding_name == NULL
  273. || !ossl_param_build_set_utf8_string(tmpl, params,
  274. OSSL_PKEY_PARAM_EC_ENCODING,
  275. encoding_name)) {
  276. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  277. return 0;
  278. }
  279. if (!ossl_param_build_set_int(tmpl, params,
  280. OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS,
  281. group->decoded_from_explicit_params))
  282. return 0;
  283. curve_nid = EC_GROUP_get_curve_name(group);
  284. /*
  285. * Get the explicit parameters in these two cases:
  286. * - We do not have a template, i.e. specific parameters are requested
  287. * - The curve is not a named curve
  288. */
  289. if (tmpl == NULL || curve_nid == NID_undef)
  290. if (!ec_group_explicit_todata(group, tmpl, params, bnctx, genbuf))
  291. goto err;
  292. if (curve_nid != NID_undef) {
  293. /* Named curve */
  294. const char *curve_name = OSSL_EC_curve_nid2name(curve_nid);
  295. if (curve_name == NULL
  296. || !ossl_param_build_set_utf8_string(tmpl, params,
  297. OSSL_PKEY_PARAM_GROUP_NAME,
  298. curve_name)) {
  299. ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
  300. goto err;
  301. }
  302. }
  303. ret = 1;
  304. err:
  305. return ret;
  306. }
  307. /*
  308. * The intention with the "backend" source file is to offer backend support
  309. * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
  310. * implementations alike.
  311. */
  312. int ossl_ec_set_ecdh_cofactor_mode(EC_KEY *ec, int mode)
  313. {
  314. const EC_GROUP *ecg = EC_KEY_get0_group(ec);
  315. const BIGNUM *cofactor;
  316. /*
  317. * mode can be only 0 for disable, or 1 for enable here.
  318. *
  319. * This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that
  320. * also supports mode == -1 with the meaning of "reset to the default for
  321. * the associated key".
  322. */
  323. if (mode < 0 || mode > 1)
  324. return 0;
  325. if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL )
  326. return 0;
  327. /* ECDH cofactor mode has no effect if cofactor is 1 */
  328. if (BN_is_one(cofactor))
  329. return 1;
  330. if (mode == 1)
  331. EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
  332. else if (mode == 0)
  333. EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);
  334. return 1;
  335. }
  336. /*
  337. * Callers of ossl_ec_key_fromdata MUST make sure that ec_key_params_fromdata has
  338. * been called before!
  339. *
  340. * This function only gets the bare keypair, domain parameters and other
  341. * parameters are treated separately, and domain parameters are required to
  342. * define a keypair.
  343. */
  344. int ossl_ec_key_fromdata(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
  345. {
  346. const OSSL_PARAM *param_priv_key = NULL, *param_pub_key = NULL;
  347. BN_CTX *ctx = NULL;
  348. BIGNUM *priv_key = NULL;
  349. unsigned char *pub_key = NULL;
  350. size_t pub_key_len;
  351. const EC_GROUP *ecg = NULL;
  352. EC_POINT *pub_point = NULL;
  353. int ok = 0;
  354. ecg = EC_KEY_get0_group(ec);
  355. if (ecg == NULL)
  356. return 0;
  357. param_pub_key =
  358. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
  359. if (include_private)
  360. param_priv_key =
  361. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
  362. ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
  363. if (ctx == NULL)
  364. goto err;
  365. if (param_pub_key != NULL)
  366. if (!OSSL_PARAM_get_octet_string(param_pub_key,
  367. (void **)&pub_key, 0, &pub_key_len)
  368. || (pub_point = EC_POINT_new(ecg)) == NULL
  369. || !EC_POINT_oct2point(ecg, pub_point, pub_key, pub_key_len, ctx))
  370. goto err;
  371. if (param_priv_key != NULL && include_private) {
  372. int fixed_words;
  373. const BIGNUM *order;
  374. /*
  375. * Key import/export should never leak the bit length of the secret
  376. * scalar in the key.
  377. *
  378. * For this reason, on export we use padded BIGNUMs with fixed length.
  379. *
  380. * When importing we also should make sure that, even if short lived,
  381. * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
  382. * soon as possible, so that any processing of this BIGNUM might opt for
  383. * constant time implementations in the backend.
  384. *
  385. * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
  386. * to preallocate the BIGNUM internal buffer to a fixed public size big
  387. * enough that operations performed during the processing never trigger
  388. * a realloc which would leak the size of the scalar through memory
  389. * accesses.
  390. *
  391. * Fixed Length
  392. * ------------
  393. *
  394. * The order of the large prime subgroup of the curve is our choice for
  395. * a fixed public size, as that is generally the upper bound for
  396. * generating a private key in EC cryptosystems and should fit all valid
  397. * secret scalars.
  398. *
  399. * For padding on export we just use the bit length of the order
  400. * converted to bytes (rounding up).
  401. *
  402. * For preallocating the BIGNUM storage we look at the number of "words"
  403. * required for the internal representation of the order, and we
  404. * preallocate 2 extra "words" in case any of the subsequent processing
  405. * might temporarily overflow the order length.
  406. */
  407. order = EC_GROUP_get0_order(ecg);
  408. if (order == NULL || BN_is_zero(order))
  409. goto err;
  410. fixed_words = bn_get_top(order) + 2;
  411. if ((priv_key = BN_secure_new()) == NULL)
  412. goto err;
  413. if (bn_wexpand(priv_key, fixed_words) == NULL)
  414. goto err;
  415. BN_set_flags(priv_key, BN_FLG_CONSTTIME);
  416. if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
  417. goto err;
  418. }
  419. if (priv_key != NULL
  420. && !EC_KEY_set_private_key(ec, priv_key))
  421. goto err;
  422. if (pub_point != NULL
  423. && !EC_KEY_set_public_key(ec, pub_point))
  424. goto err;
  425. ok = 1;
  426. err:
  427. BN_CTX_free(ctx);
  428. BN_clear_free(priv_key);
  429. OPENSSL_free(pub_key);
  430. EC_POINT_free(pub_point);
  431. return ok;
  432. }
  433. int ossl_ec_group_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
  434. {
  435. int ok = 0;
  436. EC_GROUP *group = NULL;
  437. if (ec == NULL)
  438. return 0;
  439. group = EC_GROUP_new_from_params(params, ossl_ec_key_get_libctx(ec),
  440. ossl_ec_key_get0_propq(ec));
  441. if (!EC_KEY_set_group(ec, group))
  442. goto err;
  443. ok = 1;
  444. err:
  445. EC_GROUP_free(group);
  446. return ok;
  447. }
  448. static int ec_key_point_format_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
  449. {
  450. const OSSL_PARAM *p;
  451. int format = -1;
  452. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
  453. if (p != NULL) {
  454. if (!ossl_ec_pt_format_param2id(p, &format)) {
  455. ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
  456. return 0;
  457. }
  458. EC_KEY_set_conv_form(ec, format);
  459. }
  460. return 1;
  461. }
  462. static int ec_key_group_check_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
  463. {
  464. const OSSL_PARAM *p;
  465. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE);
  466. if (p != NULL)
  467. return ec_set_check_group_type_from_param(ec, p);
  468. return 1;
  469. }
  470. static int ec_set_include_public(EC_KEY *ec, int include)
  471. {
  472. int flags = EC_KEY_get_enc_flags(ec);
  473. if (!include)
  474. flags |= EC_PKEY_NO_PUBKEY;
  475. else
  476. flags &= ~EC_PKEY_NO_PUBKEY;
  477. EC_KEY_set_enc_flags(ec, flags);
  478. return 1;
  479. }
  480. int ossl_ec_key_otherparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
  481. {
  482. const OSSL_PARAM *p;
  483. if (ec == NULL)
  484. return 0;
  485. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
  486. if (p != NULL) {
  487. int mode;
  488. if (!OSSL_PARAM_get_int(p, &mode)
  489. || !ossl_ec_set_ecdh_cofactor_mode(ec, mode))
  490. return 0;
  491. }
  492. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC);
  493. if (p != NULL) {
  494. int include = 1;
  495. if (!OSSL_PARAM_get_int(p, &include)
  496. || !ec_set_include_public(ec, include))
  497. return 0;
  498. }
  499. if (!ec_key_point_format_fromdata(ec, params))
  500. return 0;
  501. if (!ec_key_group_check_fromdata(ec, params))
  502. return 0;
  503. return 1;
  504. }
  505. int ossl_ec_key_is_foreign(const EC_KEY *ec)
  506. {
  507. #ifndef FIPS_MODULE
  508. if (ec->engine != NULL || EC_KEY_get_method(ec) != EC_KEY_OpenSSL())
  509. return 1;
  510. #endif
  511. return 0;
  512. }
  513. EC_KEY *ossl_ec_key_dup(const EC_KEY *src, int selection)
  514. {
  515. EC_KEY *ret;
  516. if (src == NULL) {
  517. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  518. return NULL;
  519. }
  520. if ((ret = ossl_ec_key_new_method_int(src->libctx, src->propq,
  521. src->engine)) == NULL)
  522. return NULL;
  523. /* copy the parameters */
  524. if (src->group != NULL
  525. && (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
  526. ret->group = ossl_ec_group_new_ex(src->libctx, src->propq,
  527. src->group->meth);
  528. if (ret->group == NULL
  529. || !EC_GROUP_copy(ret->group, src->group))
  530. goto err;
  531. if (src->meth != NULL)
  532. ret->meth = src->meth;
  533. }
  534. /* copy the public key */
  535. if (src->pub_key != NULL
  536. && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  537. if (ret->group == NULL)
  538. /* no parameter-less keys allowed */
  539. goto err;
  540. ret->pub_key = EC_POINT_new(ret->group);
  541. if (ret->pub_key == NULL
  542. || !EC_POINT_copy(ret->pub_key, src->pub_key))
  543. goto err;
  544. }
  545. /* copy the private key */
  546. if (src->priv_key != NULL
  547. && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  548. if (ret->group == NULL)
  549. /* no parameter-less keys allowed */
  550. goto err;
  551. ret->priv_key = BN_new();
  552. if (ret->priv_key == NULL || !BN_copy(ret->priv_key, src->priv_key))
  553. goto err;
  554. if (ret->group->meth->keycopy
  555. && ret->group->meth->keycopy(ret, src) == 0)
  556. goto err;
  557. }
  558. /* copy the rest */
  559. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
  560. ret->enc_flag = src->enc_flag;
  561. ret->conv_form = src->conv_form;
  562. }
  563. ret->version = src->version;
  564. ret->flags = src->flags;
  565. #ifndef FIPS_MODULE
  566. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
  567. &ret->ex_data, &src->ex_data))
  568. goto err;
  569. #endif
  570. if (ret->meth != NULL && ret->meth->copy != NULL) {
  571. if ((selection
  572. & OSSL_KEYMGMT_SELECT_KEYPAIR) != OSSL_KEYMGMT_SELECT_KEYPAIR)
  573. goto err;
  574. if (ret->meth->copy(ret, src) == 0)
  575. goto err;
  576. }
  577. return ret;
  578. err:
  579. EC_KEY_free(ret);
  580. return NULL;
  581. }
  582. int ossl_ec_encoding_param2id(const OSSL_PARAM *p, int *id)
  583. {
  584. const char *name = NULL;
  585. int status = 0;
  586. switch (p->data_type) {
  587. case OSSL_PARAM_UTF8_STRING:
  588. /* The OSSL_PARAM functions have no support for this */
  589. name = p->data;
  590. status = (name != NULL);
  591. break;
  592. case OSSL_PARAM_UTF8_PTR:
  593. status = OSSL_PARAM_get_utf8_ptr(p, &name);
  594. break;
  595. }
  596. if (status) {
  597. int i = ossl_ec_encoding_name2id(name);
  598. if (i >= 0) {
  599. *id = i;
  600. return 1;
  601. }
  602. }
  603. return 0;
  604. }
  605. int ossl_ec_pt_format_param2id(const OSSL_PARAM *p, int *id)
  606. {
  607. const char *name = NULL;
  608. int status = 0;
  609. switch (p->data_type) {
  610. case OSSL_PARAM_UTF8_STRING:
  611. /* The OSSL_PARAM functions have no support for this */
  612. name = p->data;
  613. status = (name != NULL);
  614. break;
  615. case OSSL_PARAM_UTF8_PTR:
  616. status = OSSL_PARAM_get_utf8_ptr(p, &name);
  617. break;
  618. }
  619. if (status) {
  620. int i = ossl_ec_pt_format_name2id(name);
  621. if (i >= 0) {
  622. *id = i;
  623. return 1;
  624. }
  625. }
  626. return 0;
  627. }
  628. #ifndef FIPS_MODULE
  629. int ossl_x509_algor_is_sm2(const X509_ALGOR *palg)
  630. {
  631. int ptype = 0;
  632. const void *pval = NULL;
  633. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  634. if (ptype == V_ASN1_OBJECT)
  635. return OBJ_obj2nid((ASN1_OBJECT *)pval) == NID_sm2;
  636. if (ptype == V_ASN1_SEQUENCE) {
  637. const ASN1_STRING *str = pval;
  638. const unsigned char *der = str->data;
  639. int derlen = str->length;
  640. EC_GROUP *group;
  641. int ret;
  642. if ((group = d2i_ECPKParameters(NULL, &der, derlen)) == NULL)
  643. ret = 0;
  644. else
  645. ret = (EC_GROUP_get_curve_name(group) == NID_sm2);
  646. EC_GROUP_free(group);
  647. return ret;
  648. }
  649. return 0;
  650. }
  651. EC_KEY *ossl_ec_key_param_from_x509_algor(const X509_ALGOR *palg,
  652. OSSL_LIB_CTX *libctx, const char *propq)
  653. {
  654. int ptype = 0;
  655. const void *pval = NULL;
  656. EC_KEY *eckey = NULL;
  657. EC_GROUP *group = NULL;
  658. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  659. if ((eckey = EC_KEY_new_ex(libctx, propq)) == NULL) {
  660. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  661. goto ecerr;
  662. }
  663. if (ptype == V_ASN1_SEQUENCE) {
  664. const ASN1_STRING *pstr = pval;
  665. const unsigned char *pm = pstr->data;
  666. int pmlen = pstr->length;
  667. if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) {
  668. ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
  669. goto ecerr;
  670. }
  671. } else if (ptype == V_ASN1_OBJECT) {
  672. const ASN1_OBJECT *poid = pval;
  673. /*
  674. * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
  675. */
  676. group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid));
  677. if (group == NULL)
  678. goto ecerr;
  679. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  680. if (EC_KEY_set_group(eckey, group) == 0)
  681. goto ecerr;
  682. EC_GROUP_free(group);
  683. } else {
  684. ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
  685. goto ecerr;
  686. }
  687. return eckey;
  688. ecerr:
  689. EC_KEY_free(eckey);
  690. EC_GROUP_free(group);
  691. return NULL;
  692. }
  693. EC_KEY *ossl_ec_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
  694. OSSL_LIB_CTX *libctx, const char *propq)
  695. {
  696. const unsigned char *p = NULL;
  697. int pklen;
  698. EC_KEY *eckey = NULL;
  699. const X509_ALGOR *palg;
  700. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
  701. return 0;
  702. eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);
  703. if (eckey == NULL)
  704. goto err;
  705. /* We have parameters now set private key */
  706. if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
  707. ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
  708. goto err;
  709. }
  710. return eckey;
  711. err:
  712. EC_KEY_free(eckey);
  713. return NULL;
  714. }
  715. #endif