Coverage Report

Created: 2025-12-08 06:22

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