Coverage Report

Created: 2026-04-09 06:50

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