Coverage Report

Created: 2025-12-31 06:58

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
102k
{
50
102k
    size_t i, sz;
51
52
    /* Return the default value if there is no name */
53
102k
    if (name == NULL)
54
0
        return OPENSSL_EC_NAMED_CURVE;
55
56
205k
    for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
57
205k
        if (OPENSSL_strcasecmp(name, encoding_nameid_map[i].ptr) == 0)
58
102k
            return encoding_nameid_map[i].id;
59
205k
    }
60
0
    return -1;
61
102k
}
62
63
static char *ec_param_encoding_id2name(int id)
64
564k
{
65
564k
    size_t i, sz;
66
67
1.12M
    for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
68
1.12M
        if (id == (int)encoding_nameid_map[i].id)
69
564k
            return encoding_nameid_map[i].ptr;
70
1.12M
    }
71
0
    return NULL;
72
564k
}
73
74
char *ossl_ec_check_group_type_id2name(int id)
75
512k
{
76
512k
    size_t i, sz;
77
78
512k
    for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
79
512k
        if (id == (int)check_group_type_nameid_map[i].id)
80
512k
            return check_group_type_nameid_map[i].ptr;
81
512k
    }
82
0
    return NULL;
83
512k
}
84
85
static int ec_check_group_type_name2id(const char *name)
86
51.4k
{
87
51.4k
    size_t i, sz;
88
89
    /* Return the default value if there is no name */
90
51.4k
    if (name == NULL)
91
0
        return 0;
92
93
51.4k
    for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
94
51.4k
        if (OPENSSL_strcasecmp(name, check_group_type_nameid_map[i].ptr) == 0)
95
51.4k
            return check_group_type_nameid_map[i].id;
96
51.4k
    }
97
0
    return -1;
98
51.4k
}
99
100
int ossl_ec_set_check_group_type_from_name(EC_KEY *ec, const char *name)
101
51.4k
{
102
51.4k
    int flags = ec_check_group_type_name2id(name);
103
104
51.4k
    if (flags == -1)
105
0
        return 0;
106
51.4k
    EC_KEY_clear_flags(ec, EC_FLAG_CHECK_NAMED_GROUP_MASK);
107
51.4k
    EC_KEY_set_flags(ec, flags);
108
51.4k
    return 1;
109
51.4k
}
110
111
static int ec_set_check_group_type_from_param(EC_KEY *ec, const OSSL_PARAM *p)
112
51.4k
{
113
51.4k
    const char *name = NULL;
114
51.4k
    int status = 0;
115
116
51.4k
    switch (p->data_type) {
117
51.4k
    case OSSL_PARAM_UTF8_STRING:
118
51.4k
        name = p->data;
119
51.4k
        status = (name != NULL);
120
51.4k
        break;
121
0
    case OSSL_PARAM_UTF8_PTR:
122
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
123
0
        break;
124
51.4k
    }
125
51.4k
    if (status)
126
51.4k
        return ossl_ec_set_check_group_type_from_name(ec, name);
127
0
    return 0;
128
51.4k
}
129
130
int ossl_ec_pt_format_name2id(const char *name)
131
205k
{
132
205k
    size_t i, sz;
133
134
    /* Return the default value if there is no name */
135
205k
    if (name == NULL)
136
0
        return (int)POINT_CONVERSION_UNCOMPRESSED;
137
138
205k
    for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
139
205k
        if (OPENSSL_strcasecmp(name, format_nameid_map[i].ptr) == 0)
140
205k
            return format_nameid_map[i].id;
141
205k
    }
142
0
    return -1;
143
205k
}
144
145
char *ossl_ec_pt_format_id2name(int id)
146
1.07M
{
147
1.07M
    size_t i, sz;
148
149
1.42M
    for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
150
1.37M
        if (id == (int)format_nameid_map[i].id)
151
1.02M
            return format_nameid_map[i].ptr;
152
1.37M
    }
153
48.1k
    return NULL;
