Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/ec/ec_ameth.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/x509.h>
13
#include <openssl/ec.h>
14
#include <openssl/bn.h>
15
#include <openssl/cms.h>
16
#include <openssl/asn1t.h>
17
#include "crypto/asn1.h"
18
#include "crypto/evp.h"
19
#include "ec_local.h"
20
21
#ifndef OPENSSL_NO_CMS
22
static int ecdh_cms_decrypt(CMS_RecipientInfo *ri);
23
static int ecdh_cms_encrypt(CMS_RecipientInfo *ri);
24
#endif
25
26
static int eckey_param2type(int *pptype, void **ppval, const EC_KEY *ec_key)
27
0
{
28
0
    const EC_GROUP *group;
29
0
    int nid;
30
0
    if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
31
0
        ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
32
0
        return 0;
33
0
    }
34
0
    if (EC_GROUP_get_asn1_flag(group)
35
0
        && (nid = EC_GROUP_get_curve_name(group)))
36
        /* we have a 'named curve' => just set the OID */
37
0
    {
38
0
        ASN1_OBJECT *asn1obj = OBJ_nid2obj(nid);
39
40
0
        if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
41
0
            ASN1_OBJECT_free(asn1obj);
42
0
            ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_OID);
43
0
            return 0;
44
0
        }
45
0
        *ppval = asn1obj;
46
0
        *pptype = V_ASN1_OBJECT;
47
0
    } else {                    /* explicit parameters */
48
49
0
        ASN1_STRING *pstr = NULL;
50
0
        pstr = ASN1_STRING_new();
51
0
        if (pstr == NULL)
52
0
            return 0;
53
54
        /*
55
         * The cast in the following line is intentional as the
56
         * `i2d_ECParameters` signature can't be constified (see discussion at
57
         * https://github.com/openssl/openssl/pull/9347 where related and
58
         * required constification backports were rejected).
59
         *
60
         * This cast should be safe anyway, because we can expect
61
         * `i2d_ECParameters()` to treat the first argument as if it was const.
62
         */
63
0
        pstr->length = i2d_ECParameters((EC_KEY *)ec_key, &pstr->data);
64
0
        if (pstr->length <= 0) {
65
0
            ASN1_STRING_free(pstr);
66
0
            ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
67
0
            return 0;
68
0
        }
69
0
        *ppval = pstr;
70
0
        *pptype = V_ASN1_SEQUENCE;
71
0
    }
72
0
    return 1;
73
0
}
74
75
static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
76
0
{
77
0
    const EC_KEY *ec_key = pkey->pkey.ec;
78
0
    void *pval = NULL;
79
0
    int ptype;
80
0
    unsigned char *penc = NULL, *p;
81
0
    int penclen;
82
83
0
    if (!eckey_param2type(&ptype, &pval, ec_key)) {
84
0
        ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
85
0
        return 0;
86
0
    }
87
0
    penclen = i2o_ECPublicKey(ec_key, NULL);
88
0
    if (penclen <= 0)
89
0
        goto err;
90
0
    penc = OPENSSL_malloc(penclen);
91
0
    if (penc == NULL)
92
0
        goto err;
93
0
    p = penc;
94
0
    penclen = i2o_ECPublicKey(ec_key, &p);
95
0
    if (penclen <= 0)
96
0
        goto err;
97
0
    if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
98
0
                               ptype, pval, penc, penclen))
99
0
        return 1;
100
0
 err:
101
0
    if (ptype == V_ASN1_OBJECT)
102
0
        ASN1_OBJECT_free(pval);
103
0
    else
104
0
        ASN1_STRING_free(pval);
105
0
    OPENSSL_free(penc);
106
0
    return 0;
