Coverage Report

Created: 2025-08-28 07:07

/src/openssl/crypto/ec/ec_backend.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020-2024 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
87.4k
{
51
87.4k
    size_t i, sz;
52
53
    /* Return the default value if there is no name */
54
87.4k
    if (name == NULL)
55
0
        return OPENSSL_EC_NAMED_CURVE;
56
57
174k
    for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
58
174k
        if (OPENSSL_strcasecmp(name, encoding_nameid_map[i].ptr) == 0)
59
87.4k
            return encoding_nameid_map[i].id;
60
174k
    }
61
0
    return -1;
62
87.4k
}
63
64
static char *ec_param_encoding_id2name(int id)
65
460k
{
66
460k
    size_t i, sz;
67
68
918k
    for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
69
918k
        if (id == (int)encoding_nameid_map[i].id)
70
460k
            return encoding_nameid_map[i].ptr;
71
918k
    }
72
0
    return NULL;
73
460k
}
74
75
char *ossl_ec_check_group_type_id2name(int id)
76
416k
{
77
416k
    size_t i, sz;
78
79
416k
    for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
80
416k
        if (id == (int)check_group_type_nameid_map[i].id)
81
416k
            return check_group_type_nameid_map[i].ptr;
82
416k
    }
83
0
    return NULL;
84
416k
}
85
86
static int ec_check_group_type_name2id(const char *name)
87
43.7k
{
88
43.7k
    size_t i, sz;
89
90
    /* Return the default value if there is no name */
91
43.7k
    if (name == NULL)
92
0
        return 0;
93
94
43.7k
    for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
95
43.7k
        if (OPENSSL_strcasecmp(name, check_group_type_nameid_map[i].ptr) == 0)
96
43.7k
            return check_group_type_nameid_map[i].id;
97
43.7k
    }
98
0
    return -1;
99
43.7k
}
100
101
int ossl_ec_set_check_group_type_from_name(EC_KEY *ec, const char *name)
102
43.7k
{
103
43.7k
    int flags = ec_check_group_type_name2id(name);
104
105
43.7k
    if (flags == -1)
106
0
        return 0;
107
43.7k
    EC_KEY_clear_flags(ec, EC_FLAG_CHECK_NAMED_GROUP_MASK);
108
43.7k
    EC_KEY_set_flags(ec, flags);
109
43.7k
    return 1;
110
43.7k
}
111
112
static int ec_set_check_group_type_from_param(EC_KEY *ec, const OSSL_PARAM *p)
113
43.7k
{
114
43.7k
    const char *name = NULL;
115
43.7k
    int status = 0;
116
117
43.7k
    switch (p->data_type) {
118
43.7k
    case OSSL_PARAM_UTF8_STRING:
119
43.7k
        name = p->data;
120
43.7k
        status = (name != NULL);
121
43.7k
        break;
122
0
    case OSSL_PARAM_UTF8_PTR:
123
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
124
0
        break;
125
43.7k
    }
126
43.7k
    if (status)
127
43.7k
        return ossl_ec_set_check_group_type_from_name(ec, name);
128
0
    return 0;
129
43.7k
}
130
131
int ossl_ec_pt_format_name2id(const char *name)
132
174k
{
133
174k
    size_t i, sz;
134
135
    /* Return the default value if there is no name */
136
174k
    if (name == NULL)
137
0
        return (int)POINT_CONVERSION_UNCOMPRESSED;
138
139
174k
    for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
140
174k
        if (OPENSSL_strcasecmp(name, format_nameid_map[i].ptr) == 0)
141
174k
            return format_nameid_map[i].id;
142
174k
    }
143
0
    return -1;
144
174k
}
145
146
char *ossl_ec_pt_format_id2name(int id)
147
877k
{
148
877k
    size_t i, sz;
149
150
1.15M
    for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
151
1.11M
        if (id == (int)format_nameid_map[i].id)
152
842k
            return format_nameid_map[i].ptr;
153
1.11M
    }
154
35.4k
    return NULL;
