Coverage Report

Created: 2025-07-23 06:49

/src/rauc/subprojects/openssl-3.0.8/crypto/cms/cms_lib.c
Line
Count
Source (jump to first uncovered line)
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 <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/cms.h>
18
#include "internal/sizes.h"
19
#include "crypto/x509.h"
20
#include "cms_local.h"
21
22
static STACK_OF(CMS_CertificateChoices)
23
**cms_get0_certificate_choices(CMS_ContentInfo *cms);
24
25
IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
26
27
CMS_ContentInfo *d2i_CMS_ContentInfo(CMS_ContentInfo **a,
28
                                     const unsigned char **in, long len)
29
0
{
30
0
    CMS_ContentInfo *ci;
31
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(a == NULL ? NULL : *a);
32
33
0
    ci = (CMS_ContentInfo *)ASN1_item_d2i_ex((ASN1_VALUE **)a, in, len,
34
0
                                          (CMS_ContentInfo_it()),
35
0
                                          ossl_cms_ctx_get0_libctx(ctx),
36
0
                                          ossl_cms_ctx_get0_propq(ctx));
37
0
    if (ci != NULL) {
38
0
        ERR_set_mark();
39
0
        ossl_cms_resolve_libctx(ci);
40
0
        ERR_pop_to_mark();
41
0
    }
42
0
    return ci;
43
0
}
44
45
int i2d_CMS_ContentInfo(const CMS_ContentInfo *a, unsigned char **out)
46
0
{
47
0
    return ASN1_item_i2d((const ASN1_VALUE *)a, out, (CMS_ContentInfo_it()));
48
0
}
49
50
CMS_ContentInfo *CMS_ContentInfo_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
51
0
{
52
0
    CMS_ContentInfo *ci;
53
54
0
    ci = (CMS_ContentInfo *)ASN1_item_new_ex(ASN1_ITEM_rptr(CMS_ContentInfo),
55
0
                                             libctx, propq);
56
0
    if (ci != NULL) {
57
0
        ci->ctx.libctx = libctx;
58
0
        ci->ctx.propq = NULL;
59
0
        if (propq != NULL) {
60
0
            ci->ctx.propq = OPENSSL_strdup(propq);
61
0
            if (ci->ctx.propq == NULL) {
62
0
                CMS_ContentInfo_free(ci);
63
0
                ci = NULL;
64
0
                ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
65
0
            }
66
0
        }
67
0
    }
68
0
    return ci;
69
0
}
70
71
CMS_ContentInfo *CMS_ContentInfo_new(void)
72
0
{
73
0
    return CMS_ContentInfo_new_ex(NULL, NULL);
74
0
}
75
76
void CMS_ContentInfo_free(CMS_ContentInfo *cms)
77
7.61k
{
78
7.61k
    if (cms != NULL) {
79
7.61k
        OPENSSL_free(cms->ctx.propq);
80
7.61k
        ASN1_item_free((ASN1_VALUE *)cms, ASN1_ITEM_rptr(CMS_ContentInfo));
81
7.61k
    }
82
7.61k
}
83
84
const CMS_CTX *ossl_cms_get0_cmsctx(const CMS_ContentInfo *cms)
85
32.7k
{
86
32.7k
    return cms != NULL ? &cms->ctx : NULL;
87
32.7k
}
88
89
OSSL_LIB_CTX *ossl_cms_ctx_get0_libctx(const CMS_CTX *ctx)
90
17.5k
{
91
17.5k
    return ctx != NULL ? ctx->libctx : NULL;
92
17.5k
}
93
94
const char *ossl_cms_ctx_get0_propq(const CMS_CTX *ctx)
95
17.5k
{
96
17.5k
    return ctx != NULL ? ctx->propq : NULL;
97
17.5k
}
98
99
void ossl_cms_resolve_libctx(CMS_ContentInfo *ci)
100
7.61k
{
101
7.61k
    int i;
102
7.61k
    CMS_CertificateChoices *cch;
103
7.61k
    STACK_OF(CMS_CertificateChoices) **pcerts;
104
7.61k
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(ci);
105
7.61k
    OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
106
7.61k
    const char *propq = ossl_cms_ctx_get0_propq(ctx);
107
108
7.61k
    ossl_cms_SignerInfos_set_cmsctx(ci);
109
7.61k
    ossl_cms_RecipientInfos_set_cmsctx(ci);
110
111
7.61k
    pcerts = cms_get0_certificate_choices(ci);
112
7.61k
    if (pcerts != NULL) {
113
941
        for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
114
468
            cch = sk_CMS_CertificateChoices_value(*pcerts, i);
115
468
            if (cch->type == CMS_CERTCHOICE_CERT)
116
450
                ossl_x509_set0_libctx(cch->d.certificate, libctx, propq);
117
468
        }
118
473
    }
