1 /* $OpenBSD: ec_asn1.c,v 1.24 2017/05/26 16:32:14 jsing Exp $ */
3 * Written by Nils Larsch for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 2000-2003 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/opensslconf.h>
64 #include <openssl/err.h>
65 #include <openssl/asn1t.h>
66 #include <openssl/objects.h>
69 EC_GROUP_get_basis_type(const EC_GROUP
* group
)
73 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
74 NID_X9_62_characteristic_two_field
)
75 /* everything else is currently not supported */
78 while (group
->poly
[i
] != 0)
82 return NID_X9_62_ppBasis
;
84 return NID_X9_62_tpBasis
;
86 /* everything else is currently not supported */
89 #ifndef OPENSSL_NO_EC2M
91 EC_GROUP_get_trinomial_basis(const EC_GROUP
* group
, unsigned int *k
)
96 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
97 NID_X9_62_characteristic_two_field
98 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0) && (group
->poly
[2] == 0))) {
99 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
108 EC_GROUP_get_pentanomial_basis(const EC_GROUP
* group
, unsigned int *k1
,
109 unsigned int *k2
, unsigned int *k3
)
114 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
115 NID_X9_62_characteristic_two_field
116 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0) && (group
->poly
[2] != 0) && (group
->poly
[3] != 0) && (group
->poly
[4] == 0))) {
117 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
121 *k1
= group
->poly
[3];
123 *k2
= group
->poly
[2];
125 *k3
= group
->poly
[1];
132 /* some structures needed for the asn1 encoding */
133 typedef struct x9_62_pentanomial_st
{
139 typedef struct x9_62_characteristic_two_st
{
144 /* NID_X9_62_onBasis */
146 /* NID_X9_62_tpBasis */
147 ASN1_INTEGER
*tpBasis
;
148 /* NID_X9_62_ppBasis */
149 X9_62_PENTANOMIAL
*ppBasis
;
153 } X9_62_CHARACTERISTIC_TWO
;
155 typedef struct x9_62_fieldid_st
{
156 ASN1_OBJECT
*fieldType
;
159 /* NID_X9_62_prime_field */
161 /* NID_X9_62_characteristic_two_field */
162 X9_62_CHARACTERISTIC_TWO
*char_two
;
168 typedef struct x9_62_curve_st
{
169 ASN1_OCTET_STRING
*a
;
170 ASN1_OCTET_STRING
*b
;
171 ASN1_BIT_STRING
*seed
;
174 typedef struct ec_parameters_st
{
176 X9_62_FIELDID
*fieldID
;
178 ASN1_OCTET_STRING
*base
;
180 ASN1_INTEGER
*cofactor
;
183 struct ecpk_parameters_st
{
186 ASN1_OBJECT
*named_curve
;
187 ECPARAMETERS
*parameters
;
188 ASN1_NULL
*implicitlyCA
;
190 } /* ECPKPARAMETERS */ ;
192 /* SEC1 ECPrivateKey */
193 typedef struct ec_privatekey_st
{
195 ASN1_OCTET_STRING
*privateKey
;
196 ECPKPARAMETERS
*parameters
;
197 ASN1_BIT_STRING
*publicKey
;
200 /* the OpenSSL ASN.1 definitions */
201 static const ASN1_TEMPLATE X9_62_PENTANOMIAL_seq_tt
[] = {
205 .offset
= offsetof(X9_62_PENTANOMIAL
, k1
),
212 .offset
= offsetof(X9_62_PENTANOMIAL
, k2
),
219 .offset
= offsetof(X9_62_PENTANOMIAL
, k3
),
225 const ASN1_ITEM X9_62_PENTANOMIAL_it
= {
226 .itype
= ASN1_ITYPE_SEQUENCE
,
227 .utype
= V_ASN1_SEQUENCE
,
228 .templates
= X9_62_PENTANOMIAL_seq_tt
,
229 .tcount
= sizeof(X9_62_PENTANOMIAL_seq_tt
) / sizeof(ASN1_TEMPLATE
),
231 .size
= sizeof(X9_62_PENTANOMIAL
),
232 .sname
= "X9_62_PENTANOMIAL",
235 X9_62_PENTANOMIAL
*X9_62_PENTANOMIAL_new(void);
236 void X9_62_PENTANOMIAL_free(X9_62_PENTANOMIAL
*a
);
239 X9_62_PENTANOMIAL_new(void)
241 return (X9_62_PENTANOMIAL
*)ASN1_item_new(&X9_62_PENTANOMIAL_it
);
245 X9_62_PENTANOMIAL_free(X9_62_PENTANOMIAL
*a
)
247 ASN1_item_free((ASN1_VALUE
*)a
, &X9_62_PENTANOMIAL_it
);
250 static const ASN1_TEMPLATE char_two_def_tt
= {
253 .offset
= offsetof(X9_62_CHARACTERISTIC_TWO
, p
.other
),
254 .field_name
= "p.other",
255 .item
= &ASN1_ANY_it
,
258 static const ASN1_ADB_TABLE X9_62_CHARACTERISTIC_TWO_adbtbl
[] = {
260 .value
= NID_X9_62_onBasis
,
264 .offset
= offsetof(X9_62_CHARACTERISTIC_TWO
, p
.onBasis
),
265 .field_name
= "p.onBasis",
266 .item
= &ASN1_NULL_it
,
271 .value
= NID_X9_62_tpBasis
,
275 .offset
= offsetof(X9_62_CHARACTERISTIC_TWO
, p
.tpBasis
),
276 .field_name
= "p.tpBasis",
277 .item
= &ASN1_INTEGER_it
,
282 .value
= NID_X9_62_ppBasis
,
286 .offset
= offsetof(X9_62_CHARACTERISTIC_TWO
, p
.ppBasis
),
287 .field_name
= "p.ppBasis",
288 .item
= &X9_62_PENTANOMIAL_it
,
294 static const ASN1_ADB X9_62_CHARACTERISTIC_TWO_adb
= {
296 .offset
= offsetof(X9_62_CHARACTERISTIC_TWO
, type
),
298 .tbl
= X9_62_CHARACTERISTIC_TWO_adbtbl
,
299 .tblcount
= sizeof(X9_62_CHARACTERISTIC_TWO_adbtbl
) / sizeof(ASN1_ADB_TABLE
),
300 .default_tt
= &char_two_def_tt
,
304 static const ASN1_TEMPLATE X9_62_CHARACTERISTIC_TWO_seq_tt
[] = {
308 .offset
= offsetof(X9_62_CHARACTERISTIC_TWO
, m
),
315 .offset
= offsetof(X9_62_CHARACTERISTIC_TWO
, type
),
316 .field_name
= "type",
317 .item
= &ASN1_OBJECT_it
,
320 .flags
= ASN1_TFLG_ADB_OID
,
323 .field_name
= "X9_62_CHARACTERISTIC_TWO",
324 .item
= (const ASN1_ITEM
*)&X9_62_CHARACTERISTIC_TWO_adb
,
328 const ASN1_ITEM X9_62_CHARACTERISTIC_TWO_it
= {
329 .itype
= ASN1_ITYPE_SEQUENCE
,
330 .utype
= V_ASN1_SEQUENCE
,
331 .templates
= X9_62_CHARACTERISTIC_TWO_seq_tt
,
332 .tcount
= sizeof(X9_62_CHARACTERISTIC_TWO_seq_tt
) / sizeof(ASN1_TEMPLATE
),
334 .size
= sizeof(X9_62_CHARACTERISTIC_TWO
),
335 .sname
= "X9_62_CHARACTERISTIC_TWO",
337 X9_62_CHARACTERISTIC_TWO
*X9_62_CHARACTERISTIC_TWO_new(void);
338 void X9_62_CHARACTERISTIC_TWO_free(X9_62_CHARACTERISTIC_TWO
*a
);
340 X9_62_CHARACTERISTIC_TWO
*
341 X9_62_CHARACTERISTIC_TWO_new(void)
343 return (X9_62_CHARACTERISTIC_TWO
*)ASN1_item_new(&X9_62_CHARACTERISTIC_TWO_it
);
347 X9_62_CHARACTERISTIC_TWO_free(X9_62_CHARACTERISTIC_TWO
*a
)
349 ASN1_item_free((ASN1_VALUE
*)a
, &X9_62_CHARACTERISTIC_TWO_it
);
351 static const ASN1_TEMPLATE fieldID_def_tt
= {
354 .offset
= offsetof(X9_62_FIELDID
, p
.other
),
355 .field_name
= "p.other",
356 .item
= &ASN1_ANY_it
,
359 static const ASN1_ADB_TABLE X9_62_FIELDID_adbtbl
[] = {
361 .value
= NID_X9_62_prime_field
,
365 .offset
= offsetof(X9_62_FIELDID
, p
.prime
),
366 .field_name
= "p.prime",
367 .item
= &ASN1_INTEGER_it
,
372 .value
= NID_X9_62_characteristic_two_field
,
376 .offset
= offsetof(X9_62_FIELDID
, p
.char_two
),
377 .field_name
= "p.char_two",
378 .item
= &X9_62_CHARACTERISTIC_TWO_it
,
384 static const ASN1_ADB X9_62_FIELDID_adb
= {
386 .offset
= offsetof(X9_62_FIELDID
, fieldType
),
388 .tbl
= X9_62_FIELDID_adbtbl
,
389 .tblcount
= sizeof(X9_62_FIELDID_adbtbl
) / sizeof(ASN1_ADB_TABLE
),
390 .default_tt
= &fieldID_def_tt
,
394 static const ASN1_TEMPLATE X9_62_FIELDID_seq_tt
[] = {
398 .offset
= offsetof(X9_62_FIELDID
, fieldType
),
399 .field_name
= "fieldType",
400 .item
= &ASN1_OBJECT_it
,
403 .flags
= ASN1_TFLG_ADB_OID
,
406 .field_name
= "X9_62_FIELDID",
407 .item
= (const ASN1_ITEM
*)&X9_62_FIELDID_adb
,
411 const ASN1_ITEM X9_62_FIELDID_it
= {
412 .itype
= ASN1_ITYPE_SEQUENCE
,
413 .utype
= V_ASN1_SEQUENCE
,
414 .templates
= X9_62_FIELDID_seq_tt
,
415 .tcount
= sizeof(X9_62_FIELDID_seq_tt
) / sizeof(ASN1_TEMPLATE
),
417 .size
= sizeof(X9_62_FIELDID
),
418 .sname
= "X9_62_FIELDID",
421 static const ASN1_TEMPLATE X9_62_CURVE_seq_tt
[] = {
425 .offset
= offsetof(X9_62_CURVE
, a
),
427 .item
= &ASN1_OCTET_STRING_it
,
432 .offset
= offsetof(X9_62_CURVE
, b
),
434 .item
= &ASN1_OCTET_STRING_it
,
437 .flags
= ASN1_TFLG_OPTIONAL
,
439 .offset
= offsetof(X9_62_CURVE
, seed
),
440 .field_name
= "seed",
441 .item
= &ASN1_BIT_STRING_it
,
445 const ASN1_ITEM X9_62_CURVE_it
= {
446 .itype
= ASN1_ITYPE_SEQUENCE
,
447 .utype
= V_ASN1_SEQUENCE
,
448 .templates
= X9_62_CURVE_seq_tt
,
449 .tcount
= sizeof(X9_62_CURVE_seq_tt
) / sizeof(ASN1_TEMPLATE
),
451 .size
= sizeof(X9_62_CURVE
),
452 .sname
= "X9_62_CURVE",
455 static const ASN1_TEMPLATE ECPARAMETERS_seq_tt
[] = {
459 .offset
= offsetof(ECPARAMETERS
, version
),
460 .field_name
= "version",
466 .offset
= offsetof(ECPARAMETERS
, fieldID
),
467 .field_name
= "fieldID",
468 .item
= &X9_62_FIELDID_it
,
473 .offset
= offsetof(ECPARAMETERS
, curve
),
474 .field_name
= "curve",
475 .item
= &X9_62_CURVE_it
,
480 .offset
= offsetof(ECPARAMETERS
, base
),
481 .field_name
= "base",
482 .item
= &ASN1_OCTET_STRING_it
,
487 .offset
= offsetof(ECPARAMETERS
, order
),
488 .field_name
= "order",
489 .item
= &ASN1_INTEGER_it
,
492 .flags
= ASN1_TFLG_OPTIONAL
,
494 .offset
= offsetof(ECPARAMETERS
, cofactor
),
495 .field_name
= "cofactor",
496 .item
= &ASN1_INTEGER_it
,
500 const ASN1_ITEM ECPARAMETERS_it
= {
501 .itype
= ASN1_ITYPE_SEQUENCE
,
502 .utype
= V_ASN1_SEQUENCE
,
503 .templates
= ECPARAMETERS_seq_tt
,
504 .tcount
= sizeof(ECPARAMETERS_seq_tt
) / sizeof(ASN1_TEMPLATE
),
506 .size
= sizeof(ECPARAMETERS
),
507 .sname
= "ECPARAMETERS",
509 ECPARAMETERS
*ECPARAMETERS_new(void);
510 void ECPARAMETERS_free(ECPARAMETERS
*a
);
513 ECPARAMETERS_new(void)
515 return (ECPARAMETERS
*)ASN1_item_new(&ECPARAMETERS_it
);
519 ECPARAMETERS_free(ECPARAMETERS
*a
)
521 ASN1_item_free((ASN1_VALUE
*)a
, &ECPARAMETERS_it
);
524 static const ASN1_TEMPLATE ECPKPARAMETERS_ch_tt
[] = {
528 .offset
= offsetof(ECPKPARAMETERS
, value
.named_curve
),
529 .field_name
= "value.named_curve",
530 .item
= &ASN1_OBJECT_it
,
535 .offset
= offsetof(ECPKPARAMETERS
, value
.parameters
),
536 .field_name
= "value.parameters",
537 .item
= &ECPARAMETERS_it
,
542 .offset
= offsetof(ECPKPARAMETERS
, value
.implicitlyCA
),
543 .field_name
= "value.implicitlyCA",
544 .item
= &ASN1_NULL_it
,
548 const ASN1_ITEM ECPKPARAMETERS_it
= {
549 .itype
= ASN1_ITYPE_CHOICE
,
550 .utype
= offsetof(ECPKPARAMETERS
, type
),
551 .templates
= ECPKPARAMETERS_ch_tt
,
552 .tcount
= sizeof(ECPKPARAMETERS_ch_tt
) / sizeof(ASN1_TEMPLATE
),
554 .size
= sizeof(ECPKPARAMETERS
),
555 .sname
= "ECPKPARAMETERS",
558 ECPKPARAMETERS
*ECPKPARAMETERS_new(void);
559 void ECPKPARAMETERS_free(ECPKPARAMETERS
*a
);
560 ECPKPARAMETERS
*d2i_ECPKPARAMETERS(ECPKPARAMETERS
**a
, const unsigned char **in
, long len
);
561 int i2d_ECPKPARAMETERS(const ECPKPARAMETERS
*a
, unsigned char **out
);
564 d2i_ECPKPARAMETERS(ECPKPARAMETERS
**a
, const unsigned char **in
, long len
)
566 return (ECPKPARAMETERS
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
571 i2d_ECPKPARAMETERS(const ECPKPARAMETERS
*a
, unsigned char **out
)
573 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &ECPKPARAMETERS_it
);
577 ECPKPARAMETERS_new(void)
579 return (ECPKPARAMETERS
*)ASN1_item_new(&ECPKPARAMETERS_it
);
583 ECPKPARAMETERS_free(ECPKPARAMETERS
*a
)
585 ASN1_item_free((ASN1_VALUE
*)a
, &ECPKPARAMETERS_it
);
588 static const ASN1_TEMPLATE EC_PRIVATEKEY_seq_tt
[] = {
592 .offset
= offsetof(EC_PRIVATEKEY
, version
),
593 .field_name
= "version",
599 .offset
= offsetof(EC_PRIVATEKEY
, privateKey
),
600 .field_name
= "privateKey",
601 .item
= &ASN1_OCTET_STRING_it
,
604 .flags
= ASN1_TFLG_EXPLICIT
| ASN1_TFLG_OPTIONAL
,
606 .offset
= offsetof(EC_PRIVATEKEY
, parameters
),
607 .field_name
= "parameters",
608 .item
= &ECPKPARAMETERS_it
,
611 .flags
= ASN1_TFLG_EXPLICIT
| ASN1_TFLG_OPTIONAL
,
613 .offset
= offsetof(EC_PRIVATEKEY
, publicKey
),
614 .field_name
= "publicKey",
615 .item
= &ASN1_BIT_STRING_it
,
619 const ASN1_ITEM EC_PRIVATEKEY_it
= {
620 .itype
= ASN1_ITYPE_SEQUENCE
,
621 .utype
= V_ASN1_SEQUENCE
,
622 .templates
= EC_PRIVATEKEY_seq_tt
,
623 .tcount
= sizeof(EC_PRIVATEKEY_seq_tt
) / sizeof(ASN1_TEMPLATE
),
625 .size
= sizeof(EC_PRIVATEKEY
),
626 .sname
= "EC_PRIVATEKEY",
629 EC_PRIVATEKEY
*EC_PRIVATEKEY_new(void);
630 void EC_PRIVATEKEY_free(EC_PRIVATEKEY
*a
);
631 EC_PRIVATEKEY
*d2i_EC_PRIVATEKEY(EC_PRIVATEKEY
**a
, const unsigned char **in
, long len
);
632 int i2d_EC_PRIVATEKEY(const EC_PRIVATEKEY
*a
, unsigned char **out
);
635 d2i_EC_PRIVATEKEY(EC_PRIVATEKEY
**a
, const unsigned char **in
, long len
)
637 return (EC_PRIVATEKEY
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
642 i2d_EC_PRIVATEKEY(const EC_PRIVATEKEY
*a
, unsigned char **out
)
644 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &EC_PRIVATEKEY_it
);
648 EC_PRIVATEKEY_new(void)
650 return (EC_PRIVATEKEY
*)ASN1_item_new(&EC_PRIVATEKEY_it
);
654 EC_PRIVATEKEY_free(EC_PRIVATEKEY
*a
)
656 ASN1_item_free((ASN1_VALUE
*)a
, &EC_PRIVATEKEY_it
);
658 /* some declarations of internal function */
660 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
661 static int ec_asn1_group2fieldid(const EC_GROUP
*, X9_62_FIELDID
*);
662 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
663 static int ec_asn1_group2curve(const EC_GROUP
*, X9_62_CURVE
*);
664 /* ec_asn1_parameters2group() creates a EC_GROUP object from a
665 * ECPARAMETERS object */
666 static EC_GROUP
*ec_asn1_parameters2group(const ECPARAMETERS
*);
667 /* ec_asn1_group2parameters() creates a ECPARAMETERS object from a
669 static ECPARAMETERS
*ec_asn1_group2parameters(const EC_GROUP
*, ECPARAMETERS
*);
670 /* ec_asn1_pkparameters2group() creates a EC_GROUP object from a
671 * ECPKPARAMETERS object */
672 static EC_GROUP
*ec_asn1_pkparameters2group(const ECPKPARAMETERS
*);
673 /* ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a
675 static ECPKPARAMETERS
*ec_asn1_group2pkparameters(const EC_GROUP
*,
679 /* the function definitions */
682 ec_asn1_group2fieldid(const EC_GROUP
* group
, X9_62_FIELDID
* field
)
687 if (group
== NULL
|| field
== NULL
)
690 /* clear the old values (if necessary) */
691 if (field
->fieldType
!= NULL
)
692 ASN1_OBJECT_free(field
->fieldType
);
693 if (field
->p
.other
!= NULL
)
694 ASN1_TYPE_free(field
->p
.other
);
696 nid
= EC_METHOD_get_field_type(EC_GROUP_method_of(group
));
697 /* set OID for the field */
698 if ((field
->fieldType
= OBJ_nid2obj(nid
)) == NULL
) {
699 ECerror(ERR_R_OBJ_LIB
);
702 if (nid
== NID_X9_62_prime_field
) {
703 if ((tmp
= BN_new()) == NULL
) {
704 ECerror(ERR_R_MALLOC_FAILURE
);
707 /* the parameters are specified by the prime number p */
708 if (!EC_GROUP_get_curve_GFp(group
, tmp
, NULL
, NULL
, NULL
)) {
709 ECerror(ERR_R_EC_LIB
);
712 /* set the prime number */
713 field
->p
.prime
= BN_to_ASN1_INTEGER(tmp
, NULL
);
714 if (field
->p
.prime
== NULL
) {
715 ECerror(ERR_R_ASN1_LIB
);
718 } else /* nid == NID_X9_62_characteristic_two_field */
719 #ifdef OPENSSL_NO_EC2M
721 ECerror(EC_R_GF2M_NOT_SUPPORTED
);
727 X9_62_CHARACTERISTIC_TWO
*char_two
;
729 field
->p
.char_two
= X9_62_CHARACTERISTIC_TWO_new();
730 char_two
= field
->p
.char_two
;
732 if (char_two
== NULL
) {
733 ECerror(ERR_R_MALLOC_FAILURE
);
736 char_two
->m
= (long) EC_GROUP_get_degree(group
);
738 field_type
= EC_GROUP_get_basis_type(group
);
740 if (field_type
== 0) {
741 ECerror(ERR_R_EC_LIB
);
744 /* set base type OID */
745 if ((char_two
->type
= OBJ_nid2obj(field_type
)) == NULL
) {
746 ECerror(ERR_R_OBJ_LIB
);
749 if (field_type
== NID_X9_62_tpBasis
) {
752 if (!EC_GROUP_get_trinomial_basis(group
, &k
))
755 char_two
->p
.tpBasis
= ASN1_INTEGER_new();
756 if (!char_two
->p
.tpBasis
) {
757 ECerror(ERR_R_MALLOC_FAILURE
);
760 if (!ASN1_INTEGER_set(char_two
->p
.tpBasis
, (long) k
)) {
761 ECerror(ERR_R_ASN1_LIB
);
764 } else if (field_type
== NID_X9_62_ppBasis
) {
765 unsigned int k1
, k2
, k3
;
767 if (!EC_GROUP_get_pentanomial_basis(group
, &k1
, &k2
, &k3
))
770 char_two
->p
.ppBasis
= X9_62_PENTANOMIAL_new();
771 if (!char_two
->p
.ppBasis
) {
772 ECerror(ERR_R_MALLOC_FAILURE
);
776 char_two
->p
.ppBasis
->k1
= (long) k1
;
777 char_two
->p
.ppBasis
->k2
= (long) k2
;
778 char_two
->p
.ppBasis
->k3
= (long) k3
;
779 } else { /* field_type == NID_X9_62_onBasis */
780 /* for ONB the parameters are (asn1) NULL */
781 char_two
->p
.onBasis
= ASN1_NULL_new();
782 if (!char_two
->p
.onBasis
) {
783 ECerror(ERR_R_MALLOC_FAILURE
);
798 ec_asn1_group2curve(const EC_GROUP
* group
, X9_62_CURVE
* curve
)
801 BIGNUM
*tmp_1
= NULL
, *tmp_2
= NULL
;
802 unsigned char *buffer_1
= NULL
, *buffer_2
= NULL
, *a_buf
= NULL
,
805 unsigned char char_zero
= 0;
807 if (!group
|| !curve
|| !curve
->a
|| !curve
->b
)
810 if ((tmp_1
= BN_new()) == NULL
|| (tmp_2
= BN_new()) == NULL
) {
811 ECerror(ERR_R_MALLOC_FAILURE
);
814 nid
= EC_METHOD_get_field_type(EC_GROUP_method_of(group
));
817 if (nid
== NID_X9_62_prime_field
) {
818 if (!EC_GROUP_get_curve_GFp(group
, NULL
, tmp_1
, tmp_2
, NULL
)) {
819 ECerror(ERR_R_EC_LIB
);
823 #ifndef OPENSSL_NO_EC2M
824 else { /* nid == NID_X9_62_characteristic_two_field */
825 if (!EC_GROUP_get_curve_GF2m(group
, NULL
, tmp_1
, tmp_2
, NULL
)) {
826 ECerror(ERR_R_EC_LIB
);
831 len_1
= (size_t) BN_num_bytes(tmp_1
);
832 len_2
= (size_t) BN_num_bytes(tmp_2
);
835 /* len_1 == 0 => a == 0 */
839 if ((buffer_1
= malloc(len_1
)) == NULL
) {
840 ECerror(ERR_R_MALLOC_FAILURE
);
843 if ((len_1
= BN_bn2bin(tmp_1
, buffer_1
)) == 0) {
844 ECerror(ERR_R_BN_LIB
);
851 /* len_2 == 0 => b == 0 */
855 if ((buffer_2
= malloc(len_2
)) == NULL
) {
856 ECerror(ERR_R_MALLOC_FAILURE
);
859 if ((len_2
= BN_bn2bin(tmp_2
, buffer_2
)) == 0) {
860 ECerror(ERR_R_BN_LIB
);
867 if (!ASN1_STRING_set(curve
->a
, a_buf
, len_1
) ||
868 !ASN1_STRING_set(curve
->b
, b_buf
, len_2
)) {
869 ECerror(ERR_R_ASN1_LIB
);
872 /* set the seed (optional) */
875 if ((curve
->seed
= ASN1_BIT_STRING_new()) == NULL
) {
876 ECerror(ERR_R_MALLOC_FAILURE
);
879 curve
->seed
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
880 curve
->seed
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
881 if (!ASN1_BIT_STRING_set(curve
->seed
, group
->seed
,
882 (int) group
->seed_len
)) {
883 ECerror(ERR_R_ASN1_LIB
);
888 ASN1_BIT_STRING_free(curve
->seed
);
903 static ECPARAMETERS
*
904 ec_asn1_group2parameters(const EC_GROUP
* group
, ECPARAMETERS
* param
)
908 ECPARAMETERS
*ret
= NULL
;
910 unsigned char *buffer
= NULL
;
911 const EC_POINT
*point
= NULL
;
912 point_conversion_form_t form
;
914 if ((tmp
= BN_new()) == NULL
) {
915 ECerror(ERR_R_MALLOC_FAILURE
);
919 if ((ret
= ECPARAMETERS_new()) == NULL
) {
920 ECerror(ERR_R_MALLOC_FAILURE
);
926 /* set the version (always one) */
927 ret
->version
= (long) 0x1;
929 /* set the fieldID */
930 if (!ec_asn1_group2fieldid(group
, ret
->fieldID
)) {
931 ECerror(ERR_R_EC_LIB
);
935 if (!ec_asn1_group2curve(group
, ret
->curve
)) {
936 ECerror(ERR_R_EC_LIB
);
939 /* set the base point */
940 if ((point
= EC_GROUP_get0_generator(group
)) == NULL
) {
941 ECerror(EC_R_UNDEFINED_GENERATOR
);
944 form
= EC_GROUP_get_point_conversion_form(group
);
946 len
= EC_POINT_point2oct(group
, point
, form
, NULL
, len
, NULL
);
948 ECerror(ERR_R_EC_LIB
);
951 if ((buffer
= malloc(len
)) == NULL
) {
952 ECerror(ERR_R_MALLOC_FAILURE
);
955 if (!EC_POINT_point2oct(group
, point
, form
, buffer
, len
, NULL
)) {
956 ECerror(ERR_R_EC_LIB
);
959 if (ret
->base
== NULL
&& (ret
->base
= ASN1_OCTET_STRING_new()) == NULL
) {
960 ECerror(ERR_R_MALLOC_FAILURE
);
963 if (!ASN1_OCTET_STRING_set(ret
->base
, buffer
, len
)) {
964 ECerror(ERR_R_ASN1_LIB
);
968 if (!EC_GROUP_get_order(group
, tmp
, NULL
)) {
969 ECerror(ERR_R_EC_LIB
);
972 ret
->order
= BN_to_ASN1_INTEGER(tmp
, ret
->order
);
973 if (ret
->order
== NULL
) {
974 ECerror(ERR_R_ASN1_LIB
);
977 /* set the cofactor (optional) */
978 if (EC_GROUP_get_cofactor(group
, tmp
, NULL
)) {
979 ret
->cofactor
= BN_to_ASN1_INTEGER(tmp
, ret
->cofactor
);
980 if (ret
->cofactor
== NULL
) {
981 ECerror(ERR_R_ASN1_LIB
);
989 ECPARAMETERS_free(ret
);
998 ec_asn1_group2pkparameters(const EC_GROUP
* group
, ECPKPARAMETERS
* params
)
1001 ECPKPARAMETERS
*ret
= params
;
1004 if ((ret
= ECPKPARAMETERS_new()) == NULL
) {
1005 ECerror(ERR_R_MALLOC_FAILURE
);
1009 if (ret
->type
== 0 && ret
->value
.named_curve
)
1010 ASN1_OBJECT_free(ret
->value
.named_curve
);
1011 else if (ret
->type
== 1 && ret
->value
.parameters
)
1012 ECPARAMETERS_free(ret
->value
.parameters
);
1015 if (EC_GROUP_get_asn1_flag(group
)) {
1017 * use the asn1 OID to describe the elliptic curve
1020 tmp
= EC_GROUP_get_curve_name(group
);
1023 if ((ret
->value
.named_curve
= OBJ_nid2obj(tmp
)) == NULL
)
1026 /* we don't kmow the nid => ERROR */
1029 /* use the ECPARAMETERS structure */
1031 if ((ret
->value
.parameters
= ec_asn1_group2parameters(
1032 group
, NULL
)) == NULL
)
1037 ECPKPARAMETERS_free(ret
);
1044 ec_asn1_parameters2group(const ECPARAMETERS
* params
)
1047 EC_GROUP
*ret
= NULL
;
1048 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
;
1049 EC_POINT
*point
= NULL
;
1052 if (!params
->fieldID
|| !params
->fieldID
->fieldType
||
1053 !params
->fieldID
->p
.ptr
) {
1054 ECerror(EC_R_ASN1_ERROR
);
1057 /* now extract the curve parameters a and b */
1058 if (!params
->curve
|| !params
->curve
->a
||
1059 !params
->curve
->a
->data
|| !params
->curve
->b
||
1060 !params
->curve
->b
->data
) {
1061 ECerror(EC_R_ASN1_ERROR
);
1064 a
= BN_bin2bn(params
->curve
->a
->data
, params
->curve
->a
->length
, NULL
);
1066 ECerror(ERR_R_BN_LIB
);
1069 b
= BN_bin2bn(params
->curve
->b
->data
, params
->curve
->b
->length
, NULL
);
1071 ECerror(ERR_R_BN_LIB
);
1074 /* get the field parameters */
1075 tmp
= OBJ_obj2nid(params
->fieldID
->fieldType
);
1076 if (tmp
== NID_X9_62_characteristic_two_field
)
1077 #ifdef OPENSSL_NO_EC2M
1079 ECerror(EC_R_GF2M_NOT_SUPPORTED
);
1084 X9_62_CHARACTERISTIC_TWO
*char_two
;
1086 char_two
= params
->fieldID
->p
.char_two
;
1088 field_bits
= char_two
->m
;
1089 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
1090 ECerror(EC_R_FIELD_TOO_LARGE
);
1093 if ((p
= BN_new()) == NULL
) {
1094 ECerror(ERR_R_MALLOC_FAILURE
);
1097 /* get the base type */
1098 tmp
= OBJ_obj2nid(char_two
->type
);
1100 if (tmp
== NID_X9_62_tpBasis
) {
1103 if (!char_two
->p
.tpBasis
) {
1104 ECerror(EC_R_ASN1_ERROR
);
1107 tmp_long
= ASN1_INTEGER_get(char_two
->p
.tpBasis
);
1109 if (!(char_two
->m
> tmp_long
&& tmp_long
> 0)) {
1110 ECerror(EC_R_INVALID_TRINOMIAL_BASIS
);
1113 /* create the polynomial */
1114 if (!BN_set_bit(p
, (int) char_two
->m
))
1116 if (!BN_set_bit(p
, (int) tmp_long
))
1118 if (!BN_set_bit(p
, 0))
1120 } else if (tmp
== NID_X9_62_ppBasis
) {
1121 X9_62_PENTANOMIAL
*penta
;
1123 penta
= char_two
->p
.ppBasis
;
1125 ECerror(EC_R_ASN1_ERROR
);
1128 if (!(char_two
->m
> penta
->k3
&& penta
->k3
> penta
->k2
&& penta
->k2
> penta
->k1
&& penta
->k1
> 0)) {
1129 ECerror(EC_R_INVALID_PENTANOMIAL_BASIS
);
1132 /* create the polynomial */
1133 if (!BN_set_bit(p
, (int) char_two
->m
))
1135 if (!BN_set_bit(p
, (int) penta
->k1
))
1137 if (!BN_set_bit(p
, (int) penta
->k2
))
1139 if (!BN_set_bit(p
, (int) penta
->k3
))
1141 if (!BN_set_bit(p
, 0))
1143 } else if (tmp
== NID_X9_62_onBasis
) {
1144 ECerror(EC_R_NOT_IMPLEMENTED
);
1146 } else { /* error */
1147 ECerror(EC_R_ASN1_ERROR
);
1151 /* create the EC_GROUP structure */
1152 ret
= EC_GROUP_new_curve_GF2m(p
, a
, b
, NULL
);
1155 else if (tmp
== NID_X9_62_prime_field
) {
1156 /* we have a curve over a prime field */
1157 /* extract the prime number */
1158 if (!params
->fieldID
->p
.prime
) {
1159 ECerror(EC_R_ASN1_ERROR
);
1162 p
= ASN1_INTEGER_to_BN(params
->fieldID
->p
.prime
, NULL
);
1164 ECerror(ERR_R_ASN1_LIB
);
1167 if (BN_is_negative(p
) || BN_is_zero(p
)) {
1168 ECerror(EC_R_INVALID_FIELD
);
1171 field_bits
= BN_num_bits(p
);
1172 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
1173 ECerror(EC_R_FIELD_TOO_LARGE
);
1176 /* create the EC_GROUP structure */
1177 ret
= EC_GROUP_new_curve_GFp(p
, a
, b
, NULL
);
1179 ECerror(EC_R_INVALID_FIELD
);
1184 ECerror(ERR_R_EC_LIB
);
1187 /* extract seed (optional) */
1188 if (params
->curve
->seed
!= NULL
) {
1190 if (!(ret
->seed
= malloc(params
->curve
->seed
->length
))) {
1191 ECerror(ERR_R_MALLOC_FAILURE
);
1194 memcpy(ret
->seed
, params
->curve
->seed
->data
,
1195 params
->curve
->seed
->length
);
1196 ret
->seed_len
= params
->curve
->seed
->length
;
1198 if (!params
->order
|| !params
->base
|| !params
->base
->data
) {
1199 ECerror(EC_R_ASN1_ERROR
);
1202 if ((point
= EC_POINT_new(ret
)) == NULL
)
1205 /* set the point conversion form */
1206 EC_GROUP_set_point_conversion_form(ret
, (point_conversion_form_t
)
1207 (params
->base
->data
[0] & ~0x01));
1209 /* extract the ec point */
1210 if (!EC_POINT_oct2point(ret
, point
, params
->base
->data
,
1211 params
->base
->length
, NULL
)) {
1212 ECerror(ERR_R_EC_LIB
);
1215 /* extract the order */
1216 if ((a
= ASN1_INTEGER_to_BN(params
->order
, a
)) == NULL
) {
1217 ECerror(ERR_R_ASN1_LIB
);
1220 if (BN_is_negative(a
) || BN_is_zero(a
)) {
1221 ECerror(EC_R_INVALID_GROUP_ORDER
);
1224 if (BN_num_bits(a
) > (int) field_bits
+ 1) { /* Hasse bound */
1225 ECerror(EC_R_INVALID_GROUP_ORDER
);
1228 /* extract the cofactor (optional) */
1229 if (params
->cofactor
== NULL
) {
1232 } else if ((b
= ASN1_INTEGER_to_BN(params
->cofactor
, b
)) == NULL
) {
1233 ECerror(ERR_R_ASN1_LIB
);
1236 /* set the generator, order and cofactor (if present) */
1237 if (!EC_GROUP_set_generator(ret
, point
, a
, b
)) {
1238 ECerror(ERR_R_EC_LIB
);
1244 EC_GROUP_clear_free(ret
);
1250 EC_POINT_free(point
);
1255 ec_asn1_pkparameters2group(const ECPKPARAMETERS
* params
)
1257 EC_GROUP
*ret
= NULL
;
1260 if (params
== NULL
) {
1261 ECerror(EC_R_MISSING_PARAMETERS
);
1264 if (params
->type
== 0) {/* the curve is given by an OID */
1265 tmp
= OBJ_obj2nid(params
->value
.named_curve
);
1266 if ((ret
= EC_GROUP_new_by_curve_name(tmp
)) == NULL
) {
1267 ECerror(EC_R_EC_GROUP_NEW_BY_NAME_FAILURE
);
1270 EC_GROUP_set_asn1_flag(ret
, OPENSSL_EC_NAMED_CURVE
);
1271 } else if (params
->type
== 1) { /* the parameters are given by a
1272 * ECPARAMETERS structure */
1273 ret
= ec_asn1_parameters2group(params
->value
.parameters
);
1275 ECerror(ERR_R_EC_LIB
);
1278 EC_GROUP_set_asn1_flag(ret
, 0x0);
1279 } else if (params
->type
== 2) { /* implicitlyCA */
1282 ECerror(EC_R_ASN1_ERROR
);
1289 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
1292 d2i_ECPKParameters(EC_GROUP
** a
, const unsigned char **in
, long len
)
1294 EC_GROUP
*group
= NULL
;
1295 ECPKPARAMETERS
*params
= NULL
;
1297 if ((params
= d2i_ECPKPARAMETERS(NULL
, in
, len
)) == NULL
) {
1298 ECerror(EC_R_D2I_ECPKPARAMETERS_FAILURE
);
1301 if ((group
= ec_asn1_pkparameters2group(params
)) == NULL
) {
1302 ECerror(EC_R_PKPARAMETERS2GROUP_FAILURE
);
1307 EC_GROUP_clear_free(*a
);
1312 ECPKPARAMETERS_free(params
);
1317 i2d_ECPKParameters(const EC_GROUP
* a
, unsigned char **out
)
1320 ECPKPARAMETERS
*tmp
= ec_asn1_group2pkparameters(a
, NULL
);
1322 ECerror(EC_R_GROUP2PKPARAMETERS_FAILURE
);
1325 if ((ret
= i2d_ECPKPARAMETERS(tmp
, out
)) == 0) {
1326 ECerror(EC_R_I2D_ECPKPARAMETERS_FAILURE
);
1327 ECPKPARAMETERS_free(tmp
);
1330 ECPKPARAMETERS_free(tmp
);
1334 /* some EC_KEY functions */
1337 d2i_ECPrivateKey(EC_KEY
** a
, const unsigned char **in
, long len
)
1340 EC_PRIVATEKEY
*priv_key
= NULL
;
1342 if ((priv_key
= EC_PRIVATEKEY_new()) == NULL
) {
1343 ECerror(ERR_R_MALLOC_FAILURE
);
1346 if ((priv_key
= d2i_EC_PRIVATEKEY(&priv_key
, in
, len
)) == NULL
) {
1347 ECerror(ERR_R_EC_LIB
);
1348 EC_PRIVATEKEY_free(priv_key
);
1351 if (a
== NULL
|| *a
== NULL
) {
1352 if ((ret
= EC_KEY_new()) == NULL
) {
1353 ECerror(ERR_R_MALLOC_FAILURE
);
1359 if (priv_key
->parameters
) {
1360 EC_GROUP_clear_free(ret
->group
);
1361 ret
->group
= ec_asn1_pkparameters2group(priv_key
->parameters
);
1363 if (ret
->group
== NULL
) {
1364 ECerror(ERR_R_EC_LIB
);
1367 ret
->version
= priv_key
->version
;
1369 if (priv_key
->privateKey
) {
1370 ret
->priv_key
= BN_bin2bn(
1371 ASN1_STRING_data(priv_key
->privateKey
),
1372 ASN1_STRING_length(priv_key
->privateKey
),
1374 if (ret
->priv_key
== NULL
) {
1375 ECerror(ERR_R_BN_LIB
);
1379 ECerror(EC_R_MISSING_PRIVATE_KEY
);
1383 if (priv_key
->publicKey
) {
1384 const unsigned char *pub_oct
;
1387 EC_POINT_clear_free(ret
->pub_key
);
1388 ret
->pub_key
= EC_POINT_new(ret
->group
);
1389 if (ret
->pub_key
== NULL
) {
1390 ECerror(ERR_R_EC_LIB
);
1394 pub_oct
= ASN1_STRING_data(priv_key
->publicKey
);
1395 pub_oct_len
= ASN1_STRING_length(priv_key
->publicKey
);
1396 if (pub_oct
== NULL
|| pub_oct_len
<= 0) {
1397 ECerror(EC_R_BUFFER_TOO_SMALL
);
1401 /* save the point conversion form */
1402 ret
->conv_form
= (point_conversion_form_t
) (pub_oct
[0] & ~0x01);
1403 if (!EC_POINT_oct2point(ret
->group
, ret
->pub_key
,
1404 pub_oct
, pub_oct_len
, NULL
)) {
1405 ECerror(ERR_R_EC_LIB
);
1410 EC_PRIVATEKEY_free(priv_key
);
1416 if (a
== NULL
|| *a
!= ret
)
1419 EC_PRIVATEKEY_free(priv_key
);
1425 i2d_ECPrivateKey(EC_KEY
* a
, unsigned char **out
)
1427 int ret
= 0, ok
= 0;
1428 unsigned char *buffer
= NULL
;
1429 size_t buf_len
= 0, tmp_len
;
1430 EC_PRIVATEKEY
*priv_key
= NULL
;
1432 if (a
== NULL
|| a
->group
== NULL
|| a
->priv_key
== NULL
) {
1433 ECerror(ERR_R_PASSED_NULL_PARAMETER
);
1436 if ((priv_key
= EC_PRIVATEKEY_new()) == NULL
) {
1437 ECerror(ERR_R_MALLOC_FAILURE
);
1440 priv_key
->version
= a
->version
;
1442 buf_len
= (size_t) BN_num_bytes(a
->priv_key
);
1443 buffer
= malloc(buf_len
);
1444 if (buffer
== NULL
) {
1445 ECerror(ERR_R_MALLOC_FAILURE
);
1448 if (!BN_bn2bin(a
->priv_key
, buffer
)) {
1449 ECerror(ERR_R_BN_LIB
);
1452 if (!ASN1_STRING_set(priv_key
->privateKey
, buffer
, buf_len
)) {
1453 ECerror(ERR_R_ASN1_LIB
);
1456 if (!(a
->enc_flag
& EC_PKEY_NO_PARAMETERS
)) {
1457 if ((priv_key
->parameters
= ec_asn1_group2pkparameters(
1458 a
->group
, priv_key
->parameters
)) == NULL
) {
1459 ECerror(ERR_R_EC_LIB
);
1463 if (!(a
->enc_flag
& EC_PKEY_NO_PUBKEY
) && a
->pub_key
!= NULL
) {
1464 priv_key
->publicKey
= ASN1_BIT_STRING_new();
1465 if (priv_key
->publicKey
== NULL
) {
1466 ECerror(ERR_R_MALLOC_FAILURE
);
1469 tmp_len
= EC_POINT_point2oct(a
->group
, a
->pub_key
,
1470 a
->conv_form
, NULL
, 0, NULL
);
1472 if (tmp_len
> buf_len
) {
1473 unsigned char *tmp_buffer
= realloc(buffer
, tmp_len
);
1475 ECerror(ERR_R_MALLOC_FAILURE
);
1478 buffer
= tmp_buffer
;
1481 if (!EC_POINT_point2oct(a
->group
, a
->pub_key
,
1482 a
->conv_form
, buffer
, buf_len
, NULL
)) {
1483 ECerror(ERR_R_EC_LIB
);
1486 priv_key
->publicKey
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
1487 priv_key
->publicKey
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
1488 if (!ASN1_STRING_set(priv_key
->publicKey
, buffer
,
1490 ECerror(ERR_R_ASN1_LIB
);
1494 if ((ret
= i2d_EC_PRIVATEKEY(priv_key
, out
)) == 0) {
1495 ECerror(ERR_R_EC_LIB
);
1502 EC_PRIVATEKEY_free(priv_key
);
1503 return (ok
? ret
: 0);
1507 i2d_ECParameters(EC_KEY
* a
, unsigned char **out
)
1510 ECerror(ERR_R_PASSED_NULL_PARAMETER
);
1513 return i2d_ECPKParameters(a
->group
, out
);
1517 d2i_ECParameters(EC_KEY
** a
, const unsigned char **in
, long len
)
1521 if (in
== NULL
|| *in
== NULL
) {
1522 ECerror(ERR_R_PASSED_NULL_PARAMETER
);
1525 if (a
== NULL
|| *a
== NULL
) {
1526 if ((ret
= EC_KEY_new()) == NULL
) {
1527 ECerror(ERR_R_MALLOC_FAILURE
);
1533 if (!d2i_ECPKParameters(&ret
->group
, in
, len
)) {
1534 ECerror(ERR_R_EC_LIB
);
1535 if (a
== NULL
|| *a
!= ret
)
1546 o2i_ECPublicKey(EC_KEY
** a
, const unsigned char **in
, long len
)
1550 if (a
== NULL
|| (*a
) == NULL
|| (*a
)->group
== NULL
) {
1552 * sorry, but a EC_GROUP-structur is necessary to set the
1555 ECerror(ERR_R_PASSED_NULL_PARAMETER
);
1559 if (ret
->pub_key
== NULL
&&
1560 (ret
->pub_key
= EC_POINT_new(ret
->group
)) == NULL
) {
1561 ECerror(ERR_R_MALLOC_FAILURE
);
1564 if (!EC_POINT_oct2point(ret
->group
, ret
->pub_key
, *in
, len
, NULL
)) {
1565 ECerror(ERR_R_EC_LIB
);
1568 /* save the point conversion form */
1569 ret
->conv_form
= (point_conversion_form_t
) (*in
[0] & ~0x01);
1575 i2o_ECPublicKey(EC_KEY
* a
, unsigned char **out
)
1581 ECerror(ERR_R_PASSED_NULL_PARAMETER
);
1584 buf_len
= EC_POINT_point2oct(a
->group
, a
->pub_key
,
1585 a
->conv_form
, NULL
, 0, NULL
);
1587 if (out
== NULL
|| buf_len
== 0)
1588 /* out == NULL => just return the length of the octet string */
1592 if ((*out
= malloc(buf_len
)) == NULL
) {
1593 ECerror(ERR_R_MALLOC_FAILURE
);
1598 if (!EC_POINT_point2oct(a
->group
, a
->pub_key
, a
->conv_form
,
1599 *out
, buf_len
, NULL
)) {
1600 ECerror(ERR_R_EC_LIB
);