Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/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
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
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
303
0
}
304
305
/* This strongly overlaps with PKCS7_verify() */
306
int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
307
               X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
308
0
{
309
0
    CMS_SignerInfo *si;
310
0
    STACK_OF(CMS_SignerInfo) *sinfos;
311
0
    STACK_OF(X509) *cms_certs = NULL;
312
0
    STACK_OF(X509_CRL) *crls = NULL;
313
0
    STACK_OF(X509) **si_chains = NULL;
314
0
    X509 *signer;
315
0
    int i, scount = 0, ret = 0;
316
0
    BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
317
0
    int cadesVerify = (flags & CMS_CADES) != 0;
318
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
319
320
0
    if (dcont == NULL && !check_content(cms))
321
0
        return 0;
322
0
    if (dcont != NULL && !(flags & CMS_BINARY)) {
323
0
        const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
324
325
0
        if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
326
0
            flags |= CMS_ASCIICRLF;
327
0
    }
328
329
    /* Attempt to find all signer certificates */
330
331
0
    sinfos = CMS_get0_SignerInfos(cms);
332
333
0
    if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
334
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_SIGNERS);
335
0
        goto err;
336
0
    }
337
338
0
    for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
339
0
        si = sk_CMS_SignerInfo_value(sinfos, i);
340
0
        CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
341
0
        if (signer != NULL)
342
0
            scount++;
343
0
    }
344
345
0
    if (scount != sk_CMS_SignerInfo_num(sinfos))
346
0
        scount += CMS_set1_signers_certs(cms, certs, flags);
347
348
0
    if (scount != sk_CMS_SignerInfo_num(sinfos)) {
349
0
        ERR_raise(ERR_LIB_CMS, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
350
0
        goto err;
351
0
    }
352
353
    /* Attempt to verify all signers certs */
354
    /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
355
356
0
    if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
357
0
        if (cadesVerify) {
358
            /* Certificate trust chain is required to check CAdES signature */
359
0
            si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
360
0
            if (si_chains == NULL)
361
0
                goto err;
362
0
        }
363
0
        cms_certs = CMS_get1_certs(cms);
364
0
        if (!(flags & CMS_NOCRL))
365
0
            crls = CMS_get1_crls(cms);
366
0
        for (i = 0; i < scount; i++) {
367
0
            si = sk_CMS_SignerInfo_value(sinfos, i);
368
369
0
            if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls,
370
0
                                            si_chains ? &si_chains[i] : NULL,
371
0
                                            ctx))
372
0
                goto err;
373
0
        }
374
0
    }
375
376
    /* Attempt to verify all SignerInfo signed attribute signatures */
377
378
0
    if ((flags & CMS_NO_ATTR_VERIFY) == 0 || cadesVerify) {
379
0
        for (i = 0; i < scount; i++) {
380
0
            si = sk_CMS_SignerInfo_value(sinfos, i);
381
0
            if (CMS_signed_get_attr_count(si) < 0)
382
0
                continue;
383
0
            if (CMS_SignerInfo_verify(si) <= 0)
384
0
                goto err;
385
0
            if (cadesVerify) {
386
0
                STACK_OF(X509) *si_chain = si_chains ? si_chains[i] : NULL;
387
388
0
                if (ossl_cms_check_signing_certs(si, si_chain) <= 0)
389
0
                    goto err;
390
0
            }
391
0
        }
392
0
    }
393
394
    /*
395
     * Performance optimization: if the content is a memory BIO then store
396
     * its contents in a temporary read only memory BIO. This avoids
397
     * potentially large numbers of slow copies of data which will occur when
398
     * reading from a read write memory BIO when signatures are calculated.
399
     */
400
401
0
    if (dcont != NULL && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
402
0
        char *ptr;
403
0
        long len;
404
405
0
        len = BIO_get_mem_data(dcont, &ptr);
406
0
        tmpin = (len == 0) ? dcont : BIO_new_mem_buf(ptr, len);
407
0
        if (tmpin == NULL) {
408
0
            ERR_raise(ERR_LIB_CMS, ERR_R_BIO_LIB);
409
0
            goto err2;
410
0
        }
411
0
    } else {
412
0
        tmpin = dcont;
413
0
    }
414
    /*
415
     * If not binary mode and detached generate digests by *writing* through
416
     * the BIO. That makes it possible to canonicalise the input.
417
     */
