ec_backend.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. int ossl_ec_group_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl,
  129. OSSL_PARAM params[], OSSL_LIB_CTX *libctx,
  130. const char *propq,
  131. BN_CTX *bnctx, unsigned char **genbuf)
  132. {
  133. int ret = 0, curve_nid, encoding_flag;
  134. const char *field_type, *encoding_name, *pt_form_name;
  135. const BIGNUM *cofactor, *order;
  136. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  137. point_conversion_form_t genform;
  138. const EC_POINT *genpt;
  139. unsigned char *seed = NULL;
  140. size_t genbuf_len, seed_len;
  141. if (group == NULL) {
  142. ERR_raise(ERR_LIB_EC,EC_R_PASSED_NULL_PARAMETER);
  143. return 0;
  144. }
  145. genform = EC_GROUP_get_point_conversion_form(group);
  146. pt_form_name = ossl_ec_pt_format_id2name(genform);
  147. if (pt_form_name == NULL
  148. || !ossl_param_build_set_utf8_string(
  149. tmpl, params,
  150. OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, pt_form_name)) {
  151. ECerr(0, EC_R_INVALID_FORM);
  152. return 0;
  153. }
  154. encoding_flag = EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE;
  155. encoding_name = ec_param_encoding_id2name(encoding_flag);
  156. if (encoding_name == NULL
  157. || !ossl_param_build_set_utf8_string(tmpl, params,
  158. OSSL_PKEY_PARAM_EC_ENCODING,
  159. encoding_name)) {
  160. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  161. return 0;
  162. }
  163. curve_nid = EC_GROUP_get_curve_name(group);
  164. if (curve_nid == NID_undef) {
  165. /* explicit curve */
  166. int fid = EC_GROUP_get_field_type(group);
  167. if (fid == NID_X9_62_prime_field) {
  168. field_type = SN_X9_62_prime_field;
  169. } else if (fid == NID_X9_62_characteristic_two_field) {
  170. field_type = SN_X9_62_characteristic_two_field;
  171. } else {
  172. ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
  173. return 0;
  174. }
  175. p = BN_CTX_get(bnctx);
  176. a = BN_CTX_get(bnctx);
  177. b = BN_CTX_get(bnctx);
  178. if (b == NULL) {
  179. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  180. goto err;
  181. }
  182. if (!EC_GROUP_get_curve(group, p, a, b, bnctx)) {
  183. ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
  184. goto err;
  185. }
  186. order = EC_GROUP_get0_order(group);
  187. if (order == NULL) {
  188. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
  189. goto err;
  190. }
  191. genpt = EC_GROUP_get0_generator(group);
  192. if (genpt == NULL) {
  193. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
  194. goto err;
  195. }
  196. genbuf_len = EC_POINT_point2buf(group, genpt, genform, genbuf, bnctx);
  197. if (genbuf_len == 0) {
  198. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
  199. goto err;
  200. }
  201. if (!ossl_param_build_set_utf8_string(tmpl, params,
  202. OSSL_PKEY_PARAM_EC_FIELD_TYPE,
  203. field_type)
  204. || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_P, p)
  205. || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_A, a)
  206. || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_B, b)
  207. || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_ORDER,
  208. order)
  209. || !ossl_param_build_set_octet_string(tmpl, params,
  210. OSSL_PKEY_PARAM_EC_GENERATOR,
  211. *genbuf, genbuf_len)) {
  212. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  213. goto err;
  214. }
  215. cofactor = EC_GROUP_get0_cofactor(group);
  216. if (cofactor != NULL
  217. && !ossl_param_build_set_bn(tmpl, params,
  218. OSSL_PKEY_PARAM_EC_COFACTOR, cofactor)) {
  219. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  220. goto err;
  221. }
  222. seed = EC_GROUP_get0_seed(group);
  223. seed_len = EC_GROUP_get_seed_len(group);
  224. if (seed != NULL
  225. && seed_len > 0
  226. && !ossl_param_build_set_octet_string(tmpl, params,
  227. OSSL_PKEY_PARAM_EC_SEED,
  228. seed, seed_len)) {
  229. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  230. goto err;
  231. }
  232. #ifdef OPENSSL_NO_EC2M
  233. if (fid == NID_X9_62_characteristic_two_field) {
  234. ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
  235. goto err;
  236. }
  237. #endif
  238. } else {
  239. /* named curve */
  240. const char *curve_name = ossl_ec_curve_nid2name(curve_nid);
  241. if (curve_name == NULL
  242. || !ossl_param_build_set_utf8_string(tmpl, params,
  243. OSSL_PKEY_PARAM_GROUP_NAME,
  244. curve_name)) {
  245. ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
  246. goto err;
  247. }
  248. }
  249. ret = 1;
  250. err:
  251. return ret;
  252. }
  253. /*
  254. * The intention with the "backend" source file is to offer backend support
  255. * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
  256. * implementations alike.
  257. */
  258. int ossl_ec_set_ecdh_cofactor_mode(EC_KEY *ec, int mode)
  259. {
  260. const EC_GROUP *ecg = EC_KEY_get0_group(ec);
  261. const BIGNUM *cofactor;
  262. /*
  263. * mode can be only 0 for disable, or 1 for enable here.
  264. *
  265. * This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that
  266. * also supports mode == -1 with the meaning of "reset to the default for
  267. * the associated key".
  268. */
  269. if (mode < 0 || mode > 1)
  270. return 0;
  271. if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL )
  272. return 0;
  273. /* ECDH cofactor mode has no effect if cofactor is 1 */
  274. if (BN_is_one(cofactor))
  275. return 1;
  276. if (mode == 1)
  277. EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
  278. else if (mode == 0)
  279. EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);
  280. return 1;
  281. }
  282. /*
  283. * Callers of ossl_ec_key_fromdata MUST make sure that ec_key_params_fromdata has
  284. * been called before!
  285. *
  286. * This function only gets the bare keypair, domain parameters and other
  287. * parameters are treated separately, and domain parameters are required to
  288. * define a keypair.
  289. */
  290. int ossl_ec_key_fromdata(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
  291. {
  292. const OSSL_PARAM *param_priv_key = NULL, *param_pub_key = NULL;
  293. BN_CTX *ctx = NULL;
  294. BIGNUM *priv_key = NULL;
  295. unsigned char *pub_key = NULL;
  296. size_t pub_key_len;
  297. const EC_GROUP *ecg = NULL;
  298. EC_POINT *pub_point = NULL;
  299. int ok = 0;
  300. ecg = EC_KEY_get0_group(ec);
  301. if (ecg == NULL)
  302. return 0;
  303. param_pub_key =
  304. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
  305. if (include_private)
  306. param_priv_key =
  307. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
  308. ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
  309. if (ctx == NULL)
  310. goto err;
  311. if (param_pub_key != NULL)
  312. if (!OSSL_PARAM_get_octet_string(param_pub_key,
  313. (void **)&pub_key, 0, &pub_key_len)
  314. || (pub_point = EC_POINT_new(ecg)) == NULL
  315. || !EC_POINT_oct2point(ecg, pub_point, pub_key, pub_key_len, ctx))
  316. goto err;
  317. if (param_priv_key != NULL && include_private) {
  318. int fixed_words;
  319. const BIGNUM *order;
  320. /*
  321. * Key import/export should never leak the bit length of the secret
  322. * scalar in the key.
  323. *
  324. * For this reason, on export we use padded BIGNUMs with fixed length.
  325. *
  326. * When importing we also should make sure that, even if short lived,
  327. * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
  328. * soon as possible, so that any processing of this BIGNUM might opt for
  329. * constant time implementations in the backend.
  330. *
  331. * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
  332. * to preallocate the BIGNUM internal buffer to a fixed public size big
  333. * enough that operations performed during the processing never trigger
  334. * a realloc which would leak the size of the scalar through memory
  335. * accesses.
  336. *
  337. * Fixed Length
  338. * ------------
  339. *
  340. * The order of the large prime subgroup of the curve is our choice for
  341. * a fixed public size, as that is generally the upper bound for
  342. * generating a private key in EC cryptosystems and should fit all valid
  343. * secret scalars.
  344. *
  345. * For padding on export we just use the bit length of the order
  346. * converted to bytes (rounding up).
  347. *
  348. * For preallocating the BIGNUM storage we look at the number of "words"
  349. * required for the internal representation of the order, and we
  350. * preallocate 2 extra "words" in case any of the subsequent processing
  351. * might temporarily overflow the order length.
  352. */
  353. order = EC_GROUP_get0_order(ecg);
  354. if (order == NULL || BN_is_zero(order))
  355. goto err;
  356. fixed_words = bn_get_top(order) + 2;
  357. if ((priv_key = BN_secure_new()) == NULL)
  358. goto err;
  359. if (bn_wexpand(priv_key, fixed_words) == NULL)
  360. goto err;
  361. BN_set_flags(priv_key, BN_FLG_CONSTTIME);
  362. if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
  363. goto err;
  364. }
  365. if (priv_key != NULL
  366. && !EC_KEY_set_private_key(ec, priv_key))
  367. goto err;
  368. if (pub_point != NULL
  369. && !EC_KEY_set_public_key(ec, pub_point))
  370. goto err;
  371. ok = 1;
  372. err:
  373. BN_CTX_free(ctx);
  374. BN_clear_free(priv_key);
  375. OPENSSL_free(pub_key);
  376. EC_POINT_free(pub_point);
  377. return ok;
  378. }
  379. int ossl_ec_group_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
  380. {
  381. int ok = 0;
  382. EC_GROUP *group = NULL;
  383. if (ec == NULL)
  384. return 0;
  385. group = EC_GROUP_new_from_params(params, ossl_ec_key_get_libctx(ec),
  386. ossl_ec_key_get0_propq(ec));
  387. if (!EC_KEY_set_group(ec, group))
  388. goto err;
  389. ok = 1;
  390. err:
  391. EC_GROUP_free(group);
  392. return ok;
  393. }
  394. static int ec_key_point_format_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
  395. {
  396. const OSSL_PARAM *p;
  397. int format = -1;
  398. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
  399. if (p != NULL) {
  400. if (!ossl_ec_pt_format_param2id(p, &format)) {
  401. ECerr(0, EC_R_INVALID_FORM);
  402. return 0;
  403. }
  404. EC_KEY_set_conv_form(ec, format);
  405. }
  406. return 1;
  407. }
  408. static int ec_key_group_check_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
  409. {
  410. const OSSL_PARAM *p;
  411. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE);
  412. if (p != NULL)
  413. return ec_set_check_group_type_from_param(ec, p);
  414. return 1;
  415. }
  416. static int ec_set_include_public(EC_KEY *ec, int include)
  417. {
  418. int flags = EC_KEY_get_enc_flags(ec);
  419. if (!include)
  420. flags |= EC_PKEY_NO_PUBKEY;
  421. else
  422. flags &= ~EC_PKEY_NO_PUBKEY;
  423. EC_KEY_set_enc_flags(ec, flags);
  424. return 1;
  425. }
  426. int ossl_ec_key_otherparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
  427. {
  428. const OSSL_PARAM *p;
  429. if (ec == NULL)
  430. return 0;
  431. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
  432. if (p != NULL) {
  433. int mode;
  434. if (!OSSL_PARAM_get_int(p, &mode)
  435. || !ossl_ec_set_ecdh_cofactor_mode(ec, mode))
  436. return 0;
  437. }
  438. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC);
  439. if (p != NULL) {
  440. int include = 1;
  441. if (!OSSL_PARAM_get_int(p, &include)
  442. || !ec_set_include_public(ec, include))
  443. return 0;
  444. }
  445. if (!ec_key_point_format_fromdata(ec, params))
  446. return 0;
  447. if (!ec_key_group_check_fromdata(ec, params))
  448. return 0;
  449. return 1;
  450. }
  451. EC_KEY *ossl_ec_key_dup(const EC_KEY *src, int selection)
  452. {
  453. EC_KEY *ret = ossl_ec_key_new_method_int(src->libctx, src->propq,
  454. src->engine);
  455. if (ret == NULL)
  456. return NULL;
  457. if (src == NULL) {
  458. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  459. goto err;
  460. }
  461. /* copy the parameters */
  462. if (src->group != NULL
  463. && (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
  464. ret->group = ossl_ec_group_new_ex(src->libctx, src->propq,
  465. src->group->meth);
  466. if (ret->group == NULL
  467. || !EC_GROUP_copy(ret->group, src->group))
  468. goto err;
  469. if (src->meth != NULL) {
  470. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  471. if (src->engine != NULL && ENGINE_init(src->engine) == 0)
  472. goto err;
  473. ret->engine = src->engine;
  474. #endif
  475. ret->meth = src->meth;
  476. }
  477. }
  478. /* copy the public key */
  479. if (src->pub_key != NULL
  480. && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  481. if (ret->group == NULL)
  482. /* no parameter-less keys allowed */
  483. goto err;
  484. ret->pub_key = EC_POINT_new(ret->group);
  485. if (ret->pub_key == NULL
  486. || !EC_POINT_copy(ret->pub_key, src->pub_key))
  487. goto err;
  488. }
  489. /* copy the private key */
  490. if (src->priv_key != NULL
  491. && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  492. if (ret->group == NULL)
  493. /* no parameter-less keys allowed */
  494. goto err;
  495. ret->priv_key = BN_new();
  496. if (ret->priv_key == NULL || !BN_copy(ret->priv_key, src->priv_key))
  497. goto err;
  498. if (ret->group->meth->keycopy
  499. && ret->group->meth->keycopy(ret, src) == 0)
  500. goto err;
  501. }
  502. /* copy the rest */
  503. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
  504. ret->enc_flag = src->enc_flag;
  505. ret->conv_form = src->conv_form;
  506. }
  507. ret->version = src->version;
  508. ret->flags = src->flags;
  509. #ifndef FIPS_MODULE
  510. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
  511. &ret->ex_data, &src->ex_data))
  512. goto err;
  513. #endif
  514. if (ret->meth != NULL && ret->meth->copy != NULL) {
  515. if ((selection
  516. & OSSL_KEYMGMT_SELECT_KEYPAIR) != OSSL_KEYMGMT_SELECT_KEYPAIR)
  517. goto err;
  518. if (ret->meth->copy(ret, src) == 0)
  519. goto err;
  520. }
  521. return ret;
  522. err:
  523. EC_KEY_free(ret);
  524. return NULL;
  525. }
  526. int ossl_ec_encoding_param2id(const OSSL_PARAM *p, int *id)
  527. {
  528. const char *name = NULL;
  529. int status = 0;
  530. switch (p->data_type) {
  531. case OSSL_PARAM_UTF8_STRING:
  532. /* The OSSL_PARAM functions have no support for this */
  533. name = p->data;
  534. status = (name != NULL);
  535. break;
  536. case OSSL_PARAM_UTF8_PTR:
  537. status = OSSL_PARAM_get_utf8_ptr(p, &name);
  538. break;
  539. }
  540. if (status) {
  541. int i = ossl_ec_encoding_name2id(name);
  542. if (i >= 0) {
  543. *id = i;
  544. return 1;
  545. }
  546. }
  547. return 0;
  548. }
  549. int ossl_ec_pt_format_param2id(const OSSL_PARAM *p, int *id)
  550. {
  551. const char *name = NULL;
  552. int status = 0;
  553. switch (p->data_type) {
  554. case OSSL_PARAM_UTF8_STRING:
  555. /* The OSSL_PARAM functions have no support for this */
  556. name = p->data;
  557. status = (name != NULL);
  558. break;
  559. case OSSL_PARAM_UTF8_PTR:
  560. status = OSSL_PARAM_get_utf8_ptr(p, &name);
  561. break;
  562. }
  563. if (status) {
  564. int i = ossl_ec_pt_format_name2id(name);
  565. if (i >= 0) {
  566. *id = i;
  567. return 1;
  568. }
  569. }
  570. return 0;
  571. }
  572. #ifndef FIPS_MODULE
  573. EC_KEY *ossl_ec_key_param_from_x509_algor(const X509_ALGOR *palg,
  574. OSSL_LIB_CTX *libctx, const char *propq)
  575. {
  576. int ptype = 0;
  577. const void *pval = NULL;
  578. EC_KEY *eckey = NULL;
  579. EC_GROUP *group = NULL;
  580. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  581. if ((eckey = EC_KEY_new_ex(libctx, propq)) == NULL) {
  582. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  583. goto ecerr;
  584. }
  585. if (ptype == V_ASN1_SEQUENCE) {
  586. const ASN1_STRING *pstr = pval;
  587. const unsigned char *pm = pstr->data;
  588. int pmlen = pstr->length;
  589. if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) {
  590. ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
  591. goto ecerr;
  592. }
  593. } else if (ptype == V_ASN1_OBJECT) {
  594. const ASN1_OBJECT *poid = pval;
  595. /*
  596. * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
  597. */
  598. group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid));
  599. if (group == NULL)
  600. goto ecerr;
  601. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  602. if (EC_KEY_set_group(eckey, group) == 0)
  603. goto ecerr;
  604. EC_GROUP_free(group);
  605. } else {
  606. ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
  607. goto ecerr;
  608. }
  609. return eckey;
  610. ecerr:
  611. EC_KEY_free(eckey);
  612. EC_GROUP_free(group);
  613. return NULL;
  614. }
  615. EC_KEY *ossl_ec_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
  616. OSSL_LIB_CTX *libctx, const char *propq)
  617. {
  618. const unsigned char *p = NULL;
  619. int pklen;
  620. EC_KEY *eckey = NULL;
  621. const X509_ALGOR *palg;
  622. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
  623. return 0;
  624. eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);
  625. if (eckey == NULL)
  626. goto err;
  627. /* We have parameters now set private key */
  628. if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
  629. ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
  630. goto err;
  631. }
  632. return eckey;
  633. err:
  634. EC_KEY_free(eckey);
  635. return NULL;
  636. }
  637. #endif