154
1.07M
}
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
461k
{
160
461k
    int ret = 0, fid;
161
461k
    const char *field_type;
162
461k
    const OSSL_PARAM *param = NULL;
163
461k
    const OSSL_PARAM *param_p = NULL;
164
461k
    const OSSL_PARAM *param_a = NULL;
165
461k
    const OSSL_PARAM *param_b = NULL;
166
167
461k
    fid = EC_GROUP_get_field_type(group);
168
169
461k
    if (fid == NID_X9_62_prime_field) {
170
329k
        field_type = SN_X9_62_prime_field;
171
329k
    } 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
131k
        field_type = SN_X9_62_characteristic_two_field;
177
131k
#endif
178
131k
    } else {
179
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
180
0
        return 0;
181
0
    }
182
183
461k
    param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
184
461k
    param_a = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
185
461k
    param_b = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
186
461k
    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
461k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
209
461k
    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
461k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
224
461k
    if (tmpl != NULL || param != NULL) {
225
104
        if (!ossl_param_build_set_utf8_string(tmpl, params,
226
104
                OSSL_PKEY_PARAM_EC_FIELD_TYPE,
227
104
                field_type)) {
228
0
            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
229
0
            goto err;
230
0
        }
231
104
    }
232
233
461k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
234
461k
    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
461k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
257
461k
    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
461k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
269
461k
    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
461k
    ret = 1;
283
461k
err:
284
461k
    return ret;
285
461k
}
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
564k
{
292
564k
    int ret = 0, curve_nid, encoding_flag;
293
564k
    const char *encoding_name, *pt_form_name;
294
564k
    point_conversion_form_t genform;
295
296
564k
    if (group == NULL) {
297
0
        ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER);
298
0
        return 0;
299
0
    }
300
301
564k
    genform = EC_GROUP_get_point_conversion_form(group);
302
564k
    pt_form_name = ossl_ec_pt_format_id2name(genform);
