Coverage Report

Created: 2025-12-31 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rauc/subprojects/openssl-3.0.8/crypto/cms/cms_smime.c
Line
Count
Source
1
/*
2
 * Copyright 2008-2022 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_MALLOC_FAILURE);
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
        return NULL;
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
    CMS_ContentInfo_free(cms);
249
0
    return NULL;
250
0
}
251
252
CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
253
                                           const unsigned char *key,
254
                                           size_t keylen, unsigned int flags)
255
0
{
256
0
    return CMS_EncryptedData_encrypt_ex(in, cipher, key, keylen, flags, NULL,
257
0
                                        NULL);
258
0
}
259
260
static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
261
                                      X509_STORE *store,
262
                                      STACK_OF(X509) *certs,
263
                                      STACK_OF(X509_CRL) *crls,
264
                                      STACK_OF(X509) **chain,
265
                                      const CMS_CTX *cms_ctx)
266
0
{
267
0
    X509_STORE_CTX *ctx;
268
0
    X509 *signer;
269
0
    int i, j, r = 0;
270
271
0
    ctx = X509_STORE_CTX_new_ex(ossl_cms_ctx_get0_libctx(cms_ctx),
272
0
                                ossl_cms_ctx_get0_propq(cms_ctx));
273
0
    if (ctx == NULL) {
274
0
        ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
275
0
        goto err;
276
0
    }
277
0
    CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
278
0
    if (!X509_STORE_CTX_init(ctx, store, signer, certs)) {
279
0
        ERR_raise(ERR_LIB_CMS, CMS_R_STORE_INIT_ERROR);
280
0
        goto err;
281
0
    }
282
0
    X509_STORE_CTX_set_default(ctx, "smime_sign");
283
0
    if (crls != NULL)
284
0
        X509_STORE_CTX_set0_crls(ctx, crls);
285
286
0
    i = X509_verify_cert(ctx);
287
0
    if (i <= 0) {
288
0
        j = X509_STORE_CTX_get_error(ctx);
289
0
        ERR_raise_data(ERR_LIB_CMS, CMS_R_CERTIFICATE_VERIFY_ERROR,
290
0
                       "Verify error: %s", X509_verify_cert_error_string(j));
291
0
        goto err;
292
0
    }
293
0
    r = 1;
294
295
    /* also send back the trust chain when required */
296
0
    if (chain != NULL)
297
0
        *chain = X509_STORE_CTX_get1_chain(ctx);
298
0
 err:
299
0
    X509_STORE_CTX_free(ctx);
300
0
    return r;
301
302
0
}
303
304
int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
305
               X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
306
0
{
307
0
    CMS_SignerInfo *si;
308
0
    STACK_OF(CMS_SignerInfo) *sinfos;
309
0
    STACK_OF(X509) *cms_certs = NULL;
310
0
    STACK_OF(X509_CRL) *crls = NULL;
311
0
    STACK_OF(X509) **si_chains = NULL;
312
0
    X509 *signer;
313
0
    int i, scount = 0, ret = 0;
314
0
    BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
315
0
    int cadesVerify = (flags & CMS_CADES) != 0;
316
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
317
318
0
    if (dcont == NULL && !check_content(cms))
319
0
        return 0;
320
0
    if (dcont != NULL && !(flags & CMS_BINARY)) {
321
0
        const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
322
323
0
        if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
324
0
            flags |= CMS_ASCIICRLF;
325
0
    }
326
327
    /* Attempt to find all signer certificates */
328
329
0
    sinfos = CMS_get0_SignerInfos(cms);
330
331
0
    if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
332
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_SIGNERS);
333
0
        goto err;
334
0
    }
335
336
0
    for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
337
0
        si = sk_CMS_SignerInfo_value(sinfos, i);
338
0
        CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
339
0
        if (signer)
340
0
            scount++;
341
0
    }
342
343
0
    if (scount != sk_CMS_SignerInfo_num(sinfos))