107
0
}
108
109
static EC_KEY *eckey_type2param(int ptype, const void *pval)
110
20.8k
{
111
20.8k
    EC_KEY *eckey = NULL;
112
20.8k
    EC_GROUP *group = NULL;
113
114
20.8k
    if (ptype == V_ASN1_SEQUENCE) {
115
4.42k
        const ASN1_STRING *pstr = pval;
116
4.42k
        const unsigned char *pm = pstr->data;
117
4.42k
        int pmlen = pstr->length;
118
119
4.42k
        if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {
120
2.59k
            ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
121
2.59k
            goto ecerr;
122
2.59k
        }
123
16.4k
    } else if (ptype == V_ASN1_OBJECT) {
124
16.1k
        const ASN1_OBJECT *poid = pval;
125
126
        /*
127
         * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
128
         */
129
16.1k
        if ((eckey = EC_KEY_new()) == NULL) {
130
0
            ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
131
0
            goto ecerr;
132
0
        }
133
16.1k
        group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
134
16.1k
        if (group == NULL)
135
754
            goto ecerr;
136
15.3k
        EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
137
15.3k
        if (EC_KEY_set_group(eckey, group) == 0)
138
0
            goto ecerr;
139
15.3k
        EC_GROUP_free(group);
140
15.3k
    } else {
141
272
        ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
142
272
        goto ecerr;
143
272
    }
144
145
17.2k
    return eckey;
146
147
3.62k
 ecerr:
148
3.62k
    EC_KEY_free(eckey);
149
3.62k
    EC_GROUP_free(group);
150
3.62k
    return NULL;
151
20.8k
}
152
153
static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
154
20.8k
{
155
20.8k
    const unsigned char *p = NULL;
156
20.8k
    const void *pval;
157
20.8k
    int ptype, pklen;
158
20.8k
    EC_KEY *eckey = NULL;
159
20.8k
    X509_ALGOR *palg;
160
161
20.8k
    if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
162
0
        return 0;
163
20.8k
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
164
165
20.8k
    eckey = eckey_type2param(ptype, pval);
166
167
20.8k
    if (!eckey) {
168
3.61k
        ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
169
3.61k
        return 0;
170
3.61k
    }
171
172
    /* We have parameters now set public key */
173
17.2k
    if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
174
9.12k
        ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
175
9.12k
        goto ecerr;
176
9.12k
    }
177
178
8.08k
    EVP_PKEY_assign_EC_KEY(pkey, eckey);
179
8.08k
    return 1;
180
181
9.12k
 ecerr:
182
9.12k
    EC_KEY_free(eckey);
183
9.12k
    return 0;
184
17.2k
}
185
186
static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
187
0
{
188
0
    int r;
189
0
    const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
190
0
    const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
191
0
        *pb = EC_KEY_get0_public_key(b->pkey.ec);
192
0
    if (group == NULL || pa == NULL || pb == NULL)
193
0
        return -2;
194
0
    r = EC_POINT_cmp(group, pa, pb, NULL);
195
0
    if (r == 0)
196
0
        return 1;
197
0
    if (r == 1)
198
0
        return 0;
199
0
    return -2;
200
0
}
201
202
static int eckey_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
203
2
{
204
2
    const unsigned char *p = NULL;
205
2
    const void *pval;
206
2
    int ptype, pklen;
207
2
    EC_KEY *eckey = NULL;
208
2
    const X509_ALGOR *palg;
209
210
2
    if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
211
0
        return 0;
212
2
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
213
214
2
    eckey = eckey_type2param(ptype, pval);
215
216
2
    if (!eckey)
217
1
        goto ecliberr;
218
219
    /* We have parameters now set private key */
220
1
    if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
221
1
        ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
222
1
        goto ecerr;
223
1
    }
224
225
0
    EVP_PKEY_assign_EC_KEY(pkey, eckey);
226
0
    return 1;
227
228
1
 ecliberr:
229
1
    ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
230
2
 ecerr:
231
2
    EC_KEY_free(eckey);
232
2
    return 0;
