Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/cms/cms_env.c
Line
Count
Source
1
/*
2
 * Copyright 2008-2026 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/evp.h>
17
#include "internal/sizes.h"
18
#include "crypto/asn1.h"
19
#include "crypto/evp.h"
20
#include "crypto/x509.h"
21
#include "cms_local.h"
22
23
/* CMS EnvelopedData Utilities */
24
static void cms_env_set_version(CMS_EnvelopedData *env);
25
26
5.09k
#define CMS_ENVELOPED_STANDARD 1
27
0
#define CMS_ENVELOPED_AUTH 2
28
29
static int cms_get_enveloped_type_simple(const CMS_ContentInfo *cms)
30
6.57k
{
31
6.57k
    int nid = OBJ_obj2nid(cms->contentType);
32
33
6.57k
    switch (nid) {
34
2.54k
    case NID_pkcs7_enveloped:
35
2.54k
        return CMS_ENVELOPED_STANDARD;
36
37
0
    case NID_id_smime_ct_authEnvelopedData:
38
0
        return CMS_ENVELOPED_AUTH;
39
40
4.03k
    default:
41
4.03k
        return 0;
42
6.57k
    }
43
6.57k
}
44
45
static int cms_get_enveloped_type(const CMS_ContentInfo *cms)
46
6.57k
{
47
6.57k
    int ret = cms_get_enveloped_type_simple(cms);
48
49
6.57k
    if (ret == 0)
50
6.57k
        ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
51
6.57k
    return ret;
52
6.57k
}
53
54
CMS_EnvelopedData *ossl_cms_get0_enveloped(CMS_ContentInfo *cms)
55
0
{
56
0
    if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) {
57
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
58
0
        return NULL;
59
0
    }
60
0
    return cms->d.envelopedData;
61
0
}
62
63
CMS_AuthEnvelopedData *ossl_cms_get0_auth_enveloped(CMS_ContentInfo *cms)
64
0
{
65
0
    if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_authEnvelopedData) {
66
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
67
0
        return NULL;
68
0
    }
69
0
    return cms->d.authEnvelopedData;
70
0
}
71
72
static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
73
0
{
74
0
    if (cms->d.other == NULL) {
75
0
        cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
76
0
        if (cms->d.envelopedData == NULL) {
77
0
            ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
78
0
            return NULL;
79
0
        }
80
0
        cms->d.envelopedData->version = 0;
81
0
        cms->d.envelopedData->encryptedContentInfo->contentType = OBJ_nid2obj(NID_pkcs7_data);
82
0
        ASN1_OBJECT_free(cms->contentType);
83
0
        cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
84
0
        return cms->d.envelopedData;
85
0
    }
86
0
    return ossl_cms_get0_enveloped(cms);
87
0
}
88
89
static CMS_AuthEnvelopedData *
90
cms_auth_enveloped_data_init(CMS_ContentInfo *cms)
91
0
{
92
0
    if (cms->d.other == NULL) {
93
0
        cms->d.authEnvelopedData = M_ASN1_new_of(CMS_AuthEnvelopedData);
94
0
        if (cms->d.authEnvelopedData == NULL) {
95
0
            ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
96
0
            return NULL;
97
0
        }
98
        /* Defined in RFC 5083 - Section 2.1. "AuthEnvelopedData Type" */
99
0
        cms->d.authEnvelopedData->version = 0;
100
0
        cms->d.authEnvelopedData->authEncryptedContentInfo->contentType = OBJ_nid2obj(NID_pkcs7_data);
101
0
        ASN1_OBJECT_free(cms->contentType);
102
0
        cms->contentType = OBJ_nid2obj(NID_id_smime_ct_authEnvelopedData);
103
0
        return cms->d.authEnvelopedData;
104
0
    }
105
0
    return ossl_cms_get0_auth_enveloped(cms);
106
0
}
107
108
int ossl_cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
109
0
{
110
0
    EVP_PKEY *pkey;
111
0
    int i;
112
0
    if (ri->type == CMS_RECIPINFO_TRANS)
113
0
        pkey = ri->d.ktri->pkey;
114
0
    else if (ri->type == CMS_RECIPINFO_AGREE) {
115
0
        EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
116
117
0
        if (pctx == NULL)
118
0
            return 0;
119
0
        pkey = EVP_PKEY_CTX_get0_pkey(pctx);
120
0
        if (pkey == NULL)
121
0
            return 0;
122
0
    } else
123
0
        return 0;
124
125
0
    if (EVP_PKEY_is_a(pkey, "DHX") || EVP_PKEY_is_a(pkey, "DH"))
126
0
        return ossl_cms_dh_envelope(ri, cmd);
127
0
    else if (EVP_PKEY_is_a(pkey, "EC"))
128
0
        return ossl_cms_ecdh_envelope(ri, cmd);
129
0
    else if (EVP_PKEY_is_a(pkey, "RSA"))
130
0
        return ossl_cms_rsa_envelope(ri, cmd);
131
132
    /* Something else? We'll give engines etc a chance to handle this */
133
0
    if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
134
0
        return 1;
135
0
    i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
136
0
    if (i == -2) {
137
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
138
0
        return 0;
139
0
    }
140
0
    if (i <= 0) {
141
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
142
0
        return 0;
143
0
    }
144
0
    return 1;
145
0
}
146
147
CMS_EncryptedContentInfo *ossl_cms_get0_env_enc_content(const CMS_ContentInfo *cms)
148
0
{
149
0
    switch (cms_get_enveloped_type(cms)) {
150
0
    case CMS_ENVELOPED_STANDARD:
151
0
        return cms->d.envelopedData == NULL ? NULL
152
0
                                            : cms->d.envelopedData->encryptedContentInfo;
153
154
0
    case CMS_ENVELOPED_AUTH:
155
0
        return cms->d.authEnvelopedData == NULL ? NULL
156
0
                                                : cms->d.authEnvelopedData->authEncryptedContentInfo;
157
158
0
    default:
159
0
        return NULL;
160
0
    }
161
0
}
162
163
STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
164
6.57k
{
165
6.57k
    switch (cms_get_enveloped_type(cms)) {
166
2.54k
    case CMS_ENVELOPED_STANDARD:
167
2.54k
        return cms->d.envelopedData->recipientInfos;
168
169
0
    case CMS_ENVELOPED_AUTH:
170
0
        return cms->d.authEnvelopedData->recipientInfos;
171
172
4.03k
    default:
173
4.03k
        return NULL;
174
6.57k
    }
175
6.57k
}
176
177
void ossl_cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms)
178
4.39k
{
179
4.39k
    int i;
180
4.39k
    CMS_RecipientInfo *ri;
181
4.39k
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
182
4.39k
    STACK_OF(CMS_RecipientInfo) *rinfos = CMS_get0_RecipientInfos(cms);
183
184
16.2k
    for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
185
11.8k
        ri = sk_CMS_RecipientInfo_value(rinfos, i);
186
11.8k
        if (ri != NULL) {
187
11.8k
            switch (ri->type) {
188
3.29k
            case CMS_RECIPINFO_AGREE:
189
3.29k
                ri->d.kari->cms_ctx = ctx;
190
3.29k
                break;
191
2.13k
            case CMS_RECIPINFO_TRANS:
192
2.13k
                ri->d.ktri->cms_ctx = ctx;
193
2.13k
                ossl_x509_set0_libctx(ri->d.ktri->recip,
194
2.13k
                    ossl_cms_ctx_get0_libctx(ctx),
195
2.13k
                    ossl_cms_ctx_get0_propq(ctx));
196
2.13k
                break;
197
369
            case CMS_RECIPINFO_KEK:
198
369
                ri->d.kekri->cms_ctx = ctx;
199
369
                break;
200
1.46k
            case CMS_RECIPINFO_PASS:
201
1.46k
                ri->d.pwri->cms_ctx = ctx;
202
1.46k
                break;
203
4.63k
            default:
204
4.63k
                break;
205
11.8k
            }
206
11.8k
        }
207
11.8k
    }
