Coverage Report

Created: 2025-08-28 07:07

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