344
0
        scount += CMS_set1_signers_certs(cms, certs, flags);
345
346
0
    if (scount != sk_CMS_SignerInfo_num(sinfos)) {
347
0
        ERR_raise(ERR_LIB_CMS, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
348
0
        goto err;
349
0
    }
350
351
    /* Attempt to verify all signers certs */
352
    /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
353
354
0
    if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
355
0
        if (cadesVerify) {
356
            /* Certificate trust chain is required to check CAdES signature */
357
0
            si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
358
0
            if (si_chains == NULL) {
359
0
                ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
360
0
                goto err;
361
0
            }
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_MALLOC_FAILURE);
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_MALLOC_FAILURE);
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
            sk_X509_pop_free(si_chains[i], X509_free);
483
0
        OPENSSL_free(si_chains);
484
0
    }
485
0
    sk_X509_pop_free(cms_certs, X509_free);
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
        goto merr;
515
0
    if (flags & CMS_ASCIICRLF
516
0
        && !CMS_set1_eContentType(cms,
517
0
                                  OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
518
0
        goto err;
519
520
0
    if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
521
0
        ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
522
0
        goto err;
523
0
    }
524
525
0
    for (i = 0; i < sk_X509_num(certs); i++) {
526
0
        X509 *x = sk_X509_value(certs, i);
527
528
0
        if (!CMS_add1_cert(cms, x))
529
0
            goto merr;
530
0
    }
531
532
0
    if (!(flags & CMS_DETACHED))
533
0
        CMS_set_detached(cms, 0);
534
535
0
    if ((flags & (CMS_STREAM | CMS_PARTIAL))
536
0
        || CMS_final(cms, data, NULL, flags))
537
0
        return cms;
538
0
    else
539
0
        goto err;
540
541
0
 merr:
542
0
    ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
543
544
0
 err:
545
0
    CMS_ContentInfo_free(cms);
546
0
    return NULL;
547
0
}
548
549
CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
550
                          BIO *data, unsigned int flags)
551
0
{
552
0
    return CMS_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
553
0
}
554
555
CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
556
                                  X509 *signcert, EVP_PKEY *pkey,
557
                                  STACK_OF(X509) *certs, unsigned int flags)
558
0
{
559
0
    CMS_SignerInfo *rct_si;
560
0
    CMS_ContentInfo *cms = NULL;
561
0
    ASN1_OCTET_STRING **pos, *os;
562
0
    BIO *rct_cont = NULL;
563
0
    int r = 0;
564
0
    const CMS_CTX *ctx = si->cms_ctx;
565
566
0
    flags &= ~(CMS_STREAM | CMS_TEXT);
567
    /* Not really detached but avoids content being allocated */
568
0
    flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
569
0
    if (pkey == NULL || signcert == NULL) {
570
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY_OR_CERT);
571
0
        return NULL;
572
0
    }
573
574
    /* Initialize signed data */
575
576
0
    cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags,
577
0
                      ossl_cms_ctx_get0_libctx(ctx),
578
0
                      ossl_cms_ctx_get0_propq(ctx));
579
0
    if (cms == NULL)
580
0
        goto err;
581
582
    /* Set inner content type to signed receipt */
583
0
    if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
584
0
        goto err;
585
586
0
    rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
587
0
    if (!rct_si) {
588
0
        ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
589
0
        goto err;
590
0
    }
591
592
0
    os = ossl_cms_encode_Receipt(si);
593
0
    if (os == NULL)
594
0
        goto err;
595
596
    /* Set content to digest */
597
0
    rct_cont = BIO_new_mem_buf(os->data, os->length);
598
0
    if (rct_cont == NULL)
599
0
        goto err;
600
601
    /* Add msgSigDigest attribute */
602
603
0
    if (!ossl_cms_msgSigDigest_add1(rct_si, si))
604
0
        goto err;
605
606
    /* Finalize structure */