303
564k
    if (pt_form_name == NULL
304
564k
        || !ossl_param_build_set_utf8_string(
305
564k
            tmpl, params,
306
564k
            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
564k
    encoding_flag = EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE;
311
564k
    encoding_name = ec_param_encoding_id2name(encoding_flag);
312
564k
    if (encoding_name == NULL
313
564k
        || !ossl_param_build_set_utf8_string(tmpl, params,
314
564k
            OSSL_PKEY_PARAM_EC_ENCODING,
315
564k
            encoding_name)) {
316
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
317
0
        return 0;
318
0
    }
319
320
564k
    if (!ossl_param_build_set_int(tmpl, params,
321
564k
            OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS,
322
564k
            group->decoded_from_explicit_params))
323
0
        return 0;
324
325
564k
    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
564k
    if (tmpl == NULL || curve_nid == NID_undef)
333
461k
        if (!ec_group_explicit_todata(group, tmpl, params, bnctx, genbuf))
334
0
            goto err;
335
336
564k
    if (curve_nid != NID_undef) {
337
        /* Named curve */
338
561k
        const char *curve_name = OSSL_EC_curve_nid2name(curve_nid);
339
340
561k
        if (curve_name == NULL
341
561k
            || !ossl_param_build_set_utf8_string(tmpl, params,
342
561k
                OSSL_PKEY_PARAM_GROUP_NAME,
343
561k
                curve_name)) {
344
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
345
0
            goto err;
346
0
        }
347
561k
    }
348
564k
    ret = 1;
349
564k
err:
350
564k
    return ret;
351
564k
}
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
116k
{
360
116k
    const EC_GROUP *ecg = EC_KEY_get0_group(ec);
361
116k
    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
116k
    if (mode < 0 || mode > 1)
370
0
        return 0;
371
372
116k
    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
116k
    if (BN_is_one(cofactor))
377
116k
        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
116k
}
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
82.9k
{
397
82.9k
    const OSSL_PARAM *param_priv_key = NULL, *param_pub_key = NULL;
398
82.9k
    BN_CTX *ctx = NULL;
399
82.9k
    BIGNUM *priv_key = NULL;
400
82.9k
    unsigned char *pub_key = NULL;
401
82.9k
    size_t pub_key_len;
402
82.9k
    const EC_GROUP *ecg = NULL;
403
82.9k
    EC_POINT *pub_point = NULL;
404
82.9k
    int ok = 0;
405
406
82.9k
    ecg = EC_KEY_get0_group(ec);
407
82.9k
    if (ecg == NULL)
408
0
        return 0;
409
410
82.9k
    param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
411
82.9k
    if (include_private)
412
82.9k
        param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
413
414
82.9k
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
415
82.9k
    if (ctx == NULL)
416
0
        goto err;
417
418
82.9k
    if (param_pub_key != NULL)
419
82.9k
        if (!OSSL_PARAM_get_octet_string(param_pub_key,
420
82.9k
                (void **)&pub_key, 0, &pub_key_len)
421
82.9k
            || (pub_point = EC_POINT_new(ecg)) == NULL
422
82.9k
            || !EC_POINT_oct2point(ecg, pub_point, pub_key, pub_key_len, ctx))
423
0
            goto err;
424
425
82.9k
    if (param_priv_key != NULL && include_private) {
426
82.9k
        int fixed_words;
427
82.9k
        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
82.9k
        order = EC_GROUP_get0_order(ecg);
463
82.9k
        if (order == NULL || BN_is_zero(order))
464
0
            goto err;
465
466
82.9k
        fixed_words = bn_get_top(order) + 2;
467
468
82.9k
        if ((priv_key = BN_secure_new()) == NULL)
469
0
            goto err;
470
82.9k
        if (bn_wexpand(priv_key, fixed_words) == NULL)
471
0
            goto err;
472
82.9k
        BN_set_flags(priv_key, BN_FLG_CONSTTIME);
473
474
82.9k
        if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
475
0
            goto err;
476
82.9k
    }
477
478
82.9k
    if (priv_key != NULL
479
82.9k
        && !EC_KEY_set_private_key(ec, priv_key))
480
0
        goto err;
481
482
82.9k
    if (pub_point != NULL
483
82.9k
        && !EC_KEY_set_public_key(ec, pub_point))
484
0
        goto err;
485
486
82.9k
    ok = 1;
487
488
82.9k
err:
489
82.9k
    BN_CTX_free(ctx);
490
82.9k
    BN_clear_free(priv_key);
491
82.9k
    OPENSSL_free(pub_key);
492
82.9k
    EC_POINT_free(pub_point);
493
82.9k
    return ok;
494
82.9k
}
495
496
int ossl_ec_group_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
497
102k
{
498
102k
    int ok = 0;
499
102k
    EC_GROUP *group = NULL;
500
501
102k
    if (ec == NULL)
502
0
        return 0;
503
504
102k
    group = EC_GROUP_new_from_params(params, ossl_ec_key_get_libctx(ec),
505
102k
        ossl_ec_key_get0_propq(ec));
506
507
102k
    if (!EC_KEY_set_group(ec, group))
508
0
        goto err;
509
102k
    ok = 1;
510
102k
err:
511
102k
    EC_GROUP_free(group);
512
102k
    return ok;
513
102k
}
514
515
static int ec_key_point_format_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
516
108k
{
517
108k
    const OSSL_PARAM *p;
518
108k
    int format = -1;
519
520
108k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
521
108k
    if (p != NULL) {
522
102k
        if (!ossl_ec_pt_format_param2id(p, &format)) {
523
0
            ECerr(0, EC_R_INVALID_FORM);
524
0
            return 0;
525
0
        }
526
102k
        EC_KEY_set_conv_form(ec, format);
527
102k
    }
528
108k
    return 1;
529
108k
}
530
531
static int ec_key_group_check_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
532
108k
{
533
108k
    const OSSL_PARAM *p;
534
535
108k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE);
536
108k
    if (p != NULL)
537
51.4k
        return ec_set_check_group_type_from_param(ec, p);
538
56.9k
    return 1;
539
108k
}
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
108k
{
555
108k
    const OSSL_PARAM *p;
556
557
108k
    if (ec == NULL)
558
0
        return 0;
559
560
108k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
561
108k
    if (p != NULL) {
562
102k
        int mode;
563
564
102k
        if (!OSSL_PARAM_get_int(p, &mode)
565
102k
            || !ossl_ec_set_ecdh_cofactor_mode(ec, mode))
566
0
            return 0;
567
102k
    }
568
569
108k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC);
570
108k
    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
108k
    if (!ec_key_point_format_fromdata(ec, params))
578
0
        return 0;
579
108k
    if (!ec_key_group_check_fromdata(ec, params))
580
0
        return 0;
581
108k
    return 1;
