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.
 
 
 
 
 
 

587 lines
16 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 <string.h>
  55. #include <openssl/asn1.h>
  56. #include <openssl/asn1t.h>
  57. #include <openssl/bn.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #include <openssl/obj.h>
  61. #include "internal.h"
  62. typedef struct x9_62_fieldid_st {
  63. ASN1_OBJECT *fieldType;
  64. union {
  65. char *ptr;
  66. /* NID_X9_62_prime_field */
  67. ASN1_INTEGER *prime;
  68. /* anything else */
  69. ASN1_TYPE *other;
  70. } p;
  71. } X9_62_FIELDID;
  72. ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
  73. ASN1_ADB(X9_62_FIELDID) = {
  74. ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
  75. } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
  76. ASN1_SEQUENCE(X9_62_FIELDID) = {
  77. ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
  78. ASN1_ADB_OBJECT(X9_62_FIELDID)
  79. } ASN1_SEQUENCE_END(X9_62_FIELDID);
  80. typedef struct x9_62_curve_st {
  81. ASN1_OCTET_STRING *a;
  82. ASN1_OCTET_STRING *b;
  83. ASN1_BIT_STRING *seed;
  84. } X9_62_CURVE;
  85. ASN1_SEQUENCE(X9_62_CURVE) = {
  86. ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
  87. ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
  88. ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
  89. } ASN1_SEQUENCE_END(X9_62_CURVE);
  90. typedef struct ec_parameters_st {
  91. long version;
  92. X9_62_FIELDID *fieldID;
  93. X9_62_CURVE *curve;
  94. ASN1_OCTET_STRING *base;
  95. ASN1_INTEGER *order;
  96. ASN1_INTEGER *cofactor;
  97. } ECPARAMETERS;
  98. DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS);
  99. ASN1_SEQUENCE(ECPARAMETERS) = {
  100. ASN1_SIMPLE(ECPARAMETERS, version, LONG),
  101. ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
  102. ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
  103. ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
  104. ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
  105. ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
  106. } ASN1_SEQUENCE_END(ECPARAMETERS);
  107. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS);
  108. typedef struct ecpk_parameters_st {
  109. int type;
  110. union {
  111. ASN1_OBJECT *named_curve;
  112. ECPARAMETERS *parameters;
  113. } value;
  114. } ECPKPARAMETERS;
  115. /* SEC1 ECPrivateKey */
  116. typedef struct ec_privatekey_st {
  117. long version;
  118. ASN1_OCTET_STRING *privateKey;
  119. ECPKPARAMETERS *parameters;
  120. ASN1_BIT_STRING *publicKey;
  121. } EC_PRIVATEKEY;
  122. DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS);
  123. DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS);
  124. ASN1_CHOICE(ECPKPARAMETERS) = {
  125. ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
  126. ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
  127. } ASN1_CHOICE_END(ECPKPARAMETERS);
  128. IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS);
  129. DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY);
  130. DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY);
  131. ASN1_SEQUENCE(EC_PRIVATEKEY) = {
  132. ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
  133. ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
  134. ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
  135. ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1),
  136. } ASN1_SEQUENCE_END(EC_PRIVATEKEY);
  137. IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY);
  138. ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
  139. ECPKPARAMETERS *params) {
  140. int ok = 0, nid;
  141. ECPKPARAMETERS *ret = params;
  142. if (ret == NULL) {
  143. ret = ECPKPARAMETERS_new();
  144. if (ret == NULL) {
  145. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  146. return NULL;
  147. }
  148. } else {
  149. ASN1_OBJECT_free(ret->value.named_curve);
  150. }
  151. /* use the ASN.1 OID to describe the the elliptic curve parameters. */
  152. nid = EC_GROUP_get_curve_name(group);
  153. if (nid) {
  154. ret->type = 0;
  155. ret->value.named_curve = (ASN1_OBJECT*) OBJ_nid2obj(nid);
  156. ok = ret->value.named_curve != NULL;
  157. }
  158. if (!ok) {
  159. ECPKPARAMETERS_free(ret);
  160. return NULL;
  161. }
  162. return ret;
  163. }
  164. EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params) {
  165. EC_GROUP *ret = NULL;
  166. int nid = NID_undef;
  167. if (params == NULL) {
  168. OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
  169. return NULL;
  170. }
  171. if (params->type == 0) {
  172. nid = OBJ_obj2nid(params->value.named_curve);
  173. } else if (params->type == 1) {
  174. /* We don't support arbitary curves so we attempt to recognise it from the
  175. * group order. */
  176. const ECPARAMETERS *ecparams = params->value.parameters;
  177. unsigned i;
  178. const struct built_in_curve *curve;
  179. for (i = 0; OPENSSL_built_in_curves[i].nid != NID_undef; i++) {
  180. curve = &OPENSSL_built_in_curves[i];
  181. const unsigned param_len = curve->data->param_len;
  182. if ((unsigned) ecparams->order->length == param_len &&
  183. memcmp(ecparams->order->data, &curve->data->data[param_len * 5],
  184. param_len) == 0) {
  185. nid = curve->nid;
  186. break;
  187. }
  188. }
  189. }
  190. if (nid == NID_undef) {
  191. OPENSSL_PUT_ERROR(EC, EC_R_NON_NAMED_CURVE);
  192. return NULL;
  193. }
  194. ret = EC_GROUP_new_by_curve_name(nid);
  195. if (ret == NULL) {
  196. OPENSSL_PUT_ERROR(EC, EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
  197. return NULL;
  198. }
  199. return ret;
  200. }
  201. static EC_GROUP *d2i_ECPKParameters(EC_GROUP **groupp, const uint8_t **inp,
  202. long len) {
  203. EC_GROUP *group = NULL;
  204. ECPKPARAMETERS *params = NULL;
  205. const uint8_t *in = *inp;
  206. params = d2i_ECPKPARAMETERS(NULL, &in, len);
  207. if (params == NULL) {
  208. OPENSSL_PUT_ERROR(EC, EC_R_D2I_ECPKPARAMETERS_FAILURE);
  209. ECPKPARAMETERS_free(params);
  210. return NULL;
  211. }
  212. group = ec_asn1_pkparameters2group(params);
  213. if (group == NULL) {
  214. OPENSSL_PUT_ERROR(EC, EC_R_PKPARAMETERS2GROUP_FAILURE);
  215. ECPKPARAMETERS_free(params);
  216. return NULL;
  217. }
  218. if (groupp) {
  219. EC_GROUP_free(*groupp);
  220. *groupp = group;
  221. }
  222. ECPKPARAMETERS_free(params);
  223. *inp = in;
  224. return group;
  225. }
  226. static int i2d_ECPKParameters(const EC_GROUP *group, uint8_t **outp) {
  227. int ret = 0;
  228. ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(group, NULL);
  229. if (tmp == NULL) {
  230. OPENSSL_PUT_ERROR(EC, EC_R_GROUP2PKPARAMETERS_FAILURE);
  231. return 0;
  232. }
  233. ret = i2d_ECPKPARAMETERS(tmp, outp);
  234. if (ret == 0) {
  235. OPENSSL_PUT_ERROR(EC, EC_R_I2D_ECPKPARAMETERS_FAILURE);
  236. ECPKPARAMETERS_free(tmp);
  237. return 0;
  238. }
  239. ECPKPARAMETERS_free(tmp);
  240. return ret;
  241. }
  242. EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const uint8_t **inp, long len) {
  243. int ok = 0;
  244. EC_KEY *ret = NULL;
  245. EC_PRIVATEKEY *priv_key = NULL;
  246. const uint8_t *in = *inp;
  247. priv_key = d2i_EC_PRIVATEKEY(NULL, &in, len);
  248. if (priv_key == NULL) {
  249. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  250. return NULL;
  251. }
  252. if (a == NULL || *a == NULL) {
  253. ret = EC_KEY_new();
  254. if (ret == NULL) {
  255. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  256. goto err;
  257. }
  258. } else {
  259. ret = *a;
  260. }
  261. if (priv_key->parameters) {
  262. EC_GROUP_free(ret->group);
  263. ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
  264. }
  265. if (ret->group == NULL) {
  266. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  267. goto err;
  268. }
  269. ret->version = priv_key->version;
  270. if (priv_key->privateKey) {
  271. ret->priv_key =
  272. BN_bin2bn(M_ASN1_STRING_data(priv_key->privateKey),
  273. M_ASN1_STRING_length(priv_key->privateKey), ret->priv_key);
  274. if (ret->priv_key == NULL) {
  275. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  276. goto err;
  277. }
  278. } else {
  279. OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PRIVATE_KEY);
  280. goto err;
  281. }
  282. if (BN_cmp(ret->priv_key, EC_GROUP_get0_order(ret->group)) >= 0) {
  283. OPENSSL_PUT_ERROR(EC, EC_R_WRONG_ORDER);
  284. goto err;
  285. }
  286. EC_POINT_free(ret->pub_key);
  287. ret->pub_key = EC_POINT_new(ret->group);
  288. if (ret->pub_key == NULL) {
  289. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  290. goto err;
  291. }
  292. if (priv_key->publicKey) {
  293. const uint8_t *pub_oct;
  294. int pub_oct_len;
  295. pub_oct = M_ASN1_STRING_data(priv_key->publicKey);
  296. pub_oct_len = M_ASN1_STRING_length(priv_key->publicKey);
  297. /* The first byte (the point conversion form) must be present. */
  298. if (pub_oct_len <= 0) {
  299. OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
  300. goto err;
  301. }
  302. /* Save the point conversion form. */
  303. ret->conv_form = (point_conversion_form_t)(pub_oct[0] & ~0x01);
  304. if (!EC_POINT_oct2point(ret->group, ret->pub_key, pub_oct, pub_oct_len,
  305. NULL)) {
  306. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  307. goto err;
  308. }
  309. } else {
  310. if (!EC_POINT_mul(ret->group, ret->pub_key, ret->priv_key, NULL, NULL,
  311. NULL)) {
  312. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  313. goto err;
  314. }
  315. /* Remember the original private-key-only encoding. */
  316. ret->enc_flag |= EC_PKEY_NO_PUBKEY;
  317. }
  318. if (a) {
  319. *a = ret;
  320. }
  321. *inp = in;
  322. ok = 1;
  323. err:
  324. if (!ok) {
  325. if (a == NULL || *a != ret) {
  326. EC_KEY_free(ret);
  327. }
  328. ret = NULL;
  329. }
  330. EC_PRIVATEKEY_free(priv_key);
  331. return ret;
  332. }
  333. int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp) {
  334. int ret = 0, ok = 0;
  335. uint8_t *buffer = NULL;
  336. size_t buf_len = 0, tmp_len;
  337. EC_PRIVATEKEY *priv_key = NULL;
  338. if (key == NULL || key->group == NULL || key->priv_key == NULL) {
  339. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  340. goto err;
  341. }
  342. priv_key = EC_PRIVATEKEY_new();
  343. if (priv_key == NULL) {
  344. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  345. goto err;
  346. }
  347. priv_key->version = key->version;
  348. buf_len = BN_num_bytes(&key->group->order);
  349. buffer = OPENSSL_malloc(buf_len);
  350. if (buffer == NULL) {
  351. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  352. goto err;
  353. }
  354. if (!BN_bn2bin_padded(buffer, buf_len, key->priv_key)) {
  355. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  356. goto err;
  357. }
  358. if (!M_ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len)) {
  359. OPENSSL_PUT_ERROR(EC, ERR_R_ASN1_LIB);
  360. goto err;
  361. }
  362. /* TODO(fork): replace this flexibility with key sensible default? */
  363. if (!(key->enc_flag & EC_PKEY_NO_PARAMETERS)) {
  364. if ((priv_key->parameters = ec_asn1_group2pkparameters(
  365. key->group, priv_key->parameters)) == NULL) {
  366. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  367. goto err;
  368. }
  369. }
  370. /* TODO(fork): replace this flexibility with key sensible default? */
  371. if (!(key->enc_flag & EC_PKEY_NO_PUBKEY) && key->pub_key != NULL) {
  372. priv_key->publicKey = M_ASN1_BIT_STRING_new();
  373. if (priv_key->publicKey == NULL) {
  374. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  375. goto err;
  376. }
  377. tmp_len = EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, NULL,
  378. 0, NULL);
  379. if (tmp_len > buf_len) {
  380. uint8_t *tmp_buffer = OPENSSL_realloc(buffer, tmp_len);
  381. if (!tmp_buffer) {
  382. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  383. goto err;
  384. }
  385. buffer = tmp_buffer;
  386. buf_len = tmp_len;
  387. }
  388. if (!EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, buffer,
  389. buf_len, NULL)) {
  390. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  391. goto err;
  392. }
  393. priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  394. priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  395. if (!M_ASN1_BIT_STRING_set(priv_key->publicKey, buffer, buf_len)) {
  396. OPENSSL_PUT_ERROR(EC, ERR_R_ASN1_LIB);
  397. goto err;
  398. }
  399. }
  400. ret = i2d_EC_PRIVATEKEY(priv_key, outp);
  401. if (ret == 0) {
  402. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  403. goto err;
  404. }
  405. ok = 1;
  406. err:
  407. OPENSSL_free(buffer);
  408. EC_PRIVATEKEY_free(priv_key);
  409. return (ok ? ret : 0);
  410. }
  411. int i2d_ECParameters(const EC_KEY *key, uint8_t **outp) {
  412. if (key == NULL) {
  413. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  414. return 0;
  415. }
  416. return i2d_ECPKParameters(key->group, outp);
  417. }
  418. EC_KEY *d2i_ECParameters(EC_KEY **key, const uint8_t **inp, long len) {
  419. EC_KEY *ret;
  420. if (inp == NULL || *inp == NULL) {
  421. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  422. return NULL;
  423. }
  424. if (key == NULL || *key == NULL) {
  425. ret = EC_KEY_new();
  426. if (ret == NULL) {
  427. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  428. return NULL;
  429. }
  430. } else {
  431. ret = *key;
  432. }
  433. if (!d2i_ECPKParameters(&ret->group, inp, len)) {
  434. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  435. if (key == NULL || *key == NULL) {
  436. EC_KEY_free(ret);
  437. }
  438. return NULL;
  439. }
  440. if (key) {
  441. *key = ret;
  442. }
  443. return ret;
  444. }
  445. EC_KEY *o2i_ECPublicKey(EC_KEY **keyp, const uint8_t **inp, long len) {
  446. EC_KEY *ret = NULL;
  447. if (keyp == NULL || *keyp == NULL || (*keyp)->group == NULL) {
  448. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  449. return 0;
  450. }
  451. ret = *keyp;
  452. if (ret->pub_key == NULL &&
  453. (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
  454. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  455. return 0;
  456. }
  457. if (!EC_POINT_oct2point(ret->group, ret->pub_key, *inp, len, NULL)) {
  458. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  459. return 0;
  460. }
  461. /* save the point conversion form */
  462. ret->conv_form = (point_conversion_form_t)(*inp[0] & ~0x01);
  463. *inp += len;
  464. return ret;
  465. }
  466. int i2o_ECPublicKey(const EC_KEY *key, uint8_t **outp) {
  467. size_t buf_len = 0;
  468. int new_buffer = 0;
  469. if (key == NULL) {
  470. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  471. return 0;
  472. }
  473. buf_len = EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, NULL,
  474. 0, NULL);
  475. if (outp == NULL || buf_len == 0) {
  476. /* out == NULL => just return the length of the octet string */
  477. return buf_len;
  478. }
  479. if (*outp == NULL) {
  480. *outp = OPENSSL_malloc(buf_len);
  481. if (*outp == NULL) {
  482. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  483. return 0;
  484. }
  485. new_buffer = 1;
  486. }
  487. if (!EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, *outp,
  488. buf_len, NULL)) {
  489. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  490. if (new_buffer) {
  491. OPENSSL_free(*outp);
  492. *outp = NULL;
  493. }
  494. return 0;
  495. }
  496. if (!new_buffer) {
  497. *outp += buf_len;
  498. }
  499. return buf_len;
  500. }