Coverage Report

Created: 2026-09-13 06:13

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-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
0
{
53
0
    size_t i, sz;
54
55
    /* Return the default value if there is no name */
56
0
    if (name == NULL)
57
0
        return OPENSSL_EC_NAMED_CURVE;
58
59
0
    for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
60
0
        if (OPENSSL_strcasecmp(name, encoding_nameid_map[i].ptr) == 0)
61
0
            return encoding_nameid_map[i].id;
62
0
    }
63
0
    return -1;
64
0
}
65
66
static char *ec_param_encoding_id2name(int id)
67
0
{
68
0
    size_t i, sz;
69
70
0
    for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
71
0
        if (id == (int)encoding_nameid_map[i].id)
72
0
            return encoding_nameid_map[i].ptr;
73
0
    }
74
0
    return NULL;
75
0
}
76
77
char *ossl_ec_check_group_type_id2name(int id)
78
0
{
79
0
    size_t i, sz;
80
81
0
    for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
82
0
        if (id == (int)check_group_type_nameid_map[i].id)
83
0
            return check_group_type_nameid_map[i].ptr;
84
0
    }
85
0
    return NULL;
86
0
}
87
88
static int ec_check_group_type_name2id(const char *name)
89
0
{
90
0
    size_t i, sz;
91
92
    /* Return the default value if there is no name */
93
0
    if (name == NULL)
94
0
        return 0;
95
96
0
    for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
97
0
        if (OPENSSL_strcasecmp(name, check_group_type_nameid_map[i].ptr) == 0)
98
0
            return check_group_type_nameid_map[i].id;
99
0
    }
100
0
    return -1;
101
0
}
102
103
int ossl_ec_set_check_group_type_from_name(EC_KEY *ec, const char *name)
104
0
{
105
0
    int flags = ec_check_group_type_name2id(name);
106
107
0
    if (flags == -1)
108
0
        return 0;
109
0
    EC_KEY_clear_flags(ec, EC_FLAG_CHECK_NAMED_GROUP_MASK);
110
0
    EC_KEY_set_flags(ec, flags);
111
0
    return 1;
112
0
}
113
114
static int ec_set_check_group_type_from_param(EC_KEY *ec, const OSSL_PARAM *p)
115
0
{
116
0
    const char *name = NULL;
117
0
    int status = 0;
118
119
0
    switch (p->data_type) {
120
0
    case OSSL_PARAM_UTF8_STRING:
121
0
        name = p->data;
122
0
        status = (name != NULL);
123
0
        break;
124
0
    case OSSL_PARAM_UTF8_PTR:
125
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
126
0
        break;
127
0
    }
128
0
    if (status)
129
0
        return ossl_ec_set_check_group_type_from_name(ec, name);
130
0
    return 0;
131
0
}
132
133
int ossl_ec_pt_format_name2id(const char *name)
134
0
{
135
0
    size_t i, sz;
136
137
    /* Return the default value if there is no name */
138
0
    if (name == NULL)
139
0
        return (int)POINT_CONVERSION_UNCOMPRESSED;
140
141
0
    for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
142
0
        if (OPENSSL_strcasecmp(name, format_nameid_map[i].ptr) == 0)
143
0
            return format_nameid_map[i].id;
144
0
    }
145
0
    return -1;
146
0
}
147
148
char *ossl_ec_pt_format_id2name(int id)
149
0
{
150
0
    size_t i, sz;
151
152
0
    for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
153
0
        if (id == (int)format_nameid_map[i].id)
154
0
            return format_nameid_map[i].ptr;
155
0
    }
156
0
    return NULL;
157
0
}
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
0
{
163
0
    int ret = 0, fid;
164
0
    const char *field_type;
165
0
    OSSL_PARAM *param = NULL;
166
0
    OSSL_PARAM *param_p = NULL;
167
0
    OSSL_PARAM *param_a = NULL;
168
0
    OSSL_PARAM *param_b = NULL;
169
170
0
    fid = EC_GROUP_get_field_type(group);
171
172
0
    if (fid == NID_X9_62_prime_field) {
173
0
        field_type = SN_X9_62_prime_field;
174
0
    } 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
0
        field_type = SN_X9_62_characteristic_two_field;
180
0
#endif
181
0
    } else {
182
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
183
0
        return 0;
184
0
    }
