The macro IMPLEMENT_ASN1_FUNCTIONS_const already contains an ending ;,
[openssl.git] / crypto / ecdsa / ecs_asn1.c
1 /* crypto/ecdsa/ecs_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 "ecs_locl.h"
58 #include <openssl/asn1.h>
59 #include <openssl/asn1t.h>
60 #include <openssl/objects.h>
61
62 static point_conversion_form_t POINT_CONVERSION_FORM = POINT_CONVERSION_COMPRESSED;
63
64 /* Override the default new methods */
65 static int sig_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it)
66 {
67         if(operation == ASN1_OP_NEW_PRE) {
68                 ECDSA_SIG *sig;
69                 sig = OPENSSL_malloc(sizeof(ECDSA_SIG));
70                 if (sig == NULL)
71                 {
72                         ECDSAerr(ECDSA_F_SIG_CB, ERR_R_MALLOC_FAILURE);
73                         return 0;
74                 }
75                 sig->r = NULL;
76                 sig->s = NULL;
77                 *pval = (ASN1_VALUE *)sig;
78                 return 2;
79         }
80         return 1;
81 }
82
83 ASN1_SEQUENCE_cb(ECDSA_SIG, sig_cb) = {
84         ASN1_SIMPLE(ECDSA_SIG, r, CBIGNUM),
85         ASN1_SIMPLE(ECDSA_SIG, s, CBIGNUM)
86 } ASN1_SEQUENCE_END_cb(ECDSA_SIG, ECDSA_SIG)
87
88 IMPLEMENT_ASN1_FUNCTIONS_const(ECDSA_SIG)
89
90 ASN1_SEQUENCE(X9_62_FIELDID) = {
91         ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
92         ASN1_SIMPLE(X9_62_FIELDID, parameters, ASN1_ANY)
93 } ASN1_SEQUENCE_END(X9_62_FIELDID)
94
95 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_FIELDID)
96
97 ASN1_SEQUENCE(X9_62_CURVE) = {
98         ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
99         ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
100         ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
101 } ASN1_SEQUENCE_END(X9_62_CURVE)
102
103 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_CURVE)
104
105 ASN1_SEQUENCE(X9_62_EC_PARAMETERS) = {
106         ASN1_OPT(X9_62_EC_PARAMETERS, version, ASN1_INTEGER),
107         ASN1_SIMPLE(X9_62_EC_PARAMETERS, fieldID, X9_62_FIELDID),
108         ASN1_SIMPLE(X9_62_EC_PARAMETERS, curve, X9_62_CURVE),
109         ASN1_SIMPLE(X9_62_EC_PARAMETERS, base, ASN1_OCTET_STRING),
110         ASN1_SIMPLE(X9_62_EC_PARAMETERS, order, ASN1_INTEGER),
111         ASN1_SIMPLE(X9_62_EC_PARAMETERS, cofactor, ASN1_INTEGER)
112 } ASN1_SEQUENCE_END(X9_62_EC_PARAMETERS)
113
114 IMPLEMENT_ASN1_FUNCTIONS_const(X9_62_EC_PARAMETERS)
115
116 ASN1_CHOICE(EC_PARAMETERS) = {
117         ASN1_SIMPLE(EC_PARAMETERS, value.named_curve, ASN1_OBJECT),
118         ASN1_SIMPLE(EC_PARAMETERS, value.parameters, X9_62_EC_PARAMETERS),
119         ASN1_SIMPLE(EC_PARAMETERS, value.implicitlyCA, ASN1_NULL)
120 } ASN1_CHOICE_END(EC_PARAMETERS)
121
122 IMPLEMENT_ASN1_FUNCTIONS_const(EC_PARAMETERS)
123              
124 ASN1_SEQUENCE(ECDSAPrivateKey) = {
125         ASN1_SIMPLE(ECDSAPrivateKey, version, LONG),
126         ASN1_SIMPLE(ECDSAPrivateKey, parameters, EC_PARAMETERS),
127         ASN1_SIMPLE(ECDSAPrivateKey, pub_key, ASN1_OCTET_STRING),
128         ASN1_SIMPLE(ECDSAPrivateKey, priv_key, BIGNUM)
129 } ASN1_SEQUENCE_END(ECDSAPrivateKey)
130
131 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(ECDSAPrivateKey, ECDSAPrivateKey, ECDSAPrivateKey)
132 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ECDSAPrivateKey, ECDSAPrivateKey, ecdsaPrivateKey)
133
134 ASN1_SEQUENCE(ecdsa_pub_internal) = {
135         ASN1_SIMPLE(ECDSAPrivateKey, pub_key, ASN1_OCTET_STRING),
136         ASN1_SIMPLE(ECDSAPrivateKey, parameters, EC_PARAMETERS),
137 } ASN1_SEQUENCE_END_name(ECDSAPrivateKey, ecdsa_pub_internal)
138
139 ASN1_CHOICE(ECDSAPublicKey) = {
140         ASN1_SIMPLE(ECDSAPrivateKey, pub_key, ASN1_OCTET_STRING),
141         ASN1_EX_COMBINE(0, 0, ecdsa_pub_internal)
142 } ASN1_CHOICE_END_selector(ECDSAPrivateKey, ECDSAPublicKey, write_params)
143
144 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ECDSAPrivateKey, ECDSAPublicKey, ecdsaPublicKey)
145
146
147 X9_62_FIELDID   *ECDSA_get_X9_62_FIELDID(const ECDSA *ecdsa, X9_62_FIELDID *field)
148 {
149         /* TODO : characteristic two */
150         int     ok=0, reason=ERR_R_ASN1_LIB;
151         X9_62_FIELDID *ret=NULL;
152         BIGNUM  *tmp=NULL;
153         
154         if (!ecdsa || !ecdsa->group)
155                 OPENSSL_ECDSA_ABORT(ECDSA_R_MISSING_PARAMETERS)
156         if (field == NULL)
157         {
158                 if ((ret = X9_62_FIELDID_new()) == NULL) return NULL;
159         }
160         else
161         {       
162                 ret = field;
163                 if (ret->fieldType != NULL)     ASN1_OBJECT_free(ret->fieldType);
164                 if (ret->parameters != NULL)    ASN1_TYPE_free(ret->parameters);
165         }
166         if ((tmp = BN_new()) == NULL) 
167                 OPENSSL_ECDSA_ABORT(ERR_R_BN_LIB)
168         if ((ret->fieldType = OBJ_nid2obj(NID_X9_62_prime_field)) == NULL)
169                 OPENSSL_ECDSA_ABORT(ERR_R_OBJ_LIB)
170         if ((ret->parameters = ASN1_TYPE_new()) == NULL) goto err;
171         ret->parameters->type = V_ASN1_INTEGER;
172         if (!EC_GROUP_get_curve_GFp(ecdsa->group, tmp, NULL, NULL, NULL))
173                 OPENSSL_ECDSA_ABORT(ERR_R_EC_LIB)
174         if ((ret->parameters->value.integer = BN_to_ASN1_INTEGER(tmp, NULL)) == NULL) goto err;
175         ok = 1;
176 err :   if (!ok)
177         {
178                 if (ret && !field) X9_62_FIELDID_free(ret);
179                 ret = NULL;
180                 ECDSAerr(ECDSA_F_ECDSA_GET_X9_62_FIELDID, reason);
181         }
182         if (tmp) BN_free(tmp);
183         return(ret);
184 }
185
186 X9_62_CURVE   *ECDSA_get_X9_62_CURVE(const ECDSA *ecdsa, X9_62_CURVE *curve)
187 {
188         int     ok=0, reason=ERR_R_BN_LIB, len1=0, len2=0;
189         X9_62_CURVE *ret=NULL;
190         BIGNUM      *tmp1=NULL, *tmp2=NULL;
191         unsigned char *buffer=NULL;
192
193         if (!ecdsa || !ecdsa->group)
194                 OPENSSL_ECDSA_ABORT(ECDSA_R_MISSING_PARAMETERS)
195         if ((tmp1 = BN_new()) == NULL || (tmp2 = BN_new()) == NULL) goto err;
196         if (curve == NULL)
197         {
198                 if ((ret = X9_62_CURVE_new()) == NULL)
199                         OPENSSL_ECDSA_ABORT(ECDSA_R_X9_62_CURVE_NEW_FAILURE)
200         }
201         else
202         {
203                 ret = curve;
204                 if (ret->a)     ASN1_OCTET_STRING_free(ret->a);
205                 if (ret->b)     ASN1_OCTET_STRING_free(ret->b);
206                 if (ret->seed)  ASN1_BIT_STRING_free(ret->seed);
207         }
208         if (!EC_GROUP_get_curve_GFp(ecdsa->group, NULL, tmp1, tmp2, NULL))
209                 OPENSSL_ECDSA_ABORT(ERR_R_EC_LIB)
210
211         if ((len1 = BN_num_bytes(tmp1)) == 0)
212                 OPENSSL_ECDSA_ABORT(ECDSA_R_UNEXPECTED_PARAMETER_LENGTH)
213         if ((buffer = OPENSSL_malloc(len1)) == NULL)
214                 OPENSSL_ECDSA_ABORT(ERR_R_MALLOC_FAILURE)
215         if ((len1 = BN_bn2bin(tmp1, buffer)) == 0) goto err;
216         if ((ret->a = M_ASN1_OCTET_STRING_new()) == NULL)
217                 OPENSSL_ECDSA_ABORT(ERR_R_ASN1_LIB)
218         if (!M_ASN1_OCTET_STRING_set(ret->a, buffer, len1))
219                 OPENSSL_ECDSA_ABORT(ERR_R_ASN1_LIB)
220
221         if ((len2 = BN_num_bytes(tmp2)) == 0)
222                 OPENSSL_ECDSA_ABORT(ECDSA_R_UNEXPECTED_PARAMETER_LENGTH)
223         if (len1 < len2)
224         {
225                 OPENSSL_free(buffer);
226                 if ((buffer = OPENSSL_malloc(len2)) == NULL)
227                         OPENSSL_ECDSA_ABORT(ERR_R_MALLOC_FAILURE)
228         }
229         if ((len2 = BN_bn2bin(tmp2, buffer)) == 0) goto err;
230         if ((ret->b = M_ASN1_OCTET_STRING_new()) == NULL)
231                 OPENSSL_ECDSA_ABORT(ERR_R_ASN1_LIB)
232         if (!M_ASN1_OCTET_STRING_set(ret->b, buffer, len2))
233                 OPENSSL_ECDSA_ABORT(ERR_R_ASN1_LIB)
234
235         if (ecdsa->seed)
236         {       
237                 if ((ret->seed = ASN1_BIT_STRING_new()) == NULL) goto err;
238                 if (!ASN1_BIT_STRING_set(ret->seed, ecdsa->seed, (int)ecdsa->seed_len))
239                         OPENSSL_ECDSA_ABORT(ERR_R_ASN1_LIB)
240         }
241         else
242                 ret->seed = NULL;
243
244         ok = 1;
245 err :   if (!ok)
246         {
247                 if (ret && !curve) X9_62_CURVE_free(ret);
248                 ret = NULL;
249                 ECDSAerr(ECDSA_F_ECDSA_GET_X9_62_CURVE, reason);
250         }
251         if (buffer) OPENSSL_free(buffer);
252         if (tmp1)   BN_free(tmp1);
253         if (tmp2)   BN_free(tmp2);
254         return(ret);
255 }
256
257 X9_62_EC_PARAMETERS *ECDSA_get_X9_62_EC_PARAMETERS(const ECDSA *ecdsa, X9_62_EC_PARAMETERS *param)
258 {
259         int     ok=0, reason=ERR_R_ASN1_LIB;
260         size_t  len=0;
261         X9_62_EC_PARAMETERS *ret=NULL;
262         BIGNUM        *tmp=NULL;
263         unsigned char *buffer=NULL;
264         EC_POINT      *point=NULL;
265
266         if (!ecdsa || !ecdsa->group)
267                 OPENSSL_ECDSA_ABORT(ECDSA_R_MISSING_PARAMETERS)
268         if ((tmp = BN_new()) == NULL)
269                 OPENSSL_ECDSA_ABORT(ERR_R_BN_LIB)
270         if (param == NULL)
271         {
272                 if ((ret = X9_62_EC_PARAMETERS_new()) == NULL)
273                         OPENSSL_ECDSA_ABORT(ECDSA_R_X9_62_EC_PARAMETERS_NEW_FAILURE)
274         }
275         else
276                 ret = param;
277         if (ecdsa->version == 1)
278                 ret->version = NULL;
279         else
280         {
281                 if (ret->version == NULL && (ret->version = ASN1_INTEGER_new()) == NULL)
282                         OPENSSL_ECDSA_ABORT(ERR_R_MALLOC_FAILURE)
283                 if (!ASN1_INTEGER_set(ret->version, (long)ecdsa->version)) goto err;
284         }
285         if ((ret->fieldID = ECDSA_get_X9_62_FIELDID(ecdsa, ret->fieldID)) == NULL)
286                 OPENSSL_ECDSA_ABORT(ECDSA_R_ECDSA_GET_X9_62_FIELDID_FAILURE)
287         if ((ret->curve = ECDSA_get_X9_62_CURVE(ecdsa, ret->curve)) == NULL)
288                 OPENSSL_ECDSA_ABORT(ECDSA_R_ECDSA_GET_X9_62_CURVE_FAILURE)
289         if ((point = EC_GROUP_get0_generator(ecdsa->group)) == NULL)
290                 OPENSSL_ECDSA_ABORT(ECDSA_R_CAN_NOT_GET_GENERATOR)
291         if (!(len = EC_POINT_point2oct(ecdsa->group, point, POINT_CONVERSION_COMPRESSED, NULL, len, NULL)))
292                 OPENSSL_ECDSA_ABORT(ECDSA_R_UNEXPECTED_PARAMETER_LENGTH)
293         if ((buffer = OPENSSL_malloc(len)) == NULL)
294                 OPENSSL_ECDSA_ABORT(ERR_R_MALLOC_FAILURE)
295         if (!EC_POINT_point2oct(ecdsa->group, point, POINT_CONVERSION_COMPRESSED, buffer, len, NULL)) 
296                 OPENSSL_ECDSA_ABORT(ERR_R_EC_LIB)
297         if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL)
298                 OPENSSL_ECDSA_ABORT(ERR_R_MALLOC_FAILURE)
299         if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) goto err;
300         if (!EC_GROUP_get_order(ecdsa->group, tmp, NULL))
301                 OPENSSL_ECDSA_ABORT(ERR_R_EC_LIB)
302         if ((ret->order = BN_to_ASN1_INTEGER(tmp, ret->order)) == NULL) goto err;
303         if (!EC_GROUP_get_cofactor(ecdsa->group, tmp, NULL))
304                 OPENSSL_ECDSA_ABORT(ERR_R_EC_LIB)
305         if ((ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor)) == NULL) goto err;
306         ok = 1;
307
308 err :   if(!ok)
309         {
310                 ECDSAerr(ECDSA_F_ECDSA_GET_X9_62_EC_PARAMETERS, reason);
311                 if (ret && !param) X9_62_EC_PARAMETERS_free(ret);
312                 ret = NULL;
313         }
314         if (tmp)    BN_free(tmp);
315         if (buffer) OPENSSL_free(buffer);
316         return(ret);
317 }
318
319 EC_PARAMETERS *ECDSA_get_EC_PARAMETERS(const ECDSA *ecdsa, EC_PARAMETERS *params)
320 {
321         int ok = 1;
322         int tmp = 0;
323         EC_PARAMETERS *ret = params;
324         if (ret == NULL)
325                 if ((ret = EC_PARAMETERS_new()) == NULL)
326                 {
327                         ECDSAerr(ECDSA_F_ECDSA_GET_EC_PARAMETERS, ERR_R_MALLOC_FAILURE);
328                         return NULL;
329                 }
330         if (ecdsa == NULL)
331         {       /* missing parameter */
332                 ECDSAerr(ECDSA_F_ECDSA_GET_EC_PARAMETERS, ECDSA_R_MISSING_PARAMETERS);
333                 EC_PARAMETERS_free(params);
334                 return NULL;
335         }
336         if (ecdsa->parameter_flags & ECDSA_FLAG_NAMED_CURVE)
337         {       /* use a named curve */
338                 tmp = EC_GROUP_get_nid(ecdsa->group);
339                 if (tmp)
340                 {
341                         ret->type = 0;
342                         if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
343                                 ok = 0;
344                 }
345                 else
346                 {
347                         /* use the x9_64 ec_parameters structure */
348                         ret->type = 1;
349                         if ((ret->value.parameters = ECDSA_get_X9_62_EC_PARAMETERS(ecdsa, NULL)) == NULL)
350                                 ok = 0;
351                 }
352         }
353         else if (ecdsa->parameter_flags & ECDSA_FLAG_IMPLICITLYCA)
354         {       /* use implicitlyCA */
355                 ret->type = 2;
356                 if ((ret->value.implicitlyCA = ASN1_NULL_new()) == NULL)
357                         ok = 0;
358         }
359         else
360         {       /* use the x9_64 ec_parameters structure */
361                 ret->type = 1;
362                 if ((ret->value.parameters = ECDSA_get_X9_62_EC_PARAMETERS(ecdsa, NULL)) == NULL)
363                         ok = 0;
364         }
365         if (!ok)
366         {
367                 EC_PARAMETERS_free(ret);
368                 return NULL;
369         }
370                 return ret;
371 }
372
373 ECDSA         *ECDSA_x9_62parameters2ecdsa(const X9_62_EC_PARAMETERS *params, ECDSA *ecdsa)
374 {
375         int       ok=0, reason=ERR_R_EC_LIB, tmp;
376         ECDSA     *ret=NULL;
377         EC_METHOD *meth=NULL;
378         BIGNUM    *tmp_1=NULL, *tmp_2=NULL, *tmp_3=NULL;
379         EC_POINT  *point=NULL;
380
381         if (!params) 
382                 OPENSSL_ECDSA_ABORT(ECDSA_R_MISSING_PARAMETERS)
383         if (ecdsa == NULL)
384         {
385                 if ((ret = ECDSA_new()) == NULL) 
386                         OPENSSL_ECDSA_ABORT(ECDSA_R_ECDSA_NEW_FAILURE)
387         }
388         else
389         {
390                 if (ecdsa->group)       EC_GROUP_free(ecdsa->group);
391                 if (ecdsa->pub_key)     EC_POINT_free(ecdsa->pub_key);
392                 ecdsa->pub_key = NULL;
393                 if (ecdsa->priv_key)    BN_clear_free(ecdsa->priv_key);
394                 ecdsa->priv_key = NULL;
395                 if (ecdsa->seed)        OPENSSL_free(ecdsa->seed);
396                 ecdsa->seed = NULL;
397                 if (ecdsa->kinv)        
398                 {
399                         BN_clear_free(ecdsa->kinv);
400                         ecdsa->kinv = NULL;
401                 }
402                 if (ecdsa->r)
403                 {
404                         BN_clear_free(ecdsa->r);
405                         ecdsa->r = NULL;
406                 }
407                 ret = ecdsa;
408         }
409         /* TODO : characteristic two */
410         if (!params->fieldID || !params->fieldID->fieldType || !params->fieldID->parameters)
411                 OPENSSL_ECDSA_ABORT(ECDSA_R_NO_FIELD_SPECIFIED)
412         tmp = OBJ_obj2nid(params->fieldID->fieldType); 
413         if (tmp == NID_X9_62_characteristic_two_field)
414         {
415                 OPENSSL_ECDSA_ABORT(ECDSA_R_NOT_SUPPORTED)
416         }
417         else if (tmp == NID_X9_62_prime_field)
418         {
419                 /* TODO : optimal method for the curve */
420                 (const EC_METHOD *)meth = EC_GFp_mont_method();
421                 if ((ret->group = EC_GROUP_new(meth)) == NULL) goto err;
422                 if (params->fieldID->parameters->type != V_ASN1_INTEGER)
423                         OPENSSL_ECDSA_ABORT(ECDSA_R_UNEXPECTED_ASN1_TYPE)
424                 if (!params->fieldID->parameters->value.integer)
425                         OPENSSL_ECDSA_ABORT(ECDSA_R_PRIME_MISSING)
426                 if ((tmp_1 = ASN1_INTEGER_to_BN(params->fieldID->parameters->value.integer, NULL)) == NULL)
427                         OPENSSL_ECDSA_ABORT(ERR_R_ASN1_LIB)
428                 if (!params->curve)
429                         OPENSSL_ECDSA_ABORT(ECDSA_R_NO_CURVE_SPECIFIED)
430                 if (!params->curve->a || !params->curve->a->data)
431                         OPENSSL_ECDSA_ABORT(ECDSA_R_NO_CURVE_PARAMETER_A_SPECIFIED)
432                 if ((tmp_2 = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL)) == NULL)
433                         OPENSSL_ECDSA_ABORT(ERR_R_BN_LIB)
434                 if (!params->curve->b || !params->curve->b->data)
435                         OPENSSL_ECDSA_ABORT(ECDSA_R_NO_CURVE_PARAMETER_B_SPECIFIED)
436                 if ((tmp_3 = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL)) == NULL)
437                         OPENSSL_ECDSA_ABORT(ERR_R_BN_LIB)
438                 if (!EC_GROUP_set_curve_GFp(ret->group, tmp_1, tmp_2, tmp_3, NULL)) goto err;
439                 if ((point = EC_POINT_new(ret->group)) == NULL) goto err;
440         }
441         else OPENSSL_ECDSA_ABORT(ECDSA_R_WRONG_FIELD_IDENTIFIER)
442         if (params->curve->seed != NULL && params->curve->seed->data > 0)
443         {
444                 if (ret->seed != NULL)
445                         OPENSSL_free(ret->seed);
446                 if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL)
447                         OPENSSL_ECDSA_ABORT(ERR_R_MALLOC_FAILURE)
448                 memcpy(ret->seed, params->curve->seed->data, params->curve->seed->length);
449                 ret->seed_len = params->curve->seed->length;
450         }
451         if (params->version)
452         {
453                 if ((ret->version = (int)ASN1_INTEGER_get(params->version)) < 0)
454                         OPENSSL_ECDSA_ABORT(ECDSA_R_UNEXPECTED_VERSION_NUMER)
455         }
456         else
457                 ret->version  = 1;
458         if (params->order && params->cofactor && params->base && params->base->data)
459         {
460                 if ((tmp_1 = ASN1_INTEGER_to_BN(params->order, tmp_1)) == NULL)
461                         OPENSSL_ECDSA_ABORT(ERR_R_ASN1_LIB)
462                 if ((tmp_2 = ASN1_INTEGER_to_BN(params->cofactor, tmp_2)) == NULL)
463                         OPENSSL_ECDSA_ABORT(ERR_R_ASN1_LIB)
464                 if (!EC_POINT_oct2point(ret->group, point, params->base->data, 
465                                 params->base->length, NULL)) goto err;
466                 if (!EC_GROUP_set_generator(ret->group, point, tmp_1, tmp_2)) goto err;
467         }
468         ok = 1;
469
470 err:    if (!ok)
471         {
472                 ECDSAerr(ECDSA_F_ECDSA_GET, reason);
473                 if (ret && !ecdsa) ECDSA_free(ret);
474                 ret = NULL;
475         }
476         if (tmp_1)      BN_free(tmp_1);
477         if (tmp_2)      BN_free(tmp_2);
478         if (tmp_3)      BN_free(tmp_3);
479         if (point)      EC_POINT_free(point);
480         return(ret);
481 }
482
483 ECDSA *ECDSA_ecparameters2ecdsa(const EC_PARAMETERS *params, ECDSA *ecdsa)
484 {
485         ECDSA *ret = ecdsa;
486         int tmp = 0;
487         if (ret == NULL)
488                 if ((ret = ECDSA_new()) == NULL)
489                 {
490                         ECDSAerr(ECDSA_F_ECDSA_GET_ECDSA, ERR_R_MALLOC_FAILURE);
491                         return NULL;
492                 }
493         if (params == NULL)
494         {
495                 ECDSAerr(ECDSA_F_ECDSA_GET_ECDSA, ECDSA_R_MISSING_PARAMETERS);
496                 ECDSA_free(ret);
497                 return NULL;
498         }
499         if (params->type == 0)
500         {
501                 if (ret->group)
502                         EC_GROUP_free(ret->group);
503                 tmp = OBJ_obj2nid(params->value.named_curve);
504                 ret->parameter_flags |= ECDSA_FLAG_NAMED_CURVE;
505                 if ((ret->group = EC_GROUP_new_by_name(tmp)) == NULL)
506                 {
507                         ECDSAerr(ECDSA_F_ECDSA_GET_ECDSA, ECDSA_R_EC_GROUP_NID2CURVE_FAILURE);
508                         ECDSA_free(ret);
509                         return NULL;
510                 }
511         }
512         else if (params->type == 1)
513         {
514                 ret = ECDSA_x9_62parameters2ecdsa(params->value.parameters, ret);
515         }
516         else if (params->type == 2)
517         {
518                 if (ret->group)
519                         EC_GROUP_free(ret->group);
520                 ret->group = NULL;
521                 ret->parameter_flags |= ECDSA_FLAG_IMPLICITLYCA;                
522         }
523         else
524         {
525                 ECDSAerr(ECDSA_F_ECDSA_GET_ECDSA, ECDSA_R_UNKNOWN_PARAMETERS_TYPE);
526                 ECDSA_free(ret);
527                 ret = NULL;
528         }
529         return ret;
530 }
531
532 ECDSA   *d2i_ECDSAParameters(ECDSA **a, const unsigned char **in, long len)
533 {
534         ECDSA           *ecdsa = (a && *a)? *a : NULL;
535         EC_PARAMETERS   *params = NULL;
536
537         if ((params = d2i_EC_PARAMETERS(NULL, in, len)) == NULL)
538         {
539                 ECDSAerr(ECDSA_F_D2I_ECDSAPARAMETERS, ECDSA_R_D2I_EC_PARAMETERS_FAILURE);
540                 EC_PARAMETERS_free(params);
541                 return NULL;
542         }
543         if ((ecdsa = ECDSA_ecparameters2ecdsa(params, ecdsa)) == NULL)
544         {
545                 ECDSAerr(ECDSA_F_D2I_ECDSAPARAMETERS, ECDSA_R_ECPARAMETERS2ECDSA_FAILURE);
546                 return NULL; 
547         }
548         EC_PARAMETERS_free(params);
549         return(ecdsa);  
550 }
551
552 int     i2d_ECDSAParameters(ECDSA *a, unsigned char **out)
553 {
554         int             ret=0;
555         EC_PARAMETERS   *tmp = ECDSA_get_EC_PARAMETERS(a, NULL);
556         if (tmp == NULL)
557         {
558                 ECDSAerr(ECDSA_F_I2D_ECDSAPARAMETERS, ECDSA_R_ECDSA_GET_EC_PARAMETERS_FAILURE);
559                 return 0;
560         }
561         if ((ret = i2d_EC_PARAMETERS(tmp, out)) == 0)
562         {
563                 ECDSAerr(ECDSA_F_I2D_ECDSAPARAMETERS, ECDSA_R_ECDSA_R_D2I_EC_PARAMETERS_FAILURE);
564                 EC_PARAMETERS_free(tmp);
565                 return 0;
566         }       
567         EC_PARAMETERS_free(tmp);
568         return(ret);
569 }
570
571 ECDSA   *d2i_ECDSAPrivateKey(ECDSA **a, const unsigned char **in, long len)
572 {
573         int reason=ERR_R_BN_LIB, ok=0;
574         ECDSA *ret=NULL;
575         ECDSAPrivateKey *priv_key=NULL;
576
577         if ((priv_key = ECDSAPrivateKey_new()) == NULL)
578                 OPENSSL_ECDSA_ABORT(ECDSA_R_ECDSAPRIVATEKEY_NEW_FAILURE)
579         if ((priv_key = d2i_ecdsaPrivateKey(&priv_key, in, len)) == NULL)
580                 OPENSSL_ECDSA_ABORT(ECDSA_R_D2I_ECDSA_PRIVATEKEY_FAILURE)
581         if ((ret = ECDSA_ecparameters2ecdsa(priv_key->parameters, NULL)) == NULL)
582                 OPENSSL_ECDSA_ABORT(ECDSA_R_ECDSA_GET_FAILURE)
583         ret->version = priv_key->version;
584         ret->write_params = priv_key->write_params;
585         if (priv_key->priv_key)
586         {
587                 if ((ret->priv_key = BN_dup(priv_key->priv_key)) == NULL)
588                         OPENSSL_ECDSA_ABORT(ERR_R_BN_LIB)
589         }
590         else
591                 OPENSSL_ECDSA_ABORT(ECDSA_R_D2I_ECDSAPRIVATEKEY_MISSING_PRIVATE_KEY)
592         if ((ret->pub_key = EC_POINT_new(ret->group)) == NULL)
593                 OPENSSL_ECDSA_ABORT(ERR_R_EC_LIB)
594         if (!EC_POINT_oct2point(ret->group, ret->pub_key, priv_key->pub_key->data, priv_key->pub_key->length, NULL))
595                 OPENSSL_ECDSA_ABORT(ERR_R_EC_LIB)
596         ok = 1;
597 err :   if (!ok)
598         {
599                 if (ret) ECDSA_free(ret);
600                 ret = NULL;
601                 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, reason);
602         }
603         if (priv_key)   ECDSAPrivateKey_free(priv_key);
604         return(ret);
605 }
606
607 int     i2d_ECDSAPrivateKey(ECDSA *a, unsigned char **out)
608 {
609         int ret=0, ok=0, reason=ERR_R_EC_LIB;
610         unsigned char   *buffer=NULL;
611         size_t          buf_len=0;
612         ECDSAPrivateKey *priv_key=NULL;
613
614         if (a == NULL || a->group == NULL)
615                 OPENSSL_ECDSA_ABORT(ECDSA_R_MISSING_PARAMETERS)
616         if ((priv_key = ECDSAPrivateKey_new()) == NULL)
617                 OPENSSL_ECDSA_ABORT(ECDSA_R_ECDSAPRIVATEKEY_NEW_FAILURE)
618         if ((priv_key->parameters = ECDSA_get_EC_PARAMETERS(a, priv_key->parameters)) == NULL)
619                 OPENSSL_ECDSA_ABORT(ECDSA_R_ECDSA_GET_X9_62_EC_PARAMETERS_FAILURE)
620         priv_key->version      = a->version;
621         if (BN_copy(priv_key->priv_key, a->priv_key) == NULL)
622                 OPENSSL_ECDSA_ABORT(ERR_R_BN_LIB)
623         buf_len = EC_POINT_point2oct(a->group, a->pub_key, POINT_CONVERSION_COMPRESSED, NULL, 0, NULL);
624         if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
625                 OPENSSL_ECDSA_ABORT(ERR_R_MALLOC_FAILURE)
626         if (!EC_POINT_point2oct(a->group, a->pub_key, POINT_CONVERSION_COMPRESSED,
627                                 buffer, buf_len, NULL)) goto err;
628         if (!M_ASN1_OCTET_STRING_set(priv_key->pub_key, buffer, buf_len))
629                 OPENSSL_ECDSA_ABORT(ERR_R_ASN1_LIB)
630         if ((ret = i2d_ecdsaPrivateKey(priv_key, out)) == 0)
631                 OPENSSL_ECDSA_ABORT(ECDSA_R_I2D_ECDSA_PRIVATEKEY)
632         ok=1;
633         
634 err:    if (!ok)
635                 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, reason);
636         if (buffer)   OPENSSL_free(buffer);
637         if (priv_key) ECDSAPrivateKey_free(priv_key);   
638         return(ok?ret:0);
639 }
640
641
642 ECDSA   *d2i_ECDSAPublicKey(ECDSA **a, const unsigned char **in, long len)
643 {
644         int reason=ERR_R_BN_LIB, ok=0, ecdsa_new=1;
645         ECDSA *ret=NULL;
646         ECDSAPrivateKey *priv_key=NULL;
647
648         if (a && *a)
649         {
650                 ecdsa_new = 0;
651                 ret = *a;
652         }
653         else if ((ret = ECDSA_new()) == NULL)
654                 OPENSSL_ECDSA_ABORT(ERR_R_MALLOC_FAILURE); 
655         if ((priv_key = ECDSAPrivateKey_new()) == NULL)
656                 OPENSSL_ECDSA_ABORT(ECDSA_R_ECDSAPRIVATEKEY_NEW_FAILURE)
657         if ((priv_key = d2i_ecdsaPublicKey(&priv_key, in, len)) == NULL)
658                 OPENSSL_ECDSA_ABORT(ECDSA_R_D2I_ECDSA_PRIVATEKEY_FAILURE)
659         if (priv_key->write_params == 0)
660         {
661                 if (ecdsa_new || !ret->group)
662                         OPENSSL_ECDSA_ABORT(ECDSA_R_MISSING_PARAMETERS)
663                 if (ret->pub_key == NULL && (ret->pub_key = EC_POINT_new(ret->group)) == NULL)
664                         OPENSSL_ECDSA_ABORT(ERR_R_MALLOC_FAILURE)
665                 if (!EC_POINT_oct2point(ret->group, ret->pub_key, priv_key->pub_key->data,
666                                         priv_key->pub_key->length, NULL))
667                         OPENSSL_ECDSA_ABORT(ERR_R_EC_LIB)
668         }
669         else if (priv_key->write_params == 1)
670         {
671                 if ((ret = ECDSA_ecparameters2ecdsa(priv_key->parameters, ret)) == NULL)
672                         OPENSSL_ECDSA_ABORT(ECDSA_R_ECDSA_GET_FAILURE)
673                 if (ret->pub_key == NULL && (ret->pub_key = EC_POINT_new(ret->group)) == NULL)
674                         OPENSSL_ECDSA_ABORT(ERR_R_EC_LIB)
675                 if (!EC_POINT_oct2point(ret->group, ret->pub_key, priv_key->pub_key->data, 
676                                 priv_key->pub_key->length, NULL))
677                         OPENSSL_ECDSA_ABORT(ERR_R_EC_LIB)
678         }
679         else    OPENSSL_ECDSA_ABORT(ECDSA_R_UNEXPECTED_PARAMETER)
680         ret->write_params = 1;
681         ok = 1;
682 err :   if (!ok)
683         {
684                 if (ret && ecdsa_new) ECDSA_free(ret);
685                 ret = NULL;
686                 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, reason);
687         }
688         if (priv_key)   ECDSAPrivateKey_free(priv_key);
689         return(ret);
690 }
691
692 int     i2d_ECDSAPublicKey(ECDSA *a, unsigned char **out)
693 {
694         int     ret=0, reason=ERR_R_EC_LIB, ok=0;
695         unsigned char   *buffer=NULL;
696         size_t          buf_len=0;
697         ECDSAPrivateKey *priv_key=NULL;
698
699         if (a == NULL) 
700                 OPENSSL_ECDSA_ABORT(ECDSA_R_MISSING_PARAMETERS)
701         if ((priv_key = ECDSAPrivateKey_new()) == NULL) 
702                 OPENSSL_ECDSA_ABORT(ECDSA_R_ECDSAPRIVATEKEY_NEW_FAILURE)
703         if ((priv_key->parameters = ECDSA_get_EC_PARAMETERS(a, priv_key->parameters)) == NULL)
704                 OPENSSL_ECDSA_ABORT(ECDSA_R_ECDSA_GET_X9_62_EC_PARAMETERS_FAILURE)
705         priv_key->version = a->version;
706         priv_key->write_params = a->write_params;
707         buf_len = EC_POINT_point2oct(a->group, a->pub_key, POINT_CONVERSION_FORM, NULL, 0, NULL);
708         if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL)
709                 OPENSSL_ECDSA_ABORT(ERR_R_MALLOC_FAILURE)
710         if (!EC_POINT_point2oct(a->group, a->pub_key, POINT_CONVERSION_FORM,
711                                 buffer, buf_len, NULL)) goto err;
712         if (!M_ASN1_OCTET_STRING_set(priv_key->pub_key, buffer, buf_len))
713                 OPENSSL_ECDSA_ABORT(ERR_R_ASN1_LIB)
714         if ((ret = i2d_ecdsaPublicKey(priv_key, out)) == 0)
715                 OPENSSL_ECDSA_ABORT(ECDSA_R_I2D_ECDSA_PUBLICKEY)
716         ok = 1;
717
718 err:    if (!ok)
719                 ECDSAerr(ECDSA_F_I2D_ECDSAPUBLICKEY, reason);
720         if (buffer)   OPENSSL_free(buffer);
721         if (priv_key) ECDSAPrivateKey_free(priv_key);
722         return(ok?ret:0);
723 }