Go to the documentation of this file.
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
190 max_ack, prepend_ack));
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)
458 if (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++)
@ VERDICT_VALID_RESET_V2
This packet is a valid reset packet from the peer (all but tls-crypt-v2)
#define D_TLS_STATE_ERRORS
@ VERDICT_INVALID
the packet failed on of the various checks
static struct gc_arena gc_new(void)
mbedtls_md_context_t hmac_ctx_t
Generic HMAC context.
static int buf_forward_capacity_total(const struct buffer *buf)
int len
Length in bytes of the actual content within the allocated memory.
#define P_CONTROL_HARD_RESET_CLIENT_V2
const struct buffer * tls_crypt_v2_wkc
Wrapped client key, sent to server.
struct reliable_ack * rec_ack
#define buf_init(buf, offset)
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).
static bool buf_write_u32(struct buffer *dest, uint32_t data)
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.
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 ...
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
cipher_ctx_t * cipher
Generic cipher context.
struct reliable_ack * lru_acks
hmac_ctx_t * session_id_hmac_init(void)
static bool session_id_write(const struct session_id *sid, struct buffer *buf)
@ VERDICT_VALID_CONTROL_V1
This packet is a valid control packet from the peer.
void buf_clear(struct buffer *buf)
void free_tls_pre_decrypt_state(struct tls_pre_decrypt_state *state)
struct key_ctx encrypt
Cipher and/or HMAC contexts for sending direction.
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...
static bool buf_copy(struct buffer *dest, const struct buffer *src)
void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len)
static struct buffer clear_buf(void)
Return an empty struct buffer.
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.
struct buffer clone_buf(const struct buffer *buf)
bool read_control_auth(struct buffer *buf, struct tls_wrap_ctx *ctx, const struct link_socket_actual *from, const struct tls_options *opt)
struct session_id server_session_id
Security parameter state of one TLS and data channel key session.
Container for unidirectional cipher and HMAC key material.
struct tls_wrap_ctx tls_wrap_tmp
#define EARLY_NEG_FLAG_RESEND_WKC
void free_key_ctx_bi(struct key_ctx_bi *ctx)
struct crypto_options opt
Crypto state.
#define P_CONTROL_HARD_RESET_CLIENT_V3
static bool link_socket_actual_defined(const struct link_socket_actual *act)
struct key_ctx tls_crypt_v2_server_key
Decrypts client keys.
struct session_id peer_session_id
static bool buf_advance(struct buffer *buf, int size)
static bool buf_write_u16(struct buffer *dest, uint16_t data)
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Control channel wrapping (–tls-auth/–tls-crypt) context.
struct link_socket_actual remote_addr
struct buffer work
Work buffer (only for –tls-crypt)
static bool session_id_read(struct session_id *sid, struct buffer *buf)
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...
struct that stores the temporary data for the tls lite decrypt functions
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).
Container for one set of cipher and/or HMAC contexts.
struct buffer tls_crypt_v2_metadata
Received from client.
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)
struct session_id session_id_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.
void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst)
Wrapper structure for dynamically allocated memory.
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 hmac_ctx_reset(hmac_ctx_t *ctx)
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Security parameter state of a single session within a VPN tunnel.
static bool session_id_defined(const struct session_id *sid1)
Garbage collection arena used to keep track of dynamically allocated memory.
int key_id
Key id for this key_state, inherited from struct tls_session.
int offset
Offset in bytes of the actual content within the allocated memory.
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 tls_wrap_ctx tls_wrap
void free_buf(struct buffer *buf)
int memcmp_constant_time(const void *a, const void *b, size_t size)
As memcmp(), but constant-time.
#define SHA256_DIGEST_LENGTH
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...
static SERVICE_STATUS status
static int packet_id_size(bool long_form)
const char * print_link_socket_actual(const struct link_socket_actual *act, struct gc_arena *gc)
static int min_int(int x, int y)
static void gc_free(struct gc_arena *a)
static bool session_id_write_prepend(const struct session_id *sid, struct buffer *buf)
@ VERDICT_VALID_RESET_V3
This is a valid v3 reset (tls-crypt-v2)
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
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.
hmac_ctx_t * hmac_ctx_new(void)
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...
void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname)
bool md_valid(const char *digest)
Return if a message digest parameters is valid given the name of the digest.
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.
hmac_ctx_t * hmac
Generic HMAC context.
@ VERDICT_VALID_ACK_V1
This packet is a valid ACK control packet from the peer, i.e.
#define CO_USE_TLS_KEY_MATERIAL_EXPORT
Bit-flag indicating that data channel key derivation is done using TLS keying material export [RFC570...
enum tls_wrap_ctx::@17 mode
Control channel wrapping mode.
The acknowledgment structure in which packet IDs are stored for later acknowledgment.
@ VERDICT_VALID_WKC_V1
The packet is a valid control packet with appended wrapped client key.
void tls_clear_error(void)
Clear the underlying SSL library's error state.
struct key_ctx decrypt
cipher and/or HMAC contexts for receiving direction.
bool cleanup_key_ctx
opt.key_ctx_bi is owned by this context
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
int hmac_ctx_size(hmac_ctx_t *ctx)
Security parameter state for processing data channel packets.
static const char * packet_opcode_name(int op)
#define TLV_TYPE_EARLY_NEG_FLAGS
static int cleanup(void **state)