119
7.61k
}
120
121
const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms)
122
2.64k
{
123
2.64k
    return cms->contentType;
124
2.64k
}
125
126
CMS_ContentInfo *ossl_cms_Data_create(OSSL_LIB_CTX *libctx, const char *propq)
127
0
{
128
0
    CMS_ContentInfo *cms = CMS_ContentInfo_new_ex(libctx, propq);
129
130
0
    if (cms != NULL) {
131
0
        cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
132
        /* Never detached */
133
0
        CMS_set_detached(cms, 0);
134
0
    }
135
0
    return cms;
136
0
}
137
138
BIO *ossl_cms_content_bio(CMS_ContentInfo *cms)
139
0
{
140
0
    ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
141
142
0
    if (pos == NULL)
143
0
        return NULL;
144
    /* If content detached data goes nowhere: create NULL BIO */
145
0
    if (*pos == NULL)
146
0
        return BIO_new(BIO_s_null());
147
    /*
148
     * If content not detached and created return memory BIO
149
     */
150
0
    if (*pos == NULL || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
151
0
        return BIO_new(BIO_s_mem());
152
    /* Else content was read in: return read only BIO for it */
153
0
    return BIO_new_mem_buf((*pos)->data, (*pos)->length);
154
0
}
155
156
BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
157
0
{
158
0
    BIO *cmsbio, *cont;
159
0
    if (icont)
160
0
        cont = icont;
161
0
    else
162
0
        cont = ossl_cms_content_bio(cms);
163
0
    if (!cont) {
164
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
165
0
        return NULL;
166
0
    }
167
0
    switch (OBJ_obj2nid(cms->contentType)) {
168
169
0
    case NID_pkcs7_data:
170
0
        return cont;
171
172
0
    case NID_pkcs7_signed:
173
0
        cmsbio = ossl_cms_SignedData_init_bio(cms);
174
0
        break;
175
176
0
    case NID_pkcs7_digest:
177
0
        cmsbio = ossl_cms_DigestedData_init_bio(cms);
178
0
        break;
179
#ifdef ZLIB
180
    case NID_id_smime_ct_compressedData:
181
        cmsbio = ossl_cms_CompressedData_init_bio(cms);
182
        break;
183
#endif
184
185
0
    case NID_pkcs7_encrypted:
186
0
        cmsbio = ossl_cms_EncryptedData_init_bio(cms);
187
0
        break;
188
189
0
    case NID_pkcs7_enveloped:
190
0
        cmsbio = ossl_cms_EnvelopedData_init_bio(cms);
191
0
        break;
192
193
0
    case NID_id_smime_ct_authEnvelopedData:
194
0
        cmsbio = ossl_cms_AuthEnvelopedData_init_bio(cms);
195
0
        break;
196
197
0
    default:
198
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE);
199
0
        goto err;
200
0
    }
201
202
0
    if (cmsbio)
203
0
        return BIO_push(cmsbio, cont);
204
0
err:
205
0
    if (!icont)
206
0
        BIO_free(cont);
207
0
    return NULL;
208
209
0
}
210
211
/* unfortunately cannot constify SMIME_write_ASN1() due to this function */
212
int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
213
0
{
214
0
    ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
215
216
0
    if (pos == NULL)
217
0
        return 0;
218
    /* If embedded content find memory BIO and set content */
219
0
    if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
220
0
        BIO *mbio;
221
0
        unsigned char *cont;
222
0
        long contlen;
223
0
        mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
224
0
        if (!mbio) {
225
0
            ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
226
0
            return 0;
227
0
        }
228
0
        contlen = BIO_get_mem_data(mbio, &cont);
229
        /* Set bio as read only so its content can't be clobbered */
230
0
        BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
231
0
        BIO_set_mem_eof_return(mbio, 0);
232
0
        ASN1_STRING_set0(*pos, cont, contlen);
233
0
        (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
234
0
    }
235
236
0
    switch (OBJ_obj2nid(cms->contentType)) {
237
238
0
    case NID_pkcs7_data:
239
0
    case NID_pkcs7_encrypted:
240
0
    case NID_id_smime_ct_compressedData:
241
        /* Nothing to do */
242
0
        return 1;
243
244
0
    case NID_pkcs7_enveloped:
245
0
        return ossl_cms_EnvelopedData_final(cms, cmsbio);
246
247
0
    case NID_id_smime_ct_authEnvelopedData:
248
0
        return ossl_cms_AuthEnvelopedData_final(cms, cmsbio);
249
250
0
    case NID_pkcs7_signed:
251
0
        return ossl_cms_SignedData_final(cms, cmsbio);
252
253
0
    case NID_pkcs7_digest:
254
0
        return ossl_cms_DigestedData_do_final(cms, cmsbio, 0);
255
256
0
    default:
257
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE);
258
0
        return 0;
259
0
    }
