Loading...
Searching...
No Matches
crypto.h
Go to the documentation of this file.
1
11
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 char *key_pwd;
55 gboolean early_data;
66imquic_tls *imquic_tls_create(gboolean is_server, const char *server_pem, const char *server_key, const char *password, gboolean verify);
71int imquic_tls_enable_early_data(imquic_tls *tls, const char *ticket_file);
79
81typedef struct imquic_encryption {
83 const EVP_MD *md;
85 uint8_t secret[2][48];
87 size_t secret_len;
89 uint8_t key[2][32];
91 size_t key_len;
93 uint8_t iv[2][12];
95 size_t iv_len;
97 uint8_t hp[32];
99 size_t hp_len;
108
112
119int imquic_build_hkdf_label(const char *label, uint8_t *hkdf_label, size_t buflen, size_t outlen);
127int imquic_hkdf_extract(const EVP_MD *digest, uint8_t *key, size_t keylen, uint8_t *out, size_t *outlen);
136int imquic_hkdf_expand_label(const EVP_MD *digest, uint8_t *key, size_t keylen, const char *label, uint8_t *out, size_t outlen);
145int imquic_derive_initial_secret(imquic_protection *p, uint8_t *dcid, size_t dcid_len, gboolean is_server);
153int imquic_expand_secret(const char *name, imquic_encryption *e, gboolean expand_hp, gboolean phase);
155
160int imquic_update_keys(imquic_protection *p, gboolean phase);
161
165
173int imquic_unprotect_header(uint8_t *bytes, size_t blen, size_t pn_offset, uint8_t *hp, size_t hp_len);
182int imquic_protect_header(uint8_t *bytes, size_t blen, size_t pn_offset, uint8_t *hp, size_t hp_len);
184
188
201int 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);
215int 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);
217
224int imquic_verify_retry(uint8_t *bytes, size_t blen, uint8_t *dcid, size_t dcid_len);
225
226#endif
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:701
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:769
void imquic_tls_destroy(imquic_tls *tls)
Destroy an existing imquic_tls context.
Definition crypto.c:294
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:438
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:635
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:591
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:412
int imquic_tls_enable_early_data(imquic_tls *tls, const char *ticket_file)
Enable early data on an existing TLS context.
Definition crypto.c:349
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:545
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:390
SSL * imquic_tls_new_ssl(imquic_tls *tls)
Get a new SSL instance from an existing TLS context.
Definition crypto.c:250
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:170
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:488
void imquic_tls_deinit(void)
Uninitialize the TLS stack.
Definition crypto.c:186
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:375
int imquic_update_keys(imquic_protection *p, gboolean phase)
Update the key phase for an existing context.
Definition crypto.c:517
imquic_tls * imquic_tls_create(gboolean is_server, const char *server_pem, const char *server_key, const char *password, gboolean verify)
Helper to create a new TLS context.
Definition crypto.c:192
Encryption context for a specific direction and encryption level in a QUIC connection.
Definition crypto.h:81
size_t iv_len
Length of the IV.
Definition crypto.h:95
size_t key_len
Length of the key.
Definition crypto.h:91
uint8_t key[2][32]
Key (key phased)
Definition crypto.h:89
const EVP_MD * md
Hashing algorithm.
Definition crypto.h:83
uint8_t secret[2][48]
Secret (key phased)
Definition crypto.h:85
size_t hp_len
Length of the header protection.
Definition crypto.h:99
uint8_t iv[2][12]
IV (key phased)
Definition crypto.h:93
size_t secret_len
Length of the secret.
Definition crypto.h:87
uint8_t hp[32]
Header protection.
Definition crypto.h:97
Protection context in both directions for a specific encryption level in a QUIC connection.
Definition crypto.h:102
imquic_encryption remote
Remote encryption context.
Definition crypto.h:106
imquic_encryption local
Local encryption context.
Definition crypto.h:104
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:55
gboolean is_server
Whether this is for a server or a client.
Definition crypto.h:49
char * key_pwd
Key password, if needed.
Definition crypto.h:53
char * ticket_file
File to use for session tickets, when doing early data.
Definition crypto.h:57