208
4.39k
}
209
210
int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
211
0
{
212
0
    return ri->type;
213
0
}
214
215
EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
216
0
{
217
0
    if (ri->type == CMS_RECIPINFO_TRANS)
218
0
        return ri->d.ktri->pctx;
219
0
    else if (ri->type == CMS_RECIPINFO_AGREE)
220
0
        return ri->d.kari->pctx;
221
0
    return NULL;
222
0
}
223
224
CMS_ContentInfo *CMS_EnvelopedData_create_ex(const EVP_CIPHER *cipher,
225
    OSSL_LIB_CTX *libctx,
226
    const char *propq)
227
0
{
228
0
    CMS_ContentInfo *cms;
229
0
    CMS_EnvelopedData *env;
230
231
0
    cms = CMS_ContentInfo_new_ex(libctx, propq);
232
0
    if (cms == NULL)
233
0
        goto err;
234
0
    env = cms_enveloped_data_init(cms);
235
0
    if (env == NULL)
236
0
        goto err;
237
238
0
    if (!ossl_cms_EncryptedContent_init(env->encryptedContentInfo, cipher, NULL,
239
0
            0, ossl_cms_get0_cmsctx(cms)))
240
0
        goto err;
241
0
    return cms;
242
0
err:
243
0
    CMS_ContentInfo_free(cms);
244
0
    ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
245
0
    return NULL;
246
0
}
247
248
CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
249
0
{
250
0
    return CMS_EnvelopedData_create_ex(cipher, NULL, NULL);
251
0
}
252
253
BIO *CMS_EnvelopedData_decrypt(CMS_EnvelopedData *env, BIO *detached_data,
254
    EVP_PKEY *pkey, X509 *cert,
255
    ASN1_OCTET_STRING *secret, unsigned int flags,
256
    OSSL_LIB_CTX *libctx, const char *propq)
257
0
{
258
0
    CMS_ContentInfo *ci;
259
0
    BIO *bio = NULL;
260
0
    int res = 0;
261
262
0
    if (env == NULL) {
263
0
        ERR_raise(ERR_LIB_CMS, ERR_R_PASSED_NULL_PARAMETER);
264
0
        return NULL;
265
0
    }
266
267
0
    if ((ci = CMS_ContentInfo_new_ex(libctx, propq)) == NULL
268
0
        || (bio = BIO_new(BIO_s_mem())) == NULL)
269
0
        goto end;
270
0
    ci->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
271
0
    ci->d.envelopedData = env;
272
0
    if (secret != NULL
273
0
        && CMS_decrypt_set1_password(ci, (unsigned char *)ASN1_STRING_get0_data(secret),
274
0
               ASN1_STRING_length(secret))
275
0
            != 1)
276
0
        goto end;
277
0
    res = CMS_decrypt(ci, secret == NULL ? pkey : NULL,
278
0
        secret == NULL ? cert : NULL, detached_data, bio, flags);
279
280
0
end:
281
0
    if (ci != NULL) {
282
0
        ci->d.envelopedData = NULL; /* do not indirectly free |env| */
283
0
        ci->contentType = NULL;
284
0
    }
285
0
    CMS_ContentInfo_free(ci);
286
0
    if (!res) {
287
0
        BIO_free(bio);
288
0
        bio = NULL;
289
0
    }
290
0
    return bio;
291
0
}
292
293
CMS_ContentInfo *
294
CMS_AuthEnvelopedData_create_ex(const EVP_CIPHER *cipher, OSSL_LIB_CTX *libctx,
295
    const char *propq)
