Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/cms/cms_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2008-2026 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 <openssl/asn1t.h>
11
#include <openssl/x509v3.h>
12
#include <openssl/err.h>
13
#include <openssl/pem.h>
14
#include <openssl/bio.h>
15
#include <openssl/asn1.h>
16
#include <openssl/cms.h>
17
#include <openssl/core_names.h>
18
#include "internal/sizes.h"
19
#include "internal/cryptlib.h"
20
#include "crypto/x509.h"
21
#include "cms_local.h"
22
#include "internal/cms.h"
23
24
static STACK_OF(CMS_CertificateChoices)
25
    **
26
    cms_get0_certificate_choices(CMS_ContentInfo *cms);
27
28
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(CMS_ContentInfo)
29
IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
30
31
CMS_ContentInfo *d2i_CMS_ContentInfo(CMS_ContentInfo **a,
32
    const unsigned char **in, long len)
33
0
{
34
0
    CMS_ContentInfo *ci;
35
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(a == NULL ? NULL : *a);
36
37
0
    ci = (CMS_ContentInfo *)ASN1_item_d2i_ex((ASN1_VALUE **)a, in, len,
38
0
        ASN1_ITEM_rptr(CMS_ContentInfo),
39
0
        ossl_cms_ctx_get0_libctx(ctx),
40
0
        ossl_cms_ctx_get0_propq(ctx));
41
0
    if (ci != NULL) {
42
0
        ERR_set_mark();
43
0
        ossl_cms_resolve_libctx(ci);
44
0
        ERR_pop_to_mark();
45
0
    }
46
0
    return ci;
47
0
}
48
49
int i2d_CMS_ContentInfo(const CMS_ContentInfo *a, unsigned char **out)
50
0
{
51
0
    return ASN1_item_i2d((const ASN1_VALUE *)a, out, ASN1_ITEM_rptr(CMS_ContentInfo));
52
0
}
53
54
CMS_ContentInfo *CMS_ContentInfo_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
55
0
{
56
0
    CMS_ContentInfo *ci;
57
58
0
    ci = (CMS_ContentInfo *)ASN1_item_new_ex(ASN1_ITEM_rptr(CMS_ContentInfo),
59
0
        libctx, propq);
60
0
    if (ci != NULL) {
61
0
        ci->ctx.libctx = libctx;
62
0
        ci->ctx.propq = NULL;
63
0
        if (propq != NULL) {
64
0
            ci->ctx.propq = OPENSSL_strdup(propq);
65
0
            if (ci->ctx.propq == NULL) {
66
0
                CMS_ContentInfo_free(ci);
67
0
                ci = NULL;
68
0
            }
69
0
        }
70
0
    }
71
0
    return ci;
72
0
}
73
74
const CMS_CTX *ossl_cms_get0_cmsctx(const CMS_ContentInfo *cms)
75
52.7k
{
76
52.7k
    return cms != NULL ? &cms->ctx : NULL;
77
52.7k
}
78
79
OSSL_LIB_CTX *ossl_cms_ctx_get0_libctx(const CMS_CTX *ctx)
80
43.7k
{
81
43.7k
    return ctx != NULL ? ctx->libctx : NULL;
82
43.7k
}
83
84
const char *ossl_cms_ctx_get0_propq(const CMS_CTX *ctx)
85
43.7k
{
86
43.7k
    return ctx != NULL ? ctx->propq : NULL;
87
43.7k
}
88
89
void ossl_cms_resolve_libctx(CMS_ContentInfo *ci)
90
5.50k
{
91
5.50k
    int i;
92
5.50k
    CMS_CertificateChoices *cch;
93
5.50k
    STACK_OF(CMS_CertificateChoices) **pcerts;
94
5.50k
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(ci);
95
5.50k
    OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
96
5.50k
    const char *propq = ossl_cms_ctx_get0_propq(ctx);
97
98
5.50k
    ossl_cms_SignerInfos_set_cmsctx(ci);
99
5.50k
    ossl_cms_RecipientInfos_set_cmsctx(ci);
100
101
5.50k
    pcerts = cms_get0_certificate_choices(ci);
102
5.50k
    if (pcerts != NULL) {
103
161k
        for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
104
160k
            cch = sk_CMS_CertificateChoices_value(*pcerts, i);
105
160k
            if (cch->type == CMS_CERTCHOICE_CERT)
106
722
                ossl_x509_set0_libctx(cch->d.certificate, libctx, propq);
107
160k
        }
108
1.24k
    }
