Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/cms/cms_pwri.c
Line
Count
Source
1
/*
2
 * Copyright 2009-2025 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 <openssl/aes.h>
18
#include "internal/sizes.h"
19
#include "crypto/asn1.h"
20
#include "cms_local.h"
21
22
int CMS_RecipientInfo_set0_password(CMS_RecipientInfo *ri,
23
                                    unsigned char *pass, ossl_ssize_t passlen)
24
0
{
25
0
    CMS_PasswordRecipientInfo *pwri;
26
0
    if (ri->type != CMS_RECIPINFO_PASS) {
27
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_PWRI);
28
0
        return 0;
29
0
    }
30
31
0
    pwri = ri->d.pwri;
32
0
    pwri->pass = pass;
33
0
    if (pass && passlen < 0)
34
0
        passlen = strlen((char *)pass);
35
0
    pwri->passlen = passlen;
36
0
    return 1;
37
0
}
38
39
CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
40
                                               int iter, int wrap_nid,
41
                                               int pbe_nid,
42
                                               unsigned char *pass,
43
                                               ossl_ssize_t passlen,
44
                                               const EVP_CIPHER *kekciph)
45
0
{
46
0
    STACK_OF(CMS_RecipientInfo) *ris;
47
0
    CMS_RecipientInfo *ri = NULL;
48
0
    CMS_EncryptedContentInfo *ec;
49
0
    CMS_PasswordRecipientInfo *pwri;
50
0
    EVP_CIPHER_CTX *ctx = NULL;
51
0
    X509_ALGOR *encalg = NULL;
52
0
    unsigned char iv[EVP_MAX_IV_LENGTH];
53
0
    int ivlen;
54
0
    const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
55
56
0
    ec = ossl_cms_get0_env_enc_content(cms);
57
0
    if (ec == NULL)
58
0
        return NULL;
59
0
    ris = CMS_get0_RecipientInfos(cms);
60
0
    if (ris == NULL)
61
0
        return NULL;
62
63
0
    if (wrap_nid <= 0)
64
0
        wrap_nid = NID_id_alg_PWRI_KEK;
65
66
    /* Get from enveloped data */
67
0
    if (kekciph == NULL)
68
0
        kekciph = ec->cipher;
69
70
0
    if (kekciph == NULL) {
71
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_CIPHER);
72
0
        return NULL;
73
0
    }
74
0
    if ((EVP_CIPHER_get_flags(kekciph) & EVP_CIPH_FLAG_AEAD_CIPHER) != 0) {
75
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEK_ALGORITHM);
76
0
        return NULL;
77
0
    }
78
0
    if (wrap_nid != NID_id_alg_PWRI_KEK) {
79
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
80
0
        return NULL;
81
0
    }
82
83
    /* Setup algorithm identifier for cipher */
84
0
    encalg = X509_ALGOR_new();
85
0
    if (encalg == NULL) {
86
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
87
0
        goto err;
88
0
    }
89
0
    ctx = EVP_CIPHER_CTX_new();
90
0
    if (ctx == NULL) {
91
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
92
0
        goto err;
93
0
    }
94
95
0
    if (EVP_EncryptInit_ex(ctx, kekciph, NULL, NULL, NULL) <= 0) {
96
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
97
0
        goto err;
98
0
    }
99
100
0
    ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
101
0
    if (ivlen < 0) {
102
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
103
0
        goto err;
104
0
    }
105
106
0
    if (ivlen > 0) {
107
0
        if (RAND_bytes_ex(ossl_cms_ctx_get0_libctx(cms_ctx), iv, ivlen, 0) <= 0)
108
0
            goto err;
109
0
        if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0) {
110
0
            ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
111
0
            goto err;
112
0
        }
113
0
        encalg->parameter = ASN1_TYPE_new();
114
0
        if (!encalg->parameter) {
115
0
            ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
116
0
            goto err;
117
0
        }
