Leancrypto 1.6.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_pkcs7_common.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024 - 2025, Stephan Mueller <smueller@chronox.de>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20#ifndef LC_PKCS7_COMMON_H
21#define LC_PKCS7_COMMON_H
22
23#include "lc_x509_common.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
30struct lc_pkcs7_trust_store {
31 struct lc_x509_certificate *anchor_cert;
32};
33
34struct lc_pkcs7_signed_info {
35 struct lc_pkcs7_signed_info *next;
36
37 /* Message signature.
38 *
39 * This contains the generated digest of _either_ the Content Data or
40 * the Authenticated Attributes [RFC2315 9.3]. If the latter, one of
41 * the attributes contains the digest of the Content Data within it.
42 *
43 * This also contains the issuing cert serial number and issuer's name
44 * [PKCS#7 or CMS ver 1] or issuing cert's SKID [CMS ver 3].
45 */
46 struct lc_public_key_signature sig;
47
48 /*
49 * Certificate / private key signing the message in pkcs7->data. The
50 * certificate is a pointer to one member of the pkcs7->certs list.
51 */
52 struct lc_x509_certificate *signer;
53 time64_t signing_time;
54
55 /* Message digest - the digest of the Content Data (or NULL) */
56 const uint8_t *msgdigest;
57 size_t msgdigest_len;
58
59 /* Authenticated Attribute data (or NULL) */
60 const uint8_t *authattrs;
61 size_t authattrs_len;
62
63 unsigned long aa_set;
64#define sinfo_has_content_type (1 << 0)
65#define sinfo_has_signing_time (1 << 1)
66#define sinfo_has_message_digest (1 << 2)
67#define sinfo_has_smime_caps (1 << 3)
68#define sinfo_has_ms_opus_info (1 << 4)
69#define sinfo_has_ms_statement_type (1 << 5)
70
71 unsigned int index;
72
73 unsigned int
74 unsupported_crypto : 1; /* T if not usable due to missing crypto */
75 unsigned int blacklisted : 1;
76};
77
78struct lc_pkcs7_message {
79 /*
80 * List of all certificates encapsulated by the PKCS#7 message. This
81 * includes both, the auxiliary certificates as well as the signer
82 * certificates for which also the private key is present.
83 */
84 struct lc_x509_certificate *certs;
85 struct lc_x509_certificate *crl; /* Revocation list */
86
87 /*
88 * Signed information
89 */
90 struct lc_pkcs7_signed_info *curr_sinfo;
91 struct lc_pkcs7_signed_info *list_head_sinfo;
92 struct lc_pkcs7_signed_info **list_tail_sinfo;
93 uint8_t avail_preallocated_sinfo;
94 uint8_t consumed_preallocated_sinfo;
95 struct lc_pkcs7_signed_info *preallocated_sinfo;
96
97 uint8_t version; /* Version of cert (1 -> PKCS#7 or CMS; 3 -> CMS) */
98
99 /* Content Data (or NULL) */
100 enum OID data_type; /* Type of Data */
101 size_t data_len; /* Length of Data */
102 const uint8_t *data; /* Content Data (or 0) */
103
104 uint8_t avail_preallocated_x509;
105 uint8_t consumed_preallocated_x509;
106 struct lc_x509_certificate *preallocated_x509;
107
108 unsigned int have_authattrs : 1; /* T if have authattrs */
109 unsigned int embed_data : 1; /* Embed data into message */
110};
111
113
123#define LC_PKCS7_MSG_SIZE(num_sinfo, num_x509) \
124 sizeof(struct lc_pkcs7_message) + \
125 num_sinfo * sizeof(struct lc_pkcs7_signed_info) + \
126 num_x509 * sizeof(struct lc_x509_certificate)
127
151#define LC_PKCS7_MSG_ON_STACK(name, num_sinfo, num_x509) \
152 _Pragma("GCC diagnostic push") _Pragma( \
153 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
154 _Pragma("GCC diagnostic ignored \"-Wcast-align\"") \
155 LC_ALIGNED_BUFFER( \
156 name##_ctx_buf, \
157 LC_PKCS7_MSG_SIZE(num_sinfo, num_x509), 8); \
158 struct lc_pkcs7_message *name = \
159 (struct lc_pkcs7_message *)name##_ctx_buf; \
160 (name)->avail_preallocated_sinfo = num_sinfo; \
161 (name)->preallocated_sinfo = \
162 (struct lc_pkcs7_signed_info \
163 *)((uint8_t *)(name) + \
164 sizeof(struct lc_pkcs7_message)); \
165 (name)->avail_preallocated_x509 = num_x509; \
166 (name)->preallocated_x509 = \
167 (struct lc_x509_certificate \
168 *)((uint8_t *)(name) + \
169 sizeof(struct lc_pkcs7_message) + \
170 num_sinfo * sizeof(struct lc_pkcs7_signed_info)); \
171 _Pragma("GCC diagnostic pop")
172
173#ifdef __cplusplus
174}
175#endif
176
177#endif /* LC_PKCS7_COMMON_H */
OID
Definition lc_asn1.h:44