185
186
0
    if (params != NULL) {
187
0
        param_p = params->p;
188
0
        param_a = params->a;
189
0
        param_b = params->b;
190
0
    }
191
0
    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
0
    param = params == NULL ? NULL : params->order;
216
0
    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
0
    param = params == NULL ? NULL : params->field_type;
231
0
    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
0
    param = params == NULL ? NULL : params->generator;
241
0
    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
0
    param = params == NULL ? NULL : params->cofactor;
264
0
    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
0
    param = params == NULL ? NULL : params->seed;
276
0
    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
0
    ret = 1;
290
0
err:
291
0
    return ret;
292
0
}
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
0
{
299
0
    int ret = 0, curve_nid, encoding_flag;
300
0
    const char *encoding_name, *pt_form_name;
301
0
    point_conversion_form_t genform;
302
303
0
    if (group == NULL) {
304
0
        ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER);
305
0
        return 0;
306
0
    }
307
308
0
    genform = EC_GROUP_get_point_conversion_form(group);
309
0
    pt_form_name = ossl_ec_pt_format_id2name(genform);
310
0
    if (pt_form_name == NULL
311
0
        || !ossl_param_build_set_utf8_string(
312
0
            tmpl, params == NULL ? NULL : params->pt_format,
313
0
            OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, pt_form_name)) {
314
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
315
0
        return 0;
316
0
    }
317
0
    encoding_flag = EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE;
318
0
    encoding_name = ec_param_encoding_id2name(encoding_flag);
319
0
    if (encoding_name == NULL
320
0
        || !ossl_param_build_set_utf8_string(tmpl,
321
0
            params == NULL ? NULL : params->encoding,
322
0
            OSSL_PKEY_PARAM_EC_ENCODING,
323
0
            encoding_name)) {
324
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
325
0
        return 0;
326
0
    }
327
328
0
    if (!ossl_param_build_set_int(tmpl,
329
0
            params == NULL ? NULL : params->decoded,
330
0
            OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS,
331
0
            group->decoded_from_explicit_params))
332
0
        return 0;
333
334
0
    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
0
    if (tmpl == NULL || curve_nid == NID_undef)
342
0
        if (!ec_group_explicit_todata(group, tmpl, params, bnctx, genbuf))
343
0
            goto err;
344
345
0
    if (curve_nid != NID_undef) {
346
        /* Named curve */
347
0
        const char *curve_name = OSSL_EC_curve_nid2name(curve_nid);
348
349
0
        if (curve_name == NULL
350
0
            || !ossl_param_build_set_utf8_string(tmpl,
351
0
                params == NULL ? NULL : params->group_name,
352
0
                OSSL_PKEY_PARAM_GROUP_NAME,
353
0
                curve_name)) {
354
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
355
0
            goto err;
356
0
        }
357
0
    }
358
0
    ret = 1;
359
0
err:
360
0
    return ret;
