Coverage Report

Created: 2026-07-12 07:21

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