Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/cms/cms_smime.c
Line
Count
Source
1
/*
2
 * Copyright 2008-2024 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/x509.h>
13
#include <openssl/x509v3.h>
14
#include <openssl/err.h>
15
#include <openssl/cms.h>
16
#include "cms_local.h"
17
#include "crypto/asn1.h"
18
19
static BIO *cms_get_text_bio(BIO *out, unsigned int flags)
20
0
{
21
0
    BIO *rbio;
22
23
0
    if (out == NULL)
24
0
        rbio = BIO_new(BIO_s_null());
25
0
    else if (flags & CMS_TEXT) {
26
0
        rbio = BIO_new(BIO_s_mem());
27
0
        BIO_set_mem_eof_return(rbio, 0);
28
0
    } else
29
0
        rbio = out;
30
0
    return rbio;
31
0
}
32
33
static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
34
0
{
35
0
    unsigned char buf[4096];
36
0
    int r = 0, i;
37
0
    BIO *tmpout;
38
39
0
    tmpout = cms_get_text_bio(out, flags);
40
41
0
    if (tmpout == NULL) {
42
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
43
0
        goto err;
44
0
    }
45
46
    /* Read all content through chain to process digest, decrypt etc */
47
0
    for (;;) {
48
0
        i = BIO_read(in, buf, sizeof(buf));
49
0
        if (i <= 0) {
50
0
            if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
51
0
                if (BIO_get_cipher_status(in) <= 0)
52
0
                    goto err;
53
0
            }
54
0
            if (i < 0)
55
0
                goto err;
56
0
            break;
57
0
        }
58
59
0
        if (tmpout != NULL && (BIO_write(tmpout, buf, i) != i))
60
0
            goto err;
61
0
    }
62
63
0
    if (flags & CMS_TEXT) {
64
0
        if (!SMIME_text(tmpout, out)) {
65
0
            ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR);
66
0
            goto err;
67
0
        }
68
0
    }
69
70
0
    r = 1;
71
0
err:
72
0
    if (tmpout != out)
73
0
        BIO_free(tmpout);
74
0
    return r;
75
0
}
76
77
static int check_content(CMS_ContentInfo *cms)
78
0
{
79
0
    ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
80
81
0
    if (pos == NULL || *pos == NULL) {
82
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
83
0
        return 0;
84
0
    }
85
0
    return 1;
86
0
}
87
88
static void do_free_upto(BIO *f, BIO *upto)
89
0
{
90
0
    if (upto != NULL) {
91
0
        BIO *tbio;
92
93
0
        do {
94
0
            tbio = BIO_pop(f);
95
0
            BIO_free(f);
96
0
            f = tbio;
97
0
        } while (f != NULL && f != upto);
98
0
    } else {
99
0
        BIO_free_all(f);
100
0
    }
101
0
}
102
103
int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
104
0
{
105
0
    BIO *cont;
106
0
    int r;
107
108
0
    if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
109
0
        ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_DATA);
110
0
        return 0;
111
0
    }
112
0
    cont = CMS_dataInit(cms, NULL);
113
0
    if (cont == NULL)
114
0
        return 0;
115
0
    r = cms_copy_content(out, cont, flags);
116
0
    BIO_free_all(cont);
117
0
    return r;
118
0
}
119
120
CMS_ContentInfo *CMS_data_create_ex(BIO *in, unsigned int flags,
121
    OSSL_LIB_CTX *libctx, const char *propq)
122
0
{
123
0
    CMS_ContentInfo *cms = ossl_cms_Data_create(libctx, propq);
124
125
0
    if (cms == NULL)
126
0
        return NULL;
127
128
0
    if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
129
0
        return cms;
130
131
0
    CMS_ContentInfo_free(cms);
132
0
    return NULL;
133
0
}
134
135
CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
136
0
{
137
0
    return CMS_data_create_ex(in, flags, NULL, NULL);
138
0
}
139
140
int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
141
    unsigned int flags)
142
0
{
143
0
    BIO *cont;
144
0
    int r;
145
146
0
    if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
147
0
        ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_DIGESTED_DATA);
148
0
        return 0;
149
0
    }