155
877k
}
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
373k
{
161
373k
    int ret = 0, fid;
162
373k
    const char *field_type;
163
373k
    const OSSL_PARAM *param = NULL;
164
373k
    const OSSL_PARAM *param_p = NULL;
165
373k
    const OSSL_PARAM *param_a = NULL;
166
373k
    const OSSL_PARAM *param_b = NULL;
167
168
373k
    fid = EC_GROUP_get_field_type(group);
169
170
373k
    if (fid == NID_X9_62_prime_field) {
171
265k
        field_type = SN_X9_62_prime_field;
172
265k
    } 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
107k
        field_type = SN_X9_62_characteristic_two_field;
178
107k
#endif
179
107k
    } else {
180
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
181
0
        return 0;
182
0
    }
183
184
373k
    param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
185
373k
    param_a = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
186
373k
    param_b = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
187
373k
    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
373k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
210
373k
    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
373k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
225
373k
    if (tmpl != NULL || param != NULL) {
226
88
        if (!ossl_param_build_set_utf8_string(tmpl, params,
227
88
                                              OSSL_PKEY_PARAM_EC_FIELD_TYPE,
228
88
                                              field_type)) {
229
0
            ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
230
0
            goto err;
231
0
        }
232
88
    }
233
234
373k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
235
373k
    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
373k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
258
373k
    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
373k
    param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
270
373k
    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
373k
    ret = 1;
284
373k
err:
285
373k
    return ret;
286
373k
}
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
460k
{
293
460k
    int ret = 0, curve_nid, encoding_flag;
294
460k
    const char *encoding_name, *pt_form_name;
295
460k
    point_conversion_form_t genform;
296
297
460k
    if (group == NULL) {
298
0
        ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER);
299
0
        return 0;
300
0
    }
301
302
460k
    genform = EC_GROUP_get_point_conversion_form(group);
303
460k
    pt_form_name = ossl_ec_pt_format_id2name(genform);
304
460k
    if (pt_form_name == NULL
305
460k
        || !ossl_param_build_set_utf8_string(
306
460k
                tmpl, params,
307
460k
                OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, pt_form_name)) {
308
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
309
0
        return 0;
310
0
    }
311
460k
    encoding_flag = EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE;
312
460k
    encoding_name = ec_param_encoding_id2name(encoding_flag);
313
460k
    if (encoding_name == NULL
314
460k
        || !ossl_param_build_set_utf8_string(tmpl, params,
315
460k
                                             OSSL_PKEY_PARAM_EC_ENCODING,
316
460k
                                             encoding_name)) {
317
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
318
0
        return 0;
319
0
    }
320
321
460k
    if (!ossl_param_build_set_int(tmpl, params,
322
460k
                                  OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS,
323
460k
                                  group->decoded_from_explicit_params))
324
0
        return 0;
325
326
460k
    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
460k
    if (tmpl == NULL || curve_nid == NID_undef)
334
373k
        if (!ec_group_explicit_todata(group, tmpl, params, bnctx, genbuf))
335
0
            goto err;
336
337
460k
    if (curve_nid != NID_undef) {
338
        /* Named curve */
339
458k
        const char *curve_name = OSSL_EC_curve_nid2name(curve_nid);
340
341
458k
        if (curve_name == NULL
342
458k
            || !ossl_param_build_set_utf8_string(tmpl, params,
343
458k
                                                 OSSL_PKEY_PARAM_GROUP_NAME,
344
458k
                                                 curve_name)) {
345
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
346
0
            goto err;
347
0
        }
348
458k
    }
349
460k
    ret = 1;
350
460k
err:
351
460k
    return ret;