582
108k
}
583
584
int ossl_ec_key_is_foreign(const EC_KEY *ec)
585
329k
{
586
329k
#ifndef FIPS_MODULE
587
329k
    if (ec->engine != NULL || EC_KEY_get_method(ec) != EC_KEY_OpenSSL())
588
0
        return 1;
589
329k
#endif
590
329k
    return 0;
591
329k
}
592
593
EC_KEY *ossl_ec_key_dup(const EC_KEY *src, int selection)
594
9.15k
{
595
9.15k
    EC_KEY *ret;
596
597
9.15k
    if (src == NULL) {
598
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
599
0
        return NULL;
600
0
    }
601
602
9.15k
    if ((ret = ossl_ec_key_new_method_int(src->libctx, src->propq,
603
9.15k
             src->engine))
604
9.15k
        == NULL)
605
0
        return NULL;
606
607
    /* copy the parameters */
608
9.15k
    if (src->group != NULL
609
9.15k
        && (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
610
9.15k
        ret->group = ossl_ec_group_new_ex(src->libctx, src->propq,
611
9.15k
            src->group->meth);
612
9.15k
        if (ret->group == NULL
613
9.15k
            || !EC_GROUP_copy(ret->group, src->group))
614
0
            goto err;
615
616
9.15k
        if (src->meth != NULL)
617
9.15k
            ret->meth = src->meth;
618
9.15k
    }
619
620
    /*  copy the public key */
621
9.15k
    if (src->pub_key != NULL
622
8.53k
        && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
623
4.36k
        if (ret->group == NULL)
624
            /* no parameter-less keys allowed */
625
0
            goto err;
626
4.36k
        ret->pub_key = EC_POINT_new(ret->group);
627
4.36k
        if (ret->pub_key == NULL
628
4.36k
            || !EC_POINT_copy(ret->pub_key, src->pub_key))
629
0
            goto err;
630
4.36k
    }
631
632
    /* copy the private key */
633
9.15k
    if (src->priv_key != NULL
634
7.85k
        && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
635
3.67k
        if (ret->group == NULL)
636
            /* no parameter-less keys allowed */
637
0
            goto err;
638
3.67k
        ret->priv_key = BN_new();
639
3.67k
        if (ret->priv_key == NULL || !BN_copy(ret->priv_key, src->priv_key))
640
0
            goto err;
641
3.67k
        if (ret->group->meth->keycopy
642
0
            && ret->group->meth->keycopy(ret, src) == 0)
643
0
            goto err;
644
3.67k
    }
645
646
    /* copy the rest */
647
9.15k
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
648
4.97k
        ret->enc_flag = src->enc_flag;
649
4.97k
        ret->conv_form = src->conv_form;
650
4.97k
    }
651
652
9.15k
    ret->version = src->version;
653
9.15k
    ret->flags = src->flags;
654
655
9.15k
#ifndef FIPS_MODULE
656
9.15k
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
657
9.15k
            &ret->ex_data, &src->ex_data))
658
0
        goto err;
659
9.15k
#endif
660
661
9.15k
    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
9.15k
    return ret;
671
0
err:
672
0
    EC_KEY_free(ret);
673
0
    return NULL;