150
151
0
    if (dcont == NULL && !check_content(cms))
152
0
        return 0;
153
154
0
    cont = CMS_dataInit(cms, dcont);
155
0
    if (cont == NULL)
156
0
        return 0;
157
158
0
    r = cms_copy_content(out, cont, flags);
159
0
    if (r)
160
0
        r = ossl_cms_DigestedData_do_final(cms, cont, 1);
161
0
    do_free_upto(cont, dcont);
162
0
    return r;
163
0
}
164
165
CMS_ContentInfo *CMS_digest_create_ex(BIO *in, const EVP_MD *md,
166
    unsigned int flags, OSSL_LIB_CTX *ctx,
167
    const char *propq)
168
0
{
169
0
    CMS_ContentInfo *cms;
170
171
    /*
172
     * Because the EVP_MD is cached and can be a legacy algorithm, we
173
     * cannot fetch the algorithm if it isn't supplied.
174
     */
175
0
    if (md == NULL)
176
0
        md = EVP_sha1();
177
0
    cms = ossl_cms_DigestedData_create(md, ctx, propq);
178
0
    if (cms == NULL)
179
0
        return NULL;
180
181
0
    if (!(flags & CMS_DETACHED))
182
0
        CMS_set_detached(cms, 0);
183
184
0
    if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
185
0
        return cms;
186
187
0
    CMS_ContentInfo_free(cms);
188
0
    return NULL;
189
0
}
190
191
CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
192
    unsigned int flags)
193
0
{
194
0
    return CMS_digest_create_ex(in, md, flags, NULL, NULL);
195
0
}
196
197
int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
198
    const unsigned char *key, size_t keylen,
199
    BIO *dcont, BIO *out, unsigned int flags)
200
0
{
201
0
    BIO *cont;
202
0
    int r;
203
204
0
    if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
205
0
        ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENCRYPTED_DATA);
206
0
        return 0;
207
0
    }
208
209
0
    if (dcont == NULL && !check_content(cms))
210
0
        return 0;
211
212
0
    if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
213
0
        return 0;
214
0
    cont = CMS_dataInit(cms, dcont);
215
0
    if (cont == NULL)
216
0
        return 0;
217
0
    r = cms_copy_content(out, cont, flags);
218
0
    do_free_upto(cont, dcont);
219
0
    return r;
220
0
}
221
222
CMS_ContentInfo *CMS_EncryptedData_encrypt_ex(BIO *in, const EVP_CIPHER *cipher,
223
    const unsigned char *key,
224
    size_t keylen, unsigned int flags,
225
    OSSL_LIB_CTX *libctx,
226
    const char *propq)
227
0
{
228
0
    CMS_ContentInfo *cms;
229
230
0
    if (cipher == NULL) {
231
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_CIPHER);
232
0
        return NULL;
233
0
    }
234
0
    cms = CMS_ContentInfo_new_ex(libctx, propq);
235
0
    if (cms == NULL)
236
0
        return NULL;
237
0
    if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
238
0
        goto err;
239
240
0
    if (!(flags & CMS_DETACHED))
241
0
        CMS_set_detached(cms, 0);
242
243
0
    if ((flags & (CMS_STREAM | CMS_PARTIAL))
244
0
        || CMS_final(cms, in, NULL, flags))
245
0
        return cms;
246
247
0
err:
248
0
    CMS_ContentInfo_free(cms);
249
0
    return NULL;
250
0
}
251
252
CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
253
    const unsigned char *key,
254
    size_t keylen, unsigned int flags)
255
0
{
256
0
    return CMS_EncryptedData_encrypt_ex(in, cipher, key, keylen, flags, NULL,
257
0
        NULL);
258
0
}
259
260
static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
261
    X509_STORE *store,
262
    STACK_OF(X509) *untrusted,
263
    STACK_OF(X509_CRL) *crls,
264
    STACK_OF(X509) **chain,
265
    const CMS_CTX *cms_ctx)
