OpenVPN
ssl_pkt.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
26#include "syshead.h"
27
28#include "ssl_util.h"
29#include "ssl_pkt.h"
30#include "ssl_common.h"
31#include "crypto.h"
32#include "session_id.h"
33#include "reliable.h"
34#include "siphash.h"
35#include "tls_crypt.h"
36#include "control_msg.h"
37
38/*
39 * Dependent on hmac size, opcode size, and session_id size.
40 * Will assert if too small.
41 */
42#define SWAP_BUF_SIZE 256
43
44#if defined(__GNUC__) || defined(__clang__)
45#pragma GCC diagnostic push
46#pragma GCC diagnostic ignored "-Wsign-conversion"
47#endif
48
66static bool
67swap_hmac(struct buffer *buf, const struct crypto_options *co, bool incoming)
68{
69 ASSERT(co);
70
71 const struct key_ctx *ctx = (incoming ? &co->key_ctx_bi.decrypt : &co->key_ctx_bi.encrypt);
72 ASSERT(ctx->hmac);
73
74 {
75 /* hmac + packet_id (8 bytes) */
76 const int hmac_size = hmac_ctx_size(ctx->hmac) + packet_id_size(true);
77
78 /* opcode (1 byte) + session_id (8 bytes) */
79 const int osid_size = 1 + SID_SIZE;
80
81 int e1, e2;
82 uint8_t *b = BPTR(buf);
83
84 if (incoming)
85 {
86 e1 = osid_size;
87 e2 = hmac_size;
88 }
89 else
90 {
91 e1 = hmac_size;
92 e2 = osid_size;
93 }
94
95 ASSERT(e1 <= SWAP_BUF_SIZE && e2 <= SWAP_BUF_SIZE);
96
97 if (buf->len >= e1 + e2)
98 {
99 uint8_t buf1[SWAP_BUF_SIZE];
100 uint8_t buf2[SWAP_BUF_SIZE];
101
102 memcpy(buf1, b, e1);
103 memcpy(buf2, b + e1, e2);
104 memcpy(b, buf2, e2);
105 memcpy(b + e2, buf1, e1);
106 return true;
107 }
108 else
109 {
110 return false;
111 }
112 }
113}
114
115#undef SWAP_BUF_SIZE
116
126static void
127tls_wrap_control(struct tls_wrap_ctx *ctx, uint8_t header, struct buffer *buf,
128 const struct session_id *session_id)
129{
130 if (ctx->mode == TLS_WRAP_AUTH || ctx->mode == TLS_WRAP_NONE)
131 {
133 ASSERT(buf_write_prepend(buf, &header, sizeof(header)));
134 }
135 if (ctx->mode == TLS_WRAP_AUTH)
136 {
137 struct buffer null = clear_buf();
138
139 /* no encryption, only write hmac */
140 openvpn_encrypt(buf, null, &ctx->opt);
141 ASSERT(swap_hmac(buf, &ctx->opt, false));
142 }
143 else if (ctx->mode == TLS_WRAP_CRYPT)
144 {
145 ASSERT(buf_init(&ctx->work, buf->offset));
146 ASSERT(buf_write(&ctx->work, &header, sizeof(header)));
148 if (!tls_crypt_wrap(buf, &ctx->work, &ctx->opt))
149 {
150 buf->len = 0;
151 return;
152 }
153
155 || (header >> P_OPCODE_SHIFT) == P_CONTROL_WKC_V1)
156 {
157 if (!ctx->tls_crypt_v2_wkc || !buf_copy(&ctx->work, ctx->tls_crypt_v2_wkc))
158 {
159 msg(D_TLS_ERRORS, "Could not append tls-crypt-v2 client key");
160 buf->len = 0;
161 return;
162 }
163 }
164
165 /* Don't change the original data in buf, it's used by the reliability
166 * layer to resend on failure. */
167 *buf = ctx->work;
168 }
169}
170
171void
172write_control_auth(struct tls_session *session, struct key_state *ks, struct buffer *buf,
173 struct link_socket_actual **to_link_addr, int opcode, int max_ack,
174 bool prepend_ack)
175{
176 ASSERT(ks->key_id >= 0 && ks->key_id <= P_KEY_ID_MASK);
177 /* this writes the message id and ACK array, which only these opcodes have */
179 uint8_t header = (uint8_t)(ks->key_id | (opcode << P_OPCODE_SHIFT));
180
181 /* Workaround for Softether servers. Softether has a bug that it only
182 * allows 4 ACks in packets and drops packets if more ACKs are contained
183 * in a packet (see commit 37aa1ba5 in Softether) */
184 if (session->tls_wrap.mode == TLS_WRAP_NONE && !session->opt->server
185 && !(session->opt->crypto_flags & CO_USE_TLS_KEY_MATERIAL_EXPORT))
186 {
187 max_ack = min_int(max_ack, 4);
188 }
189
191 ASSERT(reliable_ack_write(ks->rec_ack, ks->lru_acks, buf, &ks->session_id_remote, max_ack,
192 prepend_ack));
193
194 msg(D_TLS_DEBUG, "%s(): %s", __func__, packet_opcode_name(opcode));
195
197 &session->session_id);
198
199 *to_link_addr = &ks->remote_addr;
200}
201
202bool
203read_control_auth(struct buffer *buf, struct tls_wrap_ctx *ctx,
204 const struct link_socket_actual *from, const struct tls_options *opt)
205{
206 struct gc_arena gc = gc_new();
207 bool ret = false;
208
209 const uint8_t opcode = *(BPTR(buf)) >> P_OPCODE_SHIFT;
210 if ((opcode == P_CONTROL_HARD_RESET_CLIENT_V3 || opcode == P_CONTROL_WKC_V1)
211 && !tls_crypt_v2_extract_client_key(buf, ctx, opt))
212 {
213 msg(D_TLS_ERRORS, "TLS Error: can not extract tls-crypt-v2 client key from %s",
215 goto cleanup;
216 }
217
218 if (ctx->mode == TLS_WRAP_AUTH)
219 {
220 struct buffer null = clear_buf();
221
222 /* move the hmac record to the front of the packet */
223 if (!swap_hmac(buf, &ctx->opt, true))
224 {
225 msg(D_TLS_ERRORS, "TLS Error: cannot locate HMAC in incoming packet from %s",
227 gc_free(&gc);
228 return false;
229 }
230
231 /* authenticate only (no decrypt) and remove the hmac record
232 * from the head of the buffer */
233 openvpn_decrypt(buf, null, &ctx->opt, NULL, BPTR(buf));
234 if (!buf->len)
235 {
236 msg(D_TLS_ERRORS, "TLS Error: incoming packet authentication failed from %s",
238 goto cleanup;
239 }
240 }
241 else if (ctx->mode == TLS_WRAP_CRYPT)
242 {
244 if (!tls_crypt_unwrap(buf, &tmp, &ctx->opt))
245 {
246 msg(D_TLS_ERRORS, "TLS Error: tls-crypt unwrapping failed from %s",
248 goto cleanup;
249 }
250 ASSERT(buf_init(buf, buf->offset));
251 ASSERT(buf_copy(buf, &tmp));
252 buf_clear(&tmp);
253 }
254 else if (ctx->tls_crypt_v2_server_key.cipher)
255 {
256 /* If tls-crypt-v2 is enabled, require *some* wrapping */
257 msg(D_TLS_ERRORS, "TLS Error: could not determine wrapping from %s",
259 /* TODO Do we want to support using tls-crypt-v2 and no control channel
260 * wrapping at all simultaneously? That would allow server admins to
261 * upgrade clients one-by-one without running a second instance, but we
262 * should not enable it by default because it breaks DoS-protection.
263 * So, add something like --tls-crypt-v2-allow-insecure-fallback ? */
264 goto cleanup;
265 }
266
267 if (ctx->mode == TLS_WRAP_NONE || ctx->mode == TLS_WRAP_AUTH)
268 {
269 /* advance buffer pointer past opcode & session_id since our caller
270 * already read it */
271 buf_advance(buf, SID_SIZE + 1);
272 }
273
274 ret = true;
275cleanup:
276 gc_free(&gc);
277 return ret;
278}
279
280void
290
291/*
292 * This function is similar to tls_pre_decrypt, except it is called
293 * when we are in server mode and receive an initial incoming
294 * packet. Note that we don't modify any state in our parameter
295 * objects except state. The purpose is solely to
296 * determine whether we should generate a client instance
297 * object, in which case true is returned.
298 *
299 * This function is essentially the first-line HMAC firewall
300 * on the UDP port listener in --mode server mode.
301 */
304 const struct link_socket_actual *from, const struct buffer *buf)
305{
306 struct gc_arena gc = gc_new();
307 /* A packet needs to have at least an opcode and session id */
308 if (BLENZ(buf) < 1 + SID_SIZE)
309 {
310 dmsg(D_TLS_STATE_ERRORS, "TLS State Error: Too short packet (length %d) received from %s",
311 buf->len, print_link_socket_actual(from, &gc));
312 goto error;
313 }
314
315 /* get opcode and key ID */
316 uint8_t pkt_firstbyte = *CBPTR(buf);
317 int op = pkt_firstbyte >> P_OPCODE_SHIFT;
318 int key_id = pkt_firstbyte & P_KEY_ID_MASK;
319
320 /* this packet is from an as-yet untrusted source, so
321 * scrutinize carefully */
322
323 /* Allow only the reset packet or the first packet of the actual handshake. */
325 && op != P_CONTROL_V1 && op != P_CONTROL_WKC_V1 && op != P_ACK_V1)
326 {
327 /*
328 * This can occur due to bogus data or DoS packets.
329 */
330 dmsg(D_TLS_STATE_ERRORS, "TLS State Error: No TLS state for client %s, opcode=%d",
331 print_link_socket_actual(from, &gc), op);
332 goto error;
333 }
334
335 if (key_id != 0)
336 {
338 "TLS State Error: Unknown key ID (%d) received from %s -- 0 was expected", key_id,
340 goto error;
341 }
342
343 /* read peer session id, we do this at this point since
344 * read_control_auth will skip over it */
345 struct buffer tmp = *buf;
346 buf_advance(&tmp, 1);
347 if (!session_id_read(&state->peer_session_id, &tmp)
349 {
350 msg(D_TLS_ERRORS, "TLS Error: session-id not found in packet from %s",
352 goto error;
353 }
354
355 state->newbuf = clone_buf(buf);
356 state->tls_wrap_tmp = tas->tls_wrap;
357
358 /* HMAC test and unwrapping the encrypted part of the control message
359 * into newbuf or just setting newbuf to point to the start of control
360 * message */
361 bool status = read_control_auth(&state->newbuf, &state->tls_wrap_tmp, from, NULL);
362
363 if (!status)
364 {
365 goto error;
366 }
367
368 /*
369 * At this point, if --tls-auth is being used, we know that
370 * the packet has passed the HMAC test, but we don't know if
371 * it is a replay yet. We will attempt to defeat replays
372 * by not advancing to the S_START state until we
373 * receive an ACK from our first reply to the client
374 * that includes an HMAC of our randomly generated 64 bit
375 * session ID.
376 *
377 * On the other hand if --tls-auth is not being used, we
378 * will proceed to begin the TLS authentication
379 * handshake with only cursory integrity checks having
380 * been performed, since we will be leaving the task
381 * of authentication solely up to TLS.
382 */
383 gc_free(&gc);
384 if (op == P_CONTROL_V1)
385 {
387 }
388 else if (op == P_ACK_V1)
389 {
391 }
392 else if (op == P_CONTROL_HARD_RESET_CLIENT_V3)
393 {
395 }
396 else if (op == P_CONTROL_WKC_V1)
397 {
399 }
400 else
401 {
403 }
404
405error:
407 gc_free(&gc);
408 return VERDICT_INVALID;
409}
410
411
412struct buffer
414 const struct session_id *own_sid, const struct session_id *remote_sid,
415 uint8_t header, bool request_resend_wkc)
416{
417 /* Copy buffer here to point at the same data but allow tls_wrap_control
418 * to potentially change buf to point to another buffer without
419 * modifying the buffer in tas */
420 struct buffer buf = tas->workbuf;
421 ASSERT(buf_init(&buf, tas->frame.buf.headroom));
422
423 /* Reliable ACK structure */
424 /* Length of the ACK structure - 1 ACK */
425 buf_write_u8(&buf, 1);
426
427 /* ACKed packet - first packet's id is always 0 */
428 buf_write_u32(&buf, 0);
429
430 /* Remote session id */
431 buf_write(&buf, remote_sid->id, SID_SIZE);
432
433 /* Packet ID of our own packet: Our reset packet is always using
434 * packet id 0 since it is the first packet */
435 packet_id_type net_pid = htonpid(0);
436
437 ASSERT(buf_write(&buf, &net_pid, sizeof(net_pid)));
438
439 /* Add indication for tls-crypt-v2 to resend the WKc with the reply */
440 if (request_resend_wkc)
441 {
444 }
445
446 /* Add tls-auth/tls-crypt wrapping, this might replace buf with
447 * ctx->work */
448 tls_wrap_control(ctx, header, &buf, own_sid);
449
450 return buf;
451}
452
453struct session_id
454calculate_session_id_hmac(struct session_id client_sid, const struct openvpn_sockaddr *from,
455 const uint8_t *key, int handwindow, int offset)
456{
457 /* Get the valid time quantisation for our hmac,
458 * we divide time by handwindow/2 and allow the previous
459 * and future session time if specified by offset */
460 uint32_t session_id_time = ntohl((uint32_t)(now / ((handwindow + 1) / 2) + offset));
461
462 uint8_t input[64];
463
464 /* ensure input array is large enough */
465 static_assert(sizeof(input) >= sizeof(struct sockaddr_in6) + sizeof(session_id_time) + sizeof(client_sid.id), "input buffer not sized correctly");
466 static_assert(sizeof(input) >= sizeof(struct sockaddr_in) + sizeof(session_id_time) + sizeof(client_sid.id), "input buffer not sized correctly");
467
468 struct buffer in = { 0 };
469 buf_set_write(&in, input, sizeof(input));
470
471 /* We do not care about endian here since it does not need to be
472 * portable */
473 buf_write(&in, (const uint8_t *)&session_id_time, sizeof(session_id_time));
474
475 /* add client IP and port */
476 switch (from->addr.sa.sa_family)
477 {
478 case AF_INET:
479 buf_write(&in, (const uint8_t *)&from->addr.in4, sizeof(struct sockaddr_in));
480 break;
481
482 case AF_INET6:
483 buf_write(&in, (const uint8_t *)&from->addr.in6, sizeof(struct sockaddr_in6));
484 break;
485 }
486
487 /* add session id of client */
488 buf_write(&in, client_sid.id, SID_SIZE);
489
490 struct session_id sid;
491 siphash(buf_bptr(&in), buf_len(&in), key, sid.id, sizeof(sid.id));
492
493 return sid;
494}
495
496bool
498 const struct openvpn_sockaddr *from,
499 const uint8_t *key,
500 int handwindow,
501 bool pkt_is_ack)
502{
503 if (!from)
504 {
505 return false;
506 }
507
508 struct buffer buf = state->newbuf;
509 struct reliable_ack ack;
510
511 if (!reliable_ack_parse(&buf, &ack, &state->server_session_id))
512 {
513 return false;
514 }
515
516 /* Check if the packet ID of the packet or ACKED packet is <= 1 */
517 for (int i = 0; i < ack.len; i++)
518 {
519 /* This packet ACKs a packet that has a higher packet id than the
520 * ones expected in the three-way handshake, consider it as invalid
521 * for the session */
522 if (ack.packet_id[i] > 1)
523 {
524 return false;
525 }
526 }
527
528 if (!pkt_is_ack)
529 {
530 packet_id_type message_id;
531 /* Extract the packet ID from the packet */
532 if (!reliable_ack_read_packet_id(&buf, &message_id))
533 {
534 return false;
535 }
536
537 /* similar check. Anything larger than 1 is not considered part of the
538 * three-way handshake */
539 if (message_id > 1)
540 {
541 return false;
542 }
543 }
544
545
546 /* check adjacent timestamps too, the handwindow is split in 2 for the
547 * offset, so we check the current timeslot and the two before that */
548 for (int offset = -2; offset <= 0; offset++)
549 {
550 struct session_id expected_id =
551 calculate_session_id_hmac(state->peer_session_id, from, key, handwindow, offset);
552
553 if (memcmp_constant_time(&expected_id, &state->server_session_id, SID_SIZE) == 0)
554 {
555 return true;
556 }
557 }
558 return false;
559}
560
561struct buffer
563{
564 /* commands on the control channel are seperated by 0x00 bytes.
565 * cmdlen does not include the 0 byte of the string */
566 int cmdlen = (int)strnlen(BSTR(buf), BLENZ(buf));
567
568 if (cmdlen >= BLEN(buf))
569 {
570 buf_advance(buf, cmdlen);
571 /* Return empty buffer */
572 struct buffer empty = { 0 };
573 return empty;
574 }
575
576 /* include the NUL byte and ensure NUL termination */
577 cmdlen += 1;
578
579 /* Construct a buffer that only holds the current command and
580 * its closing NUL byte */
581 struct buffer cmdbuf = alloc_buf_gc(cmdlen, gc);
582 buf_write(&cmdbuf, BPTR(buf), cmdlen);
583
584 /* Remove \r and \n at the end of the buffer to avoid
585 * problems with scripts and other that add extra \r and \n */
586 buf_chomp(&cmdbuf);
587
588 /* check we have only printable characters or null byte in the
589 * command string and no newlines */
590 if (!string_check_buf(&cmdbuf, CC_PRINT | CC_NULL, CC_CRLF))
591 {
592 msg(D_PUSH_ERRORS, "WARNING: Received control with invalid characters: %s",
593 format_hex(BPTR(&cmdbuf), BLEN(&cmdbuf), 256, gc));
594 cmdbuf.len = 0;
595 }
596
597 buf_advance(buf, cmdlen);
598 return cmdbuf;
599}
600
601#if defined(__GNUC__) || defined(__clang__)
602#pragma GCC diagnostic pop
603#endif
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
Definition buffer.c:174
void buf_clear(struct buffer *buf)
Zeroise and reset a buffer.
Definition buffer.c:153
struct buffer clone_buf(const struct buffer *buf)
Duplicate a buffer, including its content.
Definition buffer.c:104
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
void buf_chomp(struct buffer *buf)
Remove trailing newline and carriage-return characters from a buffer.
Definition buffer.c:530
bool string_check_buf(const struct buffer *buf, const unsigned int inclusive, const unsigned int exclusive)
Check a buffer if it only consists of allowed characters.
Definition buffer.c:1055
static uint8_t * buf_bptr(struct buffer *buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:449
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
static struct buffer clear_buf(void)
Return an empty, undefined struct buffer (all fields zero).
Definition buffer.h:384
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Copy the content of one buffer to the end of another.
Definition buffer.h:1347
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
static bool buf_write_u32(struct buffer *dest, uint32_t data)
Append a uint32_t to a buffer in network byte order.
Definition buffer.h:1317
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Prepend data to a buffer.
Definition buffer.h:1268
static size_t 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...
Definition buffer.h:1067
#define CC_CRLF
carriage return or newline
Definition buffer.h:1723
static int buf_len(const struct buffer *buf)
Return the length of the buffer content.
Definition buffer.h:462
#define CBPTR(buf)
Return a const pointer to the start of the buffer content.
Definition buffer.h:141
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:748
static bool buf_advance(struct buffer *buf, ssize_t size)
Advance the content start of a buffer, consuming bytes from the front.
Definition buffer.h:1165
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1239
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
Definition buffer.h:1288
static void buf_set_write(struct buffer *buf, uint8_t *data, size_t size)
Initialise a buffer with an externally provided writable memory region.
Definition buffer.h:658
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
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.
Definition buffer.h:960
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:153
#define CC_NULL
null character \0
Definition buffer.h:1687
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
#define CC_PRINT
printable (>= 32, != 127)
Definition buffer.h:1694
#define buf_init(buf, offset)
Definition buffer.h:364
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
bool ctrl_msg_tlv_write_u16(struct buffer *buf, uint16_t type, bool optional, uint16_t value)
Write a complete TLV whose value is a single 16-bit integer to buf.
Definition control_msg.c:53
TLV framing shared by the TLV-based control messages of the wire protocol.
#define TLV_TYPE_EARLY_NEG_FLAGS
Definition control_msg.h:43
void free_key_ctx_bi(struct key_ctx_bi *ctx)
Definition crypto.c:1098
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...
Definition crypto.h:347
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
Definition errlevel.h:130
#define D_PUSH_ERRORS
Definition errlevel.h:66
#define D_TLS_ERRORS
Definition errlevel.h:58
#define D_TLS_DEBUG
Definition errlevel.h:160
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 ...
Definition ssl_pkt.c:303
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...
Definition crypto.c:333
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.
Definition crypto.c:776
bool reliable_ack_read_packet_id(struct buffer *buf, packet_id_type *pid)
Read the packet ID of a received packet.
Definition reliable.c:76
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.
Definition reliable.c:133
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.
Definition reliable.c:215
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...
Definition tls_crypt.c:617
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).
Definition tls_crypt.c:212
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).
Definition tls_crypt.c:139
static int min_int(int x, int y)
Definition integer.h:105
static SERVICE_STATUS status
Definition interactive.c:52
#define dmsg(flags,...)
Definition error.h:164
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
time_t now
Definition otime.c:33
#define htonpid(x)
Definition packet_id.h:61
uint32_t packet_id_type
Definition packet_id.h:45
static unsigned int packet_id_size(bool long_form)
Definition packet_id.h:253
Reliability Layer module header file.
static bool session_id_write_prepend(const struct session_id *sid, struct buffer *buf)
Definition session_id.h:65
static bool session_id_write(const struct session_id *sid, struct buffer *buf)
Definition session_id.h:71
static bool session_id_defined(const struct session_id *sid1)
Definition session_id.h:53
static bool session_id_read(struct session_id *sid, struct buffer *buf)
Definition session_id.h:59
#define SID_SIZE
Definition session_id.h:44
static void siphash(const void *in, size_t inlen, const void *k, uint8_t *out, size_t outlen)
Definition siphash.h:70
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_hmac_and_pkt_id(struct tls_pre_decrypt_state *state, const struct openvpn_sockaddr *from, const uint8_t *key, int handwindow, bool pkt_is_ack)
Checks if a control packet has a correct HMAC server session id.
Definition ssl_pkt.c:497
struct buffer tls_reset_standalone(struct tls_wrap_ctx *ctx, const struct tls_auth_standalone *tas, const struct session_id *own_sid, const 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.
Definition ssl_pkt.c:413
void free_tls_pre_decrypt_state(struct tls_pre_decrypt_state *state)
Definition ssl_pkt.c:281
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)
Definition ssl_pkt.c:172
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...
Definition ssl_pkt.c:67
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...
Definition ssl_pkt.c:562
#define SWAP_BUF_SIZE
Definition ssl_pkt.c:42
static void tls_wrap_control(struct tls_wrap_ctx *ctx, uint8_t header, struct buffer *buf, const struct session_id *session_id)
Wraps a TLS control packet by adding tls-auth HMAC or tls-crypt(-v2) encryption and opcode header inc...
Definition ssl_pkt.c:127
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.
Definition ssl_pkt.c:203
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.
Definition ssl_pkt.c:454
SSL control channel wrap/unwrap and decode functions.
#define EARLY_NEG_FLAG_RESEND_WKC
Definition ssl_pkt.h:357
#define P_OPCODE_SHIFT
Definition ssl_pkt.h:39
static bool opcode_is_reliable_control(int op)
Is a packet with opcode op carried by the reliable control channel?
Definition ssl_pkt.h:88
#define P_ACK_V1
Definition ssl_pkt.h:46
#define P_CONTROL_WKC_V1
Definition ssl_pkt.h:59
#define P_KEY_ID_MASK
Definition ssl_pkt.h:38
static const char * packet_opcode_name(int op)
Definition ssl_pkt.h:271
#define P_CONTROL_V1
Definition ssl_pkt.h:45
first_packet_verdict
Definition ssl_pkt.h:124
@ VERDICT_VALID_ACK_V1
This packet is a valid ACK control packet from the peer, i.e.
Definition ssl_pkt.h:133
@ VERDICT_VALID_WKC_V1
The packet is a valid control packet with appended wrapped client key.
Definition ssl_pkt.h:135
@ VERDICT_VALID_RESET_V2
This packet is a valid reset packet from the peer (all but tls-crypt-v2).
Definition ssl_pkt.h:126
@ VERDICT_INVALID
the packet failed on of the various checks
Definition ssl_pkt.h:137
@ VERDICT_VALID_RESET_V3
This is a valid v3 reset (tls-crypt-v2).
Definition ssl_pkt.h:128
@ VERDICT_VALID_CONTROL_V1
This packet is a valid control packet from the peer.
Definition ssl_pkt.h:130
#define P_CONTROL_HARD_RESET_CLIENT_V2
Definition ssl_pkt.h:51
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...
Definition ssl_pkt.h:328
#define P_CONTROL_HARD_RESET_CLIENT_V3
Definition ssl_pkt.h:55
SSL utility functions.
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
int offset
Offset in bytes of the actual content within the allocated memory.
Definition buffer.h:74
Security parameter state for processing data channel packets.
Definition crypto.h:286
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
Definition crypto.h:287
struct frame::@074234134026241341315172337061247271261307273127 buf
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition mtu.h:124
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
struct key_ctx decrypt
cipher and/or HMAC contexts for receiving direction.
Definition crypto.h:276
struct key_ctx encrypt
Cipher and/or HMAC contexts for sending direction.
Definition crypto.h:274
Container for one set of cipher and/or HMAC contexts.
Definition crypto.h:195
cipher_ctx_t * cipher
Generic cipher context.
Definition crypto.h:196
hmac_ctx_t * hmac
Generic HMAC context.
Definition crypto.h:197
Security parameter state of one TLS and data channel key session.
Definition ssl_common.h:208
struct link_socket_actual remote_addr
Definition ssl_common.h:235
struct reliable_ack * rec_ack
Definition ssl_common.h:247
struct session_id session_id_remote
Definition ssl_common.h:234
int key_id
Key id for this key_state, inherited from struct tls_session.
Definition ssl_common.h:217
struct reliable_ack * lru_acks
Definition ssl_common.h:248
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:145
The acknowledgment structure in which packet IDs are stored for later acknowledgment.
Definition reliable.h:75
packet_id_type packet_id[RELIABLE_ACK_SIZE]
Definition reliable.h:77
uint8_t id[8]
Definition session_id.h:39
struct frame frame
Definition ssl_pkt.h:120
struct buffer workbuf
Definition ssl_pkt.h:119
struct tls_wrap_ctx tls_wrap
Definition ssl_pkt.h:118
struct that stores the temporary data for the tls lite decrypt functions
Definition ssl_pkt.h:145
struct session_id peer_session_id
Definition ssl_pkt.h:148
struct session_id server_session_id
Definition ssl_pkt.h:149
struct buffer newbuf
Definition ssl_pkt.h:147
struct tls_wrap_ctx tls_wrap_tmp
Definition ssl_pkt.h:146
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:496
Control channel wrapping (–tls-auth/–tls-crypt) context.
Definition ssl_common.h:275
enum tls_wrap_ctx::@317033145106102005365073042374303036003032271350 mode
Control channel wrapping mode.
bool cleanup_key_ctx
opt.key_ctx_bi is owned by this context
Definition ssl_common.h:287
struct crypto_options opt
Crypto state.
Definition ssl_common.h:282
struct buffer work
Work buffer (only for –tls-crypt).
Definition ssl_common.h:283
struct key_ctx tls_crypt_v2_server_key
Decrypts client keys.
Definition ssl_common.h:284
const struct buffer * tls_crypt_v2_wkc
Wrapped client key, sent to server.
Definition ssl_common.h:285
struct key2 original_wrap_keydata
original key data to be xored in to the key for dynamic tls-crypt.
Definition ssl_common.h:297
static int cleanup(void **state)
struct gc_arena gc
Definition test_ssl.c:138