Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/ec/ec_ameth.c
Line
Count
Source
1
/*
2
 * Copyright 2006-2025 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
 * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
12
 * for internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include "internal/cryptlib.h"
18
#include <openssl/x509.h>
19
#include <openssl/ec.h>
20
#include <openssl/bn.h>
21
#include <openssl/asn1t.h>
22
#include "crypto/asn1.h"
23
#include "crypto/evp.h"
24
#include "crypto/x509.h"
25
#include <openssl/core_names.h>
26
#include <openssl/param_build.h>
27
#include "ec_local.h"
28
29
static int eckey_param2type(int *pptype, void **ppval, const EC_KEY *ec_key)
30
0
{
31
0
    const EC_GROUP *group;
32
0
    int nid;
33
34
0
    if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
35
0
        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
36
0
        return 0;
37
0
    }
38
0
    if (EC_GROUP_get_asn1_flag(group)
39
0
        && (nid = EC_GROUP_get_curve_name(group)))
40
    /* we have a 'named curve' => just set the OID */
41
0
    {
42
0
        ASN1_OBJECT *asn1obj = OBJ_nid2obj(nid);
43
44
0
        if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
45
0
            ERR_raise(ERR_LIB_EC, EC_R_MISSING_OID);
46
0
            return 0;
47
0
        }
48
0
        *ppval = asn1obj;
49
0
        *pptype = V_ASN1_OBJECT;
50
0
    } else { /* explicit parameters */
51
52
0
        ASN1_STRING *pstr = NULL;
53
0
        pstr = ASN1_STRING_new();
54
0
        if (pstr == NULL)
55
0
            return 0;
56
0
        pstr->length = i2d_ECParameters(ec_key, &pstr->data);
57
0
        if (pstr->length <= 0) {
58
0
            ASN1_STRING_free(pstr);
59
0
            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
60
0
            return 0;
61
0
        }
62
0
        *ppval = pstr;
63
0
        *pptype = V_ASN1_SEQUENCE;
64
0
    }
65
0
    return 1;
66
0
}
67
68
static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
69
0
{
70
0
    const EC_KEY *ec_key = pkey->pkey.ec;
71
0
    void *pval = NULL;
72
0
    int ptype;
73
0
    unsigned char *penc = NULL, *p;
74
0
    int penclen;
75
76
0
    if (!eckey_param2type(&ptype, &pval, ec_key)) {
77
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
78
0
        return 0;
79
0
    }
80
0
    penclen = i2o_ECPublicKey(ec_key, NULL);
81
0
    if (penclen <= 0)
82
0
        goto err;
83
0
    penc = OPENSSL_malloc(penclen);
84
0
    if (penc == NULL)
85
0
        goto err;
86
0
    p = penc;
87
0
    penclen = i2o_ECPublicKey(ec_key, &p);
88
0
    if (penclen <= 0)
89
0
        goto err;
90
0
    if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
91
0
            ptype, pval, penc, penclen))
92
0
        return 1;
93
0
err:
94
0
    if (ptype == V_ASN1_SEQUENCE)
95
0
        ASN1_STRING_free(pval);
96
0
    OPENSSL_free(penc);
97
0
    return 0;
98
0
}
99
100
static int eckey_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
101
0
{
102
0
    const unsigned char *p = NULL;
103
0
    int pklen;
104
0
    EC_KEY *eckey = NULL;
105
0
    X509_ALGOR *palg;
106
0
    OSSL_LIB_CTX *libctx = NULL;
107
0
    const char *propq = NULL;
108
109
0
    if (!ossl_x509_PUBKEY_get0_libctx(&libctx, &propq, pubkey)
110
0
        || !X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
111
0
        return 0;
112
0
    eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);
113
114
0
    if (!eckey)
115
0
        return 0;
116
117
    /* We have parameters now set public key */
118
0
    if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
119
0
        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
120
0
        goto ecerr;
121
0
    }