109
5.50k
}
110
111
const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms)
112
0
{
113
0
    return cms->contentType;
114
0
}
115
116
CMS_ContentInfo *ossl_cms_Data_create(OSSL_LIB_CTX *libctx, const char *propq)
117
0
{
118
0
    CMS_ContentInfo *cms = CMS_ContentInfo_new_ex(libctx, propq);
119
120
0
    if (cms != NULL) {
121
0
        cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
122
        /* Never detached */
123
0
        CMS_set_detached(cms, 0);
124
0
    }
125
0
    return cms;
126
0
}
127
128
BIO *ossl_cms_content_bio(CMS_ContentInfo *cms)
129
0
{
130
0
    ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
131
132
0
    if (pos == NULL)
133
0
        return NULL;
134
    /* If content detached data goes nowhere: create NULL BIO */
135
0
    if (*pos == NULL)
136
0
        return BIO_new(BIO_s_null());
137
    /*
138
     * If content not detached and created return memory BIO
139
     */
140
0
    if (*pos == NULL || cms->contentIncomplete)
141
0
        return BIO_new(BIO_s_mem());
142
    /* Else content was read in: return read only BIO for it */
143
0
    return BIO_new_mem_buf((*pos)->data, (*pos)->length);
144
0
}
145
146
BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
147
0
{
148
0
    BIO *cmsbio, *cont;
149
0
    if (icont)
150
0
        cont = icont;
151
0
    else
152
0
        cont = ossl_cms_content_bio(cms);
153
0
    if (!cont) {
154
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
155
0
        return NULL;
156
0
    }
157
0
    switch (OBJ_obj2nid(cms->contentType)) {
158
159
0
    case NID_pkcs7_data:
160
0
        return cont;
161
162
0
    case NID_pkcs7_signed:
163
0
        cmsbio = ossl_cms_SignedData_init_bio(cms);
164
0
        break;
165
166
0
    case NID_pkcs7_digest:
167
0
        cmsbio = ossl_cms_DigestedData_init_bio(cms);
168
0
        break;
169
#ifndef OPENSSL_NO_ZLIB
170
    case NID_id_smime_ct_compressedData:
171
        cmsbio = ossl_cms_CompressedData_init_bio(cms);
172
        break;
173
#endif
174
175
0
    case NID_pkcs7_encrypted:
176
0
        cmsbio = ossl_cms_EncryptedData_init_bio(cms);
177
0
        break;
178
179
0
    case NID_pkcs7_enveloped:
180
0
        cmsbio = ossl_cms_EnvelopedData_init_bio(cms);
181
0
        break;
182
183
0
    case NID_id_smime_ct_authEnvelopedData:
184
0
        cmsbio = ossl_cms_AuthEnvelopedData_init_bio(cms);
185
0
        break;
186
187
0
    default:
188
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE);
189
0
        goto err;
190
0
    }
191
192
0
    if (cmsbio)
193
0
        return BIO_push(cmsbio, cont);
194
0
err:
195
0
    if (!icont)
196
0
        BIO_free(cont);
197
0
    return NULL;
