ec_backend.c 24 KB

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