Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/cms/cms_ec.c
Line
Count
Source
1
/*
2
 * Copyright 2006-2023 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
#include <assert.h>
11
#include <limits.h>
12
#include <openssl/cms.h>
13
#include <openssl/err.h>
14
#include <openssl/decoder.h>
15
#include "internal/sizes.h"
16
#include "crypto/asn1.h"
17
#include "crypto/evp.h"
18
#include "cms_local.h"
19
20
static EVP_PKEY *pkey_type2param(int ptype, const void *pval,
21
    OSSL_LIB_CTX *libctx, const char *propq)
22
0
{
23
0
    EVP_PKEY *pkey = NULL;
24
0
    EVP_PKEY_CTX *pctx = NULL;
25
0
    OSSL_DECODER_CTX *ctx = NULL;
26
27
0
    if (ptype == V_ASN1_SEQUENCE) {
28
0
        const ASN1_STRING *pstr = pval;
29
0
        const unsigned char *pm = pstr->data;
30
0
        size_t pmlen = (size_t)pstr->length;
31
0
        int selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
32
33
0
        ctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", NULL, "EC",
34
0
            selection, libctx, propq);
35
0
        if (ctx == NULL)
36
0
            goto err;
37
38
0
        if (!OSSL_DECODER_from_data(ctx, &pm, &pmlen)) {
39
0
            ERR_raise(ERR_LIB_CMS, CMS_R_DECODE_ERROR);
40
0
            goto err;
41
0
        }
42
0
        OSSL_DECODER_CTX_free(ctx);
43
0
        return pkey;
44
0
    } else if (ptype == V_ASN1_OBJECT) {
45
0
        const ASN1_OBJECT *poid = pval;
46
0
        char groupname[OSSL_MAX_NAME_SIZE];
47
48
        /* type == V_ASN1_OBJECT => the parameters are given by an asn1 OID */
49
0
        pctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", propq);
50
0
        if (pctx == NULL || EVP_PKEY_paramgen_init(pctx) <= 0)
51
0
            goto err;
52
0
        if (OBJ_obj2txt(groupname, sizeof(groupname), poid, 0) <= 0
53
0
            || EVP_PKEY_CTX_set_group_name(pctx, groupname) <= 0) {
54
0
            ERR_raise(ERR_LIB_CMS, CMS_R_DECODE_ERROR);
55
0
            goto err;
56
0
        }
57
0
        if (EVP_PKEY_paramgen(pctx, &pkey) <= 0)
58
0
            goto err;
59
0
        EVP_PKEY_CTX_free(pctx);
60
0
        return pkey;
61
0
    }
62
63
0
    ERR_raise(ERR_LIB_CMS, CMS_R_DECODE_ERROR);
64
0
    return NULL;
65
66
0
err:
67
0
    EVP_PKEY_free(pkey);
68
0
    EVP_PKEY_CTX_free(pctx);
69
0
    OSSL_DECODER_CTX_free(ctx);
70
0
    return NULL;
71
0
}
72
73
static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
74
    X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
75
0
{
76
0
    const ASN1_OBJECT *aoid;
77
0
    int atype;
78
0
    const void *aval;
79
0
    int rv = 0;
80
0
    EVP_PKEY *pkpeer = NULL;
81
0
    const unsigned char *p;
82
0
    size_t plen;
83
84
0
    X509_ALGOR_get0(&aoid, &atype, &aval, alg);
85
0
    if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
86
0
        goto err;
87
88
    /* If absent parameters get group from main key */
89
0
    if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
90
0
        EVP_PKEY *pk;
91
92
0
        pk = EVP_PKEY_CTX_get0_pkey(pctx);
93
0
        if (pk == NULL)
94
0
            goto err;
95
96
0
        pkpeer = EVP_PKEY_new();
97
0
        if (pkpeer == NULL)
98
0
            goto err;
99
0
        if (!EVP_PKEY_copy_parameters(pkpeer, pk))
100
0
            goto err;
101
0
    } else {
102
0
        pkpeer = pkey_type2param(atype, aval,
103
0
            EVP_PKEY_CTX_get0_libctx(pctx),
104
0
            EVP_PKEY_CTX_get0_propq(pctx));
105
0
        if (pkpeer == NULL)
106
0
            goto err;
107
0
    }
108
    /* We have parameters now set public key */
109
0
    plen = ASN1_STRING_length_ex(pubkey);
110
0
    if (plen > INT_MAX)
111
0
        goto err;
112
0
    p = ASN1_STRING_get0_data(pubkey);
113
0
    if (p == NULL || plen == 0)
114
0
        goto err;
115
116
0
    if (EVP_PKEY_set1_encoded_public_key(pkpeer, p, (int)plen) <= 0)
117
0
        goto err;
118
119
0
    if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
120
0
        rv = 1;
121
0
err:
122
0
    EVP_PKEY_free(pkpeer);
123
0
    return rv;