352
460k
}
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
96.4k
{
361
96.4k
    const EC_GROUP *ecg = EC_KEY_get0_group(ec);
362
96.4k
    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
96.4k
    if (mode < 0 || mode > 1)
371
0
        return 0;
372
373
96.4k
    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
96.4k
    if (BN_is_one(cofactor))
378
96.4k
        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
96.4k
}
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
87.4k
{
398
87.4k
    const OSSL_PARAM *param_priv_key = NULL, *param_pub_key = NULL;
399
87.4k
    BN_CTX *ctx = NULL;
400
87.4k
    BIGNUM *priv_key = NULL;
401
87.4k
    unsigned char *pub_key = NULL;
402
87.4k
    size_t pub_key_len;
403
87.4k
    const EC_GROUP *ecg = NULL;
404
87.4k
    EC_POINT *pub_point = NULL;
405
87.4k
    int ok = 0;
406
407
87.4k
    ecg = EC_KEY_get0_group(ec);
408
87.4k
    if (ecg == NULL)
409
0
        return 0;
410
411
87.4k
    param_pub_key =
412
87.4k
        OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
413
87.4k
    if (include_private)
414
87.4k
        param_priv_key =
415
87.4k
            OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
416
417
87.4k
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
418
87.4k
    if (ctx == NULL)
419
0
        goto err;
420
421
87.4k
    if (param_pub_key != NULL)
422
87.4k
        if (!OSSL_PARAM_get_octet_string(param_pub_key,
423
87.4k
                                         (void **)&pub_key, 0, &pub_key_len)
424
87.4k
            || (pub_point = EC_POINT_new(ecg)) == NULL
425
87.4k
            || !EC_POINT_oct2point(ecg, pub_point, pub_key, pub_key_len, ctx))
426
0
        goto err;
427
428
87.4k
    if (param_priv_key != NULL && include_private) {
429
87.4k
        int fixed_words;
430
87.4k
        const BIGNUM *order;
431
432
        /*
433
         * Key import/export should never leak the bit length of the secret
434
         * scalar in the key.
435
         *
436
         * For this reason, on export we use padded BIGNUMs with fixed length.
437
         *
438
         * When importing we also should make sure that, even if short lived,
439
         * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
440
         * soon as possible, so that any processing of this BIGNUM might opt for
441
         * constant time implementations in the backend.
442
         *
443
         * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
444
         * to preallocate the BIGNUM internal buffer to a fixed public size big
445
         * enough that operations performed during the processing never trigger
446
         * a realloc which would leak the size of the scalar through memory
447
         * accesses.
448
         *
449
         * Fixed Length
450
         * ------------
451
         *
452
         * The order of the large prime subgroup of the curve is our choice for
453
         * a fixed public size, as that is generally the upper bound for
454
         * generating a private key in EC cryptosystems and should fit all valid
455
         * secret scalars.
456
         *
457
         * For padding on export we just use the bit length of the order
458
         * converted to bytes (rounding up).
459
         *
460
         * For preallocating the BIGNUM storage we look at the number of "words"
461
         * required for the internal representation of the order, and we
462
         * preallocate 2 extra "words" in case any of the subsequent processing
463
         * might temporarily overflow the order length.
464
         */
465
87.4k
        order = EC_GROUP_get0_order(ecg);
466
87.4k
        if (order == NULL || BN_is_zero(order))
467
0
            goto err;
468
469
87.4k
        fixed_words = bn_get_top(order) + 2;
470
471
87.4k
        if ((priv_key = BN_secure_new()) == NULL)
472
0
            goto err;
473
87.4k
        if (bn_wexpand(priv_key, fixed_words) == NULL)
474
0
            goto err;
475
87.4k
        BN_set_flags(priv_key, BN_FLG_CONSTTIME);
476
477
87.4k
        if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
478
0
            goto err;
479
87.4k
    }
480
481
87.4k
    if (priv_key != NULL
482
87.4k
        && !EC_KEY_set_private_key(ec, priv_key))
483
0
        goto err;
484
485
87.4k
    if (pub_point != NULL
486
87.4k
        && !EC_KEY_set_public_key(ec, pub_point))
487
0
        goto err;
488
489
87.4k
    ok = 1;
490
491
87.4k
 err:
492
87.4k
    BN_CTX_free(ctx);
493
87.4k
    BN_clear_free(priv_key);
494
87.4k
    OPENSSL_free(pub_key);
495
87.4k
    EC_POINT_free(pub_point);
496
87.4k
    return ok;
497
87.4k
}
498
499
int ossl_ec_group_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
500
87.4k
{
501
87.4k
    int ok = 0;
502
87.4k
    EC_GROUP *group = NULL;
503
504
87.4k
    if (ec == NULL)
505
0
        return 0;
506
507
87.4k
     group = EC_GROUP_new_from_params(params, ossl_ec_key_get_libctx(ec),
508
87.4k
                                      ossl_ec_key_get0_propq(ec));
509
510
87.4k
    if (!EC_KEY_set_group(ec, group))
511
0
        goto err;
512
87.4k
    ok = 1;
513
87.4k
err:
514
87.4k
    EC_GROUP_free(group);
515
87.4k
    return ok;
516
87.4k
}
517
518
static int ec_key_point_format_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
519
91.5k
{
520
91.5k
    const OSSL_PARAM *p;
521
91.5k
    int format = -1;
522
523
91.5k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
524
91.5k
    if (p != NULL) {
525
87.4k
        if (!ossl_ec_pt_format_param2id(p, &format)) {
526
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
527
0
            return 0;
528
0
        }
529
87.4k
        EC_KEY_set_conv_form(ec, format);
530
87.4k
    }
531
91.5k
    return 1;
532
91.5k
}
533
534
static int ec_key_group_check_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
535
91.5k
{
536
91.5k
    const OSSL_PARAM *p;
537
538
91.5k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE);
539
91.5k
    if (p != NULL)
