Support building the distribution .tar file on platforms with limited
[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, i;
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         i = EC_GROUP_get_asn1_flag(group);
459         if (i | OPENSSL_EC_COMPRESSED)
460                 form = POINT_CONVERSION_COMPRESSED;
461         else if (i | OPENSSL_EC_UNCOMPRESSED)
462                 form = POINT_CONVERSION_UNCOMPRESSED;
463         else if (i | OPENSSL_EC_HYBRID)
464                 form = POINT_CONVERSION_HYBRID;
465         else 
466                 goto err;
467
468         len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
469         if (len == 0)
470                 {
471                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
472                 goto err;
473                 }
474         if ((buffer = OPENSSL_malloc(len)) == NULL)
475                 {
476                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
477                 goto err;
478                 }
479         if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL))
480                 {
481                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
482                 goto err;
483                 }
484         if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL)
485                 {
486                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
487                 goto err;
488                 }
489         if (!ASN1_OCTET_STRING_set(ret->base, buffer, len))
490                 {
491                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
492                 goto err;
493                 }
494
495         /* set the order */
496         if (!EC_GROUP_get_order(group, tmp, NULL))
497                 {
498                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
499                 goto err;
500                 }
501         ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
502         if (ret->order == NULL)
503                 {
504                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
505                 goto err;
506                 }
507
508         /* set the cofactor */
509         if (!EC_GROUP_get_cofactor(group, tmp, NULL))
510                 {
511                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
512                 goto err;
513                 }
514         ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
515         if (ret->cofactor == NULL)
516                 {
517                 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
518                 goto err;
519                 }
520
521         ok = 1;
522
523 err :   if(!ok)
524                 {
525                 if (ret && !param)
526                         ECPARAMETERS_free(ret);
527                 ret = NULL;
528                 }
529         if (tmp)
530                 BN_free(tmp);
531         if (buffer)
532                 OPENSSL_free(buffer);
533         return(ret);
534         }
535
536 ECPKPARAMETERS *EC_ASN1_group2pkparameters(const EC_GROUP *group, 
537                                            ECPKPARAMETERS *params)
538         {
539         int            ok = 1, tmp;
540         ECPKPARAMETERS *ret = params;
541
542         if (ret == NULL)
543                 {
544                 if ((ret = ECPKPARAMETERS_new()) == NULL)
545                         {
546                         ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS, 
547                               ERR_R_MALLOC_FAILURE);
548                         return NULL;
549                         }
550                 }
551         else
552                 {
553                 if (ret->type == 0 && ret->value.named_curve)
554                         ASN1_OBJECT_free(ret->value.named_curve);
555                 else if (ret->type == 1 && ret->value.parameters)
556                         ECPARAMETERS_free(ret->value.parameters);
557                 }
558
559         tmp = EC_GROUP_get_asn1_flag(group);
560
561         if (tmp & OPENSSL_EC_NAMED_CURVE)
562                 {
563                 /* use the asn1 OID to describe the
564                  * the elliptic curve parameters
565                  */
566                 tmp = EC_GROUP_get_nid(group);
567                 if (tmp)
568                         {
569                         ret->type = 0;
570                         if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
571                                 ok = 0;
572                         }
573                 else
574                         {
575                         /* we have no nid => use the normal
576                          * ECPARAMETERS structure 
577                          */
578                         ret->type = 1;
579                         if ((ret->value.parameters = ec_asn1_group2parameters(
580                              group, NULL)) == NULL)
581                                 ok = 0;
582                         }
583                 }
584         else if (tmp & OPENSSL_EC_EXPLICIT)
585                 {       
586                 /* use the ECPARAMETERS structure */
587                 ret->type = 1;
588                 if ((ret->value.parameters = ec_asn1_group2parameters(
589                      group, NULL)) == NULL)
590                         ok = 0;
591                 }
592         else
593                 ok = 0;
594
595         if (!ok)
596                 {
597                 ECPKPARAMETERS_free(ret);
598                 return NULL;
599                 }
600         return ret;
601         }
602
603 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
604         {
605         int       ok=0, tmp;
606         EC_GROUP  *ret=NULL;
607         BIGNUM    *p=NULL, *a=NULL, *b=NULL;
608         EC_POINT  *point=NULL;
609
610         if (!params->fieldID || !params->fieldID->fieldType || 
611             !params->fieldID->parameters)
612                 {
613                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
614                 goto err;
615                 }
616
617         tmp = OBJ_obj2nid(params->fieldID->fieldType);
618
619         if (tmp == NID_X9_62_characteristic_two_field)
620                 {
621                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
622                 goto err;
623                 }
624         else if (tmp == NID_X9_62_prime_field)
625                 {
626                 /* we have a curve over a prime field */
627                 /* extract the prime number */
628                 if (params->fieldID->parameters->type != V_ASN1_INTEGER ||
629                     !params->fieldID->parameters->value.integer)
630                         {
631                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
632                         goto err;
633                         }
634                 p = ASN1_INTEGER_to_BN(params->fieldID->parameters->value.integer, NULL);
635                 if (p == NULL)
636                         {
637                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
638                         goto err;
639                         }
640                 /* now extract the curve parameters a and b */
641                 if (!params->curve || !params->curve->a || 
642                     !params->curve->a->data || !params->curve->b ||
643                     !params->curve->b->data)
644                         {
645                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
646                         goto err;
647                         }
648                 a = BN_bin2bn(params->curve->a->data, 
649                               params->curve->a->length, NULL);
650                 if (a == NULL)
651                         {
652                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
653                         goto err;
654                         }
655                 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
656                 if (b == NULL)
657                         {
658                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
659                         goto err;
660                         }
661                 /* create the EC_GROUP structure */
662 /* TODO */
663                 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
664                 if (ret == NULL)
665                         {
666                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
667                         goto err;
668                         }
669                 /* create the generator */
670                 if ((point = EC_POINT_new(ret)) == NULL) goto err;
671                 }
672         else 
673                 {
674                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_UNKNOWN_FIELD);
675                 goto err;
676                 }
677
678         if (params->curve->seed != NULL)
679                 {
680                 if (ret->seed != NULL)
681                         OPENSSL_free(ret->seed);
682                 if (!(ret->seed = OPENSSL_malloc(params->curve->seed->length)))
683                         {
684                         ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, 
685                               ERR_R_MALLOC_FAILURE);
686                         goto err;
687                         }
688                 memcpy(ret->seed, params->curve->seed->data, 
689                        params->curve->seed->length);
690                 ret->seed_len = params->curve->seed->length;
691                 }
692
693         if (!params->order || !params->cofactor || !params->base ||
694             !params->base->data)
695                 {
696                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
697                 goto err;
698                 }
699
700
701         a = ASN1_INTEGER_to_BN(params->order, a);
702         b = ASN1_INTEGER_to_BN(params->cofactor, b);
703         if (!a || !b)
704                 {
705                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
706                 goto err;
707                 }
708
709         if (!EC_POINT_oct2point(ret, point, params->base->data, 
710                                 params->base->length, NULL))
711                 {
712                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
713                 goto err;
714                 }
715         if (!EC_GROUP_set_generator(ret, point, a, b))
716                 {
717                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
718                 goto err;
719                 }
720
721         ok = 1;
722
723 err:    if (!ok)
724                 {
725                 if (ret) 
726                         EC_GROUP_clear_free(ret);
727                 ret = NULL;
728                 }
729
730         if (p)  
731                 BN_free(p);
732         if (a)  
733                 BN_free(a);
734         if (b)  
735                 BN_free(b);
736         if (point)      
737                 EC_POINT_free(point);
738         return(ret);
739 }
740
741 EC_GROUP *EC_ASN1_pkparameters2group(const ECPKPARAMETERS *params)
742         {
743         EC_GROUP *ret=NULL;
744         int      tmp=0;
745
746         if (params == NULL)
747                 {
748                 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, 
749                       EC_R_MISSING_PARAMETERS);
750                 return NULL;
751                 }
752
753         if (params->type == 0)
754                 { /* the curve is given by an OID */
755                 tmp = OBJ_obj2nid(params->value.named_curve);
756                 if ((ret = EC_GROUP_new_by_name(tmp)) == NULL)
757                         {
758                         ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, 
759                               EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
760                         return NULL;
761                         }
762                 tmp = EC_GROUP_get_asn1_flag(ret);
763                 tmp = (tmp & ~0x03) | OPENSSL_EC_NAMED_CURVE;
764                 EC_GROUP_set_asn1_flag(ret, tmp);
765                 }
766         else if (params->type == 1)
767                 { /* the parameters are given by a ECPARAMETERS
768                    * structure */
769                 ret = ec_asn1_parameters2group(params->value.parameters);
770                 if (!ret)
771                         {
772                         ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
773                         return NULL;
774                         }
775                 tmp = EC_GROUP_get_asn1_flag(ret);
776                 tmp = (tmp & ~0x03) | OPENSSL_EC_EXPLICIT;
777                 EC_GROUP_set_asn1_flag(ret, tmp);
778                 }
779         else if (params->type == 2)
780                 { /* implicitlyCA */
781                 return NULL;
782                 }
783         else
784         {
785                 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
786                 return NULL;
787         }
788
789         return ret;
790 }
791
792 /* EC_GROUP <-> DER encoding of EC[PK]PARAMETERS */
793
794 EC_GROUP *d2i_ECParameters(EC_GROUP **a, const unsigned char **in, long len)
795         {
796         EC_GROUP        *group  = NULL;
797         ECPARAMETERS    *params = NULL;
798
799         if ((params = d2i_ECPARAMETERS(NULL, in, len)) == NULL)
800                 {
801                 ECerr(EC_F_D2I_ECPARAMETERS, EC_R_D2I_ECPARAMETERS_FAILURE);
802                 ECPARAMETERS_free(params);
803                 return NULL;
804                 }
805         
806         if ((group = ec_asn1_parameters2group(params)) == NULL)
807                 {
808                 ECerr(EC_F_D2I_ECPARAMETERS, EC_R_PARAMETERS2GROUP_FAILURE);
809                 return NULL; 
810                 }
811
812         if (a && *a)
813                 EC_GROUP_clear_free(*a);
814         if (a)
815                 *a = group;
816
817         ECPARAMETERS_free(params);
818         return(group);
819         }
820
821 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
822         {
823         EC_GROUP        *group  = NULL;
824         ECPKPARAMETERS  *params = NULL;
825
826         if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL)
827                 {
828                 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
829                 ECPKPARAMETERS_free(params);
830                 return NULL;
831                 }
832         
833         if ((group = EC_ASN1_pkparameters2group(params)) == NULL)
834                 {
835                 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
836                 return NULL; 
837                 }
838
839         
840         if (a && *a)
841                 EC_GROUP_clear_free(*a);
842         if (a)
843                 *a = group;
844
845         ECPKPARAMETERS_free(params);
846         return(group);
847         }
848
849 int i2d_ECParameters(const EC_GROUP *a, unsigned char **out)
850         {
851         int             ret=0;
852         ECPARAMETERS    *tmp = ec_asn1_group2parameters(a, NULL);
853         if (tmp == NULL)
854                 {
855                 ECerr(EC_F_I2D_ECPARAMETERS, EC_R_GROUP2PARAMETERS_FAILURE);
856                 return 0;
857                 }
858         if ((ret = i2d_ECPARAMETERS(tmp, out)) == 0)
859                 {
860                 ECerr(EC_F_I2D_ECPARAMETERS, EC_R_I2D_EC_PARAMETERS_FAILURE);
861                 ECPARAMETERS_free(tmp);
862                 return 0;
863                 }       
864         ECPARAMETERS_free(tmp);
865         return(ret);
866         }
867
868 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
869         {
870         int             ret=0;
871         ECPKPARAMETERS  *tmp = EC_ASN1_group2pkparameters(a, NULL);
872         if (tmp == NULL)
873                 {
874                 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
875                 return 0;
876                 }
877         if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0)
878                 {
879                 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
880                 ECPKPARAMETERS_free(tmp);
881                 return 0;
882                 }       
883         ECPKPARAMETERS_free(tmp);
884         return(ret);
885         }