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).
57 #include <openssl/err.h>
58 #include <openssl/asn1t.h>
59 #include <openssl/objects.h>
61 /* some structures needed for the asn1 encoding */
62 typedef struct x9_62_fieldid_st {
63 ASN1_OBJECT *fieldType;
64 ASN1_TYPE *parameters;
67 typedef struct x9_62_characteristic_two_st {
70 ASN1_TYPE *parameters;
71 } X9_62_CHARACTERISTIC_TWO;
73 typedef struct x9_62_pentanomial_st {
79 typedef struct x9_62_curve_st {
82 ASN1_BIT_STRING *seed;
85 struct ec_parameters_st {
86 ASN1_INTEGER *version;
87 X9_62_FIELDID *fieldID;
89 ASN1_OCTET_STRING *base;
91 ASN1_INTEGER *cofactor;
94 struct ecpk_parameters_st {
97 ASN1_OBJECT *named_curve;
98 ECPARAMETERS *parameters;
99 ASN1_NULL *implicitlyCA;
101 }/* ECPKPARAMETERS */;
103 /* the OpenSSL asn1 definitions */
105 ASN1_SEQUENCE(X9_62_FIELDID) = {
106 ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
107 ASN1_SIMPLE(X9_62_FIELDID, parameters, ASN1_ANY)
108 } ASN1_SEQUENCE_END(X9_62_FIELDID)
110 DECLARE_ASN1_FUNCTIONS_const(X9_62_FIELDID)
111 DECLARE_ASN1_ENCODE_FUNCTIONS_const(X9_62_FIELDID, X9_62_FIELDID)
112 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_FIELDID)
114 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
115 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, m, ASN1_INTEGER),
116 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, basis, ASN1_OBJECT),
117 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, parameters, ASN1_ANY)
118 } ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
120 DECLARE_ASN1_FUNCTIONS_const(X9_62_CHARACTERISTIC_TWO)
121 DECLARE_ASN1_ENCODE_FUNCTIONS_const(X9_62_CHARACTERISTIC_TWO, X9_62_CHARACTERISTIC_TWO)
122 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_CHARACTERISTIC_TWO)
124 ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
125 ASN1_SIMPLE(X9_62_PENTANOMIAL, k1, ASN1_INTEGER),
126 ASN1_SIMPLE(X9_62_PENTANOMIAL, k2, ASN1_INTEGER),
127 ASN1_SIMPLE(X9_62_PENTANOMIAL, k3, ASN1_INTEGER)
128 } ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
130 DECLARE_ASN1_FUNCTIONS_const(X9_62_PENTANOMIAL)
131 DECLARE_ASN1_ENCODE_FUNCTIONS_const(X9_62_PENTANOMIAL, X9_62_PENTANOMIAL)
132 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_PENTANOMIAL)
134 ASN1_SEQUENCE(X9_62_CURVE) = {
135 ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
136 ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
137 ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
138 } ASN1_SEQUENCE_END(X9_62_CURVE)
140 DECLARE_ASN1_FUNCTIONS_const(X9_62_CURVE)
141 DECLARE_ASN1_ENCODE_FUNCTIONS_const(X9_62_CURVE, X9_62_CURVE)
142 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_CURVE)
144 ASN1_SEQUENCE(ECPARAMETERS) = {
145 ASN1_SIMPLE(ECPARAMETERS, version, ASN1_INTEGER),
146 ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
147 ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
148 ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
149 ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
150 ASN1_SIMPLE(ECPARAMETERS, cofactor, ASN1_INTEGER)
151 } ASN1_SEQUENCE_END(ECPARAMETERS)
153 DECLARE_ASN1_FUNCTIONS_const(ECPARAMETERS)
154 IMPLEMENT_ASN1_FUNCTIONS_const(ECPARAMETERS)
156 ASN1_CHOICE(ECPKPARAMETERS) = {
157 ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
158 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
159 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
160 } ASN1_CHOICE_END(ECPKPARAMETERS)
162 DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
163 IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
165 ASN1_SEQUENCE(EC_PRIVATEKEY) = {
166 ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
167 ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
168 ASN1_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS),
169 ASN1_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING)
170 } ASN1_SEQUENCE_END(EC_PRIVATEKEY)
172 IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
174 /* some internal functions */
176 static X9_62_FIELDID *ec_asn1_group2field(const EC_GROUP *, X9_62_FIELDID *);
177 static X9_62_CURVE *ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
178 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *);
179 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *,
182 static X9_62_FIELDID *ec_asn1_group2field(const EC_GROUP *group,
183 X9_62_FIELDID *field)
186 X9_62_FIELDID *ret=NULL;
191 if ((ret = X9_62_FIELDID_new()) == NULL)
193 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
200 if (ret->fieldType != NULL)
201 ASN1_OBJECT_free(ret->fieldType);
202 if (ret->parameters != NULL)
203 ASN1_TYPE_free(ret->parameters);
206 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
208 if ((ret->fieldType = OBJ_nid2obj(nid)) == NULL)
210 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
214 if (nid == NID_X9_62_prime_field)
216 if ((tmp = BN_new()) == NULL)
218 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
221 if ((ret->parameters = ASN1_TYPE_new()) == NULL)
223 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
226 ret->parameters->type = V_ASN1_INTEGER;
227 if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL))
229 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
232 ret->parameters->value.integer = BN_to_ASN1_INTEGER(tmp, NULL);
233 if (ret->parameters->value.integer == NULL)
235 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
247 X9_62_FIELDID_free(ret);
255 static X9_62_CURVE *ec_asn1_group2curve(const EC_GROUP *group,
259 X9_62_CURVE *ret=NULL;
262 unsigned char *buffer_1=NULL,
267 unsigned char char_zero = 0;
269 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL)
271 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
277 if ((ret = X9_62_CURVE_new()) == NULL)
279 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
287 ASN1_OCTET_STRING_free(ret->a);
289 ASN1_OCTET_STRING_free(ret->b);
291 ASN1_BIT_STRING_free(ret->seed);
294 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
297 if (nid == NID_X9_62_prime_field)
299 if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL))
301 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
305 len_1 = (size_t)BN_num_bytes(tmp_1);
306 len_2 = (size_t)BN_num_bytes(tmp_2);
310 /* len_1 == 0 => a == 0 */
316 if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL)
318 ECerr(EC_F_EC_ASN1_GROUP2CURVE,
319 ERR_R_MALLOC_FAILURE);
322 if ( (len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0)
324 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
332 /* len_2 == 0 => b == 0 */
338 if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL)
340 ECerr(EC_F_EC_ASN1_GROUP2CURVE,
341 ERR_R_MALLOC_FAILURE);
344 if ( (len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0)
346 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
356 if ((ret->a = M_ASN1_OCTET_STRING_new()) == NULL ||
357 (ret->b = M_ASN1_OCTET_STRING_new()) == NULL )
359 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
362 if (!M_ASN1_OCTET_STRING_set(ret->a, a_buf, len_1) ||
363 !M_ASN1_OCTET_STRING_set(ret->b, b_buf, len_2))
365 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
369 /* set the seed (optional) */
372 if ((ret->seed = ASN1_BIT_STRING_new()) == NULL) goto err;
373 if (!ASN1_BIT_STRING_set(ret->seed, group->seed,
374 (int)group->seed_len))
376 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
388 X9_62_CURVE_free(ret);
392 OPENSSL_free(buffer_1);
394 OPENSSL_free(buffer_2);
402 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
407 ECPARAMETERS *ret=NULL;
409 unsigned char *buffer=NULL;
410 const EC_POINT *point=NULL;
411 point_conversion_form_t form;
413 if ((tmp = BN_new()) == NULL)
415 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
421 if ((ret = ECPARAMETERS_new()) == NULL)
423 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS,
424 ERR_R_MALLOC_FAILURE);
431 /* set the version (always one) */
432 if (ret->version == NULL && !(ret->version = ASN1_INTEGER_new()))
434 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
437 if (!ASN1_INTEGER_set(ret->version, (long)0x1))
439 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
443 /* set the fieldID */
444 ret->fieldID = ec_asn1_group2field(group, ret->fieldID);
445 if (ret->fieldID == NULL)
447 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
452 ret->curve = ec_asn1_group2curve(group, ret->curve);
453 if (ret->curve == NULL)
455 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
459 /* set the base point */
460 if ((point = EC_GROUP_get0_generator(group)) == NULL)
462 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
466 form = EC_GROUP_get_point_conversion_form(group);
468 len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
471 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
474 if ((buffer = OPENSSL_malloc(len)) == NULL)
476 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
479 if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL))
481 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
484 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL)
486 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
489 if (!ASN1_OCTET_STRING_set(ret->base, buffer, len))
491 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
496 if (!EC_GROUP_get_order(group, tmp, NULL))
498 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
501 ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
502 if (ret->order == NULL)
504 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
508 /* set the cofactor */
509 if (!EC_GROUP_get_cofactor(group, tmp, NULL))
511 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
514 ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
515 if (ret->cofactor == NULL)
517 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
526 ECPARAMETERS_free(ret);
532 OPENSSL_free(buffer);
536 ECPKPARAMETERS *EC_ASN1_group2pkparameters(const EC_GROUP *group,
537 ECPKPARAMETERS *params)
540 ECPKPARAMETERS *ret = params;
544 if ((ret = ECPKPARAMETERS_new()) == NULL)
546 ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS,
547 ERR_R_MALLOC_FAILURE);
553 if (ret->type == 0 && ret->value.named_curve)
554 ASN1_OBJECT_free(ret->value.named_curve);
555 else if (ret->type == 1 && ret->value.parameters)
556 ECPARAMETERS_free(ret->value.parameters);
559 if (EC_GROUP_get_asn1_flag(group))
561 /* use the asn1 OID to describe the
562 * the elliptic curve parameters
564 tmp = EC_GROUP_get_nid(group);
568 if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
573 /* we have no nid => use the normal
574 * ECPARAMETERS structure
577 if ((ret->value.parameters = ec_asn1_group2parameters(
578 group, NULL)) == NULL)
584 /* use the ECPARAMETERS structure */
586 if ((ret->value.parameters = ec_asn1_group2parameters(
587 group, NULL)) == NULL)
593 ECPKPARAMETERS_free(ret);
599 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
603 BIGNUM *p=NULL, *a=NULL, *b=NULL;
604 EC_POINT *point=NULL;
606 if (!params->fieldID || !params->fieldID->fieldType ||
607 !params->fieldID->parameters)
609 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
613 tmp = OBJ_obj2nid(params->fieldID->fieldType);
615 if (tmp == NID_X9_62_characteristic_two_field)
617 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
620 else if (tmp == NID_X9_62_prime_field)
622 /* we have a curve over a prime field */
623 /* extract the prime number */
624 if (params->fieldID->parameters->type != V_ASN1_INTEGER ||
625 !params->fieldID->parameters->value.integer)
627 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
630 p = ASN1_INTEGER_to_BN(params->fieldID->parameters->value.integer, NULL);
633 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
636 /* now extract the curve parameters a and b */
637 if (!params->curve || !params->curve->a ||
638 !params->curve->a->data || !params->curve->b ||
639 !params->curve->b->data)
641 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
644 a = BN_bin2bn(params->curve->a->data,
645 params->curve->a->length, NULL);
648 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
651 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
654 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
657 /* create the EC_GROUP structure */
659 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
662 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
665 /* create the generator */
666 if ((point = EC_POINT_new(ret)) == NULL) goto err;
670 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_UNKNOWN_FIELD);
674 if (params->curve->seed != NULL)
676 if (ret->seed != NULL)
677 OPENSSL_free(ret->seed);
678 if (!(ret->seed = OPENSSL_malloc(params->curve->seed->length)))
680 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
681 ERR_R_MALLOC_FAILURE);
684 memcpy(ret->seed, params->curve->seed->data,
685 params->curve->seed->length);
686 ret->seed_len = params->curve->seed->length;
689 if (!params->order || !params->cofactor || !params->base ||
692 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
697 a = ASN1_INTEGER_to_BN(params->order, a);
698 b = ASN1_INTEGER_to_BN(params->cofactor, b);
701 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
705 if (!EC_POINT_oct2point(ret, point, params->base->data,
706 params->base->length, NULL))
708 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
712 /* set the point conversion form */
713 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
714 (params->base->data[0] & ~0x01));
716 if (!EC_GROUP_set_generator(ret, point, a, b))
718 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
727 EC_GROUP_clear_free(ret);
738 EC_POINT_free(point);
742 EC_GROUP *EC_ASN1_pkparameters2group(const ECPKPARAMETERS *params)
749 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
750 EC_R_MISSING_PARAMETERS);
754 if (params->type == 0)
755 { /* the curve is given by an OID */
756 tmp = OBJ_obj2nid(params->value.named_curve);
757 if ((ret = EC_GROUP_new_by_name(tmp)) == NULL)
759 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
760 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
763 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
765 else if (params->type == 1)
766 { /* the parameters are given by a ECPARAMETERS
768 ret = ec_asn1_parameters2group(params->value.parameters);
771 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
774 EC_GROUP_set_asn1_flag(ret, 0x0);
776 else if (params->type == 2)
782 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
789 /* EC_GROUP <-> DER encoding of EC[PK]PARAMETERS */
791 EC_GROUP *d2i_ECParameters(EC_GROUP **a, const unsigned char **in, long len)
793 EC_GROUP *group = NULL;
794 ECPARAMETERS *params = NULL;
796 if ((params = d2i_ECPARAMETERS(NULL, in, len)) == NULL)
798 ECerr(EC_F_D2I_ECPARAMETERS, EC_R_D2I_ECPARAMETERS_FAILURE);
799 ECPARAMETERS_free(params);
803 if ((group = ec_asn1_parameters2group(params)) == NULL)
805 ECerr(EC_F_D2I_ECPARAMETERS, EC_R_PARAMETERS2GROUP_FAILURE);
810 EC_GROUP_clear_free(*a);
814 ECPARAMETERS_free(params);
818 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
820 EC_GROUP *group = NULL;
821 ECPKPARAMETERS *params = NULL;
823 if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL)
825 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
826 ECPKPARAMETERS_free(params);
830 if ((group = EC_ASN1_pkparameters2group(params)) == NULL)
832 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
838 EC_GROUP_clear_free(*a);
842 ECPKPARAMETERS_free(params);
846 int i2d_ECParameters(const EC_GROUP *a, unsigned char **out)
849 ECPARAMETERS *tmp = ec_asn1_group2parameters(a, NULL);
852 ECerr(EC_F_I2D_ECPARAMETERS, EC_R_GROUP2PARAMETERS_FAILURE);
855 if ((ret = i2d_ECPARAMETERS(tmp, out)) == 0)
857 ECerr(EC_F_I2D_ECPARAMETERS, EC_R_I2D_EC_PARAMETERS_FAILURE);
858 ECPARAMETERS_free(tmp);
861 ECPARAMETERS_free(tmp);
865 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
868 ECPKPARAMETERS *tmp = EC_ASN1_group2pkparameters(a, NULL);
871 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
874 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0)
876 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
877 ECPKPARAMETERS_free(tmp);
880 ECPKPARAMETERS_free(tmp);