607
0
    if (!CMS_final(cms, rct_cont, NULL, flags))
608
0
        goto err;
609
610
    /* Set embedded content */
611
0
    pos = CMS_get0_content(cms);
612
0
    if (pos == NULL)
613
0
        goto err;
614
0
    *pos = os;
615
616
0
    r = 1;
617
618
0
 err:
619
0
    BIO_free(rct_cont);
620
0
    if (r)
621
0
        return cms;
622
0
    CMS_ContentInfo_free(cms);
623
0
    return NULL;
624
625
0
}
626
627
CMS_ContentInfo *CMS_encrypt_ex(STACK_OF(X509) *certs, BIO *data,
628
                                const EVP_CIPHER *cipher, unsigned int flags,
629
                                OSSL_LIB_CTX *libctx, const char *propq)
630
0
{
631
0
    CMS_ContentInfo *cms;
632
0
    int i;
633
0
    X509 *recip;
634
635
636
0
    cms = (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
637
0
          ? CMS_AuthEnvelopedData_create_ex(cipher, libctx, propq)
638
0
          : CMS_EnvelopedData_create_ex(cipher, libctx, propq);
639
0
    if (cms == NULL)
640
0
        goto merr;
641
0
    for (i = 0; i < sk_X509_num(certs); i++) {
642
0
        recip = sk_X509_value(certs, i);
643
0
        if (!CMS_add1_recipient_cert(cms, recip, flags)) {
644
0
            ERR_raise(ERR_LIB_CMS, CMS_R_RECIPIENT_ERROR);
645
0
            goto err;
646
0
        }
647
0
    }
648
649
0
    if (!(flags & CMS_DETACHED))
650
0
        CMS_set_detached(cms, 0);
651
652
0
    if ((flags & (CMS_STREAM | CMS_PARTIAL))
653
0
        || CMS_final(cms, data, NULL, flags))
654
0
        return cms;
655
0
    else
656
0
        goto err;
657
658
0
 merr:
659
0
    ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
660
0
 err:
661
0
    CMS_ContentInfo_free(cms);
662
0
    return NULL;
663
0
}
664
665
CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
666
                             const EVP_CIPHER *cipher, unsigned int flags)
667
0
{
668
0
    return CMS_encrypt_ex(certs, data, cipher, flags, NULL, NULL);
669
0
}
670
671
static int cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms,
672
                                       CMS_RecipientInfo *ri,
673
                                       EVP_PKEY *pk, X509 *cert, X509 *peer)
674
0
{
675
0
    int i;
676
0
    STACK_OF(CMS_RecipientEncryptedKey) *reks;
677
0
    CMS_RecipientEncryptedKey *rek;
678
679
0
    reks = CMS_RecipientInfo_kari_get0_reks(ri);
680
0
    for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
681
0
        int rv;
682
683
0
        rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
684
0
        if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
685
0
            continue;
686
0
        CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, peer);
687
0
        rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
688
0
        CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
689
0
        if (rv > 0)
690
0
            return 1;
691
0
        return cert == NULL ? 0 : -1;
692
0
    }
693
0
    return 0;
694
0
}
695
696
int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
697
0
{
698
0
     return CMS_decrypt_set1_pkey_and_peer(cms, pk, cert, NULL);
699
0
}
700
701
int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk,
702
                                   X509 *cert, X509 *peer)
703
0
{
704
0
    STACK_OF(CMS_RecipientInfo) *ris;
705
0
    CMS_RecipientInfo *ri;
706
0
    int i, r, cms_pkey_ri_type;
707
0
    int debug = 0, match_ri = 0;
708
709
0
    ris = CMS_get0_RecipientInfos(cms);
710
0
    if (ris != NULL)
711
0
        debug = ossl_cms_get0_env_enc_content(cms)->debug;
712
713
0
    cms_pkey_ri_type = ossl_cms_pkey_get_ri_type(pk);
714
0
    if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
715
0
         ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
716
0
         return 0;
717
0
    }
