Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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