233
1
}
234
235
static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
236
0
{
237
0
    EC_KEY ec_key = *(pkey->pkey.ec);
238
0
    unsigned char *ep, *p;
239
0
    int eplen, ptype;
240
0
    void *pval;
241
0
    unsigned int old_flags;
242
243
0
    if (!eckey_param2type(&ptype, &pval, &ec_key)) {
244
0
        ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
245
0
        return 0;
246
0
    }
247
248
    /* set the private key */
249
250
    /*
251
     * do not include the parameters in the SEC1 private key see PKCS#11
252
     * 12.11
253
     */
254
0
    old_flags = EC_KEY_get_enc_flags(&ec_key);
255
0
    EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS);
256
257
0
    eplen = i2d_ECPrivateKey(&ec_key, NULL);
258
0
    if (!eplen) {
259
0
        ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
260
0
        return 0;
261
0
    }
262
0
    ep = OPENSSL_malloc(eplen);
263
0
    if (ep == NULL) {
264
0
        ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
265
0
        return 0;
266
0
    }
267
0
    p = ep;
268
0
    if (!i2d_ECPrivateKey(&ec_key, &p)) {
269
0
        OPENSSL_free(ep);
270
0
        ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
271
0
        return 0;
272
0
    }
273
274
0
    if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
275
0
                         ptype, pval, ep, eplen)) {
276
0
        OPENSSL_free(ep);
277
0
        return 0;
278
0
    }
279
280
0
    return 1;
281
0
}
282
283
static int int_ec_size(const EVP_PKEY *pkey)
284
0
{
285
0
    return ECDSA_size(pkey->pkey.ec);
286
0
}
287
288
static int ec_bits(const EVP_PKEY *pkey)
289
0
{
290
0
    return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
291
0
}
292
293
static int ec_security_bits(const EVP_PKEY *pkey)
294
0
{
295
0
    int ecbits = ec_bits(pkey);
296
0
    if (ecbits >= 512)
297
0
        return 256;
298
0
    if (ecbits >= 384)
299
0
        return 192;
300
0
    if (ecbits >= 256)
301
0
        return 128;
302
0
    if (ecbits >= 224)
303
0
        return 112;
304
0
    if (ecbits >= 160)
305
0
        return 80;
306
0
    return ecbits / 2;
307
0
}
308
309
static int ec_missing_parameters(const EVP_PKEY *pkey)
310
0
{
311
0
    if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
312
0
        return 1;
313
0
    return 0;
314
0
}
315
316
static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
317
0
{
318
0
    EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
319
320
0
    if (group == NULL)
321
0
        return 0;
322
0
    if (to->pkey.ec == NULL) {
323
0
        to->pkey.ec = EC_KEY_new();
324
0
        if (to->pkey.ec == NULL)
325
0
            goto err;
326
0
    }
327
0
    if (EC_KEY_set_group(to->pkey.ec, group) == 0)
328
0
        goto err;
329
0
    EC_GROUP_free(group);
330
0
    return 1;
331
0
 err:
332
0
    EC_GROUP_free(group);
333
0
    return 0;
334
0
}
335
336
static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
337
0
{
338
0
    const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
339
0
        *group_b = EC_KEY_get0_group(b->pkey.ec);
340
0
    if (group_a == NULL || group_b == NULL)
341
0
        return -2;
342
0
    if (EC_GROUP_cmp(group_a, group_b, NULL))
343
0
        return 0;
344
0
    else
345
0
        return 1;
346
0
}
347
348
static void int_ec_free(EVP_PKEY *pkey)
349
21.1k
{
350
21.1k
    EC_KEY_free(pkey->pkey.ec);
351
21.1k
}
352
353
typedef enum {
354
    EC_KEY_PRINT_PRIVATE,
355
    EC_KEY_PRINT_PUBLIC,
356
    EC_KEY_PRINT_PARAM
357
} ec_print_t;
358
359
static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
360
666
{
361
666
    const char *ecstr;
362
666
    unsigned char *priv = NULL, *pub = NULL;
363
666
    size_t privlen = 0, publen = 0;
364
666
    int ret = 0;
365
666
    const EC_GROUP *group;
366
367
666
    if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
368
0
        ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_PASSED_NULL_PARAMETER);
