Coverage Report

Created: 2023-09-25 06:41

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