122
123
0
    EVP_PKEY_assign_EC_KEY(pkey, eckey);
124
0
    return 1;
125
126
0
ecerr:
127
0
    EC_KEY_free(eckey);
128
0
    return 0;
129
0
}
130
131
static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
132
0
{
133
0
    int r;
134
0
    const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
135
0
    const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
136
0
                   *pb = EC_KEY_get0_public_key(b->pkey.ec);
137
138
0
    if (group == NULL || pa == NULL || pb == NULL)
139
0
        return -2;
140
0
    r = EC_POINT_cmp(group, pa, pb, NULL);
141
0
    if (r == 0)
142
0
        return 1;
143
0
    if (r == 1)
144
0
        return 0;
145
0
    return -2;
146
0
}
147
148
static int eckey_priv_decode_ex(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8,
149
    OSSL_LIB_CTX *libctx, const char *propq)
150
0
{
151
0
    int ret = 0;
152
0
    EC_KEY *eckey = ossl_ec_key_from_pkcs8(p8, libctx, propq);
153
154
0
    if (eckey != NULL) {
155
0
        ret = 1;
156
0
        EVP_PKEY_assign_EC_KEY(pkey, eckey);
157
0
    }
158
159
0
    return ret;
160
0
}
161
162
static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
163
0
{
164
0
    EC_KEY ec_key = *(pkey->pkey.ec);
165
0
    unsigned char *ep = NULL;
166
0
    int eplen, ptype;
167
0
    void *pval;
168
0
    unsigned int old_flags;
169
170
0
    if (!eckey_param2type(&ptype, &pval, &ec_key)) {
171
0
        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
172
0
        return 0;
173
0
    }
174
175
    /* set the private key */
176
177
    /*
178
     * do not include the parameters in the SEC1 private key see PKCS#11
179
     * 12.11
180
     */
181
0
    old_flags = EC_KEY_get_enc_flags(&ec_key);
182
0
    EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS);
183
184
0
    eplen = i2d_ECPrivateKey(&ec_key, &ep);
185
0
    if (eplen <= 0) {
186
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
187
0
        goto err;
188
0
    }
189
190
0
    if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
191
0
            ptype, pval, ep, eplen)) {
192
0
        ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
193
0
        OPENSSL_clear_free(ep, eplen);
194
0
        goto err;
195
0
    }
196
197
0
    return 1;
198
199
0
err:
200
0
    if (ptype == V_ASN1_SEQUENCE)
201
0
        ASN1_STRING_free(pval);
202
0
    return 0;
203
0
}
204
205
static int int_ec_size(const EVP_PKEY *pkey)
206
0
{
207
0
    return ECDSA_size(pkey->pkey.ec);
208
0
}
209
210
static int ec_bits(const EVP_PKEY *pkey)
211
0
{
212
0
    return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
213
0
}
214
215
static int ec_security_bits(const EVP_PKEY *pkey)
216
0
{
217
0
    return EC_GROUP_security_bits(EC_KEY_get0_group(pkey->pkey.ec));
218
0
}
219
220
static int ec_missing_parameters(const EVP_PKEY *pkey)
221
0
{
222
0
    if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
223
0
        return 1;
224
0
    return 0;
225
0
}
226
227
static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
228
0
{
229
0
    EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
230
231
0
    if (group == NULL)
232
0
        return 0;
233
0
    if (to->pkey.ec == NULL) {
234
0
        to->pkey.ec = EC_KEY_new();
235
0
        if (to->pkey.ec == NULL)
236
0
            goto err;
237
0
    }
238
0
    if (EC_KEY_set_group(to->pkey.ec, group) == 0)
239
0
        goto err;
240
0
    EC_GROUP_free(group);
241
0
    return 1;
242
0
err:
243
0
    EC_GROUP_free(group);
244
0
    return 0;
245
0
}
246
247
static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
248
0
{
249
0
    const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
250
0
                   *group_b = EC_KEY_get0_group(b->pkey.ec);
251
252
0
    if (group_a == NULL || group_b == NULL)
253
0
        return -2;
254
0
    if (EC_GROUP_cmp(group_a, group_b, NULL))
255
0
        return 0;
256
0
    else
257
0
        return 1;
258
0
}
259
260
static void int_ec_free(EVP_PKEY *pkey)
261
0
{
262
0
    EC_KEY_free(pkey->pkey.ec);
263
0
}
264
265
typedef enum {
266
    EC_KEY_PRINT_PRIVATE,
267
    EC_KEY_PRINT_PUBLIC,
268
    EC_KEY_PRINT_PARAM
269
} ec_print_t;
270
271
static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
272
0
{
273
0
    const char *ecstr;
274
0
    unsigned char *priv = NULL, *pub = NULL;
275
0
    size_t privlen = 0, publen = 0;
276
0
    int ret = 0;
277
0
    const EC_GROUP *group;
278
279
0
    if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
280
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
281
0
        return 0;
282
0
    }