369
0
        return 0;
370
0
    }
371
372
666
    if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) {
373
578
        publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
374
578
        if (publen == 0)
375
0
            goto err;
376
578
    }
377
378
666
    if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
379
578
        privlen = EC_KEY_priv2buf(x, &priv);
380
578
        if (privlen == 0)
381
235
            goto err;
382
578
    }
383
384
431
    if (ktype == EC_KEY_PRINT_PRIVATE)
385
343
        ecstr = "Private-Key";
386
88
    else if (ktype == EC_KEY_PRINT_PUBLIC)
387
0
        ecstr = "Public-Key";
388
88
    else
389
88
        ecstr = "ECDSA-Parameters";
390
391
431
    if (!BIO_indent(bp, off, 128))
392
0
        goto err;
393
431
    if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
394
431
                   EC_GROUP_order_bits(group)) <= 0)
395
0
        goto err;
396
397
431
    if (privlen != 0) {
398
343
        if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
399
0
            goto err;
400
343
        if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
401
0
            goto err;
402
343
    }
403
404
431
    if (publen != 0) {
405
343
        if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
406
0
            goto err;
407
343
        if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
408
0
            goto err;
409
343
    }
410
411
431
    if (!ECPKParameters_print(bp, group, off))
412
0
        goto err;
413
431
    ret = 1;
414
666
 err:
415
666
    if (!ret)
416
666
        ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_EC_LIB);
417
666
    OPENSSL_clear_free(priv, privlen);
418
666
    OPENSSL_free(pub);
419
666
    return ret;
420
431
}
421
422
static int eckey_param_decode(EVP_PKEY *pkey,
423
                              const unsigned char **pder, int derlen)
424
0
{
425
0
    EC_KEY *eckey;
426
427
0
    if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
428
0
        ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
429
0
        return 0;
430
0
    }
431
0
    EVP_PKEY_assign_EC_KEY(pkey, eckey);
432
0
    return 1;
433
0
}
434
435
static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
436
0
{
437
0
    return i2d_ECParameters(pkey->pkey.ec, pder);
438
0
}
439
440
static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
441
                             ASN1_PCTX *ctx)
442
0
{
443
0
    return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
444
0
}
445
446
static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
447
                           ASN1_PCTX *ctx)
448
0
{
449
0
    return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
450
0
}
451
452
static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
453
                            ASN1_PCTX *ctx)
454
0
{
455
0
    return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
456
0
}
457
458
static int old_ec_priv_decode(EVP_PKEY *pkey,
459
                              const unsigned char **pder, int derlen)
460
326
{
461
326
    EC_KEY *ec;
462
463
326
    if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
464
326
        ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
465
326
        return 0;
466
326
    }
467
0
    EVP_PKEY_assign_EC_KEY(pkey, ec);
468
0
    return 1;
