Leancrypto 1.7.2
Post-Quantum Cryptographic Library
lc_pkcs7_common.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024 - 2026, 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
30
31/*
32 * Maximum size of the authenticated attribute part supported by the parser.
33 * This is required because for SLH-DSA, the authenticated attribute block
34 * must be copied to change the ASN.1 tag before performing a signature
35 * generation.
36 *
37 * Also, for the PKCS#7 generator, this size defines how much room is made
38 * available.
39 */
40#define LC_PKCS7_AUTHATTRS_MAX_SIZE 500
41
42struct lc_pkcs7_trust_store {
43 struct lc_x509_certificate *anchor_cert;
44};
45
46struct lc_pkcs7_signed_info {
47 struct lc_pkcs7_signed_info *next;
48
49 /* Message signature.
50 *
51 * This contains the generated digest of _either_ the Content Data or
52 * the Authenticated Attributes [RFC2315 9.3]. If the latter, one of
53 * the attributes contains the digest of the Content Data within it.
54 *
55 * This also contains the issuing cert serial number and issuer's name
56 * [PKCS#7 or CMS ver 1] or issuing cert's SKID [CMS ver 3].
57 */
58 struct lc_public_key_signature sig;
59
60 /*
61 * Certificate / private key signing the message in pkcs7->data. The
62 * certificate is a pointer to one member of the pkcs7->certs list.
63 */
64 struct lc_x509_certificate *signer;
65 time64_t signing_time;
66
67 /* Message digest - the digest of the Content Data (or NULL) */
68 const uint8_t *msgdigest;
69 size_t msgdigest_len;
70
71 /* Authenticated Attribute data (or NULL) */
72 const uint8_t *authattrs;
73 size_t authattrs_len;
74
75 /* Flags defining the authenticated attributes */
76 unsigned long aa_set;
78#define sinfo_has_content_type (1 << 0)
80#define sinfo_has_signing_time (1 << 1)
82#define sinfo_has_message_digest (1 << 2)
84#define sinfo_has_smime_caps (1 << 3)
86#define sinfo_has_ms_opus_info (1 << 4)
88#define sinfo_has_ms_statement_type (1 << 5)
89
90 unsigned int index;
91
92 unsigned int unsupported_crypto
93 : 1; /* T if not usable due to missing crypto */
94 unsigned int blacklisted : 1;
95};
96
97struct lc_pkcs7_message {
98 /*
99 * List of all certificates encapsulated by the PKCS#7 message. This
100 * includes both, the auxiliary certificates as well as the signer
101 * certificates for which also the private key is present.
102 */
103 struct lc_x509_certificate *certs;
104 struct lc_x509_certificate *crl; /* Revocation list */
105
106 /*
107 * Signed information
108 */
109 struct lc_pkcs7_signed_info *curr_sinfo;
110 struct lc_pkcs7_signed_info *list_head_sinfo;
111 struct lc_pkcs7_signed_info **list_tail_sinfo;
112 uint8_t avail_preallocated_sinfo;
113 uint8_t consumed_preallocated_sinfo;
114 struct lc_pkcs7_signed_info *preallocated_sinfo;
115
116 uint8_t version; /* Version of cert (1 -> PKCS#7 or CMS; 3 -> CMS) */
117
118 /* Content Data (or NULL) */
119 enum OID data_type; /* Type of Data */
120 size_t data_len; /* Length of Data */
121 const uint8_t *data; /* Content Data (or 0) */
122
123 uint8_t avail_preallocated_x509;
124 uint8_t consumed_preallocated_x509;
125 struct lc_x509_certificate *preallocated_x509;
126
127 unsigned int have_authattrs : 1; /* T if have authattrs */
128 unsigned int embed_data : 1; /* Embed data into message */
129};
130
132
142#define LC_PKCS7_MSG_SIZE(num_sinfo, num_x509) \
143 sizeof(struct lc_pkcs7_message) + \
144 num_sinfo * sizeof(struct lc_pkcs7_signed_info) + \
145 num_x509 * sizeof(struct lc_x509_certificate)
146
170#define LC_PKCS7_MSG_ON_STACK(name, num_sinfo, num_x509) \
171 _Pragma("GCC diagnostic push") _Pragma( \
172 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
173 _Pragma("GCC diagnostic ignored \"-Wcast-align\"") \
174 LC_ALIGNED_BUFFER( \
175 name##_ctx_buf, \
176 LC_PKCS7_MSG_SIZE(num_sinfo, num_x509), 8); \
177 struct lc_pkcs7_message *name = \
178 (struct lc_pkcs7_message *)name##_ctx_buf; \
179 (name)->avail_preallocated_sinfo = num_sinfo; \
180 (name)->preallocated_sinfo = \
181 (struct lc_pkcs7_signed_info \
182 *)((uint8_t *)(name) + \
183 sizeof(struct lc_pkcs7_message)); \
184 (name)->avail_preallocated_x509 = num_x509; \
185 (name)->preallocated_x509 = \
186 (struct lc_x509_certificate \
187 *)((uint8_t *)(name) + \
188 sizeof(struct lc_pkcs7_message) + \
189 num_sinfo * sizeof(struct lc_pkcs7_signed_info)); \
190 _Pragma("GCC diagnostic pop")
191
192#ifdef __cplusplus
193}
194#endif
195
196#endif /* LC_PKCS7_COMMON_H */
OID
Definition: lc_asn1.h:44