118
0
        if (EVP_CIPHER_param_to_asn1(ctx, encalg->parameter) <= 0) {
119
0
            ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
120
0
            goto err;
121
0
        }
122
0
    }
123
124
0
    encalg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_get_type(ctx));
125
126
0
    EVP_CIPHER_CTX_free(ctx);
127
0
    ctx = NULL;
128
129
    /* Initialize recipient info */
130
0
    ri = M_ASN1_new_of(CMS_RecipientInfo);
131
0
    if (ri == NULL) {
132
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
133
0
        goto err;
134
0
    }
135
136
0
    ri->d.pwri = M_ASN1_new_of(CMS_PasswordRecipientInfo);
137
0
    if (ri->d.pwri == NULL) {
138
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
139
0
        goto err;
140
0
    }
141
0
    ri->encoded_type = ri->type = CMS_RECIPINFO_PASS;
142
143
0
    pwri = ri->d.pwri;
144
0
    pwri->cms_ctx = cms_ctx;
145
    /* Since this is overwritten, free up empty structure already there */
146
0
    X509_ALGOR_free(pwri->keyEncryptionAlgorithm);
147
0
    pwri->keyEncryptionAlgorithm = X509_ALGOR_new();
148
0
    if (pwri->keyEncryptionAlgorithm == NULL) {
149
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
150
0
        goto err;
151
0
    }
152
0
    pwri->keyEncryptionAlgorithm->algorithm = OBJ_nid2obj(wrap_nid);
153
0
    pwri->keyEncryptionAlgorithm->parameter = ASN1_TYPE_new();
154
0
    if (pwri->keyEncryptionAlgorithm->parameter == NULL) {
155
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
156
0
        goto err;
157
0
    }
158
159
0
    if (!ASN1_item_pack(encalg, ASN1_ITEM_rptr(X509_ALGOR),
160
0
                        &pwri->keyEncryptionAlgorithm->parameter->
161
0
                        value.sequence)) {
162
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
163
0
        goto err;
164
0
    }
165
0
    pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE;
166
167
0
    X509_ALGOR_free(encalg);
168
0
    encalg = NULL;
169
170
    /* Setup PBE algorithm */
171
172
0
    pwri->keyDerivationAlgorithm = PKCS5_pbkdf2_set_ex(iter, NULL, 0, -1, -1,
173
0
                                                       cms_ctx->libctx);
174
175
0
    if (pwri->keyDerivationAlgorithm == NULL)
176
0
        goto err;
177
178
0
    CMS_RecipientInfo_set0_password(ri, pass, passlen);
179
0
    pwri->version = 0;
180
181
0
    if (!sk_CMS_RecipientInfo_push(ris, ri)) {
182
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
183
0
        goto err;
184
0
    }
185
186
0
    return ri;
187
188
0
 err:
189
0
    EVP_CIPHER_CTX_free(ctx);
190
0
    if (ri)
191
0
        M_ASN1_free_of(ri, CMS_RecipientInfo);
192
0
    X509_ALGOR_free(encalg);
193
0
    return NULL;
194
195
0
}
196
197
/*
198
 * This is an implementation of the key wrapping mechanism in RFC3211, at
199
 * some point this should go into EVP.
200
 */
201
202
static int kek_unwrap_key(unsigned char *out, size_t *outlen,
203
                          const unsigned char *in, size_t inlen,
204
                          EVP_CIPHER_CTX *ctx)
205
0
{
206
0
    int blocklen = EVP_CIPHER_CTX_get_block_size(ctx);
207
0
    unsigned char *tmp;
208
0
    int outl, rv = 0;
209
210
0
    if (blocklen <= 0)
211
0
        return 0;
212
213
0
    if (inlen < 2 * (size_t)blocklen) {
214
        /* too small */
215
0
        return 0;
216
0
    }
217
0
    if (inlen > INT_MAX || inlen % blocklen) {
218
        /* Invalid size */
219
0
        return 0;
220
0
    }
221
0
    if ((tmp = OPENSSL_malloc(inlen)) == NULL)
222
0
        return 0;
223
    /* setup IV by decrypting last two blocks */
224
0
    if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,
225
0
                           in + inlen - 2 * blocklen, blocklen * 2)
