Coverage Report

Created: 2025-08-29 06:32

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