260
0
}
261
262
/*
263
 * Return an OCTET STRING pointer to content. This allows it to be accessed
264
 * or set later.
265
 */
266
267
ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
268
4.97k
{
269
4.97k
    switch (OBJ_obj2nid(cms->contentType)) {
270
271
4
    case NID_pkcs7_data:
272
4
        return &cms->d.data;
273
274
297
    case NID_pkcs7_signed:
275
297
        return &cms->d.signedData->encapContentInfo->eContent;
276
277
1
    case NID_pkcs7_enveloped:
278
1
        return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
279
280
0
    case NID_pkcs7_digest:
281
0
        return &cms->d.digestedData->encapContentInfo->eContent;
282
283
0
    case NID_pkcs7_encrypted:
284
0
        return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
285
286
0
    case NID_id_smime_ct_authEnvelopedData:
287
0
        return &cms->d.authEnvelopedData->authEncryptedContentInfo
288
0
                                        ->encryptedContent;
289
290
0
    case NID_id_smime_ct_authData:
291
0
        return &cms->d.authenticatedData->encapContentInfo->eContent;
292
293
0
    case NID_id_smime_ct_compressedData:
294
0
        return &cms->d.compressedData->encapContentInfo->eContent;
295
296
4.66k
    default:
297
4.66k
        if (cms->d.other->type == V_ASN1_OCTET_STRING)
298
4.41k
            return &cms->d.other->value.octet_string;
299
4.66k
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
300
257
        return NULL;
301
302
4.97k
    }
303
4.97k
}
304
305
/*
306
 * Return an ASN1_OBJECT pointer to content type. This allows it to be
307
 * accessed or set later.
308
 */
309
310
static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
311
0
{
312
0
    switch (OBJ_obj2nid(cms->contentType)) {
313
314
0
    case NID_pkcs7_signed:
315
0
        return &cms->d.signedData->encapContentInfo->eContentType;
316
317
0
    case NID_pkcs7_enveloped:
318
0
        return &cms->d.envelopedData->encryptedContentInfo->contentType;
319
320
0
    case NID_pkcs7_digest:
321
0
        return &cms->d.digestedData->encapContentInfo->eContentType;
322
323
0
    case NID_pkcs7_encrypted:
324
0
        return &cms->d.encryptedData->encryptedContentInfo->contentType;
325
326
0
    case NID_id_smime_ct_authEnvelopedData:
327
0
        return &cms->d.authEnvelopedData->authEncryptedContentInfo
328
0
                                        ->contentType;
329
0
    case NID_id_smime_ct_authData:
330
0
        return &cms->d.authenticatedData->encapContentInfo->eContentType;
331
332
0
    case NID_id_smime_ct_compressedData:
333
0
        return &cms->d.compressedData->encapContentInfo->eContentType;
334
335
0
    default:
336
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
337
0
        return NULL;
338
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
2.64k
{
370
2.64k
    ASN1_OCTET_STRING **pos;
371
372
2.64k
    pos = CMS_get0_content(cms);
373
2.64k
    if (pos == NULL)
374
257
        return -1;
375
2.38k
    if (*pos != NULL)
376
2.33k
        return 0;
377
55
    return 1;
378
2.38k
}
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
        return 1;
391
0
    }
392
0
    if (*pos == NULL)
393
0
        *pos = ASN1_OCTET_STRING_new();
394
0
    if (*pos != NULL) {
395
        /*
396
         * NB: special flag to show content is created and not read in.
397
         */
398
0
        (*pos)->flags |= ASN1_STRING_FLAG_CONT;
399
0
        return 1;
400
0
    }
401
0
    ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
402
0
    return 0;
403
0
}
404
405
/* Create a digest BIO from an X509_ALGOR structure */
406
407
BIO *ossl_cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
408
                                       const CMS_CTX *ctx)
