Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/ec/ec_backend.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2022 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
 * Low level APIs related to EC_KEY are deprecated for public use,
12
 * but still ok for internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <openssl/core_names.h>
17
#include <openssl/objects.h>
18
#include <openssl/params.h>
19
#include <openssl/err.h>
20
#ifndef FIPS_MODULE
21
#include <openssl/engine.h>
22
#include <openssl/x509.h>
23
#endif
24
#include "crypto/bn.h"
25
#include "crypto/ec.h"
26
#include "ec_local.h"
27
#include "e_os.h"
28
#include "internal/param_build_set.h"
29
30
/* Mapping between a flag and a name */
31
static const OSSL_ITEM encoding_nameid_map[] = {
32
    { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT },
33
    { OPENSSL_EC_NAMED_CURVE, OSSL_PKEY_EC_ENCODING_GROUP },
34
};
35
36
static const OSSL_ITEM check_group_type_nameid_map[] = {
37
    { 0, OSSL_PKEY_EC_GROUP_CHECK_DEFAULT },
38
    { EC_FLAG_CHECK_NAMED_GROUP, OSSL_PKEY_EC_GROUP_CHECK_NAMED },
39
    { EC_FLAG_CHECK_NAMED_GROUP_NIST, OSSL_PKEY_EC_GROUP_CHECK_NAMED_NIST },
40
};
41
42
static const OSSL_ITEM format_nameid_map[] = {
43
    { (int)POINT_CONVERSION_UNCOMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_UNCOMPRESSED },
44
    { (int)POINT_CONVERSION_COMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_COMPRESSED },
45
    { (int)POINT_CONVERSION_HYBRID, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_HYBRID },
46
};
47
48
int ossl_ec_encoding_name2id(const char *name)
49
92.7k
{
50
92.7k
    size_t i, sz;
51
52
    /* Return the default value if there is no name */
53
92.7k
    if (name == NULL)
54
0
        return OPENSSL_EC_NAMED_CURVE;
55
56
185k
    for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
57
185k
        if (OPENSSL_strcasecmp(name, encoding_nameid_map[i].ptr) == 0)
58
92.7k
            return encoding_nameid_map[i].id;
59
185k
    }
60
0
    return -1;
61
92.7k
}
62
63
static char *ec_param_encoding_id2name(int id)
64
602k
{
65
602k
    size_t i, sz;
66
67
1.20M
    for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
68
1.20M
        if (id == (int)encoding_nameid_map[i].id)
69
602k
            return encoding_nameid_map[i].ptr;
70
1.20M
    }
71
0
    return NULL;
72
602k
}
73
74
char *ossl_ec_check_group_type_id2name(int id)
75
560k
{
76
560k
    size_t i, sz;
77
78
560k
    for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
79
560k
        if (id == (int)check_group_type_nameid_map[i].id)
80
560k
            return check_group_type_nameid_map[i].ptr;
81
560k
    }
82
0
    return NULL;
83
560k
}
84
85
static int ec_check_group_type_name2id(const char *name)
86
51.1k
{
87
51.1k
    size_t i, sz;
88
89
    /* Return the default value if there is no name */
90
51.1k
    if (name == NULL)
91
0
        return 0;
92
93
51.1k
    for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
94
51.1k
        if (OPENSSL_strcasecmp(name, check_group_type_nameid_map[i].ptr) == 0)
95
51.1k
            return check_group_type_nameid_map[i].id;
96
51.1k
    }
97
0
    return -1;
98
51.1k
}
99
100
int ossl_ec_set_check_group_type_from_name(EC_KEY *ec, const char *name)
101
51.1k
{
102
51.1k
    int flags = ec_check_group_type_name2id(name);
103
104
51.1k
    if (flags == -1)
105
0
        return 0;
106
51.1k
    EC_KEY_clear_flags(ec, EC_FLAG_CHECK_NAMED_GROUP_MASK);
107
51.1k
    EC_KEY_set_flags(ec, flags);
108
51.1k
    return 1;
109
51.1k
}
110
111
static int ec_set_check_group_type_from_param(EC_KEY *ec, const OSSL_PARAM *p)
112
51.1k
{
113
51.1k
    const char *name = NULL;
114
51.1k
    int status = 0;
115
116
51.1k
    switch (p->data_type) {
117
51.1k
    case OSSL_PARAM_UTF8_STRING:
118
51.1k
        name = p->data;
119
51.1k
        status = (name != NULL);
120
51.1k
        break;
121
0
    case OSSL_PARAM_UTF8_PTR:
122
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
123
0
        break;
124
51.1k
    }
125
51.1k
    if (status)
126
51.1k
        return ossl_ec_set_check_group_type_from_name(ec, name);
127
0
    return 0;
128
51.1k
}
129
130
int ossl_ec_pt_format_name2id(const char *name)
131
185k
{
132
185k
    size_t i, sz;
133
134
    /* Return the default value if there is no name */
135
185k
    if (name == NULL)
136
0
        return (int)POINT_CONVERSION_UNCOMPRESSED;
137
138
185k
    for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
139
185k
        if (OPENSSL_strcasecmp(name, format_nameid_map[i].ptr) == 0)
140
185k
            return format_nameid_map[i].id;
141
185k
    }