266
0
{
267
0
    X509_STORE_CTX *ctx;
268
0
    X509 *signer;
269
0
    int i, j, r = 0;
270
271
0
    ctx = X509_STORE_CTX_new_ex(ossl_cms_ctx_get0_libctx(cms_ctx),
272
0
        ossl_cms_ctx_get0_propq(cms_ctx));
273
0
    if (ctx == NULL) {
274
0
        ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
275
0
        goto err;
276
0
    }
277
0
    CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
278
0
    if (!X509_STORE_CTX_init(ctx, store, signer, untrusted)) {
279
0
        ERR_raise(ERR_LIB_CMS, CMS_R_STORE_INIT_ERROR);
280
0
        goto err;
281
0
    }
282
0
    X509_STORE_CTX_set_default(ctx, "smime_sign");
283
0
    if (crls != NULL)
284
0
        X509_STORE_CTX_set0_crls(ctx, crls);
285
286
0
    i = X509_verify_cert(ctx);
287
0
    if (i <= 0) {
288
0
        j = X509_STORE_CTX_get_error(ctx);
289
0
        ERR_raise_data(ERR_LIB_CMS, CMS_R_CERTIFICATE_VERIFY_ERROR,
290
0
            "Verify error: %s", X509_verify_cert_error_string(j));
291
0
        goto err;
292
0
    }
293
0
    r = 1;
294
295
    /* also send back the trust chain when required */
296
0
    if (chain != NULL)
297
0
        *chain = X509_STORE_CTX_get1_chain(ctx);
298
0
err:
299
0
    X509_STORE_CTX_free(ctx);
300
0
    return r;
301
0
}
302
303
/* This strongly overlaps with PKCS7_verify() */
304
int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
305
    X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
306
0
{
307
0
    CMS_SignerInfo *si;
308
0
    STACK_OF(CMS_SignerInfo) *sinfos;
309
0
    STACK_OF(X509) *cms_certs = NULL;
310
0
    STACK_OF(X509_CRL) *crls = NULL;
311
0
    STACK_OF(X509) **si_chains = NULL;
312
0
    X509 *signer;
313
0
    int i, scount = 0, ret = 0;
314
0
    BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
315
0
    int cadesVerify = (flags & CMS_CADES) != 0;
316
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
317
318
0
    if (dcont == NULL && !check_content(cms))
319
0
        return 0;
320
0
    if (dcont != NULL && !(flags & CMS_BINARY)) {
321
0
        const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
322
323
0
        if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
324
0
            flags |= CMS_ASCIICRLF;
325
0
    }
326
327
    /* Attempt to find all signer certificates */
328
329
0
    sinfos = CMS_get0_SignerInfos(cms);
330
331
0
    if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
332
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_SIGNERS);
333
0
        goto err;
334
0
    }
335
336
0
    for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
337
0
        si = sk_CMS_SignerInfo_value(sinfos, i);
338
0
        CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
339
0
        if (signer != NULL)
340
0
            scount++;
341
0
    }
342
343
0
    if (scount != sk_CMS_SignerInfo_num(sinfos))
344
0
        scount += CMS_set1_signers_certs(cms, certs, flags);
345
346
0
    if (scount != sk_CMS_SignerInfo_num(sinfos)) {
347
0
        ERR_raise(ERR_LIB_CMS, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
348
0
        goto err;
349
0
    }
350
351
    /* Attempt to verify all signers certs */
352
    /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
353
354
0
    if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
355
0
        if (cadesVerify) {
356
            /* Certificate trust chain is required to check CAdES signature */
357
0
            si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
358
0
            if (si_chains == NULL)
359
0
                goto err;
360
0
        }
361
0
        cms_certs = CMS_get1_certs(cms);
362
0
        if (!(flags & CMS_NOCRL))
363
0
            crls = CMS_get1_crls(cms);
364
0
        for (i = 0; i < scount; i++) {
365
0
            si = sk_CMS_SignerInfo_value(sinfos, i);
366
367
0
            if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls,
368
0
                    si_chains ? &si_chains[i] : NULL,
369
0
                    ctx))
370
0
                goto err;
371
0
        }
372
0
    }
373
374
    /* Attempt to verify all SignerInfo signed attribute signatures */
