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