296
0
{
297
0
    CMS_ContentInfo *cms;
298
0
    CMS_AuthEnvelopedData *aenv;
299
300
0
    cms = CMS_ContentInfo_new_ex(libctx, propq);
301
0
    if (cms == NULL)
302
0
        goto merr;
303
0
    aenv = cms_auth_enveloped_data_init(cms);
304
0
    if (aenv == NULL)
305
0
        goto merr;
306
0
    if (!ossl_cms_EncryptedContent_init(aenv->authEncryptedContentInfo,
307
0
            cipher, NULL, 0,
308
0
            ossl_cms_get0_cmsctx(cms)))
309
0
        goto merr;
310
0
    return cms;
311
0
merr:
312
0
    CMS_ContentInfo_free(cms);
313
0
    ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
314
0
    return NULL;
315
0
}
316
317
CMS_ContentInfo *CMS_AuthEnvelopedData_create(const EVP_CIPHER *cipher)
318
0
{
319
0
    return CMS_AuthEnvelopedData_create_ex(cipher, NULL, NULL);
320
0
}
321
322
/* Key Transport Recipient Info (KTRI) routines */
323
324
/* Initialise a ktri based on passed certificate and key */
325
326
static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
327
    EVP_PKEY *pk, unsigned int flags,
328
    const CMS_CTX *ctx)
329
0
{
330
0
    CMS_KeyTransRecipientInfo *ktri;
331
0
    int idtype;
332
333
0
    ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
334
0
    if (!ri->d.ktri)
335
0
        return 0;
336
0
    ri->type = CMS_RECIPINFO_TRANS;
337
338
0
    ktri = ri->d.ktri;
339
0
    ktri->cms_ctx = ctx;
340
341
0
    if (flags & CMS_USE_KEYID) {
342
0
        ktri->version = 2;
343
0
        idtype = CMS_RECIPINFO_KEYIDENTIFIER;
344
0
    } else {
345
0
        ktri->version = 0;
346
0
        idtype = CMS_RECIPINFO_ISSUER_SERIAL;
347
0
    }
348
349
    /*
350
     * Not a typo: RecipientIdentifier and SignerIdentifier are the same
351
     * structure.
352
     */
353
354
0
    if (!ossl_cms_set1_SignerIdentifier(ktri->rid, recip, idtype, ctx))
355
0
        return 0;
356
357
0
    X509_up_ref(recip);
358
0
    EVP_PKEY_up_ref(pk);
359
360
0
    ktri->pkey = pk;
361
0
    ktri->recip = recip;
362
363
0
    if (flags & CMS_KEY_PARAM) {
364
0
        ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
365
0
            ktri->pkey,
366
0
            ossl_cms_ctx_get0_propq(ctx));
367
0
        if (ktri->pctx == NULL)
368
0
            return 0;
369
0
        if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
370
0
            return 0;
371
0
    } else if (!ossl_cms_env_asn1_ctrl(ri, 0))
372
0
        return 0;
373
0
    return 1;
374
0
}
375
376
/*
377
 * Add a recipient certificate using appropriate type of RecipientInfo
378
 */
379
380
CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
381
    EVP_PKEY *originatorPrivKey,
382
    X509 *originator, unsigned int flags)
383
0
{
384
0
    CMS_RecipientInfo *ri = NULL;
385
0
    STACK_OF(CMS_RecipientInfo) *ris;
386
0
    EVP_PKEY *pk = NULL;
387
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
388
389
0
    ris = CMS_get0_RecipientInfos(cms);
390
0
    if (ris == NULL)
391
0
        goto err;
392
393
    /* Initialize recipient info */
394
0
    ri = M_ASN1_new_of(CMS_RecipientInfo);
395
0
    if (ri == NULL) {
396
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
397
0
        goto err;
398
0
    }
399
400
0
    pk = X509_get0_pubkey(recip);
401
0
    if (pk == NULL) {
402
0
        ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_GETTING_PUBLIC_KEY);
403
0
        goto err;
404
0
    }
405
406
0
    switch (ossl_cms_pkey_get_ri_type(pk)) {
407
408
0
    case CMS_RECIPINFO_TRANS:
409
0
        if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags, ctx))
410
0
            goto err;
411
0
        break;
412
413
0
    case CMS_RECIPINFO_AGREE:
414
0
        if (!ossl_cms_RecipientInfo_kari_init(ri, recip, pk, originator,
415
0
                originatorPrivKey, flags, ctx))
416
0
            goto err;
417
0
        break;
418
419
0
    default:
420
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
421
0
        goto err;
422
0
    }
423
424
0
    if (!sk_CMS_RecipientInfo_push(ris, ri)) {
425
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
426
0
        goto err;
427
0
    }
428
429
0
    return ri;
430
431
0
err:
432
0
    M_ASN1_free_of(ri, CMS_RecipientInfo);
433
0
    return NULL;
434
0
}
435
436
CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip,
437
    unsigned int flags)
438
0
{
439
0
    return CMS_add1_recipient(cms, recip, NULL, NULL, flags);
440
0
}
441
442
int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
443
    EVP_PKEY **pk, X509 **recip,
444
    X509_ALGOR **palg)
445
0
{
446
0
    CMS_KeyTransRecipientInfo *ktri;
447
0
    if (ri->type != CMS_RECIPINFO_TRANS) {
448
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
449
0
        return 0;
450
0
    }
451
452
0
    ktri = ri->d.ktri;
453
454
0
    if (pk)
455
0
        *pk = ktri->pkey;
456
0
    if (recip)
457
0
        *recip = ktri->recip;
458
0
    if (palg)
459
0
        *palg = ktri->keyEncryptionAlgorithm;
460
0
    return 1;
461
0
}
462
463
int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
464
    ASN1_OCTET_STRING **keyid,
465
    X509_NAME **issuer,
466
    ASN1_INTEGER **sno)
467
0
{
468
0
    CMS_KeyTransRecipientInfo *ktri;
469
0
    if (ri->type != CMS_RECIPINFO_TRANS) {
470
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
471
0
        return 0;
472
0
    }
473
0
    ktri = ri->d.ktri;
474
475
0
    return ossl_cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer,
476
0
        sno);