361
0
}
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
0
{
368
0
    EC_PARAMS p;
369
370
0
    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
0
    return ossl_ec_group_todata_parsed(group, tmpl, NULL, libctx, propq,
377
0
        bnctx, genbuf);
378
0
}
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
0
{
387
0
    const EC_GROUP *ecg = EC_KEY_get0_group(ec);
388
0
    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
0
    if (mode < 0 || mode > 1)
397
0
        return 0;
398
399
0
    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
0
    if (BN_is_one(cofactor))
404
0
        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
0
}
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
0
{
539
0
    EC_PARAMS p;
540
541
0
    if (!ec_key_fromdata_decoder(params, &p))
542
0
        return 0;
543
0
    return ossl_ec_key_fromdata_parsed(ec, &p, include_private);
544
0
}
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
0
{
577
0
    int format = -1;
578
579
0
    if (params->pt_format != NULL) {
580
0
        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
0
        EC_KEY_set_conv_form(ec, format);
585
0
    }
586
0
    return 1;
587
0
}
588
589
static int ec_key_group_check_fromdata(EC_KEY *ec, const EC_PARAMS *params)
590
0
{
591
0
    if (params->group_check != NULL)
592
0
        return ec_set_check_group_type_from_param(ec, params->group_check);
593
0
    return 1;
594
0
}
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
0
{
611
0
    if (ec == NULL || params == NULL)
612
0
        return 0;
613
614
0
    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
0
    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
0
    if (!ec_key_point_format_fromdata(ec, params))
630
0
        return 0;
631
0
    if (!ec_key_group_check_fromdata(ec, params))
632
0
        return 0;
633
0
    return 1;
634
0
}
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
0
{
647
0
#ifndef FIPS_MODULE
648
0
    if (EC_KEY_get_method(ec) != EC_KEY_OpenSSL())
649
0
        return 1;
650
0
#endif
651
0
    return 0;
652
0
}
653
654
EC_KEY *ossl_ec_key_dup(const EC_KEY *src, int selection)
655
0
{
656
0
    EC_KEY *ret;
657
658
0
    if (src == NULL) {
659
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
660
0
        return NULL;
661
0
    }
662
663
0
    if ((ret = ossl_ec_key_new_method_int(src->libctx, src->propq)) == NULL)
664
0
        return NULL;
665
666
    /* copy the parameters */
667
0
    if (src->group != NULL
668
0
        && (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
669
0
        ret->group = ossl_ec_group_new_ex(src->libctx, src->propq,
670
0
            src->group->meth);
671
0
        if (ret->group == NULL
672
0
            || !EC_GROUP_copy(ret->group, src->group))
673
0
            goto err;
674
675
0
        if (src->meth != NULL)
676
0
            ret->meth = src->meth;
677
0
    }
678
679
    /*  copy the public key */
680
0
    if (src->pub_key != NULL
681
0
        && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
682
0
        if (ret->group == NULL)
683
            /* no parameter-less keys allowed */
684
0
            goto err;
685
0
        ret->pub_key = EC_POINT_new(ret->group);
686
0
        if (ret->pub_key == NULL
687
0
            || !EC_POINT_copy(ret->pub_key, src->pub_key))
688
0
            goto err;
689
0
    }
690
691
    /* copy the private key */
692
0
    if (src->priv_key != NULL
693
0
        && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
694
0
        if (ret->group == NULL)
695
            /* no parameter-less keys allowed */
696
0
            goto err;
697
0
        ret->priv_key = BN_new();
698
0
        if (ret->priv_key == NULL || BN_copy(ret->priv_key, src->priv_key) == NULL)
699
0
            goto err;
700
0
        if (ret->group->meth->keycopy
701
0
            && ret->group->meth->keycopy(ret, src) == 0)
702
0
            goto err;
703
0
    }
704
705
    /* copy the rest */
706
0
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
707
0
        ret->enc_flag = src->enc_flag;
708
0
    }
709
710
0
    ret->version = src->version;
711
0
    ret->flags = src->flags;
712
713
0
#ifndef FIPS_MODULE
714
0
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
715
0
            &ret->ex_data, &src->ex_data))
716
0
        goto err;
717
0
#endif
718
719
0
    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
0
    return ret;
729
0
err:
730
0
    EC_KEY_free(ret);
731
0
    return NULL;