469
326
}
470
471
static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
472
0
{
473
0
    return i2d_ECPrivateKey(pkey->pkey.ec, pder);
474
0
}
475
476
static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
477
0
{
478
0
    switch (op) {
479
0
    case ASN1_PKEY_CTRL_PKCS7_SIGN:
480
0
        if (arg1 == 0) {
481
0
            int snid, hnid;
482
0
            X509_ALGOR *alg1, *alg2;
483
0
            PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
484
0
            if (alg1 == NULL || alg1->algorithm == NULL)
485
0
                return -1;
486
0
            hnid = OBJ_obj2nid(alg1->algorithm);
487
0
            if (hnid == NID_undef)
488
0
                return -1;
489
0
            if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
490
0
                return -1;
491
0
            X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
492
0
        }
493
0
        return 1;
494
0
#ifndef OPENSSL_NO_CMS
495
0
    case ASN1_PKEY_CTRL_CMS_SIGN:
496
0
        if (arg1 == 0) {
497
0
            int snid, hnid;
498
0
            X509_ALGOR *alg1, *alg2;
499
0
            CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
500
0
            if (alg1 == NULL || alg1->algorithm == NULL)
501
0
                return -1;
502
0
            hnid = OBJ_obj2nid(alg1->algorithm);
503
0
            if (hnid == NID_undef)
504
0
                return -1;
505
0
            if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
506
0
                return -1;
507
0
            X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
508
0
        }
509
0
        return 1;
510
511
0
    case ASN1_PKEY_CTRL_CMS_ENVELOPE:
512
0
        if (arg1 == 1)
513
0
            return ecdh_cms_decrypt(arg2);
514
0
        else if (arg1 == 0)
515
0
            return ecdh_cms_encrypt(arg2);
516
0
        return -2;
517
518
0
    case ASN1_PKEY_CTRL_CMS_RI_TYPE:
519
0
        *(int *)arg2 = CMS_RECIPINFO_AGREE;
520
0
        return 1;
521
0
#endif
522
523
0
    case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
524
0
        if (EVP_PKEY_id(pkey) == EVP_PKEY_SM2) {
525
            /* For SM2, the only valid digest-alg is SM3 */
526
0
            *(int *)arg2 = NID_sm3;
527
0
        } else {
528
0
            *(int *)arg2 = NID_sha256;
529
0
        }
530
0
        return 1;
531
532
0
    case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
533
0
        return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey), arg2, arg1, NULL);
534
535
0
    case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
536
0
        return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
537
0
                              POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
538
539
0
    default:
540
0
        return -2;
541
542
0
    }
543
544
0
}
545
546
static int ec_pkey_check(const EVP_PKEY *pkey)
547
0
{
548
0
    EC_KEY *eckey = pkey->pkey.ec;
549
550
    /* stay consistent to what EVP_PKEY_check demands */
551
0
    if (eckey->priv_key == NULL) {
552
0
        ECerr(EC_F_EC_PKEY_CHECK, EC_R_MISSING_PRIVATE_KEY);
553
0
        return 0;
554
0
    }
555
556
0
    return EC_KEY_check_key(eckey);
557
0
}
558
559
static int ec_pkey_public_check(const EVP_PKEY *pkey)
560
0
{
561
0
    EC_KEY *eckey = pkey->pkey.ec;
562
563
    /*
564
     * Note: it unnecessary to check eckey->pub_key here since
565
     * it will be checked in EC_KEY_check_key(). In fact, the
566
     * EC_KEY_check_key() mainly checks the public key, and checks
567
     * the private key optionally (only if there is one). So if
568
     * someone passes a whole EC key (public + private), this
569
     * will also work...
570
     */
571
572
0
    return EC_KEY_check_key(eckey);
573
0
}
574
575
static int ec_pkey_param_check(const EVP_PKEY *pkey)
576
0
{
577
0
    EC_KEY *eckey = pkey->pkey.ec;
578
579
    /* stay consistent to what EVP_PKEY_check demands */
580
0
    if (eckey->group == NULL) {
581
0
        ECerr(EC_F_EC_PKEY_PARAM_CHECK, EC_R_MISSING_PARAMETERS);
582
0
        return 0;
583
0
    }
584
585
0
    return EC_GROUP_check(eckey->group, NULL);
586
0
}
587
588
const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
589
    EVP_PKEY_EC,
590
    EVP_PKEY_EC,
591
    0,
592
    "EC",
593
    "OpenSSL EC algorithm",
594
595
    eckey_pub_decode,
596
    eckey_pub_encode,
597
    eckey_pub_cmp,
598
    eckey_pub_print,
599
600
    eckey_priv_decode,
601
    eckey_priv_encode,
602
    eckey_priv_print,
603
604
    int_ec_size,
