Coverage Report

Created: 2025-12-31 06:58

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->value.sequence)) {
161
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
162
0
        goto err;
163
0
    }
164
0
    pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE;
165
166
0
    X509_ALGOR_free(encalg);
167
0
    encalg = NULL;
168
169
    /* Setup PBE algorithm */
170
171
0
    pwri->keyDerivationAlgorithm = PKCS5_pbkdf2_set_ex(iter, NULL, 0, -1, -1,
172
0
        cms_ctx->libctx);
173
174
0
    if (pwri->keyDerivationAlgorithm == NULL)
175
0
        goto err;
176
177
0
    CMS_RecipientInfo_set0_password(ri, pass, passlen);
178
0
    pwri->version = 0;
179
180
0
    if (!sk_CMS_RecipientInfo_push(ris, ri)) {
181
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
182
0
        goto err;
183
0
    }
184
185
0
    return ri;
186
187
0
err:
188
0
    EVP_CIPHER_CTX_free(ctx);
189
0
    if (ri)
190
0
        M_ASN1_free_of(ri, CMS_RecipientInfo);
191
0
    X509_ALGOR_free(encalg);
192
0
    return NULL;
193
0
}
194
195
/*
196
 * This is an implementation of the key wrapping mechanism in RFC3211, at
197
 * some point this should go into EVP.
198
 */
199
200
static int kek_unwrap_key(unsigned char *out, size_t *outlen,
201
    const unsigned char *in, size_t inlen,
202
    EVP_CIPHER_CTX *ctx)
203
0
{
204
0
    int blocklen = EVP_CIPHER_CTX_get_block_size(ctx);
205
0
    unsigned char *tmp;
206
0
    int outl, rv = 0;
207
208
0
    if (blocklen <= 0)
209
0
        return 0;
210
211
0
    if (inlen < 2 * (size_t)blocklen) {
212
        /* too small */
213
0
        return 0;
214
0
    }
215
0
    if (inlen > INT_MAX || inlen % blocklen) {
216
        /* Invalid size */
217
0
        return 0;
218
0
    }
219
0
    if ((tmp = OPENSSL_malloc(inlen)) == NULL)
220
0
        return 0;
221
    /* setup IV by decrypting last two blocks */
222
0
    if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,
223
0
            in + inlen - 2 * blocklen, blocklen * 2)
224
        /*
225
         * Do a decrypt of last decrypted block to set IV to correct value
226
         * output it to start of buffer so we don't corrupt decrypted block
227
         * this works because buffer is at least two block lengths long.
228
         */
229
0
        || !EVP_DecryptUpdate(ctx, tmp, &outl,
230
0
            tmp + inlen - blocklen, blocklen)
231
        /* Can now decrypt first n - 1 blocks */
232
0
        || !EVP_DecryptUpdate(ctx, tmp, &outl, in, (int)(inlen - blocklen))
233
234
        /* Reset IV to original value */
235
0
        || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)
236
        /* Decrypt again */
237
0
        || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, (int)inlen))
238
0
        goto err;
239
    /* Check check bytes */
240
0
    if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {
241
        /* Check byte failure */
242
0
        goto err;
243
0
    }
244
0
    if (inlen < 4 + (size_t)tmp[0]) {
245
        /* Invalid length value */
246
0
        goto err;
247
0
    }
248
0
    *outlen = (size_t)tmp[0];
249
0
    memcpy(out, tmp + 4, *outlen);
250
0
    rv = 1;
251
0
err:
252
0
    OPENSSL_clear_free(tmp, inlen);
253
0
    return rv;
254
0
}
255
256
static int kek_wrap_key(unsigned char *out, size_t *outlen,
257
    const unsigned char *in, size_t inlen,
258
    EVP_CIPHER_CTX *ctx, const CMS_CTX *cms_ctx)
259
0
{
260
0
    size_t blocklen = EVP_CIPHER_CTX_get_block_size(ctx);
261
0
    size_t olen;
262
0
    int dummy;
263
264
0
    if (blocklen == 0)
265
0
        return 0;
266
267
    /*
268
     * First decide length of output buffer: need header and round up to
269
     * multiple of block length.
270
     */
271
0
    olen = (inlen + 4 + blocklen - 1) / blocklen;
272
0
    olen *= blocklen;
273
0
    if (olen < 2 * blocklen) {
274
        /* Key too small */
275
0
        return 0;
276
0
    }
277
0
    if (inlen > 0xFF) {
278
        /* Key too large */
279
0
        return 0;
280
0
    }
281
0
    if (out) {
282
        /* Set header */
283
0
        out[0] = (unsigned char)inlen;
284
0
        out[1] = in[0] ^ 0xFF;
285
0
        out[2] = in[1] ^ 0xFF;
286
0
        out[3] = in[2] ^ 0xFF;
287
0
        memcpy(out + 4, in, inlen);
288
        /* Add random padding to end */
289
0
        if (olen > inlen + 4
290
0
            && RAND_bytes_ex(ossl_cms_ctx_get0_libctx(cms_ctx), out + 4 + inlen,
291
0
                   olen - 4 - inlen, 0)
292
0
                <= 0)
293
0
            return 0;
294
        /* Encrypt twice */
295
0
        if (!EVP_EncryptUpdate(ctx, out, &dummy, out, (int)olen)
296
0
            || !EVP_EncryptUpdate(ctx, out, &dummy, out, (int)olen))
297
0
            return 0;
298
0
    }
299
300
0
    *outlen = olen;
301
302
0
    return 1;
303
0
}
304
305
/* Encrypt/Decrypt content key in PWRI recipient info */
306
307
int ossl_cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo *cms,
308
    CMS_RecipientInfo *ri, int en_de)
