Coverage Report

Created: 2025-06-13 06:58

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