Coverage Report

Created: 2025-12-31 06:58

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