718
719
0
    for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
720
0
        int ri_type;
721
722
0
        ri = sk_CMS_RecipientInfo_value(ris, i);
723
0
        ri_type = CMS_RecipientInfo_type(ri);
724
0
        if (!ossl_cms_pkey_is_ri_type_supported(pk, ri_type))
725
0
            continue;
726
0
        match_ri = 1;
727
0
        if (ri_type == CMS_RECIPINFO_AGREE) {
728
0
            r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
729
0
            if (r > 0)
730
0
                return 1;
731
0
            if (r < 0)
732
0
                return 0;
733
0
        }
734
        /*
735
         * If we have a cert try matching RecipientInfo otherwise try them
736
         * all.
737
         */
738
0
        else if (cert == NULL|| !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
739
0
            EVP_PKEY_up_ref(pk);
740
0
            CMS_RecipientInfo_set0_pkey(ri, pk);
741
0
            r = CMS_RecipientInfo_decrypt(cms, ri);
742
0
            CMS_RecipientInfo_set0_pkey(ri, NULL);
743
0
            if (cert != NULL) {
744
                /*
745
                 * If not debugging clear any error and return success to
746
                 * avoid leaking of information useful to MMA
747
                 */
748
0
                if (!debug) {
749
0
                    ERR_clear_error();
750
0
                    return 1;
751
0
                }
752
0
                if (r > 0)
753
0
                    return 1;
754
0
                ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
755
0
                return 0;
756
0
            }
757
            /*
758
             * If no cert and not debugging don't leave loop after first
759
             * successful decrypt. Always attempt to decrypt all recipients
760
             * to avoid leaking timing of a successful decrypt.
761
             */
762
0
            else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
763
0
                return 1;
764
0
        }
765
0
    }
766
    /* If no cert, key transport and not debugging always return success */
767
0
    if (cert == NULL
768
0
        && cms_pkey_ri_type == CMS_RECIPINFO_TRANS
769
0
        && match_ri
770
0
        && !debug) {
771
0
        ERR_clear_error();
772
0
        return 1;
773
0
    }
774
775
0
    ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
776
0
    return 0;
777
778
0
}
779
780
int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
781
                         unsigned char *key, size_t keylen,
782
                         const unsigned char *id, size_t idlen)
783
0
{
784
0
    STACK_OF(CMS_RecipientInfo) *ris;
785
0
    CMS_RecipientInfo *ri;
786
0
    int i, r;
787
788
0
    ris = CMS_get0_RecipientInfos(cms);
789
0
    for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
790
0
        ri = sk_CMS_RecipientInfo_value(ris, i);
791
0
        if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
792
0
            continue;
793
794
        /*
795
         * If we have an id try matching RecipientInfo otherwise try them
796
         * all.
797
         */
798
0
        if (id == NULL || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
799
0
            CMS_RecipientInfo_set0_key(ri, key, keylen);
800
0
            r = CMS_RecipientInfo_decrypt(cms, ri);
801
0
            CMS_RecipientInfo_set0_key(ri, NULL, 0);
802
0
            if (r > 0)
803
0
                return 1;
804
0
            if (id != NULL) {
805
0
                ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
806
0
                return 0;
807
0
            }
808
0
            ERR_clear_error();
809
0
        }
810
0
    }
811
812
0
    ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
813
0
    return 0;
814
815
0
}
816
817
int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
818
                              unsigned char *pass, ossl_ssize_t passlen)
819
0
{
820
0
    STACK_OF(CMS_RecipientInfo) *ris;
821
0
    CMS_RecipientInfo *ri;
822
0
    int i, r;
823
824
0
    ris = CMS_get0_RecipientInfos(cms);
825
0
    for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
826
0
        ri = sk_CMS_RecipientInfo_value(ris, i);
827
0
        if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
828
0
            continue;
829
0
        CMS_RecipientInfo_set0_password(ri, pass, passlen);
830
0
        r = CMS_RecipientInfo_decrypt(cms, ri);
831
0
        CMS_RecipientInfo_set0_password(ri, NULL, 0);
832
0
        if (r > 0)
833
0
            return 1;
834
0
    }
