ec_backend.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  533. if (src->engine != NULL && ENGINE_init(src->engine) == 0)
  534. goto err;
  535. ret->engine = src->engine;
  536. #endif
  537. ret->meth = src->meth;
  538. }
  539. }
  540. /* copy the public key */
  541. if (src->pub_key != NULL
  542. && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  543. if (ret->group == NULL)
  544. /* no parameter-less keys allowed */
  545. goto err;
  546. ret->pub_key = EC_POINT_new(ret->group);
  547. if (ret->pub_key == NULL
  548. || !EC_POINT_copy(ret->pub_key, src->pub_key))
  549. goto err;
  550. }
  551. /* copy the private key */
  552. if (src->priv_key != NULL
  553. && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  554. if (ret->group == NULL)
  555. /* no parameter-less keys allowed */
  556. goto err;
  557. ret->priv_key = BN_new();
  558. if (ret->priv_key == NULL || !BN_copy(ret->priv_key, src->priv_key))
  559. goto err;
  560. if (ret->group->meth->keycopy
  561. && ret->group->meth->keycopy(ret, src) == 0)
  562. goto err;
  563. }
  564. /* copy the rest */
  565. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
  566. ret->enc_flag = src->enc_flag;
  567. ret->conv_form = src->conv_form;
  568. }
  569. ret->version = src->version;
  570. ret->flags = src->flags;
  571. #ifndef FIPS_MODULE
  572. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
  573. &ret->ex_data, &src->ex_data))
  574. goto err;
  575. #endif
  576. if (ret->meth != NULL && ret->meth->copy != NULL) {
  577. if ((selection
  578. & OSSL_KEYMGMT_SELECT_KEYPAIR) != OSSL_KEYMGMT_SELECT_KEYPAIR)
  579. goto err;
  580. if (ret->meth->copy(ret, src) == 0)
  581. goto err;
  582. }
  583. return ret;
  584. err:
  585. EC_KEY_free(ret);
  586. return NULL;
  587. }
  588. int ossl_ec_encoding_param2id(const OSSL_PARAM *p, int *id)
  589. {
  590. const char *name = NULL;
  591. int status = 0;
  592. switch (p->data_type) {
  593. case OSSL_PARAM_UTF8_STRING:
  594. /* The OSSL_PARAM functions have no support for this */
  595. name = p->data;
  596. status = (name != NULL);
  597. break;
  598. case OSSL_PARAM_UTF8_PTR:
  599. status = OSSL_PARAM_get_utf8_ptr(p, &name);
  600. break;
  601. }
  602. if (status) {
  603. int i = ossl_ec_encoding_name2id(name);
  604. if (i >= 0) {
  605. *id = i;
  606. return 1;
  607. }
  608. }
  609. return 0;
  610. }
  611. int ossl_ec_pt_format_param2id(const OSSL_PARAM *p, int *id)
  612. {
  613. const char *name = NULL;
  614. int status = 0;
  615. switch (p->data_type) {
  616. case OSSL_PARAM_UTF8_STRING:
  617. /* The OSSL_PARAM functions have no support for this */
  618. name = p->data;
  619. status = (name != NULL);
  620. break;
  621. case OSSL_PARAM_UTF8_PTR:
  622. status = OSSL_PARAM_get_utf8_ptr(p, &name);
  623. break;
  624. }
  625. if (status) {
  626. int i = ossl_ec_pt_format_name2id(name);
  627. if (i >= 0) {
  628. *id = i;
  629. return 1;
  630. }
  631. }
  632. return 0;
  633. }
  634. #ifndef FIPS_MODULE
  635. int ossl_x509_algor_is_sm2(const X509_ALGOR *palg)
  636. {
  637. int ptype = 0;
  638. const void *pval = NULL;
  639. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  640. if (ptype == V_ASN1_OBJECT)
  641. return OBJ_obj2nid((ASN1_OBJECT *)pval) == NID_sm2;
  642. if (ptype == V_ASN1_SEQUENCE) {
  643. const ASN1_STRING *str = pval;
  644. const unsigned char *der = str->data;
  645. int derlen = str->length;
  646. EC_GROUP *group;
  647. int ret;
  648. if ((group = d2i_ECPKParameters(NULL, &der, derlen)) == NULL)
  649. ret = 0;
  650. else
  651. ret = (EC_GROUP_get_curve_name(group) == NID_sm2);
  652. EC_GROUP_free(group);
  653. return ret;
  654. }
  655. return 0;
  656. }
  657. EC_KEY *ossl_ec_key_param_from_x509_algor(const X509_ALGOR *palg,
  658. OSSL_LIB_CTX *libctx, const char *propq)
  659. {
  660. int ptype = 0;
  661. const void *pval = NULL;
  662. EC_KEY *eckey = NULL;
  663. EC_GROUP *group = NULL;
  664. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  665. if ((eckey = EC_KEY_new_ex(libctx, propq)) == NULL) {
  666. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  667. goto ecerr;
  668. }
  669. if (ptype == V_ASN1_SEQUENCE) {
  670. const ASN1_STRING *pstr = pval;
  671. const unsigned char *pm = pstr->data;
  672. int pmlen = pstr->length;
  673. if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) {
  674. ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
  675. goto ecerr;
  676. }
  677. } else if (ptype == V_ASN1_OBJECT) {
  678. const ASN1_OBJECT *poid = pval;
  679. /*
  680. * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
  681. */
  682. group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid));
  683. if (group == NULL)
  684. goto ecerr;
  685. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  686. if (EC_KEY_set_group(eckey, group) == 0)
  687. goto ecerr;
  688. EC_GROUP_free(group);
  689. } else {
  690. ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
  691. goto ecerr;
  692. }
  693. return eckey;
  694. ecerr:
  695. EC_KEY_free(eckey);
  696. EC_GROUP_free(group);
  697. return NULL;
  698. }
  699. EC_KEY *ossl_ec_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
  700. OSSL_LIB_CTX *libctx, const char *propq)
  701. {
  702. const unsigned char *p = NULL;
  703. int pklen;
  704. EC_KEY *eckey = NULL;
  705. const X509_ALGOR *palg;
  706. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
  707. return 0;
  708. eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);
  709. if (eckey == NULL)
  710. goto err;
  711. /* We have parameters now set private key */
  712. if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
  713. ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
  714. goto err;
  715. }
  716. return eckey;
  717. err:
  718. EC_KEY_free(eckey);
  719. return NULL;
  720. }
  721. #endif