375
376
0
    if ((flags & CMS_NO_ATTR_VERIFY) == 0 || cadesVerify) {
377
0
        for (i = 0; i < scount; i++) {
378
0
            si = sk_CMS_SignerInfo_value(sinfos, i);
379
0
            if (CMS_signed_get_attr_count(si) < 0)
380
0
                continue;
381
0
            if (CMS_SignerInfo_verify(si) <= 0)
382
0
                goto err;
383
0
            if (cadesVerify) {
384
0
                STACK_OF(X509) *si_chain = si_chains ? si_chains[i] : NULL;
385
386
0
                if (ossl_cms_check_signing_certs(si, si_chain) <= 0)
387
0
                    goto err;
388
0
            }
389
0
        }
390
0
    }
391
392
    /*
393
     * Performance optimization: if the content is a memory BIO then store
394
     * its contents in a temporary read only memory BIO. This avoids
395
     * potentially large numbers of slow copies of data which will occur when
396
     * reading from a read write memory BIO when signatures are calculated.
397
     */
398
399
0
    if (dcont != NULL && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
400
0
        char *ptr;
401
0
        long len;
402
403
0
        len = BIO_get_mem_data(dcont, &ptr);
404
0
        tmpin = (len == 0) ? dcont : BIO_new_mem_buf(ptr, len);
405
0
        if (tmpin == NULL) {
406
0
            ERR_raise(ERR_LIB_CMS, ERR_R_BIO_LIB);
407
0
            goto err2;
408
0
        }
409
0
    } else {
410
0
        tmpin = dcont;
411
0
    }
412
    /*
413
     * If not binary mode and detached generate digests by *writing* through
414
     * the BIO. That makes it possible to canonicalise the input.
415
     */
416
0
    if (!(flags & SMIME_BINARY) && dcont) {
417
        /*
418
         * Create output BIO so we can either handle text or to ensure
419
         * included content doesn't override detached content.
420
         */
421
0
        tmpout = cms_get_text_bio(out, flags);
422
0
        if (tmpout == NULL) {
423
0
            ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
424
0
            goto err;
425
0
        }
426
0
        cmsbio = CMS_dataInit(cms, tmpout);
427
0
        if (cmsbio == NULL)
428
0
            goto err;
429
        /*
430
         * Don't use SMIME_TEXT for verify: it adds headers and we want to
431
         * remove them.
432
         */
433
0
        if (!SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT))
434
0
            goto err;
435
436
0
        if (flags & CMS_TEXT) {
437
0
            if (!SMIME_text(tmpout, out)) {
438
0
                ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR);
439
0
                goto err;
440
0
            }
441
0
        }
442
0
    } else {
443
0
        cmsbio = CMS_dataInit(cms, tmpin);
444
0
        if (cmsbio == NULL)
445
0
            goto err;
446
447
0
        if (!cms_copy_content(out, cmsbio, flags))
448
0
            goto err;
449
0
    }
450
0
    if (!(flags & CMS_NO_CONTENT_VERIFY)) {
451
0
        for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
452
0
            si = sk_CMS_SignerInfo_value(sinfos, i);
453
0
            if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
454
0
                ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_VERIFY_ERROR);
455
0
                goto err;
456
0
            }
457
0
        }
458
0
    }
459
460
0
    ret = 1;
461
0
err:
462
0
    if (!(flags & SMIME_BINARY) && dcont) {
463
0
        do_free_upto(cmsbio, tmpout);
464
0
        if (tmpin != dcont)
465
0
            BIO_free(tmpin);
466
0
    } else {
467
0
        if (dcont && (tmpin == dcont))
468
0
            do_free_upto(cmsbio, dcont);
469
0
        else
470
0
            BIO_free_all(cmsbio);
471
0
    }
472
473
0
    if (out != tmpout)
474
0
        BIO_free_all(tmpout);
475
476
0
err2:
477
0
    if (si_chains != NULL) {
478
0
        for (i = 0; i < scount; ++i)
479
0
            OSSL_STACK_OF_X509_free(si_chains[i]);
480
0
        OPENSSL_free(si_chains);
481
0
    }
482
0
    OSSL_STACK_OF_X509_free(cms_certs);
483
0
    sk_X509_CRL_pop_free(crls, X509_CRL_free);