142
0
    return -1;
143
185k
}
144
145
char *ossl_ec_pt_format_id2name(int id)
146
1.16M
{
147
1.16M
    size_t i, sz;
148
149
1.55M
    for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
150
1.50M
        if (id == (int)format_nameid_map[i].id)
151
1.11M
            return format_nameid_map[i].ptr;
152
1.50M
    }
153
46.7k
    return NULL;
154
1.16M
}
155
156
static int ec_group_explicit_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl,
157
    OSSL_PARAM params[], BN_CTX *bnctx,
158
    unsigned char **genbuf)
159
509k
{
160
509k
    int ret = 0, fid;
161
509k
    const char *field_type;
162
509k
    const OSSL_PARAM *param = NULL;
163
509k
    const OSSL_PARAM *param_p = NULL;
164
509k
    const OSSL_PARAM *param_a = NULL;
165
509k
    const OSSL_PARAM *param_b = NULL;
166
167
509k
    fid = EC_GROUP_get_field_type(group);
168
169
509k
    if (fid == NID_X9_62_prime_field) {
170
340k
        field_type = SN_X9_62_prime_field;
171
340k
    } else if (fid == NID_X9_62_characteristic_two_field) {
172
#ifdef OPENSSL_NO_EC2M
173
        ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
174
        goto err;
175
#else
176
168k
        field_type = SN_X9_62_characteristic_two_field;
177
168k
#endif
178
168k
    } else {
179
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
180
0
        return 0;
181
0
    }
182
183
509k
    param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
184
509k
    param_a = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
185
509k
    param_b = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
186
509k
    if (tmpl != NULL || param_p != NULL || param_a != NULL || param_b != NULL) {
187
0
        BIGNUM *p = BN_CTX_get(bnctx);
188
0
        BIGNUM *a = BN_CTX_get(bnctx);
189
0
        BIGNUM *b = BN_CTX_get(bnctx);
190
191
0
        if (b == NULL) {
192
0
            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
193
0
            goto err;
194
0
        }
195
196
0
        if (!EC_GROUP_get_curve(group, p, a, b, bnctx)) {
197
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
198
0
            goto err;
199
0
        }
200
0
        if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_P, p)
201
0
            || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_A, a)
202
0
            || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_B, b)) {
203
0
            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
204
0
            goto err;
205
0
        }
206
0
    }
207
208
509k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
209
509k
    if (tmpl != NULL || param != NULL) {
210
0
        const BIGNUM *order = EC_GROUP_get0_order(group);
211
212
0
        if (order == NULL) {
213
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
214
0
            goto err;
215
0
        }
216
0
        if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_ORDER,
217
0
                order)) {
218
0
            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
219
0
            goto err;
220
0
        }
221
0
    }
222
223
509k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
224
509k
    if (tmpl != NULL || param != NULL) {
225
88
        if (!ossl_param_build_set_utf8_string(tmpl, params,
226
88
                OSSL_PKEY_PARAM_EC_FIELD_TYPE,
227
88
                field_type)) {
228
0
            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
229
0
            goto err;
230
0
        }
231
88
    }
232
233
509k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
234
509k
    if (tmpl != NULL || param != NULL) {
235
0
        size_t genbuf_len;
236
0
        const EC_POINT *genpt = EC_GROUP_get0_generator(group);
237
0
        point_conversion_form_t genform = EC_GROUP_get_point_conversion_form(group);
238
239
0
        if (genpt == NULL) {
240
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
241
0
            goto err;
242
0
        }
243
0
        genbuf_len = EC_POINT_point2buf(group, genpt, genform, genbuf, bnctx);
244
0
        if (genbuf_len == 0) {
245
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
246
0
            goto err;
247
0
        }
248
0
        if (!ossl_param_build_set_octet_string(tmpl, params,
249
0
                OSSL_PKEY_PARAM_EC_GENERATOR,
250
0
                *genbuf, genbuf_len)) {
251
0
            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
252
0
            goto err;
253
0
        }
254
0
    }
