Coverage Report

Created: 2025-08-28 07:07

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