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