Coverage Report

Created: 2018-08-29 13:53

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