255
256
509k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
257
509k
    if (tmpl != NULL || param != NULL) {
258
0
        const BIGNUM *cofactor = EC_GROUP_get0_cofactor(group);
259
260
0
        if (cofactor != NULL
261
0
            && !ossl_param_build_set_bn(tmpl, params,
262
0
                OSSL_PKEY_PARAM_EC_COFACTOR, cofactor)) {
263
0
            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
264
0
            goto err;
265
0
        }
266
0
    }
267
268
509k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
269
509k
    if (tmpl != NULL || param != NULL) {
270
0
        unsigned char *seed = EC_GROUP_get0_seed(group);
271
0
        size_t seed_len = EC_GROUP_get_seed_len(group);
272
273
0
        if (seed != NULL
274
0
            && seed_len > 0
275
0
            && !ossl_param_build_set_octet_string(tmpl, params,
276
0
                OSSL_PKEY_PARAM_EC_SEED,
277
0
                seed, seed_len)) {
278
0
            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
279
0
            goto err;
280
0
        }
281
0
    }
282
509k
    ret = 1;
283
509k
err:
284
509k
    return ret;
285
509k
}
286
287
int ossl_ec_group_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl,
288
    OSSL_PARAM params[], OSSL_LIB_CTX *libctx,
289
    const char *propq,
290
    BN_CTX *bnctx, unsigned char **genbuf)
291
602k
{
292
602k
    int ret = 0, curve_nid, encoding_flag;
293
602k
    const char *encoding_name, *pt_form_name;
294
602k
    point_conversion_form_t genform;
295
296
602k
    if (group == NULL) {
297
0
        ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER);
298
0
        return 0;
299
0
    }
300
301
602k
    genform = EC_GROUP_get_point_conversion_form(group);
302
602k
    pt_form_name = ossl_ec_pt_format_id2name(genform);
303
602k
    if (pt_form_name == NULL
304
602k
        || !ossl_param_build_set_utf8_string(
305
602k
            tmpl, params,
306
602k
            OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, pt_form_name)) {
307
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
308
0
        return 0;
309
0
    }
310
602k
    encoding_flag = EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE;
311
602k
    encoding_name = ec_param_encoding_id2name(encoding_flag);
312
602k
    if (encoding_name == NULL
313
602k
        || !ossl_param_build_set_utf8_string(tmpl, params,
314
602k
            OSSL_PKEY_PARAM_EC_ENCODING,
315
602k
            encoding_name)) {
316
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
317
0
        return 0;
318
0
    }
319
320
602k
    if (!ossl_param_build_set_int(tmpl, params,
321
602k
            OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS,
322
602k
            group->decoded_from_explicit_params))
323
0
        return 0;
324
325
602k
    curve_nid = EC_GROUP_get_curve_name(group);
326
327
    /*
328
     * Get the explicit parameters in these two cases:
329
     * - We do not have a template, i.e. specific parameters are requested
330
     * - The curve is not a named curve
331
     */
332
602k
    if (tmpl == NULL || curve_nid == NID_undef)
333
509k
        if (!ec_group_explicit_todata(group, tmpl, params, bnctx, genbuf))
334
0
            goto err;
335
336
602k
    if (curve_nid != NID_undef) {
337
        /* Named curve */
338
599k
        const char *curve_name = OSSL_EC_curve_nid2name(curve_nid);
339
340
599k
        if (curve_name == NULL
341
599k
            || !ossl_param_build_set_utf8_string(tmpl, params,
342
599k
                OSSL_PKEY_PARAM_GROUP_NAME,
343
599k
                curve_name)) {
344
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
345
0
            goto err;
346
0
        }
347
599k
    }
348
602k
    ret = 1;
349
602k
err:
350
602k
    return ret;
351
602k
}
352
353
/*
354
 * The intention with the "backend" source file is to offer backend support
355
 * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
356
 * implementations alike.
357
 */