283
284
0
    if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) {
285
0
        publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
286
0
        if (publen == 0)
287
0
            goto err;
288
0
    }
289
290
0
    if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
291
0
        privlen = EC_KEY_priv2buf(x, &priv);
292
0
        if (privlen == 0)
293
0
            goto err;
294
0
    }
295
296
0
    if (ktype == EC_KEY_PRINT_PRIVATE)
297
0
        ecstr = "Private-Key";
298
0
    else if (ktype == EC_KEY_PRINT_PUBLIC)
299
0
        ecstr = "Public-Key";
300
0
    else
301
0
        ecstr = "ECDSA-Parameters";
302
303
0
    if (!BIO_indent(bp, off, 128))
304
0
        goto err;
305
0
    if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
306
0
            EC_GROUP_order_bits(group))
307
0
        <= 0)
308
0
        goto err;
309
310
0
    if (privlen != 0) {
311
0
        if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
312
0
            goto err;
313
0
        if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
314
0
            goto err;
315
0
    }
316
317
0
    if (publen != 0) {
318
0
        if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
319
0
            goto err;
320
0
        if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
321
0
            goto err;
322
0
    }
323
324
0
    if (!ECPKParameters_print(bp, group, off))
325
0
        goto err;
326
0
    ret = 1;
327
0
err:
328
0
    if (!ret)
329
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
330
0
    OPENSSL_clear_free(priv, privlen);
331
0
    OPENSSL_free(pub);
332
0
    return ret;
333
0
}
334
335
static int eckey_param_decode(EVP_PKEY *pkey,
336
    const unsigned char **pder, int derlen)
337
0
{
338
0
    EC_KEY *eckey;
339
340
0
    if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL)
341
0
        return 0;
342
0
    EVP_PKEY_assign_EC_KEY(pkey, eckey);
343
0
    return 1;
344
0
}
345
346
static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
347
0
{
348
0
    return i2d_ECParameters(pkey->pkey.ec, pder);
349
0
}
350
351
static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
352
    ASN1_PCTX *ctx)
353
0
{
354
0
    return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
355
0
}
356
357
static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
358
    ASN1_PCTX *ctx)
359
0
{
360
0
    return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
361
0
}
362
363
static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
364
    ASN1_PCTX *ctx)
365
0
{
366
0
    return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
367
0
}
368
369
static int old_ec_priv_decode(EVP_PKEY *pkey,
370
    const unsigned char **pder, int derlen)
371
0
{
372
0
    EC_KEY *ec;
373
374
0
    if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL)
375
0
        return 0;
376
0
    EVP_PKEY_assign_EC_KEY(pkey, ec);
377
0
    return 1;