540
43.7k
        return ec_set_check_group_type_from_param(ec, p);
541
47.8k
    return 1;
542
91.5k
}
543
544
static int ec_set_include_public(EC_KEY *ec, int include)
545
0
{
546
0
    int flags = EC_KEY_get_enc_flags(ec);
547
548
0
    if (!include)
549
0
        flags |= EC_PKEY_NO_PUBKEY;
550
0
    else
551
0
        flags &= ~EC_PKEY_NO_PUBKEY;
552
0
    EC_KEY_set_enc_flags(ec, flags);
553
0
    return 1;
554
0
}
555
556
int ossl_ec_key_otherparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
557
91.5k
{
558
91.5k
    const OSSL_PARAM *p;
559
560
91.5k
    if (ec == NULL)
561
0
        return 0;
562
563
91.5k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
564
91.5k
    if (p != NULL) {
565
87.4k
        int mode;
566
567
87.4k
        if (!OSSL_PARAM_get_int(p, &mode)
568
87.4k
            || !ossl_ec_set_ecdh_cofactor_mode(ec, mode))
569
0
            return 0;
570
87.4k
    }
571
572
91.5k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC);
573
91.5k
    if (p != NULL) {
574
0
        int include = 1;
575
576
0
        if (!OSSL_PARAM_get_int(p, &include)
577
0
            || !ec_set_include_public(ec, include))
578
0
            return 0;
579
0
    }
580
91.5k
    if (!ec_key_point_format_fromdata(ec, params))
581
0
        return 0;
582
91.5k
    if (!ec_key_group_check_fromdata(ec, params))
583
0
        return 0;
584
91.5k
    return 1;