358
int ossl_ec_set_ecdh_cofactor_mode(EC_KEY *ec, int mode)
359
107k
{
360
107k
    const EC_GROUP *ecg = EC_KEY_get0_group(ec);
361
107k
    const BIGNUM *cofactor;
362
    /*
363
     * mode can be only 0 for disable, or 1 for enable here.
364
     *
365
     * This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that
366
     * also supports mode == -1 with the meaning of "reset to the default for
367
     * the associated key".
368
     */
369
107k
    if (mode < 0 || mode > 1)
370
0
        return 0;
371
372
107k
    if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL)
373
0
        return 0;
374
375
    /* ECDH cofactor mode has no effect if cofactor is 1 */
376
107k
    if (BN_is_one(cofactor))
377
107k
        return 1;
378
379
0
    if (mode == 1)
380
0
        EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
381
0
    else if (mode == 0)
382
0
        EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);
383
384
0
    return 1;
385
107k
}
386
387
/*
388
 * Callers of ossl_ec_key_fromdata MUST make sure that ec_key_params_fromdata has
389
 * been called before!
390
 *
391
 * This function only gets the bare keypair, domain parameters and other
392
 * parameters are treated separately, and domain parameters are required to
393
 * define a keypair.
394
 */
395
int ossl_ec_key_fromdata(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
396
83.1k
{
397
83.1k
    const OSSL_PARAM *param_priv_key = NULL, *param_pub_key = NULL;
398
83.1k
    BN_CTX *ctx = NULL;
399
83.1k
    BIGNUM *priv_key = NULL;
400
83.1k
    unsigned char *pub_key = NULL;
401
83.1k
    size_t pub_key_len;
402
83.1k
    const EC_GROUP *ecg = NULL;
403
83.1k
    EC_POINT *pub_point = NULL;
404
83.1k
    int ok = 0;
405
406
83.1k
    ecg = EC_KEY_get0_group(ec);
407
83.1k
    if (ecg == NULL)
408
0
        return 0;
409
410
83.1k
    param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
411
83.1k
    if (include_private)
412
83.1k
        param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
413
414
83.1k
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
415
83.1k
    if (ctx == NULL)
416
0
        goto err;
417
418
83.1k
    if (param_pub_key != NULL)
419
83.1k
        if (!OSSL_PARAM_get_octet_string(param_pub_key,
420
83.1k
                (void **)&pub_key, 0, &pub_key_len)
421
83.1k
            || (pub_point = EC_POINT_new(ecg)) == NULL
422
83.1k
            || !EC_POINT_oct2point(ecg, pub_point, pub_key, pub_key_len, ctx))
423
0
            goto err;
424
425
83.1k
    if (param_priv_key != NULL && include_private) {
426
83.1k
        int fixed_words;
427
83.1k
        const BIGNUM *order;
428
429
        /*
430
         * Key import/export should never leak the bit length of the secret
431
         * scalar in the key.
432
         *
433
         * For this reason, on export we use padded BIGNUMs with fixed length.
434
         *
435
         * When importing we also should make sure that, even if short lived,
436
         * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
437
         * soon as possible, so that any processing of this BIGNUM might opt for
438
         * constant time implementations in the backend.
439
         *
440
         * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
441
         * to preallocate the BIGNUM internal buffer to a fixed public size big
442
         * enough that operations performed during the processing never trigger
443
         * a realloc which would leak the size of the scalar through memory
444
         * accesses.
445
         *
446
         * Fixed Length
447
         * ------------
448
         *
449
         * The order of the large prime subgroup of the curve is our choice for
450
         * a fixed public size, as that is generally the upper bound for
451
         * generating a private key in EC cryptosystems and should fit all valid
452
         * secret scalars.
453
         *
454
         * For padding on export we just use the bit length of the order
455
         * converted to bytes (rounding up).
456
         *
457
         * For preallocating the BIGNUM storage we look at the number of "words"
458
         * required for the internal representation of the order, and we
459
         * preallocate 2 extra "words" in case any of the subsequent processing
460
         * might temporarily overflow the order length.
461
         */
462
83.1k
        order = EC_GROUP_get0_order(ecg);
463
83.1k
        if (order == NULL || BN_is_zero(order))
464
0
            goto err;
465
466
83.1k
        fixed_words = bn_get_top(order) + 2;
467
468
83.1k
        if ((priv_key = BN_secure_new()) == NULL)
469
0
            goto err;
470
83.1k
        if (bn_wexpand(priv_key, fixed_words) == NULL)
471
0
            goto err;
472
83.1k
        BN_set_flags(priv_key, BN_FLG_CONSTTIME);
473
474
83.1k
        if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
475
0
            goto err;
476
83.1k
    }
477
478
83.1k
    if (priv_key != NULL
479
83.1k
        && !EC_KEY_set_private_key(ec, priv_key))
480
0
        goto err;
481
482
83.1k
    if (pub_point != NULL
483
83.1k
        && !EC_KEY_set_public_key(ec, pub_point))
484
0
        goto err;
485
486
83.1k
    ok = 1;
487
488
83.1k
err:
489
83.1k
    BN_CTX_free(ctx);
490
83.1k
    BN_clear_free(priv_key);
491
83.1k
    OPENSSL_free(pub_key);
492
83.1k
    EC_POINT_free(pub_point);
493
83.1k
    return ok;
494
83.1k
}
495
496
int ossl_ec_group_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
497
92.7k
{
498
92.7k
    int ok = 0;
499
92.7k
    EC_GROUP *group = NULL;
500
501
92.7k
    if (ec == NULL)
502
0
        return 0;
503
504
92.7k
    group = EC_GROUP_new_from_params(params, ossl_ec_key_get_libctx(ec),
505
92.7k
        ossl_ec_key_get0_propq(ec));
506
507
92.7k
    if (!EC_KEY_set_group(ec, group))
508
0
        goto err;
509
92.7k
    ok = 1;
510
92.7k
err:
511
92.7k
    EC_GROUP_free(group);
512
92.7k
    return ok;
513
92.7k
}
514
515
static int ec_key_point_format_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
516
99.2k
{
517
99.2k
    const OSSL_PARAM *p;
518
99.2k
    int format = -1;
519
520
99.2k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
521
99.2k
    if (p != NULL) {
522
92.7k
        if (!ossl_ec_pt_format_param2id(p, &format)) {
523
0
            ECerr(0, EC_R_INVALID_FORM);
524
0
            return 0;
525
0
        }
526
92.7k
        EC_KEY_set_conv_form(ec, format);
527
92.7k
    }
528
99.2k
    return 1;
529
99.2k
}
530
531
static int ec_key_group_check_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
532
99.2k
{
533
99.2k
    const OSSL_PARAM *p;
534
535
99.2k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE);
536
99.2k
    if (p != NULL)
537
51.1k
        return ec_set_check_group_type_from_param(ec, p);
538
48.0k
    return 1;
539
99.2k
}
540
541
static int ec_set_include_public(EC_KEY *ec, int include)
542
0
{
543
0
    int flags = EC_KEY_get_enc_flags(ec);
544
545
0
    if (!include)
546
0
        flags |= EC_PKEY_NO_PUBKEY;
547
0
    else
548
0
        flags &= ~EC_PKEY_NO_PUBKEY;
549
0
    EC_KEY_set_enc_flags(ec, flags);
550
0
    return 1;
551
0
}
552
553
int ossl_ec_key_otherparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
554
99.2k
{
555
99.2k
    const OSSL_PARAM *p;
556
557
99.2k
    if (ec == NULL)
558
0
        return 0;
559
560
99.2k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
561
99.2k
    if (p != NULL) {
562
92.7k
        int mode;
563
564
92.7k
        if (!OSSL_PARAM_get_int(p, &mode)
565
92.7k
            || !ossl_ec_set_ecdh_cofactor_mode(ec, mode))
566
0
            return 0;
567
92.7k
    }
568
569
99.2k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC);
570
99.2k
    if (p != NULL) {
571
0
        int include = 1;
572
573
0
        if (!OSSL_PARAM_get_int(p, &include)
574
0
            || !ec_set_include_public(ec, include))
575
0
            return 0;
576
0
    }
