1 /* crypto/ec/ec_asn1.c */
2 /* ====================================================================
3 * Copyright (c) 2000-2002 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
58 #include <openssl/asn1.h>
59 #include <openssl/asn1t.h>
60 #include <openssl/objects.h>
62 /* some structures needed for the asn1 encoding */
63 typedef struct x9_62_fieldid_st {
64 ASN1_OBJECT *fieldType;
65 ASN1_TYPE *parameters;
68 typedef struct x9_62_characteristic_two_st {
71 ASN1_TYPE *parameters;
72 } X9_62_CHARACTERISTIC_TWO;
74 typedef struct x9_62_pentanomial_st {
80 typedef struct x9_62_curve_st {
83 ASN1_BIT_STRING *seed;
86 struct ec_parameters_st {
87 ASN1_INTEGER *version;
88 X9_62_FIELDID *fieldID;
90 ASN1_OCTET_STRING *base;
92 ASN1_INTEGER *cofactor;
95 struct ecpk_parameters_st {
98 ASN1_OBJECT *named_curve;
99 ECPARAMETERS *parameters;
100 ASN1_NULL *implicitlyCA;
102 }/* ECPKPARAMETERS */;
104 /* the OpenSSL asn1 definitions */
106 ASN1_SEQUENCE(X9_62_FIELDID) = {
107 ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
108 ASN1_SIMPLE(X9_62_FIELDID, parameters, ASN1_ANY)
109 } ASN1_SEQUENCE_END(X9_62_FIELDID)
111 DECLARE_ASN1_FUNCTIONS_const(X9_62_FIELDID)
112 DECLARE_ASN1_ENCODE_FUNCTIONS_const(X9_62_FIELDID, X9_62_FIELDID)
113 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_FIELDID)
115 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
116 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, m, ASN1_INTEGER),
117 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, basis, ASN1_OBJECT),
118 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, parameters, ASN1_ANY)
119 } ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
121 DECLARE_ASN1_FUNCTIONS_const(X9_62_CHARACTERISTIC_TWO)
122 DECLARE_ASN1_ENCODE_FUNCTIONS_const(X9_62_CHARACTERISTIC_TWO, X9_62_CHARACTERISTIC_TWO)
123 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_CHARACTERISTIC_TWO)
125 ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
126 ASN1_SIMPLE(X9_62_PENTANOMIAL, k1, ASN1_INTEGER),
127 ASN1_SIMPLE(X9_62_PENTANOMIAL, k2, ASN1_INTEGER),
128 ASN1_SIMPLE(X9_62_PENTANOMIAL, k3, ASN1_INTEGER)
129 } ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
131 DECLARE_ASN1_FUNCTIONS_const(X9_62_PENTANOMIAL)
132 DECLARE_ASN1_ENCODE_FUNCTIONS_const(X9_62_PENTANOMIAL, X9_62_PENTANOMIAL)
133 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_PENTANOMIAL)
135 ASN1_SEQUENCE(X9_62_CURVE) = {
136 ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
137 ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
138 ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
139 } ASN1_SEQUENCE_END(X9_62_CURVE)
141 DECLARE_ASN1_FUNCTIONS_const(X9_62_CURVE)
142 DECLARE_ASN1_ENCODE_FUNCTIONS_const(X9_62_CURVE, X9_62_CURVE)
143 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_CURVE)
145 ASN1_SEQUENCE(ECPARAMETERS) = {
146 ASN1_SIMPLE(ECPARAMETERS, version, ASN1_INTEGER),
147 ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
148 ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
149 ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
150 ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
151 ASN1_SIMPLE(ECPARAMETERS, cofactor, ASN1_INTEGER)
152 } ASN1_SEQUENCE_END(ECPARAMETERS)
154 DECLARE_ASN1_FUNCTIONS_const(ECPARAMETERS)
155 IMPLEMENT_ASN1_FUNCTIONS_const(ECPARAMETERS)
157 ASN1_CHOICE(ECPKPARAMETERS) = {
158 ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
159 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
160 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
161 } ASN1_CHOICE_END(ECPKPARAMETERS)
163 DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
164 IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
166 /* some internal functions */
168 static X9_62_FIELDID *ec_asn1_group2field(const EC_GROUP *, X9_62_FIELDID *);
169 static X9_62_CURVE *ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
170 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *);
171 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *,
174 static X9_62_FIELDID *ec_asn1_group2field(const EC_GROUP *group,
175 X9_62_FIELDID *field)
178 X9_62_FIELDID *ret=NULL;
183 if ((ret = X9_62_FIELDID_new()) == NULL)
185 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
192 if (ret->fieldType != NULL)
193 ASN1_OBJECT_free(ret->fieldType);
194 if (ret->parameters != NULL)
195 ASN1_TYPE_free(ret->parameters);
198 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
200 if ((ret->fieldType = OBJ_nid2obj(nid)) == NULL)
202 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
206 if (nid == NID_X9_62_prime_field)
208 if ((tmp = BN_new()) == NULL)
210 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
213 if ((ret->parameters = ASN1_TYPE_new()) == NULL)
215 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
218 ret->parameters->type = V_ASN1_INTEGER;
219 if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL))
221 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
224 ret->parameters->value.integer = BN_to_ASN1_INTEGER(tmp, NULL);
225 if (ret->parameters->value.integer == NULL)
227 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
239 X9_62_FIELDID_free(ret);
247 static X9_62_CURVE *ec_asn1_group2curve(const EC_GROUP *group,
251 X9_62_CURVE *ret=NULL;
254 unsigned char *buffer_1=NULL,
259 unsigned char char_zero = 0;
261 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL)
263 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
269 if ((ret = X9_62_CURVE_new()) == NULL)
271 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
279 ASN1_OCTET_STRING_free(ret->a);
281 ASN1_OCTET_STRING_free(ret->b);
283 ASN1_BIT_STRING_free(ret->seed);
286 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
289 if (nid == NID_X9_62_prime_field)
291 if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL))
293 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
297 len_1 = (size_t)BN_num_bytes(tmp_1);
298 len_2 = (size_t)BN_num_bytes(tmp_2);
302 /* len_1 == 0 => a == 0 */
308 if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL)
310 ECerr(EC_F_EC_ASN1_GROUP2CURVE,
311 ERR_R_MALLOC_FAILURE);
314 if ( (len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0)
316 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
324 /* len_2 == 0 => b == 0 */
330 if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL)
332 ECerr(EC_F_EC_ASN1_GROUP2CURVE,
333 ERR_R_MALLOC_FAILURE);
336 if ( (len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0)
338 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
348 if ((ret->a = M_ASN1_OCTET_STRING_new()) == NULL ||
349 (ret->b = M_ASN1_OCTET_STRING_new()) == NULL )
351 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
354 if (!M_ASN1_OCTET_STRING_set(ret->a, a_buf, len_1) ||
355 !M_ASN1_OCTET_STRING_set(ret->b, b_buf, len_2))
357 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
361 /* set the seed (optional) */
364 if ((ret->seed = ASN1_BIT_STRING_new()) == NULL) goto err;
365 if (!ASN1_BIT_STRING_set(ret->seed, group->seed,
366 (int)group->seed_len))
368 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
380 X9_62_CURVE_free(ret);
384 OPENSSL_free(buffer_1);
386 OPENSSL_free(buffer_2);
394 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
399 ECPARAMETERS *ret=NULL;
401 unsigned char *buffer=NULL;
402 const EC_POINT *point=NULL;
403 point_conversion_form_t form;
405 if ((tmp = BN_new()) == NULL)
407 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
413 if ((ret = ECPARAMETERS_new()) == NULL)
415 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS,
416 ERR_R_MALLOC_FAILURE);
423 /* set the version (always one) */
424 if (ret->version == NULL && !(ret->version = ASN1_INTEGER_new()))
426 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
429 if (!ASN1_INTEGER_set(ret->version, (long)0x1))
431 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
435 /* set the fieldID */
436 ret->fieldID = ec_asn1_group2field(group, ret->fieldID);
437 if (ret->fieldID == NULL)
439 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
444 ret->curve = ec_asn1_group2curve(group, ret->curve);
445 if (ret->curve == NULL)
447 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
451 /* set the base point */
452 if ((point = EC_GROUP_get0_generator(group)) == NULL)
454 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
458 form = EC_GROUP_get_point_conversion_form(group);
460 len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
463 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
466 if ((buffer = OPENSSL_malloc(len)) == NULL)
468 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
471 if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL))
473 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
476 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL)
478 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
481 if (!ASN1_OCTET_STRING_set(ret->base, buffer, len))
483 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
488 if (!EC_GROUP_get_order(group, tmp, NULL))
490 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
493 ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
494 if (ret->order == NULL)
496 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
500 /* set the cofactor */
501 if (!EC_GROUP_get_cofactor(group, tmp, NULL))
503 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
506 ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
507 if (ret->cofactor == NULL)
509 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
518 ECPARAMETERS_free(ret);
524 OPENSSL_free(buffer);
528 ECPKPARAMETERS *EC_ASN1_group2pkparameters(const EC_GROUP *group,
529 ECPKPARAMETERS *params)
532 ECPKPARAMETERS *ret = params;
536 if ((ret = ECPKPARAMETERS_new()) == NULL)
538 ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS,
539 ERR_R_MALLOC_FAILURE);
545 if (ret->type == 0 && ret->value.named_curve)
546 ASN1_OBJECT_free(ret->value.named_curve);
547 else if (ret->type == 1 && ret->value.parameters)
548 ECPARAMETERS_free(ret->value.parameters);
551 if (EC_GROUP_get_asn1_flag(group))
553 /* use the asn1 OID to describe the
554 * the elliptic curve parameters
556 tmp = EC_GROUP_get_nid(group);
560 if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
565 /* we have no nid => use the normal
566 * ECPARAMETERS structure
569 if ((ret->value.parameters = ec_asn1_group2parameters(
570 group, NULL)) == NULL)
576 /* use the ECPARAMETERS structure */
578 if ((ret->value.parameters = ec_asn1_group2parameters(
579 group, NULL)) == NULL)
585 ECPKPARAMETERS_free(ret);
591 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
595 BIGNUM *p=NULL, *a=NULL, *b=NULL;
596 EC_POINT *point=NULL;
598 if (!params->fieldID || !params->fieldID->fieldType ||
599 !params->fieldID->parameters)
601 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
605 tmp = OBJ_obj2nid(params->fieldID->fieldType);
607 if (tmp == NID_X9_62_characteristic_two_field)
609 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
612 else if (tmp == NID_X9_62_prime_field)
614 /* we have a curve over a prime field */
615 /* extract the prime number */
616 if (params->fieldID->parameters->type != V_ASN1_INTEGER ||
617 !params->fieldID->parameters->value.integer)
619 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
622 p = ASN1_INTEGER_to_BN(params->fieldID->parameters->value.integer, NULL);
625 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
628 /* now extract the curve parameters a and b */
629 if (!params->curve || !params->curve->a ||
630 !params->curve->a->data || !params->curve->b ||
631 !params->curve->b->data)
633 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
636 a = BN_bin2bn(params->curve->a->data,
637 params->curve->a->length, NULL);
640 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
643 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
646 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
649 /* create the EC_GROUP structure */
651 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
654 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
657 /* create the generator */
658 if ((point = EC_POINT_new(ret)) == NULL) goto err;
662 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_UNKNOWN_FIELD);
666 if (params->curve->seed != NULL)
668 if (ret->seed != NULL)
669 OPENSSL_free(ret->seed);
670 if (!(ret->seed = OPENSSL_malloc(params->curve->seed->length)))
672 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
673 ERR_R_MALLOC_FAILURE);
676 memcpy(ret->seed, params->curve->seed->data,
677 params->curve->seed->length);
678 ret->seed_len = params->curve->seed->length;
681 if (!params->order || !params->cofactor || !params->base ||
684 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
689 a = ASN1_INTEGER_to_BN(params->order, a);
690 b = ASN1_INTEGER_to_BN(params->cofactor, b);
693 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
697 if (!EC_POINT_oct2point(ret, point, params->base->data,
698 params->base->length, NULL))
700 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
703 if (!EC_GROUP_set_generator(ret, point, a, b))
705 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
714 EC_GROUP_clear_free(ret);
725 EC_POINT_free(point);
729 EC_GROUP *EC_ASN1_pkparameters2group(const ECPKPARAMETERS *params)
736 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
737 EC_R_MISSING_PARAMETERS);
741 if (params->type == 0)
742 { /* the curve is given by an OID */
743 tmp = OBJ_obj2nid(params->value.named_curve);
744 if ((ret = EC_GROUP_new_by_name(tmp)) == NULL)
746 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
747 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
750 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
752 else if (params->type == 1)
753 { /* the parameters are given by a ECPARAMETERS
755 ret = ec_asn1_parameters2group(params->value.parameters);
758 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
761 EC_GROUP_set_asn1_flag(ret, 0x0);
763 else if (params->type == 2)
769 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
776 /* EC_GROUP <-> DER encoding of EC[PK]PARAMETERS */
778 EC_GROUP *d2i_ECParameters(EC_GROUP **a, const unsigned char **in, long len)
780 EC_GROUP *group = NULL;
781 ECPARAMETERS *params = NULL;
783 if ((params = d2i_ECPARAMETERS(NULL, in, len)) == NULL)
785 ECerr(EC_F_D2I_ECPARAMETERS, EC_R_D2I_ECPARAMETERS_FAILURE);
786 ECPARAMETERS_free(params);
790 if ((group = ec_asn1_parameters2group(params)) == NULL)
792 ECerr(EC_F_D2I_ECPARAMETERS, EC_R_PARAMETERS2GROUP_FAILURE);
797 EC_GROUP_clear_free(*a);
801 ECPARAMETERS_free(params);
805 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
807 EC_GROUP *group = NULL;
808 ECPKPARAMETERS *params = NULL;
810 if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL)
812 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
813 ECPKPARAMETERS_free(params);
817 if ((group = EC_ASN1_pkparameters2group(params)) == NULL)
819 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
825 EC_GROUP_clear_free(*a);
829 ECPKPARAMETERS_free(params);
833 int i2d_ECParameters(const EC_GROUP *a, unsigned char **out)
836 ECPARAMETERS *tmp = ec_asn1_group2parameters(a, NULL);
839 ECerr(EC_F_I2D_ECPARAMETERS, EC_R_GROUP2PARAMETERS_FAILURE);
842 if ((ret = i2d_ECPARAMETERS(tmp, out)) == 0)
844 ECerr(EC_F_I2D_ECPARAMETERS, EC_R_I2D_EC_PARAMETERS_FAILURE);
845 ECPARAMETERS_free(tmp);
848 ECPARAMETERS_free(tmp);
852 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
855 ECPKPARAMETERS *tmp = EC_ASN1_group2pkparameters(a, NULL);
858 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
861 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0)
863 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
864 ECPKPARAMETERS_free(tmp);
867 ECPKPARAMETERS_free(tmp);