732
0
}
733
734
int ossl_ec_encoding_param2id(const OSSL_PARAM *p, int *id)
735
0
{
736
0
    const char *name = NULL;
737
0
    int status = 0;
738
739
0
    switch (p->data_type) {
740
0
    case OSSL_PARAM_UTF8_STRING:
741
        /* The OSSL_PARAM functions have no support for this */
742
0
        name = p->data;
743
0
        status = (name != NULL);
744
0
        break;
745
0
    case OSSL_PARAM_UTF8_PTR:
746
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
747
0
        break;
748
0
    }
749
0
    if (status) {
750
0
        int i = ossl_ec_encoding_name2id(name);
751
752
0
        if (i >= 0) {
753
0
            *id = i;
754
0
            return 1;
755
0
        }
756
0
    }
757
0
    return 0;
758
0
}
759
760
int ossl_ec_pt_format_param2id(const OSSL_PARAM *p, int *id)
761
0
{
762
0
    const char *name = NULL;
763
0
    int status = 0;
764
765
0
    switch (p->data_type) {
766
0
    case OSSL_PARAM_UTF8_STRING:
767
        /* The OSSL_PARAM functions have no support for this */
768
0
        name = p->data;
769
0
        status = (name != NULL);
770
0
        break;
771
0
    case OSSL_PARAM_UTF8_PTR:
772
0
        status = OSSL_PARAM_get_utf8_ptr(p, &name);
773
0
        break;
774
0
    }
775
0
    if (status) {
776
0
        int i = ossl_ec_pt_format_name2id(name);
777
778
0
        if (i >= 0) {
779
0
            *id = i;
780
0
            return 1;
781
0
        }
782
0
    }
783
0
    return 0;
784
0
}
785
786
#ifndef FIPS_MODULE
787
int ossl_x509_algor_is_sm2(const X509_ALGOR *palg)
788
0
{
789
0
    int ptype = 0;
790
0
    const void *pval = NULL;
791
792
0
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
793
794
0
    if (ptype == V_ASN1_OBJECT)
795
0
        return OBJ_obj2nid((ASN1_OBJECT *)pval) == NID_sm2;
796
797
0
    if (ptype == V_ASN1_SEQUENCE) {
798
0
        const ASN1_STRING *str = pval;
799
0
        const unsigned char *der = str->data;
800
0
        int derlen = str->length;
801
0
        EC_GROUP *group;
802
0
        int ret;
803
804
0
        if ((group = d2i_ECPKParameters(NULL, &der, derlen)) == NULL)
805
0
            ret = 0;
806
0
        else
807
0
            ret = (EC_GROUP_get_curve_name(group) == NID_sm2);
808
809
0
        EC_GROUP_free(group);
810
0
        return ret;
811
0
    }
812
813
0
    return 0;
814
0
}
815
816
EC_KEY *ossl_ec_key_param_from_x509_algor(const X509_ALGOR *palg,
817
    OSSL_LIB_CTX *libctx, const char *propq)
818
0
{
819
0
    int ptype = 0;
820
0
    const void *pval = NULL;
821
0
    EC_KEY *eckey = NULL;
822
0
    EC_GROUP *group = NULL;
823
824
0
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
825
0
    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
0
    if (ptype == V_ASN1_SEQUENCE) {
831
0
        const ASN1_STRING *pstr = pval;
832
0
        const unsigned char *pm = pstr->data;
833
0
        int pmlen = pstr->length;
834
835
0
        if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) {
836
0
            ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
837
0
            goto ecerr;
838
0
        }
839
0
    } else if (ptype == V_ASN1_OBJECT) {
840
0
        const ASN1_OBJECT *poid = pval;
841
842
        /*
843
         * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
844
         */
845
846
0
        group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid));
847
0
        if (group == NULL)
848
0
            goto ecerr;
849
0
        EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
850
0
        if (EC_KEY_set_group(eckey, group) == 0)
851
0
            goto ecerr;
852
0
        EC_GROUP_free(group);
853
0
    } else {
854
0
        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
855
0
        goto ecerr;
856
0
    }
857
858
0
    return eckey;
859
860
0
ecerr:
861
0
    EC_KEY_free(eckey);
862
0
    EC_GROUP_free(group);
863
0
    return NULL;
864
0
}
865
866
EC_KEY *ossl_ec_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
867
    OSSL_LIB_CTX *libctx, const char *propq)
868
0
{
869
0
    const unsigned char *p = NULL;
870
0
    int pklen;
871
0
    EC_KEY *eckey = NULL;
872
0
    const X509_ALGOR *palg;
873
874
0
    if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
875
0
        return 0;
876
0
    eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);
877
0
    if (eckey == NULL)
878
0
        goto err;
879
880
    /* We have parameters now set private key */
881
0
    if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
882
0
        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
883
0
        goto err;
884
0
    }
885
886
0
    return eckey;
887
0
err:
888
0
    EC_KEY_free(eckey);
889
    return NULL;
890
0
}
891
#endif