418
0
    if (!(flags & SMIME_BINARY) && dcont) {
419
        /*
420
         * Create output BIO so we can either handle text or to ensure
421
         * included content doesn't override detached content.
422
         */
423
0
        tmpout = cms_get_text_bio(out, flags);
424
0
        if (tmpout == NULL) {
425
0
            ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
426
0
            goto err;
427
0
        }
428
0
        cmsbio = CMS_dataInit(cms, tmpout);
429
0
        if (cmsbio == NULL)
430
0
            goto err;
431
        /*
432
         * Don't use SMIME_TEXT for verify: it adds headers and we want to
433
         * remove them.
434
         */
435
0
        if (!SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT))
436
0
            goto err;
437
438
0
        if (flags & CMS_TEXT) {
439
0
            if (!SMIME_text(tmpout, out)) {
440
0
                ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR);
441
0
                goto err;
442
0
            }
443
0
        }
444
0
    } else {
445
0
        cmsbio = CMS_dataInit(cms, tmpin);
446
0
        if (cmsbio == NULL)
447
0
            goto err;
448
449
0
        if (!cms_copy_content(out, cmsbio, flags))
450
0
            goto err;
451
452
0
    }
453
0
    if (!(flags & CMS_NO_CONTENT_VERIFY)) {
454
0
        for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
455
0
            si = sk_CMS_SignerInfo_value(sinfos, i);
456
0
            if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
457
0
                ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_VERIFY_ERROR);
458
0
                goto err;
459
0
            }
460
0
        }
461
0
    }
462
463
0
    ret = 1;
464
0
 err:
465
0
    if (!(flags & SMIME_BINARY) && dcont) {
466
0
        do_free_upto(cmsbio, tmpout);
467
0
        if (tmpin != dcont)
468
0
            BIO_free(tmpin);
469
0
    } else {
470
0
        if (dcont && (tmpin == dcont))
471
0
            do_free_upto(cmsbio, dcont);
472
0
        else
473
0
            BIO_free_all(cmsbio);
474
0
    }
475
476
0
    if (out != tmpout)
477
0
        BIO_free_all(tmpout);
478
479
0
 err2:
480
0
    if (si_chains != NULL) {
481
0
        for (i = 0; i < scount; ++i)
482
0
            OSSL_STACK_OF_X509_free(si_chains[i]);
483
0
        OPENSSL_free(si_chains);
484
0
    }
485
0
    OSSL_STACK_OF_X509_free(cms_certs);
486
0
    sk_X509_CRL_pop_free(crls, X509_CRL_free);
487
488
0
    return ret;
489
0
}
490
491
int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
492
                       STACK_OF(X509) *certs,
493
                       X509_STORE *store, unsigned int flags)
494
0
{
495
0
    int r;
496
497
0
    flags &= ~(CMS_DETACHED | CMS_TEXT);
498
0
    r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
499
0
    if (r <= 0)
500
0
        return r;
501
0
    return ossl_cms_Receipt_verify(rcms, ocms);
502
0
}
503
504
CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
505
                             STACK_OF(X509) *certs, BIO *data,
506
                             unsigned int flags, OSSL_LIB_CTX *libctx,
507
                             const char *propq)
508
0
{
509
0
    CMS_ContentInfo *cms;
510
0
    int i;
511
512
0
    cms = CMS_ContentInfo_new_ex(libctx, propq);
513
0
    if (cms == NULL || !CMS_SignedData_init(cms)) {
514
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
515
0
        goto err;
516
0
    }
517
0
    if (flags & CMS_ASCIICRLF
518
0
        && !CMS_set1_eContentType(cms,
519
0
                                  OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF))) {
520
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
521
0
        goto err;
522
0
    }
523
524
0
    if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
525
0
        ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
526
0
        goto err;
527
0
    }
528
529
0
    for (i = 0; i < sk_X509_num(certs); i++) {
530
0
        X509 *x = sk_X509_value(certs, i);
531
532
0
        if (!CMS_add1_cert(cms, x)) {
533
0
            ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
534
0
            goto err;
535
0
        }
536
0
    }
537
538
0
    if (!(flags & CMS_DETACHED))
539
0
        CMS_set_detached(cms, 0);
540
541
0
    if ((flags & (CMS_STREAM | CMS_PARTIAL))
542
0
        || CMS_final(cms, data, NULL, flags))
