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