484
485
0
    return ret;
486
0
}
487
488
int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
489
    STACK_OF(X509) *certs,
490
    X509_STORE *store, unsigned int flags)
491
0
{
492
0
    int r;
493
494
0
    flags &= ~(CMS_DETACHED | CMS_TEXT);
495
0
    r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
496
0
    if (r <= 0)
497
0
        return r;
498
0
    return ossl_cms_Receipt_verify(rcms, ocms);
499
0
}
500
501
CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
502
    STACK_OF(X509) *certs, BIO *data,
503
    unsigned int flags, OSSL_LIB_CTX *libctx,
504
    const char *propq)
505
0
{
506
0
    CMS_ContentInfo *cms;
507
0
    int i;
508
509
0
    cms = CMS_ContentInfo_new_ex(libctx, propq);
510
0
    if (cms == NULL || !CMS_SignedData_init(cms)) {
511
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
512
0
        goto err;
513
0
    }
514
0
    if (flags & CMS_ASCIICRLF
515
0
        && !CMS_set1_eContentType(cms,
516
0
            OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF))) {
517
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
518
0
        goto err;
519
0
    }
520
521
0
    if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
522
0
        ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
523
0
        goto err;
524
0
    }
525
526
0
    for (i = 0; i < sk_X509_num(certs); i++) {
527
0
        X509 *x = sk_X509_value(certs, i);
528
529
0
        if (!CMS_add1_cert(cms, x)) {
530
0
            ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
531
0
            goto err;
532
0
        }
533
0
    }
534
535
0
    if (!(flags & CMS_DETACHED))
536
0
        CMS_set_detached(cms, 0);
537
538
0
    if ((flags & (CMS_STREAM | CMS_PARTIAL))
539
0
        || CMS_final(cms, data, NULL, flags))
540
0
        return cms;
541
0
    else
542
0
        goto err;
543
544
0
err:
545
0
    CMS_ContentInfo_free(cms);
546
0
    return NULL;
547
0
}
548
549
CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
550
    BIO *data, unsigned int flags)
551
0
{
552
0
    return CMS_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
553
0
}
554
555
CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
556
    X509 *signcert, EVP_PKEY *pkey,
557
    STACK_OF(X509) *certs, unsigned int flags)
558
0
{
559
0
    CMS_SignerInfo *rct_si;
560
0
    CMS_ContentInfo *cms = NULL;
561
0
    ASN1_OCTET_STRING **pos, *os = NULL;
562
0
    BIO *rct_cont = NULL;
563
0
    int r = 0;
564
0
    const CMS_CTX *ctx = si->cms_ctx;
565
566
0
    flags &= ~(CMS_STREAM | CMS_TEXT);
567
    /* Not really detached but avoids content being allocated */
568
0
    flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
569
0
    if (pkey == NULL || signcert == NULL) {
570
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY_OR_CERT);
571
0
        return NULL;
572
0
    }
573
574
    /* Initialize signed data */
575
576
0
    cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags,
577
0
        ossl_cms_ctx_get0_libctx(ctx),
578
0
        ossl_cms_ctx_get0_propq(ctx));
579
0
    if (cms == NULL)
580
0
        goto err;
581
582
    /* Set inner content type to signed receipt */
583
0
    if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
584
0
        goto err;
585
586
0
    rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
587
0
    if (!rct_si) {
588
0
        ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
589
0
        goto err;
590
0
    }
591
592
0
    os = ossl_cms_encode_Receipt(si);
593
0
    if (os == NULL)
594
0
        goto err;
595
596
    /* Set content to digest */
597
0
    rct_cont = BIO_new_mem_buf(os->data, os->length);
598
0
    if (rct_cont == NULL)
599
0
        goto err;
600
601
    /* Add msgSigDigest attribute */
602
603
0
    if (!ossl_cms_msgSigDigest_add1(rct_si, si))
604
0
        goto err;
605
606
    /* Finalize structure */
607
0
    if (!CMS_final(cms, rct_cont, NULL, flags))
608
0
        goto err;
609
610
    /* Set embedded content */
611
0
    pos = CMS_get0_content(cms);
