Coverage Report

Created: 2026-10-28 07:07

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