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