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