Replace 'ecdsaparam' commandline utility by 'ecparam'
[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 "cryptlib.h"
57 #include "ec_lcl.h"
58 #include <openssl/asn1.h>
59 #include <openssl/asn1t.h>
60 #include <openssl/objects.h>
61
62 /* some structures needed for the asn1 encoding */
63 typedef struct x9_62_fieldid_st {
64         ASN1_OBJECT *fieldType;
65         ASN1_TYPE   *parameters;
66         } X9_62_FIELDID;
67
68 typedef struct x9_62_characteristic_two_st {
69         ASN1_INTEGER *m;
70         ASN1_OBJECT  *basis;
71         ASN1_TYPE    *parameters;
72         } X9_62_CHARACTERISTIC_TWO;
73
74 typedef struct x9_62_pentanomial_st {
75         ASN1_INTEGER k1;
76         ASN1_INTEGER k2;
77         ASN1_INTEGER k3;
78         } X9_62_PENTANOMIAL;
79
80 typedef struct x9_62_curve_st {
81         ASN1_OCTET_STRING *a;
82         ASN1_OCTET_STRING *b;
83         ASN1_BIT_STRING   *seed;
84         } X9_62_CURVE;
85
86 struct ec_parameters_st {
87         ASN1_INTEGER      *version;
88         X9_62_FIELDID     *fieldID;
89         X9_62_CURVE       *curve;
90         ASN1_OCTET_STRING *base;
91         ASN1_INTEGER      *order;
92         ASN1_INTEGER      *cofactor;
93         }/* ECPARAMETERS */;
94
95 struct ecpk_parameters_st {
96         int     type;
97         union {
98                 ASN1_OBJECT  *named_curve;
99                 ECPARAMETERS *parameters;
100                 ASN1_NULL    *implicitlyCA;
101         } value;
102         }/* ECPKPARAMETERS */;
103
104 /* the OpenSSL asn1 definitions */
105
106 ASN1_SEQUENCE(X9_62_FIELDID) = {
107         ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
108         ASN1_SIMPLE(X9_62_FIELDID, parameters, ASN1_ANY)
109 } ASN1_SEQUENCE_END(X9_62_FIELDID)
110
111 DECLARE_ASN1_FUNCTIONS_const(X9_62_FIELDID)
112 DECLARE_ASN1_ENCODE_FUNCTIONS_const(X9_62_FIELDID, X9_62_FIELDID)
113 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_FIELDID)
114
115 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
116         ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, m, ASN1_INTEGER),
117         ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, basis, ASN1_OBJECT),
118         ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, parameters, ASN1_ANY)
119 } ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
120
121 DECLARE_ASN1_FUNCTIONS_const(X9_62_CHARACTERISTIC_TWO)
122 DECLARE_ASN1_ENCODE_FUNCTIONS_const(X9_62_CHARACTERISTIC_TWO, X9_62_CHARACTERISTIC_TWO)
123 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_CHARACTERISTIC_TWO)
124
125 ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
126         ASN1_SIMPLE(X9_62_PENTANOMIAL, k1, ASN1_INTEGER),
127         ASN1_SIMPLE(X9_62_PENTANOMIAL, k2, ASN1_INTEGER),
128         ASN1_SIMPLE(X9_62_PENTANOMIAL, k3, ASN1_INTEGER)
129 } ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
130
131 DECLARE_ASN1_FUNCTIONS_const(X9_62_PENTANOMIAL)
132 DECLARE_ASN1_ENCODE_FUNCTIONS_const(X9_62_PENTANOMIAL, X9_62_PENTANOMIAL)
133 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_PENTANOMIAL)
134
135 ASN1_SEQUENCE(X9_62_CURVE) = {
136         ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
137         ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
138         ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
139 } ASN1_SEQUENCE_END(X9_62_CURVE)
140
141 DECLARE_ASN1_FUNCTIONS_const(X9_62_CURVE)
142 DECLARE_ASN1_ENCODE_FUNCTIONS_const(X9_62_CURVE, X9_62_CURVE)
143 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_CURVE)
144
145 ASN1_SEQUENCE(ECPARAMETERS) = {
146         ASN1_SIMPLE(ECPARAMETERS, version, ASN1_INTEGER),
147         ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
148         ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
149         ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
150         ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
151         ASN1_SIMPLE(ECPARAMETERS, cofactor, ASN1_INTEGER)
152 } ASN1_SEQUENCE_END(ECPARAMETERS)
153
154 DECLARE_ASN1_FUNCTIONS_const(ECPARAMETERS)
155 IMPLEMENT_ASN1_FUNCTIONS_const(ECPARAMETERS)
156
157 ASN1_CHOICE(ECPKPARAMETERS) = {
158         ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
159         ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
160         ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
161 } ASN1_CHOICE_END(ECPKPARAMETERS)
162
163 DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
164 IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
165
166 /* some internal functions */
167
168 static X9_62_FIELDID *ec_asn1_group2field(const EC_GROUP *, X9_62_FIELDID *);
169 static X9_62_CURVE *ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
170 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *); 
171 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *, 
172                                               ECPARAMETERS *);
173
174 static X9_62_FIELDID *ec_asn1_group2field(const EC_GROUP *group, 
175                                           X9_62_FIELDID *field)
176         {
177         int           ok=0, nid;
178         X9_62_FIELDID *ret=NULL;
179         BIGNUM        *tmp=NULL;
180         
181         if (field == NULL)
182                 {
183                 if ((ret = X9_62_FIELDID_new()) == NULL)
184                         {
185                         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
186                         return NULL;
187                         }
188                 }
189         else
190                 {       
191                 ret = field;
192                 if (ret->fieldType != NULL)
193                         ASN1_OBJECT_free(ret->fieldType);
194                 if (ret->parameters != NULL)
195                         ASN1_TYPE_free(ret->parameters);
196                 }
197
198         nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
199
200         if ((ret->fieldType = OBJ_nid2obj(nid)) == NULL)
201                 {
202                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
203                 goto err;
204                 }
205
206         if (nid == NID_X9_62_prime_field)
207                 {
208                 if ((tmp = BN_new()) == NULL) 
209                         {
210                         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
211                         goto err;
212                         }
213                 if ((ret->parameters = ASN1_TYPE_new()) == NULL)
214                         {
215                         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
216                         goto err;
217                         }
218                 ret->parameters->type = V_ASN1_INTEGER;
219                 if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL))
220                         {
221                         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
222                         goto err;
223                         }
224                 ret->parameters->value.integer = BN_to_ASN1_INTEGER(tmp, NULL);
225                 if (ret->parameters->value.integer == NULL)
226                         {
227                         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
228                         goto err;
229                         }
230                 }
231         else
232                 goto err;
233
234         ok = 1;
235
236 err :   if (!ok)
237         {
238                 if (ret && !field)
239                         X9_62_FIELDID_free(ret);
240                 ret = NULL;
241         }
242         if (tmp)
243                 BN_free(tmp);
244         return(ret);
245 }
246
247 static X9_62_CURVE *ec_asn1_group2curve(const EC_GROUP *group, 
248                                         X9_62_CURVE *curve)
249         {
250         int           ok=0, nid;
251         X9_62_CURVE   *ret=NULL;
252         BIGNUM        *tmp_1=NULL,
253                       *tmp_2=NULL;
254         unsigned char *buffer_1=NULL,
255                       *buffer_2=NULL,
256                       *a_buf=NULL,
257                       *b_buf=NULL;
258         size_t        len_1, len_2;
259         unsigned char char_zero = 0;
260
261         if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL)
262                 {
263                 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
264                 goto err;
265                 }
266
267         if (curve == NULL)
268                 {
269                 if ((ret = X9_62_CURVE_new()) == NULL)
270                         {
271                         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
272                         goto err;
273                         }
274                 }
275         else
276                 {
277                 ret = curve;
278                 if (ret->a)
279                         ASN1_OCTET_STRING_free(ret->a);
280                 if (ret->b)
281                         ASN1_OCTET_STRING_free(ret->b);
282                 if (ret->seed)
283                         ASN1_BIT_STRING_free(ret->seed);
284                 }
285
286         nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
287
288         /* get a and b */
289         if (nid == NID_X9_62_prime_field)
290                 {
291                 if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL))
292                         {
293                         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
294                         goto err;
295                         }
296
297                 len_1 = (size_t)BN_num_bytes(tmp_1);
298                 len_2 = (size_t)BN_num_bytes(tmp_2);
299
300                 if (len_1 == 0)
301                         {
302                         /* len_1 == 0 => a == 0 */
303                         a_buf = &char_zero;
304                         len_1 = 1;
305                         }
306                 else
307                         {
308                         if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL)
309                                 {
310                                 ECerr(EC_F_EC_ASN1_GROUP2CURVE,
311                                       ERR_R_MALLOC_FAILURE);
312                                 goto err;
313                                 }
314                         if ( (len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0)
315                                 {
316                                 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
317                                 goto err;
318                                 }
319                         a_buf = buffer_1;
320                         }
321
322                 if (len_2 == 0)
323                         {
324                         /* len_2 == 0 => b == 0 */
325                         b_buf = &char_zero;
326                         len_2 = 1;
327                         }
328                 else
329                         {
330                         if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL)
331                                 {
332                                 ECerr(EC_F_EC_ASN1_GROUP2CURVE,
333                                       ERR_R_MALLOC_FAILURE);
334                                 goto err;
335                                 }
336                         if ( (len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0)
337                                 {
338                                 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
339                                 goto err;
340                                 }
341                         b_buf = buffer_2;
342                         }
343                 }
344         else
345                 goto err;
346
347         /* set a and b */
348         if ((ret->a = M_ASN1_OCTET_STRING_new()) == NULL || 
349             (ret->b = M_ASN1_OCTET_STRING_new()) == NULL )
350                 {
351                 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
352                 goto err;
353                 }
354         if (!M_ASN1_OCTET_STRING_set(ret->a, a_buf, len_1) ||
355             !M_ASN1_OCTET_STRING_set(ret->b, b_buf, len_2))
356                 {
357                 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
358                 goto err;
359                 }
360         
361         /* set the seed (optional) */
362         if (group->seed)
363                 {       
364                 if ((ret->seed = ASN1_BIT_STRING_new()) == NULL) goto err;
365                 if (!ASN1_BIT_STRING_set(ret->seed, group->seed, 
366                                          (int)group->seed_len))
367                         {
368                         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
369                         goto err;
370                         }
371                 }
372         else
373                 ret->seed = NULL;
374
375         ok = 1;
376
377 err :   if (!ok)
378         {
379                 if (ret && !curve)
380                         X9_62_CURVE_free(ret);
381                 ret = NULL;
382         }
383         if (buffer_1)
384                 OPENSSL_free(buffer_1);
385         if (buffer_2)
386                 OPENSSL_free(buffer_2);
387         if (tmp_1)
388                 BN_free(tmp_1);
389         if (tmp_2)
390                 BN_free(tmp_2);
391         return(ret);
392 }
393
394 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
395                                               ECPARAMETERS *param)
396         {
397         int     ok=0;
398         size_t  len=0;
399         ECPARAMETERS   *ret=NULL;
400         BIGNUM         *tmp=NULL;
401         unsigned char  *buffer=NULL;
402         const EC_POINT *point=NULL;
403         point_conversion_form_t form;
404
405         if ((tmp = BN_new()) == NULL)
406                 {
407                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
408                 goto err;
409                 }
410
411         if (param == NULL)
412         {
413                 if ((ret = ECPARAMETERS_new()) == NULL)
414                         {
415                         ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, 
416                               ERR_R_MALLOC_FAILURE);
417                         goto err;
418                         }
419         }
420         else
421                 ret = param;
422
423         /* set the version (always one) */
424         if (ret->version == NULL && !(ret->version = ASN1_INTEGER_new()))
425                 {
426                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
427                 goto err;
428                 }
429         if (!ASN1_INTEGER_set(ret->version, (long)0x1))
430                 {
431                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
432                 goto err;
433                 }
434
435         /* set the fieldID */
436         ret->fieldID = ec_asn1_group2field(group, ret->fieldID);
437         if (ret->fieldID == NULL)
438                 {
439                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
440                 goto err;
441                 }
442
443         /* set the curve */
444         ret->curve = ec_asn1_group2curve(group, ret->curve);
445         if (ret->curve == NULL)
446                 {
447                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
448                 goto err;
449                 }
450
451         /* set the base point */
452         if ((point = EC_GROUP_get0_generator(group)) == NULL)
453                 {
454                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
455                 goto err;
456                 }
457
458         form = EC_GROUP_get_point_conversion_form(group);
459
460         len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
461         if (len == 0)
462                 {
463                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
464                 goto err;
465                 }
466         if ((buffer = OPENSSL_malloc(len)) == NULL)
467                 {
468                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
469                 goto err;
470                 }
471         if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL))
472                 {
473                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
474                 goto err;
475                 }
476         if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL)
477                 {
478                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
479                 goto err;
480                 }
481         if (!ASN1_OCTET_STRING_set(ret->base, buffer, len))
482                 {
483                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
484                 goto err;
485                 }
486
487         /* set the order */
488         if (!EC_GROUP_get_order(group, tmp, NULL))
489                 {
490                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
491                 goto err;
492                 }
493         ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
494         if (ret->order == NULL)
495                 {
496                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
497                 goto err;
498                 }
499
500         /* set the cofactor */
501         if (!EC_GROUP_get_cofactor(group, tmp, NULL))
502                 {
503                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
504                 goto err;
505                 }
506         ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
507         if (ret->cofactor == NULL)
508                 {
509                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
510                 goto err;
511                 }
512
513         ok = 1;
514
515 err :   if(!ok)
516                 {
517                 if (ret && !param)
518                         ECPARAMETERS_free(ret);
519                 ret = NULL;
520                 }
521         if (tmp)
522                 BN_free(tmp);
523         if (buffer)
524                 OPENSSL_free(buffer);
525         return(ret);
526         }
527
528 ECPKPARAMETERS *EC_ASN1_group2pkparameters(const EC_GROUP *group, 
529                                            ECPKPARAMETERS *params)
530         {
531         int            ok = 1, tmp;
532         ECPKPARAMETERS *ret = params;
533
534         if (ret == NULL)
535                 {
536                 if ((ret = ECPKPARAMETERS_new()) == NULL)
537                         {
538                         ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS, 
539                               ERR_R_MALLOC_FAILURE);
540                         return NULL;
541                         }
542                 }
543         else
544                 {
545                 if (ret->type == 0 && ret->value.named_curve)
546                         ASN1_OBJECT_free(ret->value.named_curve);
547                 else if (ret->type == 1 && ret->value.parameters)
548                         ECPARAMETERS_free(ret->value.parameters);
549                 }
550
551         if (EC_GROUP_get_asn1_flag(group))
552                 {
553                 /* use the asn1 OID to describe the
554                  * the elliptic curve parameters
555                  */
556                 tmp = EC_GROUP_get_nid(group);
557                 if (tmp)
558                         {
559                         ret->type = 0;
560                         if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
561                                 ok = 0;
562                         }
563                 else
564                         {
565                         /* we have no nid => use the normal
566                          * ECPARAMETERS structure 
567                          */
568                         ret->type = 1;
569                         if ((ret->value.parameters = ec_asn1_group2parameters(
570                              group, NULL)) == NULL)
571                                 ok = 0;
572                         }
573                 }
574         else
575                 {       
576                 /* use the ECPARAMETERS structure */
577                 ret->type = 1;
578                 if ((ret->value.parameters = ec_asn1_group2parameters(
579                      group, NULL)) == NULL)
580                         ok = 0;
581                 }
582
583         if (!ok)
584                 {
585                 ECPKPARAMETERS_free(ret);
586                 return NULL;
587                 }
588         return ret;
589         }
590
591 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
592         {
593         int       ok=0, tmp;
594         EC_GROUP  *ret=NULL;
595         BIGNUM    *p=NULL, *a=NULL, *b=NULL;
596         EC_POINT  *point=NULL;
597
598         if (!params->fieldID || !params->fieldID->fieldType || 
599             !params->fieldID->parameters)
600                 {
601                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
602                 goto err;
603                 }
604
605         tmp = OBJ_obj2nid(params->fieldID->fieldType);
606
607         if (tmp == NID_X9_62_characteristic_two_field)
608                 {
609                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
610                 goto err;
611                 }
612         else if (tmp == NID_X9_62_prime_field)
613                 {
614                 /* we have a curve over a prime field */
615                 /* extract the prime number */
616                 if (params->fieldID->parameters->type != V_ASN1_INTEGER ||
617                     !params->fieldID->parameters->value.integer)
618                         {
619                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
620                         goto err;
621                         }
622                 p = ASN1_INTEGER_to_BN(params->fieldID->parameters->value.integer, NULL);
623                 if (p == NULL)
624                         {
625                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
626                         goto err;
627                         }
628                 /* now extract the curve parameters a and b */
629                 if (!params->curve || !params->curve->a || 
630                     !params->curve->a->data || !params->curve->b ||
631                     !params->curve->b->data)
632                         {
633                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
634                         goto err;
635                         }
636                 a = BN_bin2bn(params->curve->a->data, 
637                               params->curve->a->length, NULL);
638                 if (a == NULL)
639                         {
640                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
641                         goto err;
642                         }
643                 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
644                 if (b == NULL)
645                         {
646                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
647                         goto err;
648                         }
649                 /* create the EC_GROUP structure */
650 /* TODO */
651                 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
652                 if (ret == NULL)
653                         {
654                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
655                         goto err;
656                         }
657                 /* create the generator */
658                 if ((point = EC_POINT_new(ret)) == NULL) goto err;
659                 }
660         else 
661                 {
662                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_UNKNOWN_FIELD);
663                 goto err;
664                 }
665
666         if (params->curve->seed != NULL)
667                 {
668                 if (ret->seed != NULL)
669                         OPENSSL_free(ret->seed);
670                 if (!(ret->seed = OPENSSL_malloc(params->curve->seed->length)))
671                         {
672                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, 
673                               ERR_R_MALLOC_FAILURE);
674                         goto err;
675                         }
676                 memcpy(ret->seed, params->curve->seed->data, 
677                        params->curve->seed->length);
678                 ret->seed_len = params->curve->seed->length;
679                 }
680
681         if (!params->order || !params->cofactor || !params->base ||
682             !params->base->data)
683                 {
684                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
685                 goto err;
686                 }
687
688
689         a = ASN1_INTEGER_to_BN(params->order, a);
690         b = ASN1_INTEGER_to_BN(params->cofactor, b);
691         if (!a || !b)
692                 {
693                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
694                 goto err;
695                 }
696
697         if (!EC_POINT_oct2point(ret, point, params->base->data, 
698                                 params->base->length, NULL))
699                 {
700                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
701                 goto err;
702                 }
703
704         /* set the point conversion form */
705         EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
706                                 (params->base->data[0] & ~0x01));
707
708         if (!EC_GROUP_set_generator(ret, point, a, b))
709                 {
710                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
711                 goto err;
712                 }
713
714         ok = 1;
715
716 err:    if (!ok)
717                 {
718                 if (ret) 
719                         EC_GROUP_clear_free(ret);
720                 ret = NULL;
721                 }
722
723         if (p)  
724                 BN_free(p);
725         if (a)  
726                 BN_free(a);
727         if (b)  
728                 BN_free(b);
729         if (point)      
730                 EC_POINT_free(point);
731         return(ret);
732 }
733
734 EC_GROUP *EC_ASN1_pkparameters2group(const ECPKPARAMETERS *params)
735         {
736         EC_GROUP *ret=NULL;
737         int      tmp=0;
738
739         if (params == NULL)
740                 {
741                 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, 
742                       EC_R_MISSING_PARAMETERS);
743                 return NULL;
744                 }
745
746         if (params->type == 0)
747                 { /* the curve is given by an OID */
748                 tmp = OBJ_obj2nid(params->value.named_curve);
749                 if ((ret = EC_GROUP_new_by_name(tmp)) == NULL)
750                         {
751                         ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, 
752                               EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
753                         return NULL;
754                         }
755                 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
756                 }
757         else if (params->type == 1)
758                 { /* the parameters are given by a ECPARAMETERS
759                    * structure */
760                 ret = ec_asn1_parameters2group(params->value.parameters);
761                 if (!ret)
762                         {
763                         ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
764                         return NULL;
765                         }
766                 EC_GROUP_set_asn1_flag(ret, 0x0);
767                 }
768         else if (params->type == 2)
769                 { /* implicitlyCA */
770                 return NULL;
771                 }
772         else
773         {
774                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
775                 return NULL;
776         }
777
778         return ret;
779 }
780
781 /* EC_GROUP <-> DER encoding of EC[PK]PARAMETERS */
782
783 EC_GROUP *d2i_ECParameters(EC_GROUP **a, const unsigned char **in, long len)
784         {
785         EC_GROUP        *group  = NULL;
786         ECPARAMETERS    *params = NULL;
787
788         if ((params = d2i_ECPARAMETERS(NULL, in, len)) == NULL)
789                 {
790                 ECerr(EC_F_D2I_ECPARAMETERS, EC_R_D2I_ECPARAMETERS_FAILURE);
791                 ECPARAMETERS_free(params);
792                 return NULL;
793                 }
794         
795         if ((group = ec_asn1_parameters2group(params)) == NULL)
796                 {
797                 ECerr(EC_F_D2I_ECPARAMETERS, EC_R_PARAMETERS2GROUP_FAILURE);
798                 return NULL; 
799                 }
800
801         if (a && *a)
802                 EC_GROUP_clear_free(*a);
803         if (a)
804                 *a = group;
805
806         ECPARAMETERS_free(params);
807         return(group);
808         }
809
810 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
811         {
812         EC_GROUP        *group  = NULL;
813         ECPKPARAMETERS  *params = NULL;
814
815         if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL)
816                 {
817                 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
818                 ECPKPARAMETERS_free(params);
819                 return NULL;
820                 }
821         
822         if ((group = EC_ASN1_pkparameters2group(params)) == NULL)
823                 {
824                 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
825                 return NULL; 
826                 }
827
828         
829         if (a && *a)
830                 EC_GROUP_clear_free(*a);
831         if (a)
832                 *a = group;
833
834         ECPKPARAMETERS_free(params);
835         return(group);
836         }
837
838 int i2d_ECParameters(const EC_GROUP *a, unsigned char **out)
839         {
840         int             ret=0;
841         ECPARAMETERS    *tmp = ec_asn1_group2parameters(a, NULL);
842         if (tmp == NULL)
843                 {
844                 ECerr(EC_F_I2D_ECPARAMETERS, EC_R_GROUP2PARAMETERS_FAILURE);
845                 return 0;
846                 }
847         if ((ret = i2d_ECPARAMETERS(tmp, out)) == 0)
848                 {
849                 ECerr(EC_F_I2D_ECPARAMETERS, EC_R_I2D_EC_PARAMETERS_FAILURE);
850                 ECPARAMETERS_free(tmp);
851                 return 0;
852                 }       
853         ECPARAMETERS_free(tmp);
854         return(ret);
855         }
856
857 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
858         {
859         int             ret=0;
860         ECPKPARAMETERS  *tmp = EC_ASN1_group2pkparameters(a, NULL);
861         if (tmp == NULL)
862                 {
863                 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
864                 return 0;
865                 }
866         if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0)
867                 {
868                 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
869                 ECPKPARAMETERS_free(tmp);
870                 return 0;
871                 }       
872         ECPKPARAMETERS_free(tmp);
873         return(ret);
874         }