Coverage Report

Created: 2026-10-28 06:28

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