Coverage Report

Created: 2023-09-25 06:45

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