OpenVPN
test_pkt.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single 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) 2016-2026 Sentyron B.V. <openvpn@sentyron.com>
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
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <stdarg.h>
32#include <string.h>
33#include <setjmp.h>
34#include <cmocka.h>
35#include "test_common.h"
36
37#include "crypto.h"
38#include "options.h"
39#include "ssl_backend.h"
40#include "ssl_pkt.h"
41#include "tls_crypt.h"
42
43#include "mss.h"
44#include "reliable.h"
45#include "siphash.h"
46
47int
48parse_line(const char *line, char **p, const int n, const char *file, const int line_num,
49 msglvl_t msglevel, struct gc_arena *gc)
50{
51 /* Dummy function to get the linker happy, should never be called */
52 assert_true(false);
53 return 0;
54}
55
56/* Define this function here as dummy since including the ssl_*.c files
57 * leads to having to include even more unrelated code */
58bool
59key_state_export_keying_material(struct tls_session *session, const char *label, size_t label_size,
60 void *ekm, size_t ekm_size)
61{
62 ASSERT(0);
63}
64
65const char *
67{
68 return "dummy print_link_socket_actual from unit test";
69}
70
71const char static_key[] = "<tls-auth>\n"
72 "-----BEGIN OpenVPN Static key V1-----\n"
73 "37268ea8f95d7f71f9fb8fc03770c460\n"
74 "daf714a483d815c013ce0a537efc18f2\n"
75 "8f4f172669d9e6a413bac6741d8ea054\n"
76 "00f49b7fd6326470f23798c606bf53d4\n"
77 "de63ebc64ec59d57ce5d04d5b62e68b5\n"
78 "3ca6e5354351097fa370446c4d269f18\n"
79 "7bb6ae54af2dc70ff7317fe2f8754b82\n"
80 "82aad4202f9fa42c8640245d883e2c54\n"
81 "a0c1c489a036cf3a8964d8d289c1583b\n"
82 "9447c262b1da5fd167a5d27bd5ac5143\n"
83 "17bc2343a31a2efc38dd920d910375f5\n"
84 "1c2e27f3afd36c49269da079f7ce466e\n"
85 "bb0f9ad13e9bbb4665974e6bc24b513c\n"
86 "5700393bf4a3e7f967e2f384069ac8a8\n"
87 "e78b18b15604993fd16515cce9c0f3e4\n"
88 "2b4126b999005ade802797b0eeb8b9e6\n"
89 "-----END OpenVPN Static key V1-----\n"
90 "</tls-auth>\n";
91
92const uint8_t client_reset_v2_none[] = { 0x38, 0x68, 0x91, 0x92, 0x3f, 0xa3, 0x10,
93 0x34, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00 };
94
95const uint8_t client_reset_v2_tls_auth[] = { 0x38, 0xde, 0x69, 0x4c, 0x5c, 0x7b, 0xfb, 0xa2, 0x74,
96 0x93, 0x53, 0x7c, 0x1d, 0xed, 0x4e, 0x78, 0x15, 0x29,
97 0xae, 0x7c, 0xfe, 0x4b, 0x8c, 0x6d, 0x6b, 0x2b, 0x51,
98 0xf0, 0x5a, 0x00, 0x00, 0x00, 0x01, 0x61, 0xd3, 0xbf,
99 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00 };
100
101const uint8_t client_reset_v2_tls_crypt[] = {
102 0x38, 0xf4, 0x19, 0xcb, 0x12, 0xd1, 0xf9, 0xe4, 0x8f, 0x00, 0x00, 0x00, 0x01, 0x61,
103 0xd3, 0xf8, 0xe1, 0x33, 0x02, 0x06, 0xf5, 0x68, 0x02, 0xbe, 0x44, 0xfb, 0xed, 0x90,
104 0x50, 0x64, 0xe3, 0xdb, 0x43, 0x41, 0x6b, 0xec, 0x5e, 0x52, 0x67, 0x19, 0x46, 0x2b,
105 0x7e, 0xb9, 0x0c, 0x96, 0xde, 0xfc, 0x9b, 0x05, 0xc4, 0x48, 0x79, 0xf7
106};
107
108/* Valid tls-auth client CONTROL_V1 packet with random server id */
110 0x20, 0x14, 0x01, 0x4e, 0xbc, 0x80, 0xc6, 0x14, 0x2b, 0x7b, 0xc8, 0x76, 0xfb, 0xc5, 0x2e, 0x27,
111 0xb1, 0xc5, 0x07, 0x35, 0x5b, 0xb6, 0x00, 0x6b, 0xae, 0x71, 0xba, 0x4e, 0x38, 0x00, 0x00, 0x00,
112 0x03, 0x61, 0xd3, 0xff, 0x53, 0x00, 0x00, 0x00, 0x00, 0x01, 0x16, 0x03, 0x01, 0x01, 0x0c, 0x01,
113 0x00, 0x01, 0x08, 0x03, 0x03, 0x8c, 0xaa, 0xac, 0x3a, 0x1a, 0x07, 0xbd, 0xe7, 0xb7, 0x50, 0x06,
114 0x9b, 0x94, 0x0c, 0x34, 0x4b, 0x5a, 0x35, 0xca, 0xc4, 0x79, 0xbd, 0xc9, 0x09, 0xb0, 0x7b, 0xd9,
115 0xee, 0xbb, 0x7d, 0xe7, 0x25, 0x20, 0x39, 0x38, 0xe2, 0x18, 0x33, 0x36, 0x14, 0x9f, 0x34, 0xf0,
116 0x44, 0x59, 0x96, 0x8d, 0x0e, 0xd2, 0x47, 0x76, 0x64, 0x88, 0x59, 0xe9, 0x38, 0x03, 0x97, 0x96,
117 0x98, 0x45, 0xfb, 0xf5, 0xff, 0x23, 0x00, 0x32, 0x13, 0x02, 0x13, 0x03, 0x13, 0x01, 0xc0, 0x2c,
118 0xc0, 0x30, 0x00, 0x9f, 0xcc, 0xa9, 0xcc, 0xa8, 0xcc, 0xaa, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
119 0xc0, 0x24, 0xc0, 0x28, 0x00, 0x6b, 0xc0, 0x23, 0xc0, 0x27, 0x00, 0x67, 0xc0, 0x0a, 0xc0, 0x14,
120 0x00, 0x39, 0xc0, 0x09, 0xc0, 0x13, 0x00, 0x33, 0x00, 0xff, 0x01, 0x00, 0x00, 0x8d, 0x00, 0x0b,
121 0x00, 0x04, 0x03, 0x00, 0x01, 0x02, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x1d, 0x00, 0x17,
122 0x00, 0x1e, 0x00, 0x19, 0x00, 0x18, 0x00, 0x16, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x0d,
123 0x00, 0x30, 0x00, 0x2e, 0x04, 0x03, 0x05, 0x03, 0x06, 0x03, 0x08, 0x07, 0x08, 0x08, 0x08, 0x09,
124 0x08, 0x0a, 0x08, 0x0b, 0x08, 0x04, 0x08, 0x05, 0x08, 0x06, 0x04, 0x01, 0x05, 0x01, 0x06, 0x01,
125 0x03, 0x03, 0x02, 0x03, 0x03, 0x01, 0x02, 0x01, 0x03, 0x02, 0x02, 0x02, 0x04, 0x02, 0x05, 0x02,
126 0x06, 0x02, 0x00, 0x2b, 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03, 0x00, 0x2d, 0x00, 0x02, 0x01,
127 0x01, 0x00, 0x33, 0x00, 0x26, 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0x0e, 0xc9, 0x7a, 0xff, 0x58,
128 0xdb, 0x56, 0xf6, 0x40, 0xd1, 0xed, 0xdb, 0x91, 0x81, 0xd6, 0xef, 0x83, 0x86, 0x8a, 0xb2, 0x3d,
129 0x88, 0x92, 0x3f, 0xd8, 0x51, 0x9c, 0xd6, 0x26, 0x56, 0x33, 0x6b
130};
131
132/* This is a truncated packet as we do not care for the TLS payload in the
133 * unit test */
134const uint8_t client_control_with_ack[] = { 0x20, 0x78, 0x19, 0xbf, 0x2e, 0xbc, 0xd1, 0x9a,
135 0x45, 0x01, 0x00, 0x00, 0x00, 0x00, 0xea, 0xfe,
136 0xbf, 0xa4, 0x41, 0x8a, 0xe3, 0x1b, 0x00, 0x00,
137 0x00, 0x01, 0x16, 0x03, 0x01 };
138
139const uint8_t client_ack_none_random_id[] = { 0x28, 0xae, 0xb9, 0xaf, 0xe1, 0xf0, 0x1d, 0x79,
140 0xc8, 0x01, 0x00, 0x00, 0x00, 0x00, 0xdd, 0x85,
141 0xdb, 0x53, 0x56, 0x23, 0xb0, 0x2e };
142
143/* no tls-auth, P_ACK_V1, acks 0,1, and 2 */
145 0x28,
146 0xae, 0xb9, 0xaf, 0xe1, 0xf0, 0x1d, 0x79, 0xc8,
147 0x03,
148 0x00, 0x00, 0x00, 0x00,
149 0x00, 0x00, 0x00, 0x01,
150 0x00, 0x00, 0x00, 0x02,
151 0xdd, 0x85, 0xdb, 0x53, 0x56, 0x23, 0xb0, 0x2e
152};
153
154/* no tls-auth, P_CONTROL_V1, acks 0, msg-id 2 */
156 0x20,
157 0xae, 0xb9, 0xaf, 0xe1, 0xf0, 0x1d, 0x79, 0xc8,
158 0x01,
159 0x00, 0x00, 0x00, 0x00,
160 0x02
161};
162
163
165init_tas_auth(int key_direction)
166{
167 struct tls_auth_standalone tas = { 0 };
168 struct frame frame = { .buf = { .headroom = 200, .payload_size = 1400 }, 0 };
169 tas.frame = frame;
170
171 tas.tls_wrap.mode = TLS_WRAP_AUTH;
172 /* we ignore packet ids on for the first packet check */
174
175 struct key_type tls_crypt_kt;
176 init_key_type(&tls_crypt_kt, "none", "SHA1", true, false);
177
179 key_direction, "Control Channel Authentication", "tls-auth", NULL);
180 tas.workbuf = alloc_buf(1600);
181
182 return tas;
183}
184
186init_tas_crypt(bool server)
187{
188 struct tls_auth_standalone tas = { 0 };
189 tas.tls_wrap.mode = TLS_WRAP_CRYPT;
191
193 static_key, true, server);
194 tas.workbuf = alloc_buf(1600);
195 tas.tls_wrap.work = alloc_buf(1600);
196
197 return tas;
198}
199
200void
202{
203 /* Not some of these might be null pointers but calling free on null
204 * pointers is a noop */
206 free_buf(&tas->workbuf);
207 free_buf(&tas->tls_wrap.work);
208}
209
210void
212{
213 struct link_socket_actual from = { 0 };
214 struct tls_pre_decrypt_state state = { 0 };
215
216 struct tls_auth_standalone tas = init_tas_crypt(true);
217 struct buffer buf = alloc_buf(1024);
218
219 /* tls-auth should be invalid */
221 enum first_packet_verdict verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
224
225 /* as well as the too short normal reset */
226 buf_reset_len(&buf);
228 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
231
232 /* the tls-crypt should validate */
233 buf_reset_len(&buf);
235 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
238
239 /* flip a byte in various places */
240 for (size_t i = 0; i < sizeof(client_reset_v2_tls_crypt); i++)
241 {
242 buf_reset_len(&buf);
244 (BPTR(&buf))[i] = 0x23;
245 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
248 }
249
250 free_key_ctx_bi(&tas.tls_wrap.opt.key_ctx_bi);
251 free_tas(&tas);
252 free_buf(&buf);
253}
254
255
256void
258{
259 struct link_socket_actual from = { 0 };
260 struct tls_auth_standalone tas = { 0 };
261 struct tls_pre_decrypt_state state = { 0 };
262 enum first_packet_verdict verdict;
263
264 struct buffer buf = alloc_buf(1024);
266
267 /* Packet to short to contain the hmac */
269
270 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
273
274 /* Valid tls-auth packet, should validate */
275 buf_reset_len(&buf);
277 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
280
281 /* The pre decrypt function should not modify the buffer, so calling it
282 * again should have the same result */
283 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
285
286 /* and buf memory should be equal */
289
290 buf_reset_len(&buf);
292 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
295
296 /* flip a byte in the hmac */
297 (BPTR(&buf))[20] = 0x23;
298 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
301
302 /* Wrong key direction gives a wrong hmac key and should not validate */
303 free_key_ctx_bi(&tas.tls_wrap.opt.key_ctx_bi);
304 free_tas(&tas);
306
307 buf_reset_len(&buf);
309 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
311
313 free_tas(&tas);
314 free_buf(&buf);
315}
316
317void
319{
320 struct link_socket_actual from = { 0 };
321 struct tls_auth_standalone tas = { 0 };
322 struct tls_pre_decrypt_state state = { 0 };
323
324 struct buffer buf = alloc_buf(1024);
326
327 tas.tls_wrap.mode = TLS_WRAP_NONE;
328
329 /* the method will not do additional test, so the tls-auth and tls-crypt
330 * reset will be accepted */
331 enum first_packet_verdict verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
334
335 buf_reset_len(&buf);
337 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
340
341 buf_reset_len(&buf);
343 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
346
347 /* This is not a reset packet and should trigger the other response */
348 buf_reset_len(&buf);
350 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
352
354 free_buf(&buf);
355 free_tas(&tas);
356}
357
358static void
360{
361 struct buffer buf = alloc_buf(1024);
363
364 /* skip over op code and peer session id */
365 buf_advance(&buf, 9);
366
367 struct reliable_ack ack;
368 struct session_id sid;
369 bool ret;
370
371 ret = reliable_ack_parse(&buf, &ack, &sid);
372 assert_true(ret);
373
374 assert_int_equal(ack.len, 1);
375 assert_int_equal(ack.packet_id[0], 0);
376
377 struct session_id expected_id = { .id = { 0xea, 0xfe, 0xbf, 0xa4, 0x41, 0x8a, 0xe3, 0x1b } };
378 assert_memory_equal(&sid, &expected_id, SID_SIZE);
379
380 buf_reset_len(&buf);
382
383 /* skip over op code and peer session id */
384 buf_advance(&buf, 9);
385 ret = reliable_ack_parse(&buf, &ack, &sid);
386 assert_true(ret);
387
388 assert_int_equal(ack.len, 1);
389 assert_int_equal(ack.packet_id[0], 0);
390
391 struct session_id expected_id2 = { .id = { 0xdd, 0x85, 0xdb, 0x53, 0x56, 0x23, 0xb0, 0x2e } };
392 assert_memory_equal(&sid, &expected_id2, SID_SIZE);
393
394 buf_reset_len(&buf);
396
397 /* skip over op code and peer session id */
398 buf_advance(&buf, 9);
399 ret = reliable_ack_parse(&buf, &ack, &sid);
400
401 free_buf(&buf);
402}
403
404static void
406{
407 uint8_t key[SIPHASH_KEY_SIZE] = { 0 };
409
410 struct link_socket_actual from = { 0 };
411 from.dest.addr.sa.sa_family = AF_INET;
412 from.dest.addr.in4.sin_addr.s_addr = ntohl(0x01020304);
413 struct tls_auth_standalone tas = { 0 };
414 struct tls_pre_decrypt_state state = { 0 };
415
416 struct buffer buf = alloc_buf(1024);
418
420
421 buf_reset_len(&buf);
423 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
425
426 /* This is a valid packet but containing a random id instead of an HMAC id*/
427 bool valid = check_session_hmac_and_pkt_id(&state, &from.dest, key, 30, false);
429
431 free_buf(&buf);
432 free_tas(&tas);
433}
434
435static void
437{
438 now = 1000;
441
442 struct link_socket_actual from = { 0 };
443 from.dest.addr.sa.sa_family = AF_INET;
444 from.dest.addr.in4.sin_addr.s_addr = ntohl(0x01020304);
445
446 struct tls_auth_standalone tas = { 0 };
447 struct tls_pre_decrypt_state state = { 0 };
448
449 struct buffer buf = alloc_buf(1024);
451
452 tas.tls_wrap.mode = TLS_WRAP_NONE;
453
454 buf_reset_len(&buf);
456 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
458
459 /* This packet has a random hmac, so it should fail to validate */
460 bool valid = check_session_hmac_and_pkt_id(&state, &from.dest, key, 30, true);
462
463 struct session_id client_id = { { 0xae, 0xb9, 0xaf, 0xe1, 0xf0, 0x1d, 0x79, 0xc8 } };
464 assert_memory_equal(&client_id, &state.peer_session_id, sizeof(struct session_id));
465
466 struct session_id expected_id = calculate_session_id_hmac(client_id, &from.dest, key, 30, 0);
467
469 buf_reset_len(&buf);
470
471 /* Write the packet again into the buffer but this time, replacing the peer packet
472 * id with the expected one */
474 buf_write(&buf, expected_id.id, 8);
475
476 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
477 assert_int_equal(verdict, VERDICT_VALID_ACK_V1);
478 valid = check_session_hmac_and_pkt_id(&state, &from.dest, key, 30, true);
479
480 assert_true(valid);
481
482 /* Our handwindow is 30 so the slices are half of that, so they are
483 * (975,990), (990, 1005), (1005, 1020), (1020, 1035), (1035, 1050)
484 * So setting time to the two future ones should work
485 */
486 now = 980;
487 assert_false(check_session_hmac_and_pkt_id(&state, &from.dest, key, 30, true));
488 now = 1040;
489 assert_false(check_session_hmac_and_pkt_id(&state, &from.dest, key, 30, true));
490 now = 1002;
491 assert_true(check_session_hmac_and_pkt_id(&state, &from.dest, key, 30, true));
492 now = 1022;
493 assert_true(check_session_hmac_and_pkt_id(&state, &from.dest, key, 30, true));
494 now = 1010;
495 assert_true(check_session_hmac_and_pkt_id(&state, &from.dest, key, 30, true));
496
497 /* Changing the IP address should make this invalid */
498 from.dest.addr.in4.sin_addr.s_addr = ntohl(0x01020305);
499 assert_false(check_session_hmac_and_pkt_id(&state, &from.dest, key, 30, true));
500
501 /* Change to the correct one again */
502 from.dest.addr.in4.sin_addr.s_addr = ntohl(0x01020304);
503 assert_true(check_session_hmac_and_pkt_id(&state, &from.dest, key, 30, true));
504
505 /* Modify the peer id, should now fail hmac verification */
506 buf_inc_len(&buf, -4);
507 buf_write_u32(&buf, 0x12345678);
508
510 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
511 assert_int_equal(verdict, VERDICT_VALID_ACK_V1);
512 assert_false(check_session_hmac_and_pkt_id(&state, &from.dest, key, 30, true));
513
515 free_buf(&buf);
516}
517
518static void
520{
521 uint8_t key[SIPHASH_KEY_SIZE] = { 0 };
523
524 struct link_socket_actual from = { 0 };
525 from.dest.addr.sa.sa_family = AF_INET;
526
527 struct tls_auth_standalone tas = { 0 };
528 struct tls_pre_decrypt_state state = { 0 };
529
530 struct buffer buf = alloc_buf(1024);
532
533 tas.tls_wrap.mode = TLS_WRAP_NONE;
534
535 buf_reset_len(&buf);
537
538
539 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
541
542 /* should fail because it acks 2 */
543 bool valid = check_session_hmac_and_pkt_id(&state, &from.dest, key, 30, true);
546
547 /* Try test with the control with a too high message id now */
548 buf_reset_len(&buf);
550
551 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
553
554 /* should fail because it has message id 2 */
555 valid = check_session_hmac_and_pkt_id(&state, &from.dest, key, 30, true);
557
559 free_buf(&buf);
560}
561
562static void
564{
565 const uint8_t key[SIPHASH_KEY_SIZE] = { 1, 2, 3, 0 };
566 static const int handwindow = 100;
567
568 struct openvpn_sockaddr addr = { 0 };
569
570 addr.addr.in4.sin_family = AF_INET;
571 addr.addr.in4.sin_addr.s_addr = ntohl(0xff000ff);
572 addr.addr.in4.sin_port = ntohs(1195);
573
574 struct session_id client_id = { { 0, 1, 2, 3, 4, 5, 6, 7 } };
575
576 now = 1005;
577 struct session_id server_id = calculate_session_id_hmac(client_id, &addr, key, handwindow, 0);
578
579
580 struct session_id expected_server_id = { { 0xec, 0xa3, 0xd5, 0xcc, 0xb4, 0x7c, 0xa1, 0xee } };
581 /* We have to deal with different structs here annoyingly */
582 /* Linux has an unsigned short int as family_t and this is field is always
583 * stored in host endianness even though the rest of the struct isn't...,
584 * so Linux little endian differs from all BSD and Linux big endian */
585 if (sizeof(addr.addr.in4.sin_family) == sizeof(unsigned short int) && ntohs(AF_INET) != AF_INET)
586 {
587 struct session_id linuxle = { { 0x70, 0x04, 0x8c, 0x0f, 0xfe, 0x30, 0x85, 0x12 } };
588 expected_server_id = linuxle;
589 }
590 assert_memory_equal(expected_server_id.id, server_id.id, SID_SIZE);
591
592 struct session_id server_id_m1 =
593 calculate_session_id_hmac(client_id, &addr, key, handwindow, -1);
594 struct session_id server_id_p1 =
595 calculate_session_id_hmac(client_id, &addr, key, handwindow, 1);
596 struct session_id server_id_p2 =
597 calculate_session_id_hmac(client_id, &addr, key, handwindow, 2);
598
599 assert_memory_not_equal(expected_server_id.id, server_id_m1.id, SID_SIZE);
600 assert_memory_not_equal(expected_server_id.id, server_id_p1.id, SID_SIZE);
601
602 /* changing the time puts us into the next hmac time window (handwindow/2=50)
603 * and shifts the ids by one */
604 now = 1062;
605
606 struct session_id server_id2_m2 =
607 calculate_session_id_hmac(client_id, &addr, key, handwindow, -2);
608 struct session_id server_id2_m1 =
609 calculate_session_id_hmac(client_id, &addr, key, handwindow, -1);
610 struct session_id server_id2 = calculate_session_id_hmac(client_id, &addr, key, handwindow, 0);
611 struct session_id server_id2_p1 =
612 calculate_session_id_hmac(client_id, &addr, key, handwindow, 1);
613
614 assert_memory_equal(server_id2_m2.id, server_id_m1.id, SID_SIZE);
615 assert_memory_equal(server_id2_m1.id, expected_server_id.id, SID_SIZE);
616 assert_memory_equal(server_id2.id, server_id_p1.id, SID_SIZE);
617 assert_memory_equal(server_id2_p1.id, server_id_p2.id, SID_SIZE);
618}
619
620static void
622{
623 struct link_socket_actual from = { 0 };
624 struct tls_auth_standalone tas = { 0 };
625 struct tls_pre_decrypt_state state = { 0 };
626
627 struct session_id client_id = { { 0, 1, 2, 3, 4, 5, 6, 7 } };
628 struct session_id server_id = { { 8, 9, 0, 9, 8, 7, 6, 2 } };
629
630 enum first_packet_verdict verdict;
631
632 tas.tls_wrap.mode = TLS_WRAP_NONE;
633 struct frame frame = { .buf = { .headroom = 200, .payload_size = 1400 }, 0 };
634 tas.frame = frame;
635 tas.workbuf = alloc_buf(1600);
636
637 uint8_t header = 0 | (P_CONTROL_HARD_RESET_CLIENT_V2 << P_OPCODE_SHIFT);
638
639 struct buffer buf =
640 tls_reset_standalone(&tas.tls_wrap, &tas, &client_id, &server_id, header, false);
641
642
643 verdict = tls_pre_decrypt_lite(&tas, &state, &from, &buf);
645
646 /* Assure repeated generation of reset is deterministic/stateless*/
648 struct buffer buf2 =
649 tls_reset_standalone(&tas.tls_wrap, &tas, &client_id, &server_id, header, false);
650 assert_int_equal(BLEN(&buf), BLEN(&buf2));
651 assert_memory_equal(BPTR(&buf), BPTR(&buf2), BLENZ(&buf));
652
654 free_buf(&tas.workbuf);
655}
656
657static void
659{
660 struct link_socket_actual from = { 0 };
661 struct tls_pre_decrypt_state state = { 0 };
662
665
666 packet_id_init(&tas_client.tls_wrap.opt.packet_id, 5, 5, "UNITTEST", 0);
667
668 struct session_id client_id = { { 0xab, 1, 2, 3, 4, 5, 6, 0xcd } };
669 struct session_id server_id = { { 8, 9, 0xa, 0xc, 8, 7, 6, 2 } };
670
671 uint8_t header = 0 | (P_CONTROL_HARD_RESET_CLIENT_V2 << P_OPCODE_SHIFT);
672
673 now = 0x22446688;
675 struct buffer buf = tls_reset_standalone(&tas_client.tls_wrap, &tas_client, &client_id,
676 &server_id, header, false);
677
678 enum first_packet_verdict verdict = tls_pre_decrypt_lite(&tas_server, &state, &from, &buf);
680
682
683 /* Assure repeated generation of reset is deterministic/stateless*/
684 reset_packet_id_send(&tas_client.tls_wrap.opt.packet_id.send);
686 &server_id, header, false);
687 assert_int_equal(BLEN(&buf), BLEN(&buf2));
688 assert_memory_equal(BPTR(&buf), BPTR(&buf2), BLENZ(&buf));
689
691
692 packet_id_free(&tas_client.tls_wrap.opt.packet_id);
693
696}
697
698static void
700{
701 struct gc_arena gc = gc_new();
702 struct buffer input_buf = alloc_buf_gc(1024, &gc);
703
704 /* This message will have a \0x00 at the end since it is a C string */
705 const char input[] = "valid control message\r\n\0\0Invalid\r\none\0valid one again";
706
707 buf_write(&input_buf, input, sizeof(input));
713
714 assert_string_equal(BSTR(&cmd1), "valid control message");
715 /* empty message with just a \0x00 */
716 assert_int_equal(cmd2.len, 1);
718 assert_int_equal(cmd3.len, 0);
719 assert_string_equal(BSTR(&cmd4), "valid one again");
720 assert_int_equal(cmd5.len, 0);
721
722 const uint8_t nonull[6] = { 'n', 'o', ' ', 'N', 'U', 'L' };
723 struct buffer nonull_buf = alloc_buf_gc(1024, &gc);
724
725 buf_write(&nonull_buf, nonull, sizeof(nonull));
728
729 gc_free(&gc);
730}
731
732int
733main(void)
734{
736
737 const struct CMUnitTest tests[] = {
738 cmocka_unit_test(test_verify_hmac_none),
739 cmocka_unit_test(test_tls_decrypt_lite_none),
740 cmocka_unit_test(test_tls_decrypt_lite_auth),
741 cmocka_unit_test(test_tls_decrypt_lite_crypt),
742 cmocka_unit_test(test_parse_ack),
743 cmocka_unit_test(test_calc_session_id_hmac_static),
744 cmocka_unit_test(test_verify_hmac_tls_auth),
746 cmocka_unit_test(test_generate_reset_packet_plain),
747 cmocka_unit_test(test_generate_reset_packet_tls_auth),
748 cmocka_unit_test(test_extract_control_message)
749 };
750
751 return cmocka_run_group_tests_name("pkt tests", tests, NULL, NULL);
752}
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
Definition buffer.c:169
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
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
Definition buffer.c:60
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:151
#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:1271
static bool buf_inc_len(struct buffer *buf, int inc)
Increase or decrease the length of a buffer.
Definition buffer.h:1078
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:1124
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1198
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:145
static void buf_reset_len(struct buffer *buf)
Reset the length and offset of a buffer to zero.
Definition buffer.h:552
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:147
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1912
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1896
void free_key_ctx_bi(struct key_ctx_bi *ctx)
Definition crypto.c:1100
void init_key_type(struct key_type *kt, const char *ciphername, const char *authname, bool tls_mode, bool warn)
Initialize a key_type structure with.
Definition crypto.c:875
void crypto_read_openvpn_key(const struct key_type *key_type, struct key_ctx_bi *ctx, const char *key_file, bool key_inline, const int key_direction, const char *key_name, const char *opt_name, struct key2 *keydata)
Definition crypto.c:1290
Data Channel Cryptography Module.
#define KEY_DIRECTION_NORMAL
Definition crypto.h:232
#define CO_PACKET_ID_LONG_FORM
Bit-flag indicating whether to use OpenVPN's long packet ID format.
Definition crypto.h:347
#define CO_IGNORE_PACKET_ID
Bit-flag indicating whether to ignore the packet ID of a received packet.
Definition crypto.h:350
#define KEY_DIRECTION_INVERSE
Definition crypto.h:233
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:295
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:166
void tls_crypt_init_key(struct key_ctx_bi *key, struct key2 *keydata, const char *key_file, bool key_inline, bool tls_server)
Initialize a key_ctx_bi structure for use with --tls-crypt.
Definition tls_crypt.c:62
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:219
time_t now
Definition otime.c:33
void packet_id_init(struct packet_id *p, int seq_backtrack, int time_backtrack, const char *name, int unit)
Definition packet_id.c:96
void packet_id_free(struct packet_id *p)
Definition packet_id.c:126
static void reset_packet_id_send(struct packet_id_send *p)
Reset the current send packet id to its initial state.
Definition packet_id.h:312
Reliability Layer module header file.
#define SID_SIZE
Definition session_id.h:44
#define SIPHASH_KEY_SIZE
Definition siphash.h:35
static void siphash_key_init(uint8_t *key)
Initialises a SIPHASH key with a random value.
Definition siphash.h:85
Control Channel SSL library backend module.
void free_tls_pre_decrypt_state(struct tls_pre_decrypt_state *state)
Definition ssl_pkt.c:273
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:555
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:405
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:447
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.
Definition ssl_pkt.c:490
SSL control channel wrap/unwrap and decode functions.
#define P_OPCODE_SHIFT
Definition ssl_pkt.h:39
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_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_CONTROL_V1
This packet is a valid control packet from the peer.
Definition ssl_pkt.h:91
#define P_CONTROL_HARD_RESET_CLIENT_V2
Definition ssl_pkt.h:51
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
unsigned int flags
Bit-flags determining behavior of security operation functions.
Definition crypto.h:386
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
Definition crypto.h:294
struct packet_id packet_id
Current packet ID state for both sending and receiving directions.
Definition crypto.h:333
Packet geometry parameters.
Definition mtu.h:113
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
struct frame::@8 buf
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
union openvpn_sockaddr::@27 addr
struct sockaddr sa
Definition socket_util.h:42
struct sockaddr_in in4
Definition socket_util.h:43
struct packet_id_send send
Definition packet_id.h:200
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:81
struct buffer workbuf
Definition ssl_pkt.h:80
struct tls_wrap_ctx tls_wrap
Definition ssl_pkt.h:79
struct that stores the temporary data for the tls lite decrypt functions
Definition ssl_pkt.h:106
struct session_id peer_session_id
Definition ssl_pkt.h:109
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:489
struct crypto_options opt
Crypto state.
Definition ssl_common.h:283
enum tls_wrap_ctx::@28 mode
Control channel wrapping mode.
struct buffer work
Work buffer (only for –tls-crypt)
Definition ssl_common.h:284
struct key2 original_wrap_keydata
original key data to be xored in to the key for dynamic tls-crypt.
Definition ssl_common.h:298
static void openvpn_unit_test_setup(void)
Sets up the environment for unit tests like making both stderr and stdout non-buffered to avoid messa...
Definition test_common.h:61
int parse_line(const char *line, char **p, const int n, const char *file, const int line_num, msglvl_t msglevel, struct gc_arena *gc)
Definition test_pkt.c:48
const uint8_t client_ack_none_random_id[]
Definition test_pkt.c:139
void test_tls_decrypt_lite_crypt(void **ut_state)
Definition test_pkt.c:211
static void test_parse_ack(void **ut_state)
Definition test_pkt.c:359
const uint8_t client_reset_v2_tls_auth[]
Definition test_pkt.c:95
bool key_state_export_keying_material(struct tls_session *session, const char *label, size_t label_size, void *ekm, size_t ekm_size)
Keying Material Exporters [RFC 5705] allows additional keying material to be derived from existing TL...
Definition test_pkt.c:59
static void test_extract_control_message(void **ut_state)
Definition test_pkt.c:699
static void test_verify_hmac_none(void **ut_state)
Definition test_pkt.c:436
const uint8_t client_reset_v2_tls_crypt[]
Definition test_pkt.c:101
static void test_calc_session_id_hmac_static(void **ut_state)
Definition test_pkt.c:563
const char static_key[]
Definition test_pkt.c:71
void free_tas(struct tls_auth_standalone *tas)
Definition test_pkt.c:201
static void test_generate_reset_packet_tls_auth(void **ut_state)
Definition test_pkt.c:658
static void test_generate_reset_packet_plain(void **ut_state)
Definition test_pkt.c:621
struct tls_auth_standalone init_tas_crypt(bool server)
Definition test_pkt.c:186
const uint8_t client_ack_tls_auth_randomid[]
Definition test_pkt.c:109
static void test_verify_hmac_tls_auth(void **ut_state)
Definition test_pkt.c:405
const char * print_link_socket_actual(const struct link_socket_actual *act, struct gc_arena *gc)
Definition test_pkt.c:66
void test_tls_decrypt_lite_none(void **ut_state)
Definition test_pkt.c:318
int main(void)
Definition test_pkt.c:733
const uint8_t client_control_with_ack[]
Definition test_pkt.c:134
static void test_verify_hmac_none_out_of_range_ack(void **ut_state)
Definition test_pkt.c:519
const uint8_t client_control_none_random_id[]
Definition test_pkt.c:155
const uint8_t client_ack_123_none_random_id[]
Definition test_pkt.c:144
void test_tls_decrypt_lite_auth(void **ut_state)
Definition test_pkt.c:257
struct tls_auth_standalone init_tas_auth(int key_direction)
Definition test_pkt.c:165
const uint8_t client_reset_v2_none[]
Definition test_pkt.c:92
struct gc_arena gc
Definition test_ssl.c:122
static struct key_type tls_crypt_kt(void)
Definition tls_crypt.c:50