Coverage Report

Created: 2025-06-13 06:58

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