477
0
}
478
479
int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
480
0
{
481
0
    if (ri->type != CMS_RECIPINFO_TRANS) {
482
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
483
0
        return -2;
484
0
    }
485
0
    return ossl_cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
486
0
}
487
488
int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
489
0
{
490
0
    if (ri->type != CMS_RECIPINFO_TRANS) {
491
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
492
0
        return 0;
493
0
    }
494
0
    EVP_PKEY_free(ri->d.ktri->pkey);
495
0
    ri->d.ktri->pkey = pkey;
496
0
    return 1;
497
0
}
498
499
/* Encrypt content key in key transport recipient info */
500
501
static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms,
502
    CMS_RecipientInfo *ri)
503
0
{
504
0
    CMS_KeyTransRecipientInfo *ktri;
505
0
    CMS_EncryptedContentInfo *ec;
506
0
    EVP_PKEY_CTX *pctx;
507
0
    unsigned char *ek = NULL;
508
0
    size_t eklen;
509
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
510
511
0
    int ret = 0;
512
513
0
    if (ri->type != CMS_RECIPINFO_TRANS) {
514
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
515
0
        return 0;
516
0
    }
517
0
    ktri = ri->d.ktri;
518
0
    ec = ossl_cms_get0_env_enc_content(cms);
519
520
0
    pctx = ktri->pctx;
521
522
0
    if (pctx) {
523
0
        if (!ossl_cms_env_asn1_ctrl(ri, 0))
524
0
            goto err;
525
0
    } else {
526
0
        pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
527
0
            ktri->pkey,
528
0
            ossl_cms_ctx_get0_propq(ctx));
529
0
        if (pctx == NULL)
530
0
            return 0;
531
532
0
        if (EVP_PKEY_encrypt_init(pctx) <= 0)
533
0
            goto err;
534
0
    }
535
536
0
    if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
537
0
        goto err;
538
539
0
    ek = OPENSSL_malloc(eklen);
540
0
    if (ek == NULL)
541
0
        goto err;
542
543
0
    if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
544
0
        goto err;
545
546
0
    ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
547
0
    ek = NULL;
548
549
0
    ret = 1;
550
551
0
err:
552
0
    EVP_PKEY_CTX_free(pctx);
553
0
    ktri->pctx = NULL;
554
0
    OPENSSL_free(ek);
555
0
    return ret;
556
0
}
557
558
/* Decrypt content key from KTRI */
559
560
static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
561
    CMS_RecipientInfo *ri)
562
0
{
563
0
    CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
564
0
    EVP_PKEY *pkey = ktri->pkey;
565
0
    unsigned char *ek = NULL;
566
0
    size_t eklen;
567
0
    int ret = 0;
568
0
    size_t fixlen = 0;
569
0
    const EVP_CIPHER *cipher = NULL;
570
0
    EVP_CIPHER *fetched_cipher = NULL;
571
0
    CMS_EncryptedContentInfo *ec;
572
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
573
0
    OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
574
0
    const char *propq = ossl_cms_ctx_get0_propq(ctx);
575
576
0
    ec = ossl_cms_get0_env_enc_content(cms);
577
578
0
    if (ktri->pkey == NULL) {
579
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
580
0
        return 0;
581
0
    }
582
583
0
    if (cms->d.envelopedData->encryptedContentInfo->havenocert
584
0
        && !cms->d.envelopedData->encryptedContentInfo->debug) {
585
0
        X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
586
0
        char name[OSSL_MAX_NAME_SIZE];
587
588
0
        OBJ_obj2txt(name, sizeof(name), calg->algorithm, 0);
589
590
0
        (void)ERR_set_mark();
591
0
        fetched_cipher = EVP_CIPHER_fetch(libctx, name, propq);
592
593
0
        if (fetched_cipher != NULL)
594
0
            cipher = fetched_cipher;
595
0
        else
596
0
            cipher = EVP_get_cipherbyobj(calg->algorithm);
597
0
        if (cipher == NULL) {
598
0
            (void)ERR_clear_last_mark();
599
0
            ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
600
0
            return 0;
601
0
        }
602
0
        (void)ERR_pop_to_mark();
603
604
0
        fixlen = EVP_CIPHER_get_key_length(cipher);
605
0
        EVP_CIPHER_free(fetched_cipher);
606
0
    }
607
608
0
    ktri->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
609
0
    if (ktri->pctx == NULL)
610
0
        goto err;
611
612
0
    if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
613
0
        goto err;
614
615
0
    if (!ossl_cms_env_asn1_ctrl(ri, 1))
616
0
        goto err;
617
618
0
    if (evp_pkey_decrypt_alloc(ktri->pctx, &ek, &eklen, fixlen,
619
0
            ktri->encryptedKey->data,
620
0
            ktri->encryptedKey->length)
621
0
        <= 0)
622
0
        goto err;
623
624
0
    ret = 1;
625
626
0
    OPENSSL_clear_free(ec->key, ec->keylen);
627
0
    ec->key = ek;
628
0
    ec->keylen = eklen;
629
630
0
err:
631
0
    EVP_PKEY_CTX_free(ktri->pctx);
632
0
    ktri->pctx = NULL;
633
0
    if (!ret)
634
0
        OPENSSL_free(ek);
635
636
0
    return ret;
637
0
}
638
639
/* Key Encrypted Key (KEK) RecipientInfo routines */
640
641
int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
642
    const unsigned char *id, size_t idlen)