378
0
}
379
380
static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
381
0
{
382
0
    return i2d_ECPrivateKey(pkey->pkey.ec, pder);
383
0
}
384
385
static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
386
0
{
387
0
    switch (op) {
388
0
    case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
389
0
        if (EVP_PKEY_get_id(pkey) == EVP_PKEY_SM2) {
390
            /* For SM2, the only valid digest-alg is SM3 */
391
0
            *(int *)arg2 = NID_sm3;
392
0
            return 2; /* Make it mandatory */
393
0
        }
394
0
        *(int *)arg2 = NID_sha256;
395
0
        return 1;
396
397
0
    case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
398
        /* We should only be here if we have a legacy key */
399
0
        if (!ossl_assert(evp_pkey_is_legacy(pkey)))
400
0
            return 0;
401
0
        return EC_KEY_oct2key(evp_pkey_get0_EC_KEY_int(pkey), arg2, arg1, NULL);
402
403
0
    case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
404
0
        return (int)EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
405
0
            POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
406
407
0
    default:
408
0
        return -2;
409
0
    }
410
0
}
411
412
static int ec_pkey_check(const EVP_PKEY *pkey)
413
0
{
414
0
    EC_KEY *eckey = pkey->pkey.ec;
415
416
    /* stay consistent to what EVP_PKEY_check demands */
417
0
    if (eckey->priv_key == NULL) {
418
0
        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
419
0
        return 0;
420
0
    }
421
422
0
    return EC_KEY_check_key(eckey);
423
0
}
424
425
static int ec_pkey_public_check(const EVP_PKEY *pkey)
426
0
{
427
0
    EC_KEY *eckey = pkey->pkey.ec;
428
429
    /*
430
     * Note: it unnecessary to check eckey->pub_key here since
431
     * it will be checked in EC_KEY_check_key(). In fact, the
432
     * EC_KEY_check_key() mainly checks the public key, and checks
433
     * the private key optionally (only if there is one). So if
434
     * someone passes a whole EC key (public + private), this
435
     * will also work...
436
     */
437
438
0
    return EC_KEY_check_key(eckey);
439
0
}
440
441
static int ec_pkey_param_check(const EVP_PKEY *pkey)
442
0
{
443
0
    EC_KEY *eckey = pkey->pkey.ec;
444
445
    /* stay consistent to what EVP_PKEY_check demands */
446
0
    if (eckey->group == NULL) {
447
0
        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
448
0
        return 0;
449
0
    }
450
451
0
    return EC_GROUP_check(eckey->group, NULL);
452
0
}
453
454
static size_t ec_pkey_dirty_cnt(const EVP_PKEY *pkey)
455
0
{
456
0
    return pkey->pkey.ec->dirty_cnt;
457
0
}
458
459
static int ec_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
460
    OSSL_FUNC_keymgmt_import_fn *importer,
461
    OSSL_LIB_CTX *libctx, const char *propq)