605
    ec_bits,
606
    ec_security_bits,
607
608
    eckey_param_decode,
609
    eckey_param_encode,
610
    ec_missing_parameters,
611
    ec_copy_parameters,
612
    ec_cmp_parameters,
613
    eckey_param_print,
614
    0,
615
616
    int_ec_free,
617
    ec_pkey_ctrl,
618
    old_ec_priv_decode,
619
    old_ec_priv_encode,
620
621
    0, 0, 0,
622
623
    ec_pkey_check,
624
    ec_pkey_public_check,
625
    ec_pkey_param_check
626
};
627
628
#if !defined(OPENSSL_NO_SM2)
629
const EVP_PKEY_ASN1_METHOD sm2_asn1_meth = {
630
   EVP_PKEY_SM2,
631
   EVP_PKEY_EC,
632
   ASN1_PKEY_ALIAS
633
};
634
#endif
635
636
int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
637
578
{
638
578
    int private = EC_KEY_get0_private_key(x) != NULL;
639
640
578
    return do_EC_KEY_print(bp, x, off,
641
578
                private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
642
578
}
643
644
int ECParameters_print(BIO *bp, const EC_KEY *x)
645
88
{
646
88
    return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
647
88
}
648
649
#ifndef OPENSSL_NO_CMS
650
651
static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
652
                                X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
653
0
{
654
0
    const ASN1_OBJECT *aoid;
655
0
    int atype;
656
0
    const void *aval;
657
0
    int rv = 0;
658
0
    EVP_PKEY *pkpeer = NULL;
659
0
    EC_KEY *ecpeer = NULL;
660
0
    const unsigned char *p;
661
0
    int plen;
662
0
    X509_ALGOR_get0(&aoid, &atype, &aval, alg);
663
0
    if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
664
0
        goto err;
665
    /* If absent parameters get group from main key */
666
0
    if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
667
0
        const EC_GROUP *grp;
668
0
        EVP_PKEY *pk;
669
0
        pk = EVP_PKEY_CTX_get0_pkey(pctx);
670
0
        if (!pk)
671
0
            goto err;
672
0
        grp = EC_KEY_get0_group(pk->pkey.ec);
673
0
        ecpeer = EC_KEY_new();
674
0
        if (ecpeer == NULL)
675
0
            goto err;
676
0
        if (!EC_KEY_set_group(ecpeer, grp))
677
0
            goto err;
678
0
    } else {
679
0
        ecpeer = eckey_type2param(atype, aval);
680
0
        if (!ecpeer)
681
0
            goto err;
682
0
    }
683
    /* We have parameters now set public key */
684
0
    plen = ASN1_STRING_length(pubkey);
685
0
    p = ASN1_STRING_get0_data(pubkey);
686
0
    if (!p || !plen)
687
0
        goto err;
688
0
    if (!o2i_ECPublicKey(&ecpeer, &p, plen))
689
0
        goto err;
690
0
    pkpeer = EVP_PKEY_new();
691
0
    if (pkpeer == NULL)
692
0
        goto err;
693
0
    EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
694
0
    if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
695
0
        rv = 1;
696
0
 err:
697
0
    EC_KEY_free(ecpeer);
698
0
    EVP_PKEY_free(pkpeer);
699
0
    return rv;
700
0
}
701
702
/* Set KDF parameters based on KDF NID */
703
static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
704
0
{
705
0
    int kdf_nid, kdfmd_nid, cofactor;
706
0
    const EVP_MD *kdf_md;
707
0
    if (eckdf_nid == NID_undef)
708
0
        return 0;
709
710
    /* Lookup KDF type, cofactor mode and digest */
711
0
    if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
712
0
        return 0;
713
714
0
    if (kdf_nid == NID_dh_std_kdf)
715
0
        cofactor = 0;
716
0
    else if (kdf_nid == NID_dh_cofactor_kdf)
717
0
        cofactor = 1;
718
0
    else
719
0
        return 0;
720
721
0
    if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
722
0
        return 0;
723
724
0
    if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
725
0
        return 0;
726
727
0
    kdf_md = EVP_get_digestbynid(kdfmd_nid);
728
0
    if (!kdf_md)
729
0
        return 0;
730
731
0
    if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
732
0
        return 0;
733
0
    return 1;
734
0
}
735
736
static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
737
0
{
738
0
    int rv = 0;
739
740
0
    X509_ALGOR *alg, *kekalg = NULL;
741
0
    ASN1_OCTET_STRING *ukm;
742
0
    const unsigned char *p;
743
0
    unsigned char *der = NULL;
744
0
    int plen, keylen;
745
0
    const EVP_CIPHER *kekcipher;
746
0
    EVP_CIPHER_CTX *kekctx;
747
748
0
    if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
749
0
        return 0;
750
751
0
    if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
752
0
        ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
753
0
        return 0;
754
0
    }
