Coverage Report

Created: 2025-08-28 07:07

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