OpenVPN
test_control_msg.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
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include <stdarg.h>
30#include <stddef.h>
31#include <setjmp.h>
32#include <cmocka.h>
33
34#include "control_msg.h"
35#include "ssl_pkt.h"
36#include "test_common.h"
37#include "test_control_msg.h"
38
39/* The early negotiation flags TLV of a reset packet asking a tls-crypt-v2
40 * client to resend its WKc. */
41void
43{
44 struct gc_arena gc = gc_new();
45 struct buffer buf = alloc_buf_gc(16, &gc);
46
47 assert_true(ctrl_msg_tlv_write_u16(&buf, TLV_TYPE_EARLY_NEG_FLAGS, false,
49 const uint8_t expected[] = { 0x00, 0x01, 0x00, 0x02, 0x00, 0x01 };
50 assert_int_equal(BLEN(&buf), sizeof(expected));
51 assert_memory_equal(BPTR(&buf), expected, sizeof(expected));
52
53 /* the optional flag is the most significant bit of the type field */
54 buf_clear(&buf);
55 assert_true(ctrl_msg_tlv_write_u16(&buf, TLV_TYPE_EARLY_NEG_FLAGS, true, 0));
56 assert_int_equal(BPTR(&buf)[0], 0x80);
57 assert_int_equal(BPTR(&buf)[1], 0x01);
58
59 gc_free(&gc);
60}
61
62/* A TLV header survives the round trip, the optional flag kept apart from
63 * the 15-bit type. */
64void
66{
67 struct gc_arena gc = gc_new();
68 struct buffer buf = alloc_buf_gc(16, &gc);
69
70 assert_true(ctrl_msg_tlv_write_header(&buf, 0x1234, true, 5));
71 assert_int_equal(BLEN(&buf), 4);
72
73 struct ctrl_msg_tlv_header hdr;
74 assert_true(ctrl_msg_tlv_peek_header(&buf, &hdr));
75 assert_int_equal(hdr.type, 0x1234);
76 assert_true(hdr.optional);
77 assert_int_equal(hdr.value_len, 5);
78 assert_int_equal(BLEN(&buf), CTRL_MSG_TLV_HEADER_LEN); /* peek consumes nothing */
79
80 gc_free(&gc);
81}
82
83/* Walking a payload returns each TLV in turn, with a value covering exactly
84 * its bytes, and consumes the payload. */
85void
87{
88 struct gc_arena gc = gc_new();
89 struct buffer buf = alloc_buf_gc(64, &gc);
90
91 assert_true(ctrl_msg_tlv_write_u16(&buf, TLV_TYPE_EARLY_NEG_FLAGS, false, 0xabcd));
92 assert_true(ctrl_msg_tlv_write_header(&buf, 0x7ff, true, 4));
93 assert_true(buf_write_u32(&buf, 0x11223344));
94 assert_true(ctrl_msg_tlv_write_header(&buf, 0x5, false, 0)); /* empty value */
95
96 struct ctrl_msg_tlv_header hdr;
97 struct buffer value;
98
99 assert_true(ctrl_msg_tlv_next(&buf, &hdr, &value));
100 assert_int_equal(hdr.type, TLV_TYPE_EARLY_NEG_FLAGS);
101 assert_false(hdr.optional);
102 assert_int_equal(BLEN(&value), 2);
103 assert_int_equal(buf_read_u16(&value), 0xabcd);
104
105 assert_true(ctrl_msg_tlv_next(&buf, &hdr, &value));
106 assert_int_equal(hdr.type, 0x7ff);
107 assert_true(hdr.optional);
108 assert_int_equal(BLEN(&value), 4);
109 assert_int_equal(buf_read_u32(&value, NULL), 0x11223344);
110
111 assert_true(ctrl_msg_tlv_next(&buf, &hdr, &value));
112 assert_int_equal(hdr.type, 0x5);
113 assert_int_equal(BLEN(&value), 0);
114
115 assert_int_equal(BLEN(&buf), 0);
116
117 gc_free(&gc);
118}
119
120/* A TLV header claiming more value bytes than the payload holds is rejected,
121 * rather than read past the end of the payload, and leaves buf untouched. */
122void
124{
125 struct gc_arena gc = gc_new();
126 struct buffer buf = alloc_buf_gc(64, &gc);
127
128 assert_true(ctrl_msg_tlv_write_header(&buf, TLV_TYPE_EARLY_NEG_FLAGS, false, 8));
129 assert_true(buf_write_u32(&buf, 0));
130
131 struct ctrl_msg_tlv_header hdr;
132 struct buffer value;
133 assert_false(ctrl_msg_tlv_next(&buf, &hdr, &value));
134 assert_int_equal(BLEN(&buf), CTRL_MSG_TLV_HEADER_LEN + sizeof(uint32_t));
135
136 gc_free(&gc);
137}
138
139/* Reading a TLV header must fail, rather than read past the available data,
140 * when the buffer holds less than a complete 4-byte header, and must leave
141 * buf untouched. */
142void
144{
145 struct gc_arena gc = gc_new();
146 const uint8_t bytes[] = { 0x00, 0x01, 0x00 };
147
148 /* 0 to 3 bytes: none, part of the type field, the type field, and the type
149 * field with half of the length */
150 for (size_t n = 0; n <= sizeof(bytes); n++)
151 {
152 struct buffer buf = alloc_buf_gc(16, &gc);
153 assert_true(buf_write(&buf, bytes, n));
154
155 struct ctrl_msg_tlv_header hdr;
156 struct buffer value;
157 assert_false(ctrl_msg_tlv_peek_header(&buf, &hdr));
158 assert_false(ctrl_msg_tlv_next(&buf, &hdr, &value));
159 assert_int_equal(BLEN(&buf), n);
160 }
161
162 gc_free(&gc);
163}
164
165/* Writing fails, rather than writes a partial TLV, when buf is too small. */
166void
168{
169 struct gc_arena gc = gc_new();
170
171 struct buffer buf = alloc_buf_gc(3, &gc);
172 assert_false(ctrl_msg_tlv_write_header(&buf, TLV_TYPE_EARLY_NEG_FLAGS, false, 2));
173 assert_int_equal(BLEN(&buf), 0);
174
175 /* room for the header but not the value */
176 buf = alloc_buf_gc(5, &gc);
177 assert_false(ctrl_msg_tlv_write_u16(&buf, TLV_TYPE_EARLY_NEG_FLAGS, false, 1));
178 assert_int_equal(BLEN(&buf), 0);
179
180 gc_free(&gc);
181}
void buf_clear(struct buffer *buf)
Zeroise and reset a buffer.
Definition buffer.c:153
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
#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 int buf_read_u16(struct buffer *buf)
Read and consume a uint16_t from the front of a buffer.
Definition buffer.h:1516
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1239
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
static uint32_t buf_read_u32(struct buffer *buf, bool *good)
Read and consume a uint32_t from the front of a buffer.
Definition buffer.h:1538
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
bool ctrl_msg_tlv_peek_header(const struct buffer *buf, struct ctrl_msg_tlv_header *hdr)
Read a TLV header from buf without consuming it.
Definition control_msg.c:60
bool ctrl_msg_tlv_write_header(struct buffer *buf, uint16_t type, bool optional, uint16_t value_len)
Write a TLV header (type + optional flag + value length) to buf.
Definition control_msg.c:32
bool ctrl_msg_tlv_next(struct buffer *buf, struct ctrl_msg_tlv_header *hdr, struct buffer *value)
Read the next TLV from buf, advancing past its header and value.
Definition control_msg.c:76
TLV framing shared by the TLV-based control messages of the wire protocol.
#define TLV_TYPE_EARLY_NEG_FLAGS
Definition control_msg.h:43
#define CTRL_MSG_TLV_HEADER_LEN
Definition control_msg.h:48
SSL control channel wrap/unwrap and decode functions.
#define EARLY_NEG_FLAG_RESEND_WKC
Definition ssl_pkt.h:357
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
The header every TLV carries: the 15-bit type and optional flag packed into the first 16-bit field,...
Definition control_msg.h:57
bool optional
value of the optional flag
Definition control_msg.h:59
uint16_t value_len
length of the value following the header
Definition control_msg.h:60
uint16_t type
the 15-bit TLV type
Definition control_msg.h:58
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
void test_tlv_header_roundtrip(void **state)
void test_tlv_header_truncated(void **state)
void test_tlv_next_value_truncated(void **state)
void test_tlv_write_u16_wire_format(void **state)
void test_tlv_write_no_room(void **state)
void test_tlv_next_walks_payload(void **state)
struct gc_arena gc
Definition test_ssl.c:138