198
0
}
199
200
/* unfortunately cannot constify SMIME_write_ASN1() due to this function */
201
int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
202
0
{
203
0
    return ossl_cms_DataFinal(cms, cmsbio, NULL, NULL, 0);
204
0
}
205
206
int CMS_dataFinal_ex(CMS_ContentInfo *cms, BIO *cmsbio, BIO *data)
207
0
{
208
0
    return ossl_cms_DataFinal(cms, cmsbio, data, NULL, 0);
209
0
}
210
211
int ossl_cms_DataFinal(CMS_ContentInfo *cms, BIO *cmsbio, BIO *data,
212
    const unsigned char *precomp_md,
213
    unsigned int precomp_mdlen)
214
0
{
215
0
    ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
216
217
0
    if (pos == NULL)
218
0
        return 0;
219
    /* If embedded content find memory BIO and set content */
220
0
    if (*pos && cms->contentIncomplete) {
221
0
        BIO *mbio;
222
0
        unsigned char *cont;
223
0
        long contlen;
224
0
        mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
225
0
        if (!mbio) {
226
0
            ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
227
0
            return 0;
228
0
        }
229
0
        contlen = BIO_get_mem_data(mbio, &cont);
230
        /* Set bio as read only so its content can't be clobbered */
231
0
        BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
232
0
        BIO_set_mem_eof_return(mbio, 0);
233
0
        ASN1_STRING_set0(*pos, cont, contlen);
234
0
        cms->contentIncomplete = 0;
235
0
    }
236
237
0
    switch (OBJ_obj2nid(cms->contentType)) {
238
239
0
    case NID_pkcs7_data:
240
0
    case NID_pkcs7_encrypted:
241
0
    case NID_id_smime_ct_compressedData:
242
        /* Nothing to do */
243
0
        return 1;
244
245
0
    case NID_pkcs7_enveloped:
246
0
        return ossl_cms_EnvelopedData_final(cms, cmsbio);
247
248
0
    case NID_id_smime_ct_authEnvelopedData:
249
0
        return ossl_cms_AuthEnvelopedData_final(cms, cmsbio);
250
251
0
    case NID_pkcs7_signed:
252
0
        return ossl_cms_SignedData_final(cms, cmsbio, data,
253
0
            precomp_md, precomp_mdlen);
254
255
0
    case NID_pkcs7_digest:
256
0
        return ossl_cms_DigestedData_do_final(cms, cmsbio, 0);
257
258
0
    default:
259
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE);
260
0
        return 0;
261
0
    }
262
0
}
263
264
/*
265
 * Return an OCTET STRING pointer to content. This allows it to be accessed
266
 * or set later.
267
 */
268
269
ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
270
0
{
271
0
    switch (OBJ_obj2nid(cms->contentType)) {
272
273
0
    case NID_pkcs7_data:
274
0
        return &cms->d.data;
275
276
0
    case NID_pkcs7_signed:
277
0
        return &cms->d.signedData->encapContentInfo->eContent;
278
279
0
    case NID_pkcs7_enveloped:
280
0
        return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
281
282
0
    case NID_pkcs7_digest:
283
0
        return &cms->d.digestedData->encapContentInfo->eContent;
284
285
0
    case NID_pkcs7_encrypted:
286
0
        return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
287
288
0
    case NID_id_smime_ct_authEnvelopedData:
289
0
        return &cms->d.authEnvelopedData->authEncryptedContentInfo
290
0
                    ->encryptedContent;
291
292
0
    case NID_id_smime_ct_authData:
293
0
        return &cms->d.authenticatedData->encapContentInfo->eContent;
294
295
0
    case NID_id_smime_ct_compressedData:
296
0
        return &cms->d.compressedData->encapContentInfo->eContent;
297
298
0
    default:
299
0
        if (cms->d.other->type == V_ASN1_OCTET_STRING)
300
0
            return &cms->d.other->value.octet_string;
301
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
302
0
        return NULL;
303
0
    }
304
0
}
305
306
/*
307
 * Return an ASN1_OBJECT pointer to content type. This allows it to be
308
 * accessed or set later.
309
 */
