Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/cms/cms_enc.c
Line
Count
Source
1
/*
2
 * Copyright 2008-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 "internal/cryptlib.h"
11
#include <openssl/asn1t.h>
12
#include <openssl/pem.h>
13
#include <openssl/x509v3.h>
14
#include <openssl/err.h>
15
#include <openssl/cms.h>
16
#include <openssl/rand.h>
17
#include "crypto/evp.h"
18
#include "crypto/asn1.h"
19
#include "cms_local.h"
20
#include "internal/sizes.h"
21
22
/* CMS EncryptedData Utilities */
23
24
/* Return BIO based on EncryptedContentInfo and key */
25
26
BIO *ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
27
    const CMS_CTX *cms_ctx, int auth)
28
0
{
29
0
    BIO *b;
30
0
    EVP_CIPHER_CTX *ctx;
31
0
    EVP_CIPHER *fetched_ciph = NULL;
32
0
    const EVP_CIPHER *cipher = NULL;
33
0
    X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
34
0
    evp_cipher_aead_asn1_params aparams;
35
0
    unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;
36
0
    unsigned char *tkey = NULL;
37
0
    int len;
38
0
    int ivlen = 0;
39
0
    size_t tkeylen = 0;
40
0
    int ok = 0;
41
0
    int enc, keep_key = 0;
42
0
    OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(cms_ctx);
43
0
    const char *propq = ossl_cms_ctx_get0_propq(cms_ctx);
44
45
0
    enc = ec->cipher ? 1 : 0;
46
47
0
    b = BIO_new(BIO_f_cipher());
48
0
    if (b == NULL) {
49
0
        ERR_raise(ERR_LIB_CMS, ERR_R_BIO_LIB);
50
0
        return NULL;
51
0
    }
52
53
0
    BIO_get_cipher_ctx(b, &ctx);
54
55
0
    (void)ERR_set_mark();
56
0
    if (enc) {
57
0
        cipher = ec->cipher;
58
        /*
59
         * If not keeping key set cipher to NULL so subsequent calls decrypt.
60
         */
61
0
        if (ec->key != NULL)
62
0
            ec->cipher = NULL;
63
0
    } else {
64
0
        cipher = EVP_get_cipherbyobj(calg->algorithm);
65
0
    }
66
0
    if (cipher != NULL) {
67
0
        fetched_ciph = EVP_CIPHER_fetch(libctx, EVP_CIPHER_get0_name(cipher),
68
0
            propq);
69
0
    } else {
70
0
        char txtoid[OSSL_MAX_NAME_SIZE];
71
0
        if (OBJ_obj2txt(txtoid, sizeof(txtoid), calg->algorithm, 1) > 0)
72
0
            fetched_ciph = EVP_CIPHER_fetch(libctx, txtoid, propq);
73
0
    }
74
0
    if (fetched_ciph != NULL)
75
0
        cipher = fetched_ciph;
76
0
    if (cipher == NULL) {
77
0
        (void)ERR_clear_last_mark();
78
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
79
0
        goto err;
80
0
    }
81
0
    (void)ERR_pop_to_mark();
82
83
0
    if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc) <= 0) {
84
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR);
85
0
        goto err;
86
0
    }
87
88
0
    if (enc) {
89
0
        (void)ERR_set_mark();
90
0
        calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_get_type(ctx));
91
0
        (void)ERR_pop_to_mark();
92
93
0
        if (calg->algorithm == NULL || calg->algorithm->nid == NID_undef)
94
0
            calg->algorithm = OBJ_txt2obj(EVP_CIPHER_get0_name(cipher), 0);
95
96
0
        if (calg->algorithm == NULL || OBJ_length(calg->algorithm) == 0) {
97
0
            ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_ENCRYPTION_ALGORITHM);
98
0
            goto err;
99
0
        }
100
        /* Generate a random IV if we need one */
101
0
        ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
102
103
0
        if (ivlen > 0) {
104
0
            if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
105
0
                goto err;
106
0
            piv = iv;
107
0
        }
108
0
    } else {
109
0
        if (evp_cipher_asn1_to_param_ex(ctx, calg->parameter, &aparams) <= 0) {
110
0
            ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
111
0
            goto err;
112
0
        }
113
0
        if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
114
0
            if (!auth) {
115
0
                ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_AEAD_IN_ENVELOPED_DATA);
116
0
                goto err;
117
0
            }
118
0
            piv = aparams.iv;
119
120
0
            if (ec->taglen < 4 || ec->taglen > 16
121
0
                || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (int)ec->taglen, ec->tag) <= 0) {
122
0
                ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_AEAD_SET_TAG_ERROR);
123
0
                goto err;
124
0
            }
125
0
        } else if (auth) {
126
0
            ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_ENCRYPTION_ALGORITHM);
127
0
            goto err;
128
0
        }
129
0
    }
130
0
    len = EVP_CIPHER_CTX_get_key_length(ctx);
131
0
    if (len <= 0)
132
0
        goto err;
133
0
    tkeylen = (size_t)len;
134
135
    /* Generate random session key */
136
0
    if (!enc || !ec->key) {
137
0
        tkey = OPENSSL_malloc(tkeylen);
138
0
        if (tkey == NULL)
139
0
            goto err;
140
0
        if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
141
0
            goto err;
142
0
    }