585
91.5k
}
586
587
int ossl_ec_key_is_foreign(const EC_KEY *ec)
588
326k
{
589
326k
#ifndef FIPS_MODULE
590
326k
    if (ec->engine != NULL || EC_KEY_get_method(ec) != EC_KEY_OpenSSL())
591
0
        return 1;
592
326k
#endif
593
326k
    return 0;
594
595
326k
}
596
597
EC_KEY *ossl_ec_key_dup(const EC_KEY *src, int selection)
598
7.93k
{
599
7.93k
    EC_KEY *ret;
600
601
7.93k
    if (src == NULL) {
602
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
603
0
        return NULL;
604
0
    }
605
606
7.93k
    if ((ret = ossl_ec_key_new_method_int(src->libctx, src->propq,
607
7.93k
                                          src->engine)) == NULL)
608
0
        return NULL;
609
610
    /* copy the parameters */
611
7.93k
    if (src->group != NULL
612
7.93k
        && (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
613
7.93k
        ret->group = ossl_ec_group_new_ex(src->libctx, src->propq,
614
7.93k
                                          src->group->meth);
615
7.93k
        if (ret->group == NULL
616
7.93k
            || !EC_GROUP_copy(ret->group, src->group))
617
0
            goto err;
618
619
7.93k
        if (src->meth != NULL)
620
7.93k
            ret->meth = src->meth;
621
7.93k
    }
622
623
    /*  copy the public key */
624
7.93k
    if (src->pub_key != NULL
625
7.93k
        && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
626
3.65k
        if (ret->group == NULL)
627
            /* no parameter-less keys allowed */
628
0
            goto err;
629
3.65k
        ret->pub_key = EC_POINT_new(ret->group);
630
3.65k
        if (ret->pub_key == NULL
631
3.65k
            || !EC_POINT_copy(ret->pub_key, src->pub_key))
632
0
                goto err;
633
3.65k
    }
634
635
    /* copy the private key */
636
7.93k
    if (src->priv_key != NULL
637
7.93k
        && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
638
3.04k
        if (ret->group == NULL)
639
            /* no parameter-less keys allowed */
640
0
            goto err;
641
3.04k
        ret->priv_key = BN_new();
642
3.04k
        if (ret->priv_key == NULL || !BN_copy(ret->priv_key, src->priv_key))
643
0
            goto err;
644
3.04k
        if (ret->group->meth->keycopy
645
3.04k
            && ret->group->meth->keycopy(ret, src) == 0)
646
0
            goto err;
647
3.04k
    }
648
649
    /* copy the rest */
650
7.93k
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
651
4.27k
        ret->enc_flag = src->enc_flag;
652
4.27k
        ret->conv_form = src->conv_form;
653
4.27k
    }
654
655
7.93k
    ret->version = src->version;
656
7.93k
    ret->flags = src->flags;
657
658
7.93k
#ifndef FIPS_MODULE
659
7.93k
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
660
7.93k
                            &ret->ex_data, &src->ex_data))
661
0
        goto err;
662
7.93k
#endif
663
664
7.93k
    if (ret->meth != NULL && ret->meth->copy != NULL) {
665
0
        if ((selection
666
0
             & OSSL_KEYMGMT_SELECT_KEYPAIR) != OSSL_KEYMGMT_SELECT_KEYPAIR)
667
0
            goto err;
668
0
        if (ret->meth->copy(ret, src) == 0)
669
0
            goto err;
670
0
    }
671
672
7.93k
    return ret;
673
0
 err:
674
0
    EC_KEY_free(ret);
675
0
    return NULL;
