Coverage Report

Created: 2025-08-28 07:07

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