226
        /*
227
         * Do a decrypt of last decrypted block to set IV to correct value
228
         * output it to start of buffer so we don't corrupt decrypted block
229
         * this works because buffer is at least two block lengths long.
230
         */
231
0
        || !EVP_DecryptUpdate(ctx, tmp, &outl,
232
0
                              tmp + inlen - blocklen, blocklen)
233
        /* Can now decrypt first n - 1 blocks */
234
0
        || !EVP_DecryptUpdate(ctx, tmp, &outl, in, (int)(inlen - blocklen))
235
236
        /* Reset IV to original value */
237
0
        || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)
238
        /* Decrypt again */
239
0
        || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, (int)inlen))
240
0
        goto err;
241
    /* Check check bytes */
242
0
    if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {
243
        /* Check byte failure */
244
0
        goto err;
245
0
    }
246
0
    if (inlen < 4 + (size_t)tmp[0]) {
247
        /* Invalid length value */
248
0
        goto err;
249
0
    }
250
0
    *outlen = (size_t)tmp[0];
251
0
    memcpy(out, tmp + 4, *outlen);
252
0
    rv = 1;
253
0
 err:
254
0
    OPENSSL_clear_free(tmp, inlen);
255
0
    return rv;
256
257
0
}
258
259
static int kek_wrap_key(unsigned char *out, size_t *outlen,
260
                        const unsigned char *in, size_t inlen,
261
                        EVP_CIPHER_CTX *ctx, const CMS_CTX *cms_ctx)
262
0
{
263
0
    size_t blocklen = EVP_CIPHER_CTX_get_block_size(ctx);
264
0
    size_t olen;
265
0
    int dummy;
266
267
0
    if (blocklen == 0)
268
0
        return 0;
269
270
    /*
271
     * First decide length of output buffer: need header and round up to
272
     * multiple of block length.
273
     */
274
0
    olen = (inlen + 4 + blocklen - 1) / blocklen;
275
0
    olen *= blocklen;
276
0
    if (olen < 2 * blocklen) {
277
        /* Key too small */
278
0
        return 0;
279
0
    }
280
0
    if (inlen > 0xFF) {
281
        /* Key too large */
282
0
        return 0;
283
0
    }
284
0
    if (out) {
285
        /* Set header */
286
0
        out[0] = (unsigned char)inlen;
287
0
        out[1] = in[0] ^ 0xFF;
288
0
        out[2] = in[1] ^ 0xFF;
289
0
        out[3] = in[2] ^ 0xFF;
290
0
        memcpy(out + 4, in, inlen);
291
        /* Add random padding to end */
292
0
        if (olen > inlen + 4
293
0
            && RAND_bytes_ex(ossl_cms_ctx_get0_libctx(cms_ctx), out + 4 + inlen,
294
0
                             olen - 4 - inlen, 0) <= 0)
295
0
            return 0;
296
        /* Encrypt twice */
297
0
        if (!EVP_EncryptUpdate(ctx, out, &dummy, out, (int)olen)
298
0
            || !EVP_EncryptUpdate(ctx, out, &dummy, out, (int)olen))
299
0
            return 0;
300
0
    }
301
302
0
    *outlen = olen;
303
304
0
    return 1;
305
0
}
306
307
/* Encrypt/Decrypt content key in PWRI recipient info */
308
309
int ossl_cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo *cms,
310
                                      CMS_RecipientInfo *ri, int en_de)
