2
0

ec_ameth.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * Copyright 2006-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. * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
  11. * for internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/x509.h>
  17. #include <openssl/ec.h>
  18. #include <openssl/bn.h>
  19. #include <openssl/asn1t.h>
  20. #include "crypto/asn1.h"
  21. #include "crypto/evp.h"
  22. #include "crypto/x509.h"
  23. #include <openssl/core_names.h>
  24. #include <openssl/param_build.h>
  25. #include "ec_local.h"
  26. static int eckey_param2type(int *pptype, void **ppval, const EC_KEY *ec_key)
  27. {
  28. const EC_GROUP *group;
  29. int nid;
  30. if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
  31. ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
  32. return 0;
  33. }
  34. if (EC_GROUP_get_asn1_flag(group)
  35. && (nid = EC_GROUP_get_curve_name(group)))
  36. /* we have a 'named curve' => just set the OID */
  37. {
  38. ASN1_OBJECT *asn1obj = OBJ_nid2obj(nid);
  39. if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
  40. ERR_raise(ERR_LIB_EC, EC_R_MISSING_OID);
  41. return 0;
  42. }
  43. *ppval = asn1obj;
  44. *pptype = V_ASN1_OBJECT;
  45. } else { /* explicit parameters */
  46. ASN1_STRING *pstr = NULL;
  47. pstr = ASN1_STRING_new();
  48. if (pstr == NULL)
  49. return 0;
  50. pstr->length = i2d_ECParameters(ec_key, &pstr->data);
  51. if (pstr->length <= 0) {
  52. ASN1_STRING_free(pstr);
  53. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  54. return 0;
  55. }
  56. *ppval = pstr;
  57. *pptype = V_ASN1_SEQUENCE;
  58. }
  59. return 1;
  60. }
  61. static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  62. {
  63. const EC_KEY *ec_key = pkey->pkey.ec;
  64. void *pval = NULL;
  65. int ptype;
  66. unsigned char *penc = NULL, *p;
  67. int penclen;
  68. if (!eckey_param2type(&ptype, &pval, ec_key)) {
  69. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  70. return 0;
  71. }
  72. penclen = i2o_ECPublicKey(ec_key, NULL);
  73. if (penclen <= 0)
  74. goto err;
  75. penc = OPENSSL_malloc(penclen);
  76. if (penc == NULL)
  77. goto err;
  78. p = penc;
  79. penclen = i2o_ECPublicKey(ec_key, &p);
  80. if (penclen <= 0)
  81. goto err;
  82. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
  83. ptype, pval, penc, penclen))
  84. return 1;
  85. err:
  86. if (ptype == V_ASN1_SEQUENCE)
  87. ASN1_STRING_free(pval);
  88. OPENSSL_free(penc);
  89. return 0;
  90. }
  91. static int eckey_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
  92. {
  93. const unsigned char *p = NULL;
  94. int pklen;
  95. EC_KEY *eckey = NULL;
  96. X509_ALGOR *palg;
  97. OSSL_LIB_CTX *libctx = NULL;
  98. const char *propq = NULL;
  99. if (!ossl_x509_PUBKEY_get0_libctx(&libctx, &propq, pubkey)
  100. || !X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
  101. return 0;
  102. eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);
  103. if (!eckey)
  104. return 0;
  105. /* We have parameters now set public key */
  106. if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
  107. ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
  108. goto ecerr;
  109. }
  110. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  111. return 1;
  112. ecerr:
  113. EC_KEY_free(eckey);
  114. return 0;
  115. }
  116. static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  117. {
  118. int r;
  119. const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
  120. const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
  121. *pb = EC_KEY_get0_public_key(b->pkey.ec);
  122. if (group == NULL || pa == NULL || pb == NULL)
  123. return -2;
  124. r = EC_POINT_cmp(group, pa, pb, NULL);
  125. if (r == 0)
  126. return 1;
  127. if (r == 1)
  128. return 0;
  129. return -2;
  130. }
  131. static int eckey_priv_decode_ex(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8,
  132. OSSL_LIB_CTX *libctx, const char *propq)
  133. {
  134. int ret = 0;
  135. EC_KEY *eckey = ossl_ec_key_from_pkcs8(p8, libctx, propq);
  136. if (eckey != NULL) {
  137. ret = 1;
  138. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  139. }
  140. return ret;
  141. }
  142. static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  143. {
  144. EC_KEY ec_key = *(pkey->pkey.ec);
  145. unsigned char *ep = NULL;
  146. int eplen, ptype;
  147. void *pval;
  148. unsigned int old_flags;
  149. if (!eckey_param2type(&ptype, &pval, &ec_key)) {
  150. ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
  151. return 0;
  152. }
  153. /* set the private key */
  154. /*
  155. * do not include the parameters in the SEC1 private key see PKCS#11
  156. * 12.11
  157. */
  158. old_flags = EC_KEY_get_enc_flags(&ec_key);
  159. EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS);
  160. eplen = i2d_ECPrivateKey(&ec_key, &ep);
  161. if (eplen <= 0) {
  162. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  163. goto err;
  164. }
  165. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
  166. ptype, pval, ep, eplen)) {
  167. ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
  168. OPENSSL_clear_free(ep, eplen);
  169. goto err;
  170. }
  171. return 1;
  172. err:
  173. if (ptype == V_ASN1_SEQUENCE)
  174. ASN1_STRING_free(pval);
  175. return 0;
  176. }
  177. static int int_ec_size(const EVP_PKEY *pkey)
  178. {
  179. return ECDSA_size(pkey->pkey.ec);
  180. }
  181. static int ec_bits(const EVP_PKEY *pkey)
  182. {
  183. return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
  184. }
  185. static int ec_security_bits(const EVP_PKEY *pkey)
  186. {
  187. int ecbits = ec_bits(pkey);
  188. if (ecbits >= 512)
  189. return 256;
  190. if (ecbits >= 384)
  191. return 192;
  192. if (ecbits >= 256)
  193. return 128;
  194. if (ecbits >= 224)
  195. return 112;
  196. if (ecbits >= 160)
  197. return 80;
  198. return ecbits / 2;
  199. }
  200. static int ec_missing_parameters(const EVP_PKEY *pkey)
  201. {
  202. if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
  203. return 1;
  204. return 0;
  205. }
  206. static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  207. {
  208. EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
  209. if (group == NULL)
  210. return 0;
  211. if (to->pkey.ec == NULL) {
  212. to->pkey.ec = EC_KEY_new();
  213. if (to->pkey.ec == NULL)
  214. goto err;
  215. }
  216. if (EC_KEY_set_group(to->pkey.ec, group) == 0)
  217. goto err;
  218. EC_GROUP_free(group);
  219. return 1;
  220. err:
  221. EC_GROUP_free(group);
  222. return 0;
  223. }
  224. static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  225. {
  226. const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
  227. *group_b = EC_KEY_get0_group(b->pkey.ec);
  228. if (group_a == NULL || group_b == NULL)
  229. return -2;
  230. if (EC_GROUP_cmp(group_a, group_b, NULL))
  231. return 0;
  232. else
  233. return 1;
  234. }
  235. static void int_ec_free(EVP_PKEY *pkey)
  236. {
  237. EC_KEY_free(pkey->pkey.ec);
  238. }
  239. typedef enum {
  240. EC_KEY_PRINT_PRIVATE,
  241. EC_KEY_PRINT_PUBLIC,
  242. EC_KEY_PRINT_PARAM
  243. } ec_print_t;
  244. static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
  245. {
  246. const char *ecstr;
  247. unsigned char *priv = NULL, *pub = NULL;
  248. size_t privlen = 0, publen = 0;
  249. int ret = 0;
  250. const EC_GROUP *group;
  251. if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
  252. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  253. return 0;
  254. }
  255. if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) {
  256. publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
  257. if (publen == 0)
  258. goto err;
  259. }
  260. if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
  261. privlen = EC_KEY_priv2buf(x, &priv);
  262. if (privlen == 0)
  263. goto err;
  264. }
  265. if (ktype == EC_KEY_PRINT_PRIVATE)
  266. ecstr = "Private-Key";
  267. else if (ktype == EC_KEY_PRINT_PUBLIC)
  268. ecstr = "Public-Key";
  269. else
  270. ecstr = "ECDSA-Parameters";
  271. if (!BIO_indent(bp, off, 128))
  272. goto err;
  273. if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
  274. EC_GROUP_order_bits(group)) <= 0)
  275. goto err;
  276. if (privlen != 0) {
  277. if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
  278. goto err;
  279. if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
  280. goto err;
  281. }
  282. if (publen != 0) {
  283. if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
  284. goto err;
  285. if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
  286. goto err;
  287. }
  288. if (!ECPKParameters_print(bp, group, off))
  289. goto err;
  290. ret = 1;
  291. err:
  292. if (!ret)
  293. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  294. OPENSSL_clear_free(priv, privlen);
  295. OPENSSL_free(pub);
  296. return ret;
  297. }
  298. static int eckey_param_decode(EVP_PKEY *pkey,
  299. const unsigned char **pder, int derlen)
  300. {
  301. EC_KEY *eckey;
  302. if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL)
  303. return 0;
  304. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  305. return 1;
  306. }
  307. static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  308. {
  309. return i2d_ECParameters(pkey->pkey.ec, pder);
  310. }
  311. static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  312. ASN1_PCTX *ctx)
  313. {
  314. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
  315. }
  316. static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  317. ASN1_PCTX *ctx)
  318. {
  319. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
  320. }
  321. static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  322. ASN1_PCTX *ctx)
  323. {
  324. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
  325. }
  326. static int old_ec_priv_decode(EVP_PKEY *pkey,
  327. const unsigned char **pder, int derlen)
  328. {
  329. EC_KEY *ec;
  330. if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL)
  331. return 0;
  332. EVP_PKEY_assign_EC_KEY(pkey, ec);
  333. return 1;
  334. }
  335. static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  336. {
  337. return i2d_ECPrivateKey(pkey->pkey.ec, pder);
  338. }
  339. static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  340. {
  341. switch (op) {
  342. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  343. if (EVP_PKEY_get_id(pkey) == EVP_PKEY_SM2) {
  344. /* For SM2, the only valid digest-alg is SM3 */
  345. *(int *)arg2 = NID_sm3;
  346. return 2; /* Make it mandatory */
  347. }
  348. *(int *)arg2 = NID_sha256;
  349. return 1;
  350. case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
  351. /* We should only be here if we have a legacy key */
  352. if (!ossl_assert(evp_pkey_is_legacy(pkey)))
  353. return 0;
  354. return EC_KEY_oct2key(evp_pkey_get0_EC_KEY_int(pkey), arg2, arg1, NULL);
  355. case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
  356. return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
  357. POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
  358. default:
  359. return -2;
  360. }
  361. }
  362. static int ec_pkey_check(const EVP_PKEY *pkey)
  363. {
  364. EC_KEY *eckey = pkey->pkey.ec;
  365. /* stay consistent to what EVP_PKEY_check demands */
  366. if (eckey->priv_key == NULL) {
  367. ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
  368. return 0;
  369. }
  370. return EC_KEY_check_key(eckey);
  371. }
  372. static int ec_pkey_public_check(const EVP_PKEY *pkey)
  373. {
  374. EC_KEY *eckey = pkey->pkey.ec;
  375. /*
  376. * Note: it unnecessary to check eckey->pub_key here since
  377. * it will be checked in EC_KEY_check_key(). In fact, the
  378. * EC_KEY_check_key() mainly checks the public key, and checks
  379. * the private key optionally (only if there is one). So if
  380. * someone passes a whole EC key (public + private), this
  381. * will also work...
  382. */
  383. return EC_KEY_check_key(eckey);
  384. }
  385. static int ec_pkey_param_check(const EVP_PKEY *pkey)
  386. {
  387. EC_KEY *eckey = pkey->pkey.ec;
  388. /* stay consistent to what EVP_PKEY_check demands */
  389. if (eckey->group == NULL) {
  390. ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
  391. return 0;
  392. }
  393. return EC_GROUP_check(eckey->group, NULL);
  394. }
  395. static
  396. size_t ec_pkey_dirty_cnt(const EVP_PKEY *pkey)
  397. {
  398. return pkey->pkey.ec->dirty_cnt;
  399. }
  400. static
  401. int ec_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
  402. OSSL_FUNC_keymgmt_import_fn *importer,
  403. OSSL_LIB_CTX *libctx, const char *propq)
  404. {
  405. const EC_KEY *eckey = NULL;
  406. const EC_GROUP *ecg = NULL;
  407. unsigned char *pub_key_buf = NULL, *gen_buf = NULL;
  408. size_t pub_key_buflen;
  409. OSSL_PARAM_BLD *tmpl;
  410. OSSL_PARAM *params = NULL;
  411. const BIGNUM *priv_key = NULL;
  412. const EC_POINT *pub_point = NULL;
  413. int selection = 0;
  414. int rv = 0;
  415. BN_CTX *bnctx = NULL;
  416. if (from == NULL
  417. || (eckey = from->pkey.ec) == NULL
  418. || (ecg = EC_KEY_get0_group(eckey)) == NULL)
  419. return 0;
  420. tmpl = OSSL_PARAM_BLD_new();
  421. if (tmpl == NULL)
  422. return 0;
  423. /*
  424. * EC_POINT_point2buf() can generate random numbers in some
  425. * implementations so we need to ensure we use the correct libctx.
  426. */
  427. bnctx = BN_CTX_new_ex(libctx);
  428. if (bnctx == NULL)
  429. goto err;
  430. BN_CTX_start(bnctx);
  431. /* export the domain parameters */
  432. if (!ossl_ec_group_todata(ecg, tmpl, NULL, libctx, propq, bnctx, &gen_buf))
  433. goto err;
  434. selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
  435. priv_key = EC_KEY_get0_private_key(eckey);
  436. pub_point = EC_KEY_get0_public_key(eckey);
  437. if (pub_point != NULL) {
  438. /* convert pub_point to a octet string according to the SECG standard */
  439. point_conversion_form_t format = EC_KEY_get_conv_form(eckey);
  440. if ((pub_key_buflen = EC_POINT_point2buf(ecg, pub_point,
  441. format,
  442. &pub_key_buf, bnctx)) == 0
  443. || !OSSL_PARAM_BLD_push_octet_string(tmpl,
  444. OSSL_PKEY_PARAM_PUB_KEY,
  445. pub_key_buf,
  446. pub_key_buflen))
  447. goto err;
  448. selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  449. }
  450. if (priv_key != NULL) {
  451. size_t sz;
  452. int ecbits;
  453. int ecdh_cofactor_mode;
  454. /*
  455. * Key import/export should never leak the bit length of the secret
  456. * scalar in the key.
  457. *
  458. * For this reason, on export we use padded BIGNUMs with fixed length.
  459. *
  460. * When importing we also should make sure that, even if short lived,
  461. * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
  462. * soon as possible, so that any processing of this BIGNUM might opt for
  463. * constant time implementations in the backend.
  464. *
  465. * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
  466. * to preallocate the BIGNUM internal buffer to a fixed public size big
  467. * enough that operations performed during the processing never trigger
  468. * a realloc which would leak the size of the scalar through memory
  469. * accesses.
  470. *
  471. * Fixed Length
  472. * ------------
  473. *
  474. * The order of the large prime subgroup of the curve is our choice for
  475. * a fixed public size, as that is generally the upper bound for
  476. * generating a private key in EC cryptosystems and should fit all valid
  477. * secret scalars.
  478. *
  479. * For padding on export we just use the bit length of the order
  480. * converted to bytes (rounding up).
  481. *
  482. * For preallocating the BIGNUM storage we look at the number of "words"
  483. * required for the internal representation of the order, and we
  484. * preallocate 2 extra "words" in case any of the subsequent processing
  485. * might temporarily overflow the order length.
  486. */
  487. ecbits = EC_GROUP_order_bits(ecg);
  488. if (ecbits <= 0)
  489. goto err;
  490. sz = (ecbits + 7) / 8;
  491. if (!OSSL_PARAM_BLD_push_BN_pad(tmpl,
  492. OSSL_PKEY_PARAM_PRIV_KEY,
  493. priv_key, sz))
  494. goto err;
  495. selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
  496. /*
  497. * The ECDH Cofactor Mode is defined only if the EC_KEY actually
  498. * contains a private key, so we check for the flag and export it only
  499. * in this case.
  500. */
  501. ecdh_cofactor_mode =
  502. (EC_KEY_get_flags(eckey) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
  503. /* Export the ECDH_COFACTOR_MODE parameter */
  504. if (!OSSL_PARAM_BLD_push_int(tmpl,
  505. OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
  506. ecdh_cofactor_mode))
  507. goto err;
  508. selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
  509. }
  510. params = OSSL_PARAM_BLD_to_param(tmpl);
  511. /* We export, the provider imports */
  512. rv = importer(to_keydata, selection, params);
  513. err:
  514. OSSL_PARAM_BLD_free(tmpl);
  515. OSSL_PARAM_free(params);
  516. OPENSSL_free(pub_key_buf);
  517. OPENSSL_free(gen_buf);
  518. BN_CTX_end(bnctx);
  519. BN_CTX_free(bnctx);
  520. return rv;
  521. }
  522. static int ec_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
  523. {
  524. EVP_PKEY_CTX *pctx = vpctx;
  525. EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  526. EC_KEY *ec = EC_KEY_new_ex(pctx->libctx, pctx->propquery);
  527. if (ec == NULL) {
  528. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  529. return 0;
  530. }
  531. if (!ossl_ec_group_fromdata(ec, params)
  532. || !ossl_ec_key_otherparams_fromdata(ec, params)
  533. || !ossl_ec_key_fromdata(ec, params, 1)
  534. || !EVP_PKEY_assign_EC_KEY(pkey, ec)) {
  535. EC_KEY_free(ec);
  536. return 0;
  537. }
  538. return 1;
  539. }
  540. static int ec_pkey_copy(EVP_PKEY *to, EVP_PKEY *from)
  541. {
  542. EC_KEY *eckey = from->pkey.ec;
  543. EC_KEY *dupkey = NULL;
  544. int ret;
  545. if (eckey != NULL) {
  546. dupkey = EC_KEY_dup(eckey);
  547. if (dupkey == NULL)
  548. return 0;
  549. } else {
  550. /* necessary to properly copy empty SM2 keys */
  551. return EVP_PKEY_set_type(to, from->type);
  552. }
  553. ret = EVP_PKEY_assign_EC_KEY(to, dupkey);
  554. if (!ret)
  555. EC_KEY_free(dupkey);
  556. return ret;
  557. }
  558. const EVP_PKEY_ASN1_METHOD ossl_eckey_asn1_meth = {
  559. EVP_PKEY_EC,
  560. EVP_PKEY_EC,
  561. 0,
  562. "EC",
  563. "OpenSSL EC algorithm",
  564. eckey_pub_decode,
  565. eckey_pub_encode,
  566. eckey_pub_cmp,
  567. eckey_pub_print,
  568. NULL,
  569. eckey_priv_encode,
  570. eckey_priv_print,
  571. int_ec_size,
  572. ec_bits,
  573. ec_security_bits,
  574. eckey_param_decode,
  575. eckey_param_encode,
  576. ec_missing_parameters,
  577. ec_copy_parameters,
  578. ec_cmp_parameters,
  579. eckey_param_print,
  580. 0,
  581. int_ec_free,
  582. ec_pkey_ctrl,
  583. old_ec_priv_decode,
  584. old_ec_priv_encode,
  585. 0, 0, 0,
  586. ec_pkey_check,
  587. ec_pkey_public_check,
  588. ec_pkey_param_check,
  589. 0, /* set_priv_key */
  590. 0, /* set_pub_key */
  591. 0, /* get_priv_key */
  592. 0, /* get_pub_key */
  593. ec_pkey_dirty_cnt,
  594. ec_pkey_export_to,
  595. ec_pkey_import_from,
  596. ec_pkey_copy,
  597. eckey_priv_decode_ex
  598. };
  599. #if !defined(OPENSSL_NO_SM2)
  600. const EVP_PKEY_ASN1_METHOD ossl_sm2_asn1_meth = {
  601. EVP_PKEY_SM2,
  602. EVP_PKEY_EC,
  603. ASN1_PKEY_ALIAS
  604. };
  605. #endif
  606. int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
  607. {
  608. int private = EC_KEY_get0_private_key(x) != NULL;
  609. return do_EC_KEY_print(bp, x, off,
  610. private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
  611. }
  612. int ECParameters_print(BIO *bp, const EC_KEY *x)
  613. {
  614. return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
  615. }