ec_backend.c 25 KB

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