310
311
static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
312
0
{
313
0
    switch (OBJ_obj2nid(cms->contentType)) {
314
315
0
    case NID_pkcs7_signed:
316
0
        return &cms->d.signedData->encapContentInfo->eContentType;
317
318
0
    case NID_pkcs7_enveloped:
319
0
        return &cms->d.envelopedData->encryptedContentInfo->contentType;
320
321
0
    case NID_pkcs7_digest:
322
0
        return &cms->d.digestedData->encapContentInfo->eContentType;
323
324
0
    case NID_pkcs7_encrypted:
325
0
        return &cms->d.encryptedData->encryptedContentInfo->contentType;
326
327
0
    case NID_id_smime_ct_authEnvelopedData:
328
0
        return &cms->d.authEnvelopedData->authEncryptedContentInfo
329
0
                    ->contentType;
330
0
    case NID_id_smime_ct_authData:
331
0
        return &cms->d.authenticatedData->encapContentInfo->eContentType;
332
333
0
    case NID_id_smime_ct_compressedData:
334
0
        return &cms->d.compressedData->encapContentInfo->eContentType;
335
336
0
    default:
337
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
338
0
        return NULL;
339
0
    }
340
0
}
341
342
const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
343
0
{
344
0
    ASN1_OBJECT **petype;
345
0
    petype = cms_get0_econtent_type(cms);
346
0
    if (petype)
347
0
        return *petype;
348
0
    return NULL;
349
0
}
350
351
int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
352
0
{
353
0
    ASN1_OBJECT **petype, *etype;
354
355
0
    petype = cms_get0_econtent_type(cms);
356
0
    if (petype == NULL)
357
0
        return 0;
358
0
    if (oid == NULL)
359
0
        return 1;
360
0
    etype = OBJ_dup(oid);
361
0
    if (etype == NULL)
362
0
        return 0;
363
0
    ASN1_OBJECT_free(*petype);
364
0
    *petype = etype;
365
0
    return 1;
366
0
}
367
368
int CMS_is_detached(CMS_ContentInfo *cms)
369
0
{
370
0
    ASN1_OCTET_STRING **pos;
371
372
0
    pos = CMS_get0_content(cms);
373
0
    if (pos == NULL)
374
0
        return -1;
375
0
    if (*pos != NULL)
376
0
        return 0;
377
0
    return 1;
378
0
}
379
380
int CMS_set_detached(CMS_ContentInfo *cms, int detached)
381
0
{
382
0
    ASN1_OCTET_STRING **pos;
383
384
0
    pos = CMS_get0_content(cms);
385
0
    if (pos == NULL)
386
0
        return 0;
387
0
    if (detached) {
388
0
        ASN1_OCTET_STRING_free(*pos);
389
0
        *pos = NULL;
390
0
        cms->contentIncomplete = 0;
391
0
        return 1;
392
0
    }
393
0
    if (*pos == NULL)
394
0
        *pos = ASN1_OCTET_STRING_new();
395
0
    if (*pos != NULL) {
396
0
        cms->contentIncomplete = 1;
397
0
        return 1;
398
0
    }
399
0
    ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
400
0
    return 0;
401
0
}
402
403
/* Create a digest BIO from an X509_ALGOR structure */
404
405
BIO *ossl_cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
406
    const CMS_CTX *ctx)
407
0
{
408
0
    BIO *mdbio = NULL;
409
0
    const ASN1_OBJECT *digestoid;
410
0
    EVP_MD *digest = NULL;
411
0
    char alg[OSSL_MAX_NAME_SIZE];
412
0
    size_t xof_len = 0;
413
414
0
    X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
415
0
    OBJ_obj2txt(alg, sizeof(alg), digestoid, 0);
416
417
0
    digest = EVP_MD_fetch(ossl_cms_ctx_get0_libctx(ctx), alg,
418
0
        ossl_cms_ctx_get0_propq(ctx));
419
420
0
    if (digest == NULL) {
421
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM);
422
0
        goto err;
423
0
    }
