ec_asn1.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 "internal.h"
  62. #include "../bytestring/internal.h"
  63. #include "../internal.h"
  64. static const uint8_t kParametersTag =
  65. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0;
  66. static const uint8_t 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. if (CBS_peek_asn1_tag(&ec_private_key, kParametersTag)) {
  82. /* Per SEC 1, as an alternative to omitting it, one is allowed to specify
  83. * this field and put in a NULL to mean inheriting this value. This was
  84. * omitted in a previous version of this logic without problems, so leave it
  85. * unimplemented. */
  86. CBS child;
  87. if (!CBS_get_asn1(&ec_private_key, &child, kParametersTag)) {
  88. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  89. goto err;
  90. }
  91. inner_group = EC_KEY_parse_parameters(&child);
  92. if (inner_group == NULL) {
  93. goto err;
  94. }
  95. if (group == NULL) {
  96. group = inner_group;
  97. } else if (EC_GROUP_cmp(group, inner_group, NULL) != 0) {
  98. /* If a group was supplied externally, it must match. */
  99. OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
  100. goto err;
  101. }
  102. if (CBS_len(&child) != 0) {
  103. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  104. goto err;
  105. }
  106. }
  107. if (group == NULL) {
  108. OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
  109. goto err;
  110. }
  111. ret = EC_KEY_new();
  112. if (ret == NULL || !EC_KEY_set_group(ret, group)) {
  113. goto err;
  114. }
  115. /* Although RFC 5915 specifies the length of the key, OpenSSL historically
  116. * got this wrong, so accept any length. See upstream's
  117. * 30cd4ff294252c4b6a4b69cbef6a5b4117705d22. */
  118. ret->priv_key =
  119. BN_bin2bn(CBS_data(&private_key), CBS_len(&private_key), NULL);
  120. ret->pub_key = EC_POINT_new(group);
  121. if (ret->priv_key == NULL || ret->pub_key == NULL) {
  122. goto err;
  123. }
  124. if (BN_cmp(ret->priv_key, EC_GROUP_get0_order(group)) >= 0) {
  125. OPENSSL_PUT_ERROR(EC, EC_R_WRONG_ORDER);
  126. goto err;
  127. }
  128. if (CBS_peek_asn1_tag(&ec_private_key, kPublicKeyTag)) {
  129. CBS child, public_key;
  130. uint8_t padding;
  131. if (!CBS_get_asn1(&ec_private_key, &child, kPublicKeyTag) ||
  132. !CBS_get_asn1(&child, &public_key, CBS_ASN1_BITSTRING) ||
  133. /* As in a SubjectPublicKeyInfo, the byte-encoded public key is then
  134. * encoded as a BIT STRING with bits ordered as in the DER encoding. */
  135. !CBS_get_u8(&public_key, &padding) ||
  136. padding != 0 ||
  137. /* Explicitly check |public_key| is non-empty to save the conversion
  138. * form later. */
  139. CBS_len(&public_key) == 0 ||
  140. !EC_POINT_oct2point(group, ret->pub_key, CBS_data(&public_key),
  141. CBS_len(&public_key), NULL) ||
  142. CBS_len(&child) != 0) {
  143. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  144. goto err;
  145. }
  146. /* Save the point conversion form.
  147. * TODO(davidben): Consider removing this. */
  148. ret->conv_form = (point_conversion_form_t)(CBS_data(&public_key)[0] & ~0x01);
  149. } else {
  150. /* Compute the public key instead. */
  151. if (!EC_POINT_mul(group, ret->pub_key, ret->priv_key, NULL, NULL, NULL)) {
  152. goto err;
  153. }
  154. /* Remember the original private-key-only encoding.
  155. * TODO(davidben): Consider removing this. */
  156. ret->enc_flag |= EC_PKEY_NO_PUBKEY;
  157. }
  158. if (CBS_len(&ec_private_key) != 0) {
  159. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  160. goto err;
  161. }
  162. /* Ensure the resulting key is valid. */
  163. if (!EC_KEY_check_key(ret)) {
  164. goto err;
  165. }
  166. EC_GROUP_free(inner_group);
  167. return ret;
  168. err:
  169. EC_KEY_free(ret);
  170. EC_GROUP_free(inner_group);
  171. return NULL;
  172. }
  173. int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key,
  174. unsigned enc_flags) {
  175. if (key == NULL || key->group == NULL || key->priv_key == NULL) {
  176. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  177. return 0;
  178. }
  179. CBB ec_private_key, private_key;
  180. if (!CBB_add_asn1(cbb, &ec_private_key, CBS_ASN1_SEQUENCE) ||
  181. !CBB_add_asn1_uint64(&ec_private_key, 1 /* version */) ||
  182. !CBB_add_asn1(&ec_private_key, &private_key, CBS_ASN1_OCTETSTRING) ||
  183. !BN_bn2cbb_padded(&private_key,
  184. BN_num_bytes(EC_GROUP_get0_order(key->group)),
  185. key->priv_key)) {
  186. OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR);
  187. return 0;
  188. }
  189. if (!(enc_flags & EC_PKEY_NO_PARAMETERS)) {
  190. CBB child;
  191. if (!CBB_add_asn1(&ec_private_key, &child, kParametersTag) ||
  192. !EC_KEY_marshal_curve_name(&child, key->group) ||
  193. !CBB_flush(&ec_private_key)) {
  194. OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR);
  195. return 0;
  196. }
  197. }
  198. /* TODO(fork): replace this flexibility with sensible default? */
  199. if (!(enc_flags & EC_PKEY_NO_PUBKEY) && key->pub_key != NULL) {
  200. CBB child, public_key;
  201. if (!CBB_add_asn1(&ec_private_key, &child, kPublicKeyTag) ||
  202. !CBB_add_asn1(&child, &public_key, CBS_ASN1_BITSTRING) ||
  203. /* As in a SubjectPublicKeyInfo, the byte-encoded public key is then
  204. * encoded as a BIT STRING with bits ordered as in the DER encoding. */
  205. !CBB_add_u8(&public_key, 0 /* padding */) ||
  206. !EC_POINT_point2cbb(&public_key, key->group, key->pub_key,
  207. key->conv_form, NULL) ||
  208. !CBB_flush(&ec_private_key)) {
  209. OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR);
  210. return 0;
  211. }
  212. }
  213. if (!CBB_flush(cbb)) {
  214. OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR);
  215. return 0;
  216. }
  217. return 1;
  218. }
  219. /* is_unsigned_integer returns one if |cbs| is a valid unsigned DER INTEGER and
  220. * zero otherwise. */
  221. static int is_unsigned_integer(const CBS *cbs) {
  222. if (CBS_len(cbs) == 0) {
  223. return 0;
  224. }
  225. uint8_t byte = CBS_data(cbs)[0];
  226. if ((byte & 0x80) ||
  227. (byte == 0 && CBS_len(cbs) > 1 && (CBS_data(cbs)[1] & 0x80) == 0)) {
  228. /* Negative or not minimally-encoded. */
  229. return 0;
  230. }
  231. return 1;
  232. }
  233. /* kPrimeFieldOID is the encoding of 1.2.840.10045.1.1. */
  234. static const uint8_t kPrimeField[] = {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01};
  235. static int parse_explicit_prime_curve(CBS *in, CBS *out_prime, CBS *out_a,
  236. CBS *out_b, CBS *out_base_x,
  237. CBS *out_base_y, CBS *out_order) {
  238. /* See RFC 3279, section 2.3.5. Note that RFC 3279 calls this structure an
  239. * ECParameters while RFC 5480 calls it a SpecifiedECDomain. */
  240. CBS params, field_id, field_type, curve, base;
  241. uint64_t version;
  242. if (!CBS_get_asn1(in, &params, CBS_ASN1_SEQUENCE) ||
  243. !CBS_get_asn1_uint64(&params, &version) ||
  244. version != 1 ||
  245. !CBS_get_asn1(&params, &field_id, CBS_ASN1_SEQUENCE) ||
  246. !CBS_get_asn1(&field_id, &field_type, CBS_ASN1_OBJECT) ||
  247. CBS_len(&field_type) != sizeof(kPrimeField) ||
  248. OPENSSL_memcmp(CBS_data(&field_type), kPrimeField, sizeof(kPrimeField)) != 0 ||
  249. !CBS_get_asn1(&field_id, out_prime, CBS_ASN1_INTEGER) ||
  250. !is_unsigned_integer(out_prime) ||
  251. CBS_len(&field_id) != 0 ||
  252. !CBS_get_asn1(&params, &curve, CBS_ASN1_SEQUENCE) ||
  253. !CBS_get_asn1(&curve, out_a, CBS_ASN1_OCTETSTRING) ||
  254. !CBS_get_asn1(&curve, out_b, CBS_ASN1_OCTETSTRING) ||
  255. /* |curve| has an optional BIT STRING seed which we ignore. */
  256. !CBS_get_asn1(&params, &base, CBS_ASN1_OCTETSTRING) ||
  257. !CBS_get_asn1(&params, out_order, CBS_ASN1_INTEGER) ||
  258. !is_unsigned_integer(out_order)) {
  259. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  260. return 0;
  261. }
  262. /* |params| has an optional cofactor which we ignore. With the optional seed
  263. * in |curve|, a group already has arbitrarily many encodings. Parse enough to
  264. * uniquely determine the curve. */
  265. /* Require that the base point use uncompressed form. */
  266. uint8_t form;
  267. if (!CBS_get_u8(&base, &form) || form != POINT_CONVERSION_UNCOMPRESSED) {
  268. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM);
  269. return 0;
  270. }
  271. if (CBS_len(&base) % 2 != 0) {
  272. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  273. return 0;
  274. }
  275. size_t field_len = CBS_len(&base) / 2;
  276. CBS_init(out_base_x, CBS_data(&base), field_len);
  277. CBS_init(out_base_y, CBS_data(&base) + field_len, field_len);
  278. return 1;
  279. }
  280. /* integers_equal returns one if |a| and |b| are equal, up to leading zeros, and
  281. * zero otherwise. */
  282. static int integers_equal(const CBS *a, const uint8_t *b, size_t b_len) {
  283. /* Remove leading zeros from |a| and |b|. */
  284. CBS a_copy = *a;
  285. while (CBS_len(&a_copy) > 0 && CBS_data(&a_copy)[0] == 0) {
  286. CBS_skip(&a_copy, 1);
  287. }
  288. while (b_len > 0 && b[0] == 0) {
  289. b++;
  290. b_len--;
  291. }
  292. return CBS_mem_equal(&a_copy, b, b_len);
  293. }
  294. EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs) {
  295. CBS named_curve;
  296. if (!CBS_get_asn1(cbs, &named_curve, CBS_ASN1_OBJECT)) {
  297. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  298. return NULL;
  299. }
  300. /* Look for a matching curve. */
  301. unsigned i;
  302. for (i = 0; OPENSSL_built_in_curves[i].nid != NID_undef; i++) {
  303. const struct built_in_curve *curve = &OPENSSL_built_in_curves[i];
  304. if (CBS_len(&named_curve) == curve->oid_len &&
  305. OPENSSL_memcmp(CBS_data(&named_curve), curve->oid, curve->oid_len) == 0) {
  306. return EC_GROUP_new_by_curve_name(curve->nid);
  307. }
  308. }
  309. OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
  310. return NULL;
  311. }
  312. int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group) {
  313. int nid = EC_GROUP_get_curve_name(group);
  314. if (nid == NID_undef) {
  315. OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
  316. return 0;
  317. }
  318. unsigned i;
  319. for (i = 0; OPENSSL_built_in_curves[i].nid != NID_undef; i++) {
  320. const struct built_in_curve *curve = &OPENSSL_built_in_curves[i];
  321. if (curve->nid == nid) {
  322. CBB child;
  323. return CBB_add_asn1(cbb, &child, CBS_ASN1_OBJECT) &&
  324. CBB_add_bytes(&child, curve->oid, curve->oid_len) &&
  325. CBB_flush(cbb);
  326. }
  327. }
  328. OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
  329. return 0;
  330. }
  331. EC_GROUP *EC_KEY_parse_parameters(CBS *cbs) {
  332. if (!CBS_peek_asn1_tag(cbs, CBS_ASN1_SEQUENCE)) {
  333. return EC_KEY_parse_curve_name(cbs);
  334. }
  335. /* OpenSSL sometimes produces ECPrivateKeys with explicitly-encoded versions
  336. * of named curves.
  337. *
  338. * TODO(davidben): Remove support for this. */
  339. CBS prime, a, b, base_x, base_y, order;
  340. if (!parse_explicit_prime_curve(cbs, &prime, &a, &b, &base_x, &base_y,
  341. &order)) {
  342. return NULL;
  343. }
  344. /* Look for a matching prime curve. */
  345. unsigned i;
  346. for (i = 0; OPENSSL_built_in_curves[i].nid != NID_undef; i++) {
  347. const struct built_in_curve *curve = &OPENSSL_built_in_curves[i];
  348. const unsigned param_len = curve->data->param_len;
  349. /* |curve->data->data| is ordered p, a, b, x, y, order, each component
  350. * zero-padded up to the field length. Although SEC 1 states that the
  351. * Field-Element-to-Octet-String conversion also pads, OpenSSL mis-encodes
  352. * |a| and |b|, so this comparison must allow omitting leading zeros. (This
  353. * is relevant for P-521 whose |b| has a leading 0.) */
  354. if (integers_equal(&prime, curve->data->data, param_len) &&
  355. integers_equal(&a, curve->data->data + param_len, param_len) &&
  356. integers_equal(&b, curve->data->data + param_len * 2, param_len) &&
  357. integers_equal(&base_x, curve->data->data + param_len * 3, param_len) &&
  358. integers_equal(&base_y, curve->data->data + param_len * 4, param_len) &&
  359. integers_equal(&order, curve->data->data + param_len * 5, param_len)) {
  360. return EC_GROUP_new_by_curve_name(curve->nid);
  361. }
  362. }
  363. OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
  364. return NULL;
  365. }
  366. EC_KEY *d2i_ECPrivateKey(EC_KEY **out, const uint8_t **inp, long len) {
  367. /* This function treats its |out| parameter differently from other |d2i|
  368. * functions. If supplied, take the group from |*out|. */
  369. const EC_GROUP *group = NULL;
  370. if (out != NULL && *out != NULL) {
  371. group = EC_KEY_get0_group(*out);
  372. }
  373. if (len < 0) {
  374. OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
  375. return NULL;
  376. }
  377. CBS cbs;
  378. CBS_init(&cbs, *inp, (size_t)len);
  379. EC_KEY *ret = EC_KEY_parse_private_key(&cbs, group);
  380. if (ret == NULL) {
  381. return NULL;
  382. }
  383. if (out != NULL) {
  384. EC_KEY_free(*out);
  385. *out = ret;
  386. }
  387. *inp = CBS_data(&cbs);
  388. return ret;
  389. }
  390. int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp) {
  391. CBB cbb;
  392. if (!CBB_init(&cbb, 0) ||
  393. !EC_KEY_marshal_private_key(&cbb, key, EC_KEY_get_enc_flags(key))) {
  394. CBB_cleanup(&cbb);
  395. return -1;
  396. }
  397. return CBB_finish_i2d(&cbb, outp);
  398. }
  399. EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp, long len) {
  400. if (len < 0) {
  401. return NULL;
  402. }
  403. CBS cbs;
  404. CBS_init(&cbs, *inp, (size_t)len);
  405. EC_GROUP *group = EC_KEY_parse_parameters(&cbs);
  406. if (group == NULL) {
  407. return NULL;
  408. }
  409. EC_KEY *ret = EC_KEY_new();
  410. if (ret == NULL || !EC_KEY_set_group(ret, group)) {
  411. EC_GROUP_free(group);
  412. EC_KEY_free(ret);
  413. return NULL;
  414. }
  415. EC_GROUP_free(group);
  416. if (out_key != NULL) {
  417. EC_KEY_free(*out_key);
  418. *out_key = ret;
  419. }
  420. *inp = CBS_data(&cbs);
  421. return ret;
  422. }
  423. int i2d_ECParameters(const EC_KEY *key, uint8_t **outp) {
  424. if (key == NULL || key->group == NULL) {
  425. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  426. return -1;
  427. }
  428. CBB cbb;
  429. if (!CBB_init(&cbb, 0) ||
  430. !EC_KEY_marshal_curve_name(&cbb, key->group)) {
  431. CBB_cleanup(&cbb);
  432. return -1;
  433. }
  434. return CBB_finish_i2d(&cbb, outp);
  435. }
  436. EC_KEY *o2i_ECPublicKey(EC_KEY **keyp, const uint8_t **inp, long len) {
  437. EC_KEY *ret = NULL;
  438. if (keyp == NULL || *keyp == NULL || (*keyp)->group == NULL) {
  439. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  440. return NULL;
  441. }
  442. ret = *keyp;
  443. if (ret->pub_key == NULL &&
  444. (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
  445. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  446. return NULL;
  447. }
  448. if (!EC_POINT_oct2point(ret->group, ret->pub_key, *inp, len, NULL)) {
  449. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  450. return NULL;
  451. }
  452. /* save the point conversion form */
  453. ret->conv_form = (point_conversion_form_t)(*inp[0] & ~0x01);
  454. *inp += len;
  455. return ret;
  456. }
  457. int i2o_ECPublicKey(const EC_KEY *key, uint8_t **outp) {
  458. size_t buf_len = 0;
  459. int new_buffer = 0;
  460. if (key == NULL) {
  461. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  462. return 0;
  463. }
  464. buf_len = EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, NULL,
  465. 0, NULL);
  466. if (outp == NULL || buf_len == 0) {
  467. /* out == NULL => just return the length of the octet string */
  468. return buf_len;
  469. }
  470. if (*outp == NULL) {
  471. *outp = OPENSSL_malloc(buf_len);
  472. if (*outp == NULL) {
  473. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  474. return 0;
  475. }
  476. new_buffer = 1;
  477. }
  478. if (!EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, *outp,
  479. buf_len, NULL)) {
  480. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  481. if (new_buffer) {
  482. OPENSSL_free(*outp);
  483. *outp = NULL;
  484. }
  485. return 0;
  486. }
  487. if (!new_buffer) {
  488. *outp += buf_len;
  489. }
  490. return buf_len;
  491. }