543
0
        return cms;
544
0
    else
545
0
        goto err;
546
547
0
 err:
548
0
    CMS_ContentInfo_free(cms);
549
0
    return NULL;
550
0
}
551
552
CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
553
                          BIO *data, unsigned int flags)
554
0
{
555
0
    return CMS_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
556
0
}
557
558
CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
559
                                  X509 *signcert, EVP_PKEY *pkey,
560
                                  STACK_OF(X509) *certs, unsigned int flags)
561
0
{
562
0
    CMS_SignerInfo *rct_si;
563
0
    CMS_ContentInfo *cms = NULL;
564
0
    ASN1_OCTET_STRING **pos, *os = NULL;
565
0
    BIO *rct_cont = NULL;
566
0
    int r = 0;
567
0
    const CMS_CTX *ctx = si->cms_ctx;
568
569
0
    flags &= ~(CMS_STREAM | CMS_TEXT);
570
    /* Not really detached but avoids content being allocated */
571
0
    flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
572
0
    if (pkey == NULL || signcert == NULL) {
573
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY_OR_CERT);
574
0
        return NULL;
575
0
    }
576
577
    /* Initialize signed data */
578
579
0
    cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags,
580
0
                      ossl_cms_ctx_get0_libctx(ctx),
581
0
                      ossl_cms_ctx_get0_propq(ctx));
582
0
    if (cms == NULL)
583
0
        goto err;
584
585
    /* Set inner content type to signed receipt */
586
0
    if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
587
0
        goto err;
588
589
0
    rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
590
0
    if (!rct_si) {
591
0
        ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
592
0
        goto err;
593
0
    }
594
595
0
    os = ossl_cms_encode_Receipt(si);
596
0
    if (os == NULL)
597
0
        goto err;
598
599
    /* Set content to digest */
600
0
    rct_cont = BIO_new_mem_buf(os->data, os->length);
601
0
    if (rct_cont == NULL)
602
0
        goto err;
603
604
    /* Add msgSigDigest attribute */
605
606
0
    if (!ossl_cms_msgSigDigest_add1(rct_si, si))
607
0
        goto err;
608
609
    /* Finalize structure */
610
0
    if (!CMS_final(cms, rct_cont, NULL, flags))
611
0
        goto err;
612
613
    /* Set embedded content */
614
0
    pos = CMS_get0_content(cms);
615
0
    if (pos == NULL)
616
0
        goto err;
617
0
    *pos = os;
618
619
0
    r = 1;
620
621
0
 err:
622
0
    BIO_free(rct_cont);
623
0
    if (r)
624
0
        return cms;
625
0
    CMS_ContentInfo_free(cms);
626
0
    ASN1_OCTET_STRING_free(os);
627
0
    return NULL;
628
629
0
}
630
631
CMS_ContentInfo *CMS_encrypt_ex(STACK_OF(X509) *certs, BIO *data,
632
                                const EVP_CIPHER *cipher, unsigned int flags,
633
                                OSSL_LIB_CTX *libctx, const char *propq)
634
0
{
635
0
    CMS_ContentInfo *cms;
636
0
    int i;
637
0
    X509 *recip;
638
639
640
0
    cms = (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
641
0
          ? CMS_AuthEnvelopedData_create_ex(cipher, libctx, propq)
642
0
          : CMS_EnvelopedData_create_ex(cipher, libctx, propq);
643
0
    if (cms == NULL) {
644
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
645
0
        goto err;
646
0
    }
647
0
    for (i = 0; i < sk_X509_num(certs); i++) {
648
0
        recip = sk_X509_value(certs, i);
649
0
        if (!CMS_add1_recipient_cert(cms, recip, flags)) {
650
0
            ERR_raise(ERR_LIB_CMS, CMS_R_RECIPIENT_ERROR);
651
0
            goto err;
652
0
        }
653
0
    }
654
655
0
    if (!(flags & CMS_DETACHED))
656
0
        CMS_set_detached(cms, 0);
657
658
0
    if ((flags & (CMS_STREAM | CMS_PARTIAL))
659
0
        || CMS_final(cms, data, NULL, flags))
660
0
        return cms;
661
0
    else
662
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
663
664
0
 err:
665
0
    CMS_ContentInfo_free(cms);
666
0
    return NULL;
667
0
}
668
669
CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
670
                             const EVP_CIPHER *cipher, unsigned int flags)