424
425
0
    mdbio = BIO_new(BIO_f_md());
426
0
    if (mdbio == NULL || BIO_set_md(mdbio, digest) <= 0) {
427
0
        ERR_raise(ERR_LIB_CMS, CMS_R_MD_BIO_INIT_ERROR);
428
0
        goto err;
429
0
    }
430
0
    if (EVP_MD_xof(digest)) {
431
0
        if (EVP_MD_is_a(digest, SN_shake128))
432
0
            xof_len = 32;
433
0
        else if (EVP_MD_is_a(digest, SN_shake256))
434
0
            xof_len = 64;
435
0
        if (xof_len > 0) {
436
0
            EVP_MD_CTX *mdctx;
437
0
            OSSL_PARAM params[2];
438
439
0
            if (BIO_get_md_ctx(mdbio, &mdctx) <= 0 || mdctx == NULL)
440
0
                goto err;
441
0
            params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN,
442
0
                &xof_len);
443
0
            params[1] = OSSL_PARAM_construct_end();
444
0
            if (!EVP_MD_CTX_set_params(mdctx, params))
445
0
                goto err;
446
0
        }
447
0
    }
448
0
    EVP_MD_free(digest);
449
0
    return mdbio;
450
0
err:
451
0
    EVP_MD_free(digest);
452
0
    BIO_free(mdbio);
453
0
    return NULL;
454
0
}
455
456
/* Locate a message digest content from a BIO chain based on SignerInfo */
457
458
int ossl_cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
459
    X509_ALGOR *mdalg)
460
0
{
461
0
    int nid;
462
0
    const ASN1_OBJECT *mdoid;
463
0
    X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
464
0
    nid = OBJ_obj2nid(mdoid);
465
    /* Look for digest type to match signature */
466
0
    for (;;) {
467
0
        EVP_MD_CTX *mtmp;
468
0
        chain = BIO_find_type(chain, BIO_TYPE_MD);
469
0
        if (chain == NULL) {
470
0
            ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_DIGEST);
471
0
            return 0;
472
0
        }
473
0
        BIO_get_md_ctx(chain, &mtmp);
474
0
        if (EVP_MD_CTX_get_type(mtmp) == nid || OBJ_sn2nid(EVP_MD_CTX_get0_name(mtmp)) == nid
475
            /*
476
             * Workaround for broken implementations that use signature
477
             * algorithm OID instead of digest.
478
             */
479
0
            || EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mtmp)) == nid)
480
0
            return EVP_MD_CTX_copy_ex(mctx, mtmp);
481
0
        chain = BIO_next(chain);
482
0
    }
483
0
}
484
485
static STACK_OF(CMS_CertificateChoices)
486
    **
487
    cms_get0_certificate_choices(CMS_ContentInfo *cms)
488
5.50k
{
489
5.50k
    switch (OBJ_obj2nid(cms->contentType)) {
490
491
1.23k
    case NID_pkcs7_signed:
492
1.23k
        return &cms->d.signedData->certificates;
493
494
2.11k
    case NID_pkcs7_enveloped:
495
2.11k
        if (cms->d.envelopedData->originatorInfo == NULL)
496
2.11k
            return NULL;
497
5
        return &cms->d.envelopedData->originatorInfo->certificates;
498
499
0
    case NID_id_smime_ct_authEnvelopedData:
500
0
        if (cms->d.authEnvelopedData->originatorInfo == NULL)
501
0
            return NULL;
502
0
        return &cms->d.authEnvelopedData->originatorInfo->certificates;
503
504
2.15k
    default:
505
2.15k
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
506
2.15k
        return NULL;
507
5.50k
    }
508
5.50k
}
509
510
CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
511
0
{
512
0
    STACK_OF(CMS_CertificateChoices) **pcerts;
513
0
    CMS_CertificateChoices *cch;
514
515
0
    pcerts = cms_get0_certificate_choices(cms);
516
0
    if (pcerts == NULL)
517
0
        return NULL;
518
0
    if (*pcerts == NULL)
519
0
        *pcerts = sk_CMS_CertificateChoices_new_null();
520
0
    if (*pcerts == NULL)
521
0
        return NULL;
522
0
    cch = M_ASN1_new_of(CMS_CertificateChoices);
523
0
    if (!cch)
524
0
        return NULL;
525
0
    if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
526
0
        M_ASN1_free_of(cch, CMS_CertificateChoices);
527
0
        return NULL;
528
0
    }
