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