OpenVPN
ssl.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-2018 OpenVPN Inc <sales@openvpn.net>
9  * Copyright (C) 2010-2018 Fox Crypto B.V. <openvpn@fox-it.com>
10  * Copyright (C) 2008-2013 David Sommerseth <dazo@users.sourceforge.net>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2
14  * as published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
30 /*
31  * The routines in this file deal with dynamically negotiating
32  * the data channel HMAC and cipher keys through a TLS session.
33  *
34  * Both the TLS session and the data channel are multiplexed
35  * over the same TCP/UDP port.
36  */
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #elif defined(_MSC_VER)
40 #include "config-msvc.h"
41 #endif
42 
43 #include "syshead.h"
44 #include "win32.h"
45 
46 #include "error.h"
47 #include "common.h"
48 #include "socket.h"
49 #include "misc.h"
50 #include "fdmisc.h"
51 #include "interval.h"
52 #include "perf.h"
53 #include "status.h"
54 #include "gremlin.h"
55 #include "pkcs11.h"
56 #include "route.h"
57 #include "tls_crypt.h"
58 
59 #include "ssl.h"
60 #include "ssl_verify.h"
61 #include "ssl_backend.h"
62 #include "ssl_ncp.h"
63 #include "auth_token.h"
64 
65 #include "memdbg.h"
66 
67 #ifndef ENABLE_OCC
68 static const char ssl_default_options_string[] = "V0 UNDEF";
69 #endif
70 
71 
72 static inline const char *
74 {
75 #ifdef ENABLE_OCC
76  return session->opt->local_options;
77 #else
78  return ssl_default_options_string;
79 #endif
80 }
81 
82 #ifdef MEASURE_TLS_HANDSHAKE_STATS
83 
84 static int tls_handshake_success; /* GLOBAL */
85 static int tls_handshake_error; /* GLOBAL */
86 static int tls_packets_generated; /* GLOBAL */
87 static int tls_packets_sent; /* GLOBAL */
88 
89 #define INCR_SENT ++tls_packets_sent
90 #define INCR_GENERATED ++tls_packets_generated
91 #define INCR_SUCCESS ++tls_handshake_success
92 #define INCR_ERROR ++tls_handshake_error
93 
94 void
95 show_tls_performance_stats(void)
96 {
97  msg(D_TLS_DEBUG_LOW, "TLS Handshakes, success=%f%% (good=%d, bad=%d), retransmits=%f%%",
98  (double) tls_handshake_success / (tls_handshake_success + tls_handshake_error) * 100.0,
99  tls_handshake_success, tls_handshake_error,
100  (double) (tls_packets_sent - tls_packets_generated) / tls_packets_generated * 100.0);
101 }
102 #else /* ifdef MEASURE_TLS_HANDSHAKE_STATS */
103 
104 #define INCR_SENT
105 #define INCR_GENERATED
106 #define INCR_SUCCESS
107 #define INCR_ERROR
108 
109 #endif /* ifdef MEASURE_TLS_HANDSHAKE_STATS */
110 
115  {"ADH-SEED-SHA", "TLS-DH-anon-WITH-SEED-CBC-SHA"},
116  {"AES128-GCM-SHA256", "TLS-RSA-WITH-AES-128-GCM-SHA256"},
117  {"AES128-SHA256", "TLS-RSA-WITH-AES-128-CBC-SHA256"},
118  {"AES128-SHA", "TLS-RSA-WITH-AES-128-CBC-SHA"},
119  {"AES256-GCM-SHA384", "TLS-RSA-WITH-AES-256-GCM-SHA384"},
120  {"AES256-SHA256", "TLS-RSA-WITH-AES-256-CBC-SHA256"},
121  {"AES256-SHA", "TLS-RSA-WITH-AES-256-CBC-SHA"},
122  {"CAMELLIA128-SHA256", "TLS-RSA-WITH-CAMELLIA-128-CBC-SHA256"},
123  {"CAMELLIA128-SHA", "TLS-RSA-WITH-CAMELLIA-128-CBC-SHA"},
124  {"CAMELLIA256-SHA256", "TLS-RSA-WITH-CAMELLIA-256-CBC-SHA256"},
125  {"CAMELLIA256-SHA", "TLS-RSA-WITH-CAMELLIA-256-CBC-SHA"},
126  {"DES-CBC3-SHA", "TLS-RSA-WITH-3DES-EDE-CBC-SHA"},
127  {"DES-CBC-SHA", "TLS-RSA-WITH-DES-CBC-SHA"},
128  {"DH-DSS-SEED-SHA", "TLS-DH-DSS-WITH-SEED-CBC-SHA"},
129  {"DHE-DSS-AES128-GCM-SHA256", "TLS-DHE-DSS-WITH-AES-128-GCM-SHA256"},
130  {"DHE-DSS-AES128-SHA256", "TLS-DHE-DSS-WITH-AES-128-CBC-SHA256"},
131  {"DHE-DSS-AES128-SHA", "TLS-DHE-DSS-WITH-AES-128-CBC-SHA"},
132  {"DHE-DSS-AES256-GCM-SHA384", "TLS-DHE-DSS-WITH-AES-256-GCM-SHA384"},
133  {"DHE-DSS-AES256-SHA256", "TLS-DHE-DSS-WITH-AES-256-CBC-SHA256"},
134  {"DHE-DSS-AES256-SHA", "TLS-DHE-DSS-WITH-AES-256-CBC-SHA"},
135  {"DHE-DSS-CAMELLIA128-SHA256", "TLS-DHE-DSS-WITH-CAMELLIA-128-CBC-SHA256"},
136  {"DHE-DSS-CAMELLIA128-SHA", "TLS-DHE-DSS-WITH-CAMELLIA-128-CBC-SHA"},
137  {"DHE-DSS-CAMELLIA256-SHA256", "TLS-DHE-DSS-WITH-CAMELLIA-256-CBC-SHA256"},
138  {"DHE-DSS-CAMELLIA256-SHA", "TLS-DHE-DSS-WITH-CAMELLIA-256-CBC-SHA"},
139  {"DHE-DSS-SEED-SHA", "TLS-DHE-DSS-WITH-SEED-CBC-SHA"},
140  {"DHE-RSA-AES128-GCM-SHA256", "TLS-DHE-RSA-WITH-AES-128-GCM-SHA256"},
141  {"DHE-RSA-AES128-SHA256", "TLS-DHE-RSA-WITH-AES-128-CBC-SHA256"},
142  {"DHE-RSA-AES128-SHA", "TLS-DHE-RSA-WITH-AES-128-CBC-SHA"},
143  {"DHE-RSA-AES256-GCM-SHA384", "TLS-DHE-RSA-WITH-AES-256-GCM-SHA384"},
144  {"DHE-RSA-AES256-SHA256", "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256"},
145  {"DHE-RSA-AES256-SHA", "TLS-DHE-RSA-WITH-AES-256-CBC-SHA"},
146  {"DHE-RSA-CAMELLIA128-SHA256", "TLS-DHE-RSA-WITH-CAMELLIA-128-CBC-SHA256"},
147  {"DHE-RSA-CAMELLIA128-SHA", "TLS-DHE-RSA-WITH-CAMELLIA-128-CBC-SHA"},
148  {"DHE-RSA-CAMELLIA256-SHA256", "TLS-DHE-RSA-WITH-CAMELLIA-256-CBC-SHA256"},
149  {"DHE-RSA-CAMELLIA256-SHA", "TLS-DHE-RSA-WITH-CAMELLIA-256-CBC-SHA"},
150  {"DHE-RSA-CHACHA20-POLY1305", "TLS-DHE-RSA-WITH-CHACHA20-POLY1305-SHA256"},
151  {"DHE-RSA-SEED-SHA", "TLS-DHE-RSA-WITH-SEED-CBC-SHA"},
152  {"DH-RSA-SEED-SHA", "TLS-DH-RSA-WITH-SEED-CBC-SHA"},
153  {"ECDH-ECDSA-AES128-GCM-SHA256", "TLS-ECDH-ECDSA-WITH-AES-128-GCM-SHA256"},
154  {"ECDH-ECDSA-AES128-SHA256", "TLS-ECDH-ECDSA-WITH-AES-128-CBC-SHA256"},
155  {"ECDH-ECDSA-AES128-SHA", "TLS-ECDH-ECDSA-WITH-AES-128-CBC-SHA"},
156  {"ECDH-ECDSA-AES256-GCM-SHA384", "TLS-ECDH-ECDSA-WITH-AES-256-GCM-SHA384"},
157  {"ECDH-ECDSA-AES256-SHA256", "TLS-ECDH-ECDSA-WITH-AES-256-CBC-SHA256"},
158  {"ECDH-ECDSA-AES256-SHA384", "TLS-ECDH-ECDSA-WITH-AES-256-CBC-SHA384"},
159  {"ECDH-ECDSA-AES256-SHA", "TLS-ECDH-ECDSA-WITH-AES-256-CBC-SHA"},
160  {"ECDH-ECDSA-CAMELLIA128-SHA256", "TLS-ECDH-ECDSA-WITH-CAMELLIA-128-CBC-SHA256"},
161  {"ECDH-ECDSA-CAMELLIA128-SHA", "TLS-ECDH-ECDSA-WITH-CAMELLIA-128-CBC-SHA"},
162  {"ECDH-ECDSA-CAMELLIA256-SHA256", "TLS-ECDH-ECDSA-WITH-CAMELLIA-256-CBC-SHA256"},
163  {"ECDH-ECDSA-CAMELLIA256-SHA", "TLS-ECDH-ECDSA-WITH-CAMELLIA-256-CBC-SHA"},
164  {"ECDH-ECDSA-DES-CBC3-SHA", "TLS-ECDH-ECDSA-WITH-3DES-EDE-CBC-SHA"},
165  {"ECDH-ECDSA-DES-CBC-SHA", "TLS-ECDH-ECDSA-WITH-DES-CBC-SHA"},
166  {"ECDH-ECDSA-RC4-SHA", "TLS-ECDH-ECDSA-WITH-RC4-128-SHA"},
167  {"ECDHE-ECDSA-AES128-GCM-SHA256", "TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256"},
168  {"ECDHE-ECDSA-AES128-SHA256", "TLS-ECDHE-ECDSA-WITH-AES-128-CBC-SHA256"},
169  {"ECDHE-ECDSA-AES128-SHA384", "TLS-ECDHE-ECDSA-WITH-AES-128-CBC-SHA384"},
170  {"ECDHE-ECDSA-AES128-SHA", "TLS-ECDHE-ECDSA-WITH-AES-128-CBC-SHA"},
171  {"ECDHE-ECDSA-AES256-GCM-SHA384", "TLS-ECDHE-ECDSA-WITH-AES-256-GCM-SHA384"},
172  {"ECDHE-ECDSA-AES256-SHA256", "TLS-ECDHE-ECDSA-WITH-AES-256-CBC-SHA256"},
173  {"ECDHE-ECDSA-AES256-SHA384", "TLS-ECDHE-ECDSA-WITH-AES-256-CBC-SHA384"},
174  {"ECDHE-ECDSA-AES256-SHA", "TLS-ECDHE-ECDSA-WITH-AES-256-CBC-SHA"},
175  {"ECDHE-ECDSA-CAMELLIA128-SHA256", "TLS-ECDHE-ECDSA-WITH-CAMELLIA-128-CBC-SHA256"},
176  {"ECDHE-ECDSA-CAMELLIA128-SHA", "TLS-ECDHE-ECDSA-WITH-CAMELLIA-128-CBC-SHA"},
177  {"ECDHE-ECDSA-CAMELLIA256-SHA256", "TLS-ECDHE-ECDSA-WITH-CAMELLIA-256-CBC-SHA256"},
178  {"ECDHE-ECDSA-CAMELLIA256-SHA", "TLS-ECDHE-ECDSA-WITH-CAMELLIA-256-CBC-SHA"},
179  {"ECDHE-ECDSA-CHACHA20-POLY1305", "TLS-ECDHE-ECDSA-WITH-CHACHA20-POLY1305-SHA256"},
180  {"ECDHE-ECDSA-DES-CBC3-SHA", "TLS-ECDHE-ECDSA-WITH-3DES-EDE-CBC-SHA"},
181  {"ECDHE-ECDSA-DES-CBC-SHA", "TLS-ECDHE-ECDSA-WITH-DES-CBC-SHA"},
182  {"ECDHE-ECDSA-RC4-SHA", "TLS-ECDHE-ECDSA-WITH-RC4-128-SHA"},
183  {"ECDHE-RSA-AES128-GCM-SHA256", "TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256"},
184  {"ECDHE-RSA-AES128-SHA256", "TLS-ECDHE-RSA-WITH-AES-128-CBC-SHA256"},
185  {"ECDHE-RSA-AES128-SHA384", "TLS-ECDHE-RSA-WITH-AES-128-CBC-SHA384"},
186  {"ECDHE-RSA-AES128-SHA", "TLS-ECDHE-RSA-WITH-AES-128-CBC-SHA"},
187  {"ECDHE-RSA-AES256-GCM-SHA384", "TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384"},
188  {"ECDHE-RSA-AES256-SHA256", "TLS-ECDHE-RSA-WITH-AES-256-CBC-SHA256"},
189  {"ECDHE-RSA-AES256-SHA384", "TLS-ECDHE-RSA-WITH-AES-256-CBC-SHA384"},
190  {"ECDHE-RSA-AES256-SHA", "TLS-ECDHE-RSA-WITH-AES-256-CBC-SHA"},
191  {"ECDHE-RSA-CAMELLIA128-SHA256", "TLS-ECDHE-RSA-WITH-CAMELLIA-128-CBC-SHA256"},
192  {"ECDHE-RSA-CAMELLIA128-SHA", "TLS-ECDHE-RSA-WITH-CAMELLIA-128-CBC-SHA"},
193  {"ECDHE-RSA-CAMELLIA256-SHA256", "TLS-ECDHE-RSA-WITH-CAMELLIA-256-CBC-SHA256"},
194  {"ECDHE-RSA-CAMELLIA256-SHA", "TLS-ECDHE-RSA-WITH-CAMELLIA-256-CBC-SHA"},
195  {"ECDHE-RSA-CHACHA20-POLY1305", "TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256"},
196  {"ECDHE-RSA-DES-CBC3-SHA", "TLS-ECDHE-RSA-WITH-3DES-EDE-CBC-SHA"},
197  {"ECDHE-RSA-DES-CBC-SHA", "TLS-ECDHE-RSA-WITH-DES-CBC-SHA"},
198  {"ECDHE-RSA-RC4-SHA", "TLS-ECDHE-RSA-WITH-RC4-128-SHA"},
199  {"ECDH-RSA-AES128-GCM-SHA256", "TLS-ECDH-RSA-WITH-AES-128-GCM-SHA256"},
200  {"ECDH-RSA-AES128-SHA256", "TLS-ECDH-RSA-WITH-AES-128-CBC-SHA256"},
201  {"ECDH-RSA-AES128-SHA384", "TLS-ECDH-RSA-WITH-AES-128-CBC-SHA384"},
202  {"ECDH-RSA-AES128-SHA", "TLS-ECDH-RSA-WITH-AES-128-CBC-SHA"},
203  {"ECDH-RSA-AES256-GCM-SHA384", "TLS-ECDH-RSA-WITH-AES-256-GCM-SHA384"},
204  {"ECDH-RSA-AES256-SHA256", "TLS-ECDH-RSA-WITH-AES-256-CBC-SHA256"},
205  {"ECDH-RSA-AES256-SHA384", "TLS-ECDH-RSA-WITH-AES-256-CBC-SHA384"},
206  {"ECDH-RSA-AES256-SHA", "TLS-ECDH-RSA-WITH-AES-256-CBC-SHA"},
207  {"ECDH-RSA-CAMELLIA128-SHA256", "TLS-ECDH-RSA-WITH-CAMELLIA-128-CBC-SHA256"},
208  {"ECDH-RSA-CAMELLIA128-SHA", "TLS-ECDH-RSA-WITH-CAMELLIA-128-CBC-SHA"},
209  {"ECDH-RSA-CAMELLIA256-SHA256", "TLS-ECDH-RSA-WITH-CAMELLIA-256-CBC-SHA256"},
210  {"ECDH-RSA-CAMELLIA256-SHA", "TLS-ECDH-RSA-WITH-CAMELLIA-256-CBC-SHA"},
211  {"ECDH-RSA-DES-CBC3-SHA", "TLS-ECDH-RSA-WITH-3DES-EDE-CBC-SHA"},
212  {"ECDH-RSA-DES-CBC-SHA", "TLS-ECDH-RSA-WITH-DES-CBC-SHA"},
213  {"ECDH-RSA-RC4-SHA", "TLS-ECDH-RSA-WITH-RC4-128-SHA"},
214  {"EDH-DSS-DES-CBC3-SHA", "TLS-DHE-DSS-WITH-3DES-EDE-CBC-SHA"},
215  {"EDH-DSS-DES-CBC-SHA", "TLS-DHE-DSS-WITH-DES-CBC-SHA"},
216  {"EDH-RSA-DES-CBC3-SHA", "TLS-DHE-RSA-WITH-3DES-EDE-CBC-SHA"},
217  {"EDH-RSA-DES-CBC-SHA", "TLS-DHE-RSA-WITH-DES-CBC-SHA"},
218  {"EXP-DES-CBC-SHA", "TLS-RSA-EXPORT-WITH-DES40-CBC-SHA"},
219  {"EXP-EDH-DSS-DES-CBC-SHA", "TLS-DH-DSS-EXPORT-WITH-DES40-CBC-SHA"},
220  {"EXP-EDH-RSA-DES-CBC-SHA", "TLS-DH-RSA-EXPORT-WITH-DES40-CBC-SHA"},
221  {"EXP-RC2-CBC-MD5", "TLS-RSA-EXPORT-WITH-RC2-CBC-40-MD5"},
222  {"EXP-RC4-MD5", "TLS-RSA-EXPORT-WITH-RC4-40-MD5"},
223  {"NULL-MD5", "TLS-RSA-WITH-NULL-MD5"},
224  {"NULL-SHA256", "TLS-RSA-WITH-NULL-SHA256"},
225  {"NULL-SHA", "TLS-RSA-WITH-NULL-SHA"},
226  {"PSK-3DES-EDE-CBC-SHA", "TLS-PSK-WITH-3DES-EDE-CBC-SHA"},
227  {"PSK-AES128-CBC-SHA", "TLS-PSK-WITH-AES-128-CBC-SHA"},
228  {"PSK-AES256-CBC-SHA", "TLS-PSK-WITH-AES-256-CBC-SHA"},
229  {"PSK-RC4-SHA", "TLS-PSK-WITH-RC4-128-SHA"},
230  {"RC4-MD5", "TLS-RSA-WITH-RC4-128-MD5"},
231  {"RC4-SHA", "TLS-RSA-WITH-RC4-128-SHA"},
232  {"SEED-SHA", "TLS-RSA-WITH-SEED-CBC-SHA"},
233  {"SRP-DSS-3DES-EDE-CBC-SHA", "TLS-SRP-SHA-DSS-WITH-3DES-EDE-CBC-SHA"},
234  {"SRP-DSS-AES-128-CBC-SHA", "TLS-SRP-SHA-DSS-WITH-AES-128-CBC-SHA"},
235  {"SRP-DSS-AES-256-CBC-SHA", "TLS-SRP-SHA-DSS-WITH-AES-256-CBC-SHA"},
236  {"SRP-RSA-3DES-EDE-CBC-SHA", "TLS-SRP-SHA-RSA-WITH-3DES-EDE-CBC-SHA"},
237  {"SRP-RSA-AES-128-CBC-SHA", "TLS-SRP-SHA-RSA-WITH-AES-128-CBC-SHA"},
238  {"SRP-RSA-AES-256-CBC-SHA", "TLS-SRP-SHA-RSA-WITH-AES-256-CBC-SHA"},
239 #ifdef ENABLE_CRYPTO_OPENSSL
240  /* OpenSSL-specific group names */
241  {"DEFAULT", "DEFAULT"},
242  {"ALL", "ALL"},
243  {"HIGH", "HIGH"}, {"!HIGH", "!HIGH"},
244  {"MEDIUM", "MEDIUM"}, {"!MEDIUM", "!MEDIUM"},
245  {"LOW", "LOW"}, {"!LOW", "!LOW"},
246  {"ECDH", "ECDH"}, {"!ECDH", "!ECDH"},
247  {"ECDSA", "ECDSA"}, {"!ECDSA", "!ECDSA"},
248  {"EDH", "EDH"}, {"!EDH", "!EDH"},
249  {"EXP", "EXP"}, {"!EXP", "!EXP"},
250  {"RSA", "RSA"}, {"!RSA", "!RSA"},
251  {"kRSA", "kRSA"}, {"!kRSA", "!kRSA"},
252  {"SRP", "SRP"}, {"!SRP", "!SRP"},
253 #endif
254  {NULL, NULL}
255 };
256 
268 static void
269 key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len);
270 
271 const tls_cipher_name_pair *
272 tls_get_cipher_name_pair(const char *cipher_name, size_t len)
273 {
275 
276  while (pair->openssl_name != NULL)
277  {
278  if ((strlen(pair->openssl_name) == len && 0 == memcmp(cipher_name, pair->openssl_name, len))
279  || (strlen(pair->iana_name) == len && 0 == memcmp(cipher_name, pair->iana_name, len)))
280  {
281  return pair;
282  }
283  pair++;
284  }
285 
286  /* No entry found, return NULL */
287  return NULL;
288 }
289 
297 static void
298 tls_limit_reneg_bytes(const cipher_kt_t *cipher, int *reneg_bytes)
299 {
300  if (cipher && cipher_kt_insecure(cipher))
301  {
302  if (*reneg_bytes == -1) /* Not user-specified */
303  {
304  msg(M_WARN, "WARNING: cipher with small block size in use, "
305  "reducing reneg-bytes to 64MB to mitigate SWEET32 attacks.");
306  *reneg_bytes = 64 * 1024 * 1024;
307  }
308  }
309 }
310 
311 /*
312  * Max number of bytes we will add
313  * for data structures common to both
314  * data and control channel packets.
315  * (opcode only).
316  */
317 void
319 {
320  frame_add_to_extra_frame(frame, 1); /* space for opcode */
321 }
322 
323 /*
324  * Max number of bytes we will add
325  * to control channel packet.
326  */
327 static void
328 tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame,
329  struct frame *frame)
330 {
331  /*
332  * frame->extra_frame is already initialized with tls_auth buffer requirements,
333  * if --tls-auth is enabled.
334  */
335 
336  /* inherit link MTU and extra_link from data channel */
337  frame->link_mtu = data_channel_frame->link_mtu;
338  frame->extra_link = data_channel_frame->extra_link;
339 
340  /* set extra_frame */
344 
345  /* set dynamic link MTU to cap control channel packets at 1250 bytes */
346  ASSERT(TUN_LINK_DELTA(frame) < min_int(frame->link_mtu, 1250));
347  frame->link_mtu_dynamic = min_int(frame->link_mtu, 1250) - TUN_LINK_DELTA(frame);
348 }
349 
350 void
352 {
353  tls_init_lib();
354 
355  crypto_init_lib();
356 }
357 
358 void
360 {
362  prng_uninit();
363 
364  tls_free_lib();
365 }
366 
367 /*
368  * OpenSSL library calls pem_password_callback if the
369  * private key is protected by a password.
370  */
371 
372 static struct user_pass passbuf; /* GLOBAL */
373 
374 void
375 pem_password_setup(const char *auth_file)
376 {
377  if (!strlen(passbuf.password))
378  {
380  }
381 }
382 
383 int
384 pem_password_callback(char *buf, int size, int rwflag, void *u)
385 {
386  if (buf)
387  {
388  /* prompt for password even if --askpass wasn't specified */
389  pem_password_setup(NULL);
390  strncpynt(buf, passbuf.password, size);
391  purge_user_pass(&passbuf, false);
392 
393  return strlen(buf);
394  }
395  return 0;
396 }
397 
398 /*
399  * Auth username/password handling
400  */
401 
402 static bool auth_user_pass_enabled; /* GLOBAL */
403 static struct user_pass auth_user_pass; /* GLOBAL */
404 static struct user_pass auth_token; /* GLOBAL */
405 
406 #ifdef ENABLE_MANAGEMENT
407 static char *auth_challenge; /* GLOBAL */
408 #endif
409 
410 void
411 auth_user_pass_setup(const char *auth_file, const struct static_challenge_info *sci)
412 {
413  auth_user_pass_enabled = true;
415  {
416 #ifdef ENABLE_MANAGEMENT
417  if (auth_challenge) /* dynamic challenge/response */
418  {
420  auth_file,
421  UP_TYPE_AUTH,
424  }
425  else if (sci) /* static challenge response */
426  {
428  if (sci->flags & SC_ECHO)
429  {
431  }
433  auth_file,
434  UP_TYPE_AUTH,
435  flags,
436  sci->challenge_text);
437  }
438  else
439 #endif /* ifdef ENABLE_MANAGEMENT */
441  }
442 }
443 
444 /*
445  * Disable password caching
446  */
447 void
449 {
450  passbuf.nocache = true;
451  auth_user_pass.nocache = true;
452  /* wait for push-reply, because auth-token may still need the username */
454 }
455 
456 /*
457  * Set an authentication token
458  */
459 void
460 ssl_set_auth_token(const char *token)
461 {
463 }
464 
465 /*
466  * Cleans an auth token and checks if it was active
467  */
468 bool
470 {
471  bool wasdefined = auth_token.defined;
472  purge_user_pass(&auth_token, true);
473  return wasdefined;
474 }
475 
476 /*
477  * Forget private key password AND auth-user-pass username/password.
478  */
479 void
480 ssl_purge_auth(const bool auth_user_pass_only)
481 {
482  if (!auth_user_pass_only)
483  {
484 #ifdef ENABLE_PKCS11
485  pkcs11_logout();
486 #endif
487  purge_user_pass(&passbuf, true);
488  }
490 #ifdef ENABLE_MANAGEMENT
492 #endif
493 }
494 
495 #ifdef ENABLE_MANAGEMENT
496 
497 void
499 {
501  auth_challenge = NULL;
502 }
503 
504 void
505 ssl_put_auth_challenge(const char *cr_str)
506 {
508  auth_challenge = string_alloc(cr_str, NULL);
509 }
510 
511 #endif
512 
513 /*
514  * Parse a TLS version string, returning a TLS_VER_x constant.
515  * If version string is not recognized and extra == "or-highest",
516  * return tls_version_max().
517  */
518 int
519 tls_version_parse(const char *vstr, const char *extra)
520 {
521  const int max_version = tls_version_max();
522  if (!strcmp(vstr, "1.0") && TLS_VER_1_0 <= max_version)
523  {
524  return TLS_VER_1_0;
525  }
526  else if (!strcmp(vstr, "1.1") && TLS_VER_1_1 <= max_version)
527  {
528  return TLS_VER_1_1;
529  }
530  else if (!strcmp(vstr, "1.2") && TLS_VER_1_2 <= max_version)
531  {
532  return TLS_VER_1_2;
533  }
534  else if (!strcmp(vstr, "1.3") && TLS_VER_1_3 <= max_version)
535  {
536  return TLS_VER_1_3;
537  }
538  else if (extra && !strcmp(extra, "or-highest"))
539  {
540  return max_version;
541  }
542  else
543  {
544  return TLS_VER_BAD;
545  }
546 }
547 
559 static void
560 tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file,
561  bool crl_file_inline)
562 {
563  /* if something goes wrong with stat(), we'll store 0 as mtime */
564  platform_stat_t crl_stat = {0};
565 
566  /*
567  * an inline CRL can't change at runtime, therefore there is no need to
568  * reload it. It will be reloaded upon config change + SIGHUP.
569  * Use always '1' as dummy timestamp in this case: it will trigger the
570  * first load, but will prevent any future reload.
571  */
572  if (crl_file_inline)
573  {
574  crl_stat.st_mtime = 1;
575  }
576  else if (platform_stat(crl_file, &crl_stat) < 0)
577  {
578  msg(M_WARN, "WARNING: Failed to stat CRL file, not (re)loading CRL.");
579  return;
580  }
581 
582  /*
583  * Store the CRL if this is the first time or if the file was changed since
584  * the last load.
585  * Note: Windows does not support tv_nsec.
586  */
587  if ((ssl_ctx->crl_last_size == crl_stat.st_size)
588  && (ssl_ctx->crl_last_mtime == crl_stat.st_mtime))
589  {
590  return;
591  }
592 
593  ssl_ctx->crl_last_mtime = crl_stat.st_mtime;
594  ssl_ctx->crl_last_size = crl_stat.st_size;
595  backend_tls_ctx_reload_crl(ssl_ctx, crl_file, crl_file_inline);
596 }
597 
598 /*
599  * Initialize SSL context.
600  * All files are in PEM format.
601  */
602 void
603 init_ssl(const struct options *options, struct tls_root_ctx *new_ctx)
604 {
605  ASSERT(NULL != new_ctx);
606 
607  tls_clear_error();
608 
609  if (options->tls_server)
610  {
611  tls_ctx_server_new(new_ctx);
612 
613  if (options->dh_file)
614  {
615  tls_ctx_load_dh_params(new_ctx, options->dh_file,
616  options->dh_file_inline);
617  }
618  }
619  else /* if client */
620  {
621  tls_ctx_client_new(new_ctx);
622  }
623 
624  /* Restrict allowed certificate crypto algorithms */
625  tls_ctx_set_cert_profile(new_ctx, options->tls_cert_profile);
626 
627  /* Allowable ciphers */
628  /* Since @SECLEVEL also influences loading of certificates, set the
629  * cipher restrictions before loading certificates */
630  tls_ctx_restrict_ciphers(new_ctx, options->cipher_list);
632 
633  if (!tls_ctx_set_options(new_ctx, options->ssl_flags))
634  {
635  goto err;
636  }
637 
638  if (options->pkcs12_file)
639  {
640  if (0 != tls_ctx_load_pkcs12(new_ctx, options->pkcs12_file,
641  options->pkcs12_file_inline, !options->ca_file))
642  {
643  goto err;
644  }
645  }
646 #ifdef ENABLE_PKCS11
647  else if (options->pkcs11_providers[0])
648  {
649  if (!tls_ctx_use_pkcs11(new_ctx, options->pkcs11_id_management, options->pkcs11_id))
650  {
651  msg(M_WARN, "Cannot load certificate \"%s\" using PKCS#11 interface",
652  options->pkcs11_id);
653  goto err;
654  }
655  }
656 #endif
657 #ifdef ENABLE_CRYPTOAPI
658  else if (options->cryptoapi_cert)
659  {
660  tls_ctx_load_cryptoapi(new_ctx, options->cryptoapi_cert);
661  }
662 #endif
663 #ifdef ENABLE_MANAGEMENT
664  else if (options->management_flags & MF_EXTERNAL_CERT)
665  {
666  char *cert = management_query_cert(management,
667  options->management_certificate);
668  tls_ctx_load_cert_file(new_ctx, cert, true);
669  free(cert);
670  }
671 #endif
672  else if (options->cert_file)
673  {
674  tls_ctx_load_cert_file(new_ctx, options->cert_file, options->cert_file_inline);
675  }
676 
677  if (options->priv_key_file)
678  {
679  if (0 != tls_ctx_load_priv_file(new_ctx, options->priv_key_file,
680  options->priv_key_file_inline))
681  {
682  goto err;
683  }
684  }
685 #ifdef ENABLE_MANAGEMENT
686  else if (options->management_flags & MF_EXTERNAL_KEY)
687  {
689  {
690  msg(M_WARN, "Cannot initialize mamagement-external-key");
691  goto err;
692  }
693  }
694 #endif
695 
696  if (options->ca_file || options->ca_path)
697  {
698  tls_ctx_load_ca(new_ctx, options->ca_file, options->ca_file_inline,
699  options->ca_path, options->tls_server);
700  }
701 
702  /* Load extra certificates that are part of our own certificate
703  * chain but shouldn't be included in the verify chain */
704  if (options->extra_certs_file)
705  {
707  }
708 
709  /* Check certificate notBefore and notAfter */
710  tls_ctx_check_cert_time(new_ctx);
711 
712  /* Read CRL */
713  if (options->crl_file && !(options->ssl_flags & SSLF_CRL_VERIFY_DIR))
714  {
715  tls_ctx_reload_crl(new_ctx, options->crl_file, options->crl_file_inline);
716  }
717 
718  /* Once keys and cert are loaded, load ECDH parameters */
719  if (options->tls_server)
720  {
721  tls_ctx_load_ecdh_params(new_ctx, options->ecdh_curve);
722  }
723 
724 #ifdef ENABLE_CRYPTO_MBEDTLS
725  /* Personalise the random by mixing in the certificate */
726  tls_ctx_personalise_random(new_ctx);
727 #endif
728 
729  tls_clear_error();
730  return;
731 
732 err:
733  tls_clear_error();
734  tls_ctx_free(new_ctx);
735  return;
736 }
737 
738 /*
739  * Map internal constants to ascii names.
740  */
741 static const char *
742 state_name(int state)
743 {
744  switch (state)
745  {
746  case S_UNDEF:
747  return "S_UNDEF";
748 
749  case S_INITIAL:
750  return "S_INITIAL";
751 
752  case S_PRE_START:
753  return "S_PRE_START";
754 
755  case S_START:
756  return "S_START";
757 
758  case S_SENT_KEY:
759  return "S_SENT_KEY";
760 
761  case S_GOT_KEY:
762  return "S_GOT_KEY";
763 
764  case S_ACTIVE:
765  return "S_ACTIVE";
766 
767  case S_NORMAL_OP:
768  return "S_NORMAL_OP";
769 
770  case S_ERROR:
771  return "S_ERROR";
772 
773  default:
774  return "S_???";
775  }
776 }
777 
778 static const char *
780 {
781  switch (op)
782  {
784  return "P_CONTROL_HARD_RESET_CLIENT_V1";
785 
787  return "P_CONTROL_HARD_RESET_SERVER_V1";
788 
790  return "P_CONTROL_HARD_RESET_CLIENT_V2";
791 
793  return "P_CONTROL_HARD_RESET_SERVER_V2";
794 
796  return "P_CONTROL_HARD_RESET_CLIENT_V3";
797 
799  return "P_CONTROL_SOFT_RESET_V1";
800 
801  case P_CONTROL_V1:
802  return "P_CONTROL_V1";
803 
804  case P_ACK_V1:
805  return "P_ACK_V1";
806 
807  case P_DATA_V1:
808  return "P_DATA_V1";
809 
810  case P_DATA_V2:
811  return "P_DATA_V2";
812 
813  default:
814  return "P_???";
815  }
816 }
817 
818 static const char *
820 {
821  switch (index)
822  {
823  case TM_ACTIVE:
824  return "TM_ACTIVE";
825 
826  case TM_UNTRUSTED:
827  return "TM_UNTRUSTED";
828 
829  case TM_LAME_DUCK:
830  return "TM_LAME_DUCK";
831 
832  default:
833  return "TM_???";
834  }
835 }
836 
837 /*
838  * For debugging.
839  */
840 static const char *
841 print_key_id(struct tls_multi *multi, struct gc_arena *gc)
842 {
843  int i;
844  struct buffer out = alloc_buf_gc(256, gc);
845 
846  for (i = 0; i < KEY_SCAN_SIZE; ++i)
847  {
848  struct key_state *ks = multi->key_scan[i];
849  buf_printf(&out, " [key#%d state=%s id=%d sid=%s]", i,
850  state_name(ks->state), ks->key_id,
852  }
853 
854  return BSTR(&out);
855 }
856 
857 bool
858 is_hard_reset(int op, int key_method)
859 {
860  if (!key_method || key_method == 1)
861  {
863  {
864  return true;
865  }
866  }
867 
868  if (!key_method || key_method >= 2)
869  {
872  {
873  return true;
874  }
875  }
876 
877  return false;
878 }
879 
903 static void
905 {
906  update_time();
907 
908  CLEAR(*ks);
909 
910  /*
911  * Build TLS object that reads/writes ciphertext
912  * to/from memory BIOs.
913  */
914  key_state_ssl_init(&ks->ks_ssl, &session->opt->ssl_ctx, session->opt->server,
915  session);
916 
917  /* Set control-channel initiation mode */
918  ks->initial_opcode = session->initial_opcode;
920  ks->state = S_INITIAL;
921  ks->key_id = session->key_id;
922 
923  /*
924  * key_id increments to KEY_ID_MASK then recycles back to 1.
925  * This way you know that if key_id is 0, it is the first key.
926  */
927  ++session->key_id;
928  session->key_id &= P_KEY_ID_MASK;
929  if (!session->key_id)
930  {
931  session->key_id = 1;
932  }
933 
934  /* allocate key source material object */
935  ALLOC_OBJ_CLEAR(ks->key_src, struct key_source2);
936 
937  /* allocate reliability objects */
940  ALLOC_OBJ_CLEAR(ks->rec_ack, struct reliable_ack);
941 
942  /* allocate buffers */
945  ks->ack_write_buf = alloc_buf(BUF_SIZE(&session->opt->frame));
946  reliable_init(ks->send_reliable, BUF_SIZE(&session->opt->frame),
948  ks->key_id ? false : session->opt->xmit_hold);
949  reliable_init(ks->rec_reliable, BUF_SIZE(&session->opt->frame),
951  false);
953 
954  /* init packet ID tracker */
955  if (session->opt->replay)
956  {
958  session->opt->replay_window, session->opt->replay_time, "SSL",
959  ks->key_id);
960  }
961 
962  ks->crypto_options.pid_persist = NULL;
963 
964 #ifdef MANAGEMENT_DEF_AUTH
965  ks->mda_key_id = session->opt->mda_context->mda_key_id_counter++;
966 #endif
967 }
968 
969 
983 static void
984 key_state_free(struct key_state *ks, bool clear)
985 {
986  ks->state = S_UNDEF;
987 
989 
993  free_buf(&ks->ack_write_buf);
995 
996  if (ks->send_reliable)
997  {
999  free(ks->send_reliable);
1000  }
1001 
1002  if (ks->rec_reliable)
1003  {
1005  free(ks->rec_reliable);
1006  }
1007 
1008  if (ks->rec_ack)
1009  {
1010  free(ks->rec_ack);
1011  }
1012 
1013  if (ks->key_src)
1014  {
1015  free(ks->key_src);
1016  }
1017 
1019 
1020 #ifdef PLUGIN_DEF_AUTH
1022 #endif
1023 
1024  if (clear)
1025  {
1026  secure_memzero(ks, sizeof(*ks));
1027  }
1028 }
1029 
1043 static inline bool
1045 {
1046  return (session->opt->auth_user_pass_verify_script
1048 #ifdef MANAGEMENT_DEF_AUTH
1049  || management_enable_def_auth(management)
1050 #endif
1051  );
1052 }
1053 
1054 
1075 static void
1077 {
1078  struct gc_arena gc = gc_new();
1079 
1080  dmsg(D_TLS_DEBUG, "TLS: tls_session_init: entry");
1081 
1082  CLEAR(*session);
1083 
1084  /* Set options data to point to parent's option structure */
1085  session->opt = &multi->opt;
1086 
1087  /* Randomize session # if it is 0 */
1088  while (!session_id_defined(&session->session_id))
1089  {
1090  session_id_random(&session->session_id);
1091  }
1092 
1093  /* Are we a TLS server or client? */
1094  ASSERT(session->opt->key_method >= 1);
1095  if (session->opt->key_method == 1)
1096  {
1097  session->initial_opcode = session->opt->server ?
1099  }
1100  else /* session->opt->key_method >= 2 */
1101  {
1102  if (session->opt->server)
1103  {
1105  }
1106  else
1107  {
1108  session->initial_opcode = session->opt->tls_crypt_v2 ?
1110  }
1111  }
1112 
1113  /* Initialize control channel authentication parameters */
1114  session->tls_wrap = session->opt->tls_wrap;
1115  session->tls_wrap.work = alloc_buf(BUF_SIZE(&session->opt->frame));
1116 
1117  /* initialize packet ID replay window for --tls-auth */
1119  session->opt->replay_window,
1120  session->opt->replay_time,
1121  "TLS_WRAP", session->key_id);
1122 
1123  /* load most recent packet-id to replay protect on --tls-auth */
1125  &session->tls_wrap.opt.packet_id);
1126 
1127  key_state_init(session, &session->key[KS_PRIMARY]);
1128 
1129  dmsg(D_TLS_DEBUG, "TLS: tls_session_init: new session object, sid=%s",
1130  session_id_print(&session->session_id, &gc));
1131 
1132  gc_free(&gc);
1133 }
1134 
1147 static void
1149 {
1150  tls_wrap_free(&session->tls_wrap);
1151 
1152  for (size_t i = 0; i < KS_SIZE; ++i)
1153  {
1154  key_state_free(&session->key[i], false);
1155  }
1156 
1157  if (session->common_name)
1158  {
1159  free(session->common_name);
1160  }
1161 
1162  cert_hash_free(session->cert_hash_set);
1163 
1164  if (clear)
1165  {
1166  secure_memzero(session, sizeof(*session));
1167  }
1168 }
1169 
1175 static void
1176 move_session(struct tls_multi *multi, int dest, int src, bool reinit_src)
1177 {
1178  msg(D_TLS_DEBUG_LOW, "TLS: move_session: dest=%s src=%s reinit_src=%d",
1179  session_index_name(dest),
1180  session_index_name(src),
1181  reinit_src);
1182  ASSERT(src != dest);
1183  ASSERT(src >= 0 && src < TM_SIZE);
1184  ASSERT(dest >= 0 && dest < TM_SIZE);
1185  tls_session_free(&multi->session[dest], false);
1186  multi->session[dest] = multi->session[src];
1187 
1188  if (reinit_src)
1189  {
1190  tls_session_init(multi, &multi->session[src]);
1191  }
1192  else
1193  {
1194  secure_memzero(&multi->session[src], sizeof(multi->session[src]));
1195  }
1196 
1197  dmsg(D_TLS_DEBUG, "TLS: move_session: exit");
1198 }
1199 
1200 static void
1202 {
1203  tls_session_free(session, false);
1204  tls_session_init(multi, session);
1205 }
1206 
1207 /*
1208  * Used to determine in how many seconds we should be
1209  * called again.
1210  */
1211 static inline void
1212 compute_earliest_wakeup(interval_t *earliest, interval_t seconds_from_now)
1213 {
1214  if (seconds_from_now < *earliest)
1215  {
1216  *earliest = seconds_from_now;
1217  }
1218  if (*earliest < 0)
1219  {
1220  *earliest = 0;
1221  }
1222 }
1223 
1224 /*
1225  * Return true if "lame duck" or retiring key has expired and can
1226  * no longer be used.
1227  */
1228 static inline bool
1230 {
1231  const struct key_state *lame = &session->key[KS_LAME_DUCK];
1232  if (lame->state >= S_INITIAL)
1233  {
1234  const time_t local_now = now;
1235  ASSERT(lame->must_die); /* a lame duck key must always have an expiration */
1236  if (local_now < lame->must_die)
1237  {
1238  compute_earliest_wakeup(wakeup, lame->must_die - local_now);
1239  return false;
1240  }
1241  else
1242  {
1243  return true;
1244  }
1245  }
1246  else if (lame->state == S_ERROR)
1247  {
1248  return true;
1249  }
1250  else
1251  {
1252  return false;
1253  }
1254 }
1255 
1256 struct tls_multi *
1258 {
1259  struct tls_multi *ret;
1260 
1261  ALLOC_OBJ_CLEAR(ret, struct tls_multi);
1262 
1263  /* get command line derived options */
1264  ret->opt = *tls_options;
1265 
1266  /* set up list of keys to be scanned by data channel encrypt and decrypt routines */
1267  ASSERT(SIZE(ret->key_scan) == 3);
1268  ret->key_scan[0] = &ret->session[TM_ACTIVE].key[KS_PRIMARY];
1269  ret->key_scan[1] = &ret->session[TM_ACTIVE].key[KS_LAME_DUCK];
1270  ret->key_scan[2] = &ret->session[TM_LAME_DUCK].key[KS_LAME_DUCK];
1271 
1272  /* By default not use P_DATA_V2 */
1273  ret->use_peer_id = false;
1274 
1275  return ret;
1276 }
1277 
1278 void
1279 tls_multi_init_finalize(struct tls_multi *multi, const struct frame *frame)
1280 {
1282 
1283  /* initialize the active and untrusted sessions */
1284 
1285  tls_session_init(multi, &multi->session[TM_ACTIVE]);
1286 
1287  if (!multi->opt.single_session)
1288  {
1289  tls_session_init(multi, &multi->session[TM_UNTRUSTED]);
1290  }
1291 }
1292 
1293 /*
1294  * Initialize and finalize a standalone tls-auth verification object.
1295  */
1296 
1297 struct tls_auth_standalone *
1299  struct gc_arena *gc)
1300 {
1301  struct tls_auth_standalone *tas;
1302 
1303  ALLOC_OBJ_CLEAR_GC(tas, struct tls_auth_standalone, gc);
1304 
1305  tas->tls_wrap = tls_options->tls_wrap;
1306 
1307  /*
1308  * Standalone tls-auth is in read-only mode with respect to TLS
1309  * control channel state. After we build a new client instance
1310  * object, we will process this session-initiating packet for real.
1311  */
1313 
1314  /* get initial frame parms, still need to finalize */
1315  tas->frame = tls_options->frame;
1316 
1317  return tas;
1318 }
1319 
1320 void
1322  const struct frame *frame)
1323 {
1325 }
1326 
1327 /*
1328  * Set local and remote option compatibility strings.
1329  * Used to verify compatibility of local and remote option
1330  * sets.
1331  */
1332 void
1334  const char *local,
1335  const char *remote)
1336 {
1337 #ifdef ENABLE_OCC
1338  /* initialize options string */
1339  multi->opt.local_options = local;
1340  multi->opt.remote_options = remote;
1341 #endif
1342 }
1343 
1344 /*
1345  * Cleanup a tls_multi structure and free associated memory allocations.
1346  */
1347 void
1348 tls_multi_free(struct tls_multi *multi, bool clear)
1349 {
1350  int i;
1351 
1352  ASSERT(multi);
1353 
1354  auth_set_client_reason(multi, NULL);
1355 
1356  free(multi->peer_info);
1357 
1358  if (multi->locked_cn)
1359  {
1360  free(multi->locked_cn);
1361  }
1362 
1363  if (multi->locked_username)
1364  {
1365  free(multi->locked_username);
1366  }
1367 
1369 
1370  wipe_auth_token(multi);
1371 
1372  free(multi->remote_ciphername);
1373 
1374  for (i = 0; i < TM_SIZE; ++i)
1375  {
1376  tls_session_free(&multi->session[i], false);
1377  }
1378 
1379  if (clear)
1380  {
1381  secure_memzero(multi, sizeof(*multi));
1382  }
1383 
1384  free(multi);
1385 }
1386 
1387 
1388 /*
1389  * Move a packet authentication HMAC + related fields to or from the front
1390  * of the buffer so it can be processed by encrypt/decrypt.
1391  */
1392 
1393 /*
1394  * Dependent on hmac size, opcode size, and session_id size.
1395  * Will assert if too small.
1396  */
1397 #define SWAP_BUF_SIZE 256
1398 
1399 static bool
1400 swap_hmac(struct buffer *buf, const struct crypto_options *co, bool incoming)
1401 {
1402  const struct key_ctx *ctx;
1403 
1404  ASSERT(co);
1405 
1406  ctx = (incoming ? &co->key_ctx_bi.decrypt : &co->key_ctx_bi.encrypt);
1407  ASSERT(ctx->hmac);
1408 
1409  {
1410  /* hmac + packet_id (8 bytes) */
1411  const int hmac_size = hmac_ctx_size(ctx->hmac) + packet_id_size(true);
1412 
1413  /* opcode + session_id */
1414  const int osid_size = 1 + SID_SIZE;
1415 
1416  int e1, e2;
1417  uint8_t *b = BPTR(buf);
1418  uint8_t buf1[SWAP_BUF_SIZE];
1419  uint8_t buf2[SWAP_BUF_SIZE];
1420 
1421  if (incoming)
1422  {
1423  e1 = osid_size;
1424  e2 = hmac_size;
1425  }
1426  else
1427  {
1428  e1 = hmac_size;
1429  e2 = osid_size;
1430  }
1431 
1432  ASSERT(e1 <= SWAP_BUF_SIZE && e2 <= SWAP_BUF_SIZE);
1433 
1434  if (buf->len >= e1 + e2)
1435  {
1436  memcpy(buf1, b, e1);
1437  memcpy(buf2, b + e1, e2);
1438  memcpy(b, buf2, e2);
1439  memcpy(b + e2, buf1, e1);
1440  return true;
1441  }
1442  else
1443  {
1444  return false;
1445  }
1446  }
1447 }
1448 
1449 #undef SWAP_BUF_SIZE
1450 
1451 /*
1452  * Write a control channel authentication record.
1453  */
1454 static void
1456  struct key_state *ks,
1457  struct buffer *buf,
1458  struct link_socket_actual **to_link_addr,
1459  int opcode,
1460  int max_ack,
1461  bool prepend_ack)
1462 {
1463  uint8_t header = ks->key_id | (opcode << P_OPCODE_SHIFT);
1464  struct buffer null = clear_buf();
1465 
1468  (ks->rec_ack, buf, &ks->session_id_remote, max_ack, prepend_ack));
1469 
1470  msg(D_TLS_DEBUG, "%s(): %s", __func__, packet_opcode_name(opcode));
1471 
1472  if (session->tls_wrap.mode == TLS_WRAP_AUTH
1473  || session->tls_wrap.mode == TLS_WRAP_NONE)
1474  {
1475  ASSERT(session_id_write_prepend(&session->session_id, buf));
1476  ASSERT(buf_write_prepend(buf, &header, sizeof(header)));
1477  }
1478  if (session->tls_wrap.mode == TLS_WRAP_AUTH)
1479  {
1480  /* no encryption, only write hmac */
1481  openvpn_encrypt(buf, null, &session->tls_wrap.opt);
1482  ASSERT(swap_hmac(buf, &session->tls_wrap.opt, false));
1483  }
1484  else if (session->tls_wrap.mode == TLS_WRAP_CRYPT)
1485  {
1486  ASSERT(buf_init(&session->tls_wrap.work, buf->offset));
1487  ASSERT(buf_write(&session->tls_wrap.work, &header, sizeof(header)));
1488  ASSERT(session_id_write(&session->session_id, &session->tls_wrap.work));
1489  if (!tls_crypt_wrap(buf, &session->tls_wrap.work, &session->tls_wrap.opt))
1490  {
1491  buf->len = 0;
1492  return;
1493  }
1494 
1495  if (opcode == P_CONTROL_HARD_RESET_CLIENT_V3)
1496  {
1497  if (!buf_copy(&session->tls_wrap.work,
1498  session->tls_wrap.tls_crypt_v2_wkc))
1499  {
1500  msg(D_TLS_ERRORS, "Could not append tls-crypt-v2 client key");
1501  buf->len = 0;
1502  return;
1503  }
1504  }
1505 
1506  /* Don't change the original data in buf, it's used by the reliability
1507  * layer to resend on failure. */
1508  *buf = session->tls_wrap.work;
1509  }
1510  *to_link_addr = &ks->remote_addr;
1511 }
1512 
1513 /*
1514  * Read a control channel authentication record.
1515  */
1516 static bool
1518  struct tls_wrap_ctx *ctx,
1519  const struct link_socket_actual *from,
1520  const struct tls_options *opt)
1521 {
1522  struct gc_arena gc = gc_new();
1523  bool ret = false;
1524 
1525  const uint8_t opcode = *(BPTR(buf)) >> P_OPCODE_SHIFT;
1526  if (opcode == P_CONTROL_HARD_RESET_CLIENT_V3
1527  && !tls_crypt_v2_extract_client_key(buf, ctx, opt))
1528  {
1529  msg(D_TLS_ERRORS,
1530  "TLS Error: can not extract tls-crypt-v2 client key from %s",
1531  print_link_socket_actual(from, &gc));
1532  goto cleanup;
1533  }
1534 
1535  if (ctx->mode == TLS_WRAP_AUTH)
1536  {
1537  struct buffer null = clear_buf();
1538 
1539  /* move the hmac record to the front of the packet */
1540  if (!swap_hmac(buf, &ctx->opt, true))
1541  {
1542  msg(D_TLS_ERRORS,
1543  "TLS Error: cannot locate HMAC in incoming packet from %s",
1544  print_link_socket_actual(from, &gc));
1545  gc_free(&gc);
1546  return false;
1547  }
1548 
1549  /* authenticate only (no decrypt) and remove the hmac record
1550  * from the head of the buffer */
1551  openvpn_decrypt(buf, null, &ctx->opt, NULL, BPTR(buf));
1552  if (!buf->len)
1553  {
1554  msg(D_TLS_ERRORS,
1555  "TLS Error: incoming packet authentication failed from %s",
1556  print_link_socket_actual(from, &gc));
1557  goto cleanup;
1558  }
1559 
1560  }
1561  else if (ctx->mode == TLS_WRAP_CRYPT)
1562  {
1563  struct buffer tmp = alloc_buf_gc(buf_forward_capacity_total(buf), &gc);
1564  if (!tls_crypt_unwrap(buf, &tmp, &ctx->opt))
1565  {
1566  msg(D_TLS_ERRORS, "TLS Error: tls-crypt unwrapping failed from %s",
1567  print_link_socket_actual(from, &gc));
1568  goto cleanup;
1569  }
1570  ASSERT(buf_init(buf, buf->offset));
1571  ASSERT(buf_copy(buf, &tmp));
1572  buf_clear(&tmp);
1573  }
1574  else if (ctx->tls_crypt_v2_server_key.cipher)
1575  {
1576  /* If tls-crypt-v2 is enabled, require *some* wrapping */
1577  msg(D_TLS_ERRORS, "TLS Error: could not determine wrapping from %s",
1578  print_link_socket_actual(from, &gc));
1579  /* TODO Do we want to support using tls-crypt-v2 and no control channel
1580  * wrapping at all simultaneously? That would allow server admins to
1581  * upgrade clients one-by-one without running a second instance, but we
1582  * should not enable it by default because it breaks DoS-protection.
1583  * So, add something like --tls-crypt-v2-allow-insecure-fallback ? */
1584  goto cleanup;
1585  }
1586 
1587  if (ctx->mode == TLS_WRAP_NONE || ctx->mode == TLS_WRAP_AUTH)
1588  {
1589  /* advance buffer pointer past opcode & session_id since our caller
1590  * already read it */
1591  buf_advance(buf, SID_SIZE + 1);
1592  }
1593 
1594  ret = true;
1595 cleanup:
1596  gc_free(&gc);
1597  return ret;
1598 }
1599 
1600 /*
1601  * For debugging, print contents of key_source2 structure.
1602  */
1603 
1604 static void
1606  const char *prefix)
1607 {
1608  struct gc_arena gc = gc_new();
1609 
1610  VALGRIND_MAKE_READABLE((void *)k->pre_master, sizeof(k->pre_master));
1611  VALGRIND_MAKE_READABLE((void *)k->random1, sizeof(k->random1));
1612  VALGRIND_MAKE_READABLE((void *)k->random2, sizeof(k->random2));
1613 
1615  "%s pre_master: %s",
1616  prefix,
1617  format_hex(k->pre_master, sizeof(k->pre_master), 0, &gc));
1619  "%s random1: %s",
1620  prefix,
1621  format_hex(k->random1, sizeof(k->random1), 0, &gc));
1623  "%s random2: %s",
1624  prefix,
1625  format_hex(k->random2, sizeof(k->random2), 0, &gc));
1626 
1627  gc_free(&gc);
1628 }
1629 
1630 static void
1632 {
1633  key_source_print(&k->client, "Client");
1634  key_source_print(&k->server, "Server");
1635 }
1636 
1637 /*
1638  * Generate the hash required by for the \c tls1_PRF function.
1639  *
1640  * @param md_kt Message digest to use
1641  * @param sec Secret to base the hash on
1642  * @param sec_len Length of the secret
1643  * @param seed Seed to hash
1644  * @param seed_len Length of the seed
1645  * @param out Output buffer
1646  * @param olen Length of the output buffer
1647  */
1648 static void
1649 tls1_P_hash(const md_kt_t *md_kt,
1650  const uint8_t *sec,
1651  int sec_len,
1652  const uint8_t *seed,
1653  int seed_len,
1654  uint8_t *out,
1655  int olen)
1656 {
1657  struct gc_arena gc = gc_new();
1658  int chunk;
1659  hmac_ctx_t *ctx;
1660  hmac_ctx_t *ctx_tmp;
1662  unsigned int A1_len;
1663 
1664 #ifdef ENABLE_DEBUG
1665  const int olen_orig = olen;
1666  const uint8_t *out_orig = out;
1667 #endif
1668 
1669  ctx = hmac_ctx_new();
1670  ctx_tmp = hmac_ctx_new();
1671 
1672  dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash sec: %s", format_hex(sec, sec_len, 0, &gc));
1673  dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash seed: %s", format_hex(seed, seed_len, 0, &gc));
1674 
1675  chunk = md_kt_size(md_kt);
1676  A1_len = md_kt_size(md_kt);
1677 
1678  hmac_ctx_init(ctx, sec, sec_len, md_kt);
1679  hmac_ctx_init(ctx_tmp, sec, sec_len, md_kt);
1680 
1681  hmac_ctx_update(ctx,seed,seed_len);
1682  hmac_ctx_final(ctx, A1);
1683 
1684  for (;; )
1685  {
1686  hmac_ctx_reset(ctx);
1687  hmac_ctx_reset(ctx_tmp);
1688  hmac_ctx_update(ctx,A1,A1_len);
1689  hmac_ctx_update(ctx_tmp,A1,A1_len);
1690  hmac_ctx_update(ctx,seed,seed_len);
1691 
1692  if (olen > chunk)
1693  {
1694  hmac_ctx_final(ctx, out);
1695  out += chunk;
1696  olen -= chunk;
1697  hmac_ctx_final(ctx_tmp, A1); /* calc the next A1 value */
1698  }
1699  else /* last one */
1700  {
1701  hmac_ctx_final(ctx, A1);
1702  memcpy(out,A1,olen);
1703  break;
1704  }
1705  }
1706  hmac_ctx_cleanup(ctx);
1707  hmac_ctx_free(ctx);
1708  hmac_ctx_cleanup(ctx_tmp);
1709  hmac_ctx_free(ctx_tmp);
1710  secure_memzero(A1, sizeof(A1));
1711 
1712  dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash out: %s", format_hex(out_orig, olen_orig, 0, &gc));
1713  gc_free(&gc);
1714 }
1715 
1716 /*
1717  * Use the TLS PRF function for generating data channel keys.
1718  * This code is based on the OpenSSL library.
1719  *
1720  * TLS generates keys as such:
1721  *
1722  * master_secret[48] = PRF(pre_master_secret[48], "master secret",
1723  * ClientHello.random[32] + ServerHello.random[32])
1724  *
1725  * key_block[] = PRF(SecurityParameters.master_secret[48],
1726  * "key expansion",
1727  * SecurityParameters.server_random[32] +
1728  * SecurityParameters.client_random[32]);
1729  *
1730  * Notes:
1731  *
1732  * (1) key_block contains a full set of 4 keys.
1733  * (2) The pre-master secret is generated by the client.
1734  */
1735 static void
1736 tls1_PRF(const uint8_t *label,
1737  int label_len,
1738  const uint8_t *sec,
1739  int slen,
1740  uint8_t *out1,
1741  int olen)
1742 {
1743  struct gc_arena gc = gc_new();
1744  const md_kt_t *md5 = md_kt_get("MD5");
1745  const md_kt_t *sha1 = md_kt_get("SHA1");
1746  int len,i;
1747  const uint8_t *S1,*S2;
1748  uint8_t *out2;
1749 
1750  out2 = (uint8_t *) gc_malloc(olen, false, &gc);
1751 
1752  len = slen/2;
1753  S1 = sec;
1754  S2 = &(sec[len]);
1755  len += (slen&1); /* add for odd, make longer */
1756 
1757  tls1_P_hash(md5,S1,len,label,label_len,out1,olen);
1758  tls1_P_hash(sha1,S2,len,label,label_len,out2,olen);
1759 
1760  for (i = 0; i<olen; i++)
1761  {
1762  out1[i] ^= out2[i];
1763  }
1764 
1765  secure_memzero(out2, olen);
1766 
1767  dmsg(D_SHOW_KEY_SOURCE, "tls1_PRF out[%d]: %s", olen, format_hex(out1, olen, 0, &gc));
1768 
1769  gc_free(&gc);
1770 }
1771 
1772 static void
1773 openvpn_PRF(const uint8_t *secret,
1774  int secret_len,
1775  const char *label,
1776  const uint8_t *client_seed,
1777  int client_seed_len,
1778  const uint8_t *server_seed,
1779  int server_seed_len,
1780  const struct session_id *client_sid,
1781  const struct session_id *server_sid,
1782  uint8_t *output,
1783  int output_len)
1784 {
1785  /* concatenate seed components */
1786 
1787  struct buffer seed = alloc_buf(strlen(label)
1788  + client_seed_len
1789  + server_seed_len
1790  + SID_SIZE * 2);
1791 
1792  ASSERT(buf_write(&seed, label, strlen(label)));
1793  ASSERT(buf_write(&seed, client_seed, client_seed_len));
1794  ASSERT(buf_write(&seed, server_seed, server_seed_len));
1795 
1796  if (client_sid)
1797  {
1798  ASSERT(buf_write(&seed, client_sid->id, SID_SIZE));
1799  }
1800  if (server_sid)
1801  {
1802  ASSERT(buf_write(&seed, server_sid->id, SID_SIZE));
1803  }
1804 
1805  /* compute PRF */
1806  tls1_PRF(BPTR(&seed), BLEN(&seed), secret, secret_len, output, output_len);
1807 
1808  buf_clear(&seed);
1809  free_buf(&seed);
1810 
1811  VALGRIND_MAKE_READABLE((void *)output, output_len);
1812 }
1813 
1814 /*
1815  * Using source entropy from local and remote hosts, mix into
1816  * master key.
1817  */
1818 static bool
1820  const struct key_type *key_type,
1821  const struct key_source2 *key_src,
1822  const struct session_id *client_sid,
1823  const struct session_id *server_sid,
1824  bool server)
1825 {
1826  uint8_t master[48] = { 0 };
1827  struct key2 key2 = { 0 };
1828  bool ret = false;
1829 
1830  if (key->initialized)
1831  {
1832  msg(D_TLS_ERRORS, "TLS Error: key already initialized");
1833  goto exit;
1834  }
1835 
1836  /* debugging print of source key material */
1837  key_source2_print(key_src);
1838 
1839  /* compute master secret */
1840  openvpn_PRF(key_src->client.pre_master,
1841  sizeof(key_src->client.pre_master),
1842  KEY_EXPANSION_ID " master secret",
1843  key_src->client.random1,
1844  sizeof(key_src->client.random1),
1845  key_src->server.random1,
1846  sizeof(key_src->server.random1),
1847  NULL,
1848  NULL,
1849  master,
1850  sizeof(master));
1851 
1852  /* compute key expansion */
1853  openvpn_PRF(master,
1854  sizeof(master),
1855  KEY_EXPANSION_ID " key expansion",
1856  key_src->client.random2,
1857  sizeof(key_src->client.random2),
1858  key_src->server.random2,
1859  sizeof(key_src->server.random2),
1860  client_sid,
1861  server_sid,
1862  (uint8_t *)key2.keys,
1863  sizeof(key2.keys));
1864 
1865  key2.n = 2;
1866 
1867  key2_print(&key2, key_type, "Master Encrypt", "Master Decrypt");
1868 
1869  /* check for weak keys */
1870  for (int i = 0; i < 2; ++i)
1871  {
1872  fixup_key(&key2.keys[i], key_type);
1873  if (!check_key(&key2.keys[i], key_type))
1874  {
1875  msg(D_TLS_ERRORS, "TLS Error: Bad dynamic key generated");
1876  goto exit;
1877  }
1878  }
1879 
1880  /* Initialize OpenSSL key contexts */
1881  int key_direction = server ? KEY_DIRECTION_INVERSE : KEY_DIRECTION_NORMAL;
1882  init_key_ctx_bi(key, &key2, key_direction, key_type, "Data Channel");
1883 
1884  /* Initialize implicit IVs */
1885  key_ctx_update_implicit_iv(&key->encrypt, key2.keys[(int)server].hmac,
1887  key_ctx_update_implicit_iv(&key->decrypt, key2.keys[1-(int)server].hmac,
1889 
1890  ret = true;
1891 
1892 exit:
1893  secure_memzero(&master, sizeof(master));
1894  secure_memzero(&key2, sizeof(key2));
1895 
1896  return ret;
1897 }
1898 
1899 static void
1900 key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len)
1901 {
1902  const cipher_kt_t *cipher_kt = cipher_ctx_get_cipher_kt(ctx->cipher);
1903 
1904  /* Only use implicit IV in AEAD cipher mode, where HMAC key is not used */
1905  if (cipher_kt_mode_aead(cipher_kt))
1906  {
1907  size_t impl_iv_len = 0;
1909  impl_iv_len = cipher_kt_iv_size(cipher_kt) - sizeof(packet_id_type);
1910  ASSERT(impl_iv_len <= OPENVPN_MAX_IV_LENGTH);
1911  ASSERT(impl_iv_len <= key_len);
1912  memcpy(ctx->implicit_iv, key, impl_iv_len);
1913  ctx->implicit_iv_len = impl_iv_len;
1914  }
1915 }
1916 
1923 static bool
1925 {
1926  bool ret = false;
1927  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1928  const struct session_id *client_sid = session->opt->server ?
1929  &ks->session_id_remote : &session->session_id;
1930  const struct session_id *server_sid = !session->opt->server ?
1931  &ks->session_id_remote : &session->session_id;
1932 
1933  if (!ks->authenticated) {
1934  msg(D_TLS_ERRORS, "TLS Error: key_state not authenticated");
1935  goto cleanup;
1936  }
1937 
1938  ks->crypto_options.flags = session->opt->crypto_flags;
1940  &session->opt->key_type, ks->key_src, client_sid, server_sid,
1941  session->opt->server))
1942  {
1943  msg(D_TLS_ERRORS, "TLS Error: generate_key_expansion failed");
1944  goto cleanup;
1945  }
1947  &session->opt->renegotiate_bytes);
1948 
1949  ret = true;
1950 cleanup:
1951  secure_memzero(ks->key_src, sizeof(*ks->key_src));
1952  return ret;
1953 }
1954 
1955 bool
1957  struct options *options, struct frame *frame,
1958  struct frame *frame_fragment)
1959 {
1961  {
1962  /* keys already generated, nothing to do */
1963  return true;
1964  }
1965 
1966  if (!session->opt->server
1967  && 0 != strcmp(options->ciphername, session->opt->config_ciphername)
1968  && !tls_item_in_cipher_list(options->ciphername, options->ncp_ciphers))
1969  {
1970  msg(D_TLS_ERRORS, "Error: pushed cipher not allowed - %s not in %s or %s",
1971  options->ciphername, session->opt->config_ciphername,
1972  options->ncp_ciphers);
1973  /* undo cipher push, abort connection setup */
1974  options->ciphername = session->opt->config_ciphername;
1975  return false;
1976  }
1977 
1978  if (strcmp(options->ciphername, session->opt->config_ciphername))
1979  {
1980  msg(D_HANDSHAKE, "Data Channel: using negotiated cipher '%s'",
1981  options->ciphername);
1982  if (options->keysize)
1983  {
1984  msg(D_HANDSHAKE, "NCP: overriding user-set keysize with default");
1985  options->keysize = 0;
1986  }
1987  }
1988 
1989  init_key_type(&session->opt->key_type, options->ciphername,
1990  options->authname, options->keysize, true, true);
1991 
1992  bool packet_id_long_form = cipher_kt_mode_ofb_cfb(session->opt->key_type.cipher);
1993  session->opt->crypto_flags &= ~(CO_PACKET_ID_LONG_FORM);
1994  if (packet_id_long_form)
1995  {
1997  }
1998 
1999  /* Update frame parameters: undo worst-case overhead, add actual overhead */
2001  crypto_adjust_frame_parameters(frame, &session->opt->key_type,
2002  options->replay, packet_id_long_form);
2003  frame_finalize(frame, options->ce.link_mtu_defined, options->ce.link_mtu,
2004  options->ce.tun_mtu_defined, options->ce.tun_mtu);
2005  frame_init_mssfix(frame, options);
2006  frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
2007 
2008  /*
2009  * mssfix uses data channel framing, which at this point contains
2010  * actual overhead. Fragmentation logic uses frame_fragment, which
2011  * still contains worst case overhead. Replace it with actual overhead
2012  * to prevent unneeded fragmentation.
2013  */
2014 
2015  if (frame_fragment)
2016  {
2018  crypto_adjust_frame_parameters(frame_fragment, &session->opt->key_type,
2019  options->replay, packet_id_long_form);
2020  frame_set_mtu_dynamic(frame_fragment, options->ce.fragment, SET_MTU_UPPER_BOUND);
2021  frame_print(frame_fragment, D_MTU_INFO, "Fragmentation MTU parms");
2022  }
2023 
2025 }
2026 
2027 static bool
2029  uint8_t *out,
2030  int outlen)
2031 {
2032  if (!rand_bytes(out, outlen))
2033  {
2034  msg(M_FATAL, "ERROR: Random number generator cannot obtain entropy for key generation [SSL]");
2035  }
2036  if (!buf_write(buf, out, outlen))
2037  {
2038  return false;
2039  }
2040  return true;
2041 }
2042 
2043 static bool
2045  struct buffer *buf,
2046  bool server)
2047 {
2048  struct key_source *k = &k2->client;
2049  if (server)
2050  {
2051  k = &k2->server;
2052  }
2053 
2054  CLEAR(*k);
2055 
2056  if (!server)
2057  {
2058  if (!random_bytes_to_buf(buf, k->pre_master, sizeof(k->pre_master)))
2059  {
2060  return false;
2061  }
2062  }
2063 
2064  if (!random_bytes_to_buf(buf, k->random1, sizeof(k->random1)))
2065  {
2066  return false;
2067  }
2068  if (!random_bytes_to_buf(buf, k->random2, sizeof(k->random2)))
2069  {
2070  return false;
2071  }
2072 
2073  return true;
2074 }
2075 
2076 static int
2078  struct buffer *buf,
2079  bool server)
2080 {
2081  struct key_source *k = &k2->client;
2082 
2083  if (!server)
2084  {
2085  k = &k2->server;
2086  }
2087 
2088  CLEAR(*k);
2089 
2090  if (server)
2091  {
2092  if (!buf_read(buf, k->pre_master, sizeof(k->pre_master)))
2093  {
2094  return 0;
2095  }
2096  }
2097 
2098  if (!buf_read(buf, k->random1, sizeof(k->random1)))
2099  {
2100  return 0;
2101  }
2102  if (!buf_read(buf, k->random2, sizeof(k->random2)))
2103  {
2104  return 0;
2105  }
2106 
2107  return 1;
2108 }
2109 
2110 static void
2112 {
2113  struct buffer *b;
2114 
2115  while ((b = buffer_list_peek(ks->paybuf)))
2116  {
2118  buffer_list_pop(ks->paybuf);
2119  }
2120 }
2121 
2122 /* true if no in/out acknowledgements pending */
2123 #define FULL_SYNC \
2124  (reliable_empty(ks->send_reliable) && reliable_ack_empty(ks->rec_ack))
2125 
2126 /*
2127  * Move the active key to the lame duck key and reinitialize the
2128  * active key.
2129  */
2130 static void
2132 {
2133  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2134  struct key_state *ks_lame = &session->key[KS_LAME_DUCK]; /* retiring key */
2135 
2136  ks->must_die = now + session->opt->transition_window; /* remaining lifetime of old key */
2137  key_state_free(ks_lame, false);
2138  *ks_lame = *ks;
2139 
2140  key_state_init(session, ks);
2141  ks->session_id_remote = ks_lame->session_id_remote;
2142  ks->remote_addr = ks_lame->remote_addr;
2143 }
2144 
2145 /*
2146  * Read/write strings from/to a struct buffer with a u16 length prefix.
2147  */
2148 
2149 static bool
2151 {
2152  if (!buf_write_u16(buf, 0))
2153  {
2154  return false;
2155  }
2156  return true;
2157 }
2158 
2159 static bool
2160 write_string(struct buffer *buf, const char *str, const int maxlen)
2161 {
2162  const int len = strlen(str) + 1;
2163  if (len < 1 || (maxlen >= 0 && len > maxlen))
2164  {
2165  return false;
2166  }
2167  if (!buf_write_u16(buf, len))
2168  {
2169  return false;
2170  }
2171  if (!buf_write(buf, str, len))
2172  {
2173  return false;
2174  }
2175  return true;
2176 }
2177 
2178 static bool
2179 read_string(struct buffer *buf, char *str, const unsigned int capacity)
2180 {
2181  const int len = buf_read_u16(buf);
2182  if (len < 1 || len > (int)capacity)
2183  {
2184  return false;
2185  }
2186  if (!buf_read(buf, str, len))
2187  {
2188  return false;
2189  }
2190  str[len-1] = '\0';
2191  return true;
2192 }
2193 
2194 static char *
2196 {
2197  const int len = buf_read_u16(buf);
2198  char *str;
2199 
2200  if (len < 1)
2201  {
2202  return NULL;
2203  }
2204  str = (char *) malloc(len);
2205  check_malloc_return(str);
2206  if (!buf_read(buf, str, len))
2207  {
2208  free(str);
2209  return NULL;
2210  }
2211  str[len-1] = '\0';
2212  return str;
2213 }
2214 
2215 /*
2216  * Handle the reading and writing of key data to and from
2217  * the TLS control channel (cleartext).
2218  */
2219 
2220 static bool
2222 {
2223  struct key key;
2224  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2225 
2226  ASSERT(session->opt->key_method == 1);
2227  ASSERT(buf_init(buf, 0));
2228 
2229  generate_key_random(&key, &session->opt->key_type);
2230  if (!check_key(&key, &session->opt->key_type))
2231  {
2232  msg(D_TLS_ERRORS, "TLS Error: Bad encrypting key generated");
2233  return false;
2234  }
2235 
2236  if (!write_key(&key, &session->opt->key_type, buf))
2237  {
2238  msg(D_TLS_ERRORS, "TLS Error: write_key failed");
2239  return false;
2240  }
2241 
2243  &session->opt->key_type, OPENVPN_OP_ENCRYPT,
2244  "Data Channel Encrypt");
2245  secure_memzero(&key, sizeof(key));
2246 
2247  /* send local options string */
2248  {
2249  const char *local_options = local_options_string(session);
2250  const int optlen = strlen(local_options) + 1;
2251  if (!buf_write(buf, local_options, optlen))
2252  {
2253  msg(D_TLS_ERRORS, "TLS Error: KM1 write options failed");
2254  return false;
2255  }
2256  }
2257 
2258  return true;
2259 }
2260 
2261 static bool
2263 {
2264  struct gc_arena gc = gc_new();
2265  bool ret = false;
2266 
2267  if (session->opt->push_peer_info_detail > 0)
2268  {
2269  struct env_set *es = session->opt->es;
2270  struct env_item *e;
2271  struct buffer out = alloc_buf_gc(512*3, &gc);
2272 
2273  /* push version */
2274  buf_printf(&out, "IV_VER=%s\n", PACKAGE_VERSION);
2275 
2276  /* push platform */
2277 #if defined(TARGET_LINUX)
2278  buf_printf(&out, "IV_PLAT=linux\n");
2279 #elif defined(TARGET_SOLARIS)
2280  buf_printf(&out, "IV_PLAT=solaris\n");
2281 #elif defined(TARGET_OPENBSD)
2282  buf_printf(&out, "IV_PLAT=openbsd\n");
2283 #elif defined(TARGET_DARWIN)
2284  buf_printf(&out, "IV_PLAT=mac\n");
2285 #elif defined(TARGET_NETBSD)
2286  buf_printf(&out, "IV_PLAT=netbsd\n");
2287 #elif defined(TARGET_FREEBSD)
2288  buf_printf(&out, "IV_PLAT=freebsd\n");
2289 #elif defined(TARGET_ANDROID)
2290  buf_printf(&out, "IV_PLAT=android\n");
2291 #elif defined(_WIN32)
2292  buf_printf(&out, "IV_PLAT=win\n");
2293 #endif
2294 
2295  /* support for P_DATA_V2 */
2296  buf_printf(&out, "IV_PROTO=2\n");
2297 
2298  /* support for Negotiable Crypto Parameters */
2299  if (session->opt->ncp_enabled
2300  && (session->opt->mode == MODE_SERVER || session->opt->pull))
2301  {
2302  if (tls_item_in_cipher_list("AES-128-GCM", session->opt->config_ncp_ciphers)
2303  && tls_item_in_cipher_list("AES-256-GCM", session->opt->config_ncp_ciphers))
2304  {
2305 
2306  buf_printf(&out, "IV_NCP=2\n");
2307  }
2308  buf_printf(&out, "IV_CIPHERS=%s\n", session->opt->config_ncp_ciphers);
2309  }
2310 
2311  /* push compression status */
2312 #ifdef USE_COMP
2313  comp_generate_peer_info_string(&session->opt->comp_options, &out);
2314 #endif
2315 
2316  if (session->opt->push_peer_info_detail >= 2)
2317  {
2318  /* push mac addr */
2319  struct route_gateway_info rgi;
2320  get_default_gateway(&rgi, session->opt->net_ctx);
2321  if (rgi.flags & RGI_HWADDR_DEFINED)
2322  {
2323  buf_printf(&out, "IV_HWADDR=%s\n", format_hex_ex(rgi.hwaddr, 6, 0, 1, ":", &gc));
2324  }
2325  buf_printf(&out, "IV_SSL=%s\n", get_ssl_library_version() );
2326 #if defined(_WIN32)
2327  buf_printf(&out, "IV_PLAT_VER=%s\n", win32_version_string(&gc, false));
2328 #endif
2329  }
2330 
2331  /* push env vars that begin with UV_, IV_PLAT_VER and IV_GUI_VER */
2332  for (e = es->list; e != NULL; e = e->next)
2333  {
2334  if (e->string)
2335  {
2336  if ((((strncmp(e->string, "UV_", 3)==0
2337  || strncmp(e->string, "IV_PLAT_VER=", sizeof("IV_PLAT_VER=")-1)==0)
2338  && session->opt->push_peer_info_detail >= 2)
2339  || (strncmp(e->string,"IV_GUI_VER=",sizeof("IV_GUI_VER=")-1)==0))
2340  && buf_safe(&out, strlen(e->string)+1))
2341  {
2342  buf_printf(&out, "%s\n", e->string);
2343  }
2344  }
2345  }
2346 
2347  if (!write_string(buf, BSTR(&out), -1))
2348  {
2349  goto error;
2350  }
2351  }
2352  else
2353  {
2354  if (!write_empty_string(buf)) /* no peer info */
2355  {
2356  goto error;
2357  }
2358  }
2359  ret = true;
2360 
2361 error:
2362  gc_free(&gc);
2363  return ret;
2364 }
2365 
2366 static bool
2368 {
2369  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2370 
2371  ASSERT(session->opt->key_method == 2);
2372  ASSERT(buf_init(buf, 0));
2373 
2374  /* write a uint32 0 */
2375  if (!buf_write_u32(buf, 0))
2376  {
2377  goto error;
2378  }
2379 
2380  /* write key_method + flags */
2381  if (!buf_write_u8(buf, (session->opt->key_method & KEY_METHOD_MASK)))
2382  {
2383  goto error;
2384  }
2385 
2386  /* write key source material */
2387  if (!key_source2_randomize_write(ks->key_src, buf, session->opt->server))
2388  {
2389  goto error;
2390  }
2391 
2392  /* write options string */
2393  {
2394  if (!write_string(buf, local_options_string(session), TLS_OPTIONS_LEN))
2395  {
2396  goto error;
2397  }
2398  }
2399 
2400  /* write username/password if specified */
2402  {
2403 #ifdef ENABLE_MANAGEMENT
2404  auth_user_pass_setup(session->opt->auth_user_pass_file, session->opt->sci);
2405 #else
2406  auth_user_pass_setup(session->opt->auth_user_pass_file, NULL);
2407 #endif
2408  struct user_pass *up = &auth_user_pass;
2409 
2410  /*
2411  * If we have a valid auth-token, send that instead of real
2412  * username/password
2413  */
2414  if (auth_token.defined)
2415  {
2416  up = &auth_token;
2417  }
2418 
2419  if (!write_string(buf, up->username, -1))
2420  {
2421  goto error;
2422  }
2423  else if (!write_string(buf, up->password, -1))
2424  {
2425  goto error;
2426  }
2427  /* if auth-nocache was specified, the auth_user_pass object reaches
2428  * a "complete" state only after having received the push-reply
2429  * message.
2430  *
2431  * For this reason, skip the purge operation here if no push-reply
2432  * message has been received yet.
2433  *
2434  * This normally happens upon first negotiation only.
2435  */
2437  {
2439  }
2440  }
2441  else
2442  {
2443  if (!write_empty_string(buf)) /* no username */
2444  {
2445  goto error;
2446  }
2447  if (!write_empty_string(buf)) /* no password */
2448  {
2449  goto error;
2450  }
2451  }
2452 
2453  if (!push_peer_info(buf, session))
2454  {
2455  goto error;
2456  }
2457 
2458  /* Generate tunnel keys if we're a TLS server.
2459  * If we're a p2mp server and IV_NCP >= 2 is negotiated, the first key
2460  * generation is postponed until after the pull/push, so we can process pushed
2461  * cipher directives.
2462  */
2463  if (session->opt->server && !(session->opt->ncp_enabled
2464  && session->opt->mode == MODE_SERVER && ks->key_id <= 0))
2465  {
2466  if (ks->authenticated)
2467  {
2469  {
2470  msg(D_TLS_ERRORS, "TLS Error: server generate_key_expansion failed");
2471  goto error;
2472  }
2473  }
2474  }
2475 
2476  return true;
2477 
2478 error:
2479  msg(D_TLS_ERRORS, "TLS Error: Key Method #2 write failed");
2480  secure_memzero(ks->key_src, sizeof(*ks->key_src));
2481  return false;
2482 }
2483 
2484 static bool
2486 {
2487  int status;
2488  struct key key;
2489  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2490 
2491  ASSERT(session->opt->key_method == 1);
2492 
2493  if (!session->verified)
2494  {
2495  msg(D_TLS_ERRORS,
2496  "TLS Error: Certificate verification failed (key-method 1)");
2497  goto error;
2498  }
2499 
2500  status = read_key(&key, &session->opt->key_type, buf);
2501  if (status != 1)
2502  {
2503  msg(D_TLS_ERRORS,
2504  "TLS Error: Error reading data channel key from plaintext buffer");
2505  goto error;
2506  }
2507 
2508  if (!check_key(&key, &session->opt->key_type))
2509  {
2510  msg(D_TLS_ERRORS, "TLS Error: Bad decrypting key received from peer");
2511  goto error;
2512  }
2513 
2514  if (buf->len < 1)
2515  {
2516  msg(D_TLS_ERRORS, "TLS Error: Missing options string");
2517  goto error;
2518  }
2519 
2520 #ifdef ENABLE_OCC
2521  /* compare received remote options string
2522  * with our locally computed options string */
2523  if (!session->opt->disable_occ
2524  && !options_cmp_equal_safe((char *) BPTR(buf), session->opt->remote_options, buf->len))
2525  {
2526  options_warning_safe((char *) BPTR(buf), session->opt->remote_options, buf->len);
2527  }
2528 #endif
2529 
2530  buf_clear(buf);
2531 
2533  &session->opt->key_type, OPENVPN_OP_DECRYPT,
2534  "Data Channel Decrypt");
2535  secure_memzero(&key, sizeof(key));
2536  ks->authenticated = true;
2537  return true;
2538 
2539 error:
2540  buf_clear(buf);
2541  secure_memzero(&key, sizeof(key));
2542  return false;
2543 }
2544 
2545 static bool
2546 key_method_2_read(struct buffer *buf, struct tls_multi *multi, struct tls_session *session)
2547 {
2548  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2549 
2550  int key_method_flags;
2551  bool username_status, password_status;
2552 
2553  struct gc_arena gc = gc_new();
2554  char *options;
2555  struct user_pass *up = NULL;
2556 
2557  /* allocate temporary objects */
2558  ALLOC_ARRAY_CLEAR_GC(options, char, TLS_OPTIONS_LEN, &gc);
2559 
2560  ASSERT(session->opt->key_method == 2);
2561 
2562  /* discard leading uint32 */
2563  if (!buf_advance(buf, 4))
2564  {
2565  msg(D_TLS_ERRORS, "TLS ERROR: Plaintext buffer too short (%d bytes).",
2566  buf->len);
2567  goto error;
2568  }
2569 
2570  /* get key method */
2571  key_method_flags = buf_read_u8(buf);
2572  if ((key_method_flags & KEY_METHOD_MASK) != 2)
2573  {
2574  msg(D_TLS_ERRORS,
2575  "TLS ERROR: Unknown key_method/flags=%d received from remote host",
2576  key_method_flags);
2577  goto error;
2578  }
2579 
2580  /* get key source material (not actual keys yet) */
2581  if (!key_source2_read(ks->key_src, buf, session->opt->server))
2582  {
2583  msg(D_TLS_ERRORS, "TLS Error: Error reading remote data channel key source entropy from plaintext buffer");
2584  goto error;
2585  }
2586 
2587  /* get options */
2588  if (!read_string(buf, options, TLS_OPTIONS_LEN))
2589  {
2590  msg(D_TLS_ERRORS, "TLS Error: Failed to read required OCC options string");
2591  goto error;
2592  }
2593 
2594  ks->authenticated = false;
2595 
2596  /* always extract username + password fields from buf, even if not
2597  * authenticating for it, because otherwise we can't get at the
2598  * peer_info data which follows behind
2599  */
2600  ALLOC_OBJ_CLEAR_GC(up, struct user_pass, &gc);
2601  username_status = read_string(buf, up->username, USER_PASS_LEN);
2602  password_status = read_string(buf, up->password, USER_PASS_LEN);
2603 
2604  /* get peer info from control channel */
2605  free(multi->peer_info);
2606  multi->peer_info = read_string_alloc(buf);
2607  if (multi->peer_info)
2608  {
2609  output_peer_info_env(session->opt->es, multi->peer_info);
2610  }
2611 
2612  free(multi->remote_ciphername);
2613  multi->remote_ciphername =
2614  options_string_extract_option(options, "cipher", NULL);
2615 
2616  if (!tls_peer_supports_ncp(multi->peer_info))
2617  {
2618  /* Peer does not support NCP, but leave NCP enabled if the local and
2619  * remote cipher do not match to attempt 'poor-man's NCP'.
2620  */
2621  if (multi->remote_ciphername == NULL
2622  || 0 == strcmp(multi->remote_ciphername, multi->opt.config_ciphername))
2623  {
2624  session->opt->ncp_enabled = false;
2625  }
2626  }
2627 
2628  if (tls_session_user_pass_enabled(session))
2629  {
2630  /* Perform username/password authentication */
2631  if (!username_status || !password_status)
2632  {
2633  CLEAR(*up);
2634  if (!(session->opt->ssl_flags & SSLF_AUTH_USER_PASS_OPTIONAL))
2635  {
2636  msg(D_TLS_ERRORS, "TLS Error: Auth Username/Password was not provided by peer");
2637  goto error;
2638  }
2639  }
2640 
2641  verify_user_pass(up, multi, session);
2642  }
2643  else
2644  {
2645  /* Session verification should have occurred during TLS negotiation*/
2646  if (!session->verified)
2647  {
2648  msg(D_TLS_ERRORS,
2649  "TLS Error: Certificate verification failed (key-method 2)");
2650  goto error;
2651  }
2652  ks->authenticated = true;
2653  }
2654 
2655  /* clear username and password from memory */
2656  secure_memzero(up, sizeof(*up));
2657 
2658  /* Perform final authentication checks */
2659  if (ks->authenticated)
2660  {
2661  verify_final_auth_checks(multi, session);
2662  }
2663 
2664 #ifdef ENABLE_OCC
2665  /* check options consistency */
2666  if (!session->opt->disable_occ
2667  && !options_cmp_equal(options, session->opt->remote_options))
2668  {
2669  options_warning(options, session->opt->remote_options);
2670  if (session->opt->ssl_flags & SSLF_OPT_VERIFY)
2671  {
2672  msg(D_TLS_ERRORS, "Option inconsistency warnings triggering disconnect due to --opt-verify");
2673  ks->authenticated = false;
2674  }
2675  }
2676 #endif
2677 
2678  buf_clear(buf);
2679 
2680  /*
2681  * Call OPENVPN_PLUGIN_TLS_FINAL plugin if defined, for final
2682  * veto opportunity over authentication decision.
2683  */
2685  {
2687 
2688  if (plugin_call(session->opt->plugins, OPENVPN_PLUGIN_TLS_FINAL, NULL, NULL, session->opt->es) != OPENVPN_PLUGIN_FUNC_SUCCESS)
2689  {
2690  ks->authenticated = false;
2691  }
2692 
2693  setenv_del(session->opt->es, "exported_keying_material");
2694  }
2695 
2696  /*
2697  * Generate tunnel keys if we're a client.
2698  * If --pull is enabled, the first key generation is postponed until after the
2699  * pull/push, so we can process pushed cipher directives.
2700  */
2701  if (!session->opt->server && (!session->opt->pull || ks->key_id > 0))
2702  {
2704  {
2705  msg(D_TLS_ERRORS, "TLS Error: client generate_key_expansion failed");
2706  goto error;
2707  }
2708  }
2709 
2710  gc_free(&gc);
2711  return true;
2712 
2713 error:
2714  secure_memzero(ks->key_src, sizeof(*ks->key_src));
2715  if (up)
2716  {
2717  secure_memzero(up, sizeof(*up));
2718  }
2719  buf_clear(buf);
2720  gc_free(&gc);
2721  return false;
2722 }
2723 
2724 static int
2726 {
2727  int ret = o->handshake_window;
2728  const int r2 = o->renegotiate_seconds / 2;
2729 
2730  if (o->renegotiate_seconds && r2 < ret)
2731  {
2732  ret = r2;
2733  }
2734  return ret;
2735 }
2736 
2737 /*
2738  * This is the primary routine for processing TLS stuff inside the
2739  * the main event loop. When this routine exits
2740  * with non-error status, it will set *wakeup to the number of seconds
2741  * when it wants to be called again.
2742  *
2743  * Return value is true if we have placed a packet in *to_link which we
2744  * want to send to our peer.
2745  */
2746 static bool
2747 tls_process(struct tls_multi *multi,
2748  struct tls_session *session,
2749  struct buffer *to_link,
2750  struct link_socket_actual **to_link_addr,
2751  struct link_socket_info *to_link_socket_info,
2752  interval_t *wakeup)
2753 {
2754  struct gc_arena gc = gc_new();
2755  struct buffer *buf;
2756  bool state_change = false;
2757  bool active = false;
2758  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2759  struct key_state *ks_lame = &session->key[KS_LAME_DUCK]; /* retiring key */
2760 
2761  /* Make sure we were initialized and that we're not in an error state */
2762  ASSERT(ks->state != S_UNDEF);
2763  ASSERT(ks->state != S_ERROR);
2764  ASSERT(session_id_defined(&session->session_id));
2765 
2766  /* Should we trigger a soft reset? -- new key, keeps old key for a while */
2767  if (ks->state >= S_ACTIVE
2768  && ((session->opt->renegotiate_seconds
2769  && now >= ks->established + session->opt->renegotiate_seconds)
2770  || (session->opt->renegotiate_bytes > 0
2771  && ks->n_bytes >= session->opt->renegotiate_bytes)
2772  || (session->opt->renegotiate_packets
2773  && ks->n_packets >= session->opt->renegotiate_packets)
2775  {
2776  msg(D_TLS_DEBUG_LOW, "TLS: soft reset sec=%d/%d bytes=" counter_format
2777  "/%d pkts=" counter_format "/%d",
2778  (int) (now - ks->established), session->opt->renegotiate_seconds,
2779  ks->n_bytes, session->opt->renegotiate_bytes,
2780  ks->n_packets, session->opt->renegotiate_packets);
2781  key_state_soft_reset(session);
2782  }
2783 
2784  /* Kill lame duck key transition_window seconds after primary key negotiation */
2785  if (lame_duck_must_die(session, wakeup))
2786  {
2787  key_state_free(ks_lame, true);
2788  msg(D_TLS_DEBUG_LOW, "TLS: tls_process: killed expiring key");
2789  }
2790 
2791  do
2792  {
2793  update_time();
2794 
2795  dmsg(D_TLS_DEBUG, "TLS: tls_process: chg=%d ks=%s lame=%s to_link->len=%d wakeup=%d",
2796  state_change,
2797  state_name(ks->state),
2798  state_name(ks_lame->state),
2799  to_link->len,
2800  *wakeup);
2801 
2802  state_change = false;
2803 
2804  /*
2805  * TLS activity is finished once we get to S_ACTIVE,
2806  * though we will still process acknowledgements.
2807  *
2808  * CHANGED with 2.0 -> now we may send tunnel configuration
2809  * info over the control channel.
2810  */
2811 
2812  /* Initial handshake */
2813  if (ks->state == S_INITIAL)
2814  {
2816  if (buf)
2817  {
2818  ks->must_negotiate = now + session->opt->handshake_window;
2820 
2821  /* null buffer */
2824 
2825  ks->state = S_PRE_START;
2826  state_change = true;
2827  dmsg(D_TLS_DEBUG, "TLS: Initial Handshake, sid=%s",
2828  session_id_print(&session->session_id, &gc));
2829 
2830 #ifdef ENABLE_MANAGEMENT
2832  {
2835  NULL,
2836  NULL,
2837  NULL,
2838  NULL,
2839  NULL);
2840  }
2841 #endif
2842  }
2843  }
2844 
2845  /* Are we timed out on receive? */
2846  if (now >= ks->must_negotiate)
2847  {
2848  if (ks->state < S_ACTIVE)
2849  {
2850  msg(D_TLS_ERRORS,
2851  "TLS Error: TLS key negotiation failed to occur within %d seconds (check your network connectivity)",
2852  session->opt->handshake_window);
2853  goto error;
2854  }
2855  else /* assume that ks->state == S_ACTIVE */
2856  {
2857  dmsg(D_TLS_DEBUG_MED, "STATE S_NORMAL_OP");
2858  ks->state = S_NORMAL_OP;
2859  ks->must_negotiate = 0;
2860  }
2861  }
2862 
2863  /* Wait for Initial Handshake ACK */
2864  if (ks->state == S_PRE_START && FULL_SYNC)
2865  {
2866  ks->state = S_START;
2867  state_change = true;
2868 
2869  /*
2870  * Attempt CRL reload before TLS negotiation. Won't be performed if
2871  * the file was not modified since the last reload
2872  */
2873  if (session->opt->crl_file
2874  && !(session->opt->ssl_flags & SSLF_CRL_VERIFY_DIR))
2875  {
2876  tls_ctx_reload_crl(&session->opt->ssl_ctx,
2877  session->opt->crl_file, session->opt->crl_file_inline);
2878  }
2879 
2880  /* New connection, remove any old X509 env variables */
2881  tls_x509_clear_env(session->opt->es);
2882 
2883  dmsg(D_TLS_DEBUG_MED, "STATE S_START");
2884  }
2885 
2886  /* Wait for ACK */
2887  if (((ks->state == S_GOT_KEY && !session->opt->server)
2888  || (ks->state == S_SENT_KEY && session->opt->server)))
2889  {
2890  if (FULL_SYNC)
2891  {
2892  ks->established = now;
2893  dmsg(D_TLS_DEBUG_MED, "STATE S_ACTIVE");
2895  {
2896  print_details(&ks->ks_ssl, "Control Channel:");
2897  }
2898  state_change = true;
2899  ks->state = S_ACTIVE;
2900  INCR_SUCCESS;
2901 
2902  /* Set outgoing address for data channel packets */
2903  link_socket_set_outgoing_addr(NULL, to_link_socket_info, &ks->remote_addr, session->common_name, session->opt->es);
2904 
2905  /* Flush any payload packets that were buffered before our state transitioned to S_ACTIVE */
2907 
2908 #ifdef MEASURE_TLS_HANDSHAKE_STATS
2909  show_tls_performance_stats();
2910 #endif
2911  }
2912  }
2913 
2914  /* Reliable buffer to outgoing TCP/UDP (send up to CONTROL_SEND_ACK_MAX ACKs
2915  * for previously received packets) */
2916  if (!to_link->len && reliable_can_send(ks->send_reliable))
2917  {
2918  int opcode;
2919  struct buffer b;
2920 
2921  buf = reliable_send(ks->send_reliable, &opcode);
2922  ASSERT(buf);
2923  b = *buf;
2924  INCR_SENT;
2925 
2926  write_control_auth(session, ks, &b, to_link_addr, opcode,
2927  CONTROL_SEND_ACK_MAX, true);
2928  *to_link = b;
2929  active = true;
2930  state_change = true;
2931  dmsg(D_TLS_DEBUG, "Reliable -> TCP/UDP");
2932  break;
2933  }
2934 
2935  /* Write incoming ciphertext to TLS object */
2937  if (buf)
2938  {
2939  int status = 0;
2940  if (buf->len)
2941  {
2942  status = key_state_write_ciphertext(&ks->ks_ssl, buf);
2943  if (status == -1)
2944  {
2945  msg(D_TLS_ERRORS,
2946  "TLS Error: Incoming Ciphertext -> TLS object write error");
2947  goto error;
2948  }
2949  }
2950  else
2951  {
2952  status = 1;
2953  }
2954  if (status == 1)
2955  {
2956  reliable_mark_deleted(ks->rec_reliable, buf, true);
2957  state_change = true;
2958  dmsg(D_TLS_DEBUG, "Incoming Ciphertext -> TLS");
2959  }
2960  }
2961 
2962  /* Read incoming plaintext from TLS object */
2963  buf = &ks->plaintext_read_buf;
2964  if (!buf->len)
2965  {
2966  int status;
2967 
2968  ASSERT(buf_init(buf, 0));
2970  update_time();
2971  if (status == -1)
2972  {
2973  msg(D_TLS_ERRORS, "TLS Error: TLS object -> incoming plaintext read error");
2974  goto error;
2975  }
2976  if (status == 1)
2977  {
2978  state_change = true;
2979  dmsg(D_TLS_DEBUG, "TLS -> Incoming Plaintext");
2980 
2981  /* More data may be available, wake up again asap to check. */
2982  *wakeup = 0;
2983  }
2984  }
2985 
2986  /* Send Key */
2987  buf = &ks->plaintext_write_buf;
2988  if (!buf->len && ((ks->state == S_START && !session->opt->server)
2989  || (ks->state == S_GOT_KEY && session->opt->server)))
2990  {
2991  if (session->opt->key_method == 1)
2992  {
2993  if (!key_method_1_write(buf, session))
2994  {
2995  goto error;
2996  }
2997  }
2998  else if (session->opt->key_method == 2)
2999  {
3000  if (!key_method_2_write(buf, session))
3001  {
3002  goto error;
3003  }
3004  }
3005  else
3006  {
3007  ASSERT(0);
3008  }
3009 
3010  state_change = true;
3011  dmsg(D_TLS_DEBUG_MED, "STATE S_SENT_KEY");
3012  ks->state = S_SENT_KEY;
3013  }
3014 
3015  /* Receive Key */
3016  buf = &ks->plaintext_read_buf;
3017  if (buf->len
3018  && ((ks->state == S_SENT_KEY && !session->opt->server)
3019  || (ks->state == S_START && session->opt->server)))
3020  {
3021  if (session->opt->key_method == 1)
3022  {
3023  if (!key_method_1_read(buf, session))
3024  {
3025  goto error;
3026  }
3027  }
3028  else if (session->opt->key_method == 2)
3029  {
3030  if (!key_method_2_read(buf, multi, session))
3031  {
3032  goto error;
3033  }
3034  }
3035  else
3036  {
3037  ASSERT(0);
3038  }
3039 
3040  state_change = true;
3041  dmsg(D_TLS_DEBUG_MED, "STATE S_GOT_KEY");
3042  ks->state = S_GOT_KEY;
3043  }
3044 
3045  /* Write outgoing plaintext to TLS object */
3046  buf = &ks->plaintext_write_buf;
3047  if (buf->len)
3048  {
3049  int status = key_state_write_plaintext(&ks->ks_ssl, buf);
3050  if (status == -1)
3051  {
3052  msg(D_TLS_ERRORS,
3053  "TLS ERROR: Outgoing Plaintext -> TLS object write error");
3054  goto error;
3055  }
3056  if (status == 1)
3057  {
3058  state_change = true;
3059  dmsg(D_TLS_DEBUG, "Outgoing Plaintext -> TLS");
3060  }
3061  }
3062 
3063  /* Outgoing Ciphertext to reliable buffer */
3064  if (ks->state >= S_START)
3065  {
3067  if (buf)
3068  {
3070  if (status == -1)
3071  {
3072  msg(D_TLS_ERRORS,
3073  "TLS Error: Ciphertext -> reliable TCP/UDP transport read error");
3074  goto error;
3075  }
3076  if (status == 1)
3077  {
3080  state_change = true;
3081  dmsg(D_TLS_DEBUG, "Outgoing Ciphertext -> Reliable");
3082  }
3083  }
3084  }
3085  }
3086  while (state_change);
3087 
3088  update_time();
3089 
3090  /* Send 1 or more ACKs (each received control packet gets one ACK) */
3091  if (!to_link->len && !reliable_ack_empty(ks->rec_ack))
3092  {
3093  struct buffer buf = ks->ack_write_buf;
3094  ASSERT(buf_init(&buf, FRAME_HEADROOM(&multi->opt.frame)));
3095  write_control_auth(session, ks, &buf, to_link_addr, P_ACK_V1,
3096  RELIABLE_ACK_SIZE, false);
3097  *to_link = buf;
3098  active = true;
3099  dmsg(D_TLS_DEBUG, "Dedicated ACK -> TCP/UDP");
3100  }
3101 
3102  /* When should we wake up again? */
3103  {
3104  if (ks->state >= S_INITIAL)
3105  {
3106  compute_earliest_wakeup(wakeup,
3108 
3109  if (ks->must_negotiate)
3110  {
3112  }
3113  }
3114 
3115  if (ks->established && session->opt->renegotiate_seconds)
3116  {
3117  compute_earliest_wakeup(wakeup,
3118  ks->established + session->opt->renegotiate_seconds - now);
3119  }
3120 
3121  /* prevent event-loop spinning by setting minimum wakeup of 1 second */
3122  if (*wakeup <= 0)
3123  {
3124  *wakeup = 1;
3125 
3126  /* if we had something to send to remote, but to_link was busy,
3127  * let caller know we need to be called again soon */
3128  active = true;
3129  }
3130 
3131  dmsg(D_TLS_DEBUG, "TLS: tls_process: timeout set to %d", *wakeup);
3132 
3133  gc_free(&gc);
3134  return active;
3135  }
3136 
3137 error:
3138  tls_clear_error();
3139  ks->state = S_ERROR;
3140  msg(D_TLS_ERRORS, "TLS Error: TLS handshake failed");
3141  INCR_ERROR;
3142  gc_free(&gc);
3143  return false;
3144 }
3145 
3146 /*
3147  * Called by the top-level event loop.
3148  *
3149  * Basically decides if we should call tls_process for
3150  * the active or untrusted sessions.
3151  */
3152 
3153 int
3155  struct buffer *to_link,
3156  struct link_socket_actual **to_link_addr,
3157  struct link_socket_info *to_link_socket_info,
3158  interval_t *wakeup)
3159 {
3160  struct gc_arena gc = gc_new();
3161  int i;
3162  int active = TLSMP_INACTIVE;
3163  bool error = false;
3164  int tas;
3165 
3167 
3168  tls_clear_error();
3169 
3170  /*
3171  * Process each session object having state of S_INITIAL or greater,
3172  * and which has a defined remote IP addr.
3173  */
3174 
3175  for (i = 0; i < TM_SIZE; ++i)
3176  {
3177  struct tls_session *session = &multi->session[i];
3178  struct key_state *ks = &session->key[KS_PRIMARY];
3179  struct key_state *ks_lame = &session->key[KS_LAME_DUCK];
3180 
3181  /* set initial remote address */
3182  if (i == TM_ACTIVE && ks->state == S_INITIAL
3183  && link_socket_actual_defined(&to_link_socket_info->lsa->actual))
3184  {
3185  ks->remote_addr = to_link_socket_info->lsa->actual;
3186  }
3187 
3188  dmsg(D_TLS_DEBUG,
3189  "TLS: tls_multi_process: i=%d state=%s, mysid=%s, stored-sid=%s, stored-ip=%s",
3190  i,
3191  state_name(ks->state),
3192  session_id_print(&session->session_id, &gc),
3195 
3197  {
3198  struct link_socket_actual *tla = NULL;
3199 
3200  update_time();
3201 
3202  if (tls_process(multi, session, to_link, &tla,
3203  to_link_socket_info, wakeup))
3204  {
3205  active = TLSMP_ACTIVE;
3206  }
3207 
3208  /*
3209  * If tls_process produced an outgoing packet,
3210  * return the link_socket_actual object (which
3211  * contains the outgoing address).
3212  */
3213  if (tla)
3214  {
3215  multi->to_link_addr = *tla;
3216  *to_link_addr = &multi->to_link_addr;
3217  }
3218 
3219  /*
3220  * If tls_process hits an error:
3221  * (1) If the session has an unexpired lame duck key, preserve it.
3222  * (2) Reinitialize the session.
3223  * (3) Increment soft error count
3224  */
3225  if (ks->state == S_ERROR)
3226  {
3227  ++multi->n_soft_errors;
3228 
3229  if (i == TM_ACTIVE)
3230  {
3231  error = true;
3232  }
3233 
3234  if (i == TM_ACTIVE
3235  && ks_lame->state >= S_ACTIVE
3236  && !multi->opt.single_session)
3237  {
3238  move_session(multi, TM_LAME_DUCK, TM_ACTIVE, true);
3239  }
3240  else
3241  {
3242  reset_session(multi, session);
3243  }
3244  }
3245  }
3246  }
3247 
3248  update_time();
3249 
3251 
3252  /*
3253  * If lame duck session expires, kill it.
3254  */
3255  if (lame_duck_must_die(&multi->session[TM_LAME_DUCK], wakeup))
3256  {
3257  tls_session_free(&multi->session[TM_LAME_DUCK], true);
3258  msg(D_TLS_DEBUG_LOW, "TLS: tls_multi_process: killed expiring key");
3259  }
3260 
3261  /*
3262  * If untrusted session achieves TLS authentication,
3263  * move it to active session, usurping any prior session.
3264  *
3265  * A semi-trusted session is one in which the certificate authentication
3266  * succeeded (if cert verification is enabled) but the username/password
3267  * verification failed. A semi-trusted session can forward data on the
3268  * TLS control channel but not on the tunnel channel.
3269  */
3270  if (DECRYPT_KEY_ENABLED(multi, &multi->session[TM_UNTRUSTED].key[KS_PRIMARY]))
3271  {
3272  move_session(multi, TM_ACTIVE, TM_UNTRUSTED, true);
3273  msg(D_TLS_DEBUG_LOW, "TLS: tls_multi_process: untrusted session promoted to %strusted",
3274  tas == TLS_AUTHENTICATION_SUCCEEDED ? "" : "semi-");
3275  }
3276 
3277  /*
3278  * A hard error means that TM_ACTIVE hit an S_ERROR state and that no
3279  * other key state objects are S_ACTIVE or higher.
3280  */
3281  if (error)
3282  {
3283  for (i = 0; i < (int) SIZE(multi->key_scan); ++i)
3284  {
3285  if (multi->key_scan[i]->state >= S_ACTIVE)
3286  {
3287  goto nohard;
3288  }
3289  }
3290  ++multi->n_hard_errors;
3291  }
3292 nohard:
3293 
3294 #ifdef ENABLE_DEBUG
3295  /* DEBUGGING -- flood peer with repeating connection attempts */
3296  {
3297  const int throw_level = GREMLIN_CONNECTION_FLOOD_LEVEL(multi->opt.gremlin);
3298  if (throw_level)
3299  {
3300  for (i = 0; i < (int) SIZE(multi->key_scan); ++i)
3301  {
3302  if (multi->key_scan[i]->state >= throw_level)
3303  {
3304  ++multi->n_hard_errors;
3305  ++multi->n_soft_errors;
3306  }
3307  }
3308  }
3309  }
3310 #endif
3311 
3312  perf_pop();
3313  gc_free(&gc);
3314 
3315  return (tas == TLS_AUTHENTICATION_FAILED) ? TLSMP_KILL : active;
3316 }
3317 
3318 /*
3319  * Pre and post-process the encryption & decryption buffers in order
3320  * to implement a multiplexed TLS channel over the TCP/UDP port.
3321  */
3322 
3323 /*
3324  *
3325  * When we are in TLS mode, this is the first routine which sees
3326  * an incoming packet.
3327  *
3328  * If it's a data packet, we set opt so that our caller can
3329  * decrypt it. We also give our caller the appropriate decryption key.
3330  *
3331  * If it's a control packet, we authenticate it and process it,
3332  * possibly creating a new tls_session if it represents the
3333  * first packet of a new session. For control packets, we will
3334  * also zero the size of *buf so that our caller ignores the
3335  * packet on our return.
3336  *
3337  * Note that openvpn only allows one active session at a time,
3338  * so a new session (once authenticated) will always usurp
3339  * an old session.
3340  *
3341  * Return true if input was an authenticated control channel
3342  * packet.
3343  *
3344  * If we are running in TLS thread mode, all public routines
3345  * below this point must be called with the L_TLS lock held.
3346  */
3347 
3348 bool
3350  const struct link_socket_actual *from,
3351  struct buffer *buf,
3352  struct crypto_options **opt,
3353  bool floated,
3354  const uint8_t **ad_start)
3355 {
3356  struct gc_arena gc = gc_new();
3357  bool ret = false;
3358 
3359  if (buf->len > 0)
3360  {
3361  int i;
3362  int op;
3363  int key_id;
3364 
3365  /* get opcode and key ID */
3366  {
3367  uint8_t c = *BPTR(buf);
3368  op = c >> P_OPCODE_SHIFT;
3369  key_id = c & P_KEY_ID_MASK;
3370  }
3371 
3372  if ((op == P_DATA_V1) || (op == P_DATA_V2))
3373  {
3374  /* data channel packet */
3375  for (i = 0; i < KEY_SCAN_SIZE; ++i)
3376  {
3377  struct key_state *ks = multi->key_scan[i];
3378 
3379  /*
3380  * This is the basic test of TLS state compatibility between a local OpenVPN
3381  * instance and its remote peer.
3382  *
3383  * If the test fails, it tells us that we are getting a packet from a source
3384  * which claims reference to a prior negotiated TLS session, but the local
3385  * OpenVPN instance has no memory of such a negotiation.
3386  *
3387  * It almost always occurs on UDP sessions when the passive side of the
3388  * connection is restarted without the active side restarting as well (the
3389  * passive side is the server which only listens for the connections, the
3390  * active side is the client which initiates connections).
3391  */
3392  if (DECRYPT_KEY_ENABLED(multi, ks)
3393  && key_id == ks->key_id
3394  && ks->authenticated
3395 #ifdef ENABLE_DEF_AUTH
3396  && !ks->auth_deferred
3397 #endif
3398  && (floated || link_socket_actual_match(from, &ks->remote_addr)))
3399  {
3401  {
3403  "Key %s [%d] not initialized (yet), dropping packet.",
3404  print_link_socket_actual(from, &gc), key_id);
3405  goto error_lite;
3406  }
3407 
3408  /* return appropriate data channel decrypt key in opt */
3409  *opt = &ks->crypto_options;
3410  if (op == P_DATA_V2)
3411  {
3412  *ad_start = BPTR(buf);
3413  }
3414  ASSERT(buf_advance(buf, 1));
3415  if (op == P_DATA_V1)
3416  {
3417  *ad_start = BPTR(buf);
3418  }
3419  else if (op == P_DATA_V2)
3420  {
3421  if (buf->len < 4)
3422  {
3423  msg(D_TLS_ERRORS, "Protocol error: received P_DATA_V2 from %s but length is < 4",
3424  print_link_socket_actual(from, &gc));
3425  goto error;
3426  }
3427  ASSERT(buf_advance(buf, 3));
3428  }
3429 
3430  ++ks->n_packets;
3431  ks->n_bytes += buf->len;
3433  "TLS: tls_pre_decrypt, key_id=%d, IP=%s",
3434  key_id, print_link_socket_actual(from, &gc));
3435  gc_free(&gc);
3436  return ret;
3437  }
3438  }
3439 
3440  msg(D_TLS_ERRORS,
3441  "TLS Error: local/remote TLS keys are out of sync: %s [%d]",
3442  print_link_socket_actual(from, &gc), key_id);
3443  goto error_lite;
3444  }
3445  else /* control channel packet */
3446  {
3447  bool do_burst = false;
3448  bool new_link = false;
3449  struct session_id sid; /* remote session ID */
3450 
3451  /* verify legal opcode */
3452  if (op < P_FIRST_OPCODE || op > P_LAST_OPCODE)
3453  {
3454  msg(D_TLS_ERRORS,
3455  "TLS Error: unknown opcode received from %s op=%d",
3456  print_link_socket_actual(from, &gc), op);
3457  goto error;
3458  }
3459 
3460  /* hard reset ? */
3461  if (is_hard_reset(op, 0))
3462  {
3463  /* verify client -> server or server -> client connection */
3464  if (((op == P_CONTROL_HARD_RESET_CLIENT_V1
3466  || op == P_CONTROL_HARD_RESET_CLIENT_V3) && !multi->opt.server)
3468  || op == P_CONTROL_HARD_RESET_SERVER_V2) && multi->opt.server))
3469  {
3470  msg(D_TLS_ERRORS,
3471  "TLS Error: client->client or server->server connection attempted from %s",
3472  print_link_socket_actual(from, &gc));
3473  goto error;
3474  }
3475  }
3476 
3477  /*
3478  * Authenticate Packet
3479  */
3480  dmsg(D_TLS_DEBUG, "TLS: control channel, op=%s, IP=%s",
3482 
3483  /* get remote session-id */
3484  {
3485  struct buffer tmp = *buf;
3486  buf_advance(&tmp, 1);
3487  if (!session_id_read(&sid, &tmp) || !session_id_defined(&sid))
3488  {
3489  msg(D_TLS_ERRORS,
3490  "TLS Error: session-id not found in packet from %s",
3491  print_link_socket_actual(from, &gc));
3492  goto error;
3493  }
3494  }
3495 
3496  /* use session ID to match up packet with appropriate tls_session object */
3497  for (i = 0; i < TM_SIZE; ++i)
3498  {
3499  struct tls_session *session = &multi->session[i];
3500  struct key_state *ks = &session->key[KS_PRIMARY];
3501 
3502  dmsg(D_TLS_DEBUG,
3503  "TLS: initial packet test, i=%d state=%s, mysid=%s, rec-sid=%s, rec-ip=%s, stored-sid=%s, stored-ip=%s",
3504  i,
3505  state_name(ks->state),
3506  session_id_print(&session->session_id, &gc),
3507  session_id_print(&sid, &gc),
3508  print_link_socket_actual(from, &gc),
3511 
3512  if (session_id_equal(&ks->session_id_remote, &sid))
3513  /* found a match */
3514  {
3515  if (i == TM_LAME_DUCK)
3516  {
3517  msg(D_TLS_ERRORS,
3518  "TLS ERROR: received control packet with stale session-id=%s",
3519  session_id_print(&sid, &gc));
3520  goto error;
3521  }
3522  dmsg(D_TLS_DEBUG,
3523  "TLS: found match, session[%d], sid=%s",
3524  i, session_id_print(&sid, &gc));
3525  break;
3526  }
3527  }
3528 
3529  /*
3530  * Initial packet received.
3531  */
3532 
3533  if (i == TM_SIZE && is_hard_reset(op, 0))
3534  {
3535  struct tls_session *session = &multi->session[TM_ACTIVE];
3536  struct key_state *ks = &session->key[KS_PRIMARY];
3537 
3538  if (!is_hard_reset(op, multi->opt.key_method))
3539  {
3540  msg(D_TLS_ERRORS, "TLS ERROR: initial packet local/remote key_method mismatch, local key_method=%d, op=%s",
3541  multi->opt.key_method,
3542  packet_opcode_name(op));
3543  goto error;
3544  }
3545 
3546  /*
3547  * If we have no session currently in progress, the initial packet will
3548  * open a new session in TM_ACTIVE rather than TM_UNTRUSTED.
3549  */
3551  {
3552  if (multi->opt.single_session && multi->n_sessions)
3553  {
3554  msg(D_TLS_ERRORS,
3555  "TLS Error: Cannot accept new session request from %s due to session context expire or --single-session [1]",
3556  print_link_socket_actual(from, &gc));
3557  goto error;
3558  }
3559 
3560 #ifdef ENABLE_MANAGEMENT
3561  if (management)
3562  {
3565  NULL,
3566  NULL,
3567  NULL,
3568  NULL,
3569  NULL);
3570  }
3571 #endif
3572 
3574  "TLS: Initial packet from %s, sid=%s",
3575  print_link_socket_actual(from, &gc),
3576  session_id_print(&sid, &gc));
3577 
3578  do_burst = true;
3579  new_link = true;
3580  i = TM_ACTIVE;
3581  session->untrusted_addr = *from;
3582  }
3583  }
3584 
3585  if (i == TM_SIZE && is_hard_reset(op, 0))
3586  {
3587  /*
3588  * No match with existing sessions,
3589  * probably a new session.
3590  */
3591  struct tls_session *session = &multi->session[TM_UNTRUSTED];
3592 
3593  /*
3594  * If --single-session, don't allow any hard-reset connection request
3595  * unless it the the first packet of the session.
3596  */
3597  if (multi->opt.single_session)
3598  {
3599  msg(D_TLS_ERRORS,
3600  "TLS Error: Cannot accept new session request from %s due to session context expire or --single-session [2]",
3601  print_link_socket_actual(from, &gc));
3602  goto error;
3603  }
3604 
3605  if (!is_hard_reset(op, multi->opt.key_method))
3606  {
3607  msg(D_TLS_ERRORS, "TLS ERROR: new session local/remote key_method mismatch, local key_method=%d, op=%s",
3608  multi->opt.key_method,
3609  packet_opcode_name(op));
3610  goto error;
3611  }
3612 
3613  if (!read_control_auth(buf, &session->tls_wrap, from,
3614  session->opt))
3615  {
3616  goto error;
3617  }
3618 
3619  /*
3620  * New session-initiating control packet is authenticated at this point,
3621  * assuming that the --tls-auth command line option was used.
3622  *
3623  * Without --tls-auth, we leave authentication entirely up to TLS.
3624  */
3626  "TLS: new session incoming connection from %s",
3627  print_link_socket_actual(from, &gc));
3628 
3629  new_link = true;
3630  i = TM_UNTRUSTED;
3631  session->untrusted_addr = *from;
3632  }
3633  else
3634  {
3635  struct tls_session *session = &multi->session[i];
3636  struct key_state *ks = &session->key[KS_PRIMARY];
3637 
3638  /*
3639  * Packet must belong to an existing session.
3640  */
3641  if (i != TM_ACTIVE && i != TM_UNTRUSTED)
3642  {
3643  msg(D_TLS_ERRORS,
3644  "TLS Error: Unroutable control packet received from %s (si=%d op=%s)",
3645  print_link_socket_actual(from, &gc),
3646  i,
3647  packet_opcode_name(op));
3648  goto error;
3649  }
3650 
3651  /*
3652  * Verify remote IP address
3653  */
3654  if (!new_link && !link_socket_actual_match(&ks->remote_addr, from))
3655  {
3656  msg(D_TLS_ERRORS, "TLS Error: Received control packet from unexpected IP addr: %s",
3657  print_link_socket_actual(from, &gc));
3658  goto error;
3659  }
3660 
3661  /*
3662  * Remote is requesting a key renegotiation
3663  */
3664  if (op == P_CONTROL_SOFT_RESET_V1
3665  && DECRYPT_KEY_ENABLED(multi, ks))
3666  {
3667  if (!read_control_auth(buf, &session->tls_wrap, from,
3668  session->opt))
3669  {
3670  goto error;
3671  }
3672 
3673  key_state_soft_reset(session);
3674 
3675  dmsg(D_TLS_DEBUG,
3676  "TLS: received P_CONTROL_SOFT_RESET_V1 s=%d sid=%s",
3677  i, session_id_print(&sid, &gc));
3678  }
3679  else
3680  {
3681  /*
3682  * Remote responding to our key renegotiation request?
3683  */
3684  if (op == P_CONTROL_SOFT_RESET_V1)
3685  {
3686  do_burst = true;
3687  }
3688 
3689  if (!read_control_auth(buf, &session->tls_wrap, from,
3690  session->opt))
3691  {
3692  goto error;
3693  }
3694 
3695  dmsg(D_TLS_DEBUG,
3696  "TLS: received control channel packet s#=%d sid=%s",
3697  i, session_id_print(&sid, &gc));
3698  }
3699  }
3700 
3701  /*
3702  * We have an authenticated control channel packet (if --tls-auth was set).
3703  * Now pass to our reliability layer which deals with
3704  * packet acknowledgements, retransmits, sequencing, etc.
3705  */
3706  {
3707  struct tls_session *session = &multi->session[i];
3708  struct key_state *ks = &session->key[KS_PRIMARY];
3709 
3710  /* Make sure we were initialized and that we're not in an error state */
3711  ASSERT(ks->state != S_UNDEF);
3712  ASSERT(ks->state != S_ERROR);
3713  ASSERT(session_id_defined(&session->session_id));
3714 
3715  /* Let our caller know we processed a control channel packet */
3716  ret = true;
3717 
3718  /*
3719  * Set our remote address and remote session_id
3720  */
3721  if (new_link)
3722  {
3723  ks->session_id_remote = sid;
3724  ks->remote_addr = *from;
3725  ++multi->n_sessions;
3726  }
3727  else if (!link_socket_actual_match(&ks->remote_addr, from))
3728  {
3729  msg(D_TLS_ERRORS,
3730  "TLS Error: Existing session control channel packet from unknown IP address: %s",
3731  print_link_socket_actual(from, &gc));
3732  goto error;
3733  }
3734 
3735  /*
3736  * Should we do a retransmit of all unacknowledged packets in
3737  * the send buffer? This improves the start-up efficiency of the
3738  * initial key negotiation after the 2nd peer comes online.
3739  */
3740  if (do_burst && !session->burst)
3741  {
3743  session->burst = true;
3744  }
3745 
3746  /* Check key_id */
3747  if (ks->key_id != key_id)
3748  {
3749  msg(D_TLS_ERRORS,
3750  "TLS ERROR: local/remote key IDs out of sync (%d/%d) ID: %s",
3751  ks->key_id, key_id, print_key_id(multi, &gc));
3752  goto error;
3753  }
3754 
3755  /*
3756  * Process incoming ACKs for packets we can now
3757  * delete from reliable send buffer
3758  */
3759  {
3760  /* buffers all packet IDs to delete from send_reliable */
3761  struct reliable_ack send_ack;
3762 
3763  send_ack.len = 0;
3764  if (!reliable_ack_read(&send_ack, buf, &session->session_id))
3765  {
3766  msg(D_TLS_ERRORS,
3767  "TLS Error: reading acknowledgement record from packet");
3768  goto error;
3769  }
3770  reliable_send_purge(ks->send_reliable, &send_ack);
3771  }
3772 
3773  if (op != P_ACK_V1 && reliable_can_get(ks->rec_reliable))
3774  {
3775  packet_id_type id;
3776 
3777  /* Extract the packet ID from the packet */
3778  if (reliable_ack_read_packet_id(buf, &id))
3779  {
3780  /* Avoid deadlock by rejecting packet that would de-sequentialize receive buffer */
3782  {
3783  if (reliable_not_replay(ks->rec_reliable, id))
3784  {
3785  /* Save incoming ciphertext packet to reliable buffer */
3786  struct buffer *in = reliable_get_buf(ks->rec_reliable);
3787  ASSERT(in);
3788  if (!buf_copy(in, buf))
3789  {
3791  "Incoming control channel packet too big, dropping.");
3792  goto error;
3793  }
3795  }
3796 
3797  /* Process outgoing acknowledgment for packet just received, even if it's a replay */
3799  }
3800  }
3801  }
3802  }
3803  }
3804  }
3805 
3806 done:
3807  buf->len = 0;
3808  *opt = NULL;
3809  gc_free(&gc);
3810  return ret;
3811 
3812 error:
3813  ++multi->n_soft_errors;
3814 error_lite:
3815  tls_clear_error();
3816  goto done;
3817 }
3818 
3819 /*
3820  * This function is similar to tls_pre_decrypt, except it is called
3821  * when we are in server mode and receive an initial incoming
3822  * packet. Note that we don't modify
3823  * any state in our parameter objects. The purpose is solely to
3824  * determine whether we should generate a client instance
3825  * object, in which case true is returned.
3826  *
3827  * This function is essentially the first-line HMAC firewall
3828  * on the UDP port listener in --mode server mode.
3829  */
3830 bool
3832  const struct link_socket_actual *from,
3833  const struct buffer *buf)
3834 
3835 {
3836  struct gc_arena gc = gc_new();
3837  bool ret = false;
3838 
3839  if (buf->len > 0)
3840  {
3841  int op;
3842  int key_id;
3843 
3844  /* get opcode and key ID */
3845  {
3846  uint8_t c = *BPTR(buf);
3847  op = c >> P_OPCODE_SHIFT;
3848  key_id = c & P_KEY_ID_MASK;
3849  }
3850 
3851  /* this packet is from an as-yet untrusted source, so
3852  * scrutinize carefully */
3853 
3856  {
3857  /*
3858  * This can occur due to bogus data or DoS packets.
3859  */
3861  "TLS State Error: No TLS state for client %s, opcode=%d",
3862  print_link_socket_actual(from, &gc),
3863  op);
3864  goto error;
3865  }
3866 
3867  if (key_id != 0)
3868  {
3870  "TLS State Error: Unknown key ID (%d) received from %s -- 0 was expected",
3871  key_id,
3872  print_link_socket_actual(from, &gc));
3873  goto error;
3874  }
3875 
3876  if (buf->len > EXPANDED_SIZE_DYNAMIC(&tas->frame))
3877  {
3879  "TLS State Error: Large packet (size %d) received from %s -- a packet no larger than %d bytes was expected",
3880  buf->len,
3881  print_link_socket_actual(from, &gc),
3882  EXPANDED_SIZE_DYNAMIC(&tas->frame));
3883  goto error;
3884  }
3885 
3886  {
3887  struct buffer newbuf = clone_buf(buf);
3888  struct tls_wrap_ctx tls_wrap_tmp = tas->tls_wrap;
3889  bool status;
3890 
3891  /* HMAC test, if --tls-auth was specified */
3892  status = read_control_auth(&newbuf, &tls_wrap_tmp, from, NULL);
3893  free_buf(&newbuf);
3894  free_buf(&tls_wrap_tmp.tls_crypt_v2_metadata);
3895  if (tls_wrap_tmp.cleanup_key_ctx)
3896  {
3897  free_key_ctx_bi(&tls_wrap_tmp.opt.key_ctx_bi);
3898  }
3899  if (!status)
3900  {
3901  goto error;
3902  }
3903 
3904  /*
3905  * At this point, if --tls-auth is being used, we know that
3906  * the packet has passed the HMAC test, but we don't know if
3907  * it is a replay yet. We will attempt to defeat replays
3908  * by not advancing to the S_START state until we
3909  * receive an ACK from our first reply to the client
3910  * that includes an HMAC of our randomly generated 64 bit
3911  * session ID.
3912  *
3913  * On the other hand if --tls-auth is not being used, we
3914  * will proceed to begin the TLS authentication
3915  * handshake with only cursory integrity checks having
3916  * been performed, since we will be leaving the task
3917  * of authentication solely up to TLS.
3918  */
3919 
3920  ret = true;
3921  }
3922  }
3923  gc_free(&gc);
3924  return ret;
3925 
3926 error:
3927  tls_clear_error();
3928  gc_free(&gc);
3929  return ret;
3930 }
3931 
3932 /* Choose the key with which to encrypt a data packet */
3933 void
3935  struct buffer *buf, struct crypto_options **opt)
3936 {
3937  multi->save_ks = NULL;
3938  if (buf->len > 0)
3939  {
3940  int i;
3941  struct key_state *ks_select = NULL;
3942  for (i = 0; i < KEY_SCAN_SIZE; ++i)
3943  {
3944  struct key_state *ks = multi->key_scan[i];
3945  if (ks->state >= S_ACTIVE
3946  && ks->authenticated
3948 #ifdef ENABLE_DEF_AUTH
3949  && !ks->auth_deferred
3950 #endif
3951  )
3952  {
3953  if (!ks_select)
3954  {
3955  ks_select = ks;
3956  }
3957  if (now >= ks->auth_deferred_expire)
3958  {
3959  ks_select = ks;
3960  break;
3961  }
3962  }
3963  }
3964 
3965  if (ks_select)
3966  {
3967  *opt = &ks_select->crypto_options;
3968  multi->save_ks = ks_select;
3969  dmsg(D_TLS_KEYSELECT, "TLS: tls_pre_encrypt: key_id=%d", ks_select->key_id);
3970  return;
3971  }
3972  else
3973  {
3974  struct gc_arena gc = gc_new();
3975  dmsg(D_TLS_KEYSELECT, "TLS Warning: no data channel send key available: %s",
3976  print_key_id(multi, &gc));
3977  gc_free(&gc);
3978  }
3979  }
3980 
3981  buf->len = 0;
3982  *opt = NULL;
3983 }
3984 
3985 void
3986 tls_prepend_opcode_v1(const struct tls_multi *multi, struct buffer *buf)
3987 {
3988  struct key_state *ks = multi->save_ks;
3989  uint8_t op;
3990 
3991  msg(D_TLS_DEBUG, __func__);
3992 
3993  ASSERT(ks);
3994 
3995  op = (P_DATA_V1 << P_OPCODE_SHIFT) | ks->key_id;
3996  ASSERT(buf_write_prepend(buf, &op, 1));
3997 }
3998 
3999 void
4000 tls_prepend_opcode_v2(const struct tls_multi *multi, struct buffer *buf)
4001 {
4002  struct key_state *ks = multi->save_ks;
4003  uint32_t peer;
4004 
4005  msg(D_TLS_DEBUG, __func__);
4006 
4007  ASSERT(ks);
4008 
4009  peer = htonl(((P_DATA_V2 << P_OPCODE_SHIFT) | ks->key_id) << 24
4010  | (multi->peer_id & 0xFFFFFF));
4011  ASSERT(buf_write_prepend(buf, &peer, 4));
4012 }
4013 
4014 void
4015 tls_post_encrypt(struct tls_multi *multi, struct buffer *buf)
4016 {
4017  struct key_state *ks = multi->save_ks;
4018  multi->save_ks = NULL;
4019 
4020  if (buf->len > 0)
4021  {
4022  ASSERT(ks);
4023 
4024  ++ks->n_packets;
4025  ks->n_bytes += buf->len;
4026  }
4027 }
4028 
4029 /*
4030  * Send a payload over the TLS control channel.
4031  * Called externally.
4032  */
4033 
4034 bool
4036  const uint8_t *data,
4037  int size)
4038 {
4039  struct tls_session *session;
4040  struct key_state *ks;
4041  bool ret = false;
4042 
4043  tls_clear_error();
4044 
4045  ASSERT(multi);
4046 
4047  session = &multi->session[TM_ACTIVE];
4048  ks = &session->key[KS_PRIMARY];
4049 
4050  if (ks->state >= S_ACTIVE)
4051  {
4052  if (key_state_write_plaintext_const(&ks->ks_ssl, data, size) == 1)
4053  {
4054  ret = true;
4055  }
4056  }
4057  else
4058  {
4059  if (!ks->paybuf)
4060  {
4061  ks->paybuf = buffer_list_new(0);
4062  }
4063  buffer_list_push_data(ks->paybuf, data, (size_t)size);
4064  ret = true;
4065  }
4066 
4067 
4068  tls_clear_error();
4069 
4070  return ret;
4071 }
4072 
4073 bool
4075  struct buffer *buf)
4076 {
4077  struct tls_session *session;
4078  struct key_state *ks;
4079  bool ret = false;
4080 
4081  tls_clear_error();
4082 
4083  ASSERT(multi);
4084 
4085  session = &multi->session[TM_ACTIVE];
4086  ks = &session->key[KS_PRIMARY];
4087 
4088  if (ks->state >= S_ACTIVE && BLEN(&ks->plaintext_read_buf))
4089  {
4090  if (buf_copy(buf, &ks->plaintext_read_buf))
4091  {
4092  ret = true;
4093  }
4094  ks->plaintext_read_buf.len = 0;
4095  }
4096 
4097  tls_clear_error();
4098 
4099  return ret;
4100 }
4101 
4102 void
4103 tls_update_remote_addr(struct tls_multi *multi, const struct link_socket_actual *addr)
4104 {
4105  struct gc_arena gc = gc_new();
4106  int i, j;
4107 
4108  for (i = 0; i < TM_SIZE; ++i)
4109  {
4110  struct tls_session *session = &multi->session[i];
4111 
4112  for (j = 0; j < KS_SIZE; ++j)
4113  {
4114  struct key_state *ks = &session->key[j];
4115 
4117  || link_socket_actual_match(addr, &ks->remote_addr))
4118  {
4119  continue;
4120  }
4121 
4122  dmsg(D_TLS_KEYSELECT, "TLS: tls_update_remote_addr from IP=%s to IP=%s",
4124  print_link_socket_actual(addr, &gc));
4125 
4126  ks->remote_addr = *addr;
4127  }
4128  }
4129  gc_free(&gc);
4130 }
4131 
4132 void
4133 show_available_tls_ciphers(const char *cipher_list,
4134  const char *cipher_list_tls13,
4135  const char *tls_cert_profile)
4136 {
4137  printf("Available TLS Ciphers, listed in order of preference:\n");
4138 
4139  if (tls_version_max() >= TLS_VER_1_3)
4140  {
4141  printf("\nFor TLS 1.3 and newer (--tls-ciphersuites):\n\n");
4142  show_available_tls_ciphers_list(cipher_list_tls13, tls_cert_profile, true);
4143  }
4144 
4145  printf("\nFor TLS 1.2 and older (--tls-cipher):\n\n");
4146  show_available_tls_ciphers_list(cipher_list, tls_cert_profile, false);
4147 
4148  printf("\n"
4149  "Be aware that that whether a cipher suite in this list can actually work\n"
4150  "depends on the specific setup of both peers. See the man page entries of\n"
4151  "--tls-cipher and --show-tls for more details.\n\n"
4152  );
4153 }
4154 
4155 /*
4156  * Dump a human-readable rendition of an openvpn packet
4157  * into a garbage collectable string which is returned.
4158  */
4159 const char *
4160 protocol_dump(struct buffer *buffer, unsigned int flags, struct gc_arena *gc)
4161 {
4162  struct buffer out = alloc_buf_gc(256, gc);
4163  struct buffer buf = *buffer;
4164 
4165  uint8_t c;
4166  int op;
4167  int key_id;
4168 
4169  int tls_auth_hmac_size = (flags & PD_TLS_AUTH_HMAC_SIZE_MASK);
4170 
4171  if (buf.len <= 0)
4172  {
4173  buf_printf(&out, "DATA UNDEF len=%d", buf.len);
4174  goto done;
4175  }
4176 
4177  if (!(flags & PD_TLS))
4178  {
4179  goto print_data;
4180  }
4181 
4182  /*
4183  * Initial byte (opcode)
4184  */
4185  if (!buf_read(&buf, &c, sizeof(c)))
4186  {
4187  goto done;
4188  }
4189  op = (c >> P_OPCODE_SHIFT);
4190  key_id = c & P_KEY_ID_MASK;
4191  buf_printf(&out, "%s kid=%d", packet_opcode_name(op), key_id);
4192 
4193  if ((op == P_DATA_V1) || (op == P_DATA_V2))
4194  {
4195  goto print_data;
4196  }
4197 
4198  /*
4199  * Session ID
4200  */
4201  {
4202  struct session_id sid;
4203 
4204  if (!session_id_read(&sid, &buf))
4205  {
4206  goto done;
4207  }
4208  if (flags & PD_VERBOSE)
4209  {
4210  buf_printf(&out, " sid=%s", session_id_print(&sid, gc));
4211  }
4212  }
4213 
4214  /*
4215  * tls-auth hmac + packet_id
4216  */
4217  if (tls_auth_hmac_size)
4218  {
4219  struct packet_id_net pin;
4220  uint8_t tls_auth_hmac[MAX_HMAC_KEY_LENGTH];
4221 
4222  ASSERT(tls_auth_hmac_size <= MAX_HMAC_KEY_LENGTH);
4223 
4224  if (!buf_read(&buf, tls_auth_hmac, tls_auth_hmac_size))
4225  {
4226  goto done;
4227  }
4228  if (flags & PD_VERBOSE)
4229  {
4230  buf_printf(&out, " tls_hmac=%s", format_hex(tls_auth_hmac, tls_auth_hmac_size, 0, gc));
4231  }
4232 
4233  if (!packet_id_read(&pin, &buf, true))
4234  {
4235  goto done;
4236  }
4237  buf_printf(&out, " pid=%s", packet_id_net_print(&pin, (flags & PD_VERBOSE), gc));
4238  }
4239 
4240  /*
4241  * ACK list
4242  */
4243  buf_printf(&out, " %s", reliable_ack_print(&buf, (flags & PD_VERBOSE), gc));
4244 
4245  if (op == P_ACK_V1)
4246  {
4247  goto done;
4248  }
4249 
4250  /*
4251  * Packet ID
4252  */
4253  {
4254  packet_id_type l;
4255  if (!buf_read(&buf, &l, sizeof(l)))
4256  {
4257  goto done;
4258  }
4259  l = ntohpid(l);
4261  }
4262 
4263 print_data:
4264  if (flags & PD_SHOW_DATA)
4265  {
4266  buf_printf(&out, " DATA %s", format_hex(BPTR(&buf), BLEN(&buf), 80, gc));
4267  }
4268  else
4269  {
4270  buf_printf(&out, " DATA len=%d", buf.len);
4271  }
4272 
4273 done:
4274  return BSTR(&out);
4275 }
4276 
4277 void
4279 {
4280  auth_user_pass.wait_for_push = false;
4282 }
static void tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame, struct frame *frame)
Definition: ssl.c:328
struct reliable * rec_reliable
Definition: ssl_common.h:190
#define TLSMP_ACTIVE
Definition: ssl.h:231
bool cipher_kt_mode_aead(const cipher_kt_t *cipher)
Check if the supplied cipher is a supported AEAD mode cipher.
char * management_query_cert(struct management *man, const char *cert_name)
Definition: manage.c:3671
static bool buf_write_u8(struct buffer *dest, int data)
Definition: buffer.h:713
void reliable_free(struct reliable *rel)
Free allocated memory associated with a reliable structure.
Definition: reliable.c:327
static const char * packet_opcode_name(int op)
Definition: ssl.c:779
const cipher_kt_t * cipher_ctx_get_cipher_kt(const cipher_ctx_t *ctx)
Returns the static cipher parameters for this context.
void options_warning(char *actual, const char *expected)
Definition: options.c:3867
const char * ecdh_curve
Definition: options.h:545
struct key_type key_type
Definition: ssl_common.h:247
int read_key(struct key *key, const struct key_type *kt, struct buffer *buf)
Definition: crypto.c:1659
#define EXPANDED_SIZE_DYNAMIC(f)
Definition: mtu.h:173
#define P_CONTROL_HARD_RESET_SERVER_V1
Definition: ssl.h:55
static const char * print_key_id(struct tls_multi *multi, struct gc_arena *gc)
Definition: ssl.c:841
size_t implicit_iv_len
The length of implicit_iv.
Definition: crypto.h:170
Security parameter state for processing data channel packets.
Definition: crypto.h:232
struct key_state_ssl ks_ssl
Definition: ssl_common.h:171
#define D_TLS_KEYSELECT
Definition: errlevel.h:143
const char * management_certificate
Definition: options.h:396
static void tls_wrap_free(struct tls_wrap_ctx *tls_wrap)
Free the elements of a tls_wrap_ctx structure.
Definition: ssl.h:507
#define CO_PACKET_ID_LONG_FORM
Bit-flag indicating whether to use OpenVPN&#39;s long packet ID format.
Definition: crypto.h:245
static void strncpynt(char *dest, const char *src, size_t maxlen)
Definition: buffer.h:348
#define TM_ACTIVE
Active tls_session.
Definition: ssl_common.h:464
Security parameter state of one TLS and data channel key session.
Definition: ssl_common.h:161
static void compute_earliest_wakeup(interval_t *earliest, interval_t seconds_from_now)
Definition: ssl.c:1212
bool tun_mtu_defined
Definition: options.h:108
struct buffer work
Work buffer (only for –tls-crypt)
Definition: ssl_common.h:228
unsigned int management_flags
Definition: options.h:395
struct packet_id packet_id
Current packet ID state for both sending and receiving directions.
Definition: crypto.h:238
struct key_state key[KS_SIZE]
Definition: ssl_common.h:444
#define TLSMP_INACTIVE
Definition: ssl.h:230
const char * crl_file
Definition: ssl_common.h:284
struct buffer plaintext_read_buf
Definition: ssl_common.h:185
int n_sessions
Number of sessions negotiated thus far.
Definition: ssl_common.h:526
unsigned int crypto_max_overhead(void)
Return the worst-case OpenVPN crypto overhead (in bytes)
Definition: crypto.c:732
#define S_NORMAL_OP
Normal operational key_state state.
Definition: ssl_common.h:110
void tls_auth_standalone_finalize(struct tls_auth_standalone *tas, const struct frame *frame)
Definition: ssl.c:1321
bool tls_crypt_v2
Definition: ssl_common.h:308
#define SSLF_CRL_VERIFY_DIR
Definition: ssl_common.h:349
bool options_cmp_equal_safe(char *actual, const char *expected, size_t actual_n)
Definition: options.c:4002
void free_buf(struct buffer *buf)
Definition: buffer.c:185
void ssl_put_auth_challenge(const char *cr_str)
Definition: ssl.c:505
#define D_TLS_DEBUG_LOW
Definition: errlevel.h:77
const char * cipher_list
Definition: options.h:542
static bool write_string(struct buffer *buf, const char *str, const int maxlen)
Definition: ssl.c:2160
unsigned int flags
Bit-flags determining behavior of security operation functions.
Definition: crypto.h:257
bool tls_server
Definition: options.h:527
#define TLS_MULTI_AUTH_STATUS_INTERVAL
Definition: ssl.h:100
void tls_ctx_load_cryptoapi(struct tls_root_ctx *ctx, const char *cryptoapi_cert)
Definition: ssl_openssl.c:880
char * string_alloc(const char *str, struct gc_arena *gc)
Definition: buffer.c:688
#define S_PRE_START
Waiting for the remote OpenVPN peer to acknowledge during the initial three-way handshake.
Definition: ssl_common.h:86
static const char * session_index_name(int index)
Definition: ssl.c:819
static bool session_id_equal(const struct session_id *sid1, const struct session_id *sid2)
Definition: session_id.h:48
#define D_TLS_DEBUG
Definition: errlevel.h:162
#define USER_PASS_LEN
Definition: misc.h:73
struct key keys[2]
Two unidirectional sets of key material.
Definition: crypto.h:185
struct buffer tls_crypt_v2_metadata
Received from client.
Definition: ssl_common.h:232
void init_key_ctx(struct key_ctx *ctx, const struct key *key, const struct key_type *kt, int enc, const char *prefix)
Definition: crypto.c:837
static void tls_limit_reneg_bytes(const cipher_kt_t *cipher, int *reneg_bytes)
Limit the reneg_bytes value when using a small-block (<128 bytes) cipher.
Definition: ssl.c:298
const char * cert_file
Definition: options.h:534
struct packet_id_persist * pid_persist
Persistent packet ID state for keeping state between successive OpenVPN process startups.
Definition: crypto.h:240
bool options_cmp_equal(char *actual, const char *expected)
Definition: options.c:3861
const char * tls_cert_profile
Definition: options.h:544
#define GET_USER_PASS_STATIC_CHALLENGE
Definition: misc.h:122
Container for both halves of random material to be used in key method 2 data channel key generation...
Definition: ssl_common.h:138
static bool session_id_write(const struct session_id *sid, struct buffer *buf)
Definition: session_id.h:73
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition: buffer.h:401
#define TLS_OPTIONS_LEN
Definition: ssl.h:107
void tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name)
Load Elliptic Curve Parameters, and load them into the library-specific TLS context.
Definition: ssl_openssl.c:664
int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl, const uint8_t *data, int len)
Insert plaintext data into the TLS module.
Definition: ssl_openssl.c:1985
static bool buf_advance(struct buffer *buf, int size)
Definition: buffer.h:639
void packet_id_persist_load_obj(const struct packet_id_persist *p, struct packet_id *pid)
Definition: packet_id.c:524
Packet geometry parameters.
Definition: mtu.h:93
struct key_state * save_ks
Definition: ssl_common.h:518
#define UP_TYPE_AUTH
Definition: ssl_common.h:41
#define OPENVPN_MAX_IV_LENGTH
Maximum length of an IV.
bool cipher_kt_mode_ofb_cfb(const cipher_kt_t *cipher)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
void fixup_key(struct key *key, const struct key_type *kt)
Definition: crypto.c:986
static void key_state_soft_reset(struct tls_session *session)
Definition: ssl.c:2131
static void link_socket_set_outgoing_addr(const struct buffer *buf, struct link_socket_info *info, const struct link_socket_actual *act, const char *common_name, struct env_set *es)
Definition: socket.h:985
const char * ca_path
Definition: options.h:531
struct tls_options * opt
Definition: ssl_common.h:409
time_t must_negotiate
Definition: ssl_common.h:174
#define SSLF_AUTH_USER_PASS_OPTIONAL
Definition: ssl_common.h:347
char * peer_info
Definition: ssl_common.h:556
const char * ca_file
Definition: options.h:529
const char * print_link_socket_actual(const struct link_socket_actual *act, struct gc_arena *gc)
Definition: socket.c:2892
#define RELIABLE_ACK_SIZE
The maximum number of packet IDs waiting to be acknowledged which can be stored in one reliable_ack s...
Definition: reliable.h:46
bool extra_certs_file_inline
Definition: options.h:537
bool cert_file_inline
Definition: options.h:535
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1023
struct tls_root_ctx ssl_ctx
Definition: ssl_common.h:244
bool write_key(const struct key *key, const struct key_type *kt, struct buffer *buf)
Definition: crypto.c:1626
bool verified
Definition: ssl_common.h:439
void backend_tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, bool crl_inline)
Reload the Certificate Revocation List for the SSL channel.
Definition: ssl_openssl.c:1056
void options_warning_safe(char *actual, const char *expected, size_t actual_n)
Definition: options.c:4025
static void frame_add_to_extra_frame(struct frame *frame, const unsigned int increment)
Definition: mtu.h:274
void tls_post_encrypt(struct tls_multi *multi, struct buffer *buf)
Perform some accounting for the key state used.
Definition: ssl.c:4015
static int plugin_call(const struct plugin_list *pl, const int type, const struct argv *av, struct plugin_return *pr, struct env_set *es)
Definition: plugin.h:202
#define dmsg
Definition: error.h:174
const char * auth_user_pass_verify_script
Definition: ssl_common.h:317
const char * priv_key_file
Definition: options.h:538
void session_id_random(struct session_id *sid)
Definition: session_id.c:51
Security parameter state for a single VPN tunnel.
Definition: ssl_common.h:501
#define D_TLS_ERRORS
Definition: errlevel.h:59
static bool buf_safe(const struct buffer *buf, int len)
Definition: buffer.h:541
struct buffer_list * buffer_list_new(const int max_size)
Allocate an empty buffer list of capacity max_size.
Definition: buffer.c:1201
static bool tls_process(struct tls_multi *multi, struct tls_session *session, struct buffer *to_link, struct link_socket_actual **to_link_addr, struct link_socket_info *to_link_socket_info, interval_t *wakeup)
Definition: ssl.c:2747
int pem_password_callback(char *buf, int size, int rwflag, void *u)
Callback to retrieve the user&#39;s password.
Definition: ssl.c:384
int tls_authentication_status(struct tls_multi *multi, const int latency)
Definition: ssl_verify.c:907
void key_state_ssl_free(struct key_state_ssl *ks_ssl)
Free the SSL channel part of the given key state.
Definition: ssl_openssl.c:1952
#define SID_SIZE
Definition: session_id.h:45
void tls_multi_free(struct tls_multi *multi, bool clear)
Cleanup a tls_multi structure and free associated memory allocations.
Definition: ssl.c:1348
#define SET_MTU_UPPER_BOUND
Definition: mtu.h:221
const char * auth_user_pass_file
Definition: ssl_common.h:320
int n
The number of key objects stored in the key2.keys array.
Definition: crypto.h:183
struct reliable_ack * rec_ack
Definition: ssl_common.h:191
struct buffer alloc_buf(size_t size)
Definition: buffer.c:64
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition: buffer.c:1211
const char * config_ncp_ciphers
Definition: ssl_common.h:305
int handshake_window
Definition: ssl_common.h:273
struct crypto_options opt
Crypto state.
Definition: ssl_common.h:227
void tls_update_remote_addr(struct tls_multi *multi, const struct link_socket_actual *addr)
Updates remote address in TLS sessions.
Definition: ssl.c:4103
#define S_UNDEF
Undefined state, used after a key_state is cleaned up.
Definition: ssl_common.h:78
struct cert_hash_set * cert_hash_set
Definition: ssl_common.h:433
static bool lame_duck_must_die(const struct tls_session *session, interval_t *wakeup)
Definition: ssl.c:1229
#define packet_id_format
Definition: packet_id.h:95
static void perf_pop(void)
Definition: perf.h:82
bool buf_printf(struct buffer *buf, const char *format,...)
Definition: buffer.c:245
#define D_MULTI_DROPPED
Definition: errlevel.h:102
static bool read_string(struct buffer *buf, char *str, const unsigned int capacity)
Definition: ssl.c:2179
static void perf_push(int type)
Definition: perf.h:78
void tls_prepend_opcode_v2(const struct tls_multi *multi, struct buffer *buf)
Prepend an OpenVPN data channel P_DATA_V2 header to the packet.
Definition: ssl.c:4000
static bool read_control_auth(struct buffer *buf, struct tls_wrap_ctx *ctx, const struct link_socket_actual *from, const struct tls_options *opt)
Definition: ssl.c:1517
time_t crl_last_mtime
CRL last modification time.
Definition: ssl_mbedtls.h:101
static char * format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
Definition: buffer.h:526
bool defined
Definition: misc.h:65
#define SIZE(x)
Definition: basic.h:30
#define D_MTU_INFO
Definition: errlevel.h:106
static int buf_read_u8(struct buffer *buf)
Definition: buffer.h:812
const char * win32_version_string(struct gc_arena *gc, bool add_name)
Definition: win32.c:1441
int cipher_kt_iv_size(const cipher_kt_t *cipher_kt)
Returns the size of the IV used by the cipher, in bytes, or 0 if no IV is used.
bool cipher_kt_insecure(const cipher_kt_t *cipher)
Returns true if we consider this cipher to be insecure.
void key_state_export_keying_material(struct key_state_ssl *ks_ssl, struct tls_session *session) __attribute__((nonnull))
Keying Material Exporters [RFC 5705] allows additional keying material to be derived from existing TL...
Definition: ssl_openssl.c:162
const char * protocol_dump(struct buffer *buffer, unsigned int flags, struct gc_arena *gc)
Definition: ssl.c:4160
#define ENABLE_DEF_AUTH
Definition: config-msvc.h:5
const char * cryptoapi_cert
Definition: options.h:571
#define TLS_VER_1_1
Definition: ssl_backend.h:115
static void flush_payload_buffer(struct key_state *ks)
Definition: ssl.c:2111
bool packet_id_read(struct packet_id_net *pin, struct buffer *buf, bool long_form)
Definition: packet_id.c:301
struct buffer * reliable_get_buf(struct reliable *rel)
Get the buffer of a free reliable entry in which to store a packet.
Definition: reliable.c:473
static bool push_peer_info(struct buffer *buf, struct tls_session *session)
Definition: ssl.c:2262
#define DECRYPT_KEY_ENABLED(multi, ks)
Check whether the ks key_state is ready to receive data channel packets.
Definition: ssl_verify.h:89
int key_state_write_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Insert a plaintext buffer into the TLS module.
Definition: ssl_openssl.c:1967
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:161
#define TLS_VER_BAD
Parse a TLS version specifier.
Definition: ssl_backend.h:112
#define ASSERT(x)
Definition: error.h:221
uint8_t random1[32]
Seed used for master secret generation, provided by both client and server.
Definition: ssl_common.h:125
void tls_free_lib(void)
Free any global SSL library-specific data structures.
Definition: ssl_openssl.c:91
bool check_key(struct key *key, const struct key_type *kt)
Definition: crypto.c:947
#define OPENVPN_STATE_AUTH
Definition: manage.h:495
static bool check_debug_level(unsigned int level)
Definition: error.h:245
unsigned int crypto_flags
Definition: ssl_common.h:298
bool tls_pre_decrypt_lite(const struct tls_auth_standalone *tas, 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.c:3831
#define OPENVPN_PLUGIN_FUNC_SUCCESS
bool authenticated
Definition: ssl_common.h:201
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer...
Definition: ssl_common.h:589
const char * get_ssl_library_version(void)
return a pointer to a static memory area containing the name and version number of the SSL library in...
Definition: ssl_openssl.c:2248
static struct user_pass auth_user_pass
Definition: ssl.c:403
void ssl_set_auth_token(const char *token)
Definition: ssl.c:460
const char * config_ciphername
Definition: ssl_common.h:304
bool tls_session_update_crypto_params(struct tls_session *session, struct options *options, struct frame *frame, struct frame *frame_fragment)
Update TLS session crypto parameters (cipher and auth) and derive data channel keys based on the supp...
Definition: ssl.c:1956
const char * pkcs12_file
Definition: options.h:540
struct reliable * send_reliable
Definition: ssl_common.h:189
int renegotiate_bytes
Definition: ssl_common.h:275
void hmac_ctx_cleanup(hmac_ctx_t *ctx)
mbedtls_md_context_t hmac_ctx_t
Generic HMAC context.
#define SSLF_OPT_VERIFY
Definition: ssl_common.h:348
bool link_mtu_defined
Definition: options.h:112
char username[USER_PASS_LEN]
Definition: misc.h:75
uint8_t hmac[MAX_HMAC_KEY_LENGTH]
Key material for HMAC operations.
Definition: crypto.h:155
void packet_id_free(struct packet_id *p)
Definition: packet_id.c:101
bool is_hard_reset(int op, int key_method)
Given a key_method, return true if opcode represents the required form of hard_reset.
Definition: ssl.c:858
static bool buf_read(struct buffer *src, void *dest, int size)
Definition: buffer.h:800
#define MODE_SERVER
Definition: options.h:205
void tls_pre_encrypt(struct tls_multi *multi, struct buffer *buf, struct crypto_options **opt)
Choose the appropriate security parameters with which to process an outgoing packet.
Definition: ssl.c:3934
bool use_peer_id
Definition: ssl_common.h:582
bool initialized
Definition: crypto.h:225
int offset
Offset in bytes of the actual content within the allocated memory.
Definition: buffer.h:64
void tls_ctx_client_new(struct tls_root_ctx *ctx)
Initialises a library-specific TLS context for a client.
Definition: ssl_openssl.c:126
#define TLS_VER_1_2
Definition: ssl_backend.h:116
struct link_socket_actual to_link_addr
Definition: ssl_common.h:524
bool tls_peer_supports_ncp(const char *peer_info)
Returns whether the client supports NCP either by announcing IV_NCP>=2 or the IV_CIPHERS list...
Definition: ssl_ncp.c:77
#define OPENVPN_AEAD_MIN_IV_LEN
Minimal IV length for AEAD mode ciphers (in bytes): 4-byte packet id + 8 bytes implicit IV...
Definition: crypto.h:268
void reliable_mark_active_outgoing(struct reliable *rel, struct buffer *buf, int opcode)
Mark the reliable entry associated with the given buffer as active outgoing.
Definition: reliable.c:696
list flags
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
void tls_ctx_set_cert_profile(struct tls_root_ctx *ctx, const char *profile)
Set the TLS certificate profile.
Definition: ssl_openssl.c:535
#define SWAP_BUF_SIZE
Definition: ssl.c:1397
void setenv_del(struct env_set *es, const char *name)
Definition: env_set.c:330
#define S_ACTIVE
Operational key_state state immediately after negotiation has completed while still within the handsh...
Definition: ssl_common.h:102
const char * ncp_ciphers
Definition: options.h:510
bool cleanup_key_ctx
opt.key_ctx_bi is owned by this context
Definition: ssl_common.h:233
static void reliable_set_timeout(struct reliable *rel, interval_t timeout)
Definition: reliable.h:465
static bool buf_write(struct buffer *dest, const void *src, int size)
Definition: buffer.h:689
off_t crl_last_size
size of last loaded CRL
Definition: ssl_mbedtls.h:102
struct key_state * key_scan[KEY_SCAN_SIZE]
List of key_state objects in the order they should be scanned by data channel modules.
Definition: ssl_common.h:509
const char * packet_id_net_print(const struct packet_id_net *pin, bool print_timestamp, struct gc_arena *gc)
Definition: packet_id.c:391
void prng_uninit(void)
Definition: crypto.c:1754
struct tls_auth_standalone * tls_auth_standalone_init(struct tls_options *tls_options, struct gc_arena *gc)
Definition: ssl.c:1298
static char * auth_challenge
Definition: ssl.c:407
static bool key_source2_randomize_write(struct key_source2 *k2, struct buffer *buf, bool server)
Definition: ssl.c:2044
uint8_t id[8]
Definition: session_id.h:40
struct link_socket_actual remote_addr
Definition: ssl_common.h:179
bool nocache
Definition: misc.h:66
#define CLEAR(x)
Definition: basic.h:33
char * options_string_extract_option(const char *options_string, const char *opt_name, struct gc_arena *gc)
Given an OpenVPN options string, extract the value of an option.
Definition: options.c:4041
#define BPTR(buf)
Definition: buffer.h:124
void packet_id_init(struct packet_id *p, int seq_backtrack, int time_backtrack, const char *name, int unit)
Definition: packet_id.c:78
bool reliable_ack_write(struct reliable_ack *ack, struct buffer *buf, const struct session_id *sid, int max, bool prepend)
Write a packet ID acknowledgment record to a buffer.
Definition: reliable.c:213
#define BUF_SIZE(f)
Definition: mtu.h:194
static bool write_empty_string(struct buffer *buf)
Definition: ssl.c:2150
void init_ssl(const struct options *options, struct tls_root_ctx *new_ctx)
Build master SSL context object that serves for the whole of OpenVPN instantiation.
Definition: ssl.c:603
#define KEY_DIRECTION_NORMAL
Definition: crypto.h:174
void print_details(struct key_state_ssl *ks_ssl, const char *prefix)
Definition: ssl_openssl.c:2051
Control channel wrapping (–tls-auth/–tls-crypt) context.
Definition: ssl_common.h:220
bool crl_file_inline
Definition: options.h:551
void get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
Definition: route.c:2722
static void move_session(struct tls_multi *multi, int dest, int src, bool reinit_src)
Definition: ssl.c:1176
#define KEY_SCAN_SIZE
Definition: ssl_common.h:486
uint8_t random2[32]
Seed used for key expansion, provided by both client and server.
Definition: ssl_common.h:128
#define OPENVPN_STATE_WAIT
Definition: manage.h:494
#define GET_USER_PASS_STATIC_CHALLENGE_ECHO
Definition: misc.h:123
interval_t packet_timeout
Definition: ssl_common.h:274
void tls_prepend_opcode_v1(const struct tls_multi *multi, struct buffer *buf)
Prepend a one-byte OpenVPN data channel P_DATA_V1 opcode to the packet.
Definition: ssl.c:3986
#define S_ERROR
Error state.
Definition: ssl_common.h:77
int tun_mtu
Definition: options.h:107
static void tls1_PRF(const uint8_t *label, int label_len, const uint8_t *sec, int slen, uint8_t *out1, int olen)
Definition: ssl.c:1736
interval_t renegotiate_seconds
Definition: ssl_common.h:277
#define KS_SIZE
Size of the tls_session.key array.
Definition: ssl_common.h:386
struct env_item * list
Definition: env_set.h:44
#define P_KEY_ID_MASK
Definition: ssl.h:50
void tls_ctx_free(struct tls_root_ctx *ctx)
Frees the library-specific TLSv1 context.
Definition: ssl_openssl.c:144
unsigned int packet_id_print_type
Definition: packet_id.h:96
void key_state_ssl_init(struct key_state_ssl *ks_ssl, const struct tls_root_ctx *ssl_ctx, bool is_server, struct tls_session *session)
Initialise the SSL channel part of the given key state.
Definition: ssl_openssl.c:1912
struct session_id session_id_remote
Definition: ssl_common.h:178
const char * iana_name
Definition: ssl_backend.h:62
static bool tls_session_generate_data_channel_keys(struct tls_session *session)
Generate data channel keys for the supplied TLS session.
Definition: ssl.c:1924
static bool packet_id_close_to_wrapping(const struct packet_id_send *p)
Definition: packet_id.h:311
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
#define counter_format
Definition: common.h:39
void frame_print(const struct frame *frame, int level, const char *prefix)
Definition: mtu.c:139
static struct gc_arena gc_new(void)
Definition: buffer.h:1015
Container for one half of random material to be used in key method 2 data channel key generation...
Definition: ssl_common.h:121
bool ca_file_inline
Definition: options.h:530
#define KS_PRIMARY
Primary key state index.
Definition: ssl_common.h:382
bool single_session
Definition: ssl_common.h:265
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:106
time_t now
Definition: otime.c:36
void reliable_ack_adjust_frame_parameters(struct frame *frame, int max)
Definition: reliable.c:258
void key_state_rm_auth_control_file(struct key_state *ks)
Remove the given key state&#39;s auth control file, if it exists.
#define malloc
Definition: cmocka.c:1795
#define P_CONTROL_HARD_RESET_SERVER_V2
Definition: ssl.h:64
const char * authname
Definition: options.h:511
#define S_START
Three-way handshake is complete, start of key exchange.
Definition: ssl_common.h:91
#define INCR_SUCCESS
Definition: ssl.c:106
openvpn_net_ctx_t * net_ctx
Definition: ssl_common.h:335
time_t established
Definition: ssl_common.h:173
void show_available_tls_ciphers(const char *cipher_list, const char *cipher_list_tls13, const char *tls_cert_profile)
Definition: ssl.c:4133
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition: buffer.h:1050
#define TM_UNTRUSTED
As yet un-trusted tls_session being negotiated.
Definition: ssl_common.h:465
struct crypto_options crypto_options
Definition: ssl_common.h:181
#define P_LAST_OPCODE
Definition: ssl.h:71
struct tls_multi * tls_multi_init(struct tls_options *tls_options)
Allocate and initialize a tls_multi structure.
Definition: ssl.c:1257
char * locked_cn
Definition: ssl_common.h:538
bool reliable_not_replay(const struct reliable *rel, packet_id_type id)
Check that a received packet&#39;s ID is not a replay.
Definition: reliable.c:426
void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len)
struct buffer * reliable_get_buf_output_sequenced(struct reliable *rel)
Get the buffer of free reliable entry and check whether the outgoing acknowledgment sequence is still...
Definition: reliable.c:490
void free_ssl_lib(void)
Definition: ssl.c:359
static void key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len)
Update the implicit IV for a key_ctx_bi based on TLS session ids and cipher used. ...
Definition: ssl.c:1900
int extra_link
Maximum number of bytes in excess of external network interface&#39;s MTU that might be read from or writ...
Definition: mtu.h:122
#define FRAME_HEADROOM(f)
Definition: mtu.h:187
#define ntohpid(x)
Definition: packet_id.h:64
const struct static_challenge_info * sci
Definition: ssl_common.h:363
const char * reliable_ack_print(struct buffer *buf, bool verbose, struct gc_arena *gc)
Definition: reliable.c:265
int n_hard_errors
Definition: ssl_common.h:532
void auth_set_client_reason(struct tls_multi *multi, const char *client_reason)
Sets the reason why authentication of a client failed.
Definition: ssl_verify.c:807
int transition_window
Definition: ssl_common.h:272
#define KEY_DIRECTION_INVERSE
Definition: crypto.h:175
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition: buffer.c:408
void frame_init_mssfix(struct frame *frame, const struct options *options)
Set the –mssfix option.
Definition: mtu.c:130
static struct user_pass passbuf
Definition: ssl.c:372
cipher_ctx_t * cipher
Generic cipher context.
Definition: crypto.h:166
#define TLS_RELIABLE_N_SEND_BUFFERS
Definition: ssl.h:82
bool xmit_hold
Definition: ssl_common.h:253
#define GET_USER_PASS_PASSWORD_ONLY
Definition: misc.h:115
unsigned __int32 uint32_t
Definition: config-msvc.h:158
void wipe_auth_token(struct tls_multi *multi)
Wipes the authentication token out of the memory, frees and cleans up related buffers and flags...
Definition: auth_token.c:390
mbedtls_md_info_t md_kt_t
Generic message digest key type context.
#define D_TLS_STATE_ERRORS
Definition: errlevel.h:131
#define S_INITIAL
Initial key_state state after initialization by key_state_init() before start of three-way handshake...
Definition: ssl_common.h:81
unsigned int flags
Definition: route.h:153
int keysize
Definition: options.h:512
#define INCR_ERROR
Definition: ssl.c:107
const char * ciphername
Definition: options.h:508
void tls_ctx_restrict_ciphers(struct tls_root_ctx *ctx, const char *ciphers)
Restrict the list of ciphers that can be used within the TLS context for TLS 1.2 and below...
Definition: ssl_openssl.c:444
static bool buf_write_u16(struct buffer *dest, int data)
Definition: buffer.h:720
void pem_password_setup(const char *auth_file)
Definition: ssl.c:375
interval_t reliable_send_timeout(const struct reliable *rel)
Determined how many seconds until the earliest resend should be attempted.
Definition: reliable.c:627
void ssl_purge_auth(const bool auth_user_pass_only)
Definition: ssl.c:480
static bool swap_hmac(struct buffer *buf, const struct crypto_options *co, bool incoming)
Definition: ssl.c:1400
int key_id
The current active key id, used to keep track of renegotiations.
Definition: ssl_common.h:425
static bool link_socket_actual_defined(const struct link_socket_actual *act)
Definition: socket.h:698
static struct user_pass auth_token
Definition: ssl.c:404
Container for one set of cipher and/or HMAC contexts.
Definition: crypto.h:164
bool ncp_enabled
Definition: ssl_common.h:306
#define P_CONTROL_V1
Definition: ssl.h:57
#define OPENVPN_PLUGIN_TLS_FINAL
static void reset_session(struct tls_multi *multi, struct tls_session *session)
Definition: ssl.c:1201
struct buffer_list * paybuf
Definition: ssl_common.h:193
mbedtls_cipher_info_t cipher_kt_t
Generic cipher key type context.
static bool reliable_ack_empty(struct reliable_ack *ack)
Check whether an acknowledgment structure contains any packet IDs to be acknowledged.
Definition: reliable.h:151
void tls_ctx_restrict_ciphers_tls13(struct tls_root_ctx *ctx, const char *ciphers)
Restrict the list of ciphers that can be used within the TLS context for TLS 1.3 and higher...
Definition: ssl_openssl.c:506
#define TLS_RELIABLE_N_REC_BUFFERS
Definition: ssl.h:83
void tls_adjust_frame_parameters(struct frame *frame)
Definition: ssl.c:318
struct key_ctx encrypt
Cipher and/or HMAC contexts for sending direction.
Definition: crypto.h:221
static bool session_id_defined(const struct session_id *sid1)
Definition: session_id.h:55
const char * extra_certs_file
Definition: options.h:536
void tls_ctx_server_new(struct tls_root_ctx *ctx)
Initialise a library-specific TLS context for a server.
Definition: ssl_openssl.c:108
int key_state_read_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf, int maxlen)
Extract plaintext data from the TLS module.
Definition: ssl_openssl.c:2029
int replay_window
Definition: ssl_common.h:300
static SERVICE_STATUS status
Definition: automatic.c:43
void init_key_ctx_bi(struct key_ctx_bi *ctx, const struct key2 *key2, int key_direction, const struct key_type *kt, const char *name)
Definition: crypto.c:885
void set_auth_token(struct user_pass *up, struct user_pass *tk, const char *token)
Definition: misc.c:510
static bool key_method_1_write(struct buffer *buf, struct tls_session *session)
Definition: ssl.c:2221
unsigned int ssl_flags
Definition: options.h:558
const struct buffer * tls_crypt_v2_wkc
Wrapped client key, sent to server.
Definition: ssl_common.h:230
static bool session_id_read(struct session_id *sid, struct buffer *buf)
Definition: session_id.h:61
#define P_CONTROL_HARD_RESET_CLIENT_V2
Definition: ssl.h:63
struct cert_hash_set * locked_cert_hash_set
Definition: ssl_common.h:540
struct tls_wrap_ctx tls_wrap
Definition: ssl.h:132
void ssl_set_auth_nocache(void)
Definition: ssl.c:448
int hmac_ctx_size(const hmac_ctx_t *ctx)
struct tls_wrap_ctx tls_wrap
Definition: ssl_common.h:415
#define PERF_TLS_MULTI_PROCESS
Definition: perf.h:42
struct tls_options opt
Definition: ssl_common.h:507
void buf_clear(struct buffer *buf)
Definition: buffer.c:164
int n_soft_errors
Definition: ssl_common.h:533
void crypto_adjust_frame_parameters(struct frame *frame, const struct key_type *kt, bool packet_id, bool packet_id_long_form)
Calculate crypto overhead and adjust frame to account for that.
Definition: crypto.c:698
const md_kt_t * md_kt_get(const char *digest)
Return message digest parameters, based on the given digest name.
const char * challenge_text
Definition: misc.h:102
const struct plugin_list * plugins
Definition: ssl_common.h:336
struct buffer plaintext_write_buf
Definition: ssl_common.h:186
struct env_item * next
Definition: env_set.h:39
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Definition: buffer.h:734
struct link_socket_actual untrusted_addr
Definition: ssl_common.h:442
char * string
Definition: env_set.h:38
#define msg
Definition: error.h:173
hmac_ctx_t * hmac
Generic HMAC context.
Definition: crypto.h:167
#define S_GOT_KEY
Local OpenVPN process has received the remote&#39;s part of the key material.
Definition: ssl_common.h:97
char * format_hex_ex(const uint8_t *data, int size, int maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
Definition: buffer.c:522
#define RGI_HWADDR_DEFINED
Definition: route.h:149
#define PAYLOAD_SIZE_DYNAMIC(f)
Definition: mtu.h:166
void free_key_ctx_bi(struct key_ctx_bi *ctx)
Definition: crypto.c:922
#define PD_TLS_AUTH_HMAC_SIZE_MASK
Definition: ssl.h:566
#define CO_IGNORE_PACKET_ID
Bit-flag indicating whether to ignore the packet ID of a received packet.
Definition: crypto.h:248
char * remote_ciphername
cipher specified in peer&#39;s config file
Definition: ssl_common.h:584
#define INCR_SENT
Definition: ssl.c:104
bool tls_rec_payload(struct tls_multi *multi, struct buffer *buf)
Definition: ssl.c:4074
static void key_source2_print(const struct key_source2 *k)
Definition: ssl.c:1631
void tls_multi_init_set_options(struct tls_multi *multi, const char *local, const char *remote)
Definition: ssl.c:1333
static const char * local_options_string(const struct tls_session *session)
Definition: ssl.c:73
struct connection_entry ce
Definition: options.h:230
#define SC_ECHO
Definition: misc.h:99
void tls_ctx_load_extra_certs(struct tls_root_ctx *ctx, const char *extra_certs_file, bool extra_certs_file_inline)
Load extra certificate authority certificates from the given file or path.
Definition: ssl_openssl.c:1683
static void openvpn_PRF(const uint8_t *secret, int secret_len, const char *label, const uint8_t *client_seed, int client_seed_len, const uint8_t *server_seed, int server_seed_len, const struct session_id *client_sid, const struct session_id *server_sid, uint8_t *output, int output_len)
Definition: ssl.c:1773
Get a tls_cipher_name_pair containing OpenSSL and IANA names for supplied TLS cipher name...
Definition: ssl_backend.h:62
struct packet_id_send send
Definition: packet_id.h:207
uint8_t * data
Pointer to the allocated memory.
Definition: buffer.h:68
bool wait_for_push
Definition: misc.h:67
const char * local_options
Definition: ssl_common.h:258
#define TM_SIZE
Size of the tls_multi.session array.
Definition: ssl_common.h:469
#define TLS_VER_1_0
Definition: ssl_backend.h:114
#define TLS_AUTHENTICATION_FAILED
Definition: ssl_verify.h:69
struct key_ctx decrypt
cipher and/or HMAC contexts for receiving direction.
Definition: crypto.h:223
struct key_source client
Random provided by client.
Definition: ssl_common.h:139
int platform_stat(const char *path, platform_stat_t *buf)
Definition: platform.c:328
#define TLS_VER_1_3
Definition: ssl_backend.h:117
#define MAX_HMAC_KEY_LENGTH
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:303
Container for bidirectional cipher and HMAC key material.
Definition: crypto.h:181
void tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
Check our certificate notBefore and notAfter fields, and warn if the cert is either not yet valid or ...
Definition: ssl_openssl.c:569
void show_available_tls_ciphers_list(const char *cipher_list, const char *tls_cert_profile, bool tls13)
Definition: ssl_openssl.c:2111
int tls_version_parse(const char *vstr, const char *extra)
Definition: ssl.c:519
static bool tls_session_user_pass_enabled(struct tls_session *session)
Returns whether or not the server should check for username/password.
Definition: ssl.c:1044
#define P_DATA_V1
Definition: ssl.h:59
static void tls1_P_hash(const md_kt_t *md_kt, const uint8_t *sec, int sec_len, const uint8_t *seed, int seed_len, uint8_t *out, int olen)
Definition: ssl.c:1649
#define CONTROL_SEND_ACK_MAX
Definition: ssl.h:77
#define BLEN(buf)
Definition: buffer.h:127
void tls_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *cert_file, bool cert_file_inline)
Use Windows cryptoapi for key and cert, and add to library-specific TLS context.
Definition: ssl_openssl.c:927
time_t auth_deferred_expire
Definition: ssl_common.h:202
struct key_source server
Random provided by server.
Definition: ssl_common.h:140
static void tls_session_init(struct tls_multi *multi, struct tls_session *session)
Initialize a tls_session structure.
Definition: ssl.c:1076
bool reliable_wont_break_sequentiality(const struct reliable *rel, packet_id_type id)
Check that a received packet&#39;s ID can safely be stored in the reliable structure&#39;s processing window...
Definition: reliable.c:453
void init_ssl_lib(void)
Definition: ssl.c:351
bool pkcs12_file_inline
Definition: options.h:541
void verify_user_pass(struct user_pass *up, struct tls_multi *multi, struct tls_session *session)
Verify the given username and password, using either an external script, a plugin, or the management interface.
Definition: ssl_verify.c:1254
#define P_OPCODE_SHIFT
Definition: ssl.h:51
unsigned char md_kt_size(const md_kt_t *kt)
Returns the size of the message digest, in bytes.
struct buffer * reliable_get_buf_sequenced(struct reliable *rel)
Get the buffer of the next sequential and active entry.
Definition: reliable.c:526
void auth_user_pass_setup(const char *auth_file, const struct static_challenge_info *sci)
Definition: ssl.c:411
struct frame frame
Definition: ssl.h:133
#define D_HANDSHAKE
Definition: errlevel.h:72
static bool get_user_pass(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags)
Definition: misc.h:134
int key_id
Key id for this key_state, inherited from struct tls_session.
Definition: ssl_common.h:169
#define GET_USER_PASS_DYNAMIC_CHALLENGE
Definition: misc.h:121
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:672
#define D_SHOW_KEY_SOURCE
Definition: errlevel.h:119
#define TLSMP_KILL
Definition: ssl.h:232
unsigned __int8 uint8_t
Definition: config-msvc.h:160
struct buffer * reliable_send(struct reliable *rel, int *opcode)
Get the next packet to send to the remote peer.
Definition: reliable.c:570
void reliable_mark_deleted(struct reliable *rel, struct buffer *buf, bool inc_pid)
Remove an entry from a reliable structure.
Definition: reliable.c:723
struct session_id session_id
Definition: ssl_common.h:418
bool get_user_pass_cr(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags, const char *auth_challenge)
Definition: misc.c:156
bool tls_pre_decrypt(struct tls_multi *multi, const struct link_socket_actual *from, struct buffer *buf, struct crypto_options **opt, bool floated, const uint8_t **ad_start)
Determine whether an incoming packet is a data channel or control channel packet, and process accordi...
Definition: ssl.c:3349
#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc)
Definition: buffer.h:1072
void purge_user_pass(struct user_pass *up, const bool force)
Definition: misc.c:489
enum tls_wrap_ctx::@11 mode
Control channel wrapping mode.
Structure that wraps the TLS context.
Definition: ssl_mbedtls.h:91
static bool buf_write_u32(struct buffer *dest, int data)
Definition: buffer.h:727
void frame_set_mtu_dynamic(struct frame *frame, int mtu, unsigned int flags)
Definition: mtu.c:87
unsigned int flags
Definition: misc.h:100
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Definition: buffer.h:1087
static char * read_string_alloc(struct buffer *buf)
Definition: ssl.c:2195
#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY
bool plugin_defined(const struct plugin_list *pl, const int type)
Definition: plugin.c:895
bool ssl_clean_auth_token(void)
Definition: ssl.c:469
static void key_state_free(struct key_state *ks, bool clear)
Cleanup a key_state structure.
Definition: ssl.c:984
#define KS_LAME_DUCK
Key state index that will retire soon.
Definition: ssl_common.h:383
static void check_malloc_return(const void *p)
Definition: buffer.h:1093
#define P_CONTROL_SOFT_RESET_V1
Definition: ssl.h:56
struct key_source2 * key_src
Definition: ssl_common.h:183
int initial_opcode
Definition: ssl_common.h:417
static bool key_method_1_read(struct buffer *buf, struct tls_session *session)
Definition: ssl.c:2485
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
Definition: crypto.h:234
void delayed_auth_pass_purge(void)
Definition: ssl.c:4278
Security parameter state of a single session within a VPN tunnel.
Definition: ssl_common.h:406
#define VALGRIND_MAKE_READABLE(addr, len)
Definition: memdbg.h:53
#define S_SENT_KEY
Local OpenVPN process has sent its part of the key material.
Definition: ssl_common.h:94
bool tls_ctx_set_options(struct tls_root_ctx *ctx, unsigned int ssl_flags)
Set any library specific options.
Definition: ssl_openssl.c:330
void output_peer_info_env(struct env_set *es, const char *peer_info)
Definition: misc.c:745
static 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.c:1455
Definition: misc.h:63
int renegotiate_packets
Definition: ssl_common.h:276
int push_peer_info_detail
Definition: ssl_common.h:271
The acknowledgment structure in which packet IDs are stored for later acknowledgment.
Definition: reliable.h:64
int initial_opcode
Definition: ssl_common.h:177
bool disable_occ
Definition: ssl_common.h:267
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
#define M_FATAL
Definition: error.h:94
bool reliable_ack_read_packet_id(struct buffer *buf, packet_id_type *pid)
Read the packet ID of a received packet.
Definition: reliable.c:123
struct frame frame
Definition: ssl_common.h:314
void tls_multi_init_finalize(struct tls_multi *multi, const struct frame *frame)
Finalize initialization of a tls_multi structure.
Definition: ssl.c:1279
#define TLS_AUTHENTICATION_SUCCEEDED
Definition: ssl_verify.h:68
bool crl_file_inline
Definition: ssl_common.h:285
#define buf_init(buf, offset)
Definition: buffer.h:196
#define OPENVPN_OP_ENCRYPT
Cipher should encrypt.
bool tls_send_payload(struct tls_multi *multi, const uint8_t *data, int size)
Definition: ssl.c:4035
int tls_ctx_load_pkcs12(struct tls_root_ctx *ctx, const char *pkcs12_file, bool pkcs12_file_inline, bool load_ca_file)
Load PKCS #12 file for key, cert and (optionally) CA certs, and add to library-specific TLS context...
Definition: ssl_openssl.c:753
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Definition: buffer.h:701
static bool key_method_2_write(struct buffer *buf, struct tls_session *session)
Definition: ssl.c:2367
void tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file, bool dh_file_inline)
Load Diffie Hellman Parameters, and load them into the library-specific TLS context.
Definition: ssl_openssl.c:620
void frame_finalize(struct frame *frame, bool link_mtu_defined, int link_mtu, bool tun_mtu_defined, int tun_mtu)
Definition: mtu.c:56
const char * cipher_list_tls13
Definition: options.h:543
#define OPENVPN_OP_DECRYPT
Cipher should decrypt.
bool reliable_can_send(const struct reliable *rel)
Check whether a reliable structure has any active entries ready to be (re)sent.
Definition: reliable.c:542
void management_set_state(struct management *man, const int state, const char *detail, const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6, const struct openvpn_sockaddr *local, const struct openvpn_sockaddr *remote)
Definition: manage.c:2663
char * locked_username
Definition: ssl_common.h:539
char * common_name
Definition: ssl_common.h:431
void reliable_init(struct reliable *rel, int buf_size, int offset, int array_size, bool hold)
Initialize a reliable structure.
Definition: reliable.c:309
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:570
void crypto_uninit_lib(void)
The reliability layer storage structure for one VPN tunnel&#39;s control channel in one direction...
Definition: reliable.h:88
int link_mtu
Maximum packet size to be sent over the external network interface.
Definition: mtu.h:94
#define D_TLS_DEBUG_MED
Definition: errlevel.h:154
#define TM_LAME_DUCK
Old tls_session.
Definition: ssl_common.h:468
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition: buffer.c:90
uint8_t hwaddr[6]
Definition: route.h:163
uint32_t peer_id
Definition: ssl_common.h:581
int key_state_write_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Insert a ciphertext buffer into the TLS module.
Definition: ssl_openssl.c:2014
static void frame_remove_from_extra_frame(struct frame *frame, const unsigned int decrement)
Definition: mtu.h:280
uint32_t packet_id_type
Definition: packet_id.h:49
#define PD_VERBOSE
Definition: ssl.h:569
static const char * state_name(int state)
Definition: ssl.c:742
void tls_init_lib(void)
Perform any static initialisation necessary by the library.
Definition: ssl_openssl.c:77
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:183
static bool generate_key_expansion(struct key_ctx_bi *key, const struct key_type *key_type, const struct key_source2 *key_src, const struct session_id *client_sid, const struct session_id *server_sid, bool server)
Definition: ssl.c:1819
#define M_WARN
Definition: error.h:96
void tls_clear_error(void)
Clear the underlying SSL library&#39;s error state.
Definition: ssl_openssl.c:102
void ssl_purge_auth_challenge(void)
Definition: ssl.c:498
static int min_int(int x, int y)
Definition: integer.h:57
#define PACKAGE_VERSION
Definition: config.h:742
static int buf_forward_capacity_total(const struct buffer *buf)
Definition: buffer.h:580
bool reliable_can_get(const struct reliable *rel)
Check whether a reliable structure has any free buffers available for use.
Definition: reliable.c:407
#define free
Definition: cmocka.c:1850
void tls_x509_clear_env(struct env_set *es)
Remove any X509_ env variables from env_set es.
Definition: ssl_verify.c:1498
int tls_version_max(void)
Return the maximum TLS version (as a TLS_VER_x constant) supported by current SSL implementation...
Definition: ssl_openssl.c:227
#define PD_TLS
Definition: ssl.h:568
#define P_CONTROL_HARD_RESET_CLIENT_V1
Definition: ssl.h:54
void generate_key_random(struct key *key, const struct key_type *kt)
Definition: crypto.c:1033
void key2_print(const struct key2 *k, const struct key_type *kt, const char *prefix0, const char *prefix1)
Definition: crypto.c:1077
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
bool reliable_ack_read(struct reliable_ack *ack, struct buffer *buf, const struct session_id *sid)
Read an acknowledgment record from a received packet.
Definition: reliable.c:158
const char * openssl_name
Definition: ssl_backend.h:62
static bool link_socket_actual_match(const struct link_socket_actual *a1, const struct link_socket_actual *a2)
Definition: socket.h:885
int tls_ctx_use_management_external_key(struct tls_root_ctx *ctx)
Tell the management interface to load the given certificate and the external private key matching the...
Definition: ssl_openssl.c:1454
void cert_hash_free(struct cert_hash_set *chs)
Frees the given set of certificate hashes.
Definition: ssl_verify.c:234
int key_method
Definition: ssl_common.h:263
#define FULL_SYNC
Definition: ssl.c:2123
static int buf_read_u16(struct buffer *buf)
Definition: buffer.h:825
static struct buffer clear_buf(void)
Return an empty struct buffer.
Definition: buffer.h:209
int interval_t
Definition: common.h:45
#define UP_TYPE_PRIVATE_KEY
Definition: ssl_common.h:42
static void key_source_print(const struct key_source *k, const char *prefix)
Definition: ssl.c:1605
counter_type n_bytes
Definition: ssl_common.h:195
struct buffer ack_write_buf
Definition: ssl_common.h:187
const char * session_id_print(const struct session_id *sid, struct gc_arena *gc)
Definition: session_id.c:57
uint8_t implicit_iv[OPENVPN_MAX_IV_LENGTH]
The implicit part of the IV.
Definition: crypto.h:168
void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, int key_length, const md_kt_t *kt)
int key_state_read_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf, int maxlen)
Extract ciphertext data from the TLS module.
Definition: ssl_openssl.c:1999
static const tls_cipher_name_pair tls_cipher_name_translation_table[]
SSL/TLS Cipher suite name translation table.
Definition: ssl.c:114
static void key_state_init(struct tls_session *session, struct key_state *ks)
Initialize a key_state structure.
Definition: ssl.c:904
static int key_source2_read(struct key_source2 *k2, struct buffer *buf, bool server)
Definition: ssl.c:2077
#define BSTR(buf)
Definition: buffer.h:129
static bool auth_user_pass_enabled
Definition: ssl.c:402
hmac_ctx_t * hmac_ctx_new(void)
#define TUN_LINK_DELTA(f)
Definition: mtu.h:151
#define KEY_METHOD_MASK
Definition: ssl.h:119
const char * dh_file
Definition: options.h:532
time_t must_die
Definition: ssl_common.h:175
#define KEY_EXPANSION_ID
Definition: ssl.h:47
static void tls_session_free(struct tls_session *session, bool clear)
Clean up a tls_session structure.
Definition: ssl.c:1148
char password[USER_PASS_LEN]
Definition: misc.h:76
#define P_CONTROL_HARD_RESET_CLIENT_V3
Definition: ssl.h:67
struct buffer clone_buf(const struct buffer *buf)
Definition: buffer.c:117
void verify_final_auth_checks(struct tls_multi *multi, struct tls_session *session)
Perform final authentication checks, including locking of the cn, the allowed certificate hashes...
Definition: ssl_verify.c:1436
static bool random_bytes_to_buf(struct buffer *buf, uint8_t *out, int outlen)
Definition: ssl.c:2028
void tls_ctx_load_ca(struct tls_root_ctx *ctx, const char *ca_file, bool ca_file_inline, const char *ca_path, bool tls_server)
Load certificate authority certificates from the given file or path.
Definition: ssl_openssl.c:1531
void reliable_mark_active_incoming(struct reliable *rel, struct buffer *buf, packet_id_type pid, int opcode)
Mark the reliable entry associated with the given buffer as active incoming.
Definition: reliable.c:664
void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst)
const cipher_kt_t * cipher
Cipher static parameters.
Definition: crypto.h:143
static void update_time(void)
Definition: otime.h:93
bool replay
Definition: options.h:516
static int auth_deferred_expire_window(const struct tls_options *o)
Definition: ssl.c:2725
char * dest
Definition: compat-lz4.h:431
counter_type n_packets
Definition: ssl_common.h:196
bool dh_file_inline
Definition: options.h:533
int tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file, bool priv_key_file_inline)
Load private key file into the given TLS context.
Definition: ssl_openssl.c:994
#define TLS_CHANNEL_BUF_SIZE
Definition: common.h:78
#define MF_EXTERNAL_CERT
Definition: manage.h:351
void buffer_list_pop(struct buffer_list *ol)
Definition: buffer.c:1348
#define P_ACK_V1
Definition: ssl.h:58
void hmac_ctx_free(hmac_ctx_t *ctx)
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
Definition: buffer.c:1283
struct key_ctx tls_crypt_v2_server_key
Decrypts client keys.
Definition: ssl_common.h:229
static void tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, bool crl_file_inline)
Load (or possibly reload) the CRL file into the SSL context.
Definition: ssl.c:560
struct buffer_entry * buffer_list_push_data(struct buffer_list *ol, const void *data, size_t size)
Allocates and appends a new buffer containing data of length size.
Definition: buffer.c:1256
#define MF_EXTERNAL_KEY
Definition: manage.h:345
const char * remote_options
Definition: ssl_common.h:259
#define P_DATA_V2
Definition: ssl.h:60
const tls_cipher_name_pair * tls_get_cipher_name_pair(const char *cipher_name, size_t len)
Definition: ssl.c:272
int replay_time
Definition: ssl_common.h:301
void reliable_schedule_now(struct reliable *rel)
Reschedule all entries of a reliable structure to be ready for (re)sending immediately.
Definition: reliable.c:608
void reliable_send_purge(struct reliable *rel, const struct reliable_ack *ack)
Remove acknowledged packets from a reliable structure.
Definition: reliable.c:355
static int packet_id_size(bool long_form)
Definition: packet_id.h:305
void crypto_init_lib(void)
unsigned int ssl_flags
Definition: ssl_common.h:354
struct _stat platform_stat_t
Definition: platform.h:141
#define PD_SHOW_DATA
Definition: ssl.h:567
uint8_t pre_master[48]
Random used for master secret generation, provided only by client OpenVPN peer.
Definition: ssl_common.h:122
void hmac_ctx_reset(hmac_ctx_t *ctx)
struct env_set * es
Definition: ssl_common.h:334
bool priv_key_file_inline
Definition: options.h:539
static bool session_id_write_prepend(const struct session_id *sid, struct buffer *buf)
Definition: session_id.h:67
static bool key_method_2_read(struct buffer *buf, struct tls_multi *multi, struct tls_session *session)
Definition: ssl.c:2546
int fragment
Definition: options.h:117
Container for two sets of OpenSSL cipher and/or HMAC contexts for both sending and receiving directio...
Definition: crypto.h:219
void init_key_type(struct key_type *kt, const char *ciphername, const char *authname, int keysize, bool tls_mode, bool warn)
Initialize a key_type structure with.
Definition: crypto.c:755
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:151
struct tls_wrap_ctx tls_wrap
TLS handshake wrapping state.
Definition: ssl_common.h:312
#define GET_USER_PASS_MANAGEMENT
Definition: misc.h:113
bool reliable_ack_acknowledge_packet_id(struct reliable_ack *ack, packet_id_type pid)
Record a packet ID for later acknowledgment.
Definition: reliable.c:141
int link_mtu_dynamic
Dynamic MTU value for the external network interface.
Definition: mtu.h:97
int link_mtu
Definition: options.h:111
#define INCR_GENERATED
Definition: ssl.c:105
int tls_multi_process(struct tls_multi *multi, struct buffer *to_link, struct link_socket_actual **to_link_addr, struct link_socket_info *to_link_socket_info, interval_t *wakeup)
Definition: ssl.c:3154
const char * crl_file
Definition: options.h:550