Coverage Report

Created: 2025-06-13 06:58

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