409
0
{
410
0
    BIO *mdbio = NULL;
411
0
    const ASN1_OBJECT *digestoid;
412
0
    const EVP_MD *digest = NULL;
413
0
    EVP_MD *fetched_digest = NULL;
414
0
    char alg[OSSL_MAX_NAME_SIZE];
415
416
0
    X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
417
0
    OBJ_obj2txt(alg, sizeof(alg), digestoid, 0);
418
419
0
    (void)ERR_set_mark();
420
0
    fetched_digest = EVP_MD_fetch(ossl_cms_ctx_get0_libctx(ctx), alg,
421
0
                                  ossl_cms_ctx_get0_propq(ctx));
422
423
0
    if (fetched_digest != NULL)
424
0
        digest = fetched_digest;
425
0
    else
426
0
        digest = EVP_get_digestbyobj(digestoid);
427
0
    if (digest == NULL) {
428
0
        (void)ERR_clear_last_mark();
429
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM);
430
0
        goto err;
431
0
    }
432
0
    (void)ERR_pop_to_mark();
433
434
0
    mdbio = BIO_new(BIO_f_md());
435
0
    if (mdbio == NULL || !BIO_set_md(mdbio, digest)) {
436
0
        ERR_raise(ERR_LIB_CMS, CMS_R_MD_BIO_INIT_ERROR);
437
0
        goto err;
438
0
    }
439
0
    EVP_MD_free(fetched_digest);
440
0
    return mdbio;
441
0
 err:
442
0
    EVP_MD_free(fetched_digest);
443
0
    BIO_free(mdbio);
444
0
    return NULL;
445
0
}
446
447
/* Locate a message digest content from a BIO chain based on SignerInfo */
448
449
int ossl_cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
450
                                      X509_ALGOR *mdalg)
451
0
{
452
0
    int nid;
453
0
    const ASN1_OBJECT *mdoid;
454
0
    X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
455
0
    nid = OBJ_obj2nid(mdoid);
456
    /* Look for digest type to match signature */
457
0
    for (;;) {
458
0
        EVP_MD_CTX *mtmp;
459
0
        chain = BIO_find_type(chain, BIO_TYPE_MD);
460
0
        if (chain == NULL) {
461
0
            ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_DIGEST);
462
0
            return 0;
463
0
        }
464
0
        BIO_get_md_ctx(chain, &mtmp);
465
0
        if (EVP_MD_CTX_get_type(mtmp) == nid
466
            /*
467
             * Workaround for broken implementations that use signature
468
             * algorithm OID instead of digest.
469
             */
470
0
            || EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mtmp)) == nid)
471
0
            return EVP_MD_CTX_copy_ex(mctx, mtmp);
472
0
        chain = BIO_next(chain);
473
0
    }
