Coverage Report

Created: 2025-06-13 06:58

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