41#define SWAP_BUF_SIZE 256
77 uint8_t *b =
BPTR(buf);
94 if (buf->
len >= e1 + e2)
97 memcpy(buf2, b + e1, e2);
99 memcpy(b + e2, buf1, e1);
124 if (ctx->
mode == TLS_WRAP_AUTH
125 || ctx->
mode == TLS_WRAP_NONE)
130 if (ctx->
mode == TLS_WRAP_AUTH)
138 else if (ctx->
mode == TLS_WRAP_CRYPT)
181 if (
session->tls_wrap.mode == TLS_WRAP_NONE && !
session->opt->server
214 "TLS Error: can not extract tls-crypt-v2 client key from %s",
219 if (ctx->
mode == TLS_WRAP_AUTH)
227 "TLS Error: cannot locate HMAC in incoming packet from %s",
239 "TLS Error: incoming packet authentication failed from %s",
245 else if (ctx->
mode == TLS_WRAP_CRYPT)
271 if (ctx->
mode == TLS_WRAP_NONE || ctx->
mode == TLS_WRAP_AUTH)
317 "TLS State Error: Too short packet (length %d) received from %s",
323 uint8_t pkt_firstbyte = *
BPTR(buf);
341 "TLS State Error: No TLS state for client %s, opcode=%d",
350 "TLS State Error: Unknown key ID (%d) received from %s -- 0 was expected",
364 "TLS Error: session-id not found in packet from %s",
433 bool request_resend_wkc)
490 int handwindow,
int offset)
500 uint32_t session_id_time = ntohl(
now/((handwindow+1)/2) + offset);
506 sizeof(session_id_time));
509 switch (from->addr.sa.sa_family)
512 hmac_ctx_update(hmac, (
const uint8_t *) &from->addr.in4,
sizeof(
struct sockaddr_in));
516 hmac_ctx_update(hmac, (
const uint8_t *) &from->addr.in6,
sizeof(
struct sockaddr_in6));
548 for (
int offset = -2; offset <= 1; offset++)
566 int cmdlen = (int)strnlen(
BSTR(buf),
BLEN(buf));
568 if (cmdlen >=
BLEN(buf))
572 struct buffer empty = { 0 };
void free_buf(struct buffer *buf)
void buf_clear(struct buffer *buf)
struct buffer clone_buf(const struct buffer *buf)
bool string_check_buf(struct buffer *buf, const unsigned int inclusive, const unsigned int exclusive)
Check a buffer if it only consists of allowed characters.
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
void buf_chomp(struct buffer *buf)
static bool buf_write_u16(struct buffer *dest, uint16_t data)
static char * format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
static struct buffer clear_buf(void)
Return an empty struct buffer.
static bool buf_copy(struct buffer *dest, const struct buffer *src)
static bool buf_write_u32(struct buffer *dest, uint32_t data)
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
#define CC_CRLF
carriage return or newline
static bool buf_advance(struct buffer *buf, int size)
static bool buf_write(struct buffer *dest, const void *src, size_t size)
static bool buf_write_u8(struct buffer *dest, uint8_t data)
#define CC_NULL
null character \0
static void gc_free(struct gc_arena *a)
#define CC_PRINT
printable (>= 32, != 127)
#define buf_init(buf, offset)
static struct gc_arena gc_new(void)
static int buf_forward_capacity_total(const struct buffer *buf)
void free_key_ctx_bi(struct key_ctx_bi *ctx)
Data Channel Cryptography Module.
#define CO_USE_TLS_KEY_MATERIAL_EXPORT
Bit-flag indicating that data channel key derivation is done using TLS keying material export [RFC570...
int memcmp_constant_time(const void *a, const void *b, size_t size)
As memcmp(), but constant-time.
void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len)
hmac_ctx_t * hmac_ctx_new(void)
void hmac_ctx_reset(hmac_ctx_t *ctx)
void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname)
void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst)
bool md_valid(const char *digest)
Return if a message digest parameters is valid given the name of the digest.
int hmac_ctx_size(hmac_ctx_t *ctx)
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
mbedtls_md_context_t hmac_ctx_t
Generic HMAC context.
#define SHA256_DIGEST_LENGTH
#define D_TLS_STATE_ERRORS
enum first_packet_verdict tls_pre_decrypt_lite(const struct tls_auth_standalone *tas, struct tls_pre_decrypt_state *state, const struct link_socket_actual *from, const struct buffer *buf)
Inspect an incoming packet for which no VPN tunnel is active, and determine whether a new VPN tunnel ...
void openvpn_encrypt(struct buffer *buf, struct buffer work, struct crypto_options *opt)
Encrypt and HMAC sign a packet so that it can be sent as a data channel VPN tunnel packet to a remote...
bool openvpn_decrypt(struct buffer *buf, struct buffer work, struct crypto_options *opt, const struct frame *frame, const uint8_t *ad_start)
HMAC verify and decrypt a data channel packet received from a remote OpenVPN peer.
bool reliable_ack_parse(struct buffer *buf, struct reliable_ack *ack, struct session_id *session_id_remote)
Parse an acknowledgment record from a received packet.
bool reliable_ack_write(struct reliable_ack *ack, struct reliable_ack *ack_mru, struct buffer *buf, const struct session_id *sid, int max, bool prepend)
Write a packet ID acknowledgment record to a buffer.
bool tls_crypt_v2_extract_client_key(struct buffer *buf, struct tls_wrap_ctx *ctx, const struct tls_options *opt)
Extract a tls-crypt-v2 client key from a P_CONTROL_HARD_RESET_CLIENT_V3 message, and load the key int...
bool tls_crypt_unwrap(const struct buffer *src, struct buffer *dst, struct crypto_options *opt)
Unwrap a control channel packet (decrypts, authenticates and performs replay checks).
bool tls_crypt_wrap(const struct buffer *src, struct buffer *dst, struct crypto_options *opt)
Wrap a control channel packet (both authenticates and encrypts the data).
static int min_int(int x, int y)
static SERVICE_STATUS status
static int packet_id_size(bool long_form)
Reliability Layer module header file.
static bool session_id_write_prepend(const struct session_id *sid, struct buffer *buf)
static bool session_id_write(const struct session_id *sid, struct buffer *buf)
static bool session_id_defined(const struct session_id *sid1)
static bool session_id_read(struct session_id *sid, struct buffer *buf)
const char * print_link_socket_actual(const struct link_socket_actual *act, struct gc_arena *gc)
static bool link_socket_actual_defined(const struct link_socket_actual *act)
void tls_clear_error(void)
Clear the underlying SSL library's error state.
Control Channel Common Data Structures.
bool check_session_id_hmac(struct tls_pre_decrypt_state *state, const struct openvpn_sockaddr *from, hmac_ctx_t *hmac, int handwindow)
Checks if a control packet has a correct HMAC server session id.
static void tls_wrap_control(struct tls_wrap_ctx *ctx, uint8_t header, struct buffer *buf, struct session_id *session_id)
Wraps a TLS control packet by adding tls-auth HMAC or tls-crypt(-v2) encryption and opcode header inc...
struct session_id calculate_session_id_hmac(struct session_id client_sid, const struct openvpn_sockaddr *from, hmac_ctx_t *hmac, int handwindow, int offset)
Calculates the HMAC based server session id based on a client session id and socket addr.
void free_tls_pre_decrypt_state(struct tls_pre_decrypt_state *state)
void write_control_auth(struct tls_session *session, struct key_state *ks, struct buffer *buf, struct link_socket_actual **to_link_addr, int opcode, int max_ack, bool prepend_ack)
static bool swap_hmac(struct buffer *buf, const struct crypto_options *co, bool incoming)
Move a packet authentication HMAC + related fields to or from the front of the buffer so it can be pr...
hmac_ctx_t * session_id_hmac_init(void)
struct buffer extract_command_buffer(struct buffer *buf, struct gc_arena *gc)
Extracts a control channel message from buf and adjusts the size of buf after the message has been ex...
bool read_control_auth(struct buffer *buf, struct tls_wrap_ctx *ctx, const struct link_socket_actual *from, const struct tls_options *opt)
struct buffer tls_reset_standalone(struct tls_wrap_ctx *ctx, struct tls_auth_standalone *tas, struct session_id *own_sid, struct session_id *remote_sid, uint8_t header, bool request_resend_wkc)
This function creates a reset packet using the information from the tls pre decrypt state.
SSL control channel wrap/unwrap and decode functions.
#define EARLY_NEG_FLAG_RESEND_WKC
#define TLV_TYPE_EARLY_NEG_FLAGS
static const char * packet_opcode_name(int op)
@ VERDICT_VALID_ACK_V1
This packet is a valid ACK control packet from the peer, i.e.
@ VERDICT_VALID_WKC_V1
The packet is a valid control packet with appended wrapped client key.
@ VERDICT_VALID_RESET_V2
This packet is a valid reset packet from the peer (all but tls-crypt-v2)
@ VERDICT_INVALID
the packet failed on of the various checks
@ VERDICT_VALID_RESET_V3
This is a valid v3 reset (tls-crypt-v2)
@ VERDICT_VALID_CONTROL_V1
This packet is a valid control packet from the peer.
#define P_CONTROL_HARD_RESET_CLIENT_V2
static struct tls_wrap_ctx * tls_session_get_tls_wrap(struct tls_session *session, int key_id)
Determines if the current session should use the renegotiation tls wrap struct instead the normal one...
#define P_CONTROL_HARD_RESET_CLIENT_V3
Wrapper structure for dynamically allocated memory.
int len
Length in bytes of the actual content within the allocated memory.
int offset
Offset in bytes of the actual content within the allocated memory.
Security parameter state for processing data channel packets.
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
Garbage collection arena used to keep track of dynamically allocated memory.
struct key_ctx decrypt
cipher and/or HMAC contexts for receiving direction.
struct key_ctx encrypt
Cipher and/or HMAC contexts for sending direction.
Container for one set of cipher and/or HMAC contexts.
cipher_ctx_t * cipher
Generic cipher context.
hmac_ctx_t * hmac
Generic HMAC context.
Security parameter state of one TLS and data channel key session.
struct link_socket_actual remote_addr
struct reliable_ack * rec_ack
struct session_id session_id_remote
int key_id
Key id for this key_state, inherited from struct tls_session.
struct reliable_ack * lru_acks
Container for unidirectional cipher and HMAC key material.
The acknowledgment structure in which packet IDs are stored for later acknowledgment.
struct that stores the temporary data for the tls lite decrypt functions
struct session_id peer_session_id
struct session_id server_session_id
struct tls_wrap_ctx tls_wrap_tmp
Security parameter state of a single session within a VPN tunnel.
Control channel wrapping (–tls-auth/–tls-crypt) context.
struct buffer tls_crypt_v2_metadata
Received from client.
bool cleanup_key_ctx
opt.key_ctx_bi is owned by this context
struct crypto_options opt
Crypto state.
struct buffer work
Work buffer (only for –tls-crypt)
struct key_ctx tls_crypt_v2_server_key
Decrypts client keys.
const struct buffer * tls_crypt_v2_wkc
Wrapped client key, sent to server.
enum tls_wrap_ctx::@23 mode
Control channel wrapping mode.
static int cleanup(void **state)