755
756
0
    if (alg->parameter->type != V_ASN1_SEQUENCE)
757
0
        return 0;
758
759
0
    p = alg->parameter->value.sequence->data;
760
0
    plen = alg->parameter->value.sequence->length;
761
0
    kekalg = d2i_X509_ALGOR(NULL, &p, plen);
762
0
    if (!kekalg)
763
0
        goto err;
764
0
    kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
765
0
    if (!kekctx)
766
0
        goto err;
767
0
    kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
768
0
    if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
769
0
        goto err;
770
0
    if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
771
0
        goto err;
772
0
    if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
773
0
        goto err;
774
775
0
    keylen = EVP_CIPHER_CTX_key_length(kekctx);
776
0
    if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
777
0
        goto err;
778
779
0
    plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
780
781
0
    if (!plen)
782
0
        goto err;
783
784
0
    if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
785
0
        goto err;
786
0
    der = NULL;
787
788
0
    rv = 1;
789
0
 err:
790
0
    X509_ALGOR_free(kekalg);
791
0
    OPENSSL_free(der);
792
0
    return rv;
793
0
}
794
795
static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
796
0
{
797
0
    EVP_PKEY_CTX *pctx;
798
0
    pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
799
0
    if (!pctx)
800
0
        return 0;
801
    /* See if we need to set peer key */
802
0
    if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
803
0
        X509_ALGOR *alg;
804
0
        ASN1_BIT_STRING *pubkey;
805
0
        if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
806
0
                                                 NULL, NULL, NULL))
807
0
            return 0;
808
0
        if (!alg || !pubkey)
809
0
            return 0;
810
0
        if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
811
0
            ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
812
0
            return 0;
813
0
        }
814
0
    }
815
    /* Set ECDH derivation parameters and initialise unwrap context */
816
0
    if (!ecdh_cms_set_shared_info(pctx, ri)) {
817
0
        ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
818
0
        return 0;
819
0
    }
820
0
    return 1;
821
0
}
822
823
static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
824
0
{
825
0
    EVP_PKEY_CTX *pctx;
826
0
    EVP_PKEY *pkey;
827
0
    EVP_CIPHER_CTX *ctx;
828
0
    int keylen;
829
0
    X509_ALGOR *talg, *wrap_alg = NULL;
830
0
    const ASN1_OBJECT *aoid;
831
0
    ASN1_BIT_STRING *pubkey;
832
0
    ASN1_STRING *wrap_str;
833
0
    ASN1_OCTET_STRING *ukm;
834
0
    unsigned char *penc = NULL;
835
0
    int penclen;
836
0
    int rv = 0;
837
0
    int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
838
0
    const EVP_MD *kdf_md;
839
0
    pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
840
0
    if (!pctx)
841
0
        return 0;
842
    /* Get ephemeral key */
843
0
    pkey = EVP_PKEY_CTX_get0_pkey(pctx);
844
0
    if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
845
0
                                             NULL, NULL, NULL))
