20#ifndef LC_X509_COMMON_H
21#define LC_X509_COMMON_H
23#include "ext_headers.h"
27#if defined __has_include
30#if __has_include("lc_dilithium.h")
32#define LC_DILITHIUM_ENABLED
37#if __has_include("lc_sphincs.h")
39#define LC_SPHINCS_ENABLED
44#error "Compiler misses __has_include"
60#define LC_X509_SKID_DEFAULT_HASH lc_sha3_256
61#define LC_X509_SKID_DEFAULT_HASHSIZE LC_SHA3_256_SIZE_DIGEST
62#define LC_X509_PQC_SK_SEED_SIZE 32
99#define LC_ASN1_MAX_ISSUER_NAME 128
100#define LC_ASN1_MAX_SKID 64
101struct lc_asymmetric_key_id {
103 uint8_t data[LC_ASN1_MAX_SKID + LC_ASN1_MAX_ISSUER_NAME];
116 LC_SIG_DILITHIUM_44_ED25519,
118 LC_SIG_DILITHIUM_65_ED25519,
120 LC_SIG_DILITHIUM_87_ED25519,
122 LC_SIG_DILITHIUM_44_ED448,
124 LC_SIG_DILITHIUM_65_ED448,
126 LC_SIG_DILITHIUM_87_ED448,
128 LC_SIG_SPINCS_SHAKE_256S,
130 LC_SIG_SPINCS_SHAKE_256F,
132 LC_SIG_SPINCS_SHAKE_192S,
134 LC_SIG_SPINCS_SHAKE_192F,
136 LC_SIG_SPINCS_SHAKE_128S,
138 LC_SIG_SPINCS_SHAKE_128F,
156struct lc_public_key {
159 const uint8_t *params;
163 enum lc_sig_types pkey_algo;
166#define LC_KEY_USAGE_DIGITALSIG 0x0080
167#define LC_KEY_USAGE_CONTENT_COMMITMENT 0x0040
168#define LC_KEY_USAGE_KEY_ENCIPHERMENT 0x0020
169#define LC_KEY_USAGE_DATA_ENCIPHERMENT 0x0010
170#define LC_KEY_USAGE_KEY_AGREEMENT 0x0008
171#define LC_KEY_USAGE_KEYCERTSIGN 0x0004
172#define LC_KEY_USAGE_CRLSIGN 0x0002
173#define LC_KEY_USAGE_ENCIPHER_ONLY 0x0001
174#define LC_KEY_USAGE_DECIPHER_ONLY 0x8000
175#define LC_KEY_USAGE_CRITICAL 0x4000
176#define LC_KEY_USAGE_EXTENSION_PRESENT 0x2000
177#define LC_KEY_USAGE_MASK \
178 ((uint16_t)~(LC_KEY_USAGE_CRITICAL | LC_KEY_USAGE_EXTENSION_PRESENT))
181#define LC_KEY_EKU_CRITICAL (1 << 1)
182#define LC_KEY_EKU_EXTENSION_PRESENT (1 << 2)
183#define LC_KEY_EKU_ANY (1 << 3)
184#define LC_KEY_EKU_SERVER_AUTH (1 << 4)
185#define LC_KEY_EKU_CLIENT_AUTH (1 << 5)
186#define LC_KEY_EKU_CODE_SIGNING (1 << 6)
187#define LC_KEY_EKU_EMAIL_PROTECTION (1 << 7)
188#define LC_KEY_EKU_TIME_STAMPING (1 << 8)
189#define LC_KEY_EKU_OCSP_SIGNING (1 << 9)
190#define LC_KEY_EKU_MODULE_SIGNING (1 << 10)
191#define LC_KEY_EKU_MASK \
192 ((uint16_t)~LC_KEY_EKU_CRITICAL | LC_KEY_EKU_EXTENSION_PRESENT)
194 uint8_t basic_constraint;
195#define LC_KEY_CA (1 << 2)
196#define LC_KEY_NOCA (1 << 1)
197#define LC_KEY_BASIC_CONSTRAINT_CRITICAL (1 << 0)
198#define LC_KEY_IS_CA (LC_KEY_CA | LC_KEY_BASIC_CONSTRAINT_CRITICAL)
201#define LC_KEY_CA_MAXLEN 16
202#define LC_KEY_CA_MASK ((LC_KEY_CA_MAXLEN << 1) - 1)
204 unsigned int key_is_private : 1;
210struct lc_public_key_signature {
228 uint8_t digest[LC_SHA_MAX_SIZE_DIGEST];
229 const struct lc_hash *hash_algo;
230 unsigned int request_prehash : 1;
232 enum lc_sig_types pkey_algo;
238 const uint8_t *raw_data;
244 struct lc_asymmetric_key_id auth_ids[3];
247struct lc_x509_certificate_name_component {
252struct lc_x509_certificate_name {
253 struct lc_x509_certificate_name_component email;
254 struct lc_x509_certificate_name_component cn;
255 struct lc_x509_certificate_name_component ou;
256 struct lc_x509_certificate_name_component o;
257 struct lc_x509_certificate_name_component st;
258 struct lc_x509_certificate_name_component c;
265struct lc_x509_key_data {
266 enum lc_sig_types sig_type;
267 unsigned int data_struct_size;
269 struct lc_dilithium_pk *dilithium_pk;
270#ifdef LC_DILITHIUM_ED448_SIG
271 struct lc_dilithium_ed448_pk *dilithium_ed448_pk;
273#ifdef LC_DILITHIUM_ED25519_SIG
274 struct lc_dilithium_ed25519_pk *dilithium_ed25519_pk;
276 struct lc_sphincs_pk *sphincs_pk;
279 struct lc_dilithium_sk *dilithium_sk;
280#ifdef LC_DILITHIUM_ED448_SIG
281 struct lc_dilithium_ed448_sk *dilithium_ed448_sk;
283#ifdef LC_DILITHIUM_ED25519_SIG
284 struct lc_dilithium_ed25519_sk *dilithium_ed25519_sk;
286 struct lc_sphincs_sk *sphincs_sk;
288 uint8_t sk_seed[LC_X509_PQC_SK_SEED_SIZE];
289 uint8_t pk_digest[LC_X509_SKID_DEFAULT_HASHSIZE];
292struct lc_x509_certificate {
293 struct lc_x509_certificate *next;
294 struct lc_x509_certificate *signer;
295 struct lc_x509_key_data sig_gen_data;
296 struct lc_x509_key_data pub_gen_data;
297 struct lc_public_key pub;
298 struct lc_public_key_signature sig;
299 struct lc_asymmetric_key_id id;
300 struct lc_asymmetric_key_id skid;
302 struct lc_x509_certificate_name issuer_segments;
303 struct lc_x509_certificate_name subject_segments;
304 struct lc_x509_certificate_name san_directory_name_segments;
310 const uint8_t *raw_cert;
311 size_t raw_cert_size;
315 const uint8_t *san_ip;
322 const uint8_t *raw_sig;
323 const uint8_t *raw_serial;
324 size_t raw_serial_size;
325 size_t raw_issuer_size;
326 const uint8_t *raw_issuer;
327 const uint8_t *raw_subject;
328 size_t raw_subject_size;
329 size_t raw_skid_size;
330 const uint8_t *raw_skid;
331 size_t raw_akid_size;
332 const uint8_t *raw_akid;
334 char issuer[LC_ASN1_MAX_ISSUER_NAME + 1];
335 char subject[LC_ASN1_MAX_ISSUER_NAME +
338 uint8_t x509_version;
339 unsigned int seen : 1;
340 unsigned int verified : 1;
345 unsigned int blacklisted : 1;
346 unsigned int allocated : 1;
371 const struct lc_hash **hash_algo);
int lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.
int lc_gmtime(time64_t timeval, struct lc_tm *tm)
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_sig_type_to_name(enum lc_sig_types pkey_algo, const char **alg)
Convert a leancrypto public key algorithm reference into human readable form.