Loading...
Searching...
No Matches
crypto.h
Go to the documentation of this file.
1
12#ifndef IMQUIC_CRYPTO_H
13#define IMQUIC_CRYPTO_H
14
15#include <stdint.h>
16
17#include <glib.h>
18
19#include <openssl/ssl.h>
20#include <openssl/crypto.h>
21#include <openssl/evp.h>
22#ifdef IMQUIC_BORINGSSL
23#include <openssl/hkdf.h>
24#else
25#include <openssl/kdf.h>
26#endif
27#include <openssl/err.h>
28
32const char *imquic_encryption_level_str(enum ssl_encryption_level_t level);
37const char *imquic_encryption_key_type_str(enum ssl_encryption_level_t level, gboolean server);
38
42int imquic_tls_init(const char *secrets_log);
44void imquic_tls_deinit(void);
45
47typedef struct imquic_tls {
49 gboolean is_server;
51 SSL_CTX *ssl_ctx;
53 X509 *ssl_cert;
55 EVP_PKEY *ssl_key;
57 gboolean early_data;
67imquic_tls *imquic_tls_create(gboolean is_server, const char *server_pem, const char *server_key, const char *password);
72int imquic_tls_enable_early_data(imquic_tls *tls, const char *ticket_file);
80
82typedef struct imquic_encryption {
84 const EVP_MD *md;
86 uint8_t secret[2][48];
88 size_t secret_len;
90 uint8_t key[2][32];
92 size_t key_len;
94 uint8_t iv[2][12];
96 size_t iv_len;
98 uint8_t hp[32];
100 size_t hp_len;
109
113
120int imquic_build_hkdf_label(const char *label, uint8_t *hkdf_label, size_t buflen, size_t outlen);
128int imquic_hkdf_extract(const EVP_MD *digest, uint8_t *key, size_t keylen, uint8_t *out, size_t *outlen);
137int imquic_hkdf_expand_label(const EVP_MD *digest, uint8_t *key, size_t keylen, const char *label, uint8_t *out, size_t outlen);
146int imquic_derive_initial_secret(imquic_protection *p, uint8_t *dcid, size_t dcid_len, gboolean is_server);
154int imquic_expand_secret(const char *name, imquic_encryption *e, gboolean expand_hp, gboolean phase);
156
161int imquic_update_keys(imquic_protection *p, gboolean phase);
162
166
174int imquic_unprotect_header(uint8_t *bytes, size_t blen, size_t pn_offset, uint8_t *hp, size_t hp_len);
183int imquic_protect_header(uint8_t *bytes, size_t blen, size_t pn_offset, uint8_t *hp, size_t hp_len);
185
189
202int imquic_decrypt_payload(uint8_t *bytes, size_t blen, uint8_t *to, size_t tlen, uint8_t *header, size_t hlen, uint64_t pn, uint8_t *key, size_t key_len, uint8_t *iv, size_t iv_len);
216int imquic_encrypt_payload(uint8_t *bytes, size_t blen, uint8_t *to, size_t tlen, uint8_t *header, size_t hlen, uint64_t pn, uint8_t *key, size_t key_len, uint8_t *iv, size_t iv_len);
218
225int imquic_verify_retry(uint8_t *bytes, size_t blen, uint8_t *dcid, size_t dcid_len);
226
227#endif
imquic_tls * imquic_tls_create(gboolean is_server, const char *server_pem, const char *server_key, const char *password)
Helper to create a new TLS context.
Definition crypto.c:218
int imquic_encrypt_payload(uint8_t *bytes, size_t blen, uint8_t *to, size_t tlen, uint8_t *header, size_t hlen, uint64_t pn, uint8_t *key, size_t key_len, uint8_t *iv, size_t iv_len)
Encrypt a QUIC payload to send.
Definition crypto.c:714
const char * imquic_encryption_level_str(enum ssl_encryption_level_t level)
Helper function to serialize to string the name of an SSL encryption level.
Definition crypto.c:36
int imquic_verify_retry(uint8_t *bytes, size_t blen, uint8_t *dcid, size_t dcid_len)
Verify the integrity of a Retry packet.
Definition crypto.c:782
void imquic_tls_destroy(imquic_tls *tls)
Destroy an existing imquic_tls context.
Definition crypto.c:311
int imquic_derive_initial_secret(imquic_protection *p, uint8_t *dcid, size_t dcid_len, gboolean is_server)
Helper to derive the initial secrets from a known connection ID.
Definition crypto.c:451
struct imquic_encryption imquic_encryption
Encryption context for a specific direction and encryption level in a QUIC connection.
int imquic_decrypt_payload(uint8_t *bytes, size_t blen, uint8_t *to, size_t tlen, uint8_t *header, size_t hlen, uint64_t pn, uint8_t *key, size_t key_len, uint8_t *iv, size_t iv_len)
Decrypt a received QUIC payload.
Definition crypto.c:648
int imquic_protect_header(uint8_t *bytes, size_t blen, size_t pn_offset, uint8_t *hp, size_t hp_len)
Protect a QUIC message to send (and obfuscate the header)
Definition crypto.c:604
int imquic_hkdf_expand_label(const EVP_MD *digest, uint8_t *key, size_t keylen, const char *label, uint8_t *out, size_t outlen)
Helper to expand an HKDF label.
Definition crypto.c:425
struct imquic_protection imquic_protection
Protection context in both directions for a specific encryption level in a QUIC connection.
int imquic_tls_enable_early_data(imquic_tls *tls, const char *ticket_file)
Enable early data on an existing TLS context.
Definition crypto.c:367
int imquic_unprotect_header(uint8_t *bytes, size_t blen, size_t pn_offset, uint8_t *hp, size_t hp_len)
Unprotect a received QUIC message (and unobfuscate the header)
Definition crypto.c:558
int imquic_hkdf_extract(const EVP_MD *digest, uint8_t *key, size_t keylen, uint8_t *out, size_t *outlen)
Helper to perform an HKDF extract.
Definition crypto.c:403
SSL * imquic_tls_new_ssl(imquic_tls *tls)
Get a new SSL instance from an existing TLS context.
Definition crypto.c:273
const char * imquic_encryption_key_type_str(enum ssl_encryption_level_t level, gboolean server)
Helper function to serialize to string the key type of an SSL encryption level.
Definition crypto.c:51
int imquic_tls_init(const char *secrets_log)
Initialize the TLS stack at startup.
Definition crypto.c:196
int imquic_expand_secret(const char *name, imquic_encryption *e, gboolean expand_hp, gboolean phase)
Helper to expand a secret, taking into account the key phase.
Definition crypto.c:501
struct imquic_tls imquic_tls
TLS context.
void imquic_tls_deinit(void)
Uninitialize the TLS stack.
Definition crypto.c:212
int imquic_build_hkdf_label(const char *label, uint8_t *hkdf_label, size_t buflen, size_t outlen)
Helper to build an HKDF label, to use with HKDF_expand.
Definition crypto.c:388
int imquic_update_keys(imquic_protection *p, gboolean phase)
Update the key phase for an existing context.
Definition crypto.c:530
Encryption context for a specific direction and encryption level in a QUIC connection.
Definition crypto.h:82
size_t iv_len
Length of the IV.
Definition crypto.h:96
size_t key_len
Length of the key.
Definition crypto.h:92
uint8_t key[2][32]
Key (key phased)
Definition crypto.h:90
const EVP_MD * md
Hashing algorithm.
Definition crypto.h:84
uint8_t secret[2][48]
Secret (key phased)
Definition crypto.h:86
size_t hp_len
Length of the header protection.
Definition crypto.h:100
uint8_t iv[2][12]
IV (key phased)
Definition crypto.h:94
size_t secret_len
Length of the secret.
Definition crypto.h:88
uint8_t hp[32]
Header protection.
Definition crypto.h:98
Protection context in both directions for a specific encryption level in a QUIC connection.
Definition crypto.h:103
imquic_encryption remote
Remote encryption context.
Definition crypto.h:107
imquic_encryption local
Local encryption context.
Definition crypto.h:105
TLS context.
Definition crypto.h:47
SSL_CTX * ssl_ctx
TLS context.
Definition crypto.h:51
gboolean early_data
Whether early data should be supported.
Definition crypto.h:57
EVP_PKEY * ssl_key
Key.
Definition crypto.h:55
gboolean is_server
Whether this is for a server or a client.
Definition crypto.h:49
char * ticket_file
File to use for session tickets, when doing early data.
Definition crypto.h:59
X509 * ssl_cert
Certificate.
Definition crypto.h:53