674
9.15k
}
675
676
int ossl_ec_encoding_param2id(const OSSL_PARAM *p, int *id)
677
102k
{
678
102k
    const char *name = NULL;
679
102k
    int status = 0;
680
681
102k
    switch (p->data_type) {
682
102k
    case OSSL_PARAM_UTF8_STRING:
683
        /* The OSSL_PARAM functions have no support for this */
684
102k
        name = p->data;
685
102k
        status = (name != NULL);
686
102k
        break;
687
0
    case OSSL_PARAM_UTF8_PTR:
688
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
689
0
        break;
690
102k
    }
691
102k
    if (status) {
692
102k
        int i = ossl_ec_encoding_name2id(name);
693
694
102k
        if (i >= 0) {
695
102k
            *id = i;
696
102k
            return 1;
697
102k
        }
698
102k
    }
699
0
    return 0;
700
102k
}
701
702
int ossl_ec_pt_format_param2id(const OSSL_PARAM *p, int *id)
703
205k
{
704
205k
    const char *name = NULL;
705
205k
    int status = 0;
706
707
205k
    switch (p->data_type) {
708
205k
    case OSSL_PARAM_UTF8_STRING:
709
        /* The OSSL_PARAM functions have no support for this */
710
205k
        name = p->data;
711
205k
        status = (name != NULL);
712
205k
        break;
713
0
    case OSSL_PARAM_UTF8_PTR:
714
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
715
0
        break;
716
205k
    }
717
205k
    if (status) {
718
205k
        int i = ossl_ec_pt_format_name2id(name);
719
720
205k
        if (i >= 0) {
721
205k
            *id = i;
722
205k
            return 1;
723
205k
        }
724
205k
    }
725
0
    return 0;
726
205k
}
727
728
#ifndef FIPS_MODULE
729
int ossl_x509_algor_is_sm2(const X509_ALGOR *palg)
730
641k
{
731
641k
    int ptype = 0;
732
641k
    const void *pval = NULL;
733
734
641k
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
735
736
641k
    if (ptype == V_ASN1_OBJECT)
737
598k
        return OBJ_obj2nid((ASN1_OBJECT *)pval) == NID_sm2;
738
739
42.9k
    if (ptype == V_ASN1_SEQUENCE) {
740
32.3k
        const ASN1_STRING *str = pval;
741
32.3k
        const unsigned char *der = str->data;
742
32.3k
        int derlen = str->length;
743
32.3k
        EC_GROUP *group;
744
32.3k
        int ret;
745
746
32.3k
        if ((group = d2i_ECPKParameters(NULL, &der, derlen)) == NULL)
747
29.1k
            ret = 0;
748
3.20k
        else
749
3.20k
            ret = (EC_GROUP_get_curve_name(group) == NID_sm2);
750
751
32.3k
        EC_GROUP_free(group);
752
32.3k
        return ret;
753
32.3k
    }
754
755
10.5k
    return 0;
756
42.9k
}
757
758
EC_KEY *ossl_ec_key_param_from_x509_algor(const X509_ALGOR *palg,
759
    OSSL_LIB_CTX *libctx, const char *propq)
760
640k
{
761
640k
    int ptype = 0;
762
640k
    const void *pval = NULL;
763
640k
    EC_KEY *eckey = NULL;
764
640k
    EC_GROUP *group = NULL;
765
766
640k
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
767
640k
    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
640k
    if (ptype == V_ASN1_SEQUENCE) {
773
32.6k
        const ASN1_STRING *pstr = pval;
774
32.6k
        const unsigned char *pm = pstr->data;
775
32.6k
        int pmlen = pstr->length;
776
777
32.6k
        if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) {
778
29.2k
            ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
779
29.2k
            goto ecerr;
780
29.2k
        }
781
607k
    } else if (ptype == V_ASN1_OBJECT) {
782
597k
        const ASN1_OBJECT *poid = pval;
783
784
        /*
785
         * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
786
         */
787
788
597k
        group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid));
789
597k
        if (group == NULL)
790
39.7k
            goto ecerr;
791
557k
        EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
792
557k
        if (EC_KEY_set_group(eckey, group) == 0)
793
0
            goto ecerr;
794
557k
        EC_GROUP_free(group);
795
557k
    } else {
796
10.3k
        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
797
10.3k
        goto ecerr;
798
10.3k
    }
799
800
560k
    return eckey;
801
802
79.4k
ecerr:
803
79.4k
    EC_KEY_free(eckey);
804
79.4k
    EC_GROUP_free(group);
805
79.4k
    return NULL;
806
640k
}
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.19k
{
811
2.19k
    const unsigned char *p = NULL;
812
2.19k
    int pklen;
813
2.19k
    EC_KEY *eckey = NULL;
814
2.19k
    const X509_ALGOR *palg;
815
816
2.19k
    if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
817
0
        return 0;
818
2.19k
    eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);
819
2.19k
    if (eckey == NULL)
820
436
        goto err;
821
822
    /* We have parameters now set private key */
823
1.76k
    if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
824
756
        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
825
756
        goto err;
826
756
    }
827
828
1.00k
    return eckey;
829
1.19k
err:
830
1.19k
    EC_KEY_free(eckey);
831
    return NULL;
832
1.76k
}
833
#endif