462
0
{
463
0
    const EC_KEY *eckey = NULL;
464
0
    const EC_GROUP *ecg = NULL;
465
0
    unsigned char *pub_key_buf = NULL, *gen_buf = NULL;
466
0
    size_t pub_key_buflen;
467
0
    OSSL_PARAM_BLD *tmpl;
468
0
    OSSL_PARAM *params = NULL;
469
0
    const BIGNUM *priv_key = NULL;
470
0
    const EC_POINT *pub_point = NULL;
471
0
    int selection = 0;
472
0
    int rv = 0;
473
0
    BN_CTX *bnctx = NULL;
474
475
0
    if (from == NULL
476
0
        || (eckey = from->pkey.ec) == NULL
477
0
        || (ecg = EC_KEY_get0_group(eckey)) == NULL)
478
0
        return 0;
479
480
0
    tmpl = OSSL_PARAM_BLD_new();
481
0
    if (tmpl == NULL)
482
0
        return 0;
483
484
    /*
485
     * EC_POINT_point2buf() can generate random numbers in some
486
     * implementations so we need to ensure we use the correct libctx.
487
     */
488
0
    bnctx = BN_CTX_new_ex(libctx);
489
0
    if (bnctx == NULL)
490
0
        goto err;
491
0
    BN_CTX_start(bnctx);
492
493
    /* export the domain parameters */
494
0
    if (!ossl_ec_group_todata(ecg, tmpl, NULL, libctx, propq, bnctx, &gen_buf))
495
0
        goto err;
496
0
    selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
497
498
0
    priv_key = EC_KEY_get0_private_key(eckey);
499
0
    pub_point = EC_KEY_get0_public_key(eckey);
500
501
0
    if (pub_point != NULL) {
502
        /* convert pub_point to a octet string according to the SECG standard */
503
0
        point_conversion_form_t format = EC_KEY_get_conv_form(eckey);
504
505
0
        if ((pub_key_buflen = EC_POINT_point2buf(ecg, pub_point,
506
0
                 format,
507
0
                 &pub_key_buf, bnctx))
508
0
                == 0
509
0
            || !OSSL_PARAM_BLD_push_octet_string(tmpl,
510
0
                OSSL_PKEY_PARAM_PUB_KEY,
511
0
                pub_key_buf,
512
0
                pub_key_buflen))
513
0
            goto err;
514
0
        selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
515
0
    }
516
517
0
    if (priv_key != NULL) {
518
0
        size_t sz;
519
0
        int ecbits;
520
0
        int ecdh_cofactor_mode;
521
522
        /*
523
         * Key import/export should never leak the bit length of the secret
524
         * scalar in the key.
525
         *
526
         * For this reason, on export we use padded BIGNUMs with fixed length.
527
         *
528
         * When importing we also should make sure that, even if short lived,
529
         * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
530
         * soon as possible, so that any processing of this BIGNUM might opt for
531
         * constant time implementations in the backend.
532
         *
533
         * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
534
         * to preallocate the BIGNUM internal buffer to a fixed public size big
535
         * enough that operations performed during the processing never trigger
536
         * a realloc which would leak the size of the scalar through memory
537
         * accesses.
538
         *
539
         * Fixed Length
540
         * ------------
541
         *
542
         * The order of the large prime subgroup of the curve is our choice for
543
         * a fixed public size, as that is generally the upper bound for
544
         * generating a private key in EC cryptosystems and should fit all valid
545
         * secret scalars.
546
         *
547
         * For padding on export we just use the bit length of the order
548
         * converted to bytes (rounding up).
549
         *
550
         * For preallocating the BIGNUM storage we look at the number of "words"
551
         * required for the internal representation of the order, and we
552
         * preallocate 2 extra "words" in case any of the subsequent processing
553
         * might temporarily overflow the order length.
554
         */
555
0
        ecbits = EC_GROUP_order_bits(ecg);
556
0
        if (ecbits <= 0)
557
0
            goto err;
558
559
0
        sz = (ecbits + 7) / 8;
560
0
        if (!OSSL_PARAM_BLD_push_BN_pad(tmpl,
561
0
                OSSL_PKEY_PARAM_PRIV_KEY,
562
0
                priv_key, sz))
563
0
            goto err;
564
0
        selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
565
566
        /*
567
         * The ECDH Cofactor Mode is defined only if the EC_KEY actually
568
         * contains a private key, so we check for the flag and export it only
569
         * in this case.
570
         */
571
0
        ecdh_cofactor_mode = (EC_KEY_get_flags(eckey) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
572
573
        /* Export the ECDH_COFACTOR_MODE parameter */
574
0
        if (!OSSL_PARAM_BLD_push_int(tmpl,
575
0
                OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
576
0
                ecdh_cofactor_mode))
577
0
            goto err;
578
0
        selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
579
0
    }
580
581
0
    params = OSSL_PARAM_BLD_to_param(tmpl);
582
583
    /* We export, the provider imports */
584
0
    rv = importer(to_keydata, selection, params);
585
586
0
err:
587
0
    OSSL_PARAM_BLD_free(tmpl);
588
0
    OSSL_PARAM_free(params);
589
0
    OPENSSL_free(pub_key_buf);
590
0
    OPENSSL_free(gen_buf);
591
0
    BN_CTX_end(bnctx);
592
0
    BN_CTX_free(bnctx);
593
0
    return rv;
594
0
}
595
596
static int ec_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
597
0
{
598
0
    EVP_PKEY_CTX *pctx = vpctx;
599
0
    EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
600
0
    EC_KEY *ec = EC_KEY_new_ex(pctx->libctx, pctx->propquery);
601
602
0
    if (ec == NULL) {
603
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
604
0
        return 0;
605
0
    }
606
607
0
    if (!ossl_ec_group_fromdata(ec, params)
608
0
        || !ossl_ec_key_otherparams_fromdata(ec, params)
609
0
        || !ossl_ec_key_fromdata(ec, params, 1)
610
0
        || !EVP_PKEY_assign_EC_KEY(pkey, ec)) {
611
0
        EC_KEY_free(ec);
612
0
        return 0;
613
0
    }
614
0
    return 1;
615
0
}
616
617
static int ec_pkey_copy(EVP_PKEY *to, EVP_PKEY *from)
618
0
{
619
0
    EC_KEY *eckey = from->pkey.ec;
620
0
    EC_KEY *dupkey = NULL;
621
0
    int ret;
622
623
0
    if (eckey != NULL) {
624
0
        dupkey = EC_KEY_dup(eckey);
625
0
        if (dupkey == NULL)
626
0
            return 0;
627
0
    } else {
628
        /* necessary to properly copy empty SM2 keys */
629
0
        return EVP_PKEY_set_type(to, from->type);
630
0
    }
631
632
0
    ret = EVP_PKEY_assign_EC_KEY(to, dupkey);
633
0
    if (!ret)
634
0
        EC_KEY_free(dupkey);
635
0
    return ret;
636
0
}
637
638
const EVP_PKEY_ASN1_METHOD ossl_eckey_asn1_meth = {
639
    EVP_PKEY_EC,
640
    EVP_PKEY_EC,
641
    0,
642
    "EC",
643
    "OpenSSL EC algorithm",
644
645
    eckey_pub_decode,
646
    eckey_pub_encode,
647
    eckey_pub_cmp,
648
    eckey_pub_print,
649
650
    NULL,
651
    eckey_priv_encode,
652
    eckey_priv_print,
653
654
    int_ec_size,
655
    ec_bits,
656
    ec_security_bits,
657
658
    eckey_param_decode,
659
    eckey_param_encode,
660
    ec_missing_parameters,
661
    ec_copy_parameters,
662
    ec_cmp_parameters,
663
    eckey_param_print,
664
    0,
665
666
    int_ec_free,
667
    ec_pkey_ctrl,
668
    old_ec_priv_decode,
669
    old_ec_priv_encode,
670
671
    0, 0, 0,
672
673
    ec_pkey_check,
674
    ec_pkey_public_check,
675
    ec_pkey_param_check,
676
677
    0, /* set_priv_key */
678
    0, /* set_pub_key */
679
    0, /* get_priv_key */
680
    0, /* get_pub_key */
681
682
    ec_pkey_dirty_cnt,
683
    ec_pkey_export_to,
684
    ec_pkey_import_from,
685
    ec_pkey_copy,
686
    eckey_priv_decode_ex
687
};
688
689
#if !defined(OPENSSL_NO_SM2)
690
const EVP_PKEY_ASN1_METHOD ossl_sm2_asn1_meth = {
691
    EVP_PKEY_SM2,
692
    EVP_PKEY_EC,
693
    ASN1_PKEY_ALIAS
694
};
695
#endif
696
697
int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
698
0
{
699
0
    int private = EC_KEY_get0_private_key(x) != NULL;
700
701
0
    return do_EC_KEY_print(bp, x, off,
702
0
        private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
703
0
}
704
705
int ECParameters_print(BIO *bp, const EC_KEY *x)
706
0
{
707
0
    return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
708
0
}