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