846
0
        goto err;
847
0
    X509_ALGOR_get0(&aoid, NULL, NULL, talg);
848
    /* Is everything uninitialised? */
849
0
    if (aoid == OBJ_nid2obj(NID_undef)) {
850
851
0
        EC_KEY *eckey = pkey->pkey.ec;
852
        /* Set the key */
853
0
        unsigned char *p;
854
855
0
        penclen = i2o_ECPublicKey(eckey, NULL);
856
0
        if (penclen <= 0)
857
0
            goto err;
858
0
        penc = OPENSSL_malloc(penclen);
859
0
        if (penc == NULL)
860
0
            goto err;
861
0
        p = penc;
862
0
        penclen = i2o_ECPublicKey(eckey, &p);
863
0
        if (penclen <= 0)
864
0
            goto err;
865
0
        ASN1_STRING_set0(pubkey, penc, penclen);
866
0
        pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
867
0
        pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
868
869
0
        penc = NULL;
870
0
        X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
871
0
                        V_ASN1_UNDEF, NULL);
872
0
    }
873
874
    /* See if custom parameters set */
875
0
    kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
876
0
    if (kdf_type <= 0)
877
0
        goto err;
878
0
    if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
879
0
        goto err;
880
0
    ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
881
0
    if (ecdh_nid < 0)
882
0
        goto err;
883
0
    else if (ecdh_nid == 0)
884
0
        ecdh_nid = NID_dh_std_kdf;
885
0
    else if (ecdh_nid == 1)
886
0
        ecdh_nid = NID_dh_cofactor_kdf;
887
888
0
    if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
889
0
        kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
890
0
        if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
891
0
            goto err;
892
0
    } else
893
        /* Unknown KDF */
894
0
        goto err;
895
0
    if (kdf_md == NULL) {
896
        /* Fixme later for better MD */
897
0
        kdf_md = EVP_sha1();
898
0
        if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
899
0
            goto err;
900
0
    }
901
902
0
    if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
903
0
        goto err;
904
905
    /* Lookup NID for KDF+cofactor+digest */
906
907
0
    if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
908
0
        goto err;
909
    /* Get wrap NID */
910
0
    ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
911
0
    wrap_nid = EVP_CIPHER_CTX_type(ctx);
912
0
    keylen = EVP_CIPHER_CTX_key_length(ctx);
913
914
    /* Package wrap algorithm in an AlgorithmIdentifier */
915
916
0
    wrap_alg = X509_ALGOR_new();
917
0
    if (wrap_alg == NULL)
918
0
        goto err;
919
0
    wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
920
0
    wrap_alg->parameter = ASN1_TYPE_new();
921
0
    if (wrap_alg->parameter == NULL)
922
0
        goto err;
923
0
    if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
924
0
        goto err;
925
0
    if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
926
0
        ASN1_TYPE_free(wrap_alg->parameter);
927
0
        wrap_alg->parameter = NULL;
928
0
    }
929
930
0
    if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
931
0
        goto err;
932
933
0
    penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
934
935
0
    if (!penclen)
936
0
        goto err;
937
938
0
    if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
939
0
        goto err;
940
0
    penc = NULL;
941
942
    /*
943
     * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
944
     * of another AlgorithmIdentifier.
945
     */
946
0
    penclen = i2d_X509_ALGOR(wrap_alg, &penc);
947
0
    if (!penc || !penclen)
948
0
        goto err;
949
0
    wrap_str = ASN1_STRING_new();
950
0
    if (wrap_str == NULL)
951
0
        goto err;
952
0
    ASN1_STRING_set0(wrap_str, penc, penclen);
953
0
    penc = NULL;
954
0
    X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
955
956
0
    rv = 1;
957
958
0
 err:
959
0
    OPENSSL_free(penc);
960
0
    X509_ALGOR_free(wrap_alg);
961
0
    return rv;
962
0
}
963
964
#endif