Coverage Report

Created: 2025-12-31 06:58

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