474
0
}
475
476
static STACK_OF(CMS_CertificateChoices)
477
**cms_get0_certificate_choices(CMS_ContentInfo *cms)
478
7.61k
{
479
7.61k
    switch (OBJ_obj2nid(cms->contentType)) {
480
481
473
    case NID_pkcs7_signed:
482
473
        return &cms->d.signedData->certificates;
483
484
2
    case NID_pkcs7_enveloped:
485
2
        if (cms->d.envelopedData->originatorInfo == NULL)
486
2
            return NULL;
487
0
        return &cms->d.envelopedData->originatorInfo->certificates;
488
489
0
    case NID_id_smime_ct_authEnvelopedData:
490
0
        if (cms->d.authEnvelopedData->originatorInfo == NULL)
491
0
            return NULL;
492
0
        return &cms->d.authEnvelopedData->originatorInfo->certificates;
493
494
7.13k
    default:
495
7.13k
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
496
7.13k
        return NULL;
497
498
7.61k
    }
499
7.61k
}
500
501
CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
502
0
{
503
0
    STACK_OF(CMS_CertificateChoices) **pcerts;
504
0
    CMS_CertificateChoices *cch;
505
506
0
    pcerts = cms_get0_certificate_choices(cms);
507
0
    if (pcerts == NULL)
508
0
        return NULL;
509
0
    if (*pcerts == NULL)
510
0
        *pcerts = sk_CMS_CertificateChoices_new_null();
511
0
    if (*pcerts == NULL)
512
0
        return NULL;
513
0
    cch = M_ASN1_new_of(CMS_CertificateChoices);
514
0
    if (!cch)
515
0
        return NULL;
516
0
    if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
517
0
        M_ASN1_free_of(cch, CMS_CertificateChoices);
518
0
        return NULL;
519
0
    }
520
0
    return cch;
521
0
}
522
523
int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
524
0
{
525
0
    CMS_CertificateChoices *cch;
526
0
    STACK_OF(CMS_CertificateChoices) **pcerts;
527
0
    int i;
528
529
0
    pcerts = cms_get0_certificate_choices(cms);
530
0
    if (pcerts == NULL)
531
0
        return 0;
532
0
    for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
533
0
        cch = sk_CMS_CertificateChoices_value(*pcerts, i);
534
0
        if (cch->type == CMS_CERTCHOICE_CERT) {
535
0
            if (!X509_cmp(cch->d.certificate, cert)) {
536
0
                ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_ALREADY_PRESENT);
537
0
                return 0;
538
0
            }
539
0
        }
540
0
    }
541
0
    cch = CMS_add0_CertificateChoices(cms);
542
0
    if (!cch)
543
0
        return 0;
544
0
    cch->type = CMS_CERTCHOICE_CERT;
545
0
    cch->d.certificate = cert;
546
0
    return 1;
547
0
}
548
549
int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
550
0
{
551
0
    int r;
552
0
    r = CMS_add0_cert(cms, cert);
553
0
    if (r > 0)
554
0
        X509_up_ref(cert);
555
0
    return r;
556
0
}
557
558
static STACK_OF(CMS_RevocationInfoChoice)
559
**cms_get0_revocation_choices(CMS_ContentInfo *cms)
560
0
{
561
0
    switch (OBJ_obj2nid(cms->contentType)) {
562
563
0
    case NID_pkcs7_signed:
564
0
        return &cms->d.signedData->crls;
565
566
0
    case NID_pkcs7_enveloped:
567
0
        if (cms->d.envelopedData->originatorInfo == NULL)
568
0
            return NULL;
569
0
        return &cms->d.envelopedData->originatorInfo->crls;
570
571
0
    case NID_id_smime_ct_authEnvelopedData:
572
0
        if (cms->d.authEnvelopedData->originatorInfo == NULL)
573
0
            return NULL;
574
0
        return &cms->d.authEnvelopedData->originatorInfo->crls;
575
576
0
    default:
577
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
578
0
        return NULL;
579
580
0
    }
581
0
}
582
583
CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
584
0
{
585
0
    STACK_OF(CMS_RevocationInfoChoice) **pcrls;
586
0
    CMS_RevocationInfoChoice *rch;
587
588
0
    pcrls = cms_get0_revocation_choices(cms);
589
0
    if (pcrls == NULL)
590
0
        return NULL;
591
0
    if (*pcrls == NULL)
592
0
        *pcrls = sk_CMS_RevocationInfoChoice_new_null();
593
0
    if (*pcrls == NULL)
594
0
        return NULL;
595
0
    rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
596
0
    if (rch == NULL)
597
0
        return NULL;
598
0
    if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
599
0
        M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
600
0
        return NULL;
601
0
    }
602
0
    return rch;
