41#define SWAP_BUF_SIZE 256
76 uint8_t *b =
BPTR(buf);
93 if (buf->
len >= e1 + e2)
96 memcpy(buf2, b + e1, e2);
98 memcpy(b + e2, buf1, e1);
123 if (ctx->
mode == TLS_WRAP_AUTH || ctx->
mode == TLS_WRAP_NONE)
128 if (ctx->
mode == TLS_WRAP_AUTH)
136 else if (ctx->
mode == TLS_WRAP_CRYPT)
176 if (
session->tls_wrap.mode == TLS_WRAP_NONE && !
session->opt->server
205 msg(
D_TLS_ERRORS,
"TLS Error: can not extract tls-crypt-v2 client key from %s",
210 if (ctx->
mode == TLS_WRAP_AUTH)
217 msg(
D_TLS_ERRORS,
"TLS Error: cannot locate HMAC in incoming packet from %s",
228 msg(
D_TLS_ERRORS,
"TLS Error: incoming packet authentication failed from %s",
233 else if (ctx->
mode == TLS_WRAP_CRYPT)
259 if (ctx->
mode == TLS_WRAP_NONE || ctx->
mode == TLS_WRAP_AUTH)
308 uint8_t pkt_firstbyte = *
BPTR(buf);
330 "TLS State Error: Unknown key ID (%d) received from %s -- 0 was expected", key_id,
407 bool request_resend_wkc)
448 const uint8_t *
key,
int handwindow,
int offset)
453 uint32_t session_id_time = ntohl((uint32_t)(
now / ((handwindow + 1) / 2) + offset));
458 static_assert(
sizeof(input) >=
sizeof(
struct sockaddr_in6) +
sizeof(session_id_time) +
sizeof(client_sid.id),
"input buffer not sized correctly");
459 static_assert(
sizeof(input) >=
sizeof(
struct sockaddr_in) +
sizeof(session_id_time) +
sizeof(client_sid.id),
"input buffer not sized correctly");
469 switch (from->addr.sa.sa_family)
510 for (
int i = 0;
i <
ack.len;
i++)
515 if (
ack.packet_id[
i] > 1)
541 for (
int offset = -2; offset <= 0; offset++)
559 int cmdlen = (int)strnlen(
BSTR(buf),
BLENZ(buf));
561 if (cmdlen >=
BLEN(buf))
565 struct buffer empty = { 0 };
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
void buf_clear(struct buffer *buf)
Zeroise and reset a buffer.
struct buffer clone_buf(const struct buffer *buf)
Duplicate a buffer, including its content.
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)
Allocate a buffer of the given size under garbage collection.
void buf_chomp(struct buffer *buf)
Remove trailing newline and carriage-return characters from a buffer.
static bool buf_write_u16(struct buffer *dest, uint16_t data)
Append a uint16_t to a buffer in network byte order.
static uint8_t * buf_bptr(const struct buffer *buf)
Return a pointer to the start of the buffer content.
#define BSTR(buf)
Return the buffer content pointer cast to char *.
static struct buffer clear_buf(void)
Return an empty, undefined struct buffer (all fields zero).
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Copy the content of one buffer to the end of another.
#define BPTR(buf)
Return a pointer to the start of the buffer content.
static bool buf_write_u32(struct buffer *dest, uint32_t data)
Append a uint32_t to a buffer in network byte order.
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Prepend data to a buffer.
#define CC_CRLF
carriage return or newline
static void buf_set_write(struct buffer *buf, uint8_t *data, int size)
Initialise a buffer with an externally provided writable memory region.
static int buf_len(const struct buffer *buf)
Return the length of the buffer content.
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
static bool buf_advance(struct buffer *buf, ssize_t size)
Advance the content start of a buffer, consuming bytes from the front.
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
#define BLEN(buf)
Return the length of the buffer content in bytes.
static char * format_hex(const uint8_t *data, size_t size, size_t maxoutput, struct gc_arena *gc)
Format a binary buffer as a hex string with spaces every 4 bytes.
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
#define CC_NULL
null character \0
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
#define CC_PRINT
printable (>= 32, != 127)
#define buf_init(buf, offset)
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
static int buf_forward_capacity_total(const struct buffer *buf)
Return the total number of bytes available from the current offset to the end of the allocated memory...
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.
int hmac_ctx_size(hmac_ctx_t *ctx)
#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_read_packet_id(struct buffer *buf, packet_id_type *pid)
Read the packet ID of a received packet.
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)
static void siphash(const void *in, size_t inlen, const void *k, uint8_t *out, size_t outlen)
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.
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...
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...
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)
Read a control channel authentication record.
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.
struct session_id calculate_session_id_hmac(struct session_id client_sid, const struct openvpn_sockaddr *from, const uint8_t *key, int handwindow, int offset)
Calculates the HMAC based server session id based on a client session id and socket addr.
bool check_session_hmac_and_pkt_id(struct tls_pre_decrypt_state *state, const struct openvpn_sockaddr *from, uint8_t *key, int handwindow, bool pkt_is_ack)
Checks if a control packet has a correct HMAC server session id.
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.
bool cleanup_key_ctx
opt.key_ctx_bi is owned by this context
struct crypto_options opt
Crypto state.
enum tls_wrap_ctx::@28 mode
Control channel wrapping mode.
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.
struct key2 original_wrap_keydata
original key data to be xored in to the key for dynamic tls-crypt.
static int cleanup(void **state)