671
0
{
672
0
    return CMS_encrypt_ex(certs, data, cipher, flags, NULL, NULL);
673
0
}
674
675
static int cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms,
676
                                       CMS_RecipientInfo *ri,
677
                                       EVP_PKEY *pk, X509 *cert, X509 *peer)
678
0
{
679
0
    int i;
680
0
    STACK_OF(CMS_RecipientEncryptedKey) *reks;
681
0
    CMS_RecipientEncryptedKey *rek;
682
683
0
    reks = CMS_RecipientInfo_kari_get0_reks(ri);
684
0
    for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
685
0
        int rv;
686
687
0
        rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
688
0
        if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
689
0
            continue;
690
0
        CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, peer);
691
0
        rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
692
0
        CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
693
0
        if (rv > 0)
694
0
            return 1;
695
0
        return cert == NULL ? 0 : -1;
696
0
    }
697
0
    return 0;
698
0
}
699
700
int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
701
0
{
702
0
     return CMS_decrypt_set1_pkey_and_peer(cms, pk, cert, NULL);
703
0
}
704
705
int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk,
706
                                   X509 *cert, X509 *peer)
707
0
{
708
0
    STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
709
0
    CMS_RecipientInfo *ri;
710
0
    int i, r, cms_pkey_ri_type;
711
0
    int debug = 0, match_ri = 0;
712
0
    CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
713
714
    /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
715
0
    if (ec != NULL) {
716
0
        OPENSSL_clear_free(ec->key, ec->keylen);
717
0
        ec->key = NULL;
718
0
        ec->keylen = 0;
719
0
    }
720
721
0
    if (ris != NULL && ec != NULL)
722
0
        debug = ec->debug;
723
724
0
    cms_pkey_ri_type = ossl_cms_pkey_get_ri_type(pk);
725
0
    if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
726
0
         ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
727
0
         return 0;
728
0
    }
729
730
0
    for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
731
0
        int ri_type;
732
733
0
        ri = sk_CMS_RecipientInfo_value(ris, i);
734
0
        ri_type = CMS_RecipientInfo_type(ri);
735
0
        if (!ossl_cms_pkey_is_ri_type_supported(pk, ri_type))
736
0
            continue;
737
0
        match_ri = 1;
738
0
        if (ri_type == CMS_RECIPINFO_AGREE) {
739
0
            r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
740
0
            if (r > 0)
741
0
                return 1;
742
0
            if (r < 0)
743
0
                return 0;
744
0
        }
745
        /* If we have a cert, try matching RecipientInfo, else try them all */
746
0
        else if (cert == NULL || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
747
0
            EVP_PKEY_up_ref(pk);
748
0
            CMS_RecipientInfo_set0_pkey(ri, pk);
749
0
            r = CMS_RecipientInfo_decrypt(cms, ri);
750
0
            CMS_RecipientInfo_set0_pkey(ri, NULL);
751
0
            if (cert != NULL) {
752
                /*
753
                 * If not debugging clear any error and return success to
754
                 * avoid leaking of information useful to MMA
755
                 */
756
0
                if (!debug) {
757
0
                    ERR_clear_error();
758
0
                    return 1;
759
0
                }
760
0
                if (r > 0)
761
0
                    return 1;
762
0
                ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
763
0
                return 0;
764
0
            }
765
            /*
766
             * If no cert and not debugging don't leave loop after first
767
             * successful decrypt. Always attempt to decrypt all recipients
768
             * to avoid leaking timing of a successful decrypt.
769
             */
770
0
            else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
771
0
                return 1;
772
0
        }
773
0
    }
774
    /* If no cert, key transport and not debugging always return success */
775
0
    if (cert == NULL
776
0
        && cms_pkey_ri_type == CMS_RECIPINFO_TRANS
777
0
        && match_ri
778
0
        && !debug) {
779
0
        ERR_clear_error();
780
0
        return 1;
781
0
    }
782
783
0
    if (!match_ri)
784
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
785
0
    return 0;
786
787
0
}
788
789
int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
790
                         unsigned char *key, size_t keylen,
791
                         const unsigned char *id, size_t idlen)
