Coverage Report

Created: 2025-08-28 07:07

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