612
0
    if (pos == NULL)
613
0
        goto err;
614
0
    *pos = os;
615
616
0
    r = 1;
617
618
0
err:
619
0
    BIO_free(rct_cont);
620
0
    if (r)
621
0
        return cms;
622
0
    CMS_ContentInfo_free(cms);
623
0
    ASN1_OCTET_STRING_free(os);
624
0
    return NULL;
625
0
}
626
627
CMS_ContentInfo *CMS_encrypt_ex(STACK_OF(X509) *certs, BIO *data,
628
    const EVP_CIPHER *cipher, unsigned int flags,
629
    OSSL_LIB_CTX *libctx, const char *propq)
630
0
{
631
0
    CMS_ContentInfo *cms;
632
0
    int i;
633
0
    X509 *recip;
634
635
0
    cms = (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
636
0
        ? CMS_AuthEnvelopedData_create_ex(cipher, libctx, propq)
637
0
        : CMS_EnvelopedData_create_ex(cipher, libctx, propq);
638
0
    if (cms == NULL) {
639
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
640
0
        goto err;
641
0
    }
642
0
    for (i = 0; i < sk_X509_num(certs); i++) {
643
0
        recip = sk_X509_value(certs, i);
644
0
        if (!CMS_add1_recipient_cert(cms, recip, flags)) {
645
0
            ERR_raise(ERR_LIB_CMS, CMS_R_RECIPIENT_ERROR);
646
0
            goto err;
647
0
        }
648
0
    }
649
650
0
    if (!(flags & CMS_DETACHED))
651
0
        CMS_set_detached(cms, 0);
652
653
0
    if ((flags & (CMS_STREAM | CMS_PARTIAL))
654
0
        || CMS_final(cms, data, NULL, flags))
655
0
        return cms;
656
0
    else
657
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
658
659
0
err:
660
0
    CMS_ContentInfo_free(cms);
661
0
    return NULL;
662
0
}
663
664
CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
665
    const EVP_CIPHER *cipher, unsigned int flags)
666
0
{
667
0
    return CMS_encrypt_ex(certs, data, cipher, flags, NULL, NULL);
668
0
}
669
670
static int cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms,
671
    CMS_RecipientInfo *ri,
672
    EVP_PKEY *pk, X509 *cert, X509 *peer)
673
0
{
674
0
    int i;
675
0
    STACK_OF(CMS_RecipientEncryptedKey) *reks;
676
0
    CMS_RecipientEncryptedKey *rek;
677
678
0
    reks = CMS_RecipientInfo_kari_get0_reks(ri);
679
0
    for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
680
0
        int rv;
681
682
0
        rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
683
0
        if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
684
0
            continue;
685
0
        CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, peer);
686
0
        rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
687
0
        CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
688
0
        if (rv > 0)
689
0
            return 1;
690
0
        return cert == NULL ? 0 : -1;
691
0
    }
692
0
    return 0;
693
0
}
694
695
int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
696
0
{
697
0
    return CMS_decrypt_set1_pkey_and_peer(cms, pk, cert, NULL);
698
0
}
699
700
int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk,
701
    X509 *cert, X509 *peer)
702
0
{
703
0
    STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
704
0
    CMS_RecipientInfo *ri;
705
0
    int i, r, cms_pkey_ri_type;
706
0
    int debug = 0, match_ri = 0;
707
0
    CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
708
709
    /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
710
0
    if (ec != NULL) {
711
0
        OPENSSL_clear_free(ec->key, ec->keylen);
712
0
        ec->key = NULL;
713
0
        ec->keylen = 0;
714
0
    }
715
716
0
    if (ris != NULL && ec != NULL)
717
0
        debug = ec->debug;
718
719
0
    cms_pkey_ri_type = ossl_cms_pkey_get_ri_type(pk);
720
0
    if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
721
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
722
0
        return 0;
723
0
    }
724
725
0
    for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
726
0
        int ri_type;
727
728
0
        ri = sk_CMS_RecipientInfo_value(ris, i);
729
0
        ri_type = CMS_RecipientInfo_type(ri);
