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