Coverage Report

Created: 2026-09-12 06:55

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