143
144
0
    if (!ec->key) {
145
0
        ec->key = tkey;
146
0
        ec->keylen = tkeylen;
147
0
        tkey = NULL;
148
0
        if (enc)
149
0
            keep_key = 1;
150
0
        else
151
0
            ERR_clear_error();
152
0
    }
153
154
0
    if (ec->keylen != tkeylen) {
155
        /* If necessary set key length */
156
0
        if (EVP_CIPHER_CTX_set_key_length(ctx, (int)ec->keylen) <= 0) {
157
            /*
158
             * Only reveal failure if debugging so we don't leak information
159
             * which may be useful in MMA.
160
             */
161
0
            if (enc || ec->debug) {
162
0
                ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
163
0
                goto err;
164
0
            } else {
165
                /* Use random key */
166
0
                OPENSSL_clear_free(ec->key, ec->keylen);
167
0
                ec->key = tkey;
168
0
                ec->keylen = tkeylen;
169
0
                tkey = NULL;
170
0
                ERR_clear_error();
171
0
            }
172
0
        }
173
0
    }
174
175
0
    if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {
176
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR);
177
0
        goto err;
178
0
    }
179
0
    if (enc) {
180
0
        calg->parameter = ASN1_TYPE_new();
181
0
        if (calg->parameter == NULL) {
182
0
            ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
183
0
            goto err;
184
0
        }
185
0
        if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
186
0
            if (ivlen > EVP_MAX_IV_LENGTH || ivlen < 0) {
187
0
                ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
188
0
                goto err;
189
0
            }
190
0
            if (ivlen != 0)
191
0
                memcpy(aparams.iv, piv, ivlen);
192
0
            aparams.iv_len = ivlen;
193
0
            aparams.tag_len = EVP_CIPHER_CTX_get_tag_length(ctx);
194
0
            if (aparams.tag_len <= 0) {
195
0
                ASN1_TYPE_free(calg->parameter);
196
0
                calg->parameter = NULL;
197
0
                goto err;
198
0
            }
199
0
        }
200
201
0
        if (evp_cipher_param_to_asn1_ex(ctx, calg->parameter, &aparams) <= 0) {
202
0
            ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
203
0
            ASN1_TYPE_free(calg->parameter);
204
0
            calg->parameter = NULL;
205
0
            goto err;
206
0
        }
207
        /* If parameter type not set omit parameter */
208
0
        if (calg->parameter->type == V_ASN1_UNDEF) {
209
0
            ASN1_TYPE_free(calg->parameter);
210
0
            calg->parameter = NULL;
211
0
        }
212
0
    }
213
0
    ok = 1;
214
215
0
err:
216
0
    EVP_CIPHER_free(fetched_ciph);
217
0
    if (!keep_key || !ok) {
218
0
        OPENSSL_clear_free(ec->key, ec->keylen);
219
0
        ec->key = NULL;
220
0
    }
221
0
    OPENSSL_clear_free(tkey, tkeylen);
222
0
    if (ok)
223
0
        return b;
224
0
    BIO_free(b);
225
0
    return NULL;
226
0
}
227
228
int ossl_cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
229
    const EVP_CIPHER *cipher,
230
    const unsigned char *key, size_t keylen,
231
    const CMS_CTX *cms_ctx)
232
0
{
233
0
    ec->cipher = cipher;
234
0
    if (key) {
235
0
        if ((ec->key = OPENSSL_malloc(keylen)) == NULL)
236
0
            return 0;
237
0
        memcpy(ec->key, key, keylen);
238
0
    }
239
0
    ec->keylen = keylen;
240
0
    if (cipher != NULL)
241
0
        ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
242
0
    return 1;
243
0
}
244
245
int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
246
    const unsigned char *key, size_t keylen)
247
0
{
248
0
    CMS_EncryptedContentInfo *ec;
249
250
0
    if (!key || !keylen) {
251
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
252
0
        return 0;
253
0
    }
254
0
    if (ciph) {
255
0
        if ((EVP_CIPHER_get_flags(ciph) & EVP_CIPH_FLAG_AEAD_CIPHER) != 0) {
256
0
            ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_ENCRYPTION_ALGORITHM);
257
0
            return 0;
258
0
        }
259
0
        if (cms->d.encryptedData != NULL) {
260
0
            M_ASN1_free_of(cms->d.encryptedData, CMS_EncryptedData);
261
0
            cms->d.encryptedData = NULL;
262
0
        }
263
0
        cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
264
0
        if (!cms->d.encryptedData) {
265
0
            ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
266
0
            return 0;
267
0
        }
268
0
        cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
269
0
        cms->d.encryptedData->version = 0;
270
0
    } else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted) {
271
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_ENCRYPTED_DATA);
272
0
        return 0;
273
0
    }
274
0
    ec = cms->d.encryptedData->encryptedContentInfo;
275
0
    return ossl_cms_EncryptedContent_init(ec, ciph, key, keylen,
276
0
        ossl_cms_get0_cmsctx(cms));
277
0
}
278
279
BIO *ossl_cms_EncryptedData_init_bio(const CMS_ContentInfo *cms)
280
0
{
281
0
    CMS_EncryptedData *enc = cms->d.encryptedData;
282
283
0
    if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
284
0
        enc->version = 2;
285
0
    return ossl_cms_EncryptedContent_init_bio(enc->encryptedContentInfo,
286
0
        ossl_cms_get0_cmsctx(cms), 0);
287
0
}