ec_key.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include "internal/cryptlib.h"
  11. #include <string.h>
  12. #include "ec_lcl.h"
  13. #include "internal/refcount.h"
  14. #include <openssl/err.h>
  15. #include <openssl/engine.h>
  16. EC_KEY *EC_KEY_new(void)
  17. {
  18. return EC_KEY_new_method(NULL);
  19. }
  20. EC_KEY *EC_KEY_new_by_curve_name(int nid)
  21. {
  22. EC_KEY *ret = EC_KEY_new();
  23. if (ret == NULL)
  24. return NULL;
  25. ret->group = EC_GROUP_new_by_curve_name(nid);
  26. if (ret->group == NULL) {
  27. EC_KEY_free(ret);
  28. return NULL;
  29. }
  30. if (ret->meth->set_group != NULL
  31. && ret->meth->set_group(ret, ret->group) == 0) {
  32. EC_KEY_free(ret);
  33. return NULL;
  34. }
  35. return ret;
  36. }
  37. void EC_KEY_free(EC_KEY *r)
  38. {
  39. int i;
  40. if (r == NULL)
  41. return;
  42. CRYPTO_DOWN_REF(&r->references, &i, r->lock);
  43. REF_PRINT_COUNT("EC_KEY", r);
  44. if (i > 0)
  45. return;
  46. REF_ASSERT_ISNT(i < 0);
  47. if (r->meth->finish != NULL)
  48. r->meth->finish(r);
  49. #ifndef OPENSSL_NO_ENGINE
  50. ENGINE_finish(r->engine);
  51. #endif
  52. if (r->group && r->group->meth->keyfinish)
  53. r->group->meth->keyfinish(r);
  54. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
  55. CRYPTO_THREAD_lock_free(r->lock);
  56. EC_GROUP_free(r->group);
  57. EC_POINT_free(r->pub_key);
  58. BN_clear_free(r->priv_key);
  59. OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
  60. }
  61. EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
  62. {
  63. if (dest == NULL || src == NULL) {
  64. ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
  65. return NULL;
  66. }
  67. if (src->meth != dest->meth) {
  68. if (dest->meth->finish != NULL)
  69. dest->meth->finish(dest);
  70. if (dest->group && dest->group->meth->keyfinish)
  71. dest->group->meth->keyfinish(dest);
  72. #ifndef OPENSSL_NO_ENGINE
  73. if (ENGINE_finish(dest->engine) == 0)
  74. return 0;
  75. dest->engine = NULL;
  76. #endif
  77. }
  78. /* copy the parameters */
  79. if (src->group != NULL) {
  80. const EC_METHOD *meth = EC_GROUP_method_of(src->group);
  81. /* clear the old group */
  82. EC_GROUP_free(dest->group);
  83. dest->group = EC_GROUP_new(meth);
  84. if (dest->group == NULL)
  85. return NULL;
  86. if (!EC_GROUP_copy(dest->group, src->group))
  87. return NULL;
  88. /* copy the public key */
  89. if (src->pub_key != NULL) {
  90. EC_POINT_free(dest->pub_key);
  91. dest->pub_key = EC_POINT_new(src->group);
  92. if (dest->pub_key == NULL)
  93. return NULL;
  94. if (!EC_POINT_copy(dest->pub_key, src->pub_key))
  95. return NULL;
  96. }
  97. /* copy the private key */
  98. if (src->priv_key != NULL) {
  99. if (dest->priv_key == NULL) {
  100. dest->priv_key = BN_new();
  101. if (dest->priv_key == NULL)
  102. return NULL;
  103. }
  104. if (!BN_copy(dest->priv_key, src->priv_key))
  105. return NULL;
  106. if (src->group->meth->keycopy
  107. && src->group->meth->keycopy(dest, src) == 0)
  108. return NULL;
  109. }
  110. }
  111. /* copy the rest */
  112. dest->enc_flag = src->enc_flag;
  113. dest->conv_form = src->conv_form;
  114. dest->version = src->version;
  115. dest->flags = src->flags;
  116. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
  117. &dest->ex_data, &src->ex_data))
  118. return NULL;
  119. if (src->meth != dest->meth) {
  120. #ifndef OPENSSL_NO_ENGINE
  121. if (src->engine != NULL && ENGINE_init(src->engine) == 0)
  122. return NULL;
  123. dest->engine = src->engine;
  124. #endif
  125. dest->meth = src->meth;
  126. }
  127. if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
  128. return NULL;
  129. return dest;
  130. }
  131. EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
  132. {
  133. EC_KEY *ret = EC_KEY_new_method(ec_key->engine);
  134. if (ret == NULL)
  135. return NULL;
  136. if (EC_KEY_copy(ret, ec_key) == NULL) {
  137. EC_KEY_free(ret);
  138. return NULL;
  139. }
  140. return ret;
  141. }
  142. int EC_KEY_up_ref(EC_KEY *r)
  143. {
  144. int i;
  145. if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
  146. return 0;
  147. REF_PRINT_COUNT("EC_KEY", r);
  148. REF_ASSERT_ISNT(i < 2);
  149. return ((i > 1) ? 1 : 0);
  150. }
  151. ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey)
  152. {
  153. return eckey->engine;
  154. }
  155. int EC_KEY_generate_key(EC_KEY *eckey)
  156. {
  157. if (eckey == NULL || eckey->group == NULL) {
  158. ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
  159. return 0;
  160. }
  161. if (eckey->meth->keygen != NULL)
  162. return eckey->meth->keygen(eckey);
  163. ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
  164. return 0;
  165. }
  166. int ossl_ec_key_gen(EC_KEY *eckey)
  167. {
  168. return eckey->group->meth->keygen(eckey);
  169. }
  170. int ec_key_simple_generate_key(EC_KEY *eckey)
  171. {
  172. int ok = 0;
  173. BN_CTX *ctx = NULL;
  174. BIGNUM *priv_key = NULL;
  175. const BIGNUM *order = NULL;
  176. EC_POINT *pub_key = NULL;
  177. if ((ctx = BN_CTX_new()) == NULL)
  178. goto err;
  179. if (eckey->priv_key == NULL) {
  180. priv_key = BN_new();
  181. if (priv_key == NULL)
  182. goto err;
  183. } else
  184. priv_key = eckey->priv_key;
  185. order = EC_GROUP_get0_order(eckey->group);
  186. if (order == NULL)
  187. goto err;
  188. do
  189. if (!BN_priv_rand_range(priv_key, order))
  190. goto err;
  191. while (BN_is_zero(priv_key)) ;
  192. if (eckey->pub_key == NULL) {
  193. pub_key = EC_POINT_new(eckey->group);
  194. if (pub_key == NULL)
  195. goto err;
  196. } else
  197. pub_key = eckey->pub_key;
  198. if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
  199. goto err;
  200. eckey->priv_key = priv_key;
  201. eckey->pub_key = pub_key;
  202. ok = 1;
  203. err:
  204. if (eckey->pub_key == NULL)
  205. EC_POINT_free(pub_key);
  206. if (eckey->priv_key != priv_key)
  207. BN_free(priv_key);
  208. BN_CTX_free(ctx);
  209. return ok;
  210. }
  211. int ec_key_simple_generate_public_key(EC_KEY *eckey)
  212. {
  213. return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
  214. NULL, NULL);
  215. }
  216. int EC_KEY_check_key(const EC_KEY *eckey)
  217. {
  218. if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
  219. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
  220. return 0;
  221. }
  222. if (eckey->group->meth->keycheck == NULL) {
  223. ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  224. return 0;
  225. }
  226. return eckey->group->meth->keycheck(eckey);
  227. }
  228. int ec_key_simple_check_key(const EC_KEY *eckey)
  229. {
  230. int ok = 0;
  231. BN_CTX *ctx = NULL;
  232. const BIGNUM *order = NULL;
  233. EC_POINT *point = NULL;
  234. if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
  235. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
  236. return 0;
  237. }
  238. if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
  239. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
  240. goto err;
  241. }
  242. if ((ctx = BN_CTX_new()) == NULL)
  243. goto err;
  244. if ((point = EC_POINT_new(eckey->group)) == NULL)
  245. goto err;
  246. /* testing whether the pub_key is on the elliptic curve */
  247. if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
  248. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
  249. goto err;
  250. }
  251. /* testing whether pub_key * order is the point at infinity */
  252. order = eckey->group->order;
  253. if (BN_is_zero(order)) {
  254. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
  255. goto err;
  256. }
  257. if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
  258. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
  259. goto err;
  260. }
  261. if (!EC_POINT_is_at_infinity(eckey->group, point)) {
  262. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
  263. goto err;
  264. }
  265. /*
  266. * in case the priv_key is present : check if generator * priv_key ==
  267. * pub_key
  268. */
  269. if (eckey->priv_key != NULL) {
  270. if (BN_cmp(eckey->priv_key, order) >= 0) {
  271. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
  272. goto err;
  273. }
  274. if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
  275. NULL, NULL, ctx)) {
  276. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
  277. goto err;
  278. }
  279. if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
  280. ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
  281. goto err;
  282. }
  283. }
  284. ok = 1;
  285. err:
  286. BN_CTX_free(ctx);
  287. EC_POINT_free(point);
  288. return ok;
  289. }
  290. int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
  291. BIGNUM *y)
  292. {
  293. BN_CTX *ctx = NULL;
  294. BIGNUM *tx, *ty;
  295. EC_POINT *point = NULL;
  296. int ok = 0;
  297. #ifndef OPENSSL_NO_EC2M
  298. int tmp_nid, is_char_two = 0;
  299. #endif
  300. if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
  301. ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
  302. ERR_R_PASSED_NULL_PARAMETER);
  303. return 0;
  304. }
  305. ctx = BN_CTX_new();
  306. if (ctx == NULL)
  307. return 0;
  308. BN_CTX_start(ctx);
  309. point = EC_POINT_new(key->group);
  310. if (point == NULL)
  311. goto err;
  312. tx = BN_CTX_get(ctx);
  313. ty = BN_CTX_get(ctx);
  314. if (ty == NULL)
  315. goto err;
  316. #ifndef OPENSSL_NO_EC2M
  317. tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group));
  318. if (tmp_nid == NID_X9_62_characteristic_two_field)
  319. is_char_two = 1;
  320. if (is_char_two) {
  321. if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point,
  322. x, y, ctx))
  323. goto err;
  324. if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point,
  325. tx, ty, ctx))
  326. goto err;
  327. } else
  328. #endif
  329. {
  330. if (!EC_POINT_set_affine_coordinates_GFp(key->group, point,
  331. x, y, ctx))
  332. goto err;
  333. if (!EC_POINT_get_affine_coordinates_GFp(key->group, point,
  334. tx, ty, ctx))
  335. goto err;
  336. }
  337. /*
  338. * Check if retrieved coordinates match originals and are less than field
  339. * order: if not values are out of range.
  340. */
  341. if (BN_cmp(x, tx) || BN_cmp(y, ty)
  342. || (BN_cmp(x, key->group->field) >= 0)
  343. || (BN_cmp(y, key->group->field) >= 0)) {
  344. ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
  345. EC_R_COORDINATES_OUT_OF_RANGE);
  346. goto err;
  347. }
  348. if (!EC_KEY_set_public_key(key, point))
  349. goto err;
  350. if (EC_KEY_check_key(key) == 0)
  351. goto err;
  352. ok = 1;
  353. err:
  354. BN_CTX_end(ctx);
  355. BN_CTX_free(ctx);
  356. EC_POINT_free(point);
  357. return ok;
  358. }
  359. const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
  360. {
  361. return key->group;
  362. }
  363. int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
  364. {
  365. if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
  366. return 0;
  367. EC_GROUP_free(key->group);
  368. key->group = EC_GROUP_dup(group);
  369. return (key->group == NULL) ? 0 : 1;
  370. }
  371. const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
  372. {
  373. return key->priv_key;
  374. }
  375. int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
  376. {
  377. if (key->group == NULL || key->group->meth == NULL)
  378. return 0;
  379. if (key->group->meth->set_private != NULL
  380. && key->group->meth->set_private(key, priv_key) == 0)
  381. return 0;
  382. if (key->meth->set_private != NULL
  383. && key->meth->set_private(key, priv_key) == 0)
  384. return 0;
  385. BN_clear_free(key->priv_key);
  386. key->priv_key = BN_dup(priv_key);
  387. return (key->priv_key == NULL) ? 0 : 1;
  388. }
  389. const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
  390. {
  391. return key->pub_key;
  392. }
  393. int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
  394. {
  395. if (key->meth->set_public != NULL
  396. && key->meth->set_public(key, pub_key) == 0)
  397. return 0;
  398. EC_POINT_free(key->pub_key);
  399. key->pub_key = EC_POINT_dup(pub_key, key->group);
  400. return (key->pub_key == NULL) ? 0 : 1;
  401. }
  402. unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
  403. {
  404. return key->enc_flag;
  405. }
  406. void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
  407. {
  408. key->enc_flag = flags;
  409. }
  410. point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
  411. {
  412. return key->conv_form;
  413. }
  414. void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
  415. {
  416. key->conv_form = cform;
  417. if (key->group != NULL)
  418. EC_GROUP_set_point_conversion_form(key->group, cform);
  419. }
  420. void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
  421. {
  422. if (key->group != NULL)
  423. EC_GROUP_set_asn1_flag(key->group, flag);
  424. }
  425. int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
  426. {
  427. if (key->group == NULL)
  428. return 0;
  429. return EC_GROUP_precompute_mult(key->group, ctx);
  430. }
  431. int EC_KEY_get_flags(const EC_KEY *key)
  432. {
  433. return key->flags;
  434. }
  435. void EC_KEY_set_flags(EC_KEY *key, int flags)
  436. {
  437. key->flags |= flags;
  438. }
  439. void EC_KEY_clear_flags(EC_KEY *key, int flags)
  440. {
  441. key->flags &= ~flags;
  442. }
  443. size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
  444. unsigned char **pbuf, BN_CTX *ctx)
  445. {
  446. if (key == NULL || key->pub_key == NULL || key->group == NULL)
  447. return 0;
  448. return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
  449. }
  450. int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
  451. BN_CTX *ctx)
  452. {
  453. if (key == NULL || key->group == NULL)
  454. return 0;
  455. if (key->pub_key == NULL)
  456. key->pub_key = EC_POINT_new(key->group);
  457. if (key->pub_key == NULL)
  458. return 0;
  459. if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
  460. return 0;
  461. /*
  462. * Save the point conversion form.
  463. * For non-custom curves the first octet of the buffer (excluding
  464. * the last significant bit) contains the point conversion form.
  465. * EC_POINT_oct2point() has already performed sanity checking of
  466. * the buffer so we know it is valid.
  467. */
  468. if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
  469. key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
  470. return 1;
  471. }
  472. size_t EC_KEY_priv2oct(const EC_KEY *eckey,
  473. unsigned char *buf, size_t len)
  474. {
  475. if (eckey->group == NULL || eckey->group->meth == NULL)
  476. return 0;
  477. if (eckey->group->meth->priv2oct == NULL) {
  478. ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  479. return 0;
  480. }
  481. return eckey->group->meth->priv2oct(eckey, buf, len);
  482. }
  483. size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
  484. unsigned char *buf, size_t len)
  485. {
  486. size_t buf_len;
  487. buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
  488. if (eckey->priv_key == NULL)
  489. return 0;
  490. if (buf == NULL)
  491. return buf_len;
  492. else if (len < buf_len)
  493. return 0;
  494. /* Octetstring may need leading zeros if BN is to short */
  495. if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
  496. ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
  497. return 0;
  498. }
  499. return buf_len;
  500. }
  501. int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
  502. {
  503. if (eckey->group == NULL || eckey->group->meth == NULL)
  504. return 0;
  505. if (eckey->group->meth->oct2priv == NULL) {
  506. ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  507. return 0;
  508. }
  509. return eckey->group->meth->oct2priv(eckey, buf, len);
  510. }
  511. int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
  512. {
  513. if (eckey->priv_key == NULL)
  514. eckey->priv_key = BN_secure_new();
  515. if (eckey->priv_key == NULL) {
  516. ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
  517. return 0;
  518. }
  519. eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
  520. if (eckey->priv_key == NULL) {
  521. ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
  522. return 0;
  523. }
  524. return 1;
  525. }
  526. size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
  527. {
  528. size_t len;
  529. unsigned char *buf;
  530. len = EC_KEY_priv2oct(eckey, NULL, 0);
  531. if (len == 0)
  532. return 0;
  533. if ((buf = OPENSSL_malloc(len)) == NULL) {
  534. ECerr(EC_F_EC_KEY_PRIV2BUF, ERR_R_MALLOC_FAILURE);
  535. return 0;
  536. }
  537. len = EC_KEY_priv2oct(eckey, buf, len);
  538. if (len == 0) {
  539. OPENSSL_free(buf);
  540. return 0;
  541. }
  542. *pbuf = buf;
  543. return len;
  544. }
  545. int EC_KEY_can_sign(const EC_KEY *eckey)
  546. {
  547. if (eckey->group == NULL || eckey->group->meth == NULL
  548. || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))
  549. return 0;
  550. return 1;
  551. }