643
0
{
644
0
    ASN1_OCTET_STRING tmp_os;
645
0
    CMS_KEKRecipientInfo *kekri;
646
0
    if (ri->type != CMS_RECIPINFO_KEK) {
647
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
648
0
        return -2;
649
0
    }
650
0
    kekri = ri->d.kekri;
651
0
    tmp_os.type = V_ASN1_OCTET_STRING;
652
0
    tmp_os.flags = 0;
653
0
    tmp_os.data = (unsigned char *)id;
654
0
    tmp_os.length = (int)idlen;
655
0
    return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
656
0
}
657
658
/* For now hard code AES key wrap info */
659
660
static size_t aes_wrap_keylen(int nid)
661
0
{
662
0
    switch (nid) {
663
0
    case NID_id_aes128_wrap:
664
0
        return 16;
665
666
0
    case NID_id_aes192_wrap:
667
0
        return 24;
668
669
0
    case NID_id_aes256_wrap:
670
0
        return 32;
671
672
0
    default:
673
0
        return 0;
674
0
    }
675
0
}
676
677
CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
678
    unsigned char *key, size_t keylen,
679
    unsigned char *id, size_t idlen,
680
    ASN1_GENERALIZEDTIME *date,
681
    ASN1_OBJECT *otherTypeId,
682
    ASN1_TYPE *otherType)
683
0
{
684
0
    CMS_RecipientInfo *ri = NULL;
685
0
    CMS_KEKRecipientInfo *kekri;
686
0
    STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
687
688
0
    if (ris == NULL)
689
0
        goto err;
690
691
0
    if (nid == NID_undef) {
692
0
        switch (keylen) {
693
0
        case 16:
694
0
            nid = NID_id_aes128_wrap;
695
0
            break;
696
697
0
        case 24:
698
0
            nid = NID_id_aes192_wrap;
699
0
            break;
700
701
0
        case 32:
702
0
            nid = NID_id_aes256_wrap;
703
0
            break;
704
705
0
        default:
706
0
            ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
707
0
            goto err;
708
0
        }
709
710
0
    } else {
711
712
0
        size_t exp_keylen = aes_wrap_keylen(nid);
713
714
0
        if (!exp_keylen) {
715
0
            ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEK_ALGORITHM);
716
0
            goto err;
717
0
        }
718
719
0
        if (keylen != exp_keylen) {
720
0
            ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
721
0
            goto err;
722
0
        }
723
0
    }
724
725
    /* Initialize recipient info */
726
0
    ri = M_ASN1_new_of(CMS_RecipientInfo);
727
0
    if (!ri) {
728
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
729
0
        goto err;
730
0
    }
731
732
0
    ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
733
0
    if (!ri->d.kekri) {
734
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
735
0
        goto err;
736
0
    }
737
0
    ri->type = CMS_RECIPINFO_KEK;
738
739
0
    kekri = ri->d.kekri;
740
741
0
    if (otherTypeId) {
742
0
        kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
743
0
        if (kekri->kekid->other == NULL) {
744
0
            ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
745
0
            goto err;
746
0
        }
747
0
    }
748
749
0
    if (!sk_CMS_RecipientInfo_push(ris, ri)) {
750
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
751
0
        goto err;
752
0
    }
753
754
    /* After this point no calls can fail */
755
756
0
    kekri->version = 4;
757
758
0
    kekri->key = key;
759
0
    kekri->keylen = keylen;
760
761
0
    ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
762
763
0
    kekri->kekid->date = date;
764
765
0
    if (kekri->kekid->other) {
766
0
        kekri->kekid->other->keyAttrId = otherTypeId;
767
0
        kekri->kekid->other->keyAttr = otherType;
768
0
    }
769
770
0
    (void)X509_ALGOR_set0(kekri->keyEncryptionAlgorithm, OBJ_nid2obj(nid),
771
0
        V_ASN1_UNDEF, NULL); /* cannot fail */
772
773
0
    return ri;
774
775
0
err:
776
0
    M_ASN1_free_of(ri, CMS_RecipientInfo);
777
0
    return NULL;
778
0
}
779
780
int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
781
    X509_ALGOR **palg,
782
    ASN1_OCTET_STRING **pid,
783
    ASN1_GENERALIZEDTIME **pdate,
784
    ASN1_OBJECT **potherid,
785
    ASN1_TYPE **pothertype)
786
0
{
787
0
    CMS_KEKIdentifier *rkid;
788
0
    if (ri->type != CMS_RECIPINFO_KEK) {
789
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
790
0
        return 0;
791
0
    }
792
0
    rkid = ri->d.kekri->kekid;
793
0
    if (palg)
794
0
        *palg = ri->d.kekri->keyEncryptionAlgorithm;
795
0
    if (pid)
796
0
        *pid = rkid->keyIdentifier;
797
0
    if (pdate)
798
0
        *pdate = rkid->date;
799
0
    if (potherid) {
800
0
        if (rkid->other)
801
0
            *potherid = rkid->other->keyAttrId;
802
0
        else
803
0
            *potherid = NULL;
804
0
    }
805
0
    if (pothertype) {
806
0
        if (rkid->other)
807
0
            *pothertype = rkid->other->keyAttr;
808
0
        else
809
0
            *pothertype = NULL;
810
0
    }
811
0
    return 1;
812
0
}
813
814
int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
815
    unsigned char *key, size_t keylen)
816
0
{
817
0
    CMS_KEKRecipientInfo *kekri;
818
0
    if (ri->type != CMS_RECIPINFO_KEK) {
819
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
820
0
        return 0;
821
0
    }
822
823
0
    kekri = ri->d.kekri;
824
0
    kekri->key = key;
825
0
    kekri->keylen = keylen;
826
0
    return 1;
827
0
}
828
829
static EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen, const CMS_CTX *ctx)
830
0
{
831
0
    const char *alg = NULL;
832
833
0
    switch (keylen) {
834
0
    case 16:
835
0
        alg = "AES-128-WRAP";
836
0
        break;
837
0
    case 24:
838
0
        alg = "AES-192-WRAP";
839
0
        break;
840
0
    case 32:
841
0
        alg = "AES-256-WRAP";
842
0
        break;
843
0
    default:
844
0
        return NULL;
845
0
    }
846
0
    return EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(ctx), alg,
847
0
        ossl_cms_ctx_get0_propq(ctx));
