Add group_order_bits to EC_METHOD.
[openssl.git] / crypto / ec / ec_asn1.c
1 /*
2  * Written by Nils Larsch for the OpenSSL project.
3  */
4 /* ====================================================================
5  * Copyright (c) 2000-2003 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    licensing@OpenSSL.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * (eay@cryptsoft.com).  This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com).
55  *
56  */
57
58 #include <string.h>
59 #include "ec_lcl.h"
60 #include <openssl/err.h>
61 #include <openssl/asn1t.h>
62 #include <openssl/objects.h>
63
64 int EC_GROUP_get_basis_type(const EC_GROUP *group)
65 {
66     int i = 0;
67
68     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
69         NID_X9_62_characteristic_two_field)
70         /* everything else is currently not supported */
71         return 0;
72
73     while (group->poly[i] != 0)
74         i++;
75
76     if (i == 4)
77         return NID_X9_62_ppBasis;
78     else if (i == 2)
79         return NID_X9_62_tpBasis;
80     else
81         /* everything else is currently not supported */
82         return 0;
83 }
84
85 #ifndef OPENSSL_NO_EC2M
86 int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
87 {
88     if (group == NULL)
89         return 0;
90
91     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
92         NID_X9_62_characteristic_two_field
93         || !((group->poly[0] != 0) && (group->poly[1] != 0)
94              && (group->poly[2] == 0))) {
95         ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS,
96               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
97         return 0;
98     }
99
100     if (k)
101         *k = group->poly[1];
102
103     return 1;
104 }
105
106 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
107                                    unsigned int *k2, unsigned int *k3)
108 {
109     if (group == NULL)
110         return 0;
111
112     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
113         NID_X9_62_characteristic_two_field
114         || !((group->poly[0] != 0) && (group->poly[1] != 0)
115              && (group->poly[2] != 0) && (group->poly[3] != 0)
116              && (group->poly[4] == 0))) {
117         ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS,
118               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
119         return 0;
120     }
121
122     if (k1)
123         *k1 = group->poly[3];
124     if (k2)
125         *k2 = group->poly[2];
126     if (k3)
127         *k3 = group->poly[1];
128
129     return 1;
130 }
131 #endif
132
133 /* some structures needed for the asn1 encoding */
134 typedef struct x9_62_pentanomial_st {
135     long k1;
136     long k2;
137     long k3;
138 } X9_62_PENTANOMIAL;
139
140 typedef struct x9_62_characteristic_two_st {
141     long m;
142     ASN1_OBJECT *type;
143     union {
144         char *ptr;
145         /* NID_X9_62_onBasis */
146         ASN1_NULL *onBasis;
147         /* NID_X9_62_tpBasis */
148         ASN1_INTEGER *tpBasis;
149         /* NID_X9_62_ppBasis */
150         X9_62_PENTANOMIAL *ppBasis;
151         /* anything else */
152         ASN1_TYPE *other;
153     } p;
154 } X9_62_CHARACTERISTIC_TWO;
155
156 typedef struct x9_62_fieldid_st {
157     ASN1_OBJECT *fieldType;
158     union {
159         char *ptr;
160         /* NID_X9_62_prime_field */
161         ASN1_INTEGER *prime;
162         /* NID_X9_62_characteristic_two_field */
163         X9_62_CHARACTERISTIC_TWO *char_two;
164         /* anything else */
165         ASN1_TYPE *other;
166     } p;
167 } X9_62_FIELDID;
168
169 typedef struct x9_62_curve_st {
170     ASN1_OCTET_STRING *a;
171     ASN1_OCTET_STRING *b;
172     ASN1_BIT_STRING *seed;
173 } X9_62_CURVE;
174
175 typedef struct ec_parameters_st {
176     long version;
177     X9_62_FIELDID *fieldID;
178     X9_62_CURVE *curve;
179     ASN1_OCTET_STRING *base;
180     ASN1_INTEGER *order;
181     ASN1_INTEGER *cofactor;
182 } ECPARAMETERS;
183
184 struct ecpk_parameters_st {
185     int type;
186     union {
187         ASN1_OBJECT *named_curve;
188         ECPARAMETERS *parameters;
189         ASN1_NULL *implicitlyCA;
190     } value;
191 } /* ECPKPARAMETERS */ ;
192
193 /* SEC1 ECPrivateKey */
194 typedef struct ec_privatekey_st {
195     long version;
196     ASN1_OCTET_STRING *privateKey;
197     ECPKPARAMETERS *parameters;
198     ASN1_BIT_STRING *publicKey;
199 } EC_PRIVATEKEY;
200
201 /* the OpenSSL ASN.1 definitions */
202 ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
203         ASN1_SIMPLE(X9_62_PENTANOMIAL, k1, LONG),
204         ASN1_SIMPLE(X9_62_PENTANOMIAL, k2, LONG),
205         ASN1_SIMPLE(X9_62_PENTANOMIAL, k3, LONG)
206 } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
207
208 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
209 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
210
211 ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
212
213 ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
214         ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
215         ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
216         ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
217 } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
218
219 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
220         ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, m, LONG),
221         ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
222         ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
223 } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
224
225 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
226 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
227
228 ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
229
230 ASN1_ADB(X9_62_FIELDID) = {
231         ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
232         ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
233 } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
234
235 ASN1_SEQUENCE(X9_62_FIELDID) = {
236         ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
237         ASN1_ADB_OBJECT(X9_62_FIELDID)
238 } static_ASN1_SEQUENCE_END(X9_62_FIELDID)
239
240 ASN1_SEQUENCE(X9_62_CURVE) = {
241         ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
242         ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
243         ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
244 } static_ASN1_SEQUENCE_END(X9_62_CURVE)
245
246 ASN1_SEQUENCE(ECPARAMETERS) = {
247         ASN1_SIMPLE(ECPARAMETERS, version, LONG),
248         ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
249         ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
250         ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
251         ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
252         ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
253 } static_ASN1_SEQUENCE_END(ECPARAMETERS)
254
255 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
256 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
257
258 ASN1_CHOICE(ECPKPARAMETERS) = {
259         ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
260         ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
261         ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
262 } static_ASN1_CHOICE_END(ECPKPARAMETERS)
263
264 DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
265 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
266 IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
267
268 ASN1_SEQUENCE(EC_PRIVATEKEY) = {
269         ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
270         ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
271         ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
272         ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
273 } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY)
274
275 DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
276 DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
277 IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
278
279 /* some declarations of internal function */
280
281 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
282 static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
283 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
284 static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
285 /*
286  * ec_asn1_parameters2group() creates a EC_GROUP object from a ECPARAMETERS
287  * object
288  */
289 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *);
290 /*
291  * ec_asn1_group2parameters() creates a ECPARAMETERS object from a EC_GROUP
292  * object
293  */
294 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *,
295                                               ECPARAMETERS *);
296 /*
297  * ec_asn1_pkparameters2group() creates a EC_GROUP object from a
298  * ECPKPARAMETERS object
299  */
300 static EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *);
301 /*
302  * ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a
303  * EC_GROUP object
304  */
305 static ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *,
306                                                   ECPKPARAMETERS *);
307
308 /* the function definitions */
309
310 static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
311 {
312     int ok = 0, nid;
313     BIGNUM *tmp = NULL;
314
315     if (group == NULL || field == NULL)
316         return 0;
317
318     /* clear the old values (if necessary) */
319     ASN1_OBJECT_free(field->fieldType);
320     ASN1_TYPE_free(field->p.other);
321
322     nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
323     /* set OID for the field */
324     if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
325         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
326         goto err;
327     }
328
329     if (nid == NID_X9_62_prime_field) {
330         if ((tmp = BN_new()) == NULL) {
331             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
332             goto err;
333         }
334         /* the parameters are specified by the prime number p */
335         if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) {
336             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
337             goto err;
338         }
339         /* set the prime number */
340         field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
341         if (field->p.prime == NULL) {
342             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
343             goto err;
344         }
345     } else if (nid == NID_X9_62_characteristic_two_field)
346 #ifdef OPENSSL_NO_EC2M
347     {
348         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
349         goto err;
350     }
351 #else
352     {
353         int field_type;
354         X9_62_CHARACTERISTIC_TWO *char_two;
355
356         field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
357         char_two = field->p.char_two;
358
359         if (char_two == NULL) {
360             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
361             goto err;
362         }
363
364         char_two->m = (long)EC_GROUP_get_degree(group);
365
366         field_type = EC_GROUP_get_basis_type(group);
367
368         if (field_type == 0) {
369             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
370             goto err;
371         }
372         /* set base type OID */
373         if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
374             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
375             goto err;
376         }
377
378         if (field_type == NID_X9_62_tpBasis) {
379             unsigned int k;
380
381             if (!EC_GROUP_get_trinomial_basis(group, &k))
382                 goto err;
383
384             char_two->p.tpBasis = ASN1_INTEGER_new();
385             if (char_two->p.tpBasis == NULL) {
386                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
387                 goto err;
388             }
389             if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
390                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
391                 goto err;
392             }
393         } else if (field_type == NID_X9_62_ppBasis) {
394             unsigned int k1, k2, k3;
395
396             if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
397                 goto err;
398
399             char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
400             if (char_two->p.ppBasis == NULL) {
401                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
402                 goto err;
403             }
404
405             /* set k? values */
406             char_two->p.ppBasis->k1 = (long)k1;
407             char_two->p.ppBasis->k2 = (long)k2;
408             char_two->p.ppBasis->k3 = (long)k3;
409         } else {                /* field_type == NID_X9_62_onBasis */
410
411             /* for ONB the parameters are (asn1) NULL */
412             char_two->p.onBasis = ASN1_NULL_new();
413             if (char_two->p.onBasis == NULL) {
414                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
415                 goto err;
416             }
417         }
418     }
419 #endif
420     else {
421         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_UNSUPPORTED_FIELD);
422         goto err;
423     }
424
425     ok = 1;
426
427  err:
428     BN_free(tmp);
429     return (ok);
430 }
431
432 static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
433 {
434     int ok = 0, nid;
435     BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
436     unsigned char *buffer_1 = NULL, *buffer_2 = NULL,
437         *a_buf = NULL, *b_buf = NULL;
438     size_t len_1, len_2;
439     unsigned char char_zero = 0;
440
441     if (!group || !curve || !curve->a || !curve->b)
442         return 0;
443
444     if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
445         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
446         goto err;
447     }
448
449     nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
450
451     /* get a and b */
452     if (nid == NID_X9_62_prime_field) {
453         if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
454             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
455             goto err;
456         }
457     }
458 #ifndef OPENSSL_NO_EC2M
459     else {                      /* nid == NID_X9_62_characteristic_two_field */
460
461         if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
462             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
463             goto err;
464         }
465     }
466 #endif
467     len_1 = (size_t)BN_num_bytes(tmp_1);
468     len_2 = (size_t)BN_num_bytes(tmp_2);
469
470     if (len_1 == 0) {
471         /* len_1 == 0 => a == 0 */
472         a_buf = &char_zero;
473         len_1 = 1;
474     } else {
475         if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL) {
476             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
477             goto err;
478         }
479         if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
480             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
481             goto err;
482         }
483         a_buf = buffer_1;
484     }
485
486     if (len_2 == 0) {
487         /* len_2 == 0 => b == 0 */
488         b_buf = &char_zero;
489         len_2 = 1;
490     } else {
491         if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL) {
492             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
493             goto err;
494         }
495         if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
496             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
497             goto err;
498         }
499         b_buf = buffer_2;
500     }
501
502     /* set a and b */
503     if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
504         !ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) {
505         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
506         goto err;
507     }
508
509     /* set the seed (optional) */
510     if (group->seed) {
511         if (!curve->seed)
512             if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
513                 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
514                 goto err;
515             }
516         curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
517         curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
518         if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
519                                  (int)group->seed_len)) {
520             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
521             goto err;
522         }
523     } else {
524         ASN1_BIT_STRING_free(curve->seed);
525         curve->seed = NULL;
526     }
527
528     ok = 1;
529
530  err:
531     OPENSSL_free(buffer_1);
532     OPENSSL_free(buffer_2);
533     BN_free(tmp_1);
534     BN_free(tmp_2);
535     return (ok);
536 }
537
538 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
539                                               ECPARAMETERS *param)
540 {
541     size_t len = 0;
542     ECPARAMETERS *ret = NULL;
543     const BIGNUM *tmp;
544     unsigned char *buffer = NULL;
545     const EC_POINT *point = NULL;
546     point_conversion_form_t form;
547
548     if (param == NULL) {
549         if ((ret = ECPARAMETERS_new()) == NULL) {
550             ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
551             goto err;
552         }
553     } else
554         ret = param;
555
556     /* set the version (always one) */
557     ret->version = (long)0x1;
558
559     /* set the fieldID */
560     if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
561         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
562         goto err;
563     }
564
565     /* set the curve */
566     if (!ec_asn1_group2curve(group, ret->curve)) {
567         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
568         goto err;
569     }
570
571     /* set the base point */
572     if ((point = EC_GROUP_get0_generator(group)) == NULL) {
573         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
574         goto err;
575     }
576
577     form = EC_GROUP_get_point_conversion_form(group);
578
579     len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
580     if (len == 0) {
581         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
582         goto err;
583     }
584     if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
585         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
586         goto err;
587     }
588     if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) {
589         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
590         goto err;
591     }
592
593     /* set the order */
594     tmp = EC_GROUP_get0_order(group);
595     if (tmp == NULL) {
596         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
597         goto err;
598     }
599     ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
600     if (ret->order == NULL) {
601         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
602         goto err;
603     }
604
605     /* set the cofactor (optional) */
606     tmp = EC_GROUP_get0_cofactor(group);
607     if (tmp != NULL) {
608         ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
609         if (ret->cofactor == NULL) {
610             ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
611             goto err;
612         }
613     }
614
615     return ret;
616
617  err:
618     if (!param)
619         ECPARAMETERS_free(ret);
620     OPENSSL_free(buffer);
621     return NULL;
622 }
623
624 ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
625                                            ECPKPARAMETERS *params)
626 {
627     int ok = 1, tmp;
628     ECPKPARAMETERS *ret = params;
629
630     if (ret == NULL) {
631         if ((ret = ECPKPARAMETERS_new()) == NULL) {
632             ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS, ERR_R_MALLOC_FAILURE);
633             return NULL;
634         }
635     } else {
636         if (ret->type == 0)
637             ASN1_OBJECT_free(ret->value.named_curve);
638         else if (ret->type == 1 && ret->value.parameters)
639             ECPARAMETERS_free(ret->value.parameters);
640     }
641
642     if (EC_GROUP_get_asn1_flag(group)) {
643         /*
644          * use the asn1 OID to describe the the elliptic curve parameters
645          */
646         tmp = EC_GROUP_get_curve_name(group);
647         if (tmp) {
648             ret->type = 0;
649             if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
650                 ok = 0;
651         } else
652             /* we don't know the nid => ERROR */
653             ok = 0;
654     } else {
655         /* use the ECPARAMETERS structure */
656         ret->type = 1;
657         if ((ret->value.parameters =
658              ec_asn1_group2parameters(group, NULL)) == NULL)
659             ok = 0;
660     }
661
662     if (!ok) {
663         ECPKPARAMETERS_free(ret);
664         return NULL;
665     }
666     return ret;
667 }
668
669 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
670 {
671     int ok = 0, tmp;
672     EC_GROUP *ret = NULL;
673     BIGNUM *p = NULL, *a = NULL, *b = NULL;
674     EC_POINT *point = NULL;
675     long field_bits;
676
677     if (!params->fieldID || !params->fieldID->fieldType ||
678         !params->fieldID->p.ptr) {
679         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
680         goto err;
681     }
682
683     /* now extract the curve parameters a and b */
684     if (!params->curve || !params->curve->a ||
685         !params->curve->a->data || !params->curve->b ||
686         !params->curve->b->data) {
687         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
688         goto err;
689     }
690     a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
691     if (a == NULL) {
692         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
693         goto err;
694     }
695     b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
696     if (b == NULL) {
697         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
698         goto err;
699     }
700
701     /* get the field parameters */
702     tmp = OBJ_obj2nid(params->fieldID->fieldType);
703     if (tmp == NID_X9_62_characteristic_two_field)
704 #ifdef OPENSSL_NO_EC2M
705     {
706         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED);
707         goto err;
708     }
709 #else
710     {
711         X9_62_CHARACTERISTIC_TWO *char_two;
712
713         char_two = params->fieldID->p.char_two;
714
715         field_bits = char_two->m;
716         if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
717             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
718             goto err;
719         }
720
721         if ((p = BN_new()) == NULL) {
722             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
723             goto err;
724         }
725
726         /* get the base type */
727         tmp = OBJ_obj2nid(char_two->type);
728
729         if (tmp == NID_X9_62_tpBasis) {
730             long tmp_long;
731
732             if (!char_two->p.tpBasis) {
733                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
734                 goto err;
735             }
736
737             tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
738
739             if (!(char_two->m > tmp_long && tmp_long > 0)) {
740                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
741                       EC_R_INVALID_TRINOMIAL_BASIS);
742                 goto err;
743             }
744
745             /* create the polynomial */
746             if (!BN_set_bit(p, (int)char_two->m))
747                 goto err;
748             if (!BN_set_bit(p, (int)tmp_long))
749                 goto err;
750             if (!BN_set_bit(p, 0))
751                 goto err;
752         } else if (tmp == NID_X9_62_ppBasis) {
753             X9_62_PENTANOMIAL *penta;
754
755             penta = char_two->p.ppBasis;
756             if (!penta) {
757                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
758                 goto err;
759             }
760
761             if (!
762                 (char_two->m > penta->k3 && penta->k3 > penta->k2
763                  && penta->k2 > penta->k1 && penta->k1 > 0)) {
764                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
765                       EC_R_INVALID_PENTANOMIAL_BASIS);
766                 goto err;
767             }
768
769             /* create the polynomial */
770             if (!BN_set_bit(p, (int)char_two->m))
771                 goto err;
772             if (!BN_set_bit(p, (int)penta->k1))
773                 goto err;
774             if (!BN_set_bit(p, (int)penta->k2))
775                 goto err;
776             if (!BN_set_bit(p, (int)penta->k3))
777                 goto err;
778             if (!BN_set_bit(p, 0))
779                 goto err;
780         } else if (tmp == NID_X9_62_onBasis) {
781             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
782             goto err;
783         } else {                /* error */
784
785             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
786             goto err;
787         }
788
789         /* create the EC_GROUP structure */
790         ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
791     }
792 #endif
793     else if (tmp == NID_X9_62_prime_field) {
794         /* we have a curve over a prime field */
795         /* extract the prime number */
796         if (!params->fieldID->p.prime) {
797             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
798             goto err;
799         }
800         p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
801         if (p == NULL) {
802             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
803             goto err;
804         }
805
806         if (BN_is_negative(p) || BN_is_zero(p)) {
807             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
808             goto err;
809         }
810
811         field_bits = BN_num_bits(p);
812         if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
813             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
814             goto err;
815         }
816
817         /* create the EC_GROUP structure */
818         ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
819     } else {
820         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
821         goto err;
822     }
823
824     if (ret == NULL) {
825         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
826         goto err;
827     }
828
829     /* extract seed (optional) */
830     if (params->curve->seed != NULL) {
831         OPENSSL_free(ret->seed);
832         if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
833             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
834             goto err;
835         }
836         memcpy(ret->seed, params->curve->seed->data,
837                params->curve->seed->length);
838         ret->seed_len = params->curve->seed->length;
839     }
840
841     if (!params->order || !params->base || !params->base->data) {
842         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
843         goto err;
844     }
845
846     if ((point = EC_POINT_new(ret)) == NULL)
847         goto err;
848
849     /* set the point conversion form */
850     EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
851                                        (params->base->data[0] & ~0x01));
852
853     /* extract the ec point */
854     if (!EC_POINT_oct2point(ret, point, params->base->data,
855                             params->base->length, NULL)) {
856         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
857         goto err;
858     }
859
860     /* extract the order */
861     if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
862         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
863         goto err;
864     }
865     if (BN_is_negative(a) || BN_is_zero(a)) {
866         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
867         goto err;
868     }
869     if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
870         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
871         goto err;
872     }
873
874     /* extract the cofactor (optional) */
875     if (params->cofactor == NULL) {
876         BN_free(b);
877         b = NULL;
878     } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
879         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
880         goto err;
881     }
882     /* set the generator, order and cofactor (if present) */
883     if (!EC_GROUP_set_generator(ret, point, a, b)) {
884         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
885         goto err;
886     }
887
888     ok = 1;
889
890  err:
891     if (!ok) {
892         EC_GROUP_clear_free(ret);
893         ret = NULL;
894     }
895
896     BN_free(p);
897     BN_free(a);
898     BN_free(b);
899     EC_POINT_free(point);
900     return (ret);
901 }
902
903 EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)
904 {
905     EC_GROUP *ret = NULL;
906     int tmp = 0;
907
908     if (params == NULL) {
909         ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS);
910         return NULL;
911     }
912
913     if (params->type == 0) {    /* the curve is given by an OID */
914         tmp = OBJ_obj2nid(params->value.named_curve);
915         if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
916             ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
917                   EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
918             return NULL;
919         }
920         EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
921     } else if (params->type == 1) { /* the parameters are given by a
922                                      * ECPARAMETERS structure */
923         ret = ec_asn1_parameters2group(params->value.parameters);
924         if (!ret) {
925             ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
926             return NULL;
927         }
928         EC_GROUP_set_asn1_flag(ret, 0x0);
929     } else if (params->type == 2) { /* implicitlyCA */
930         return NULL;
931     } else {
932         ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);
933         return NULL;
934     }
935
936     return ret;
937 }
938
939 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
940
941 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
942 {
943     EC_GROUP *group = NULL;
944     ECPKPARAMETERS *params = NULL;
945     const unsigned char *p = *in;
946
947     if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
948         ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
949         ECPKPARAMETERS_free(params);
950         return NULL;
951     }
952
953     if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
954         ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
955         ECPKPARAMETERS_free(params);
956         return NULL;
957     }
958
959     if (a) {
960         EC_GROUP_clear_free(*a);
961         *a = group;
962     }
963
964     ECPKPARAMETERS_free(params);
965     *in = p;
966     return (group);
967 }
968
969 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
970 {
971     int ret = 0;
972     ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
973     if (tmp == NULL) {
974         ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
975         return 0;
976     }
977     if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
978         ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
979         ECPKPARAMETERS_free(tmp);
980         return 0;
981     }
982     ECPKPARAMETERS_free(tmp);
983     return (ret);
984 }
985
986 /* some EC_KEY functions */
987
988 EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
989 {
990     EC_KEY *ret = NULL;
991     EC_PRIVATEKEY *priv_key = NULL;
992     const unsigned char *p = *in;
993
994     if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
995         ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
996         return NULL;
997     }
998
999     if (a == NULL || *a == NULL) {
1000         if ((ret = EC_KEY_new()) == NULL) {
1001             ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1002             goto err;
1003         }
1004     } else
1005         ret = *a;
1006
1007     if (priv_key->parameters) {
1008         EC_GROUP_clear_free(ret->group);
1009         ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
1010     }
1011
1012     if (ret->group == NULL) {
1013         ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1014         goto err;
1015     }
1016
1017     ret->version = priv_key->version;
1018
1019     if (priv_key->privateKey) {
1020         ASN1_OCTET_STRING *pkey = priv_key->privateKey;
1021         if (EC_KEY_oct2priv(ret, ASN1_STRING_data(pkey),
1022                             ASN1_STRING_length(pkey)) == 0)
1023             goto err;
1024     } else {
1025         ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
1026         goto err;
1027     }
1028
1029     EC_POINT_clear_free(ret->pub_key);
1030     ret->pub_key = EC_POINT_new(ret->group);
1031     if (ret->pub_key == NULL) {
1032         ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1033         goto err;
1034     }
1035
1036     if (priv_key->publicKey) {
1037         const unsigned char *pub_oct;
1038         int pub_oct_len;
1039
1040         pub_oct = ASN1_STRING_data(priv_key->publicKey);
1041         pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
1042         if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) {
1043             ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1044             goto err;
1045         }
1046     } else {
1047         if (ret->group->meth->keygenpub != NULL) {
1048             if (ret->group->meth->keygenpub(ret) == 0)
1049                 goto err;
1050         } else if (!EC_POINT_mul(ret->group, ret->pub_key, ret->priv_key, NULL,
1051                                  NULL, NULL)) {
1052             goto err;
1053         }
1054         /* Remember the original private-key-only encoding. */
1055         ret->enc_flag |= EC_PKEY_NO_PUBKEY;
1056     }
1057
1058     if (a)
1059         *a = ret;
1060     EC_PRIVATEKEY_free(priv_key);
1061     *in = p;
1062     return (ret);
1063
1064  err:
1065     if (a == NULL || *a != ret)
1066         EC_KEY_free(ret);
1067     EC_PRIVATEKEY_free(priv_key);
1068     return NULL;
1069 }
1070
1071 int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
1072 {
1073     int ret = 0, ok = 0;
1074     unsigned char *priv= NULL, *pub= NULL;
1075     size_t privlen = 0, publen = 0;
1076
1077     EC_PRIVATEKEY *priv_key = NULL;
1078
1079     if (a == NULL || a->group == NULL ||
1080         (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
1081         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
1082         goto err;
1083     }
1084
1085     if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1086         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1087         goto err;
1088     }
1089
1090     priv_key->version = a->version;
1091
1092     privlen = EC_KEY_priv2buf(a, &priv);
1093
1094     if (privlen == 0) {
1095         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1096         goto err;
1097     }
1098
1099     ASN1_STRING_set0(priv_key->privateKey, priv, privlen);
1100     priv = NULL;
1101
1102     if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1103         if ((priv_key->parameters =
1104              ec_asn1_group2pkparameters(a->group,
1105                                         priv_key->parameters)) == NULL) {
1106             ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1107             goto err;
1108         }
1109     }
1110
1111     if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
1112         priv_key->publicKey = ASN1_BIT_STRING_new();
1113         if (priv_key->publicKey == NULL) {
1114             ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1115             goto err;
1116         }
1117
1118         publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL);
1119
1120         if (publen == 0) {
1121             ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1122             goto err;
1123         }
1124
1125         priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1126         priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1127         ASN1_STRING_set0(priv_key->publicKey, pub, publen);
1128         pub = NULL;
1129     }
1130
1131     if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1132         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1133         goto err;
1134     }
1135     ok = 1;
1136  err:
1137     OPENSSL_clear_free(priv, privlen);
1138     OPENSSL_free(pub);
1139     EC_PRIVATEKEY_free(priv_key);
1140     return (ok ? ret : 0);
1141 }
1142
1143 int i2d_ECParameters(EC_KEY *a, unsigned char **out)
1144 {
1145     if (a == NULL) {
1146         ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1147         return 0;
1148     }
1149     return i2d_ECPKParameters(a->group, out);
1150 }
1151
1152 EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
1153 {
1154     EC_KEY *ret;
1155
1156     if (in == NULL || *in == NULL) {
1157         ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1158         return NULL;
1159     }
1160
1161     if (a == NULL || *a == NULL) {
1162         if ((ret = EC_KEY_new()) == NULL) {
1163             ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1164             return NULL;
1165         }
1166     } else
1167         ret = *a;
1168
1169     if (!d2i_ECPKParameters(&ret->group, in, len)) {
1170         ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1171         if (a == NULL || *a != ret)
1172              EC_KEY_free(ret);
1173         return NULL;
1174     }
1175
1176     if (a)
1177         *a = ret;
1178
1179     return ret;
1180 }
1181
1182 EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
1183 {
1184     EC_KEY *ret = NULL;
1185
1186     if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1187         /*
1188          * sorry, but a EC_GROUP-structure is necessary to set the public key
1189          */
1190         ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1191         return 0;
1192     }
1193     ret = *a;
1194     if (!EC_KEY_oct2key(ret, *in, len, NULL)) {
1195         ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1196         return 0;
1197     }
1198     *in += len;
1199     return ret;
1200 }
1201
1202 int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
1203 {
1204     size_t buf_len = 0;
1205     int new_buffer = 0;
1206
1207     if (a == NULL) {
1208         ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1209         return 0;
1210     }
1211
1212     buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1213                                  a->conv_form, NULL, 0, NULL);
1214
1215     if (out == NULL || buf_len == 0)
1216         /* out == NULL => just return the length of the octet string */
1217         return buf_len;
1218
1219     if (*out == NULL) {
1220         if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1221             ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1222             return 0;
1223         }
1224         new_buffer = 1;
1225     }
1226     if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1227                             *out, buf_len, NULL)) {
1228         ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
1229         if (new_buffer) {
1230             OPENSSL_free(*out);
1231             *out = NULL;
1232         }
1233         return 0;
1234     }
1235     if (!new_buffer)
1236         *out += buf_len;
1237     return buf_len;
1238 }
1239
1240 ASN1_SEQUENCE(ECDSA_SIG) = {
1241         ASN1_SIMPLE(ECDSA_SIG, r, CBIGNUM),
1242         ASN1_SIMPLE(ECDSA_SIG, s, CBIGNUM)
1243 } static_ASN1_SEQUENCE_END(ECDSA_SIG)
1244
1245 DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG)
1246 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG, ECDSA_SIG)
1247 IMPLEMENT_ASN1_FUNCTIONS_const(ECDSA_SIG)
1248
1249 void ECDSA_SIG_get0(BIGNUM **pr, BIGNUM **ps, ECDSA_SIG *sig)
1250 {
1251     if (pr != NULL)
1252         *pr = sig->r;
1253     if (ps != NULL)
1254         *ps = sig->s;
1255 }
1256
1257 int ECDSA_size(const EC_KEY *r)
1258 {
1259     int ret, i;
1260     ASN1_INTEGER bs;
1261     unsigned char buf[4];
1262     const EC_GROUP *group;
1263
1264     if (r == NULL)
1265         return 0;
1266     group = EC_KEY_get0_group(r);
1267     if (group == NULL)
1268         return 0;
1269
1270     i = EC_GROUP_order_bits(group);
1271     if (i == 0)
1272         return 0;
1273     bs.length = (i + 7) / 8;
1274     bs.data = buf;
1275     bs.type = V_ASN1_INTEGER;
1276     /* If the top bit is set the asn1 encoding is 1 larger. */
1277     buf[0] = 0xff;
1278
1279     i = i2d_ASN1_INTEGER(&bs, NULL);
1280     i += i;                     /* r and s */
1281     ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
1282     return (ret);
1283 }