730
0
        if (!ossl_cms_pkey_is_ri_type_supported(pk, ri_type))
731
0
            continue;
732
0
        match_ri = 1;
733
0
        if (ri_type == CMS_RECIPINFO_AGREE) {
734
0
            r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
735
0
            if (r > 0)
736
0
                return 1;
737
0
            if (r < 0)
738
0
                return 0;
739
0
        }
740
        /* If we have a cert, try matching RecipientInfo, else try them all */
741
0
        else if (cert == NULL || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
742
0
            EVP_PKEY_up_ref(pk);
743
0
            CMS_RecipientInfo_set0_pkey(ri, pk);
744
0
            r = CMS_RecipientInfo_decrypt(cms, ri);
745
0
            CMS_RecipientInfo_set0_pkey(ri, NULL);
746
0
            if (cert != NULL) {
747
                /*
748
                 * If not debugging clear any error and return success to
749
                 * avoid leaking of information useful to MMA
750
                 */
751
0
                if (!debug) {
752
0
                    ERR_clear_error();
753
0
                    return 1;
754
0
                }
755
0
                if (r > 0)
756
0
                    return 1;
757
0
                ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
758
0
                return 0;
759
0
            }
760
            /*
761
             * If no cert and not debugging don't leave loop after first
762
             * successful decrypt. Always attempt to decrypt all recipients
763
             * to avoid leaking timing of a successful decrypt.
764
             */
765
0
            else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
766
0
                return 1;
767
0
        }
768
0
    }
769
    /* If no cert, key transport and not debugging always return success */
770
0
    if (cert == NULL
771
0
        && cms_pkey_ri_type == CMS_RECIPINFO_TRANS
772
0
        && match_ri
773
0
        && !debug) {
774
0
        ERR_clear_error();
775
0
        return 1;
776
0
    }
777
778
0
    if (!match_ri)
779
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
780
0
    return 0;
781
0
}
782
783
int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
784
    unsigned char *key, size_t keylen,
785
    const unsigned char *id, size_t idlen)
786
0
{
787
0
    STACK_OF(CMS_RecipientInfo) *ris;
788
0
    CMS_RecipientInfo *ri;
789
0
    int i, r, match_ri = 0;
790
791
0
    ris = CMS_get0_RecipientInfos(cms);
792
0
    for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
793
0
        ri = sk_CMS_RecipientInfo_value(ris, i);
794
0
        if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
795
0
            continue;
796
797
        /* If we have an id, try matching RecipientInfo, else try them all */
798
0
        if (id == NULL
799
0
            || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
800
0
            match_ri = 1;
801
0
            CMS_RecipientInfo_set0_key(ri, key, keylen);
802
0
            r = CMS_RecipientInfo_decrypt(cms, ri);
803
0
            CMS_RecipientInfo_set0_key(ri, NULL, 0);
804
0
            if (r > 0)
805
0
                return 1;
806
0
            if (id != NULL) {
807
0
                ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
808
0
                return 0;
809
0
            }
810
0
            ERR_clear_error();
811
0
        }
812
0
    }
813
814
0
    if (!match_ri)
815
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
816
0
    return 0;
817
0
}
818
819
int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
820
    unsigned char *pass, ossl_ssize_t passlen)
821
0
{
822
0
    STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
823
0
    CMS_RecipientInfo *ri;
824
0
    int i, r, match_ri = 0;
825
0
    CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
826
827
    /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
828
0
    if (ec != NULL) {
829
0
        OPENSSL_clear_free(ec->key, ec->keylen);
830
0
        ec->key = NULL;
831
0
        ec->keylen = 0;
832
0
    }
833
834
0
    for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
835
0
        ri = sk_CMS_RecipientInfo_value(ris, i);
836
0
        if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
837
0
            continue;
838
839
        /* Must try each PasswordRecipientInfo */
840
0
        match_ri = 1;
841
0
        CMS_RecipientInfo_set0_password(ri, pass, passlen);
842
0
        r = CMS_RecipientInfo_decrypt(cms, ri);
843
0
        CMS_RecipientInfo_set0_password(ri, NULL, 0);
844
0
        if (r > 0)
845
0
            return 1;
846
0
    }