848
0
}
849
850
/* Encrypt content key in KEK recipient info */
851
852
static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
853
    CMS_RecipientInfo *ri)
854
0
{
855
0
    CMS_EncryptedContentInfo *ec;
856
0
    CMS_KEKRecipientInfo *kekri;
857
0
    unsigned char *wkey = NULL;
858
0
    int wkeylen;
859
0
    int r = 0;
860
0
    EVP_CIPHER *cipher = NULL;
861
0
    int outlen = 0;
862
0
    EVP_CIPHER_CTX *ctx = NULL;
863
0
    const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
864
865
0
    ec = ossl_cms_get0_env_enc_content(cms);
866
0
    if (ec == NULL)
867
0
        return 0;
868
869
0
    kekri = ri->d.kekri;
870
871
0
    if (kekri->key == NULL) {
872
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
873
0
        return 0;
874
0
    }
875
876
0
    cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
877
0
    if (cipher == NULL) {
878
0
        ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
879
0
        goto err;
880
0
    }
881
882
    /* 8 byte prefix for AES wrap ciphers */
883
0
    wkey = OPENSSL_malloc(ec->keylen + 8);
884
0
    if (wkey == NULL)
885
0
        goto err;
886
887
0
    ctx = EVP_CIPHER_CTX_new();
888
0
    if (ctx == NULL) {
889
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
890
0
        goto err;
891
0
    }
892
893
0
    EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
894
0
    if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
895
0
        || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen)
896
0
        || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) {
897
0
        ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
898
0
        goto err;
899
0
    }
900
0
    wkeylen += outlen;
901
0
    if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) {
902
0
        ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
903
0
        goto err;
904
0
    }
905
906
0
    ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
907
908
0
    r = 1;
909
910
0
err:
911
0
    EVP_CIPHER_free(cipher);
912
0
    if (!r)
913
0
        OPENSSL_free(wkey);
914
0
    EVP_CIPHER_CTX_free(ctx);
915
916
0
    return r;
917
0
}
918
919
/* Decrypt content key in KEK recipient info */
920
921
static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
922
    CMS_RecipientInfo *ri)
923
0
{
924
0
    CMS_EncryptedContentInfo *ec;
925
0
    CMS_KEKRecipientInfo *kekri;
926
0
    unsigned char *ukey = NULL;
927
0
    int ukeylen;
928
0
    int r = 0, wrap_nid;
929
0
    EVP_CIPHER *cipher = NULL;
930
0
    int outlen = 0;
931
0
    EVP_CIPHER_CTX *ctx = NULL;
932
0
    const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
933
934
0
    ec = ossl_cms_get0_env_enc_content(cms);
935
0
    if (ec == NULL)
936
0
        return 0;
937
938
0
    kekri = ri->d.kekri;
939
940
0
    if (!kekri->key) {
941
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
942
0
        return 0;
943
0
    }
944
945
0
    wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
946
0
    if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
947
0
        ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
948
0
        return 0;
949
0
    }
950
951
    /* If encrypted key length is invalid don't bother */
952
953
0
    if (kekri->encryptedKey->length < 16) {
954
0
        ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
955
0
        goto err;
956
0
    }
957
958
0
    cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
959
0
    if (cipher == NULL) {
960
0
        ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
961
0
        goto err;
962
0
    }
963
964
0
    ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
965
0
    if (ukey == NULL)
966
0
        goto err;
967
968
0
    ctx = EVP_CIPHER_CTX_new();
969
0
    if (ctx == NULL) {
970
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
971
0
        goto err;
972
0
    }
973
974
0
    if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
975
0
        || !EVP_DecryptUpdate(ctx, ukey, &ukeylen,
976
0
            kekri->encryptedKey->data,
977
0
            kekri->encryptedKey->length)
978
0
        || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) {
979
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_ERROR);
980
0
        goto err;
981
0
    }
982
0
    ukeylen += outlen;
983
984
0
    OPENSSL_clear_free(ec->key, ec->keylen);
985
0
    ec->key = ukey;
986
0
    ec->keylen = ukeylen;
987
988
0
    r = 1;
989
990
0
err:
991
0
    EVP_CIPHER_free(cipher);
992
0
    if (!r)
993
0
        OPENSSL_free(ukey);
994
0
    EVP_CIPHER_CTX_free(ctx);
995
996
0
    return r;
997
0
}
998
999
int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
1000
0
{
1001
0
    switch (ri->type) {
1002
0
    case CMS_RECIPINFO_TRANS:
1003
0
        return cms_RecipientInfo_ktri_decrypt(cms, ri);
1004
1005
0
    case CMS_RECIPINFO_KEK:
1006
0
        return cms_RecipientInfo_kekri_decrypt(cms, ri);
1007
1008
0
    case CMS_RECIPINFO_PASS:
1009
0
        return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 0);
1010
1011
0
    default:
1012
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
1013
0
        return 0;
1014
0
    }
1015
0
}
1016
1017
int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
1018
0
{
1019
0
    switch (ri->type) {
1020
0
    case CMS_RECIPINFO_TRANS:
1021
0
        return cms_RecipientInfo_ktri_encrypt(cms, ri);
1022
1023
0
    case CMS_RECIPINFO_AGREE:
1024
0
        return ossl_cms_RecipientInfo_kari_encrypt(cms, ri);
1025
1026
0
    case CMS_RECIPINFO_KEK:
1027
0
        return cms_RecipientInfo_kekri_encrypt(cms, ri);
1028
1029
0
    case CMS_RECIPINFO_PASS:
1030
0
        return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 1);
1031
1032
0
    default:
1033
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
1034
0
        return 0;
1035
0
    }