603
0
}
604
605
int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
606
0
{
607
0
    CMS_RevocationInfoChoice *rch;
608
0
    rch = CMS_add0_RevocationInfoChoice(cms);
609
0
    if (!rch)
610
0
        return 0;
611
0
    rch->type = CMS_REVCHOICE_CRL;
612
0
    rch->d.crl = crl;
613
0
    return 1;
614
0
}
615
616
int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
617
0
{
618
0
    int r;
619
0
    r = CMS_add0_crl(cms, crl);
620
0
    if (r > 0)
621
0
        X509_CRL_up_ref(crl);
622
0
    return r;
623
0
}
624
625
STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
626
0
{
627
0
    STACK_OF(X509) *certs = NULL;
628
0
    CMS_CertificateChoices *cch;
629
0
    STACK_OF(CMS_CertificateChoices) **pcerts;
630
0
    int i;
631
632
0
    pcerts = cms_get0_certificate_choices(cms);
633
0
    if (pcerts == NULL)
634
0
        return NULL;
635
0
    for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
636
0
        cch = sk_CMS_CertificateChoices_value(*pcerts, i);
637
0
        if (cch->type == 0) {
638
0
            if (!ossl_x509_add_cert_new(&certs, cch->d.certificate,
639
0
                                        X509_ADD_FLAG_UP_REF)) {
640
0
                sk_X509_pop_free(certs, X509_free);
641
0
                return NULL;
642
0
            }
643
0
        }
644
0
    }
645
0
    return certs;
646
647
0
}
648
649
STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
650
0
{
651
0
    STACK_OF(X509_CRL) *crls = NULL;
652
0
    STACK_OF(CMS_RevocationInfoChoice) **pcrls;
653
0
    CMS_RevocationInfoChoice *rch;
654
0
    int i;
655
656
0
    pcrls = cms_get0_revocation_choices(cms);
657
0
    if (pcrls == NULL)
658
0
        return NULL;
659
0
    for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
660
0
        rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
661
0
        if (rch->type == 0) {
662
0
            if (!crls) {
663
0
                crls = sk_X509_CRL_new_null();
664
0
                if (!crls)
665
0
                    return NULL;
666
0
            }
667
0
            if (!sk_X509_CRL_push(crls, rch->d.crl)) {
668
0
                sk_X509_CRL_pop_free(crls, X509_CRL_free);
669
0
                return NULL;
670
0
            }
671
0
            X509_CRL_up_ref(rch->d.crl);
672
0
        }
673
0
    }
674
0
    return crls;
675
0
}
676
677
int ossl_cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
678
0
{
679
0
    int ret;
680
0
    ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
681
0
    if (ret)
682
0
        return ret;
683
0
    return ASN1_INTEGER_cmp(ias->serialNumber, X509_get0_serialNumber(cert));
684
0
}
685
686
int ossl_cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
687
0
{
688
0
    const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
689
690
0
    if (cert_keyid == NULL)
691
0
        return -1;
692
0
    return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
693
0
}
694
695
int ossl_cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
696
0
{
697
0
    CMS_IssuerAndSerialNumber *ias;
698
0
    ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
699
0
    if (!ias)
700
0
        goto err;
701
0
    if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
702
0
        goto err;
703
0
    if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert)))
704
0
        goto err;
705
0
    M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
706
0
    *pias = ias;
707
0
    return 1;
708
0
 err:
709
0
    M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
710
0
    ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
711
0
    return 0;
712
0
}
713
714
int ossl_cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
715
0
{
716
0
    ASN1_OCTET_STRING *keyid = NULL;
717
0
    const ASN1_OCTET_STRING *cert_keyid;
718
0
    cert_keyid = X509_get0_subject_key_id(cert);
719
0
    if (cert_keyid == NULL) {
720
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_HAS_NO_KEYID);
721
0
        return 0;
722
0
    }
723
0
    keyid = ASN1_STRING_dup(cert_keyid);
724
0
    if (!keyid) {
725
0
        ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
726
0
        return 0;
727
0
    }
728
0
    ASN1_OCTET_STRING_free(*pkeyid);
729
0
    *pkeyid = keyid;
730
0
    return 1;
731
0
}