309
0
{
310
0
    CMS_EncryptedContentInfo *ec;
311
0
    CMS_PasswordRecipientInfo *pwri;
312
0
    int r = 0;
313
0
    X509_ALGOR *algtmp, *kekalg = NULL;
314
0
    EVP_CIPHER_CTX *kekctx = NULL;
315
0
    char name[OSSL_MAX_NAME_SIZE];
316
0
    EVP_CIPHER *kekcipher;
317
0
    unsigned char *key = NULL;
318
0
    size_t keylen;
319
0
    const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
320
321
0
    ec = ossl_cms_get0_env_enc_content(cms);
322
323
0
    pwri = ri->d.pwri;
324
325
0
    if (pwri->pass == NULL) {
326
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_PASSWORD);
327
0
        return 0;
328
0
    }
329
0
    algtmp = pwri->keyEncryptionAlgorithm;
330
331
0
    if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) {
332
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
333
0
        return 0;
334
0
    }
335
336
0
    kekalg = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
337
0
        algtmp->parameter);
338
339
0
    if (kekalg == NULL) {
340
0
        ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);
341
0
        return 0;
342
0
    }
343
344
0
    OBJ_obj2txt(name, sizeof(name), kekalg->algorithm, 0);
345
0
    kekcipher = EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(cms_ctx), name,
346
0
        ossl_cms_ctx_get0_propq(cms_ctx));
347
348
0
    if (kekcipher == NULL) {
349
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
350
0
        goto err;
351
0
    }
352
353
0
    kekctx = EVP_CIPHER_CTX_new();
354
0
    if (kekctx == NULL) {
355
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
356
0
        goto err;
357
0
    }
358
    /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
359
0
    if (!EVP_CipherInit_ex(kekctx, kekcipher, NULL, NULL, NULL, en_de))
360
0
        goto err;
361
0
    EVP_CIPHER_CTX_set_padding(kekctx, 0);
362
0
    if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0) {
363
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
364
0
        goto err;
365
0
    }
366
367
0
    algtmp = pwri->keyDerivationAlgorithm;
368
369
    /* Finish password based key derivation to setup key in "ctx" */
370
371
0
    if (!EVP_PBE_CipherInit_ex(algtmp->algorithm,
372
0
            (char *)pwri->pass, (int)pwri->passlen,
373
0
            algtmp->parameter, kekctx, en_de,
374
0
            cms_ctx->libctx, cms_ctx->propq)) {
375
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
376
0
        goto err;
377
0
    }
378
379
    /* Finally wrap/unwrap the key */
380
381
0
    if (en_de) {
382
383
0
        if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, kekctx, cms_ctx))
384
0
            goto err;
385
386
0
        key = OPENSSL_malloc(keylen);
387
388
0
        if (key == NULL)
389
0
            goto err;
390
391
0
        if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, kekctx, cms_ctx))
392
0
            goto err;
393
0
        pwri->encryptedKey->data = key;
394
0
        pwri->encryptedKey->length = (int)keylen;
395
0
    } else {
396
0
        key = OPENSSL_malloc(pwri->encryptedKey->length);
397
0
        if (key == NULL)
398
0
            goto err;
399
0
        if (!kek_unwrap_key(key, &keylen,
400
0
                pwri->encryptedKey->data,
401
0
                pwri->encryptedKey->length, kekctx)) {
402
0
            ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_FAILURE);
403
0
            goto err;
404
0
        }
405
406
0
        OPENSSL_clear_free(ec->key, ec->keylen);
407
0
        ec->key = key;
408
0
        ec->keylen = keylen;
409
0
    }
410
411
0
    r = 1;
412
413
0
err:
414
0
    EVP_CIPHER_free(kekcipher);
415
0
    EVP_CIPHER_CTX_free(kekctx);
416
417
0
    if (!r)
418
0
        OPENSSL_free(key);
419
0
    X509_ALGOR_free(kekalg);
420
421
0
    return r;
422
0
}