OpenVPN
ssl_pkt.h
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-2024 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, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
31#ifndef SSL_PKT_H
32#define SSL_PKT_H
33
34#include "buffer.h"
35#include "ssl_backend.h"
36#include "ssl_common.h"
37
38/* packet opcode (high 5 bits) and key-id (low 3 bits) are combined in one byte */
39#define P_KEY_ID_MASK 0x07
40#define P_OPCODE_SHIFT 3
41
42/* packet opcodes -- the V1 is intended to allow protocol changes in the future */
43#define P_CONTROL_HARD_RESET_CLIENT_V1 1 /* initial key from client, forget previous state */
44#define P_CONTROL_HARD_RESET_SERVER_V1 2 /* initial key from server, forget previous state */
45#define P_CONTROL_SOFT_RESET_V1 3 /* new key, graceful transition from old to new key */
46#define P_CONTROL_V1 4 /* control channel packet (usually TLS ciphertext) */
47#define P_ACK_V1 5 /* acknowledgement for packets received */
48#define P_DATA_V1 6 /* data channel packet */
49#define P_DATA_V2 9 /* data channel packet with peer-id */
50
51/* indicates key_method >= 2 */
52#define P_CONTROL_HARD_RESET_CLIENT_V2 7 /* initial key from client, forget previous state */
53#define P_CONTROL_HARD_RESET_SERVER_V2 8 /* initial key from server, forget previous state */
54
55/* indicates key_method >= 2 and client-specific tls-crypt key */
56#define P_CONTROL_HARD_RESET_CLIENT_V3 10 /* initial key from client, forget previous state */
57
58/* Variant of P_CONTROL_V1 but with appended wrapped key
59 * like P_CONTROL_HARD_RESET_CLIENT_V3 */
60#define P_CONTROL_WKC_V1 11
61
62/* define the range of legal opcodes
63 * Since we do no longer support key-method 1 we consider
64 * the v1 op codes invalid */
65#define P_FIRST_OPCODE 3
66#define P_LAST_OPCODE 11
67
68/*
69 * Define number of buffers for send and receive in the reliability layer.
70 */
71#define TLS_RELIABLE_N_SEND_BUFFERS 6 /* also window size for reliability layer */
72#define TLS_RELIABLE_N_REC_BUFFERS 12
73
74/*
75 * Used in --mode server mode to check tls-auth signature on initial
76 * packets received from new clients.
77 */
84
100
111
113
150 struct tls_pre_decrypt_state *state,
151 const struct link_socket_actual *from,
152 const struct buffer *buf);
153
154/* Creates an SHA256 HMAC context with a random key that is used for the
155 * session id.
156 *
157 * We do not support loading this from a config file since continuing session
158 * between restarts of OpenVPN has never been supported and that includes
159 * early session setup.
160 */
162
174struct session_id
175calculate_session_id_hmac(struct session_id client_sid,
176 const struct openvpn_sockaddr *from,
177 hmac_ctx_t *hmac,
178 int handwindow, int offset);
179
189bool
191 const struct openvpn_sockaddr *from,
192 hmac_ctx_t *hmac,
193 int handwindow);
194
195/*
196 * Write a control channel authentication record.
197 */
198void
200 struct key_state *ks,
201 struct buffer *buf,
202 struct link_socket_actual **to_link_addr,
203 int opcode,
204 int max_ack,
205 bool prepend_ack);
206
207
208
218bool
219read_control_auth(struct buffer *buf,
220 struct tls_wrap_ctx *ctx,
221 const struct link_socket_actual *from,
222 const struct tls_options *opt,
223 bool initial_packet);
224
225
231struct buffer
233 struct tls_auth_standalone *tas,
234 struct session_id *own_sid,
235 struct session_id *remote_sid,
236 uint8_t header,
237 bool request_resend_wkc);
238
239
250struct buffer
252
253static inline const char *
255{
256 switch (op)
257 {
259 return "P_CONTROL_HARD_RESET_CLIENT_V1";
260
262 return "P_CONTROL_HARD_RESET_SERVER_V1";
263
265 return "P_CONTROL_HARD_RESET_CLIENT_V2";
266
268 return "P_CONTROL_HARD_RESET_SERVER_V2";
269
271 return "P_CONTROL_HARD_RESET_CLIENT_V3";
272
274 return "P_CONTROL_SOFT_RESET_V1";
275
276 case P_CONTROL_V1:
277 return "P_CONTROL_V1";
278
279 case P_CONTROL_WKC_V1:
280 return "P_CONTROL_WKC_V1";
281
282 case P_ACK_V1:
283 return "P_ACK_V1";
284
285 case P_DATA_V1:
286 return "P_DATA_V1";
287
288 case P_DATA_V2:
289 return "P_DATA_V2";
290
291 default:
292 return "P_???";
293 }
294}
295
304static inline struct tls_wrap_ctx *
306{
307 /* OpenVPN has the hardcoded assumption in its protocol that
308 * key-id 0 is always first session and renegotiations use key-id
309 * 1 to 7 and wrap around to 1 after that. So key-id > 0 is equivalent
310 * to "this is a renegotiation"
311 */
312 if (key_id > 0 && session->tls_wrap_reneg.mode == TLS_WRAP_CRYPT)
313 {
314 return &session->tls_wrap_reneg;
315 }
316 else
317 {
318 return &session->tls_wrap;
319 }
320}
321
322/* initial packet id (instead of 0) that indicates that the peer supports
323 * early protocol negotiation. This will make the packet id turn a bit faster
324 * but the network time part of the packet id takes care of that. And
325 * this is also a rather theoretical scenario as it still needs more than
326 * 2^31 control channel packets to happen */
327#define EARLY_NEG_MASK 0xff000000
328#define EARLY_NEG_START 0x0f000000
329
330
331/* Early negotiation that part of the server response in the RESET_V2 packet.
332 * Since clients that announce early negotiation support will treat the payload
333 * of reset packets special and parse it as TLV messages.
334 * as TLV (type, length, value) */
335#define TLV_TYPE_EARLY_NEG_FLAGS 0x0001
336#define EARLY_NEG_FLAG_RESEND_WKC 0x0001
337#endif /* ifndef SSL_PKT_H */
mbedtls_md_context_t hmac_ctx_t
Generic HMAC context.
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:308
Control Channel SSL library backend module.
Control Channel Common Data Structures.
#define P_DATA_V1
Definition ssl_pkt.h:48
#define P_DATA_V2
Definition ssl_pkt.h:49
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.
Definition ssl_pkt.c:530
#define P_ACK_V1
Definition ssl_pkt.h:47
#define P_CONTROL_WKC_V1
Definition ssl_pkt.h:60
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.
Definition ssl_pkt.c:488
void free_tls_pre_decrypt_state(struct tls_pre_decrypt_state *state)
Definition ssl_pkt.c:286
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:168
#define P_CONTROL_HARD_RESET_CLIENT_V1
Definition ssl_pkt.h:43
hmac_ctx_t * session_id_hmac_init(void)
Definition ssl_pkt.c:474
static const char * packet_opcode_name(int op)
Definition ssl_pkt.h:254
#define P_CONTROL_HARD_RESET_SERVER_V2
Definition ssl_pkt.h:53
#define P_CONTROL_SOFT_RESET_V1
Definition ssl_pkt.h:45
#define P_CONTROL_V1
Definition ssl_pkt.h:46
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:563
first_packet_verdict
Definition ssl_pkt.h:85
@ VERDICT_VALID_ACK_V1
This packet is a valid ACK control packet from the peer, i.e.
Definition ssl_pkt.h:94
@ VERDICT_VALID_WKC_V1
The packet is a valid control packet with appended wrapped client key.
Definition ssl_pkt.h:96
@ VERDICT_VALID_RESET_V2
This packet is a valid reset packet from the peer (all but tls-crypt-v2)
Definition ssl_pkt.h:87
@ VERDICT_INVALID
the packet failed on of the various checks
Definition ssl_pkt.h:98
@ VERDICT_VALID_RESET_V3
This is a valid v3 reset (tls-crypt-v2)
Definition ssl_pkt.h:89
@ VERDICT_VALID_CONTROL_V1
This packet is a valid control packet from the peer.
Definition ssl_pkt.h:91
bool read_control_auth(struct buffer *buf, struct tls_wrap_ctx *ctx, const struct link_socket_actual *from, const struct tls_options *opt, bool initial_packet)
Read a control channel authentication record.
Definition ssl_pkt.c:200
#define P_CONTROL_HARD_RESET_CLIENT_V2
Definition ssl_pkt.h:52
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.
Definition ssl_pkt.c:429
#define P_CONTROL_HARD_RESET_SERVER_V1
Definition ssl_pkt.h:44
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:305
#define P_CONTROL_HARD_RESET_CLIENT_V3
Definition ssl_pkt.h:56
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:66
Packet geometry parameters.
Definition mtu.h:98
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:117
Security parameter state of one TLS and data channel key session.
Definition ssl_common.h:203
struct buffer workbuf
Definition ssl_pkt.h:81
struct tls_wrap_ctx tls_wrap
Definition ssl_pkt.h:80
struct that stores the temporary data for the tls lite decrypt functions
Definition ssl_pkt.h:105
struct session_id peer_session_id
Definition ssl_pkt.h:108
struct session_id server_session_id
Definition ssl_pkt.h:109
struct buffer newbuf
Definition ssl_pkt.h:107
struct tls_wrap_ctx tls_wrap_tmp
Definition ssl_pkt.h:106
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:483
Control channel wrapping (–tls-auth/–tls-crypt) context.
Definition ssl_common.h:271
@ TLS_WRAP_CRYPT
Control channel encryption and authentication.
Definition ssl_common.h:275
struct gc_arena gc
Definition test_ssl.c:155