pkcs7_common.h
Go to the documentation of this file.
1 /**
2  * @file pkcs7_common.h
3  * @brief PKCS #7 common definitions
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.2
29  **/
30 
31 #ifndef _PKCS7_COMMON_H
32 #define _PKCS7_COMMON_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "pkix/x509_common.h"
37 
38 //DES encryption support (insecure)
39 #ifndef PKCS7_DES_SUPPORT
40  #define PKCS7_DES_SUPPORT DISABLED
41 #elif (PKCS7_DES_SUPPORT != ENABLED && PKCS7_DES_SUPPORT != DISABLED)
42  #error PKCS7_DES_SUPPORT parameter is not valid
43 #endif
44 
45 //Triple DES encryption support (weak)
46 #ifndef PKCS7_3DES_SUPPORT
47  #define PKCS7_3DES_SUPPORT DISABLED
48 #elif (PKCS7_3DES_SUPPORT != ENABLED && PKCS7_3DES_SUPPORT != DISABLED)
49  #error PKCS7_3DES_SUPPORT parameter is not valid
50 #endif
51 
52 //AES encryption support
53 #ifndef PKCS7_AES_SUPPORT
54  #define PKCS7_AES_SUPPORT ENABLED
55 #elif (PKCS7_AES_SUPPORT != ENABLED && PKCS7_AES_SUPPORT != DISABLED)
56  #error PKCS7_AES_SUPPORT parameter is not valid
57 #endif
58 
59 //MD5 hash support (insecure)
60 #ifndef PKCS7_MD5_SUPPORT
61  #define PKCS7_MD5_SUPPORT DISABLED
62 #elif (PKCS7_MD5_SUPPORT != ENABLED && PKCS7_MD5_SUPPORT != DISABLED)
63  #error PKCS7_MD5_SUPPORT parameter is not valid
64 #endif
65 
66 //SHA-1 hash support (weak)
67 #ifndef PKCS7_SHA1_SUPPORT
68  #define PKCS7_SHA1_SUPPORT DISABLED
69 #elif (PKCS7_SHA1_SUPPORT != ENABLED && PKCS7_SHA1_SUPPORT != DISABLED)
70  #error PKCS7_SHA1_SUPPORT parameter is not valid
71 #endif
72 
73 //SHA-224 hash support (weak)
74 #ifndef PKCS7_SHA224_SUPPORT
75  #define PKCS7_SHA224_SUPPORT DISABLED
76 #elif (PKCS7_SHA224_SUPPORT != ENABLED && PKCS7_SHA224_SUPPORT != DISABLED)
77  #error PKCS7_SHA224_SUPPORT parameter is not valid
78 #endif
79 
80 //SHA-256 hash support
81 #ifndef PKCS7_SHA256_SUPPORT
82  #define PKCS7_SHA256_SUPPORT ENABLED
83 #elif (PKCS7_SHA256_SUPPORT != ENABLED && PKCS7_SHA256_SUPPORT != DISABLED)
84  #error PKCS7_SHA256_SUPPORT parameter is not valid
85 #endif
86 
87 //SHA-384 hash support
88 #ifndef PKCS7_SHA384_SUPPORT
89  #define PKCS7_SHA384_SUPPORT ENABLED
90 #elif (PKCS7_SHA384_SUPPORT != ENABLED && PKCS7_SHA384_SUPPORT != DISABLED)
91  #error PKCS7_SHA384_SUPPORT parameter is not valid
92 #endif
93 
94 //SHA-512 hash support
95 #ifndef PKCS7_SHA512_SUPPORT
96  #define PKCS7_SHA512_SUPPORT ENABLED
97 #elif (PKCS7_SHA512_SUPPORT != ENABLED && PKCS7_SHA512_SUPPORT != DISABLED)
98  #error PKCS7_SHA512_SUPPORT parameter is not valid
99 #endif
100 
101 //RSA signature support
102 #ifndef PKCS7_RSA_SUPPORT
103  #define PKCS7_RSA_SUPPORT ENABLED
104 #elif (PKCS7_RSA_SUPPORT != ENABLED && PKCS7_RSA_SUPPORT != DISABLED)
105  #error PKCS7_RSA_SUPPORT parameter is not valid
106 #endif
107 
108 //Maximum number of digest algorithm identifiers
109 #ifndef PKCS7_MAX_DIGEST_ALGO_IDENTIFIERS
110  #define PKCS7_MAX_DIGEST_ALGO_IDENTIFIERS 2
111 #elif (PKCS7_MAX_DIGEST_ALGO_IDENTIFIERS < 1)
112  #error PKCS7_MAX_DIGEST_ALGO_IDENTIFIERS parameter is not valid
113 #endif
114 
115 //Maximum number of certificates
116 #ifndef PKCS7_MAX_CERTIFICATES
117  #define PKCS7_MAX_CERTIFICATES 4
118 #elif (PKCS7_MAX_CERTIFICATES < 1)
119  #error PKCS7_MAX_CERTIFICATES parameter is not valid
120 #endif
121 
122 //Maximum number of CRLs
123 #ifndef PKCS7_MAX_CRLS
124  #define PKCS7_MAX_CRLS 4
125 #elif (PKCS7_MAX_CRLS < 1)
126  #error PKCS7_MAX_CRLS parameter is not valid
127 #endif
128 
129 //Maximum number of signer informations
130 #ifndef PKCS7_MAX_SIGNER_INFOS
131  #define PKCS7_MAX_SIGNER_INFOS 2
132 #elif (PKCS7_MAX_SIGNER_INFOS < 1)
133  #error PKCS7_MAX_SIGNER_INFOS parameter is not valid
134 #endif
135 
136 //Maximum number of recipient informations
137 #ifndef PKCS7_MAX_RECIPIENT_INFOS
138  #define PKCS7_MAX_RECIPIENT_INFOS 2
139 #elif (PKCS7_MAX_RECIPIENT_INFOS < 1)
140  #error PKCS7_MAX_RECIPIENT_INFOS parameter is not valid
141 #endif
142 
143 //Maximum number of custom attributes
144 #ifndef PKCS7_MAX_CUSTOM_ATTRIBUTES
145  #define PKCS7_MAX_CUSTOM_ATTRIBUTES 4
146 #elif (PKCS7_MAX_CUSTOM_ATTRIBUTES < 1)
147  #error PKCS7_MAX_CUSTOM_ATTRIBUTES parameter is not valid
148 #endif
149 
150 //Maximum key size for encryption algorithms
151 #define PKCS7_MAX_ENCR_KEY_SIZE 32
152 
153 //C++ guard
154 #ifdef __cplusplus
155 extern "C" {
156 #endif
157 
158 
159 /**
160  * @brief Syntax version number
161  **/
162 
163 typedef enum
164 {
165  PKCS7_VERSION_0 = 0, ///<v0
166  PKCS7_VERSION_1 = 1, ///<v1
167  PKCS7_VERSION_2 = 2, ///<v2
168  PKCS7_VERSION_3 = 3, ///<v3
169  PKCS7_VERSION_4 = 4, ///<v4
170  PKCS7_VERSION_5 = 5 ///<v5
172 
173 
174 /**
175  * @brief Collection of digest algorithm identifiers
176  **/
177 
178 typedef struct
179 {
183 
184 
185 /**
186  * @brief Collection of certificates
187  **/
188 
189 typedef struct
190 {
195 
196 
197 /**
198  * @brief Collection of CRLs
199  **/
200 
201 typedef struct
202 {
206 } Pkcs7Crls;
207 
208 
209 /**
210  * @brief Issuer and serial number
211  **/
212 
213 typedef struct
214 {
218 
219 
220 /**
221  * @brief Attribute
222  **/
223 
224 typedef struct
225 {
230 
231 
232 /**
233  * @brief Authenticated attributes
234  **/
235 
236 typedef struct
237 {
245 
246 
247 /**
248  * @brief Unauthenticated attributes
249  **/
250 
251 typedef struct
252 {
257 
258 
259 /**
260  * @brief Signer information
261  **/
262 
263 typedef struct
264 {
265  int32_t version;
273 
274 
275 /**
276  * @brief Collection of signer informations
277  **/
278 
279 typedef struct
280 {
285 
286 
287 /**
288  * @brief Recipient information
289  **/
290 
291 typedef struct
292 {
293  int32_t version;
298 
299 
300 /**
301  * @brief Collection of recipient informations
302  **/
303 
304 typedef struct
305 {
310 
311 
312 /**
313  * @brief Content information
314  **/
315 
316 typedef struct
317 {
321 
322 
323 /**
324  * @brief Content encryption algorithm
325  **/
326 
327 typedef struct
328 {
332 
333 
334 /**
335  * @brief Encrypted content information
336  **/
337 
338 typedef struct
339 {
344 
345 
346 /**
347  * @brief Signed data content
348  **/
349 
350 typedef struct
351 {
352  int32_t version;
359 
360 
361 /**
362  * @brief Enveloped data content
363  **/
364 
365 typedef struct
366 {
367  int32_t version;
371 
372 
373 //PKCS #7 related constants
374 extern const uint8_t PKCS7_OID[8];
375 extern const uint8_t PKCS7_DATA_OID[9];
376 extern const uint8_t PKCS7_SIGNED_DATA_OID[9];
377 extern const uint8_t PKCS7_ENVELOPED_DATA_OID[9];
378 extern const uint8_t PKCS7_SIGNED_AND_ENVELOPED_DATA_OID[9];
379 extern const uint8_t PKCS7_DIGESTED_DATA_OID[9];
380 extern const uint8_t PKCS7_ENCRYPTED_DATA_OID[9];
381 
382 extern const uint8_t PKCS9_CONTENT_TYPE_OID[9];
383 extern const uint8_t PKCS9_MESSAGE_DIGEST_OID[9];
384 extern const uint8_t PKCS9_SIGNING_TIME_OID[9];
385 
386 //PKCS #7 related functions
387 const HashAlgo *pkcs7GetHashAlgo(const uint8_t *oid, size_t length);
388 const HashAlgo *pkcs7GetSignHashAlgo(const uint8_t *oid, size_t length);
389 
390 const CipherAlgo *pkcs7GetCipherAlgo(const uint8_t *oid, size_t length);
391 uint_t pkcs7GetKeyLength(const uint8_t *oid, size_t length);
392 
393 int_t pkcs7CompAttributes(const uint8_t *attribute1, size_t attributeLen1,
394  const uint8_t *attribute2, size_t attributeLen2);
395 
397  const uint8_t *data, size_t length, uint8_t *digest);
398 
399 //C++ guard
400 #ifdef __cplusplus
401 }
402 #endif
403 
404 #endif
int_t pkcs7CompAttributes(const uint8_t *attribute1, size_t attributeLen1, const uint8_t *attribute2, size_t attributeLen2)
Compare attributes.
Definition: pkcs7_common.c:329
X.509 common definitions.
const CipherAlgo * pkcs7GetCipherAlgo(const uint8_t *oid, size_t length)
Get the cipher algorithm that matches the specified OID.
Definition: pkcs7_common.c:215
X509OctetString oid
Definition: pkcs7_common.h:226
const uint8_t PKCS7_OID[8]
Definition: pkcs7_common.c:48
X509SerialNumber serialNumber
Definition: pkcs7_common.h:216
signed int int_t
Definition: compiler_port.h:56
Pkcs7Certificates certificates
Definition: pkcs7_common.h:355
X509OctetString encryptedContent
Definition: pkcs7_common.h:342
Pkcs7AuthenticatedAttributes authenticatedAttributes
Definition: pkcs7_common.h:268
uint8_t data[]
Definition: ethernet.h:224
X509OctetString messageDigest
Definition: pkcs7_common.h:240
X509OctetString contentType
Definition: pkcs7_common.h:340
X509OctetString raw
Definition: pkcs7_common.h:203
const uint8_t PKCS7_SIGNED_DATA_OID[9]
Definition: pkcs7_common.c:52
Pkcs7ContentInfo contentInfo
Definition: pkcs7_common.h:354
const uint8_t PKCS7_SIGNED_AND_ENVELOPED_DATA_OID[9]
Definition: pkcs7_common.c:56
X509OctetString iv
Definition: pkcs7_common.h:330
Content information.
Definition: pkcs7_common.h:317
const uint8_t PKCS7_DATA_OID[9]
Definition: pkcs7_common.c:50
Pkcs7Crls crls
Definition: pkcs7_common.h:356
Pkcs7UnauthenticatedAttributes unauthenticatedAttributes
Definition: pkcs7_common.h:271
uint_t pkcs7GetKeyLength(const uint8_t *oid, size_t length)
Get the encryption key length to be used for PBES2 operation.
Definition: pkcs7_common.c:270
X509OctetString data
Definition: pkcs7_common.h:228
uint8_t oid[]
Definition: lldp_tlv.h:300
Content encryption algorithm.
Definition: pkcs7_common.h:328
const uint8_t PKCS7_ENVELOPED_DATA_OID[9]
Definition: pkcs7_common.c:54
const HashAlgo * pkcs7GetHashAlgo(const uint8_t *oid, size_t length)
Get the hash algorithm that matches the specified OID.
Definition: pkcs7_common.c:77
const uint8_t PKCS9_SIGNING_TIME_OID[9]
Definition: pkcs7_common.c:67
Pkcs7RecipientInfos recipientInfos
Definition: pkcs7_common.h:368
#define PKCS7_MAX_CERTIFICATES
Definition: pkcs7_common.h:117
error_t
Error codes.
Definition: error.h:43
#define PKCS7_MAX_RECIPIENT_INFOS
Definition: pkcs7_common.h:138
Encrypted content information.
Definition: pkcs7_common.h:339
@ PKCS7_VERSION_3
v3
Definition: pkcs7_common.h:168
const HashAlgo * pkcs7GetSignHashAlgo(const uint8_t *oid, size_t length)
Get the signature hash algorithm that matches the specified OID.
Definition: pkcs7_common.c:146
#define PKCS7_MAX_CRLS
Definition: pkcs7_common.h:124
Signer information.
Definition: pkcs7_common.h:264
@ PKCS7_VERSION_0
v0
Definition: pkcs7_common.h:165
X509OctetString contentType
Definition: pkcs7_common.h:318
X509AlgoId keyEncryptionAlgo
Definition: pkcs7_common.h:295
#define PKCS7_MAX_CUSTOM_ATTRIBUTES
Definition: pkcs7_common.h:145
X509OctetString encryptedKey
Definition: pkcs7_common.h:296
X509OctetString encryptedDigest
Definition: pkcs7_common.h:270
#define PKCS7_MAX_SIGNER_INFOS
Definition: pkcs7_common.h:131
General definitions for cryptographic algorithms.
uint_t numCrls
Definition: pkcs7_common.h:204
#define PKCS7_MAX_DIGEST_ALGO_IDENTIFIERS
Definition: pkcs7_common.h:110
Date and time representation.
Definition: date_time.h:47
@ PKCS7_VERSION_2
v2
Definition: pkcs7_common.h:167
uint8_t length
Definition: tcp.h:375
Pkcs7DigestAlgos digestAlgos
Definition: pkcs7_common.h:353
Recipient information.
Definition: pkcs7_common.h:292
Collection of digest algorithm identifiers.
Definition: pkcs7_common.h:179
Pkcs7EncryptedContentInfo encryptedContentInfo
Definition: pkcs7_common.h:369
Pkcs7IssuerAndSerialNumber issuerAndSerialNumber
Definition: pkcs7_common.h:266
X509OctetString raw
Definition: pkcs7_common.h:306
Pkcs7ContentEncrAlgo contentEncrAlgo
Definition: pkcs7_common.h:341
Signed data content.
Definition: pkcs7_common.h:351
Collection of signer informations.
Definition: pkcs7_common.h:280
Issuer and serial number.
Definition: pkcs7_common.h:214
const uint8_t PKCS7_DIGESTED_DATA_OID[9]
Definition: pkcs7_common.c:58
X509OctetString oid
Definition: pkcs7_common.h:329
X509AlgoId digestAlgo
Definition: pkcs7_common.h:267
X509SignAlgoId digestEncryptionAlgo
Definition: pkcs7_common.h:269
@ PKCS7_VERSION_5
v5
Definition: pkcs7_common.h:170
Issuer or subject name.
Definition: x509_common.h:723
X509OctetString content
Definition: pkcs7_common.h:319
Unauthenticated attributes.
Definition: pkcs7_common.h:252
const uint8_t PKCS7_ENCRYPTED_DATA_OID[9]
Definition: pkcs7_common.c:60
Pkcs7Version
Syntax version number.
Definition: pkcs7_common.h:164
Common interface for encryption algorithms.
Definition: crypto.h:1111
Pkcs7SignerInfos signerInfos
Definition: pkcs7_common.h:357
Collection of CRLs.
Definition: pkcs7_common.h:202
@ PKCS7_VERSION_1
v1
Definition: pkcs7_common.h:166
Collection of certificates.
Definition: pkcs7_common.h:190
X509OctetString raw
Definition: pkcs7_common.h:191
Common interface for hash algorithms.
Definition: crypto.h:1089
@ PKCS7_VERSION_4
v4
Definition: pkcs7_common.h:169
Authenticated attributes.
Definition: pkcs7_common.h:237
Serial number.
Definition: x509_common.h:712
Octet string.
Definition: x509_common.h:701
const uint8_t PKCS9_CONTENT_TYPE_OID[9]
Definition: pkcs7_common.c:63
unsigned int uint_t
Definition: compiler_port.h:57
const uint8_t PKCS9_MESSAGE_DIGEST_OID[9]
Definition: pkcs7_common.c:65
X509OctetString raw
Definition: pkcs7_common.h:281
Collection of recipient informations.
Definition: pkcs7_common.h:305
Pkcs7IssuerAndSerialNumber issuerAndSerialNumber
Definition: pkcs7_common.h:294
Enveloped data content.
Definition: pkcs7_common.h:366
Signature algorithm identifier.
Definition: x509_common.h:1088
Algorithm identifier.
Definition: x509_common.h:774
error_t pkcs7DigestAuthenticatedAttributes(const Pkcs7SignerInfo *signerInfo, const uint8_t *data, size_t length, uint8_t *digest)
Digest the DER encoding of the authenticatedAttributes field.
Definition: pkcs7_common.c:382
Attribute.
Definition: pkcs7_common.h:225