ec_asn1.c 17 KB

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