577
99.2k
    if (!ec_key_point_format_fromdata(ec, params))
578
0
        return 0;
579
99.2k
    if (!ec_key_group_check_fromdata(ec, params))
580
0
        return 0;
581
99.2k
    return 1;
582
99.2k
}
583
584
int ossl_ec_key_is_foreign(const EC_KEY *ec)
585
359k
{
586
359k
#ifndef FIPS_MODULE
587
359k
    if (ec->engine != NULL || EC_KEY_get_method(ec) != EC_KEY_OpenSSL())
588
0
        return 1;
589
359k
#endif
590
359k
    return 0;
591
359k
}
592
593
EC_KEY *ossl_ec_key_dup(const EC_KEY *src, int selection)
594
7.34k
{
595
7.34k
    EC_KEY *ret;
596
597
7.34k
    if (src == NULL) {
598
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
599
0
        return NULL;
600
0
    }
601
602
7.34k
    if ((ret = ossl_ec_key_new_method_int(src->libctx, src->propq,
603
7.34k
             src->engine))
604
7.34k
        == NULL)
605
0
        return NULL;
606
607
    /* copy the parameters */
608
7.34k
    if (src->group != NULL
609
7.34k
        && (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
610
7.34k
        ret->group = ossl_ec_group_new_ex(src->libctx, src->propq,
611
7.34k
            src->group->meth);
612
7.34k
        if (ret->group == NULL
613
7.34k
            || !EC_GROUP_copy(ret->group, src->group))
614
0
            goto err;
615
616
7.34k
        if (src->meth != NULL)
617
7.34k
            ret->meth = src->meth;
618
7.34k
    }
619
620
    /*  copy the public key */
621
7.34k
    if (src->pub_key != NULL
622
6.84k
        && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
623
3.52k
        if (ret->group == NULL)
624
            /* no parameter-less keys allowed */
625
0
            goto err;
626
3.52k
        ret->pub_key = EC_POINT_new(ret->group);
627
3.52k
        if (ret->pub_key == NULL
628
3.52k
            || !EC_POINT_copy(ret->pub_key, src->pub_key))
629
0
            goto err;
630
3.52k
    }
631
632
    /* copy the private key */
633
7.34k
    if (src->priv_key != NULL
634
6.31k
        && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
635
3.00k
        if (ret->group == NULL)
636
            /* no parameter-less keys allowed */
637
0
            goto err;
638
3.00k
        ret->priv_key = BN_new();
639
3.00k
        if (ret->priv_key == NULL || !BN_copy(ret->priv_key, src->priv_key))
640
0
            goto err;
641
3.00k
        if (ret->group->meth->keycopy
642
0
            && ret->group->meth->keycopy(ret, src) == 0)
643
0
            goto err;
644
3.00k
    }
645
646
    /* copy the rest */
647
7.34k
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
648
4.02k
        ret->enc_flag = src->enc_flag;
649
4.02k
        ret->conv_form = src->conv_form;
650
4.02k
    }
651
652
7.34k
    ret->version = src->version;
653
7.34k
    ret->flags = src->flags;
654
655
7.34k
#ifndef FIPS_MODULE
656
7.34k
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
657
7.34k
            &ret->ex_data, &src->ex_data))
