OpenVPN
options_string.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) 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 "buffer.h"
30#include "crypto.h"
31#include "error.h"
32#include "mtu.h"
33#include "options.h"
34#include "socket_util.h"
35#include "ssl.h"
36#include "ssl_ncp.h"
37#include "tun.h"
38
39#include <string.h>
40
41char *
42options_string(const struct options *o, const struct frame *frame, struct tuntap *tt,
43 openvpn_net_ctx_t *ctx, bool remote, struct gc_arena *gc)
44{
45 struct buffer out = alloc_buf(OPTION_LINE_SIZE);
46 bool tt_local = false;
47
48 buf_printf(&out, "V4");
49
50 /*
51 * Tunnel Options
52 */
53
54 buf_printf(&out, ",dev-type %s", dev_type_string(o->dev, o->dev_type));
55 /* the link-mtu that we send has only a meaning if have a fixed
56 * cipher (p2p) or have a fallback cipher configured for older non
57 * ncp clients. But not sending it will make even 2.4 complain
58 * about it being missing. So still send it. */
59 buf_printf(&out, ",link-mtu %u", (unsigned int)calc_options_string_link_mtu(o, frame));
60
61 if (o->ce.occ_mtu != 0)
62 {
63 buf_printf(&out, ",tun-mtu %d", o->ce.occ_mtu);
64 }
65 else
66 {
67 buf_printf(&out, ",tun-mtu %d", frame->tun_mtu);
68 }
69
70 buf_printf(&out, ",proto %s", proto_remote(o->ce.proto, remote));
71
72 bool p2p_nopull = o->mode == MODE_POINT_TO_POINT && !PULL_DEFINED(o);
73 /* send tun_ipv6 only in peer2peer mode - in client/server mode, it
74 * is usually pushed by the server, triggering a non-helpful warning
75 */
76 if (o->ifconfig_ipv6_local && p2p_nopull)
77 {
78 buf_printf(&out, ",tun-ipv6");
79 }
80
81 /*
82 * Try to get ifconfig parameters into the options string.
83 * If tt is undefined, make a temporary instantiation.
84 */
85 if (!tt)
86 {
87 tt = init_tun(o->dev, o->dev_type, o->topology, o->ifconfig_local,
89 o->ifconfig_ipv6_remote, NULL, NULL, false, NULL, ctx, NULL);
90 if (tt)
91 {
92 tt_local = true;
93 }
94 }
95
96 if (tt && p2p_nopull)
97 {
98 const char *ios = ifconfig_options_string(tt, remote, o->ifconfig_nowarn, gc);
99 if (ios && strlen(ios))
100 {
101 buf_printf(&out, ",ifconfig %s", ios);
102 }
103 }
104 if (tt_local)
105 {
106 free(tt);
107 tt = NULL;
108 }
109
110#ifdef USE_COMP
111 if (o->comp.alg != COMP_ALG_UNDEF)
112 {
113 /* for compatibility, this simply indicates that compression
114 * context is active, not necessarily LZO per-se */
115 buf_printf(&out, ",comp-lzo");
116 }
117#endif
118
119#ifdef ENABLE_FRAGMENT
120 if (o->ce.fragment)
121 {
122 buf_printf(&out, ",mtu-dynamic");
123 }
124#endif
125
126#define TLS_CLIENT (o->tls_client)
127#define TLS_SERVER (o->tls_server)
128
129 /*
130 * Key direction
131 */
132 {
133 const char *kd = keydirection2ascii(o->key_direction, remote, false);
134 if (kd)
135 {
136 buf_printf(&out, ",keydir %s", kd);
137 }
138 }
139
140 /*
141 * Crypto Options
142 */
143 struct key_type kt;
144
145 ASSERT((TLS_CLIENT == true) + (TLS_SERVER == true) <= 1);
146
147 /* Skip resolving BF-CBC to allow SSL libraries without BF-CBC
148 * to work here in the default configuration */
149 const char *ciphername = o->ciphername;
150 size_t keysize = 0;
151
152 if (strcmp(o->ciphername, "BF-CBC") == 0)
153 {
154 init_key_type(&kt, "none", o->authname, false);
155 keysize = 128;
156 }
157 else
158 {
159 init_key_type(&kt, o->ciphername, o->authname, false);
160 ciphername = cipher_kt_name(kt.cipher);
162 {
163 keysize = cipher_kt_key_size(kt.cipher) * 8;
164 }
165 }
166 /* Only announce the cipher to our peer if we are willing to
167 * support it */
168 if (p2p_nopull || tls_item_in_cipher_list(ciphername, o->ncp_ciphers))
169 {
170 buf_printf(&out, ",cipher %s", ciphername);
171 }
172 buf_printf(&out, ",auth %s", md_kt_name(kt.digest));
173 buf_printf(&out, ",keysize %zu", keysize);
174
175 /*
176 * SSL Options
177 */
178 if (o->ce.tls_auth_file)
179 {
180 buf_printf(&out, ",tls-auth");
181 }
182 /* Not adding tls-crypt here, because we won't reach this code if
183 * tls-auth/tls-crypt does not match. Removing tls-auth here would
184 * break stuff, so leaving that in place. */
185
186 buf_printf(&out, ",key-method %d", KEY_METHOD_2);
187
188
189 if (remote)
190 {
191 if (TLS_CLIENT)
192 {
193 buf_printf(&out, ",tls-server");
194 }
195 else if (TLS_SERVER)
196 {
197 buf_printf(&out, ",tls-client");
198 }
199 }
200 else
201 {
202 if (TLS_CLIENT)
203 {
204 buf_printf(&out, ",tls-client");
205 }
206 else if (TLS_SERVER)
207 {
208 buf_printf(&out, ",tls-server");
209 }
210 }
211
212#undef TLS_CLIENT
213#undef TLS_SERVER
214
215 return BSTR(&out);
216}
217
225static const char *
226options_warning_extract_parm1(const char *option_string, struct gc_arena *gc_ret)
227{
228 struct gc_arena gc = gc_new();
229 struct buffer b = string_alloc_buf(option_string, &gc);
230 char *p = gc_malloc(OPTION_PARM_SIZE, false, &gc);
231 const char *ret;
232
233 buf_parse(&b, ' ', p, OPTION_PARM_SIZE);
234 ret = string_alloc(p, gc_ret);
235 gc_free(&gc);
236 return ret;
237}
238
239static void
240options_warning_safe_scan2(const msglvl_t msglevel, const int delim,
241 const bool report_inconsistent, const char *p1,
242 const struct buffer *b2_src, const char *b1_name,
243 const char *b2_name)
244{
245 /* We will stop sending 'key-method', 'keydir', 'proto' and 'tls-auth' in
246 * OCC in a future version (because it's not useful). To reduce questions
247 * when interoperating, we no longer printing a warning about it.
248 */
249 if (strprefix(p1, "key-method ") || strprefix(p1, "keydir ") || strprefix(p1, "proto ")
250 || streq(p1, "tls-auth") || strprefix(p1, "tun-ipv6") || strprefix(p1, "cipher "))
251 {
252 return;
253 }
254
255 if (strlen(p1) > 0)
256 {
257 struct gc_arena gc = gc_new();
258 struct buffer b2 = *b2_src;
259 const char *p1_prefix = options_warning_extract_parm1(p1, &gc);
260 char *p2 = gc_malloc(OPTION_PARM_SIZE, false, &gc);
261
262 while (buf_parse(&b2, delim, p2, OPTION_PARM_SIZE))
263 {
264 if (strlen(p2))
265 {
266 const char *p2_prefix = options_warning_extract_parm1(p2, &gc);
267
268 if (!strcmp(p1, p2))
269 {
270 goto done;
271 }
272 if (!strcmp(p1_prefix, p2_prefix))
273 {
274 if (report_inconsistent)
275 {
276 msg(msglevel, "WARNING: '%s' is used inconsistently, %s='%s', %s='%s'",
277 safe_print(p1_prefix, &gc), b1_name, safe_print(p1, &gc), b2_name,
278 safe_print(p2, &gc));
279 }
280 goto done;
281 }
282 }
283 }
284
285 msg(msglevel, "WARNING: '%s' is present in %s config but missing in %s config, %s='%s'",
286 safe_print(p1_prefix, &gc), b1_name, b2_name, b1_name, safe_print(p1, &gc));
287
288done:
289 gc_free(&gc);
290 }
291}
292
293static void
294options_warning_safe_scan1(const msglvl_t msglevel, const int delim,
295 const bool report_inconsistent, const struct buffer *b1_src,
296 const struct buffer *b2_src, const char *b1_name, const char *b2_name)
297{
298 struct gc_arena gc = gc_new();
299 struct buffer b = *b1_src;
300 char *p = gc_malloc(OPTION_PARM_SIZE, true, &gc);
301
302 while (buf_parse(&b, delim, p, OPTION_PARM_SIZE))
303 {
304 options_warning_safe_scan2(msglevel, delim, report_inconsistent, p, b2_src, b1_name,
305 b2_name);
306 }
307
308 gc_free(&gc);
309}
310
311static void
312options_warning_safe_ml(const msglvl_t msglevel, char *actual, const char *expected, size_t actual_n)
313{
314 struct gc_arena gc = gc_new();
315
316 if (actual_n > 0)
317 {
318 struct buffer local = alloc_buf_gc(OPTION_PARM_SIZE + 16, &gc);
319 struct buffer remote = alloc_buf_gc(OPTION_PARM_SIZE + 16, &gc);
320 actual[actual_n - 1] = 0;
321
322 buf_printf(&local, "version %s", expected);
323 buf_printf(&remote, "version %s", actual);
324
325 options_warning_safe_scan1(msglevel, ',', true, &local, &remote, "local", "remote");
326
327 options_warning_safe_scan1(msglevel, ',', false, &remote, &local, "remote", "local");
328 }
329
330 gc_free(&gc);
331}
332
333bool
334options_cmp_equal_safe(char *actual, const char *expected, size_t actual_n)
335{
336 bool ret = true;
337
338 if (actual_n > 0)
339 {
340 actual[actual_n - 1] = 0;
341 if (strncmp(actual, expected, 2))
342 {
343 msg(D_SHOW_OCC, "NOTE: Options consistency check may be skewed by version differences");
344 options_warning_safe_ml(D_SHOW_OCC, actual, expected, actual_n);
345 }
346 else
347 {
348 ret = !strcmp(actual, expected);
349 }
350 }
351 return ret;
352}
353
354bool
355options_cmp_equal(char *actual, const char *expected)
356{
357 return options_cmp_equal_safe(actual, expected, strlen(actual) + 1);
358}
359
360void
361options_warning_safe(char *actual, const char *expected, size_t actual_n)
362{
363 options_warning_safe_ml(D_SHOW_OCC, actual, expected, actual_n);
364}
365
366void
367options_warning(char *actual, const char *expected)
368{
369 options_warning_safe(actual, expected, strlen(actual) + 1);
370}
371
372const char *
373options_string_version(const char *s, struct gc_arena *gc)
374{
375 char *out = gc_malloc(3, false, gc);
376 strlcpy(out, s, 3);
377 return out;
378}
379
380#if defined(__GNUC__) || defined(__clang__)
381#pragma GCC diagnostic push
382#pragma GCC diagnostic ignored "-Wsign-compare"
383#pragma GCC diagnostic ignored "-Wsign-conversion"
384#endif
385
386char *
387options_string_extract_option(const char *option_string, const char *opt_name, struct gc_arena *gc)
388{
389 char *ret = NULL;
390 const size_t opt_name_len = strlen(opt_name);
391
392 const char *p = option_string;
393 while (p)
394 {
395 if (0 == strncmp(p, opt_name, opt_name_len) && strlen(p) > (opt_name_len + 1)
396 && p[opt_name_len] == ' ')
397 {
398 /* option found, extract value */
399 const char *start = &p[opt_name_len + 1];
400 const char *end = strchr(p, ',');
401 size_t val_len = end ? end - start : strlen(start);
402 ret = gc_malloc(val_len + 1, true, gc);
403 memcpy(ret, start, val_len);
404 break;
405 }
406 p = strchr(p, ',');
407 if (p)
408 {
409 p++; /* skip delimiter */
410 }
411 }
412 return ret;
413}
414
415#if defined(__GNUC__) || defined(__clang__)
416#pragma GCC diagnostic pop
417#endif
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Allocate memory and, optionally, zero it.
Definition buffer.c:321
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
bool buf_parse(struct buffer *buf, const int delim, char *line, const int size)
Extract the next token from a buffer, delimited by a given character.
Definition buffer.c:783
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:618
struct buffer string_alloc_buf(const char *str, struct gc_arena *gc)
Allocate a buffer containing a copy of the given string.
Definition buffer.c:705
Buffer management functions and garbage collection.
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
#define streq(x, y)
helper to work around the semantics of the strcmp() return value
Definition buffer.h:1822
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
static bool strprefix(const char *str, const char *prefix)
Return true iff str starts with prefix.
Definition buffer.h:1814
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
#define COMP_ALG_UNDEF
Definition comp.h:54
size_t strlcpy(char *dest, const char *src, size_t maxlen)
void init_key_type(struct key_type *kt, const char *ciphername, const char *authname, bool warn)
Initialize a key_type structure with.
Definition crypto.c:872
const char * keydirection2ascii(int kd, bool remote, bool humanreadable)
Definition crypto.c:1620
Data Channel Cryptography Module.
static bool cipher_defined(const char *ciphername)
Checks if the cipher is defined and is not the null (none) cipher.
const char * md_kt_name(const char *mdname)
Retrieve a string describing the digest digest (e.g.
const char * cipher_kt_name(const char *ciphername)
Retrieve a normalised string describing the cipher (e.g.
unsigned int cipher_kt_key_size(const char *ciphername)
Returns the size of keys used by the cipher, in bytes.
#define D_SHOW_OCC
Definition errlevel.h:147
const char * safe_print(const char *str, struct gc_arena *gc)
Definition misc.c:560
size_t calc_options_string_link_mtu(const struct options *o, const struct frame *frame)
Calculate the link-mtu to advertise to our peer.
Definition mtu.c:154
void * openvpn_net_ctx_t
Definition networking.h:41
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:221
#define MODE_POINT_TO_POINT
Definition options.h:264
#define OPTION_PARM_SIZE
Definition options.h:56
#define PULL_DEFINED(opt)
Definition options.h:778
#define OPTION_LINE_SIZE
Definition options.h:57
bool options_cmp_equal(char *actual, const char *expected)
#define TLS_CLIENT
static const char * options_warning_extract_parm1(const char *option_string, struct gc_arena *gc_ret)
Compare option strings for equality.
bool options_cmp_equal_safe(char *actual, const char *expected, size_t actual_n)
void options_warning_safe(char *actual, const char *expected, size_t actual_n)
static void options_warning_safe_scan1(const msglvl_t msglevel, const int delim, const bool report_inconsistent, const struct buffer *b1_src, const struct buffer *b2_src, const char *b1_name, const char *b2_name)
static void options_warning_safe_ml(const msglvl_t msglevel, char *actual, const char *expected, size_t actual_n)
char * options_string_extract_option(const char *option_string, const char *opt_name, struct gc_arena *gc)
Given an OpenVPN options string, extract the value of an option.
static void options_warning_safe_scan2(const msglvl_t msglevel, const int delim, const bool report_inconsistent, const char *p1, const struct buffer *b2_src, const char *b1_name, const char *b2_name)
void options_warning(char *actual, const char *expected)
const char * options_string_version(const char *s, struct gc_arena *gc)
#define TLS_SERVER
char * options_string(const struct options *o, const struct frame *frame, struct tuntap *tt, openvpn_net_ctx_t *ctx, bool remote, struct gc_arena *gc)
Build an options string to represent data channel encryption options.
const char * proto_remote(int proto, bool remote)
Control Channel SSL/Data channel negotiation module.
#define KEY_METHOD_2
Definition ssl.h:122
bool tls_item_in_cipher_list(const char *item, const char *list)
Return true iff item is present in the colon-separated zero-terminated cipher list.
Definition ssl_ncp.c:197
Control Channel SSL/Data dynamic negotiation Module This file is split from ssl.h to be able to unit ...
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int occ_mtu
Definition options.h:130
int fragment
Definition options.h:143
int proto
Definition options.h:111
const char * tls_auth_file
Definition options.h:167
Packet geometry parameters.
Definition mtu.h:113
int tun_mtu
the (user) configured tun-mtu.
Definition mtu.h:147
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
const char * cipher
const name of the cipher
Definition crypto.h:135
const char * digest
Message digest static parameters.
Definition crypto.h:136
struct compress_options comp
Definition options.h:410
const char * ifconfig_ipv6_remote
Definition options.h:331
bool ifconfig_nowarn
Definition options.h:333
const char * authname
Definition options.h:579
const char * ifconfig_ipv6_local
Definition options.h:329
const char * dev_type
Definition options.h:323
const char * ifconfig_local
Definition options.h:327
struct connection_entry ce
Definition options.h:294
int topology
Definition options.h:326
const char * ncp_ciphers
Definition options.h:578
const char * ciphername
Definition options.h:573
int mode
Definition options.h:266
int key_direction
Definition options.h:572
const char * ifconfig_remote_netmask
Definition options.h:328
const char * dev
Definition options.h:322
int ifconfig_ipv6_netbits
Definition options.h:330
Definition tun.h:181
struct gc_arena gc
Definition test_ssl.c:138
struct tuntap * init_tun(const char *dev, const char *dev_type, int topology, const char *ifconfig_local_parm, const char *ifconfig_remote_netmask_parm, const char *ifconfig_ipv6_local_parm, int ifconfig_ipv6_netbits_parm, const char *ifconfig_ipv6_remote_parm, const struct addrinfo *local_public, const struct addrinfo *remote_public, const bool strict_warn, struct env_set *es, openvpn_net_ctx_t *ctx, struct tuntap *tt)
Definition tun.c:839
const char * dev_type_string(const char *dev, const char *dev_type)
Definition tun.c:546
const char * ifconfig_options_string(const struct tuntap *tt, bool remote, bool disable, struct gc_arena *gc)
Definition tun.c:698