1036
0
}
1037
1038
/* Check structures and fixup version numbers (if necessary) */
1039
1040
static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
1041
0
{
1042
0
    CMS_OriginatorInfo *org = env->originatorInfo;
1043
0
    int i;
1044
0
    if (org == NULL)
1045
0
        return;
1046
0
    for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
1047
0
        CMS_CertificateChoices *cch;
1048
0
        cch = sk_CMS_CertificateChoices_value(org->certificates, i);
1049
0
        if (cch->type == CMS_CERTCHOICE_OTHER) {
1050
0
            env->version = 4;
1051
0
            return;
1052
0
        } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
1053
0
            if (env->version < 3)
1054
0
                env->version = 3;
1055
0
        }
1056
0
    }
1057
1058
0
    for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
1059
0
        CMS_RevocationInfoChoice *rch;
1060
0
        rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
1061
0
        if (rch->type == CMS_REVCHOICE_OTHER) {
1062
0
            env->version = 4;
1063
0
            return;
1064
0
        }
1065
0
    }
1066
0
}
1067
1068
static void cms_env_set_version(CMS_EnvelopedData *env)
1069
0
{
1070
0
    int i;
1071
0
    CMS_RecipientInfo *ri;
1072
1073
    /*
1074
     * Can't set version higher than 4 so if 4 or more already nothing to do.
1075
     */
1076
0
    if (env->version >= 4)
1077
0
        return;
1078
1079
0
    cms_env_set_originfo_version(env);
1080
1081
0
    if (env->version >= 3)
1082
0
        return;
1083
1084
0
    for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
1085
0
        ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
1086
0
        if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
1087
0
            env->version = 3;
1088
0
            return;
1089
0
        } else if (ri->type != CMS_RECIPINFO_TRANS
1090
0
            || ri->d.ktri->version != 0) {
1091
0
            env->version = 2;
1092
0
        }
1093
0
    }
1094
0
    if (env->originatorInfo || env->unprotectedAttrs)
1095
0
        env->version = 2;
1096
0
    if (env->version == 2)
1097
0
        return;
1098
0
    env->version = 0;
1099
0
}
1100
1101
static int cms_env_encrypt_content_key(const CMS_ContentInfo *cms,
1102
    STACK_OF(CMS_RecipientInfo) *ris)
1103
0
{
1104
0
    int i;
1105
0
    CMS_RecipientInfo *ri;
1106
1107
0
    for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
1108
0
        ri = sk_CMS_RecipientInfo_value(ris, i);
1109
0
        if (CMS_RecipientInfo_encrypt(cms, ri) <= 0)
1110
0
            return -1;
1111
0
    }
1112
0
    return 1;
1113
0
}
1114
1115
static void cms_env_clear_ec(CMS_EncryptedContentInfo *ec)
1116
0
{
1117
0
    ec->cipher = NULL;
1118
0
    OPENSSL_clear_free(ec->key, ec->keylen);
1119
0
    ec->key = NULL;
1120
0
    ec->keylen = 0;
1121
0
}
1122
1123
static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms)
1124
0
{
1125
0
    CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo;
1126
0
    BIO *contentBio = ossl_cms_EncryptedContent_init_bio(ec,
1127
0
        ossl_cms_get0_cmsctx(cms), 0);
1128
0
    EVP_CIPHER_CTX *ctx = NULL;
1129
1130
0
    if (contentBio == NULL)
1131
0
        return NULL;
1132
1133
0
    BIO_get_cipher_ctx(contentBio, &ctx);
1134
0
    if (ctx == NULL) {
1135
0
        BIO_free(contentBio);
1136
0
        return NULL;
1137
0
    }
1138
    /*
1139
     * If the selected cipher supports unprotected attributes,
1140
     * deal with it using special ctrl function
1141
     */
1142
0
    if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
1143
0
            & EVP_CIPH_FLAG_CIPHER_WITH_MAC)
1144
0
            != 0
1145
0
        && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0,
1146
0
               cms->d.envelopedData->unprotectedAttrs)
1147
0
            <= 0) {
1148
0
        BIO_free(contentBio);
1149
0
        return NULL;
1150
0
    }
1151
0
    return contentBio;
1152
0
}
1153
1154
static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms)
1155
0
{
1156
0
    CMS_EncryptedContentInfo *ec;
1157
0
    STACK_OF(CMS_RecipientInfo) *rinfos;
1158
0
    int ok = 0;
1159
0
    BIO *ret;
1160
0
    CMS_EnvelopedData *env = cms->d.envelopedData;
1161
1162
    /* Get BIO first to set up key */
1163
1164
0
    ec = env->encryptedContentInfo;
1165
0
    ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms), 0);
1166
1167
    /* If error end of processing */
1168
0
    if (!ret)
1169
0
        return ret;
1170
1171
    /* Now encrypt content key according to each RecipientInfo type */
1172
0
    rinfos = env->recipientInfos;
1173
0
    if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1174
0
        ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1175
0
        goto err;
1176
0
    }
1177
1178
    /* And finally set the version */
1179
0
    cms_env_set_version(env);
1180
1181
0
    ok = 1;
1182
1183
0
err:
1184
0
    cms_env_clear_ec(ec);
1185
0
    if (ok)
1186
0
        return ret;
1187
0
    BIO_free(ret);
1188
0
    return NULL;
1189
0
}
1190
1191
BIO *ossl_cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
1192
0
{
1193
0
    if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) {
1194
        /* If cipher is set it's encryption */
1195
0
        return cms_EnvelopedData_Encryption_init_bio(cms);
1196
0
    }
1197
1198
    /* If cipher is not set it's decryption */
1199
0
    return cms_EnvelopedData_Decryption_init_bio(cms);