658
0
        goto err;
659
7.34k
#endif
660
661
7.34k
    if (ret->meth != NULL && ret->meth->copy != NULL) {
662
0
        if ((selection
663
0
                & OSSL_KEYMGMT_SELECT_KEYPAIR)
664
0
            != OSSL_KEYMGMT_SELECT_KEYPAIR)
665
0
            goto err;
666
0
        if (ret->meth->copy(ret, src) == 0)
667
0
            goto err;
668
0
    }
669
670
7.34k
    return ret;
671
0
err:
672
0
    EC_KEY_free(ret);
673
0
    return NULL;
674
7.34k
}
675
676
int ossl_ec_encoding_param2id(const OSSL_PARAM *p, int *id)
677
92.7k
{
678
92.7k
    const char *name = NULL;
679
92.7k
    int status = 0;
680
681
92.7k
    switch (p->data_type) {
682
92.7k
    case OSSL_PARAM_UTF8_STRING:
683
        /* The OSSL_PARAM functions have no support for this */
684
92.7k
        name = p->data;
685
92.7k
        status = (name != NULL);
686
92.7k
        break;
687
0
    case OSSL_PARAM_UTF8_PTR:
688
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
689
0
        break;
690
92.7k
    }
691
92.7k
    if (status) {
692
92.7k
        int i = ossl_ec_encoding_name2id(name);
693
694
92.7k
        if (i >= 0) {
695
92.7k
            *id = i;
696
92.7k
            return 1;
697
92.7k
        }
698
92.7k
    }
699
0
    return 0;
700
92.7k
}
701
702
int ossl_ec_pt_format_param2id(const OSSL_PARAM *p, int *id)
703
185k
{
704
185k
    const char *name = NULL;
705
185k
    int status = 0;
706
707
185k
    switch (p->data_type) {
708
185k
    case OSSL_PARAM_UTF8_STRING:
709
        /* The OSSL_PARAM functions have no support for this */
710
185k
        name = p->data;
711
185k
        status = (name != NULL);
712
185k
        break;
713
0
    case OSSL_PARAM_UTF8_PTR:
714
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
715
0
        break;
716
185k
    }
717
185k
    if (status) {
718
185k
        int i = ossl_ec_pt_format_name2id(name);
719
720
185k
        if (i >= 0) {
721
185k
            *id = i;
722
185k
            return 1;
723
185k
        }
724
185k
    }
725
0
    return 0;
726
185k
}
727
728
#ifndef FIPS_MODULE
729
int ossl_x509_algor_is_sm2(const X509_ALGOR *palg)
730
726k
{
731
726k
    int ptype = 0;
732
726k
    const void *pval = NULL;
733
734
726k
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
735
736
726k
    if (ptype == V_ASN1_OBJECT)
737
676k
        return OBJ_obj2nid((ASN1_OBJECT *)pval) == NID_sm2;
738
739
49.6k
    if (ptype == V_ASN1_SEQUENCE) {
740
37.6k
        const ASN1_STRING *str = pval;
741
37.6k
        const unsigned char *der = str->data;
742
37.6k
        int derlen = str->length;
743
37.6k
        EC_GROUP *group;
744
37.6k
        int ret;
745
746
37.6k
        if ((group = d2i_ECPKParameters(NULL, &der, derlen)) == NULL)
747
34.7k
            ret = 0;
748
2.91k
        else
749
2.91k
            ret = (EC_GROUP_get_curve_name(group) == NID_sm2);
750
751
37.6k
        EC_GROUP_free(group);
752
37.6k
        return ret;
753
37.6k
    }
754
755
11.9k
    return 0;
756
49.6k
}
757
758
EC_KEY *ossl_ec_key_param_from_x509_algor(const X509_ALGOR *palg,
759
    OSSL_LIB_CTX *libctx, const char *propq)
