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