124
0
}
125
126
/* Set KDF parameters based on KDF NID */
127
static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
128
0
{
129
0
    int kdf_nid, kdfmd_nid, cofactor;
130
0
    const EVP_MD *kdf_md;
131
132
0
    if (eckdf_nid == NID_undef)
133
0
        return 0;
134
135
    /* Lookup KDF type, cofactor mode and digest */
136
0
    if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
137
0
        return 0;
138
139
0
    if (kdf_nid == NID_dh_std_kdf)
140
0
        cofactor = 0;
141
0
    else if (kdf_nid == NID_dh_cofactor_kdf)
142
0
        cofactor = 1;
143
0
    else
144
0
        return 0;
145
146
0
    if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
147
0
        return 0;
148
149
0
    if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
150
0
        return 0;
151
152
0
    kdf_md = EVP_get_digestbynid(kdfmd_nid);
153
0
    if (!kdf_md)
154
0
        return 0;
155
156
0
    if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
157
0
        return 0;
158
0
    return 1;
159
0
}
160
161
static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
162
0
{
163
0
    int rv = 0;
164
0
    X509_ALGOR *alg, *kekalg = NULL;
165
0
    ASN1_OCTET_STRING *ukm;
166
0
    const unsigned char *p;
167
0
    unsigned char *der = NULL;
168
0
    int keylen, plen_i;
169
0
    size_t plen;
170
0
    EVP_CIPHER *kekcipher = NULL;
171
0
    EVP_CIPHER_CTX *kekctx;
172
0
    const ASN1_OBJECT *aoid = NULL;
173
0
    int ptype = 0;
174
0
    const void *parameter = NULL;
175
176
0
    char name[OSSL_MAX_NAME_SIZE];
177
178
0
    if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
179
0
        return 0;
180
181
0
    X509_ALGOR_get0(&aoid, &ptype, &parameter, alg);
182
183
0
    if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(aoid))) {
184
0
        ERR_raise(ERR_LIB_CMS, CMS_R_KDF_PARAMETER_ERROR);
185
0
        return 0;
186
0
    }
187
188
0
    if (ptype != V_ASN1_SEQUENCE)
189
0
        return 0;
190
191
0
    p = ASN1_STRING_get0_data(parameter);
192
0
    plen = ASN1_STRING_length_ex(parameter);
193
0
    if (plen > INT_MAX)
194
0
        goto err;
195
0
    kekalg = d2i_X509_ALGOR(NULL, &p, (int)plen);
196
0
    if (kekalg == NULL)
197
0
        goto err;
198
0
    kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
199
0
    if (kekctx == NULL)
200
0
        goto err;
201
0
    OBJ_obj2txt(name, sizeof(name), kekalg->algorithm, 0);
202
0
    kekcipher = EVP_CIPHER_fetch(pctx->libctx, name, pctx->propquery);
203
0
    if (kekcipher == NULL || EVP_CIPHER_get_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
204
0
        goto err;
205
0
    if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
206
0
        goto err;
207
0
    if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
208
0
        goto err;
209
210
0
    keylen = EVP_CIPHER_CTX_get_key_length(kekctx);
211
0
    if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
212
0
        goto err;
213
214
0
    plen_i = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
215
216
0
    if (plen_i <= 0)
217
0
        goto err;
218
219
0
    if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen_i) <= 0)
220
0
        goto err;
221
0
    der = NULL;
222
223
0
    rv = 1;
224
0
err:
225
0
    EVP_CIPHER_free(kekcipher);
226
0
    X509_ALGOR_free(kekalg);
227
0
    OPENSSL_free(der);
228
0
    return rv;
229
0
}
230
231
static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
232
0
{
233
0
    EVP_PKEY_CTX *pctx;
234
235
0
    pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
236
0
    if (pctx == NULL)
237
0
        return 0;
238
    /* See if we need to set peer key */
239
0
    if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
240
0
        X509_ALGOR *alg;
241
0
        ASN1_BIT_STRING *pubkey;
242
243
0
        if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
244
0
                NULL, NULL, NULL))
245
0
            return 0;
246
0
        if (alg == NULL || pubkey == NULL)
247
0
            return 0;
248
0
        if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
249
0
            ERR_raise(ERR_LIB_CMS, CMS_R_PEER_KEY_ERROR);
250
0
            return 0;
251
0
        }
252
0
    }
253
    /* Set ECDH derivation parameters and initialise unwrap context */
254
0
    if (!ecdh_cms_set_shared_info(pctx, ri)) {
255
0
        ERR_raise(ERR_LIB_CMS, CMS_R_SHARED_INFO_ERROR);
256
0
        return 0;
257
0
    }
258
0
    return 1;