792
0
{
793
0
    STACK_OF(CMS_RecipientInfo) *ris;
794
0
    CMS_RecipientInfo *ri;
795
0
    int i, r, match_ri = 0;
796
797
0
    ris = CMS_get0_RecipientInfos(cms);
798
0
    for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
799
0
        ri = sk_CMS_RecipientInfo_value(ris, i);
800
0
        if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
801
0
            continue;
802
803
        /* If we have an id, try matching RecipientInfo, else try them all */
804
0
        if (id == NULL
805
0
                || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
806
0
            match_ri = 1;
807
0
            CMS_RecipientInfo_set0_key(ri, key, keylen);
808
0
            r = CMS_RecipientInfo_decrypt(cms, ri);
809
0
            CMS_RecipientInfo_set0_key(ri, NULL, 0);
810
0
            if (r > 0)
811
0
                return 1;
812
0
            if (id != NULL) {
813
0
                ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
814
0
                return 0;
815
0
            }
816
0
            ERR_clear_error();
817
0
        }
818
0
    }
819
820
0
    if (!match_ri)
821
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
822
0
    return 0;
823
824
0
}
825
826
int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
827
                              unsigned char *pass, ossl_ssize_t passlen)
828
0
{
829
0
    STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
830
0
    CMS_RecipientInfo *ri;
831
0
    int i, r, match_ri = 0;
832
0
    CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
833
834
    /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
835
0
    if (ec != NULL) {
836
0
        OPENSSL_clear_free(ec->key, ec->keylen);
837
0
        ec->key = NULL;
838
0
        ec->keylen = 0;
839
0
    }
840
841
0
    for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
842
0
        ri = sk_CMS_RecipientInfo_value(ris, i);
843
0
        if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
844
0
            continue;
845
846
        /* Must try each PasswordRecipientInfo */
847
0
        match_ri = 1;
848
0
        CMS_RecipientInfo_set0_password(ri, pass, passlen);
849
0
        r = CMS_RecipientInfo_decrypt(cms, ri);
850
0
        CMS_RecipientInfo_set0_password(ri, NULL, 0);
851
0
        if (r > 0)
852
0
            return 1;
853
0
    }
854
855
0
    if (!match_ri)
856
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
857
0
    return 0;
858
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
919
0
}
920
921
int CMS_final_digest(CMS_ContentInfo *cms,
922
                     const unsigned char *md, unsigned int mdlen,
923
                     BIO *dcont, unsigned int flags)
924
0
{
925
0
    BIO *cmsbio;
926
0
    int ret = 0;
927
928
0
    if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
929
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
930
0
        return 0;
931
0
    }
932
933
0
    (void)BIO_flush(cmsbio);
934
935
0
    if (!ossl_cms_DataFinal(cms, cmsbio, md, mdlen)) {
936
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
937
0
        goto err;
938
0
    }
939
0
    ret = 1;
940
941
0
err:
942
0
    do_free_upto(cmsbio, dcont);
943
0
    return ret;
944
0
}
945
946
#ifndef OPENSSL_NO_ZLIB
947
948
int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
949
                   unsigned int flags)
950
{
951
    BIO *cont;
952
    int r;
953
954
    if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
955
        ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
956
        return 0;
957
    }
958
959
    if (dcont == NULL && !check_content(cms))
960
        return 0;
961
962
    cont = CMS_dataInit(cms, dcont);
963
    if (cont == NULL)
964
        return 0;
965
    r = cms_copy_content(out, cont, flags);
966
    do_free_upto(cont, dcont);
967
    return r;
968
}
969
970
CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
971
{
972
    CMS_ContentInfo *cms;
973
974
    if (comp_nid <= 0)
975
        comp_nid = NID_zlib_compression;
976
    cms = ossl_cms_CompressedData_create(comp_nid, NULL, NULL);
977
    if (cms == NULL)
978
        return NULL;
979
980
    if (!(flags & CMS_DETACHED))
981
        CMS_set_detached(cms, 0);
982
983
    if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
984
        return cms;
985
986
    CMS_ContentInfo_free(cms);
987
    return NULL;
988
}
989
990
#else
991
992
int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
993
                   unsigned int flags)
994
0
{
995
0
    ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
996
0
    return 0;
997
0
}
998
999
CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
1000
0
{
1001
0
    ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1002
0
    return NULL;
1003
0
}
1004
1005
#endif