529
0
    return cch;
530
0
}
531
532
int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
533
0
{
534
0
    CMS_CertificateChoices *cch;
535
0
    STACK_OF(CMS_CertificateChoices) **pcerts;
536
0
    int i;
537
538
0
    pcerts = cms_get0_certificate_choices(cms);
539
0
    if (pcerts == NULL)
540
0
        return 0;
541
0
    for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
542
0
        cch = sk_CMS_CertificateChoices_value(*pcerts, i);
543
0
        if (cch->type == CMS_CERTCHOICE_CERT) {
544
0
            if (X509_cmp(cch->d.certificate, cert) == 0) {
545
0
                X509_free(cert);
546
0
                return 1; /* cert already present */
547
0
            }
548
0
        }
549
0
    }
550
0
    cch = CMS_add0_CertificateChoices(cms);
551
0
    if (!cch)
552
0
        return 0;
553
0
    cch->type = CMS_CERTCHOICE_CERT;
554
0
    cch->d.certificate = cert;
555
0
    return 1;
556
0
}
557
558
int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
559
0
{
560
0
    if (!X509_up_ref(cert))
561
0
        return 0;
562
0
    if (CMS_add0_cert(cms, cert))
563
0
        return 1;
564
0
    X509_free(cert);
565
0
    return 0;
566
0
}
567
568
static STACK_OF(CMS_RevocationInfoChoice)
569
    **
570
    cms_get0_revocation_choices(CMS_ContentInfo *cms)
571
0
{
572
0
    switch (OBJ_obj2nid(cms->contentType)) {
573
574
0
    case NID_pkcs7_signed:
575
0
        return &cms->d.signedData->crls;
576
577
0
    case NID_pkcs7_enveloped:
578
0
        if (cms->d.envelopedData->originatorInfo == NULL)
579
0
            return NULL;
580
0
        return &cms->d.envelopedData->originatorInfo->crls;
581
582
0
    case NID_id_smime_ct_authEnvelopedData:
583
0
        if (cms->d.authEnvelopedData->originatorInfo == NULL)
584
0
            return NULL;
585
0
        return &cms->d.authEnvelopedData->originatorInfo->crls;
586
587
0
    default:
588
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
589
0
        return NULL;
590
0
    }
591
0
}
592
593
CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
594
0
{
595
0
    STACK_OF(CMS_RevocationInfoChoice) **pcrls;
596
0
    CMS_RevocationInfoChoice *rch;
597
598
0
    pcrls = cms_get0_revocation_choices(cms);
599
0
    if (pcrls == NULL)
600
0
        return NULL;
601
0
    if (*pcrls == NULL)
602
0
        *pcrls = sk_CMS_RevocationInfoChoice_new_null();
603
0
    if (*pcrls == NULL)
604
0
        return NULL;
605
0
    rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
606
0
    if (rch == NULL)
607
0
        return NULL;
608
0
    if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
609
0
        M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
610
0
        return NULL;
611
0
    }
612
0
    return rch;
