Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/cms/cms_io.c
Line
Count
Source
1
/*
2
 * Copyright 2008-2026 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/x509.h>
12
#include <openssl/err.h>
13
#include <openssl/pem.h>
14
#include <openssl/cms.h>
15
#include "cms_local.h"
16
17
int ossl_cms_stream(CMS_ContentInfo *cms)
18
0
{
19
0
    ASN1_OCTET_STRING **pos;
20
21
0
    pos = CMS_get0_content(cms);
22
0
    if (pos == NULL)
23
0
        return 0;
24
0
    if (*pos == NULL)
25
0
        *pos = ASN1_OCTET_STRING_new();
26
0
    if (*pos != NULL) {
27
0
        cms->contentIncomplete = 0;
28
0
        return 1;
29
0
    }
30
0
    ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
31
0
    return 0;
32
0
}
33
34
#if !defined(OPENSSL_NO_DEPRECATED_4_1)
35
int CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms)
36
0
{
37
0
    if (boundary != NULL)
38
0
        *boundary = NULL;
39
0
    return ossl_cms_stream(cms);
40
0
}
41
#endif /* !defined(OPENSSL_NO_DEPRECATED_4_1) */
42
43
CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms)
44
36.2k
{
45
36.2k
    CMS_ContentInfo *ci;
46
36.2k
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms == NULL ? NULL : *cms);
47
48
36.2k
    ci = ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms,
49
36.2k
        ossl_cms_ctx_get0_libctx(ctx),
50
36.2k
        ossl_cms_ctx_get0_propq(ctx));
51
36.2k
    if (ci != NULL) {
52
5.50k
        ERR_set_mark();
53
5.50k
        ossl_cms_resolve_libctx(ci);
54
5.50k
        ERR_pop_to_mark();
55
5.50k
    }
56
36.2k
    return ci;
57
36.2k
}
58
59
int i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms)
60
5.50k
{
61
5.50k
    return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
62
5.50k
}
63
64
0
IMPLEMENT_PEM_rw(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo)
Unexecuted instantiation: PEM_read_bio_CMS
Unexecuted instantiation: PEM_read_CMS
Unexecuted instantiation: PEM_write_bio_CMS
Unexecuted instantiation: PEM_write_CMS
65
0
66
0
BIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms)
67
0
{
68
0
    return BIO_new_NDEF(out, (ASN1_VALUE *)cms,
69
0
        ASN1_ITEM_rptr(CMS_ContentInfo));
70
0
}
71
72
/* CMS wrappers round generalised stream and MIME routines */
73
74
int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags)
75
0
{
76
0
    return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags,
77
0
        ASN1_ITEM_rptr(CMS_ContentInfo));
78
0
}
79
80
int PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in,
81
    int flags)
82
0
{
83
0
    return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *)cms, in, flags,
84
0
        "CMS", ASN1_ITEM_rptr(CMS_ContentInfo));
85
0
}
86
87
int SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags)
88
0
{
89
0
    STACK_OF(X509_ALGOR) *mdalgs;
90
0
    int ctype_nid = OBJ_obj2nid(cms->contentType);
91
0
    int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms));
92
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
93
94
0
    if (ctype_nid == NID_pkcs7_signed)
95
0
        mdalgs = cms->d.signedData->digestAlgorithms;
96
0
    else
97
0
        mdalgs = NULL;
98
99
0
    return SMIME_write_ASN1_ex(bio, (ASN1_VALUE *)cms, data, flags, ctype_nid,
100
0
        econt_nid, mdalgs,
101
0
        ASN1_ITEM_rptr(CMS_ContentInfo),
102
0
        ossl_cms_ctx_get0_libctx(ctx),
103
0
        ossl_cms_ctx_get0_propq(ctx));
104
0
}
105
106
CMS_ContentInfo *SMIME_read_CMS_ex(BIO *bio, int flags, BIO **bcont,
107
    CMS_ContentInfo **cms)
108
0
{
109
0
    CMS_ContentInfo *ci;
110
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms == NULL ? NULL : *cms);
111
112
0
    ci = (CMS_ContentInfo *)SMIME_read_ASN1_ex(bio, flags, bcont,
113
0
        ASN1_ITEM_rptr(CMS_ContentInfo),
114
0
        (ASN1_VALUE **)cms,
115
0
        ossl_cms_ctx_get0_libctx(ctx),
116
0
        ossl_cms_ctx_get0_propq(ctx));
117
0
    if (ci != NULL) {
118
0
        ERR_set_mark();
119
0
        ossl_cms_resolve_libctx(ci);
120
0
        ERR_pop_to_mark();
121
0
    }
122
0
    return ci;
123
0
}
124
125
CMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont)
126
0
{
127
    return SMIME_read_CMS_ex(bio, 0, bcont, NULL);
128
0
}