Coverage Report

Created: 2025-12-31 06:58

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