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