Leancrypto 1.7.2
Post-Quantum Cryptographic Library
lc_x509_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_X509_COMMON_H
21#define LC_X509_COMMON_H
22
23#include "ext_headers.h"
24#include "lc_asn1.h"
25#include "lc_hash.h"
26#include "lc_sha3.h"
27
28#if defined __has_include
29
30#ifdef LC_DILITHIUM
31#if __has_include("lc_dilithium.h")
32#include "lc_dilithium.h"
33#define LC_DILITHIUM_ENABLED
34#endif
35#endif
36
37#ifdef LC_SPHINCS
38#if __has_include("lc_sphincs.h")
39#include "lc_sphincs.h"
40#define LC_SPHINCS_ENABLED
41#endif
42#endif
43
44#else
45#error "Compiler misses __has_include"
46#endif
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
53
54/*
55 * Default hash type used to generate the SKID / AKID from the public key.
56 * The algorithm is only used if the caller does not set an SKID/AKID.
57 *
58 * NOTE: X.509 support requires asymmetric algorithms. All asymmetric algorithms
59 * require the presence of SHA-3 which means the use of SHA-3 is a safe choice.
60 */
61#define LC_X509_SKID_DEFAULT_HASH lc_sha3_256
62#define LC_X509_SKID_DEFAULT_HASHSIZE LC_SHA3_256_SIZE_DIGEST
63#define LC_X509_PQC_SK_SEED_SIZE 32
64
65/*
66 * According to RFC5280 section 4.2.1.2, the serial number must not be larger
67 * than 20 bytes.
68 */
69#define LC_X509_SERIAL_MAX_SIZE 20
70
71/*
72 * Identifiers for an asymmetric key ID. We have three ways of looking up a
73 * key derived from an X.509 certificate:
74 *
75 * (1) Serial Number & Issuer. Non-optional. This is the only valid way to
76 * map a PKCS#7 signature to an X.509 certificate.
77 *
78 * (2) Issuer & Subject Unique IDs. Optional. These were the original way to
79 * match X.509 certificates, but have fallen into disuse in favour of (3).
80 *
81 * (3) Auth & Subject Key Identifiers. Optional. SKIDs are only provided on
82 * CA keys that are intended to sign other keys, so don't appear in end
83 * user certificates unless forced.
84 *
85 * We could also support an PGP key identifier, which is just a SHA1 sum of the
86 * public key and certain parameters, but since we don't support PGP keys at
87 * the moment, we shall ignore those.
88 *
89 * What we actually do is provide a place where binary identifiers can be
90 * stashed and then compare against them when checking for an id match.
91 *
92 * The following size constraints are considered:
93 *
94 * * Serial number can be up to 20 octets (RFC 5280 section 4.1.2.2)
95 *
96 * * Subject Key Identifier can be SHA-1 or other sizes (RFC 5280 section
97 * 4.2.1.2) - we allow up to 64 bytes
98 *
99 * * Issuer can be an arbitrary size of bytes but at least one (RFC 5280
100 * section 4.1.2.4) - we apply the upper limit of 128 bytes
101 *
102 * * Subject Key Identifier "MAY be based on either the key identifier (the
103 * subject key identifier in the issuer's certificate) or the issuer name
104 * and serial number." (RFC 5280 section 4.2.1.1)
105 */
106#define LC_ASN1_MAX_ISSUER_NAME 128
107#define LC_ASN1_MAX_SKID 64
108struct lc_asymmetric_key_id {
109 uint8_t len;
110 uint8_t data[LC_ASN1_MAX_SKID + LC_ASN1_MAX_ISSUER_NAME];
111};
112
113enum lc_sig_types {
115 LC_SIG_UNKNOWN,
117 LC_SIG_DILITHIUM_44,
119 LC_SIG_DILITHIUM_65,
121 LC_SIG_DILITHIUM_87,
123 LC_SIG_DILITHIUM_44_ED25519,
125 LC_SIG_DILITHIUM_65_ED25519,
127 LC_SIG_DILITHIUM_87_ED25519,
129 LC_SIG_DILITHIUM_44_ED448,
131 LC_SIG_DILITHIUM_65_ED448,
133 LC_SIG_DILITHIUM_87_ED448,
135 LC_SIG_SPINCS_SHAKE_256S,
137 LC_SIG_SPINCS_SHAKE_256F,
139 LC_SIG_SPINCS_SHAKE_192S,
141 LC_SIG_SPINCS_SHAKE_192F,
143 LC_SIG_SPINCS_SHAKE_128S,
145 LC_SIG_SPINCS_SHAKE_128F,
146
148 LC_SIG_RSA_PKCS1,
149 LC_SIG_RSA_PKCS1_SHA2_256,
150 LC_SIG_RSA_PKCS1_SHA2_384,
151 LC_SIG_RSA_PKCS1_SHA2_512,
152 LC_SIG_RSA_PKCS1_SHA3_256,
153 LC_SIG_RSA_PKCS1_SHA3_384,
154 LC_SIG_RSA_PKCS1_SHA3_512,
156 LC_SIG_ECDSA_X963,
157 LC_SIG_ECDSA_X963_SHA2_256,
158 LC_SIG_ECDSA_X963_SHA2_384,
159 LC_SIG_ECDSA_X963_SHA2_512,
160 LC_SIG_ECDSA_X963_SHA3_256,
161 LC_SIG_ECDSA_X963_SHA3_384,
162 LC_SIG_ECDSA_X963_SHA3_512,
164 LC_SIG_SM2,
166 LC_SIG_ECRDSA_PKCS1,
167};
168
169/*
170 * Cryptographic data for the public-key subtype of the asymmetric key type.
171 *
172 * Note that this may include private part of the key as well as the public
173 * part.
174 */
175struct lc_public_key {
176 const uint8_t *key;
177 size_t keylen;
178 const uint8_t *params;
179 size_t paramlen;
180 enum OID algo;
181 enum lc_sig_types pkey_algo;
182
183 uint16_t key_usage; /* key extension flags */
184#define LC_KEY_USAGE_DIGITALSIG 0x0080 /* (0) */
185#define LC_KEY_USAGE_CONTENT_COMMITMENT 0x0040 /* (1) */
186#define LC_KEY_USAGE_KEY_ENCIPHERMENT 0x0020 /* (2) */
187#define LC_KEY_USAGE_DATA_ENCIPHERMENT 0x0010 /* (3) */
188#define LC_KEY_USAGE_KEY_AGREEMENT 0x0008 /* (4) */
189#define LC_KEY_USAGE_KEYCERTSIGN 0x0004 /* (5) */
190#define LC_KEY_USAGE_CRLSIGN 0x0002 /* (6) */
191#define LC_KEY_USAGE_ENCIPHER_ONLY 0x0001 /* (7) */
192#define LC_KEY_USAGE_DECIPHER_ONLY 0x8000 /* (8) */
193#define LC_KEY_USAGE_CRITICAL 0x4000
194#define LC_KEY_USAGE_EXTENSION_PRESENT 0x2000
195#define LC_KEY_USAGE_MASK \
196 ((uint16_t)~(LC_KEY_USAGE_CRITICAL | LC_KEY_USAGE_EXTENSION_PRESENT))
197
198 uint16_t key_eku;
199#define LC_KEY_EKU_CRITICAL (1 << 1)
200#define LC_KEY_EKU_EXTENSION_PRESENT (1 << 2)
201#define LC_KEY_EKU_ANY (1 << 3)
202#define LC_KEY_EKU_SERVER_AUTH (1 << 4)
203#define LC_KEY_EKU_CLIENT_AUTH (1 << 5)
204#define LC_KEY_EKU_CODE_SIGNING (1 << 6)
205#define LC_KEY_EKU_EMAIL_PROTECTION (1 << 7)
206#define LC_KEY_EKU_TIME_STAMPING (1 << 8)
207#define LC_KEY_EKU_OCSP_SIGNING (1 << 9)
208#define LC_KEY_EKU_MODULE_SIGNING (1 << 10)
209#define LC_KEY_EKU_MASK \
210 ((uint16_t)~LC_KEY_EKU_CRITICAL | LC_KEY_EKU_EXTENSION_PRESENT)
211
212 uint8_t basic_constraint;
213#define LC_KEY_CA (1 << 2)
214#define LC_KEY_NOCA (1 << 1)
215#define LC_KEY_BASIC_CONSTRAINT_CRITICAL (1 << 0)
216#define LC_KEY_IS_CA (LC_KEY_CA | LC_KEY_BASIC_CONSTRAINT_CRITICAL)
217
218 uint8_t ca_pathlen;
219#define LC_KEY_CA_MAXLEN 16
220#define LC_KEY_CA_MASK ((LC_KEY_CA_MAXLEN << 1) - 1)
221
222 unsigned int key_is_private : 1;
223};
224
225/*
226 * Public key cryptography signature data
227 */
228struct lc_public_key_signature {
229 /*
230 * Signature
231 */
232 const uint8_t *s;
233
234 /*
235 * Number of bytes in signature
236 */
237 size_t s_size;
238
239 /*
240 * Digest size (0 if no digest was calculated and in this case the
241 * raw_data is used for signature). In case of having no digest, the
242 * signature algorithm must calculate the signature over the full
243 * ->raw_data.
244 */
245 size_t digest_size;
246 uint8_t digest[LC_SHA_MAX_SIZE_DIGEST];
247 const struct lc_hash *hash_algo;
248
249 enum lc_sig_types pkey_algo;
250
251 /*
252 * Pointers to raw daa to be signed in case no message digest is
253 * calculated. This pointer is set if no message digest is calculated.
254 */
255 const uint8_t *raw_data;
256 size_t raw_data_len;
257
258 /*
259 * Pointers to authenticated attributes to be signed.
260 */
261 const uint8_t *authattrs;
262 size_t authattrs_size;
263
264 /*
265 * Auth IDs of the signer
266 */
267 struct lc_asymmetric_key_id auth_ids[3];
268};
269
270struct lc_x509_certificate_name_component {
271 const char *value;
272 size_t size;
273};
274
275struct lc_x509_certificate_name {
276 struct lc_x509_certificate_name_component email;
277 struct lc_x509_certificate_name_component cn;
278 struct lc_x509_certificate_name_component ou;
279 struct lc_x509_certificate_name_component o;
280 struct lc_x509_certificate_name_component st;
281 struct lc_x509_certificate_name_component c;
282};
283
284/*
285 * The X.509 Generator also uses the parser for final operations. This
286 * data structure encapsulates the information only required during generation.
287 */
288struct lc_x509_key_data {
289 enum lc_sig_types sig_type;
290 unsigned int data_struct_size;
291 union {
292 struct lc_dilithium_pk *dilithium_pk;
293#ifdef LC_DILITHIUM_ED448_SIG
294 struct lc_dilithium_ed448_pk *dilithium_ed448_pk;
295#endif
296#ifdef LC_DILITHIUM_ED25519_SIG
297 struct lc_dilithium_ed25519_pk *dilithium_ed25519_pk;
298#endif
299 struct lc_sphincs_pk *sphincs_pk;
300 } pk;
301 union {
302 struct lc_dilithium_sk *dilithium_sk;
303#ifdef LC_DILITHIUM_ED448_SIG
304 struct lc_dilithium_ed448_sk *dilithium_ed448_sk;
305#endif
306#ifdef LC_DILITHIUM_ED25519_SIG
307 struct lc_dilithium_ed25519_sk *dilithium_ed25519_sk;
308#endif
309 struct lc_sphincs_sk *sphincs_sk;
310 } sk;
311 uint8_t sk_seed[LC_X509_PQC_SK_SEED_SIZE];
312 uint8_t pk_digest[LC_X509_SKID_DEFAULT_HASHSIZE];
313
314 unsigned int sk_seed_set : 1;
315};
316
317struct lc_x509_certificate {
318 struct lc_x509_certificate *next;
319 struct lc_x509_certificate *signer; /* Certificate that signed this one */
320 struct lc_x509_key_data sig_gen_data;
321 struct lc_x509_key_data pub_gen_data;
322 struct lc_public_key pub; /* Public key details */
323 struct lc_public_key_signature sig; /* Signature parameters */
324 struct lc_asymmetric_key_id id; /* Issuer + Serial number */
325 struct lc_asymmetric_key_id skid; /* Subject + subjectKeyId (optional) */
326
327 struct lc_x509_certificate_name issuer_segments;
328 struct lc_x509_certificate_name subject_segments;
329 struct lc_x509_certificate_name san_directory_name_segments;
330
331 /*
332 * Pointer to encoded certificate data. This is used when parsing
333 * a certificate.
334 */
335 const uint8_t *raw_cert;
336 size_t raw_cert_size;
337
338 const char *san_dns; /* Subject Alternative Name DNS */
339 size_t san_dns_len;
340 const uint8_t *san_ip; /* Subject Alternative Name IP */
341 size_t san_ip_len;
342 const char *san_email; /* Subject Alternative Name email */
343 size_t san_email_len;
344 time64_t valid_from; /* Time since EPOCH in UTC */
345 time64_t valid_to; /* Time since EPOCH in UTC */
346 const uint8_t *tbs; /* Signed data */
347 size_t tbs_size; /* Size of signed data */
348 size_t raw_sig_size; /* Size of signature */
349 const uint8_t *raw_sig; /* Signature data */
350 const uint8_t *raw_serial; /* Raw serial number in ASN.1 */
351 size_t raw_serial_size;
352 size_t raw_issuer_size;
353 const uint8_t *raw_issuer; /* Raw issuer name in ASN.1 */
354 const uint8_t *raw_subject; /* Raw subject name in ASN.1 */
355 size_t raw_subject_size;
356 size_t raw_skid_size;
357 const uint8_t *raw_skid; /* subjectKeyId in binary format */
358 size_t raw_akid_size;
359 const uint8_t *raw_akid; /* authority key Id binary format */
360 unsigned int index;
361
362 uint8_t x509_version; /* X.509 Version of certificate */
363
364 /* Certificate chain path length up to the CA certificate. */
365#define LC_PATHLEN_MAXSIZE ((1 << (sizeof(uint8_t) << 3)) - 1)
366 uint8_t consumed_pathlen;
367
368 unsigned int seen : 1; /* Infinite recursion prevention */
369 unsigned int verified : 1;
370 unsigned int self_signed
371 : 1; /* T if self-signed (check unsupported_sig too) */
372 unsigned int unsupported_sig
373 : 1; /* T if signature uses unsupported crypto */
374 unsigned int blacklisted : 1;
375 unsigned int allocated : 1;
376
377 /*
378 * Structure pertains to a CSR and shall not be handled as a regular
379 * certificate.
380 */
381 unsigned int is_csr : 1;
382};
383
385
395int lc_x509_sig_type_to_name(enum lc_sig_types pkey_algo, const char **alg);
396
405int lc_x509_sig_type_to_hash(enum lc_sig_types pkey_algo,
406 const struct lc_hash **hash_algo);
407
416int lc_x509_oid_to_hash(enum OID oid, const struct lc_hash **hash_algo);
417
427enum OID lc_look_up_OID(const uint8_t *data, size_t datasize);
428
429struct lc_tm {
430 unsigned short year;
431 unsigned char month;
432 unsigned char day;
433 unsigned char hour;
434 unsigned char min;
435 unsigned char sec;
436};
437
451int lc_gmtime(time64_t timeval, struct lc_tm *tm);
452
464int lc_OID_to_data(enum OID oid, const uint8_t **data, size_t *datalen);
465
466struct lc_asn1_encoder;
467
480int lc_asn1_ber_encoder(const struct lc_asn1_encoder *encoder, void *context,
481 uint8_t *data, size_t *in_out_avail_datalen);
482
495int lc_asn1_ber_encoder_small(const struct lc_asn1_encoder *encoder,
496 void *context, uint8_t *data,
497 size_t *in_out_avail_datalen);
498
499struct lc_asn1_decoder;
500
528int lc_asn1_ber_decoder(const struct lc_asn1_decoder *decoder, void *context,
529 const uint8_t *data, size_t datalen);
530
531#ifdef __cplusplus
532}
533#endif
534
535#endif /* LC_X509_COMMON_H */
int lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.
OID
Definition: lc_asn1.h:44
Dilithium public key.
Definition: lc_dilithium.h:801
Dilithium secret key.
Definition: lc_dilithium.h:783
Dilithium public key.
Dilithium secret key.
Dilithium public key.
Definition: lc_dilithium.h:125
Dilithium secret key.
Definition: lc_dilithium.h:107
Sphincs public key.
Definition: lc_sphincs.h:155
Sphincs secret key.
Definition: lc_sphincs.h:128
unsigned short year
int lc_gmtime(time64_t timeval, struct lc_tm *tm)
Trivial conversion of time to human-readable time value.
int lc_OID_to_data(enum OID oid, const uint8_t **data, size_t *datalen)
Conversion of OID to binary representation using an internal lookup table.
int lc_x509_sig_type_to_hash(enum lc_sig_types pkey_algo, const struct lc_hash **hash_algo)
Obtain the hash type to be used with a given public key algorithm.
int lc_x509_oid_to_hash(enum OID oid, const struct lc_hash **hash_algo)
Obtain the hash type to be used with a given OID.
int lc_asn1_ber_encoder_small(const struct lc_asn1_encoder *encoder, void *context, uint8_t *data, size_t *in_out_avail_datalen)
ASN.1 encoder with 4 level hierarchy defined on stack.
unsigned char month
int lc_x509_sig_type_to_name(enum lc_sig_types pkey_algo, const char **alg)
Convert a leancrypto public key algorithm reference into human readable form.
enum OID lc_look_up_OID(const uint8_t *data, size_t datasize)
Convert a binary representation of an OID to leancrypto internal reference.
int lc_asn1_ber_encoder(const struct lc_asn1_encoder *encoder, void *context, uint8_t *data, size_t *in_out_avail_datalen)
ASN.1 encoder with 10 level hierarchy.
unsigned char min
unsigned char day
int lc_asn1_ber_decoder(const struct lc_asn1_decoder *decoder, void *context, const uint8_t *data, size_t datalen)
Decoder BER/DER/CER ASN.1 according to pattern.
unsigned char sec
unsigned char hour