Coverage Report

Created: 2026-07-23 06:28

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