613
0
}
614
615
int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
616
0
{
617
0
    CMS_RevocationInfoChoice *rch = CMS_add0_RevocationInfoChoice(cms);
618
619
0
    if (rch == NULL)
620
0
        return 0;
621
0
    rch->type = CMS_REVCHOICE_CRL;
622
0
    rch->d.crl = crl;
623
0
    return 1;
624
0
}
625
626
int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
627
0
{
628
0
    if (!X509_CRL_up_ref(crl))
629
0
        return 0;
630
0
    if (CMS_add0_crl(cms, crl))
631
0
        return 1;
632
0
    X509_CRL_free(crl);
633
0
    return 0;
634
0
}
635
636
STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
637
0
{
638
0
    STACK_OF(X509) *certs = NULL;
639
640
0
    if (!ossl_cms_get1_certs_ex(cms, &certs))
641
0
        return NULL;
642
0
    if (sk_X509_num(certs) == 0) {
643
0
        sk_X509_free(certs);
644
0
        return NULL;
645
0
    }
646
0
    return certs;
647
0
}
648
649
int ossl_cms_get1_certs_ex(CMS_ContentInfo *cms, STACK_OF(X509) **certs)
650
0
{
651
0
    CMS_CertificateChoices *cch;
652
0
    STACK_OF(CMS_CertificateChoices) **pcerts;
653
0
    int i, n;
654
655
0
    if (certs == NULL)
656
0
        return 0;
657
0
    *certs = NULL;
658
0
    pcerts = cms_get0_certificate_choices(cms);
659
0
    if (pcerts == NULL)
660
0
        return 0;
661
662
    /* make sure to return NULL *certs only on error */
663
0
    n = sk_CMS_CertificateChoices_num(*pcerts);
664
0
    if ((*certs = sk_X509_new_reserve(NULL, n)) == NULL)
665
0
        return 0;
666
667
0
    for (i = 0; i < n; i++) {
668
0
        cch = sk_CMS_CertificateChoices_value(*pcerts, i);
669
0
        if (cch->type == 0) {
670
0
            if (!X509_add_cert(*certs, cch->d.certificate,
671
0
                    X509_ADD_FLAG_UP_REF)) {
672
0
                OSSL_STACK_OF_X509_free(*certs);
673
0
                *certs = NULL;
674
0
                return 0;
675
0
            }
676
0
        }
677
0
    }
678
0
    return 1;
679
0
}
680
681
STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
682
0
{
683
0
    STACK_OF(X509_CRL) *crls = NULL;
684
685
0
    if (!ossl_cms_get1_crls_ex(cms, &crls))
686
0
        return NULL;
687
0
    if (sk_X509_CRL_num(crls) == 0) {
688
0
        sk_X509_CRL_free(crls);
689
0
        return NULL;
690
0
    }
691
0
    return crls;
692
0
}
693
694
int ossl_cms_get1_crls_ex(CMS_ContentInfo *cms, STACK_OF(X509_CRL) **crls)
695
0
{
696
0
    STACK_OF(CMS_RevocationInfoChoice) **pcrls;
697
0
    CMS_RevocationInfoChoice *rch;
698
0
    int i, n;
699
700
0
    if (crls == NULL)
701
0
        return 0;
702
0
    *crls = NULL;
703
0
    pcrls = cms_get0_revocation_choices(cms);
704
0
    if (pcrls == NULL)
705
0
        return 0;
706
707
    /* make sure to return NULL *crls only on error */
708
0
    n = sk_CMS_RevocationInfoChoice_num(*pcrls);
709
0
    if ((*crls = sk_X509_CRL_new_reserve(NULL, n)) == NULL)
710
0
        return 0;
711
712
0
    for (i = 0; i < n; i++) {
713
0
        rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
714
0
        if (rch->type == 0) {
715
0
            if (!X509_CRL_up_ref(rch->d.crl)
716
0
                || !ossl_assert(sk_X509_CRL_push(*crls, rch->d.crl))) {
717
                /* push cannot fail on reserved stack */
718
0
                sk_X509_CRL_pop_free(*crls, X509_CRL_free);
719
0
                *crls = NULL;
720
0
                return 0;
721
0
            }
722
0
        }
723
0
    }
724
0
    return 1;
725
0
}
726
727
int ossl_cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
728
0
{
729
0
    int ret;
730
0
    ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
731
0
    if (ret)
732
0
        return ret;
733
0
    return ASN1_INTEGER_cmp(ias->serialNumber, X509_get0_serialNumber(cert));
734
0
}
735
736
int ossl_cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
737
0
{
738
0
    const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
739
740
0
    if (cert_keyid == NULL)
741
0
        return -1;
742
0
    return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
743
0
}
744
745
int ossl_cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
746
0
{
747
0
    CMS_IssuerAndSerialNumber *ias;
748
0
    ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
749
0
    if (!ias) {
750
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
751
0
        goto err;
752
0
    }
753
0
    if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert))) {
754
0
        ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
755
0
        goto err;
756
0
    }
