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