Coverage Report

Created: 2024-07-27 06:39

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