1200
0
}
1201
1202
BIO *ossl_cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms)
1203
0
{
1204
0
    CMS_EncryptedContentInfo *ec;
1205
0
    STACK_OF(CMS_RecipientInfo) *rinfos;
1206
0
    int ok = 0;
1207
0
    BIO *ret;
1208
0
    CMS_AuthEnvelopedData *aenv = cms->d.authEnvelopedData;
1209
1210
    /* Get BIO first to set up key */
1211
0
    ec = aenv->authEncryptedContentInfo;
1212
    /* Set tag for decryption */
1213
0
    if (ec->cipher == NULL) {
1214
0
        ec->tag = aenv->mac->data;
1215
0
        ec->taglen = aenv->mac->length;
1216
0
    }
1217
0
    ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms), 1);
1218
1219
    /* If error or no cipher end of processing */
1220
0
    if (ret == NULL || ec->cipher == NULL)
1221
0
        return ret;
1222
1223
    /* Now encrypt content key according to each RecipientInfo type */
1224
0
    rinfos = aenv->recipientInfos;
1225
0
    if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1226
0
        ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1227
0
        goto err;
1228
0
    }
1229
1230
    /* And finally set the version */
1231
0
    aenv->version = 0;
1232
1233
0
    ok = 1;
1234
1235
0
err:
1236
0
    cms_env_clear_ec(ec);
1237
0
    if (ok)
1238
0
        return ret;
1239
0
    BIO_free(ret);
1240
0
    return NULL;
1241
0
}
1242
1243
int ossl_cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain)
1244
0
{
1245
0
    CMS_EnvelopedData *env = NULL;
1246
0
    EVP_CIPHER_CTX *ctx = NULL;
1247
0
    BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER);
1248
1249
0
    env = ossl_cms_get0_enveloped(cms);
1250
0
    if (env == NULL)
1251
0
        return 0;
1252
1253
0
    if (mbio == NULL) {
1254
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
1255
0
        return 0;
1256
0
    }
1257
1258
0
    BIO_get_cipher_ctx(mbio, &ctx);
1259
1260
    /*
1261
     * If the selected cipher supports unprotected attributes,
1262
     * deal with it using special ctrl function
1263
     */
1264
0
    if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
1265
0
            & EVP_CIPH_FLAG_CIPHER_WITH_MAC)
1266
0
        != 0) {
1267
0
        if (env->unprotectedAttrs == NULL)
1268
0
            env->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null();
1269
1270
0
        if (env->unprotectedAttrs == NULL) {
1271
0
            ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
1272
0
            return 0;
1273
0
        }
1274
1275
0
        if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED,
1276
0
                1, env->unprotectedAttrs)
1277
0
            <= 0) {
1278
0
            ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
1279
0
            return 0;
1280
0
        }
1281
0
    }
1282
1283
0
    cms_env_set_version(cms->d.envelopedData);
1284
0
    return 1;
1285
0
}
1286
1287
int ossl_cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio)
1288
0
{
1289
0
    EVP_CIPHER_CTX *ctx;
1290
0
    unsigned char *tag = NULL;
1291
0
    int taglen, ok = 0;
1292
1293
0
    BIO_get_cipher_ctx(cmsbio, &ctx);
1294
1295
    /*
1296
     * The tag is set only for encryption. There is nothing to do for
1297
     * decryption.
1298
     */
1299
0
    if (!EVP_CIPHER_CTX_is_encrypting(ctx))
1300
0
        return 1;
1301
1302
0
    taglen = EVP_CIPHER_CTX_get_tag_length(ctx);
1303
0
    if (taglen <= 0
1304
0
        || (tag = OPENSSL_malloc(taglen)) == NULL
1305
0
        || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
1306
0
               tag)
1307
0
            <= 0) {
1308
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_GET_TAG);
1309
0
        goto err;
1310
0
    }
1311
1312
0
    if (!ASN1_OCTET_STRING_set(cms->d.authEnvelopedData->mac, tag, taglen))
1313
0
        goto err;
1314
1315
0
    ok = 1;
1316
0
err:
1317
0
    OPENSSL_free(tag);
1318
0
    return ok;
1319
0
}
1320
1321
/*
1322
 * Get RecipientInfo type (if any) supported by a key (public or private). To
1323
 * retain compatibility with previous behaviour if the ctrl value isn't
1324
 * supported we assume key transport.
1325
 */
1326
int ossl_cms_pkey_get_ri_type(EVP_PKEY *pk)
1327
0
{
1328
    /* Check types that we know about */
1329
0
    if (EVP_PKEY_is_a(pk, "DH"))
1330
0
        return CMS_RECIPINFO_AGREE;
1331
0
    else if (EVP_PKEY_is_a(pk, "DHX"))
1332
0
        return CMS_RECIPINFO_AGREE;
1333
0
    else if (EVP_PKEY_is_a(pk, "DSA"))
1334
0
        return CMS_RECIPINFO_NONE;
1335
0
    else if (EVP_PKEY_is_a(pk, "EC"))
1336
0
        return CMS_RECIPINFO_AGREE;
1337
0
    else if (EVP_PKEY_is_a(pk, "RSA"))
1338
0
        return CMS_RECIPINFO_TRANS;
1339
1340
    /*
1341
     * Otherwise this might ben an engine implementation, so see if we can get
1342
     * the type from the ameth.
1343
     */
1344
0
    if (pk->ameth && pk->ameth->pkey_ctrl) {
1345
0
        int i, r;
1346
0
        i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
1347
0
        if (i > 0)
1348
0
            return r;
1349
0
    }
1350
0
    return CMS_RECIPINFO_TRANS;
1351
0
}
1352
1353
int ossl_cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type)
1354
0
{
1355
0
    int supportedRiType;
1356
1357
0
    if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) {
1358
0
        int i, r;
1359
1360
0
        i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED,
1361
0
            ri_type, &r);
1362
0
        if (i > 0)
1363
0
            return r;
1364
0
    }
1365
1366
0
    supportedRiType = ossl_cms_pkey_get_ri_type(pk);
1367
0
    if (supportedRiType < 0)
1368
0
        return 0;
1369
1370
0
    return (supportedRiType == ri_type);
1371
0
}