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