You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

563 lines
18 KiB

  1. /* Written by Nils Larsch for the OpenSSL project. */
  2. /* ====================================================================
  3. * Copyright (c) 2000-2003 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * licensing@OpenSSL.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com). */
  53. #include <openssl/ec.h>
  54. #include <limits.h>
  55. #include <string.h>
  56. #include <openssl/bytestring.h>
  57. #include <openssl/bn.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #include <openssl/nid.h>
  61. #include "../fipsmodule/ec/internal.h"
  62. #include "../bytestring/internal.h"
  63. #include "../internal.h"
  64. static const unsigned kParametersTag =
  65. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0;
  66. static const unsigned kPublicKeyTag =
  67. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
  68. EC_KEY *EC_KEY_parse_private_key(CBS *cbs, const EC_GROUP *group) {
  69. CBS ec_private_key, private_key;
  70. uint64_t version;
  71. if (!CBS_get_asn1(cbs, &ec_private_key, CBS_ASN1_SEQUENCE) ||
  72. !CBS_get_asn1_uint64(&ec_private_key, &version) ||
  73. version != 1 ||
  74. !CBS_get_asn1(&ec_private_key, &private_key, CBS_ASN1_OCTETSTRING)) {
  75. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  76. return NULL;
  77. }
  78. // Parse the optional parameters field.
  79. EC_GROUP *inner_group = NULL;
  80. EC_KEY *ret = NULL;
  81. BIGNUM *priv_key = NULL;
  82. if (CBS_peek_asn1_tag(&ec_private_key, kParametersTag)) {
  83. // Per SEC 1, as an alternative to omitting it, one is allowed to specify
  84. // this field and put in a NULL to mean inheriting this value. This was
  85. // omitted in a previous version of this logic without problems, so leave it
  86. // unimplemented.
  87. CBS child;
  88. if (!CBS_get_asn1(&ec_private_key, &child, kParametersTag)) {
  89. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  90. goto err;
  91. }
  92. inner_group = EC_KEY_parse_parameters(&child);
  93. if (inner_group == NULL) {
  94. goto err;
  95. }
  96. if (group == NULL) {
  97. group = inner_group;
  98. } else if (EC_GROUP_cmp(group, inner_group, NULL) != 0) {
  99. // If a group was supplied externally, it must match.
  100. OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
  101. goto err;
  102. }
  103. if (CBS_len(&child) != 0) {
  104. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  105. goto err;
  106. }
  107. }
  108. if (group == NULL) {
  109. OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
  110. goto err;
  111. }
  112. ret = EC_KEY_new();
  113. if (ret == NULL || !EC_KEY_set_group(ret, group)) {
  114. goto err;
  115. }
  116. // Although RFC 5915 specifies the length of the key, OpenSSL historically
  117. // got this wrong, so accept any length. See upstream's
  118. // 30cd4ff294252c4b6a4b69cbef6a5b4117705d22.
  119. priv_key = BN_bin2bn(CBS_data(&private_key), CBS_len(&private_key), NULL);
  120. ret->pub_key = EC_POINT_new(group);
  121. if (priv_key == NULL || ret->pub_key == NULL ||
  122. !EC_KEY_set_private_key(ret, priv_key)) {
  123. goto err;
  124. }
  125. if (CBS_peek_asn1_tag(&ec_private_key, kPublicKeyTag)) {
  126. CBS child, public_key;
  127. uint8_t padding;
  128. if (!CBS_get_asn1(&ec_private_key, &child, kPublicKeyTag) ||
  129. !CBS_get_asn1(&child, &public_key, CBS_ASN1_BITSTRING) ||
  130. // As in a SubjectPublicKeyInfo, the byte-encoded public key is then
  131. // encoded as a BIT STRING with bits ordered as in the DER encoding.
  132. !CBS_get_u8(&public_key, &padding) ||
  133. padding != 0 ||
  134. // Explicitly check |public_key| is non-empty to save the conversion
  135. // form later.
  136. CBS_len(&public_key) == 0 ||
  137. !EC_POINT_oct2point(group, ret->pub_key, CBS_data(&public_key),
  138. CBS_len(&public_key), NULL) ||
  139. CBS_len(&child) != 0) {
  140. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  141. goto err;
  142. }
  143. // Save the point conversion form.
  144. // TODO(davidben): Consider removing this.
  145. ret->conv_form =
  146. (point_conversion_form_t)(CBS_data(&public_key)[0] & ~0x01);
  147. } else {
  148. // Compute the public key instead.
  149. if (!ec_point_mul_scalar(group, &ret->pub_key->raw, &ret->priv_key->scalar,
  150. NULL, NULL)) {
  151. goto err;
  152. }
  153. // Remember the original private-key-only encoding.
  154. // TODO(davidben): Consider removing this.
  155. ret->enc_flag |= EC_PKEY_NO_PUBKEY;
  156. }
  157. if (CBS_len(&ec_private_key) != 0) {
  158. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  159. goto err;
  160. }
  161. // Ensure the resulting key is valid.
  162. if (!EC_KEY_check_key(ret)) {
  163. goto err;
  164. }
  165. BN_free(priv_key);
  166. EC_GROUP_free(inner_group);
  167. return ret;
  168. err:
  169. EC_KEY_free(ret);
  170. BN_free(priv_key);
  171. EC_GROUP_free(inner_group);
  172. return NULL;
  173. }
  174. int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key,
  175. unsigned enc_flags) {
  176. if (key == NULL || key->group == NULL || key->priv_key == NULL) {
  177. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  178. return 0;
  179. }
  180. CBB ec_private_key, private_key;
  181. if (!CBB_add_asn1(cbb, &ec_private_key, CBS_ASN1_SEQUENCE) ||
  182. !CBB_add_asn1_uint64(&ec_private_key, 1 /* version */) ||
  183. !CBB_add_asn1(&ec_private_key, &private_key, CBS_ASN1_OCTETSTRING) ||
  184. !BN_bn2cbb_padded(&private_key,
  185. BN_num_bytes(EC_GROUP_get0_order(key->group)),
  186. EC_KEY_get0_private_key(key))) {
  187. OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR);
  188. return 0;
  189. }
  190. if (!(enc_flags & EC_PKEY_NO_PARAMETERS)) {
  191. CBB child;
  192. if (!CBB_add_asn1(&ec_private_key, &child, kParametersTag) ||
  193. !EC_KEY_marshal_curve_name(&child, key->group) ||
  194. !CBB_flush(&ec_private_key)) {
  195. OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR);
  196. return 0;
  197. }
  198. }
  199. // TODO(fork): replace this flexibility with sensible default?
  200. if (!(enc_flags & EC_PKEY_NO_PUBKEY) && key->pub_key != NULL) {
  201. CBB child, public_key;
  202. if (!CBB_add_asn1(&ec_private_key, &child, kPublicKeyTag) ||
  203. !CBB_add_asn1(&child, &public_key, CBS_ASN1_BITSTRING) ||
  204. // As in a SubjectPublicKeyInfo, the byte-encoded public key is then
  205. // encoded as a BIT STRING with bits ordered as in the DER encoding.
  206. !CBB_add_u8(&public_key, 0 /* padding */) ||
  207. !EC_POINT_point2cbb(&public_key, key->group, key->pub_key,
  208. key->conv_form, NULL) ||
  209. !CBB_flush(&ec_private_key)) {
  210. OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR);
  211. return 0;
  212. }
  213. }
  214. if (!CBB_flush(cbb)) {
  215. OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR);
  216. return 0;
  217. }
  218. return 1;
  219. }
  220. // is_unsigned_integer returns one if |cbs| is a valid unsigned DER INTEGER and
  221. // zero otherwise.
  222. static int is_unsigned_integer(const CBS *cbs) {
  223. if (CBS_len(cbs) == 0) {
  224. return 0;
  225. }
  226. uint8_t byte = CBS_data(cbs)[0];
  227. if ((byte & 0x80) ||
  228. (byte == 0 && CBS_len(cbs) > 1 && (CBS_data(cbs)[1] & 0x80) == 0)) {
  229. // Negative or not minimally-encoded.
  230. return 0;
  231. }
  232. return 1;
  233. }
  234. // kPrimeFieldOID is the encoding of 1.2.840.10045.1.1.
  235. static const uint8_t kPrimeField[] = {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01};
  236. static int parse_explicit_prime_curve(CBS *in, CBS *out_prime, CBS *out_a,
  237. CBS *out_b, CBS *out_base_x,
  238. CBS *out_base_y, CBS *out_order) {
  239. // See RFC 3279, section 2.3.5. Note that RFC 3279 calls this structure an
  240. // ECParameters while RFC 5480 calls it a SpecifiedECDomain.
  241. CBS params, field_id, field_type, curve, base;
  242. uint64_t version;
  243. if (!CBS_get_asn1(in, &params, CBS_ASN1_SEQUENCE) ||
  244. !CBS_get_asn1_uint64(&params, &version) ||
  245. version != 1 ||
  246. !CBS_get_asn1(&params, &field_id, CBS_ASN1_SEQUENCE) ||
  247. !CBS_get_asn1(&field_id, &field_type, CBS_ASN1_OBJECT) ||
  248. CBS_len(&field_type) != sizeof(kPrimeField) ||
  249. OPENSSL_memcmp(CBS_data(&field_type), kPrimeField, sizeof(kPrimeField)) != 0 ||
  250. !CBS_get_asn1(&field_id, out_prime, CBS_ASN1_INTEGER) ||
  251. !is_unsigned_integer(out_prime) ||
  252. CBS_len(&field_id) != 0 ||
  253. !CBS_get_asn1(&params, &curve, CBS_ASN1_SEQUENCE) ||
  254. !CBS_get_asn1(&curve, out_a, CBS_ASN1_OCTETSTRING) ||
  255. !CBS_get_asn1(&curve, out_b, CBS_ASN1_OCTETSTRING) ||
  256. // |curve| has an optional BIT STRING seed which we ignore.
  257. !CBS_get_asn1(&params, &base, CBS_ASN1_OCTETSTRING) ||
  258. !CBS_get_asn1(&params, out_order, CBS_ASN1_INTEGER) ||
  259. !is_unsigned_integer(out_order)) {
  260. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  261. return 0;
  262. }
  263. // |params| has an optional cofactor which we ignore. With the optional seed
  264. // in |curve|, a group already has arbitrarily many encodings. Parse enough to
  265. // uniquely determine the curve.
  266. // Require that the base point use uncompressed form.
  267. uint8_t form;
  268. if (!CBS_get_u8(&base, &form) || form != POINT_CONVERSION_UNCOMPRESSED) {
  269. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM);
  270. return 0;
  271. }
  272. if (CBS_len(&base) % 2 != 0) {
  273. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  274. return 0;
  275. }
  276. size_t field_len = CBS_len(&base) / 2;
  277. CBS_init(out_base_x, CBS_data(&base), field_len);
  278. CBS_init(out_base_y, CBS_data(&base) + field_len, field_len);
  279. return 1;
  280. }
  281. // integers_equal returns one if |a| and |b| are equal, up to leading zeros, and
  282. // zero otherwise.
  283. static int integers_equal(const CBS *a, const uint8_t *b, size_t b_len) {
  284. // Remove leading zeros from |a| and |b|.
  285. CBS a_copy = *a;
  286. while (CBS_len(&a_copy) > 0 && CBS_data(&a_copy)[0] == 0) {
  287. CBS_skip(&a_copy, 1);
  288. }
  289. while (b_len > 0 && b[0] == 0) {
  290. b++;
  291. b_len--;
  292. }
  293. return CBS_mem_equal(&a_copy, b, b_len);
  294. }
  295. EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs) {
  296. CBS named_curve;
  297. if (!CBS_get_asn1(cbs, &named_curve, CBS_ASN1_OBJECT)) {
  298. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  299. return NULL;
  300. }
  301. // Look for a matching curve.
  302. const struct built_in_curves *const curves = OPENSSL_built_in_curves();
  303. for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) {
  304. const struct built_in_curve *curve = &curves->curves[i];
  305. if (CBS_len(&named_curve) == curve->oid_len &&
  306. OPENSSL_memcmp(CBS_data(&named_curve), curve->oid, curve->oid_len) ==
  307. 0) {
  308. return EC_GROUP_new_by_curve_name(curve->nid);
  309. }
  310. }
  311. OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
  312. return NULL;
  313. }
  314. int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group) {
  315. int nid = EC_GROUP_get_curve_name(group);
  316. if (nid == NID_undef) {
  317. OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
  318. return 0;
  319. }
  320. const struct built_in_curves *const curves = OPENSSL_built_in_curves();
  321. for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) {
  322. const struct built_in_curve *curve = &curves->curves[i];
  323. if (curve->nid == nid) {
  324. CBB child;
  325. return CBB_add_asn1(cbb, &child, CBS_ASN1_OBJECT) &&
  326. CBB_add_bytes(&child, curve->oid, curve->oid_len) &&
  327. CBB_flush(cbb);
  328. }
  329. }
  330. OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
  331. return 0;
  332. }
  333. EC_GROUP *EC_KEY_parse_parameters(CBS *cbs) {
  334. if (!CBS_peek_asn1_tag(cbs, CBS_ASN1_SEQUENCE)) {
  335. return EC_KEY_parse_curve_name(cbs);
  336. }
  337. // OpenSSL sometimes produces ECPrivateKeys with explicitly-encoded versions
  338. // of named curves.
  339. //
  340. // TODO(davidben): Remove support for this.
  341. CBS prime, a, b, base_x, base_y, order;
  342. if (!parse_explicit_prime_curve(cbs, &prime, &a, &b, &base_x, &base_y,
  343. &order)) {
  344. return NULL;
  345. }
  346. // Look for a matching prime curve.
  347. const struct built_in_curves *const curves = OPENSSL_built_in_curves();
  348. for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) {
  349. const struct built_in_curve *curve = &curves->curves[i];
  350. const unsigned param_len = curve->param_len;
  351. // |curve->params| is ordered p, a, b, x, y, order, each component
  352. // zero-padded up to the field length. Although SEC 1 states that the
  353. // Field-Element-to-Octet-String conversion also pads, OpenSSL mis-encodes
  354. // |a| and |b|, so this comparison must allow omitting leading zeros. (This
  355. // is relevant for P-521 whose |b| has a leading 0.)
  356. if (integers_equal(&prime, curve->params, param_len) &&
  357. integers_equal(&a, curve->params + param_len, param_len) &&
  358. integers_equal(&b, curve->params + param_len * 2, param_len) &&
  359. integers_equal(&base_x, curve->params + param_len * 3, param_len) &&
  360. integers_equal(&base_y, curve->params + param_len * 4, param_len) &&
  361. integers_equal(&order, curve->params + param_len * 5, param_len)) {
  362. return EC_GROUP_new_by_curve_name(curve->nid);
  363. }
  364. }
  365. OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
  366. return NULL;
  367. }
  368. int EC_POINT_point2cbb(CBB *out, const EC_GROUP *group, const EC_POINT *point,
  369. point_conversion_form_t form, BN_CTX *ctx) {
  370. size_t len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx);
  371. if (len == 0) {
  372. return 0;
  373. }
  374. uint8_t *p;
  375. return CBB_add_space(out, &p, len) &&
  376. EC_POINT_point2oct(group, point, form, p, len, ctx) == len;
  377. }
  378. EC_KEY *d2i_ECPrivateKey(EC_KEY **out, const uint8_t **inp, long len) {
  379. // This function treats its |out| parameter differently from other |d2i|
  380. // functions. If supplied, take the group from |*out|.
  381. const EC_GROUP *group = NULL;
  382. if (out != NULL && *out != NULL) {
  383. group = EC_KEY_get0_group(*out);
  384. }
  385. if (len < 0) {
  386. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  387. return NULL;
  388. }
  389. CBS cbs;
  390. CBS_init(&cbs, *inp, (size_t)len);
  391. EC_KEY *ret = EC_KEY_parse_private_key(&cbs, group);
  392. if (ret == NULL) {
  393. return NULL;
  394. }
  395. if (out != NULL) {
  396. EC_KEY_free(*out);
  397. *out = ret;
  398. }
  399. *inp = CBS_data(&cbs);
  400. return ret;
  401. }
  402. int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp) {
  403. CBB cbb;
  404. if (!CBB_init(&cbb, 0) ||
  405. !EC_KEY_marshal_private_key(&cbb, key, EC_KEY_get_enc_flags(key))) {
  406. CBB_cleanup(&cbb);
  407. return -1;
  408. }
  409. return CBB_finish_i2d(&cbb, outp);
  410. }
  411. EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp, long len) {
  412. if (len < 0) {
  413. return NULL;
  414. }
  415. CBS cbs;
  416. CBS_init(&cbs, *inp, (size_t)len);
  417. EC_GROUP *group = EC_KEY_parse_parameters(&cbs);
  418. if (group == NULL) {
  419. return NULL;
  420. }
  421. EC_KEY *ret = EC_KEY_new();
  422. if (ret == NULL || !EC_KEY_set_group(ret, group)) {
  423. EC_GROUP_free(group);
  424. EC_KEY_free(ret);
  425. return NULL;
  426. }
  427. EC_GROUP_free(group);
  428. if (out_key != NULL) {
  429. EC_KEY_free(*out_key);
  430. *out_key = ret;
  431. }
  432. *inp = CBS_data(&cbs);
  433. return ret;
  434. }
  435. int i2d_ECParameters(const EC_KEY *key, uint8_t **outp) {
  436. if (key == NULL || key->group == NULL) {
  437. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  438. return -1;
  439. }
  440. CBB cbb;
  441. if (!CBB_init(&cbb, 0) ||
  442. !EC_KEY_marshal_curve_name(&cbb, key->group)) {
  443. CBB_cleanup(&cbb);
  444. return -1;
  445. }
  446. return CBB_finish_i2d(&cbb, outp);
  447. }
  448. EC_KEY *o2i_ECPublicKey(EC_KEY **keyp, const uint8_t **inp, long len) {
  449. EC_KEY *ret = NULL;
  450. if (keyp == NULL || *keyp == NULL || (*keyp)->group == NULL) {
  451. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  452. return NULL;
  453. }
  454. ret = *keyp;
  455. if (ret->pub_key == NULL &&
  456. (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
  457. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  458. return NULL;
  459. }
  460. if (!EC_POINT_oct2point(ret->group, ret->pub_key, *inp, len, NULL)) {
  461. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  462. return NULL;
  463. }
  464. // save the point conversion form
  465. ret->conv_form = (point_conversion_form_t)(*inp[0] & ~0x01);
  466. *inp += len;
  467. return ret;
  468. }
  469. int i2o_ECPublicKey(const EC_KEY *key, uint8_t **outp) {
  470. size_t buf_len = 0;
  471. int new_buffer = 0;
  472. if (key == NULL) {
  473. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  474. return 0;
  475. }
  476. buf_len = EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, NULL,
  477. 0, NULL);
  478. if (outp == NULL || buf_len == 0) {
  479. // out == NULL => just return the length of the octet string
  480. return buf_len;
  481. }
  482. if (*outp == NULL) {
  483. *outp = OPENSSL_malloc(buf_len);
  484. if (*outp == NULL) {
  485. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  486. return 0;
  487. }
  488. new_buffer = 1;
  489. }
  490. if (!EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, *outp,
  491. buf_len, NULL)) {
  492. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  493. if (new_buffer) {
  494. OPENSSL_free(*outp);
  495. *outp = NULL;
  496. }
  497. return 0;
  498. }
  499. if (!new_buffer) {
  500. *outp += buf_len;
  501. }
  502. return buf_len;
  503. }