847
848
0
    if (!match_ri)
849
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
850
0
    return 0;
851
0
}
852
853
int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
854
    BIO *dcont, BIO *out, unsigned int flags)
855
0
{
856
0
    int r;
857
0
    BIO *cont;
858
0
    CMS_EncryptedContentInfo *ec;
859
0
    int nid = OBJ_obj2nid(CMS_get0_type(cms));
860
861
0
    if (nid != NID_pkcs7_enveloped
862
0
        && nid != NID_id_smime_ct_authEnvelopedData) {
863
0
        ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENVELOPED_DATA);
864
0
        return 0;
865
0
    }
866
0
    if (dcont == NULL && !check_content(cms))
867
0
        return 0;
868
0
    ec = ossl_cms_get0_env_enc_content(cms);
869
0
    ec->debug = (flags & CMS_DEBUG_DECRYPT) != 0;
870
0
    ec->havenocert = cert == NULL;
871
0
    if (pk == NULL && cert == NULL && dcont == NULL && out == NULL)
872
0
        return 1;
873
0
    if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert))
874
0
        return 0;
875
0
    cont = CMS_dataInit(cms, dcont);
876
0
    if (cont == NULL)
877
0
        return 0;
878
0
    r = cms_copy_content(out, cont, flags);
879
0
    do_free_upto(cont, dcont);
880
0
    return r;
881
0
}
882
883
int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
884
0
{
885
0
    BIO *cmsbio;
886
0
    int ret = 0;
887
888
0
    if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
889
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
890
0
        return 0;
891
0
    }
892
893
0
    if (!SMIME_crlf_copy(data, cmsbio, flags)) {
894
0
        goto err;
895
0
    }
896
897
0
    (void)BIO_flush(cmsbio);
898
899
0
    if (!CMS_dataFinal(cms, cmsbio)) {
900
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
901
0
        goto err;
902
0
    }
903
904
0
    ret = 1;
905
906
0
err:
907
0
    do_free_upto(cmsbio, dcont);
908
909
0
    return ret;
910
0
}
911
912
int CMS_final_digest(CMS_ContentInfo *cms,
913
    const unsigned char *md, unsigned int mdlen,
914
    BIO *dcont, unsigned int flags)
915
0
{
916
0
    BIO *cmsbio;
917
0
    int ret = 0;
918
919
0
    if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
920
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
921
0
        return 0;
922
0
    }
923
924
0
    (void)BIO_flush(cmsbio);
925
926
0
    if (!ossl_cms_DataFinal(cms, cmsbio, md, mdlen)) {
927
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
928
0
        goto err;
929
0
    }
930
0
    ret = 1;
931
932
0
err:
933
0
    do_free_upto(cmsbio, dcont);
934
0
    return ret;
935
0
}
936
937
#ifndef OPENSSL_NO_ZLIB
938
939
int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
940
    unsigned int flags)
941
{
942
    BIO *cont;
943
    int r;
944
945
    if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
946
        ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
947
        return 0;
948
    }
949
950
    if (dcont == NULL && !check_content(cms))
951
        return 0;
952
953
    cont = CMS_dataInit(cms, dcont);
954
    if (cont == NULL)
955
        return 0;
956
    r = cms_copy_content(out, cont, flags);
957
    do_free_upto(cont, dcont);
958
    return r;
959
}
960
961
CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
962
{
963
    CMS_ContentInfo *cms;
964
965
    if (comp_nid <= 0)
966
        comp_nid = NID_zlib_compression;
967
    cms = ossl_cms_CompressedData_create(comp_nid, NULL, NULL);
968
    if (cms == NULL)
969
        return NULL;
970
971
    if (!(flags & CMS_DETACHED))
972
        CMS_set_detached(cms, 0);
973
974
    if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
975
        return cms;
976
977
    CMS_ContentInfo_free(cms);
978
    return NULL;
979
}
980
981
#else
982
983
int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
984
    unsigned int flags)
985
0
{
986
0
    ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
987
0
    return 0;
988
0
}
989
990
CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
991
0
{
992
0
    ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
993
    return NULL;
994
0
}
995
996
#endif