757
0
    if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert))) {
758
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
759
0
        goto err;
760
0
    }
761
0
    M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
762
0
    *pias = ias;
763
0
    return 1;
764
0
err:
765
0
    M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
766
0
    return 0;
767
0
}
768
769
int ossl_cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
770
0
{
771
0
    ASN1_OCTET_STRING *keyid = NULL;
772
0
    const ASN1_OCTET_STRING *cert_keyid;
773
0
    cert_keyid = X509_get0_subject_key_id(cert);
774
0
    if (cert_keyid == NULL) {
775
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_HAS_NO_KEYID);
776
0
        return 0;
777
0
    }
778
0
    keyid = ASN1_STRING_dup(cert_keyid);
779
0
    if (!keyid) {
780
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
781
0
        return 0;
782
0
    }
783
0
    ASN1_OCTET_STRING_free(*pkeyid);
784
0
    *pkeyid = keyid;
785
0
    return 1;
786
0
}
787
788
CMS_EnvelopedData *ossl_cms_sign_encrypt(BIO *data, X509 *sign_cert, STACK_OF(X509) *certs,
789
    EVP_PKEY *sign_key, unsigned int sign_flags,
790
    STACK_OF(X509) *enc_recip, const EVP_CIPHER *cipher,
791
    unsigned int enc_flags, OSSL_LIB_CTX *libctx,
792
    const char *propq)
793
0
{
794
0
    CMS_EnvelopedData *evd = NULL;
795
0
    BIO *privbio = NULL, *signbio = NULL;
796
0
    CMS_ContentInfo *signcms = NULL, *evpcms = NULL;
797
798
0
    if (data == NULL || sign_key == NULL || sign_cert == NULL || enc_recip == NULL) {
799
0
        ERR_raise(ERR_LIB_CMS, ERR_R_PASSED_NULL_PARAMETER);
800
0
        return NULL;
801
0
    }
802
0
    signcms = CMS_sign_ex(sign_cert, sign_key, certs, data, sign_flags, libctx, propq);
803
0
    if (signcms == NULL)
804
0
        goto err;
805
806
0
    signbio = BIO_new(BIO_s_mem());
807
0
    if (signbio == NULL
808
0
        || ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_SignedData), signbio, signcms->d.signedData) <= 0)
809
0
        goto err;
810
811
0
    evpcms = CMS_encrypt_ex(enc_recip, signbio, cipher, enc_flags, libctx, propq);
812
0
    if (evpcms == NULL)
813
0
        goto err;
814
0
    evd = CMS_EnvelopedData_dup(evpcms->d.envelopedData);
815
816
0
err:
817
0
    BIO_free(privbio);
818
0
    BIO_free(signbio);
819
0
    CMS_ContentInfo_free(signcms);
820
0
    CMS_ContentInfo_free(evpcms);
821
822
0
    return evd;
823
0
}