760
725k
{
761
725k
    int ptype = 0;
762
725k
    const void *pval = NULL;
763
725k
    EC_KEY *eckey = NULL;
764
725k
    EC_GROUP *group = NULL;
765
766
725k
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
767
725k
    if ((eckey = EC_KEY_new_ex(libctx, propq)) == NULL) {
768
0
        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
769
0
        goto ecerr;
770
0
    }
771
772
725k
    if (ptype == V_ASN1_SEQUENCE) {
773
38.0k
        const ASN1_STRING *pstr = pval;
774
38.0k
        const unsigned char *pm = pstr->data;
775
38.0k
        int pmlen = pstr->length;
776
777
38.0k
        if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) {
778
34.9k
            ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
779
34.9k
            goto ecerr;
780
34.9k
        }
781
687k
    } else if (ptype == V_ASN1_OBJECT) {
782
675k
        const ASN1_OBJECT *poid = pval;
783
784
        /*
785
         * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
786
         */
787
788
675k
        group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid));
789
675k
        if (group == NULL)
790
43.5k
            goto ecerr;
791
631k
        EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
792
631k
        if (EC_KEY_set_group(eckey, group) == 0)
793
0
            goto ecerr;
794
631k
        EC_GROUP_free(group);
795
631k
    } else {
796
11.8k
        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
797
11.8k
        goto ecerr;
798
11.8k
    }
799
800
634k
    return eckey;
801
802
90.3k
ecerr:
803
90.3k
    EC_KEY_free(eckey);
804
90.3k
    EC_GROUP_free(group);
805
90.3k
    return NULL;
806
725k
}
807
808
EC_KEY *ossl_ec_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
809
    OSSL_LIB_CTX *libctx, const char *propq)
810
2.16k
{
811
2.16k
    const unsigned char *p = NULL;
812
2.16k
    int pklen;
813
2.16k
    EC_KEY *eckey = NULL;
814
2.16k
    const X509_ALGOR *palg;
815
816
2.16k
    if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
817
0
        return 0;
818
2.16k
    eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);
819
2.16k
    if (eckey == NULL)
820
554
        goto err;
821
822
    /* We have parameters now set private key */
823
1.61k
    if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
824
799
        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
825
799
        goto err;
826
799
    }
827
828
811
    return eckey;
829
1.35k
err:
830
1.35k
    EC_KEY_free(eckey);
831
    return NULL;
832
1.61k
}
833
#endif