676
7.93k
}
677
678
int ossl_ec_encoding_param2id(const OSSL_PARAM *p, int *id)
679
87.4k
{
680
87.4k
    const char *name = NULL;
681
87.4k
    int status = 0;
682
683
87.4k
    switch (p->data_type) {
684
87.4k
    case OSSL_PARAM_UTF8_STRING:
685
        /* The OSSL_PARAM functions have no support for this */
686
87.4k
        name = p->data;
687
87.4k
        status = (name != NULL);
688
87.4k
        break;
689
0
    case OSSL_PARAM_UTF8_PTR:
690
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
691
0
        break;
692
87.4k
    }
693
87.4k
    if (status) {
694
87.4k
        int i = ossl_ec_encoding_name2id(name);
695
696
87.4k
        if (i >= 0) {
697
87.4k
            *id = i;
698
87.4k
            return 1;
699
87.4k
        }
700
87.4k
    }
701
0
    return 0;
702
87.4k
}
703
704
int ossl_ec_pt_format_param2id(const OSSL_PARAM *p, int *id)
705
174k
{
706
174k
    const char *name = NULL;
707
174k
    int status = 0;
708
709
174k
    switch (p->data_type) {
710
174k
    case OSSL_PARAM_UTF8_STRING:
711
        /* The OSSL_PARAM functions have no support for this */
712
174k
        name = p->data;
713
174k
        status = (name != NULL);
714
174k
        break;
715
0
    case OSSL_PARAM_UTF8_PTR:
716
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
717
0
        break;
718
174k
    }
719
174k
    if (status) {
720
174k
        int i = ossl_ec_pt_format_name2id(name);
721
722
174k
        if (i >= 0) {
723
174k
            *id = i;
724
174k
            return 1;
725
174k
        }
726
174k
    }
727
0
    return 0;
728
174k
}
729
730
#ifndef FIPS_MODULE
731
int ossl_x509_algor_is_sm2(const X509_ALGOR *palg)
732
500k
{
733
500k
    int ptype = 0;
734
500k
    const void *pval = NULL;
735
736
500k
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
737
738
500k
    if (ptype == V_ASN1_OBJECT)
739
453k
        return OBJ_obj2nid((ASN1_OBJECT *)pval) == NID_sm2;
740
741
47.1k
    if (ptype == V_ASN1_SEQUENCE) {
742
35.8k
        const ASN1_STRING *str = pval;
743
35.8k
        const unsigned char *der = str->data;
744
35.8k
        int derlen = str->length;
745
35.8k
        EC_GROUP *group;
746
35.8k
        int ret;
747
748
35.8k
        if ((group = d2i_ECPKParameters(NULL, &der, derlen)) == NULL)
749
30.7k
            ret = 0;
750
5.01k
        else
751
5.01k
            ret = (EC_GROUP_get_curve_name(group) == NID_sm2);
752
753
35.8k
        EC_GROUP_free(group);
754
35.8k
        return ret;
755
35.8k
    }
756
757
11.2k
    return 0;
758
47.1k
}
759
760
EC_KEY *ossl_ec_key_param_from_x509_algor(const X509_ALGOR *palg,
761
                                     OSSL_LIB_CTX *libctx, const char *propq)
762
499k
{
763
499k
    int ptype = 0;
764
499k
    const void *pval = NULL;
765
499k
    EC_KEY *eckey = NULL;
766
499k
    EC_GROUP *group = NULL;
767
768
499k
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
769
499k
    if ((eckey = EC_KEY_new_ex(libctx, propq)) == NULL) {
770
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
771
0
        goto ecerr;
772
0
    }
773
774
499k
    if (ptype == V_ASN1_SEQUENCE) {
775
36.0k
        const ASN1_STRING *pstr = pval;
776
36.0k
        const unsigned char *pm = pstr->data;
777
36.0k
        int pmlen = pstr->length;
778
779
780
36.0k
        if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) {
781
30.9k
            ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
782
30.9k
            goto ecerr;
783
30.9k
        }
784
463k
    } else if (ptype == V_ASN1_OBJECT) {
785
452k
        const ASN1_OBJECT *poid = pval;
786
787
        /*
788
         * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
789
         */
790
791
452k
        group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid));
792
452k
        if (group == NULL)
793
28.1k
            goto ecerr;
794
423k
        EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
795
423k
        if (EC_KEY_set_group(eckey, group) == 0)
796
0
            goto ecerr;
797
423k
        EC_GROUP_free(group);
798
423k
    } else {
799
11.1k
        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
800
11.1k
        goto ecerr;
801
11.1k
    }
802
803
428k
    return eckey;
804
805
70.2k
 ecerr:
806
70.2k
    EC_KEY_free(eckey);
807
70.2k
    EC_GROUP_free(group);
808
70.2k
    return NULL;
809
499k
}
810
811
EC_KEY *ossl_ec_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
812
                               OSSL_LIB_CTX *libctx, const char *propq)
813
1.88k
{
814
1.88k
    const unsigned char *p = NULL;
815
1.88k
    int pklen;
816
1.88k
    EC_KEY *eckey = NULL;
817
1.88k
    const X509_ALGOR *palg;
818
819
1.88k
    if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
820
0
        return 0;
821
1.88k
    eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);
822
1.88k
    if (eckey == NULL)
823
425
        goto err;
824
825
    /* We have parameters now set private key */
826
1.45k
    if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
827
639
        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
828
639
        goto err;
829
639
    }
830
831
816
    return eckey;
832
1.06k
 err:
833
1.06k
    EC_KEY_free(eckey);
834
1.06k
    return NULL;
835
1.45k
}
836
#endif