835
836
0
    ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
837
0
    return 0;
838
839
0
}
840
841
int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
842
                BIO *dcont, BIO *out, unsigned int flags)
843
0
{
844
0
    int r;
845
0
    BIO *cont;
846
847
0
    int nid = OBJ_obj2nid(CMS_get0_type(cms));
848
849
0
    if (nid != NID_pkcs7_enveloped
850
0
            && nid != NID_id_smime_ct_authEnvelopedData) {
851
0
        ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENVELOPED_DATA);
852
0
        return 0;
853
0
    }
854
0
    if (dcont == NULL && !check_content(cms))
855
0
        return 0;
856
0
    if (flags & CMS_DEBUG_DECRYPT)
857
0
        ossl_cms_get0_env_enc_content(cms)->debug = 1;
858
0
    else
859
0
        ossl_cms_get0_env_enc_content(cms)->debug = 0;
860
0
    if (cert == NULL)
861
0
        ossl_cms_get0_env_enc_content(cms)->havenocert = 1;
862
0
    else
863
0
        ossl_cms_get0_env_enc_content(cms)->havenocert = 0;
864
0
    if (pk == NULL && cert == NULL && dcont == NULL && out == NULL)
865
0
        return 1;
866
0
    if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert))
867
0
        return 0;
868
0
    cont = CMS_dataInit(cms, dcont);
869
0
    if (cont == NULL)
870
0
        return 0;
871
0
    r = cms_copy_content(out, cont, flags);
872
0
    do_free_upto(cont, dcont);
873
0
    return r;
874
0
}
875
876
int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
877
0
{
878
0
    BIO *cmsbio;
879
0
    int ret = 0;
880
881
0
    if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
882
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
883
0
        return 0;
884
0
    }
885
886
0
    if (!SMIME_crlf_copy(data, cmsbio, flags)) {
887
0
        goto err;
888
0
    }
889
890
0
    (void)BIO_flush(cmsbio);
891
892
0
    if (!CMS_dataFinal(cms, cmsbio)) {
893
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
894
0
        goto err;
895
0
    }
896
897
0
    ret = 1;
898
899
0
err:
900
0
    do_free_upto(cmsbio, dcont);
901
902
0
    return ret;
903
904
0
}
905
906
#ifdef ZLIB
907
908
int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
909
                   unsigned int flags)
910
{
911
    BIO *cont;
912
    int r;
913
914
    if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
915
        ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
916
        return 0;
917
    }
918
919
    if (dcont == NULL && !check_content(cms))
920
        return 0;
921
922
    cont = CMS_dataInit(cms, dcont);
923
    if (cont == NULL)
924
        return 0;
925
    r = cms_copy_content(out, cont, flags);
926
    do_free_upto(cont, dcont);
927
    return r;
928
}
929
930
CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
931
{
932
    CMS_ContentInfo *cms;
933
934
    if (comp_nid <= 0)
935
        comp_nid = NID_zlib_compression;
936
    cms = ossl_cms_CompressedData_create(comp_nid, NULL, NULL);
937
    if (cms == NULL)
938
        return NULL;
939
940
    if (!(flags & CMS_DETACHED))
941
        CMS_set_detached(cms, 0);
942
943
    if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
944
        return cms;
945
946
    CMS_ContentInfo_free(cms);
947
    return NULL;
948
}
949
950
#else
951
952
int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
953
                   unsigned int flags)
954
0
{
955
0
    ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
956
0
    return 0;
957
0
}
958
959
CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
960
0
{
961
0
    ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
962
    return NULL;
963
0
}
964
965
#endif