259
0
}
260
261
static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
262
0
{
263
0
    EVP_PKEY_CTX *pctx;
264
0
    EVP_PKEY *pkey;
265
0
    EVP_CIPHER_CTX *ctx;
266
0
    int keylen;
267
0
    X509_ALGOR *talg, *wrap_alg = NULL;
268
0
    const ASN1_OBJECT *aoid;
269
0
    ASN1_BIT_STRING *pubkey;
270
0
    ASN1_STRING *wrap_str;
271
0
    ASN1_OCTET_STRING *ukm;
272
0
    unsigned char *penc = NULL;
273
0
    int penclen;
274
0
    int rv = 0;
275
0
    int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
276
0
    const EVP_MD *kdf_md;
277
278
0
    pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
279
0
    if (pctx == NULL)
280
0
        return 0;
281
    /* Get ephemeral key */
282
0
    pkey = EVP_PKEY_CTX_get0_pkey(pctx);
283
0
    if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
284
0
            NULL, NULL, NULL))
285
0
        goto err;
286
0
    X509_ALGOR_get0(&aoid, NULL, NULL, talg);
287
    /* Is everything uninitialised? */
288
0
    if (aoid == OBJ_nid2obj(NID_undef)) {
289
        /* Set the key */
290
0
        size_t enckeylen;
291
292
0
        enckeylen = EVP_PKEY_get1_encoded_public_key(pkey, &penc);
293
0
        if (enckeylen > INT_MAX || enckeylen == 0)
294
0
            goto err;
295
0
        ASN1_STRING_set0(pubkey, penc, (int)enckeylen);
296
0
        ossl_asn1_bit_string_set_unused_bits(pubkey, 0);
297
298
0
        penc = NULL;
299
0
        (void)X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
300
0
            V_ASN1_UNDEF, NULL); /* cannot fail */
301
0
    }
302
303
    /* See if custom parameters set */
304
0
    kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
305
0
    if (kdf_type <= 0)
306
0
        goto err;
307
0
    if (EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md) <= 0)
308
0
        goto err;
309
0
    ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
310
0
    if (ecdh_nid < 0)
311
0
        goto err;
312
0
    else if (ecdh_nid == 0)
313
0
        ecdh_nid = NID_dh_std_kdf;
314
0
    else if (ecdh_nid == 1)
315
0
        ecdh_nid = NID_dh_cofactor_kdf;
316
317
0
    if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
318
0
        kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
319
0
        if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
320
0
            goto err;
321
0
    } else
322
        /* Unknown KDF */
323
0
        goto err;
324
0
    if (kdf_md == NULL) {
325
        /* Fixme later for better MD */
326
0
        kdf_md = EVP_sha1();
327
0
        if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
328
0
            goto err;
329
0
    }
330
331
0
    if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
332
0
        goto err;
333
334
    /* Lookup NID for KDF+cofactor+digest */
335
336
0
    if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_get_type(kdf_md), ecdh_nid))
337
0
        goto err;
338
    /* Get wrap NID */
339
0
    ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
340
0
    wrap_nid = EVP_CIPHER_CTX_get_type(ctx);
341
0
    keylen = EVP_CIPHER_CTX_get_key_length(ctx);
342
343
    /* Package wrap algorithm in an AlgorithmIdentifier */
344
345
0
    wrap_alg = X509_ALGOR_new();
346
0
    if (wrap_alg == NULL)
347
0
        goto err;
348
0
    wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
349
0
    wrap_alg->parameter = ASN1_TYPE_new();
350
0
    if (wrap_alg->parameter == NULL)
351
0
        goto err;
352
0
    if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
353
0
        goto err;
354
0
    if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
355
0
        ASN1_TYPE_free(wrap_alg->parameter);
356
0
        wrap_alg->parameter = NULL;
357
0
    }
358
359
0
    if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
360
0
        goto err;
361
362
0
    penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
363
364
0
    if (penclen <= 0)
365
0
        goto err;
366
367
0
    if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
368
0
        goto err;
369
0
    penc = NULL;
370
371
    /*
372
     * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
373
     * of another AlgorithmIdentifier.
374
     */
375
0
    penclen = i2d_X509_ALGOR(wrap_alg, &penc);
376
0
    if (penclen <= 0)
377
0
        goto err;
378
0
    wrap_str = ASN1_STRING_new();
379
0
    if (wrap_str == NULL)
380
0
        goto err;
381
0
    ASN1_STRING_set0(wrap_str, penc, penclen);
382
0
    penc = NULL;
383
0
    rv = X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
384
0
    if (!rv)
385
0
        ASN1_STRING_free(wrap_str);
386
387
0
err:
388
0
    OPENSSL_free(penc);
389
0
    X509_ALGOR_free(wrap_alg);
390
0
    return rv;
391
0
}
392
393
int ossl_cms_ecdh_envelope(CMS_RecipientInfo *ri, int decrypt)
394
0
{
395
0
    assert(decrypt == 0 || decrypt == 1);
396
397
0
    if (decrypt == 1)
398
0
        return ecdh_cms_decrypt(ri);
399
400
0
    if (decrypt == 0)
401
0
        return ecdh_cms_encrypt(ri);
402
403
0
    ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
404
0
    return 0;
405
0
}