Ensure EC private keys retain leading zeros
[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     if (field->p.other != NULL)
322         ASN1_TYPE_free(field->p.other);
323
324     nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
325     /* set OID for the field */
326     if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
327         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
328         goto err;
329     }
330
331     if (nid == NID_X9_62_prime_field) {
332         if ((tmp = BN_new()) == NULL) {
333             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
334             goto err;
335         }
336         /* the parameters are specified by the prime number p */
337         if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) {
338             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
339             goto err;
340         }
341         /* set the prime number */
342         field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
343         if (field->p.prime == NULL) {
344             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
345             goto err;
346         }
347     } else                      /* nid == NID_X9_62_characteristic_two_field */
348 #ifdef OPENSSL_NO_EC2M
349     {
350         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
351         goto err;
352     }
353 #else
354     {
355         int field_type;
356         X9_62_CHARACTERISTIC_TWO *char_two;
357
358         field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
359         char_two = field->p.char_two;
360
361         if (char_two == NULL) {
362             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
363             goto err;
364         }
365
366         char_two->m = (long)EC_GROUP_get_degree(group);
367
368         field_type = EC_GROUP_get_basis_type(group);
369
370         if (field_type == 0) {
371             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
372             goto err;
373         }
374         /* set base type OID */
375         if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
376             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
377             goto err;
378         }
379
380         if (field_type == NID_X9_62_tpBasis) {
381             unsigned int k;
382
383             if (!EC_GROUP_get_trinomial_basis(group, &k))
384                 goto err;
385
386             char_two->p.tpBasis = ASN1_INTEGER_new();
387             if (!char_two->p.tpBasis) {
388                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
389                 goto err;
390             }
391             if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
392                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
393                 goto err;
394             }
395         } else if (field_type == NID_X9_62_ppBasis) {
396             unsigned int k1, k2, k3;
397
398             if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
399                 goto err;
400
401             char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
402             if (!char_two->p.ppBasis) {
403                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
404                 goto err;
405             }
406
407             /* set k? values */
408             char_two->p.ppBasis->k1 = (long)k1;
409             char_two->p.ppBasis->k2 = (long)k2;
410             char_two->p.ppBasis->k3 = (long)k3;
411         } else {                /* field_type == NID_X9_62_onBasis */
412
413             /* for ONB the parameters are (asn1) NULL */
414             char_two->p.onBasis = ASN1_NULL_new();
415             if (!char_two->p.onBasis) {
416                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
417                 goto err;
418             }
419         }
420     }
421 #endif
422
423     ok = 1;
424
425  err:if (tmp)
426         BN_free(tmp);
427     return (ok);
428 }
429
430 static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
431 {
432     int ok = 0, nid;
433     BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
434     unsigned char *buffer_1 = NULL, *buffer_2 = NULL,
435         *a_buf = NULL, *b_buf = NULL;
436     size_t len_1, len_2;
437     unsigned char char_zero = 0;
438
439     if (!group || !curve || !curve->a || !curve->b)
440         return 0;
441
442     if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
443         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
444         goto err;
445     }
446
447     nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
448
449     /* get a and b */
450     if (nid == NID_X9_62_prime_field) {
451         if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
452             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
453             goto err;
454         }
455     }
456 #ifndef OPENSSL_NO_EC2M
457     else {                      /* nid == NID_X9_62_characteristic_two_field */
458
459         if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
460             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
461             goto err;
462         }
463     }
464 #endif
465     len_1 = (size_t)BN_num_bytes(tmp_1);
466     len_2 = (size_t)BN_num_bytes(tmp_2);
467
468     if (len_1 == 0) {
469         /* len_1 == 0 => a == 0 */
470         a_buf = &char_zero;
471         len_1 = 1;
472     } else {
473         if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL) {
474             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
475             goto err;
476         }
477         if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
478             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
479             goto err;
480         }
481         a_buf = buffer_1;
482     }
483
484     if (len_2 == 0) {
485         /* len_2 == 0 => b == 0 */
486         b_buf = &char_zero;
487         len_2 = 1;
488     } else {
489         if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL) {
490             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
491             goto err;
492         }
493         if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
494             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
495             goto err;
496         }
497         b_buf = buffer_2;
498     }
499
500     /* set a and b */
501     if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
502         !ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) {
503         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
504         goto err;
505     }
506
507     /* set the seed (optional) */
508     if (group->seed) {
509         if (!curve->seed)
510             if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
511                 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
512                 goto err;
513             }
514         curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
515         curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
516         if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
517                                  (int)group->seed_len)) {
518             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
519             goto err;
520         }
521     } else {
522         if (curve->seed) {
523             ASN1_BIT_STRING_free(curve->seed);
524             curve->seed = NULL;
525         }
526     }
527
528     ok = 1;
529
530  err:if (buffer_1)
531         OPENSSL_free(buffer_1);
532     if (buffer_2)
533         OPENSSL_free(buffer_2);
534     if (tmp_1)
535         BN_free(tmp_1);
536     if (tmp_2)
537         BN_free(tmp_2);
538     return (ok);
539 }
540
541 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
542                                               ECPARAMETERS *param)
543 {
544     int ok = 0;
545     size_t len = 0;
546     ECPARAMETERS *ret = NULL;
547     BIGNUM *tmp = NULL;
548     unsigned char *buffer = NULL;
549     const EC_POINT *point = NULL;
550     point_conversion_form_t form;
551
552     if ((tmp = BN_new()) == NULL) {
553         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
554         goto err;
555     }
556
557     if (param == NULL) {
558         if ((ret = ECPARAMETERS_new()) == NULL) {
559             ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
560             goto err;
561         }
562     } else
563         ret = param;
564
565     /* set the version (always one) */
566     ret->version = (long)0x1;
567
568     /* set the fieldID */
569     if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
570         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
571         goto err;
572     }
573
574     /* set the curve */
575     if (!ec_asn1_group2curve(group, ret->curve)) {
576         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
577         goto err;
578     }
579
580     /* set the base point */
581     if ((point = EC_GROUP_get0_generator(group)) == NULL) {
582         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
583         goto err;
584     }
585
586     form = EC_GROUP_get_point_conversion_form(group);
587
588     len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
589     if (len == 0) {
590         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
591         goto err;
592     }
593     if ((buffer = OPENSSL_malloc(len)) == NULL) {
594         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
595         goto err;
596     }
597     if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) {
598         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
599         goto err;
600     }
601     if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
602         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
603         goto err;
604     }
605     if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) {
606         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
607         goto err;
608     }
609
610     /* set the order */
611     if (!EC_GROUP_get_order(group, tmp, NULL)) {
612         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
613         goto err;
614     }
615     ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
616     if (ret->order == NULL) {
617         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
618         goto err;
619     }
620
621     /* set the cofactor (optional) */
622     if (EC_GROUP_get_cofactor(group, tmp, NULL)) {
623         ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
624         if (ret->cofactor == NULL) {
625             ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
626             goto err;
627         }
628     }
629
630     ok = 1;
631
632  err:if (!ok) {
633         if (ret && !param)
634             ECPARAMETERS_free(ret);
635         ret = NULL;
636     }
637     if (tmp)
638         BN_free(tmp);
639     if (buffer)
640         OPENSSL_free(buffer);
641     return (ret);
642 }
643
644 ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
645                                            ECPKPARAMETERS *params)
646 {
647     int ok = 1, tmp;
648     ECPKPARAMETERS *ret = params;
649
650     if (ret == NULL) {
651         if ((ret = ECPKPARAMETERS_new()) == NULL) {
652             ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS, ERR_R_MALLOC_FAILURE);
653             return NULL;
654         }
655     } else {
656         if (ret->type == 0)
657             ASN1_OBJECT_free(ret->value.named_curve);
658         else if (ret->type == 1 && ret->value.parameters)
659             ECPARAMETERS_free(ret->value.parameters);
660     }
661
662     if (EC_GROUP_get_asn1_flag(group)) {
663         /*
664          * use the asn1 OID to describe the the elliptic curve parameters
665          */
666         tmp = EC_GROUP_get_curve_name(group);
667         if (tmp) {
668             ret->type = 0;
669             if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
670                 ok = 0;
671         } else
672             /* we don't kmow the nid => ERROR */
673             ok = 0;
674     } else {
675         /* use the ECPARAMETERS structure */
676         ret->type = 1;
677         if ((ret->value.parameters =
678              ec_asn1_group2parameters(group, NULL)) == NULL)
679             ok = 0;
680     }
681
682     if (!ok) {
683         ECPKPARAMETERS_free(ret);
684         return NULL;
685     }
686     return ret;
687 }
688
689 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
690 {
691     int ok = 0, tmp;
692     EC_GROUP *ret = NULL;
693     BIGNUM *p = NULL, *a = NULL, *b = NULL;
694     EC_POINT *point = NULL;
695     long field_bits;
696
697     if (!params->fieldID || !params->fieldID->fieldType ||
698         !params->fieldID->p.ptr) {
699         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
700         goto err;
701     }
702
703     /* now extract the curve parameters a and b */
704     if (!params->curve || !params->curve->a ||
705         !params->curve->a->data || !params->curve->b ||
706         !params->curve->b->data) {
707         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
708         goto err;
709     }
710     a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
711     if (a == NULL) {
712         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
713         goto err;
714     }
715     b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
716     if (b == NULL) {
717         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
718         goto err;
719     }
720
721     /* get the field parameters */
722     tmp = OBJ_obj2nid(params->fieldID->fieldType);
723     if (tmp == NID_X9_62_characteristic_two_field)
724 #ifdef OPENSSL_NO_EC2M
725     {
726         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED);
727         goto err;
728     }
729 #else
730     {
731         X9_62_CHARACTERISTIC_TWO *char_two;
732
733         char_two = params->fieldID->p.char_two;
734
735         field_bits = char_two->m;
736         if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
737             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
738             goto err;
739         }
740
741         if ((p = BN_new()) == NULL) {
742             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
743             goto err;
744         }
745
746         /* get the base type */
747         tmp = OBJ_obj2nid(char_two->type);
748
749         if (tmp == NID_X9_62_tpBasis) {
750             long tmp_long;
751
752             if (!char_two->p.tpBasis) {
753                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
754                 goto err;
755             }
756
757             tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
758
759             if (!(char_two->m > tmp_long && tmp_long > 0)) {
760                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
761                       EC_R_INVALID_TRINOMIAL_BASIS);
762                 goto err;
763             }
764
765             /* create the polynomial */
766             if (!BN_set_bit(p, (int)char_two->m))
767                 goto err;
768             if (!BN_set_bit(p, (int)tmp_long))
769                 goto err;
770             if (!BN_set_bit(p, 0))
771                 goto err;
772         } else if (tmp == NID_X9_62_ppBasis) {
773             X9_62_PENTANOMIAL *penta;
774
775             penta = char_two->p.ppBasis;
776             if (!penta) {
777                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
778                 goto err;
779             }
780
781             if (!
782                 (char_two->m > penta->k3 && penta->k3 > penta->k2
783                  && penta->k2 > penta->k1 && penta->k1 > 0)) {
784                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
785                       EC_R_INVALID_PENTANOMIAL_BASIS);
786                 goto err;
787             }
788
789             /* create the polynomial */
790             if (!BN_set_bit(p, (int)char_two->m))
791                 goto err;
792             if (!BN_set_bit(p, (int)penta->k1))
793                 goto err;
794             if (!BN_set_bit(p, (int)penta->k2))
795                 goto err;
796             if (!BN_set_bit(p, (int)penta->k3))
797                 goto err;
798             if (!BN_set_bit(p, 0))
799                 goto err;
800         } else if (tmp == NID_X9_62_onBasis) {
801             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
802             goto err;
803         } else {                /* error */
804
805             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
806             goto err;
807         }
808
809         /* create the EC_GROUP structure */
810         ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
811     }
812 #endif
813     else if (tmp == NID_X9_62_prime_field) {
814         /* we have a curve over a prime field */
815         /* extract the prime number */
816         if (!params->fieldID->p.prime) {
817             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
818             goto err;
819         }
820         p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
821         if (p == NULL) {
822             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
823             goto err;
824         }
825
826         if (BN_is_negative(p) || BN_is_zero(p)) {
827             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
828             goto err;
829         }
830
831         field_bits = BN_num_bits(p);
832         if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
833             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
834             goto err;
835         }
836
837         /* create the EC_GROUP structure */
838         ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
839     } else {
840         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
841         goto err;
842     }
843
844     if (ret == NULL) {
845         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
846         goto err;
847     }
848
849     /* extract seed (optional) */
850     if (params->curve->seed != NULL) {
851         if (ret->seed != NULL)
852             OPENSSL_free(ret->seed);
853         if (!(ret->seed = OPENSSL_malloc(params->curve->seed->length))) {
854             ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
855             goto err;
856         }
857         memcpy(ret->seed, params->curve->seed->data,
858                params->curve->seed->length);
859         ret->seed_len = params->curve->seed->length;
860     }
861
862     if (!params->order || !params->base || !params->base->data) {
863         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
864         goto err;
865     }
866
867     if ((point = EC_POINT_new(ret)) == NULL)
868         goto err;
869
870     /* set the point conversion form */
871     EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
872                                        (params->base->data[0] & ~0x01));
873
874     /* extract the ec point */
875     if (!EC_POINT_oct2point(ret, point, params->base->data,
876                             params->base->length, NULL)) {
877         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
878         goto err;
879     }
880
881     /* extract the order */
882     if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
883         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
884         goto err;
885     }
886     if (BN_is_negative(a) || BN_is_zero(a)) {
887         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
888         goto err;
889     }
890     if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
891         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
892         goto err;
893     }
894
895     /* extract the cofactor (optional) */
896     if (params->cofactor == NULL) {
897         if (b) {
898             BN_free(b);
899             b = NULL;
900         }
901     } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
902         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
903         goto err;
904     }
905     /* set the generator, order and cofactor (if present) */
906     if (!EC_GROUP_set_generator(ret, point, a, b)) {
907         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
908         goto err;
909     }
910
911     ok = 1;
912
913  err:
914     if (!ok) {
915         EC_GROUP_clear_free(ret);
916         ret = NULL;
917     }
918
919     if (p)
920         BN_free(p);
921     if (a)
922         BN_free(a);
923     if (b)
924         BN_free(b);
925     EC_POINT_free(point);
926     return (ret);
927 }
928
929 EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)
930 {
931     EC_GROUP *ret = NULL;
932     int tmp = 0;
933
934     if (params == NULL) {
935         ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS);
936         return NULL;
937     }
938
939     if (params->type == 0) {    /* the curve is given by an OID */
940         tmp = OBJ_obj2nid(params->value.named_curve);
941         if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
942             ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
943                   EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
944             return NULL;
945         }
946         EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
947     } else if (params->type == 1) { /* the parameters are given by a
948                                      * ECPARAMETERS structure */
949         ret = ec_asn1_parameters2group(params->value.parameters);
950         if (!ret) {
951             ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
952             return NULL;
953         }
954         EC_GROUP_set_asn1_flag(ret, 0x0);
955     } else if (params->type == 2) { /* implicitlyCA */
956         return NULL;
957     } else {
958         ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);
959         return NULL;
960     }
961
962     return ret;
963 }
964
965 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
966
967 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
968 {
969     EC_GROUP *group = NULL;
970     ECPKPARAMETERS *params = NULL;
971
972     if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) {
973         ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
974         ECPKPARAMETERS_free(params);
975         return NULL;
976     }
977
978     if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
979         ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
980         ECPKPARAMETERS_free(params);
981         return NULL;
982     }
983
984     if (a) {
985         EC_GROUP_clear_free(*a);
986         *a = group;
987     }
988
989     ECPKPARAMETERS_free(params);
990     return (group);
991 }
992
993 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
994 {
995     int ret = 0;
996     ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
997     if (tmp == NULL) {
998         ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
999         return 0;
1000     }
1001     if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
1002         ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
1003         ECPKPARAMETERS_free(tmp);
1004         return 0;
1005     }
1006     ECPKPARAMETERS_free(tmp);
1007     return (ret);
1008 }
1009
1010 /* some EC_KEY functions */
1011
1012 EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
1013 {
1014     int ok = 0;
1015     EC_KEY *ret = NULL;
1016     EC_PRIVATEKEY *priv_key = NULL;
1017
1018     if ((priv_key = d2i_EC_PRIVATEKEY(NULL, in, len)) == NULL) {
1019         ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1020         return NULL;
1021     }
1022
1023     if (a == NULL || *a == NULL) {
1024         if ((ret = EC_KEY_new()) == NULL) {
1025             ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1026             goto err;
1027         }
1028     } else
1029         ret = *a;
1030
1031     if (priv_key->parameters) {
1032         EC_GROUP_clear_free(ret->group);
1033         ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
1034     }
1035
1036     if (ret->group == NULL) {
1037         ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1038         goto err;
1039     }
1040
1041     ret->version = priv_key->version;
1042
1043     if (priv_key->privateKey) {
1044         ret->priv_key = BN_bin2bn(ASN1_STRING_data(priv_key->privateKey),
1045                                   ASN1_STRING_length(priv_key->privateKey),
1046                                   ret->priv_key);
1047         if (ret->priv_key == NULL) {
1048             ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_BN_LIB);
1049             goto err;
1050         }
1051     } else {
1052         ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
1053         goto err;
1054     }
1055
1056     EC_POINT_clear_free(ret->pub_key);
1057     ret->pub_key = EC_POINT_new(ret->group);
1058     if (ret->pub_key == NULL) {
1059         ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1060         goto err;
1061     }
1062
1063     if (priv_key->publicKey) {
1064         const unsigned char *pub_oct;
1065         int pub_oct_len;
1066
1067         pub_oct = ASN1_STRING_data(priv_key->publicKey);
1068         pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
1069         /*
1070          * The first byte - point conversion form - must be present.
1071          */
1072         if (pub_oct_len <= 0) {
1073             ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
1074             goto err;
1075         }
1076         /* Save the point conversion form. */
1077         ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);
1078         if (!EC_POINT_oct2point(ret->group, ret->pub_key,
1079                                 pub_oct, (size_t)(pub_oct_len), NULL)) {
1080             ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1081             goto err;
1082         }
1083     } else {
1084         if (!EC_POINT_mul
1085             (ret->group, ret->pub_key, ret->priv_key, NULL, NULL, NULL)) {
1086             ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1087             goto err;
1088         }
1089         /* Remember the original private-key-only encoding. */
1090         ret->enc_flag |= EC_PKEY_NO_PUBKEY;
1091     }
1092
1093     if (a)
1094         *a = ret;
1095     ok = 1;
1096  err:
1097     if (!ok) {
1098         if (a == NULL || *a != ret)
1099             EC_KEY_free(ret);
1100         ret = NULL;
1101     }
1102
1103     if (priv_key)
1104         EC_PRIVATEKEY_free(priv_key);
1105
1106     return (ret);
1107 }
1108
1109 int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
1110 {
1111     int ret = 0, ok = 0;
1112     unsigned char *buffer = NULL;
1113     size_t buf_len = 0, tmp_len, bn_len;
1114     EC_PRIVATEKEY *priv_key = NULL;
1115
1116     if (a == NULL || a->group == NULL || a->priv_key == NULL ||
1117         (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
1118         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
1119         goto err;
1120     }
1121
1122     if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1123         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1124         goto err;
1125     }
1126
1127     priv_key->version = a->version;
1128
1129     bn_len = (size_t)BN_num_bytes(a->priv_key);
1130
1131     /* Octetstring may need leading zeros if BN is to short */
1132
1133     buf_len = (EC_GROUP_get_degree(a->group) + 7) / 8;
1134
1135     if (bn_len > buf_len) {
1136         ECerr(EC_F_I2D_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
1137         goto err;
1138     }
1139
1140     buffer = OPENSSL_malloc(buf_len);
1141     if (buffer == NULL) {
1142         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1143         goto err;
1144     }
1145
1146     if (!BN_bn2bin(a->priv_key, buffer + buf_len - bn_len)) {
1147         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB);
1148         goto err;
1149     }
1150
1151     if (buf_len - bn_len > 0) {
1152         memset(buffer, 0, buf_len - bn_len);
1153     }
1154
1155     if (!ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len)) {
1156         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1157         goto err;
1158     }
1159
1160     if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1161         if ((priv_key->parameters =
1162              ec_asn1_group2pkparameters(a->group,
1163                                         priv_key->parameters)) == NULL) {
1164             ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1165             goto err;
1166         }
1167     }
1168
1169     if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
1170         priv_key->publicKey = ASN1_BIT_STRING_new();
1171         if (priv_key->publicKey == NULL) {
1172             ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1173             goto err;
1174         }
1175
1176         tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
1177                                      a->conv_form, NULL, 0, NULL);
1178
1179         if (tmp_len > buf_len) {
1180             unsigned char *tmp_buffer = OPENSSL_realloc(buffer, tmp_len);
1181             if (!tmp_buffer) {
1182                 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1183                 goto err;
1184             }
1185             buffer = tmp_buffer;
1186             buf_len = tmp_len;
1187         }
1188
1189         if (!EC_POINT_point2oct(a->group, a->pub_key,
1190                                 a->conv_form, buffer, buf_len, NULL)) {
1191             ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1192             goto err;
1193         }
1194
1195         priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1196         priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1197         if (!ASN1_BIT_STRING_set(priv_key->publicKey, buffer, buf_len)) {
1198             ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1199             goto err;
1200         }
1201     }
1202
1203     if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1204         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1205         goto err;
1206     }
1207     ok = 1;
1208  err:
1209     if (buffer)
1210         OPENSSL_free(buffer);
1211     if (priv_key)
1212         EC_PRIVATEKEY_free(priv_key);
1213     return (ok ? ret : 0);
1214 }
1215
1216 int i2d_ECParameters(EC_KEY *a, unsigned char **out)
1217 {
1218     if (a == NULL) {
1219         ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1220         return 0;
1221     }
1222     return i2d_ECPKParameters(a->group, out);
1223 }
1224
1225 EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
1226 {
1227     EC_KEY *ret;
1228
1229     if (in == NULL || *in == NULL) {
1230         ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1231         return NULL;
1232     }
1233
1234     if (a == NULL || *a == NULL) {
1235         if ((ret = EC_KEY_new()) == NULL) {
1236             ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1237             return NULL;
1238         }
1239     } else
1240         ret = *a;
1241
1242     if (!d2i_ECPKParameters(&ret->group, in, len)) {
1243         ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1244         if (a == NULL || *a != ret)
1245              EC_KEY_free(ret);
1246         return NULL;
1247     }
1248
1249     if (a)
1250         *a = ret;
1251
1252     return ret;
1253 }
1254
1255 EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
1256 {
1257     EC_KEY *ret = NULL;
1258
1259     if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1260         /*
1261          * sorry, but a EC_GROUP-structur is necessary to set the public key
1262          */
1263         ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1264         return 0;
1265     }
1266     ret = *a;
1267     if (ret->pub_key == NULL &&
1268         (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
1269         ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1270         return 0;
1271     }
1272     if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) {
1273         ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1274         return 0;
1275     }
1276     /* save the point conversion form */
1277     ret->conv_form = (point_conversion_form_t) (*in[0] & ~0x01);
1278     *in += len;
1279     return ret;
1280 }
1281
1282 int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
1283 {
1284     size_t buf_len = 0;
1285     int new_buffer = 0;
1286
1287     if (a == NULL) {
1288         ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1289         return 0;
1290     }
1291
1292     buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1293                                  a->conv_form, NULL, 0, NULL);
1294
1295     if (out == NULL || buf_len == 0)
1296         /* out == NULL => just return the length of the octet string */
1297         return buf_len;
1298
1299     if (*out == NULL) {
1300         if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1301             ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1302             return 0;
1303         }
1304         new_buffer = 1;
1305     }
1306     if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1307                             *out, buf_len, NULL)) {
1308         ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
1309         if (new_buffer) {
1310             OPENSSL_free(*out);
1311             *out = NULL;
1312         }
1313         return 0;
1314     }
1315     if (!new_buffer)
1316         *out += buf_len;
1317     return buf_len;
1318 }