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