57de90d3674787be1c1a1138e68ec6529114e37d
[openssl.git] / crypto / ec / ec_asn1.c
1 /*
2  * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <string.h>
11 #include "ec_lcl.h"
12 #include <openssl/err.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/objects.h>
15 #include "internal/nelem.h"
16 #include "internal/asn1_dsa.h"
17
18 int EC_GROUP_get_basis_type(const EC_GROUP *group)
19 {
20     int i;
21
22     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
23         NID_X9_62_characteristic_two_field)
24         /* everything else is currently not supported */
25         return 0;
26
27     /* Find the last non-zero element of group->poly[] */
28     for (i = 0;
29          i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
30          i++)
31         continue;
32
33     if (i == 4)
34         return NID_X9_62_ppBasis;
35     else if (i == 2)
36         return NID_X9_62_tpBasis;
37     else
38         /* everything else is currently not supported */
39         return 0;
40 }
41
42 #ifndef OPENSSL_NO_EC2M
43 int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
44 {
45     if (group == NULL)
46         return 0;
47
48     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
49         NID_X9_62_characteristic_two_field
50         || !((group->poly[0] != 0) && (group->poly[1] != 0)
51              && (group->poly[2] == 0))) {
52         ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS,
53               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
54         return 0;
55     }
56
57     if (k)
58         *k = group->poly[1];
59
60     return 1;
61 }
62
63 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
64                                    unsigned int *k2, unsigned int *k3)
65 {
66     if (group == NULL)
67         return 0;
68
69     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
70         NID_X9_62_characteristic_two_field
71         || !((group->poly[0] != 0) && (group->poly[1] != 0)
72              && (group->poly[2] != 0) && (group->poly[3] != 0)
73              && (group->poly[4] == 0))) {
74         ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS,
75               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
76         return 0;
77     }
78
79     if (k1)
80         *k1 = group->poly[3];
81     if (k2)
82         *k2 = group->poly[2];
83     if (k3)
84         *k3 = group->poly[1];
85
86     return 1;
87 }
88 #endif
89
90 /* some structures needed for the asn1 encoding */
91 typedef struct x9_62_pentanomial_st {
92     int32_t k1;
93     int32_t k2;
94     int32_t k3;
95 } X9_62_PENTANOMIAL;
96
97 typedef struct x9_62_characteristic_two_st {
98     int32_t m;
99     ASN1_OBJECT *type;
100     union {
101         char *ptr;
102         /* NID_X9_62_onBasis */
103         ASN1_NULL *onBasis;
104         /* NID_X9_62_tpBasis */
105         ASN1_INTEGER *tpBasis;
106         /* NID_X9_62_ppBasis */
107         X9_62_PENTANOMIAL *ppBasis;
108         /* anything else */
109         ASN1_TYPE *other;
110     } p;
111 } X9_62_CHARACTERISTIC_TWO;
112
113 typedef struct x9_62_fieldid_st {
114     ASN1_OBJECT *fieldType;
115     union {
116         char *ptr;
117         /* NID_X9_62_prime_field */
118         ASN1_INTEGER *prime;
119         /* NID_X9_62_characteristic_two_field */
120         X9_62_CHARACTERISTIC_TWO *char_two;
121         /* anything else */
122         ASN1_TYPE *other;
123     } p;
124 } X9_62_FIELDID;
125
126 typedef struct x9_62_curve_st {
127     ASN1_OCTET_STRING *a;
128     ASN1_OCTET_STRING *b;
129     ASN1_BIT_STRING *seed;
130 } X9_62_CURVE;
131
132 struct ec_parameters_st {
133     int32_t version;
134     X9_62_FIELDID *fieldID;
135     X9_62_CURVE *curve;
136     ASN1_OCTET_STRING *base;
137     ASN1_INTEGER *order;
138     ASN1_INTEGER *cofactor;
139 } /* ECPARAMETERS */ ;
140
141 struct ecpk_parameters_st {
142     int type;
143     union {
144         ASN1_OBJECT *named_curve;
145         ECPARAMETERS *parameters;
146         ASN1_NULL *implicitlyCA;
147     } value;
148 } /* ECPKPARAMETERS */ ;
149
150 /* SEC1 ECPrivateKey */
151 typedef struct ec_privatekey_st {
152     int32_t version;
153     ASN1_OCTET_STRING *privateKey;
154     ECPKPARAMETERS *parameters;
155     ASN1_BIT_STRING *publicKey;
156 } EC_PRIVATEKEY;
157
158 /* the OpenSSL ASN.1 definitions */
159 ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
160         ASN1_EMBED(X9_62_PENTANOMIAL, k1, INT32),
161         ASN1_EMBED(X9_62_PENTANOMIAL, k2, INT32),
162         ASN1_EMBED(X9_62_PENTANOMIAL, k3, INT32)
163 } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
164
165 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
166 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
167
168 ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
169
170 ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
171         ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
172         ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
173         ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
174 } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
175
176 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
177         ASN1_EMBED(X9_62_CHARACTERISTIC_TWO, m, INT32),
178         ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
179         ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
180 } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
181
182 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
183 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
184
185 ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
186
187 ASN1_ADB(X9_62_FIELDID) = {
188         ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
189         ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
190 } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
191
192 ASN1_SEQUENCE(X9_62_FIELDID) = {
193         ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
194         ASN1_ADB_OBJECT(X9_62_FIELDID)
195 } static_ASN1_SEQUENCE_END(X9_62_FIELDID)
196
197 ASN1_SEQUENCE(X9_62_CURVE) = {
198         ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
199         ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
200         ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
201 } static_ASN1_SEQUENCE_END(X9_62_CURVE)
202
203 ASN1_SEQUENCE(ECPARAMETERS) = {
204         ASN1_EMBED(ECPARAMETERS, version, INT32),
205         ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
206         ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
207         ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
208         ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
209         ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
210 } ASN1_SEQUENCE_END(ECPARAMETERS)
211
212 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
213 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
214
215 ASN1_CHOICE(ECPKPARAMETERS) = {
216         ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
217         ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
218         ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
219 } ASN1_CHOICE_END(ECPKPARAMETERS)
220
221 DECLARE_ASN1_FUNCTIONS(ECPKPARAMETERS)
222 DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECPKPARAMETERS, ECPKPARAMETERS)
223 IMPLEMENT_ASN1_FUNCTIONS(ECPKPARAMETERS)
224
225 ASN1_SEQUENCE(EC_PRIVATEKEY) = {
226         ASN1_EMBED(EC_PRIVATEKEY, version, INT32),
227         ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
228         ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
229         ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
230 } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY)
231
232 DECLARE_ASN1_FUNCTIONS(EC_PRIVATEKEY)
233 DECLARE_ASN1_ENCODE_FUNCTIONS_name(EC_PRIVATEKEY, EC_PRIVATEKEY)
234 IMPLEMENT_ASN1_FUNCTIONS(EC_PRIVATEKEY)
235
236 /* some declarations of internal function */
237
238 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
239 static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
240 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
241 static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
242
243 /* the function definitions */
244
245 static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
246 {
247     int ok = 0, nid;
248     BIGNUM *tmp = NULL;
249
250     if (group == NULL || field == NULL)
251         return 0;
252
253     /* clear the old values (if necessary) */
254     ASN1_OBJECT_free(field->fieldType);
255     ASN1_TYPE_free(field->p.other);
256
257     nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
258     /* set OID for the field */
259     if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
260         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
261         goto err;
262     }
263
264     if (nid == NID_X9_62_prime_field) {
265         if ((tmp = BN_new()) == NULL) {
266             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
267             goto err;
268         }
269         /* the parameters are specified by the prime number p */
270         if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) {
271             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
272             goto err;
273         }
274         /* set the prime number */
275         field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
276         if (field->p.prime == NULL) {
277             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
278             goto err;
279         }
280     } else if (nid == NID_X9_62_characteristic_two_field)
281 #ifdef OPENSSL_NO_EC2M
282     {
283         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
284         goto err;
285     }
286 #else
287     {
288         int field_type;
289         X9_62_CHARACTERISTIC_TWO *char_two;
290
291         field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
292         char_two = field->p.char_two;
293
294         if (char_two == NULL) {
295             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
296             goto err;
297         }
298
299         char_two->m = (long)EC_GROUP_get_degree(group);
300
301         field_type = EC_GROUP_get_basis_type(group);
302
303         if (field_type == 0) {
304             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
305             goto err;
306         }
307         /* set base type OID */
308         if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
309             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
310             goto err;
311         }
312
313         if (field_type == NID_X9_62_tpBasis) {
314             unsigned int k;
315
316             if (!EC_GROUP_get_trinomial_basis(group, &k))
317                 goto err;
318
319             char_two->p.tpBasis = ASN1_INTEGER_new();
320             if (char_two->p.tpBasis == NULL) {
321                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
322                 goto err;
323             }
324             if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
325                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
326                 goto err;
327             }
328         } else if (field_type == NID_X9_62_ppBasis) {
329             unsigned int k1, k2, k3;
330
331             if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
332                 goto err;
333
334             char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
335             if (char_two->p.ppBasis == NULL) {
336                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
337                 goto err;
338             }
339
340             /* set k? values */
341             char_two->p.ppBasis->k1 = (long)k1;
342             char_two->p.ppBasis->k2 = (long)k2;
343             char_two->p.ppBasis->k3 = (long)k3;
344         } else {                /* field_type == NID_X9_62_onBasis */
345
346             /* for ONB the parameters are (asn1) NULL */
347             char_two->p.onBasis = ASN1_NULL_new();
348             if (char_two->p.onBasis == NULL) {
349                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
350                 goto err;
351             }
352         }
353     }
354 #endif
355     else {
356         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_UNSUPPORTED_FIELD);
357         goto err;
358     }
359
360     ok = 1;
361
362  err:
363     BN_free(tmp);
364     return ok;
365 }
366
367 static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
368 {
369     int ok = 0;
370     BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
371     unsigned char *a_buf = NULL, *b_buf = NULL;
372     size_t len;
373
374     if (!group || !curve || !curve->a || !curve->b)
375         return 0;
376
377     if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
378         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
379         goto err;
380     }
381
382     /* get a and b */
383     if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) {
384         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
385         goto err;
386     }
387
388     /*
389      * Per SEC 1, the curve coefficients must be padded up to size. See C.2's
390      * definition of Curve, C.1's definition of FieldElement, and 2.3.5's
391      * definition of how to encode the field elements.
392      */
393     len = ((size_t)EC_GROUP_get_degree(group) + 7) / 8;
394     if ((a_buf = OPENSSL_malloc(len)) == NULL
395         || (b_buf = OPENSSL_malloc(len)) == NULL) {
396         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
397         goto err;
398     }
399     if (BN_bn2binpad(tmp_1, a_buf, len) < 0
400         || BN_bn2binpad(tmp_2, b_buf, len) < 0) {
401         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
402         goto err;
403     }
404
405     /* set a and b */
406     if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len)
407         || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) {
408         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
409         goto err;
410     }
411
412     /* set the seed (optional) */
413     if (group->seed) {
414         if (!curve->seed)
415             if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
416                 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
417                 goto err;
418             }
419         curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
420         curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
421         if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
422                                  (int)group->seed_len)) {
423             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
424             goto err;
425         }
426     } else {
427         ASN1_BIT_STRING_free(curve->seed);
428         curve->seed = NULL;
429     }
430
431     ok = 1;
432
433  err:
434     OPENSSL_free(a_buf);
435     OPENSSL_free(b_buf);
436     BN_free(tmp_1);
437     BN_free(tmp_2);
438     return ok;
439 }
440
441 ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
442                                                ECPARAMETERS *params)
443 {
444     size_t len = 0;
445     ECPARAMETERS *ret = NULL;
446     const BIGNUM *tmp;
447     unsigned char *buffer = NULL;
448     const EC_POINT *point = NULL;
449     point_conversion_form_t form;
450
451     if (params == NULL) {
452         if ((ret = ECPARAMETERS_new()) == NULL) {
453             ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
454             goto err;
455         }
456     } else
457         ret = params;
458
459     /* set the version (always one) */
460     ret->version = (long)0x1;
461
462     /* set the fieldID */
463     if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
464         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
465         goto err;
466     }
467
468     /* set the curve */
469     if (!ec_asn1_group2curve(group, ret->curve)) {
470         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
471         goto err;
472     }
473
474     /* set the base point */
475     if ((point = EC_GROUP_get0_generator(group)) == NULL) {
476         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, EC_R_UNDEFINED_GENERATOR);
477         goto err;
478     }
479
480     form = EC_GROUP_get_point_conversion_form(group);
481
482     len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
483     if (len == 0) {
484         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
485         goto err;
486     }
487     if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
488         OPENSSL_free(buffer);
489         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
490         goto err;
491     }
492     ASN1_STRING_set0(ret->base, buffer, len);
493
494     /* set the order */
495     tmp = EC_GROUP_get0_order(group);
496     if (tmp == NULL) {
497         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
498         goto err;
499     }
500     ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
501     if (ret->order == NULL) {
502         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
503         goto err;
504     }
505
506     /* set the cofactor (optional) */
507     tmp = EC_GROUP_get0_cofactor(group);
508     if (tmp != NULL) {
509         ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
510         if (ret->cofactor == NULL) {
511             ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
512             goto err;
513         }
514     }
515
516     return ret;
517
518  err:
519     if (params == NULL)
520         ECPARAMETERS_free(ret);
521     return NULL;
522 }
523
524 ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
525                                             ECPKPARAMETERS *params)
526 {
527     int ok = 1, tmp;
528     ECPKPARAMETERS *ret = params;
529
530     if (ret == NULL) {
531         if ((ret = ECPKPARAMETERS_new()) == NULL) {
532             ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, ERR_R_MALLOC_FAILURE);
533             return NULL;
534         }
535     } else {
536         if (ret->type == 0)
537             ASN1_OBJECT_free(ret->value.named_curve);
538         else if (ret->type == 1 && ret->value.parameters)
539             ECPARAMETERS_free(ret->value.parameters);
540     }
541
542     if (EC_GROUP_get_asn1_flag(group)) {
543         /*
544          * use the asn1 OID to describe the elliptic curve parameters
545          */
546         tmp = EC_GROUP_get_curve_name(group);
547         if (tmp) {
548             ret->type = 0;
549             if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
550                 ok = 0;
551         } else
552             /* we don't know the nid => ERROR */
553             ok = 0;
554     } else {
555         /* use the ECPARAMETERS structure */
556         ret->type = 1;
557         if ((ret->value.parameters =
558              EC_GROUP_get_ecparameters(group, NULL)) == NULL)
559             ok = 0;
560     }
561
562     if (!ok) {
563         ECPKPARAMETERS_free(ret);
564         return NULL;
565     }
566     return ret;
567 }
568
569 EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params)
570 {
571     int ok = 0, tmp;
572     EC_GROUP *ret = NULL;
573     BIGNUM *p = NULL, *a = NULL, *b = NULL;
574     EC_POINT *point = NULL;
575     long field_bits;
576
577     if (!params->fieldID || !params->fieldID->fieldType ||
578         !params->fieldID->p.ptr) {
579         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
580         goto err;
581     }
582
583     /*
584      * Now extract the curve parameters a and b. Note that, although SEC 1
585      * specifies the length of their encodings, historical versions of OpenSSL
586      * encoded them incorrectly, so we must accept any length for backwards
587      * compatibility.
588      */
589     if (!params->curve || !params->curve->a ||
590         !params->curve->a->data || !params->curve->b ||
591         !params->curve->b->data) {
592         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
593         goto err;
594     }
595     a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
596     if (a == NULL) {
597         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
598         goto err;
599     }
600     b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
601     if (b == NULL) {
602         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
603         goto err;
604     }
605
606     /* get the field parameters */
607     tmp = OBJ_obj2nid(params->fieldID->fieldType);
608     if (tmp == NID_X9_62_characteristic_two_field)
609 #ifdef OPENSSL_NO_EC2M
610     {
611         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED);
612         goto err;
613     }
614 #else
615     {
616         X9_62_CHARACTERISTIC_TWO *char_two;
617
618         char_two = params->fieldID->p.char_two;
619
620         field_bits = char_two->m;
621         if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
622             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
623             goto err;
624         }
625
626         if ((p = BN_new()) == NULL) {
627             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
628             goto err;
629         }
630
631         /* get the base type */
632         tmp = OBJ_obj2nid(char_two->type);
633
634         if (tmp == NID_X9_62_tpBasis) {
635             long tmp_long;
636
637             if (!char_two->p.tpBasis) {
638                 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
639                 goto err;
640             }
641
642             tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
643
644             if (!(char_two->m > tmp_long && tmp_long > 0)) {
645                 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
646                       EC_R_INVALID_TRINOMIAL_BASIS);
647                 goto err;
648             }
649
650             /* create the polynomial */
651             if (!BN_set_bit(p, (int)char_two->m))
652                 goto err;
653             if (!BN_set_bit(p, (int)tmp_long))
654                 goto err;
655             if (!BN_set_bit(p, 0))
656                 goto err;
657         } else if (tmp == NID_X9_62_ppBasis) {
658             X9_62_PENTANOMIAL *penta;
659
660             penta = char_two->p.ppBasis;
661             if (!penta) {
662                 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
663                 goto err;
664             }
665
666             if (!
667                 (char_two->m > penta->k3 && penta->k3 > penta->k2
668                  && penta->k2 > penta->k1 && penta->k1 > 0)) {
669                 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
670                       EC_R_INVALID_PENTANOMIAL_BASIS);
671                 goto err;
672             }
673
674             /* create the polynomial */
675             if (!BN_set_bit(p, (int)char_two->m))
676                 goto err;
677             if (!BN_set_bit(p, (int)penta->k1))
678                 goto err;
679             if (!BN_set_bit(p, (int)penta->k2))
680                 goto err;
681             if (!BN_set_bit(p, (int)penta->k3))
682                 goto err;
683             if (!BN_set_bit(p, 0))
684                 goto err;
685         } else if (tmp == NID_X9_62_onBasis) {
686             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);
687             goto err;
688         } else {                /* error */
689
690             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
691             goto err;
692         }
693
694         /* create the EC_GROUP structure */
695         ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
696     }
697 #endif
698     else if (tmp == NID_X9_62_prime_field) {
699         /* we have a curve over a prime field */
700         /* extract the prime number */
701         if (!params->fieldID->p.prime) {
702             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
703             goto err;
704         }
705         p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
706         if (p == NULL) {
707             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
708             goto err;
709         }
710
711         if (BN_is_negative(p) || BN_is_zero(p)) {
712             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
713             goto err;
714         }
715
716         field_bits = BN_num_bits(p);
717         if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
718             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
719             goto err;
720         }
721
722         /* create the EC_GROUP structure */
723         ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
724     } else {
725         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
726         goto err;
727     }
728
729     if (ret == NULL) {
730         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
731         goto err;
732     }
733
734     /* extract seed (optional) */
735     if (params->curve->seed != NULL) {
736         OPENSSL_free(ret->seed);
737         if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
738             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
739             goto err;
740         }
741         memcpy(ret->seed, params->curve->seed->data,
742                params->curve->seed->length);
743         ret->seed_len = params->curve->seed->length;
744     }
745
746     if (!params->order || !params->base || !params->base->data) {
747         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
748         goto err;
749     }
750
751     if ((point = EC_POINT_new(ret)) == NULL)
752         goto err;
753
754     /* set the point conversion form */
755     EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
756                                        (params->base->data[0] & ~0x01));
757
758     /* extract the ec point */
759     if (!EC_POINT_oct2point(ret, point, params->base->data,
760                             params->base->length, NULL)) {
761         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
762         goto err;
763     }
764
765     /* extract the order */
766     if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
767         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
768         goto err;
769     }
770     if (BN_is_negative(a) || BN_is_zero(a)) {
771         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
772         goto err;
773     }
774     if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
775         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
776         goto err;
777     }
778
779     /* extract the cofactor (optional) */
780     if (params->cofactor == NULL) {
781         BN_free(b);
782         b = NULL;
783     } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
784         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
785         goto err;
786     }
787     /* set the generator, order and cofactor (if present) */
788     if (!EC_GROUP_set_generator(ret, point, a, b)) {
789         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
790         goto err;
791     }
792
793     ok = 1;
794
795  err:
796     if (!ok) {
797         EC_GROUP_clear_free(ret);
798         ret = NULL;
799     }
800
801     BN_free(p);
802     BN_free(a);
803     BN_free(b);
804     EC_POINT_free(point);
805     return ret;
806 }
807
808 EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params)
809 {
810     EC_GROUP *ret = NULL;
811     int tmp = 0;
812
813     if (params == NULL) {
814         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_MISSING_PARAMETERS);
815         return NULL;
816     }
817
818     if (params->type == 0) {    /* the curve is given by an OID */
819         tmp = OBJ_obj2nid(params->value.named_curve);
820         if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
821             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS,
822                   EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
823             return NULL;
824         }
825         EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
826     } else if (params->type == 1) { /* the parameters are given by a
827                                      * ECPARAMETERS structure */
828         ret = EC_GROUP_new_from_ecparameters(params->value.parameters);
829         if (!ret) {
830             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, ERR_R_EC_LIB);
831             return NULL;
832         }
833         EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
834     } else if (params->type == 2) { /* implicitlyCA */
835         return NULL;
836     } else {
837         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_ASN1_ERROR);
838         return NULL;
839     }
840
841     return ret;
842 }
843
844 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
845
846 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
847 {
848     EC_GROUP *group = NULL;
849     ECPKPARAMETERS *params = NULL;
850     const unsigned char *p = *in;
851
852     if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
853         ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
854         ECPKPARAMETERS_free(params);
855         return NULL;
856     }
857
858     if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) {
859         ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
860         ECPKPARAMETERS_free(params);
861         return NULL;
862     }
863
864     if (a) {
865         EC_GROUP_clear_free(*a);
866         *a = group;
867     }
868
869     ECPKPARAMETERS_free(params);
870     *in = p;
871     return group;
872 }
873
874 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
875 {
876     int ret = 0;
877     ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL);
878     if (tmp == NULL) {
879         ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
880         return 0;
881     }
882     if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
883         ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
884         ECPKPARAMETERS_free(tmp);
885         return 0;
886     }
887     ECPKPARAMETERS_free(tmp);
888     return ret;
889 }
890
891 /* some EC_KEY functions */
892
893 EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
894 {
895     EC_KEY *ret = NULL;
896     EC_PRIVATEKEY *priv_key = NULL;
897     const unsigned char *p = *in;
898
899     if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
900         ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
901         return NULL;
902     }
903
904     if (a == NULL || *a == NULL) {
905         if ((ret = EC_KEY_new()) == NULL) {
906             ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
907             goto err;
908         }
909     } else
910         ret = *a;
911
912     if (priv_key->parameters) {
913         EC_GROUP_clear_free(ret->group);
914         ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters);
915     }
916
917     if (ret->group == NULL) {
918         ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
919         goto err;
920     }
921
922     ret->version = priv_key->version;
923
924     if (priv_key->privateKey) {
925         ASN1_OCTET_STRING *pkey = priv_key->privateKey;
926         if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey),
927                             ASN1_STRING_length(pkey)) == 0)
928             goto err;
929     } else {
930         ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
931         goto err;
932     }
933
934     EC_POINT_clear_free(ret->pub_key);
935     ret->pub_key = EC_POINT_new(ret->group);
936     if (ret->pub_key == NULL) {
937         ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
938         goto err;
939     }
940
941     if (priv_key->publicKey) {
942         const unsigned char *pub_oct;
943         int pub_oct_len;
944
945         pub_oct = ASN1_STRING_get0_data(priv_key->publicKey);
946         pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
947         if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) {
948             ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
949             goto err;
950         }
951     } else {
952         if (ret->group->meth->keygenpub == NULL
953             || ret->group->meth->keygenpub(ret) == 0)
954                 goto err;
955         /* Remember the original private-key-only encoding. */
956         ret->enc_flag |= EC_PKEY_NO_PUBKEY;
957     }
958
959     if (a)
960         *a = ret;
961     EC_PRIVATEKEY_free(priv_key);
962     *in = p;
963     return ret;
964
965  err:
966     if (a == NULL || *a != ret)
967         EC_KEY_free(ret);
968     EC_PRIVATEKEY_free(priv_key);
969     return NULL;
970 }
971
972 int i2d_ECPrivateKey(const EC_KEY *a, unsigned char **out)
973 {
974     int ret = 0, ok = 0;
975     unsigned char *priv= NULL, *pub= NULL;
976     size_t privlen = 0, publen = 0;
977
978     EC_PRIVATEKEY *priv_key = NULL;
979
980     if (a == NULL || a->group == NULL ||
981         (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
982         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
983         goto err;
984     }
985
986     if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
987         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
988         goto err;
989     }
990
991     priv_key->version = a->version;
992
993     privlen = EC_KEY_priv2buf(a, &priv);
994
995     if (privlen == 0) {
996         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
997         goto err;
998     }
999
1000     ASN1_STRING_set0(priv_key->privateKey, priv, privlen);
1001     priv = NULL;
1002
1003     if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1004         if ((priv_key->parameters =
1005              EC_GROUP_get_ecpkparameters(a->group,
1006                                         priv_key->parameters)) == NULL) {
1007             ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1008             goto err;
1009         }
1010     }
1011
1012     if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
1013         priv_key->publicKey = ASN1_BIT_STRING_new();
1014         if (priv_key->publicKey == NULL) {
1015             ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1016             goto err;
1017         }
1018
1019         publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL);
1020
1021         if (publen == 0) {
1022             ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1023             goto err;
1024         }
1025
1026         priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1027         priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1028         ASN1_STRING_set0(priv_key->publicKey, pub, publen);
1029         pub = NULL;
1030     }
1031
1032     if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1033         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1034         goto err;
1035     }
1036     ok = 1;
1037  err:
1038     OPENSSL_clear_free(priv, privlen);
1039     OPENSSL_free(pub);
1040     EC_PRIVATEKEY_free(priv_key);
1041     return (ok ? ret : 0);
1042 }
1043
1044 int i2d_ECParameters(const EC_KEY *a, unsigned char **out)
1045 {
1046     if (a == NULL) {
1047         ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1048         return 0;
1049     }
1050     return i2d_ECPKParameters(a->group, out);
1051 }
1052
1053 EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
1054 {
1055     EC_KEY *ret;
1056
1057     if (in == NULL || *in == NULL) {
1058         ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1059         return NULL;
1060     }
1061
1062     if (a == NULL || *a == NULL) {
1063         if ((ret = EC_KEY_new()) == NULL) {
1064             ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1065             return NULL;
1066         }
1067     } else
1068         ret = *a;
1069
1070     if (!d2i_ECPKParameters(&ret->group, in, len)) {
1071         ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1072         if (a == NULL || *a != ret)
1073              EC_KEY_free(ret);
1074         return NULL;
1075     }
1076
1077     if (a)
1078         *a = ret;
1079
1080     return ret;
1081 }
1082
1083 EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
1084 {
1085     EC_KEY *ret = NULL;
1086
1087     if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1088         /*
1089          * sorry, but a EC_GROUP-structure is necessary to set the public key
1090          */
1091         ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1092         return 0;
1093     }
1094     ret = *a;
1095     if (!EC_KEY_oct2key(ret, *in, len, NULL)) {
1096         ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1097         return 0;
1098     }
1099     *in += len;
1100     return ret;
1101 }
1102
1103 int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out)
1104 {
1105     size_t buf_len = 0;
1106     int new_buffer = 0;
1107
1108     if (a == NULL) {
1109         ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1110         return 0;
1111     }
1112
1113     buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1114                                  a->conv_form, NULL, 0, NULL);
1115
1116     if (out == NULL || buf_len == 0)
1117         /* out == NULL => just return the length of the octet string */
1118         return buf_len;
1119
1120     if (*out == NULL) {
1121         if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1122             ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1123             return 0;
1124         }
1125         new_buffer = 1;
1126     }
1127     if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1128                             *out, buf_len, NULL)) {
1129         ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
1130         if (new_buffer) {
1131             OPENSSL_free(*out);
1132             *out = NULL;
1133         }
1134         return 0;
1135     }
1136     if (!new_buffer)
1137         *out += buf_len;
1138     return buf_len;
1139 }
1140
1141 DECLARE_ASN1_FUNCTIONS(ECDSA_SIG)
1142 DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG, ECDSA_SIG)
1143
1144 ECDSA_SIG *ECDSA_SIG_new(void)
1145 {
1146     ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
1147     if (sig == NULL)
1148         ECerr(EC_F_ECDSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
1149     return sig;
1150 }
1151
1152 void ECDSA_SIG_free(ECDSA_SIG *sig)
1153 {
1154     if (sig == NULL)
1155         return;
1156     BN_clear_free(sig->r);
1157     BN_clear_free(sig->s);
1158     OPENSSL_free(sig);
1159 }
1160
1161 ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **psig, const unsigned char **ppin, long len)
1162 {
1163     ECDSA_SIG *sig;
1164
1165     if (len < 0)
1166         return NULL;
1167     if (psig != NULL && *psig != NULL) {
1168         sig = *psig;
1169     } else {
1170         sig = ECDSA_SIG_new();
1171         if (sig == NULL)
1172             return NULL;
1173     }
1174     if (sig->r == NULL)
1175         sig->r = BN_new();
1176     if (sig->s == NULL)
1177         sig->s = BN_new();
1178     if (decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) {
1179         if (psig == NULL || *psig == NULL)
1180             ECDSA_SIG_free(sig);
1181         return NULL;
1182     }
1183     if (psig != NULL && *psig == NULL)
1184         *psig = sig;
1185     return sig;
1186 }
1187
1188 int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **ppout)
1189 {
1190     unsigned char *buf = NULL;
1191     unsigned char *tmp;
1192     unsigned char **pp = NULL;
1193     size_t len;
1194     size_t encoded_len;
1195
1196     if (ppout != NULL && *ppout == NULL) {
1197         if ((len = encode_der_dsa_sig(sig->r, sig->s, NULL, SIZE_MAX)) == 0)
1198             return -1;
1199         buf = OPENSSL_malloc(len);
1200         if (buf == NULL)
1201             return -1;
1202         tmp = buf;
1203         pp = &tmp;
1204     } else {
1205         len = SIZE_MAX;
1206         pp = ppout;
1207     }
1208     if ((encoded_len = encode_der_dsa_sig(sig->r, sig->s, pp, len)) == 0) {
1209         OPENSSL_free(buf);
1210         return -1;
1211     }
1212     if (buf != NULL)
1213         *ppout = buf;
1214     return (int)encoded_len;
1215 }
1216
1217 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
1218 {
1219     if (pr != NULL)
1220         *pr = sig->r;
1221     if (ps != NULL)
1222         *ps = sig->s;
1223 }
1224
1225 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig)
1226 {
1227     return sig->r;
1228 }
1229
1230 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig)
1231 {
1232     return sig->s;
1233 }
1234
1235 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
1236 {
1237     if (r == NULL || s == NULL)
1238         return 0;
1239     BN_clear_free(sig->r);
1240     BN_clear_free(sig->s);
1241     sig->r = r;
1242     sig->s = s;
1243     return 1;
1244 }
1245
1246 int ECDSA_size(const EC_KEY *r)
1247 {
1248     int ret, i;
1249     ASN1_INTEGER bs;
1250     unsigned char buf[4];
1251     const EC_GROUP *group;
1252
1253     if (r == NULL)
1254         return 0;
1255     group = EC_KEY_get0_group(r);
1256     if (group == NULL)
1257         return 0;
1258
1259     i = EC_GROUP_order_bits(group);
1260     if (i == 0)
1261         return 0;
1262     bs.length = (i + 7) / 8;
1263     bs.data = buf;
1264     bs.type = V_ASN1_INTEGER;
1265     /* If the top bit is set the asn1 encoding is 1 larger. */
1266     buf[0] = 0xff;
1267
1268     i = i2d_ASN1_INTEGER(&bs, NULL);
1269     i += i;                     /* r and s */
1270     ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
1271     return ret;
1272 }