311
0
{
312
0
    CMS_EncryptedContentInfo *ec;
313
0
    CMS_PasswordRecipientInfo *pwri;
314
0
    int r = 0;
315
0
    X509_ALGOR *algtmp, *kekalg = NULL;
316
0
    EVP_CIPHER_CTX *kekctx = NULL;
317
0
    char name[OSSL_MAX_NAME_SIZE];
318
0
    EVP_CIPHER *kekcipher;
319
0
    unsigned char *key = NULL;
320
0
    size_t keylen;
321
0
    const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
322
323
0
    ec = ossl_cms_get0_env_enc_content(cms);
324
325
0
    pwri = ri->d.pwri;
326
327
0
    if (pwri->pass == NULL) {
328
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_PASSWORD);
329
0
        return 0;
330
0
    }
331
0
    algtmp = pwri->keyEncryptionAlgorithm;
332
333
0
    if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) {
334
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
335
0
        return 0;
336
0
    }
337
338
0
    kekalg = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
339
0
                                       algtmp->parameter);
340
341
0
    if (kekalg == NULL) {
342
0
        ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);
343
0
        return 0;
344
0
    }
345
346
0
    OBJ_obj2txt(name, sizeof(name), kekalg->algorithm, 0);
347
0
    kekcipher = EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(cms_ctx), name,
348
0
                                 ossl_cms_ctx_get0_propq(cms_ctx));
349
350
0
    if (kekcipher == NULL) {
351
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
352
0
        goto err;
353
0
    }
354
355
0
    kekctx = EVP_CIPHER_CTX_new();
356
0
    if (kekctx == NULL) {
357
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
358
0
        goto err;
359
0
    }
360
    /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
361
0
    if (!EVP_CipherInit_ex(kekctx, kekcipher, NULL, NULL, NULL, en_de))
362
0
        goto err;
363
0
    EVP_CIPHER_CTX_set_padding(kekctx, 0);
364
0
    if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0) {
365
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
366
0
        goto err;
367
0
    }
368
369
0
    algtmp = pwri->keyDerivationAlgorithm;
370
371
    /* Finish password based key derivation to setup key in "ctx" */
372
373
0
    if (!EVP_PBE_CipherInit_ex(algtmp->algorithm,
374
0
                               (char *)pwri->pass, (int)pwri->passlen,
375
0
                               algtmp->parameter, kekctx, en_de,
376
0
                               cms_ctx->libctx, cms_ctx->propq)) {
377
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
378
0
        goto err;
379
0
    }
380
381
    /* Finally wrap/unwrap the key */
382
383
0
    if (en_de) {
384
385
0
        if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, kekctx, cms_ctx))
386
0
            goto err;
387
388
0
        key = OPENSSL_malloc(keylen);
389
390
0
        if (key == NULL)
391
0
            goto err;
392
393
0
        if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, kekctx, cms_ctx))
394
0
            goto err;
395
0
        pwri->encryptedKey->data = key;
396
0
        pwri->encryptedKey->length = (int)keylen;
397
0
    } else {
398
0
        key = OPENSSL_malloc(pwri->encryptedKey->length);
399
0
        if (key == NULL)
400
0
            goto err;
401
0
        if (!kek_unwrap_key(key, &keylen,
402
0
                            pwri->encryptedKey->data,
403
0
                            pwri->encryptedKey->length, kekctx)) {
404
0
            ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_FAILURE);
405
0
            goto err;
406
0
        }
407
408
0
        OPENSSL_clear_free(ec->key, ec->keylen);
409
0
        ec->key = key;
410
0
        ec->keylen = keylen;
411
412
0
    }
413
414
0
    r = 1;
415
416
0
 err:
417
0
    EVP_CIPHER_free(kekcipher);
418
0
    EVP_CIPHER_CTX_free(kekctx);
419
420
0
    if (!r)
421
0
        OPENSSL_free(key);
422
0
    X509_ALGOR_free(kekalg);
423
424
0
    return r;
425
426
0
}