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 "auth_token.h"
63 
64 #include "memdbg.h"
65 
66 #ifndef ENABLE_OCC
67 static const char ssl_default_options_string[] = "V0 UNDEF";
68 #endif
69 
70 static inline const char *
72 {
73 #ifdef ENABLE_OCC
74  return session->opt->local_options;
75 #else
76  return ssl_default_options_string;
77 #endif
78 }
79 
80 #ifdef MEASURE_TLS_HANDSHAKE_STATS
81 
82 static int tls_handshake_success; /* GLOBAL */
83 static int tls_handshake_error; /* GLOBAL */
84 static int tls_packets_generated; /* GLOBAL */
85 static int tls_packets_sent; /* GLOBAL */
86 
87 #define INCR_SENT ++tls_packets_sent
88 #define INCR_GENERATED ++tls_packets_generated
89 #define INCR_SUCCESS ++tls_handshake_success
90 #define INCR_ERROR ++tls_handshake_error
91 
92 void
93 show_tls_performance_stats(void)
94 {
95  msg(D_TLS_DEBUG_LOW, "TLS Handshakes, success=%f%% (good=%d, bad=%d), retransmits=%f%%",
96  (double) tls_handshake_success / (tls_handshake_success + tls_handshake_error) * 100.0,
97  tls_handshake_success, tls_handshake_error,
98  (double) (tls_packets_sent - tls_packets_generated) / tls_packets_generated * 100.0);
99 }
100 #else /* ifdef MEASURE_TLS_HANDSHAKE_STATS */
101 
102 #define INCR_SENT
103 #define INCR_GENERATED
104 #define INCR_SUCCESS
105 #define INCR_ERROR
106 
107 #endif /* ifdef MEASURE_TLS_HANDSHAKE_STATS */
108 
113  {"ADH-SEED-SHA", "TLS-DH-anon-WITH-SEED-CBC-SHA"},
114  {"AES128-GCM-SHA256", "TLS-RSA-WITH-AES-128-GCM-SHA256"},
115  {"AES128-SHA256", "TLS-RSA-WITH-AES-128-CBC-SHA256"},
116  {"AES128-SHA", "TLS-RSA-WITH-AES-128-CBC-SHA"},
117  {"AES256-GCM-SHA384", "TLS-RSA-WITH-AES-256-GCM-SHA384"},
118  {"AES256-SHA256", "TLS-RSA-WITH-AES-256-CBC-SHA256"},
119  {"AES256-SHA", "TLS-RSA-WITH-AES-256-CBC-SHA"},
120  {"CAMELLIA128-SHA256", "TLS-RSA-WITH-CAMELLIA-128-CBC-SHA256"},
121  {"CAMELLIA128-SHA", "TLS-RSA-WITH-CAMELLIA-128-CBC-SHA"},
122  {"CAMELLIA256-SHA256", "TLS-RSA-WITH-CAMELLIA-256-CBC-SHA256"},
123  {"CAMELLIA256-SHA", "TLS-RSA-WITH-CAMELLIA-256-CBC-SHA"},
124  {"DES-CBC3-SHA", "TLS-RSA-WITH-3DES-EDE-CBC-SHA"},
125  {"DES-CBC-SHA", "TLS-RSA-WITH-DES-CBC-SHA"},
126  {"DH-DSS-SEED-SHA", "TLS-DH-DSS-WITH-SEED-CBC-SHA"},
127  {"DHE-DSS-AES128-GCM-SHA256", "TLS-DHE-DSS-WITH-AES-128-GCM-SHA256"},
128  {"DHE-DSS-AES128-SHA256", "TLS-DHE-DSS-WITH-AES-128-CBC-SHA256"},
129  {"DHE-DSS-AES128-SHA", "TLS-DHE-DSS-WITH-AES-128-CBC-SHA"},
130  {"DHE-DSS-AES256-GCM-SHA384", "TLS-DHE-DSS-WITH-AES-256-GCM-SHA384"},
131  {"DHE-DSS-AES256-SHA256", "TLS-DHE-DSS-WITH-AES-256-CBC-SHA256"},
132  {"DHE-DSS-AES256-SHA", "TLS-DHE-DSS-WITH-AES-256-CBC-SHA"},
133  {"DHE-DSS-CAMELLIA128-SHA256", "TLS-DHE-DSS-WITH-CAMELLIA-128-CBC-SHA256"},
134  {"DHE-DSS-CAMELLIA128-SHA", "TLS-DHE-DSS-WITH-CAMELLIA-128-CBC-SHA"},
135  {"DHE-DSS-CAMELLIA256-SHA256", "TLS-DHE-DSS-WITH-CAMELLIA-256-CBC-SHA256"},
136  {"DHE-DSS-CAMELLIA256-SHA", "TLS-DHE-DSS-WITH-CAMELLIA-256-CBC-SHA"},
137  {"DHE-DSS-SEED-SHA", "TLS-DHE-DSS-WITH-SEED-CBC-SHA"},
138  {"DHE-RSA-AES128-GCM-SHA256", "TLS-DHE-RSA-WITH-AES-128-GCM-SHA256"},
139  {"DHE-RSA-AES128-SHA256", "TLS-DHE-RSA-WITH-AES-128-CBC-SHA256"},
140  {"DHE-RSA-AES128-SHA", "TLS-DHE-RSA-WITH-AES-128-CBC-SHA"},
141  {"DHE-RSA-AES256-GCM-SHA384", "TLS-DHE-RSA-WITH-AES-256-GCM-SHA384"},
142  {"DHE-RSA-AES256-SHA256", "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256"},
143  {"DHE-RSA-AES256-SHA", "TLS-DHE-RSA-WITH-AES-256-CBC-SHA"},
144  {"DHE-RSA-CAMELLIA128-SHA256", "TLS-DHE-RSA-WITH-CAMELLIA-128-CBC-SHA256"},
145  {"DHE-RSA-CAMELLIA128-SHA", "TLS-DHE-RSA-WITH-CAMELLIA-128-CBC-SHA"},
146  {"DHE-RSA-CAMELLIA256-SHA256", "TLS-DHE-RSA-WITH-CAMELLIA-256-CBC-SHA256"},
147  {"DHE-RSA-CAMELLIA256-SHA", "TLS-DHE-RSA-WITH-CAMELLIA-256-CBC-SHA"},
148  {"DHE-RSA-CHACHA20-POLY1305", "TLS-DHE-RSA-WITH-CHACHA20-POLY1305-SHA256"},
149  {"DHE-RSA-SEED-SHA", "TLS-DHE-RSA-WITH-SEED-CBC-SHA"},
150  {"DH-RSA-SEED-SHA", "TLS-DH-RSA-WITH-SEED-CBC-SHA"},
151  {"ECDH-ECDSA-AES128-GCM-SHA256", "TLS-ECDH-ECDSA-WITH-AES-128-GCM-SHA256"},
152  {"ECDH-ECDSA-AES128-SHA256", "TLS-ECDH-ECDSA-WITH-AES-128-CBC-SHA256"},
153  {"ECDH-ECDSA-AES128-SHA", "TLS-ECDH-ECDSA-WITH-AES-128-CBC-SHA"},
154  {"ECDH-ECDSA-AES256-GCM-SHA384", "TLS-ECDH-ECDSA-WITH-AES-256-GCM-SHA384"},
155  {"ECDH-ECDSA-AES256-SHA256", "TLS-ECDH-ECDSA-WITH-AES-256-CBC-SHA256"},
156  {"ECDH-ECDSA-AES256-SHA384", "TLS-ECDH-ECDSA-WITH-AES-256-CBC-SHA384"},
157  {"ECDH-ECDSA-AES256-SHA", "TLS-ECDH-ECDSA-WITH-AES-256-CBC-SHA"},
158  {"ECDH-ECDSA-CAMELLIA128-SHA256", "TLS-ECDH-ECDSA-WITH-CAMELLIA-128-CBC-SHA256"},
159  {"ECDH-ECDSA-CAMELLIA128-SHA", "TLS-ECDH-ECDSA-WITH-CAMELLIA-128-CBC-SHA"},
160  {"ECDH-ECDSA-CAMELLIA256-SHA256", "TLS-ECDH-ECDSA-WITH-CAMELLIA-256-CBC-SHA256"},
161  {"ECDH-ECDSA-CAMELLIA256-SHA", "TLS-ECDH-ECDSA-WITH-CAMELLIA-256-CBC-SHA"},
162  {"ECDH-ECDSA-DES-CBC3-SHA", "TLS-ECDH-ECDSA-WITH-3DES-EDE-CBC-SHA"},
163  {"ECDH-ECDSA-DES-CBC-SHA", "TLS-ECDH-ECDSA-WITH-DES-CBC-SHA"},
164  {"ECDH-ECDSA-RC4-SHA", "TLS-ECDH-ECDSA-WITH-RC4-128-SHA"},
165  {"ECDHE-ECDSA-AES128-GCM-SHA256", "TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256"},
166  {"ECDHE-ECDSA-AES128-SHA256", "TLS-ECDHE-ECDSA-WITH-AES-128-CBC-SHA256"},
167  {"ECDHE-ECDSA-AES128-SHA384", "TLS-ECDHE-ECDSA-WITH-AES-128-CBC-SHA384"},
168  {"ECDHE-ECDSA-AES128-SHA", "TLS-ECDHE-ECDSA-WITH-AES-128-CBC-SHA"},
169  {"ECDHE-ECDSA-AES256-GCM-SHA384", "TLS-ECDHE-ECDSA-WITH-AES-256-GCM-SHA384"},
170  {"ECDHE-ECDSA-AES256-SHA256", "TLS-ECDHE-ECDSA-WITH-AES-256-CBC-SHA256"},
171  {"ECDHE-ECDSA-AES256-SHA384", "TLS-ECDHE-ECDSA-WITH-AES-256-CBC-SHA384"},
172  {"ECDHE-ECDSA-AES256-SHA", "TLS-ECDHE-ECDSA-WITH-AES-256-CBC-SHA"},
173  {"ECDHE-ECDSA-CAMELLIA128-SHA256", "TLS-ECDHE-ECDSA-WITH-CAMELLIA-128-CBC-SHA256"},
174  {"ECDHE-ECDSA-CAMELLIA128-SHA", "TLS-ECDHE-ECDSA-WITH-CAMELLIA-128-CBC-SHA"},
175  {"ECDHE-ECDSA-CAMELLIA256-SHA256", "TLS-ECDHE-ECDSA-WITH-CAMELLIA-256-CBC-SHA256"},
176  {"ECDHE-ECDSA-CAMELLIA256-SHA", "TLS-ECDHE-ECDSA-WITH-CAMELLIA-256-CBC-SHA"},
177  {"ECDHE-ECDSA-CHACHA20-POLY1305", "TLS-ECDHE-ECDSA-WITH-CHACHA20-POLY1305-SHA256"},
178  {"ECDHE-ECDSA-DES-CBC3-SHA", "TLS-ECDHE-ECDSA-WITH-3DES-EDE-CBC-SHA"},
179  {"ECDHE-ECDSA-DES-CBC-SHA", "TLS-ECDHE-ECDSA-WITH-DES-CBC-SHA"},
180  {"ECDHE-ECDSA-RC4-SHA", "TLS-ECDHE-ECDSA-WITH-RC4-128-SHA"},
181  {"ECDHE-RSA-AES128-GCM-SHA256", "TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256"},
182  {"ECDHE-RSA-AES128-SHA256", "TLS-ECDHE-RSA-WITH-AES-128-CBC-SHA256"},
183  {"ECDHE-RSA-AES128-SHA384", "TLS-ECDHE-RSA-WITH-AES-128-CBC-SHA384"},
184  {"ECDHE-RSA-AES128-SHA", "TLS-ECDHE-RSA-WITH-AES-128-CBC-SHA"},
185  {"ECDHE-RSA-AES256-GCM-SHA384", "TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384"},
186  {"ECDHE-RSA-AES256-SHA256", "TLS-ECDHE-RSA-WITH-AES-256-CBC-SHA256"},
187  {"ECDHE-RSA-AES256-SHA384", "TLS-ECDHE-RSA-WITH-AES-256-CBC-SHA384"},
188  {"ECDHE-RSA-AES256-SHA", "TLS-ECDHE-RSA-WITH-AES-256-CBC-SHA"},
189  {"ECDHE-RSA-CAMELLIA128-SHA256", "TLS-ECDHE-RSA-WITH-CAMELLIA-128-CBC-SHA256"},
190  {"ECDHE-RSA-CAMELLIA128-SHA", "TLS-ECDHE-RSA-WITH-CAMELLIA-128-CBC-SHA"},
191  {"ECDHE-RSA-CAMELLIA256-SHA256", "TLS-ECDHE-RSA-WITH-CAMELLIA-256-CBC-SHA256"},
192  {"ECDHE-RSA-CAMELLIA256-SHA", "TLS-ECDHE-RSA-WITH-CAMELLIA-256-CBC-SHA"},
193  {"ECDHE-RSA-CHACHA20-POLY1305", "TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256"},
194  {"ECDHE-RSA-DES-CBC3-SHA", "TLS-ECDHE-RSA-WITH-3DES-EDE-CBC-SHA"},
195  {"ECDHE-RSA-DES-CBC-SHA", "TLS-ECDHE-RSA-WITH-DES-CBC-SHA"},
196  {"ECDHE-RSA-RC4-SHA", "TLS-ECDHE-RSA-WITH-RC4-128-SHA"},
197  {"ECDH-RSA-AES128-GCM-SHA256", "TLS-ECDH-RSA-WITH-AES-128-GCM-SHA256"},
198  {"ECDH-RSA-AES128-SHA256", "TLS-ECDH-RSA-WITH-AES-128-CBC-SHA256"},
199  {"ECDH-RSA-AES128-SHA384", "TLS-ECDH-RSA-WITH-AES-128-CBC-SHA384"},
200  {"ECDH-RSA-AES128-SHA", "TLS-ECDH-RSA-WITH-AES-128-CBC-SHA"},
201  {"ECDH-RSA-AES256-GCM-SHA384", "TLS-ECDH-RSA-WITH-AES-256-GCM-SHA384"},
202  {"ECDH-RSA-AES256-SHA256", "TLS-ECDH-RSA-WITH-AES-256-CBC-SHA256"},
203  {"ECDH-RSA-AES256-SHA384", "TLS-ECDH-RSA-WITH-AES-256-CBC-SHA384"},
204  {"ECDH-RSA-AES256-SHA", "TLS-ECDH-RSA-WITH-AES-256-CBC-SHA"},
205  {"ECDH-RSA-CAMELLIA128-SHA256", "TLS-ECDH-RSA-WITH-CAMELLIA-128-CBC-SHA256"},
206  {"ECDH-RSA-CAMELLIA128-SHA", "TLS-ECDH-RSA-WITH-CAMELLIA-128-CBC-SHA"},
207  {"ECDH-RSA-CAMELLIA256-SHA256", "TLS-ECDH-RSA-WITH-CAMELLIA-256-CBC-SHA256"},
208  {"ECDH-RSA-CAMELLIA256-SHA", "TLS-ECDH-RSA-WITH-CAMELLIA-256-CBC-SHA"},
209  {"ECDH-RSA-DES-CBC3-SHA", "TLS-ECDH-RSA-WITH-3DES-EDE-CBC-SHA"},
210  {"ECDH-RSA-DES-CBC-SHA", "TLS-ECDH-RSA-WITH-DES-CBC-SHA"},
211  {"ECDH-RSA-RC4-SHA", "TLS-ECDH-RSA-WITH-RC4-128-SHA"},
212  {"EDH-DSS-DES-CBC3-SHA", "TLS-DHE-DSS-WITH-3DES-EDE-CBC-SHA"},
213  {"EDH-DSS-DES-CBC-SHA", "TLS-DHE-DSS-WITH-DES-CBC-SHA"},
214  {"EDH-RSA-DES-CBC3-SHA", "TLS-DHE-RSA-WITH-3DES-EDE-CBC-SHA"},
215  {"EDH-RSA-DES-CBC-SHA", "TLS-DHE-RSA-WITH-DES-CBC-SHA"},
216  {"EXP-DES-CBC-SHA", "TLS-RSA-EXPORT-WITH-DES40-CBC-SHA"},
217  {"EXP-EDH-DSS-DES-CBC-SHA", "TLS-DH-DSS-EXPORT-WITH-DES40-CBC-SHA"},
218  {"EXP-EDH-RSA-DES-CBC-SHA", "TLS-DH-RSA-EXPORT-WITH-DES40-CBC-SHA"},
219  {"EXP-RC2-CBC-MD5", "TLS-RSA-EXPORT-WITH-RC2-CBC-40-MD5"},
220  {"EXP-RC4-MD5", "TLS-RSA-EXPORT-WITH-RC4-40-MD5"},
221  {"NULL-MD5", "TLS-RSA-WITH-NULL-MD5"},
222  {"NULL-SHA256", "TLS-RSA-WITH-NULL-SHA256"},
223  {"NULL-SHA", "TLS-RSA-WITH-NULL-SHA"},
224  {"PSK-3DES-EDE-CBC-SHA", "TLS-PSK-WITH-3DES-EDE-CBC-SHA"},
225  {"PSK-AES128-CBC-SHA", "TLS-PSK-WITH-AES-128-CBC-SHA"},
226  {"PSK-AES256-CBC-SHA", "TLS-PSK-WITH-AES-256-CBC-SHA"},
227  {"PSK-RC4-SHA", "TLS-PSK-WITH-RC4-128-SHA"},
228  {"RC4-MD5", "TLS-RSA-WITH-RC4-128-MD5"},
229  {"RC4-SHA", "TLS-RSA-WITH-RC4-128-SHA"},
230  {"SEED-SHA", "TLS-RSA-WITH-SEED-CBC-SHA"},
231  {"SRP-DSS-3DES-EDE-CBC-SHA", "TLS-SRP-SHA-DSS-WITH-3DES-EDE-CBC-SHA"},
232  {"SRP-DSS-AES-128-CBC-SHA", "TLS-SRP-SHA-DSS-WITH-AES-128-CBC-SHA"},
233  {"SRP-DSS-AES-256-CBC-SHA", "TLS-SRP-SHA-DSS-WITH-AES-256-CBC-SHA"},
234  {"SRP-RSA-3DES-EDE-CBC-SHA", "TLS-SRP-SHA-RSA-WITH-3DES-EDE-CBC-SHA"},
235  {"SRP-RSA-AES-128-CBC-SHA", "TLS-SRP-SHA-RSA-WITH-AES-128-CBC-SHA"},
236  {"SRP-RSA-AES-256-CBC-SHA", "TLS-SRP-SHA-RSA-WITH-AES-256-CBC-SHA"},
237 #ifdef ENABLE_CRYPTO_OPENSSL
238  /* OpenSSL-specific group names */
239  {"DEFAULT", "DEFAULT"},
240  {"ALL", "ALL"},
241  {"HIGH", "HIGH"}, {"!HIGH", "!HIGH"},
242  {"MEDIUM", "MEDIUM"}, {"!MEDIUM", "!MEDIUM"},
243  {"LOW", "LOW"}, {"!LOW", "!LOW"},
244  {"ECDH", "ECDH"}, {"!ECDH", "!ECDH"},
245  {"ECDSA", "ECDSA"}, {"!ECDSA", "!ECDSA"},
246  {"EDH", "EDH"}, {"!EDH", "!EDH"},
247  {"EXP", "EXP"}, {"!EXP", "!EXP"},
248  {"RSA", "RSA"}, {"!RSA", "!RSA"},
249  {"kRSA", "kRSA"}, {"!kRSA", "!kRSA"},
250  {"SRP", "SRP"}, {"!SRP", "!SRP"},
251 #endif
252  {NULL, NULL}
253 };
254 
266 static void
267 key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len);
268 
269 const tls_cipher_name_pair *
270 tls_get_cipher_name_pair(const char *cipher_name, size_t len)
271 {
273 
274  while (pair->openssl_name != NULL)
275  {
276  if ((strlen(pair->openssl_name) == len && 0 == memcmp(cipher_name, pair->openssl_name, len))
277  || (strlen(pair->iana_name) == len && 0 == memcmp(cipher_name, pair->iana_name, len)))
278  {
279  return pair;
280  }
281  pair++;
282  }
283 
284  /* No entry found, return NULL */
285  return NULL;
286 }
287 
295 static void
296 tls_limit_reneg_bytes(const cipher_kt_t *cipher, int *reneg_bytes)
297 {
298  if (cipher && cipher_kt_insecure(cipher))
299  {
300  if (*reneg_bytes == -1) /* Not user-specified */
301  {
302  msg(M_WARN, "WARNING: cipher with small block size in use, "
303  "reducing reneg-bytes to 64MB to mitigate SWEET32 attacks.");
304  *reneg_bytes = 64 * 1024 * 1024;
305  }
306  }
307 }
308 
309 /*
310  * Max number of bytes we will add
311  * for data structures common to both
312  * data and control channel packets.
313  * (opcode only).
314  */
315 void
317 {
318  frame_add_to_extra_frame(frame, 1); /* space for opcode */
319 }
320 
321 /*
322  * Max number of bytes we will add
323  * to control channel packet.
324  */
325 static void
326 tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame,
327  struct frame *frame)
328 {
329  /*
330  * frame->extra_frame is already initialized with tls_auth buffer requirements,
331  * if --tls-auth is enabled.
332  */
333 
334  /* inherit link MTU and extra_link from data channel */
335  frame->link_mtu = data_channel_frame->link_mtu;
336  frame->extra_link = data_channel_frame->extra_link;
337 
338  /* set extra_frame */
342 
343  /* set dynamic link MTU to cap control channel packets at 1250 bytes */
344  ASSERT(TUN_LINK_DELTA(frame) < min_int(frame->link_mtu, 1250));
345  frame->link_mtu_dynamic = min_int(frame->link_mtu, 1250) - TUN_LINK_DELTA(frame);
346 }
347 
348 void
350 {
351  tls_init_lib();
352 
353  crypto_init_lib();
354 }
355 
356 void
358 {
360  prng_uninit();
361 
362  tls_free_lib();
363 }
364 
365 /*
366  * OpenSSL library calls pem_password_callback if the
367  * private key is protected by a password.
368  */
369 
370 static struct user_pass passbuf; /* GLOBAL */
371 
372 void
373 pem_password_setup(const char *auth_file)
374 {
375  if (!strlen(passbuf.password))
376  {
378  }
379 }
380 
381 int
382 pem_password_callback(char *buf, int size, int rwflag, void *u)
383 {
384  if (buf)
385  {
386  /* prompt for password even if --askpass wasn't specified */
387  pem_password_setup(NULL);
388  strncpynt(buf, passbuf.password, size);
389  purge_user_pass(&passbuf, false);
390 
391  return strlen(buf);
392  }
393  return 0;
394 }
395 
396 /*
397  * Auth username/password handling
398  */
399 
400 static bool auth_user_pass_enabled; /* GLOBAL */
401 static struct user_pass auth_user_pass; /* GLOBAL */
402 static struct user_pass auth_token; /* GLOBAL */
403 
404 #ifdef ENABLE_MANAGEMENT
405 static char *auth_challenge; /* GLOBAL */
406 #endif
407 
408 void
409 auth_user_pass_setup(const char *auth_file, const struct static_challenge_info *sci)
410 {
411  auth_user_pass_enabled = true;
413  {
414 #ifdef ENABLE_MANAGEMENT
415  if (auth_challenge) /* dynamic challenge/response */
416  {
418  auth_file,
419  UP_TYPE_AUTH,
422  }
423  else if (sci) /* static challenge response */
424  {
426  if (sci->flags & SC_ECHO)
427  {
429  }
431  auth_file,
432  UP_TYPE_AUTH,
433  flags,
434  sci->challenge_text);
435  }
436  else
437 #endif /* ifdef ENABLE_MANAGEMENT */
439  }
440 }
441 
442 /*
443  * Disable password caching
444  */
445 void
447 {
448  passbuf.nocache = true;
449  auth_user_pass.nocache = true;
450  /* wait for push-reply, because auth-token may still need the username */
452 }
453 
454 /*
455  * Set an authentication token
456  */
457 void
458 ssl_set_auth_token(const char *token)
459 {
461 }
462 
463 /*
464  * Cleans an auth token and checks if it was active
465  */
466 bool
468 {
469  bool wasdefined = auth_token.defined;
470  purge_user_pass(&auth_token, true);
471  return wasdefined;
472 }
473 
474 /*
475  * Forget private key password AND auth-user-pass username/password.
476  */
477 void
478 ssl_purge_auth(const bool auth_user_pass_only)
479 {
480  if (!auth_user_pass_only)
481  {
482 #ifdef ENABLE_PKCS11
483  pkcs11_logout();
484 #endif
485  purge_user_pass(&passbuf, true);
486  }
488 #ifdef ENABLE_MANAGEMENT
490 #endif
491 }
492 
493 #ifdef ENABLE_MANAGEMENT
494 
495 void
497 {
499  auth_challenge = NULL;
500 }
501 
502 void
503 ssl_put_auth_challenge(const char *cr_str)
504 {
506  auth_challenge = string_alloc(cr_str, NULL);
507 }
508 
509 #endif
510 
511 /*
512  * Parse a TLS version string, returning a TLS_VER_x constant.
513  * If version string is not recognized and extra == "or-highest",
514  * return tls_version_max().
515  */
516 int
517 tls_version_parse(const char *vstr, const char *extra)
518 {
519  const int max_version = tls_version_max();
520  if (!strcmp(vstr, "1.0") && TLS_VER_1_0 <= max_version)
521  {
522  return TLS_VER_1_0;
523  }
524  else if (!strcmp(vstr, "1.1") && TLS_VER_1_1 <= max_version)
525  {
526  return TLS_VER_1_1;
527  }
528  else if (!strcmp(vstr, "1.2") && TLS_VER_1_2 <= max_version)
529  {
530  return TLS_VER_1_2;
531  }
532  else if (!strcmp(vstr, "1.3") && TLS_VER_1_3 <= max_version)
533  {
534  return TLS_VER_1_3;
535  }
536  else if (extra && !strcmp(extra, "or-highest"))
537  {
538  return max_version;
539  }
540  else
541  {
542  return TLS_VER_BAD;
543  }
544 }
545 
557 static void
558 tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file,
559  const char *crl_file_inline)
560 {
561  /* if something goes wrong with stat(), we'll store 0 as mtime */
562  platform_stat_t crl_stat = {0};
563 
564  /*
565  * an inline CRL can't change at runtime, therefore there is no need to
566  * reload it. It will be reloaded upon config change + SIGHUP.
567  * Use always '1' as dummy timestamp in this case: it will trigger the
568  * first load, but will prevent any future reload.
569  */
570  if (crl_file_inline)
571  {
572  crl_stat.st_mtime = 1;
573  }
574  else if (platform_stat(crl_file, &crl_stat) < 0)
575  {
576  msg(M_WARN, "WARNING: Failed to stat CRL file, not (re)loading CRL.");
577  return;
578  }
579 
580  /*
581  * Store the CRL if this is the first time or if the file was changed since
582  * the last load.
583  * Note: Windows does not support tv_nsec.
584  */
585  if ((ssl_ctx->crl_last_size == crl_stat.st_size)
586  && (ssl_ctx->crl_last_mtime == crl_stat.st_mtime))
587  {
588  return;
589  }
590 
591  ssl_ctx->crl_last_mtime = crl_stat.st_mtime;
592  ssl_ctx->crl_last_size = crl_stat.st_size;
593  backend_tls_ctx_reload_crl(ssl_ctx, crl_file, crl_file_inline);
594 }
595 
596 /*
597  * Initialize SSL context.
598  * All files are in PEM format.
599  */
600 void
601 init_ssl(const struct options *options, struct tls_root_ctx *new_ctx)
602 {
603  ASSERT(NULL != new_ctx);
604 
605  tls_clear_error();
606 
607  if (options->tls_server)
608  {
609  tls_ctx_server_new(new_ctx);
610 
611  if (options->dh_file)
612  {
613  tls_ctx_load_dh_params(new_ctx, options->dh_file,
614  options->dh_file_inline);
615  }
616  }
617  else /* if client */
618  {
619  tls_ctx_client_new(new_ctx);
620  }
621 
622  /* Restrict allowed certificate crypto algorithms */
623  tls_ctx_set_cert_profile(new_ctx, options->tls_cert_profile);
624 
625  /* Allowable ciphers */
626  /* Since @SECLEVEL also influences loading of certificates, set the
627  * cipher restrictions before loading certificates */
628  tls_ctx_restrict_ciphers(new_ctx, options->cipher_list);
630 
631  if (!tls_ctx_set_options(new_ctx, options->ssl_flags))
632  {
633  goto err;
634  }
635 
636  if (options->pkcs12_file)
637  {
638  if (0 != tls_ctx_load_pkcs12(new_ctx, options->pkcs12_file,
639  options->pkcs12_file_inline, !options->ca_file))
640  {
641  goto err;
642  }
643  }
644 #ifdef ENABLE_PKCS11
645  else if (options->pkcs11_providers[0])
646  {
647  if (!tls_ctx_use_pkcs11(new_ctx, options->pkcs11_id_management, options->pkcs11_id))
648  {
649  msg(M_WARN, "Cannot load certificate \"%s\" using PKCS#11 interface",
650  options->pkcs11_id);
651  goto err;
652  }
653  }
654 #endif
655 #ifdef ENABLE_CRYPTOAPI
656  else if (options->cryptoapi_cert)
657  {
658  tls_ctx_load_cryptoapi(new_ctx, options->cryptoapi_cert);
659  }
660 #endif
661 #ifdef ENABLE_MANAGEMENT
662  else if (options->management_flags & MF_EXTERNAL_CERT)
663  {
664  char *cert = management_query_cert(management,
665  options->management_certificate);
666  tls_ctx_load_cert_file(new_ctx, INLINE_FILE_TAG, cert);
667  free(cert);
668  }
669 #endif
670  else if (options->cert_file)
671  {
672  tls_ctx_load_cert_file(new_ctx, options->cert_file, options->cert_file_inline);
673  }
674 
675  if (options->priv_key_file)
676  {
677  if (0 != tls_ctx_load_priv_file(new_ctx, options->priv_key_file,
678  options->priv_key_file_inline))
679  {
680  goto err;
681  }
682  }
683 #ifdef ENABLE_MANAGEMENT
684  else if (options->management_flags & MF_EXTERNAL_KEY)
685  {
687  {
688  msg(M_WARN, "Cannot initialize mamagement-external-key");
689  goto err;
690  }
691  }
692 #endif
693 
694  if (options->ca_file || options->ca_path)
695  {
696  tls_ctx_load_ca(new_ctx, options->ca_file, options->ca_file_inline,
697  options->ca_path, options->tls_server);
698  }
699 
700  /* Load extra certificates that are part of our own certificate
701  * chain but shouldn't be included in the verify chain */
702  if (options->extra_certs_file)
703  {
705  }
706 
707  /* Check certificate notBefore and notAfter */
708  tls_ctx_check_cert_time(new_ctx);
709 
710  /* Read CRL */
711  if (options->crl_file && !(options->ssl_flags & SSLF_CRL_VERIFY_DIR))
712  {
713  tls_ctx_reload_crl(new_ctx, options->crl_file, options->crl_file_inline);
714  }
715 
716  /* Once keys and cert are loaded, load ECDH parameters */
717  if (options->tls_server)
718  {
719  tls_ctx_load_ecdh_params(new_ctx, options->ecdh_curve);
720  }
721 
722 #ifdef ENABLE_CRYPTO_MBEDTLS
723  /* Personalise the random by mixing in the certificate */
724  tls_ctx_personalise_random(new_ctx);
725 #endif
726 
727  tls_clear_error();
728  return;
729 
730 err:
731  tls_clear_error();
732  tls_ctx_free(new_ctx);
733  return;
734 }
735 
736 /*
737  * Map internal constants to ascii names.
738  */
739 static const char *
740 state_name(int state)
741 {
742  switch (state)
743  {
744  case S_UNDEF:
745  return "S_UNDEF";
746 
747  case S_INITIAL:
748  return "S_INITIAL";
749 
750  case S_PRE_START:
751  return "S_PRE_START";
752 
753  case S_START:
754  return "S_START";
755 
756  case S_SENT_KEY:
757  return "S_SENT_KEY";
758 
759  case S_GOT_KEY:
760  return "S_GOT_KEY";
761 
762  case S_ACTIVE:
763  return "S_ACTIVE";
764 
765  case S_NORMAL_OP:
766  return "S_NORMAL_OP";
767 
768  case S_ERROR:
769  return "S_ERROR";
770 
771  default:
772  return "S_???";
773  }
774 }
775 
776 static const char *
778 {
779  switch (op)
780  {
782  return "P_CONTROL_HARD_RESET_CLIENT_V1";
783 
785  return "P_CONTROL_HARD_RESET_SERVER_V1";
786 
788  return "P_CONTROL_HARD_RESET_CLIENT_V2";
789 
791  return "P_CONTROL_HARD_RESET_SERVER_V2";
792 
794  return "P_CONTROL_HARD_RESET_CLIENT_V3";
795 
797  return "P_CONTROL_SOFT_RESET_V1";
798 
799  case P_CONTROL_V1:
800  return "P_CONTROL_V1";
801 
802  case P_ACK_V1:
803  return "P_ACK_V1";
804 
805  case P_DATA_V1:
806  return "P_DATA_V1";
807 
808  case P_DATA_V2:
809  return "P_DATA_V2";
810 
811  default:
812  return "P_???";
813  }
814 }
815 
816 static const char *
818 {
819  switch (index)
820  {
821  case TM_ACTIVE:
822  return "TM_ACTIVE";
823 
824  case TM_UNTRUSTED:
825  return "TM_UNTRUSTED";
826 
827  case TM_LAME_DUCK:
828  return "TM_LAME_DUCK";
829 
830  default:
831  return "TM_???";
832  }
833 }
834 
835 /*
836  * For debugging.
837  */
838 static const char *
839 print_key_id(struct tls_multi *multi, struct gc_arena *gc)
840 {
841  int i;
842  struct buffer out = alloc_buf_gc(256, gc);
843 
844  for (i = 0; i < KEY_SCAN_SIZE; ++i)
845  {
846  struct key_state *ks = multi->key_scan[i];
847  buf_printf(&out, " [key#%d state=%s id=%d sid=%s]", i,
848  state_name(ks->state), ks->key_id,
850  }
851 
852  return BSTR(&out);
853 }
854 
855 bool
856 is_hard_reset(int op, int key_method)
857 {
858  if (!key_method || key_method == 1)
859  {
861  {
862  return true;
863  }
864  }
865 
866  if (!key_method || key_method >= 2)
867  {
870  {
871  return true;
872  }
873  }
874 
875  return false;
876 }
877 
901 static void
903 {
904  update_time();
905 
906  CLEAR(*ks);
907 
908  /*
909  * Build TLS object that reads/writes ciphertext
910  * to/from memory BIOs.
911  */
912  key_state_ssl_init(&ks->ks_ssl, &session->opt->ssl_ctx, session->opt->server,
913  session);
914 
915  /* Set control-channel initiation mode */
916  ks->initial_opcode = session->initial_opcode;
918  ks->state = S_INITIAL;
919  ks->key_id = session->key_id;
920 
921  /*
922  * key_id increments to KEY_ID_MASK then recycles back to 1.
923  * This way you know that if key_id is 0, it is the first key.
924  */
925  ++session->key_id;
926  session->key_id &= P_KEY_ID_MASK;
927  if (!session->key_id)
928  {
929  session->key_id = 1;
930  }
931 
932  /* allocate key source material object */
933  ALLOC_OBJ_CLEAR(ks->key_src, struct key_source2);
934 
935  /* allocate reliability objects */
938  ALLOC_OBJ_CLEAR(ks->rec_ack, struct reliable_ack);
939 
940  /* allocate buffers */
943  ks->ack_write_buf = alloc_buf(BUF_SIZE(&session->opt->frame));
944  reliable_init(ks->send_reliable, BUF_SIZE(&session->opt->frame),
946  ks->key_id ? false : session->opt->xmit_hold);
947  reliable_init(ks->rec_reliable, BUF_SIZE(&session->opt->frame),
949  false);
951 
952  /* init packet ID tracker */
953  if (session->opt->replay)
954  {
956  session->opt->replay_window, session->opt->replay_time, "SSL",
957  ks->key_id);
958  }
959 
960  ks->crypto_options.pid_persist = NULL;
961 
962 #ifdef MANAGEMENT_DEF_AUTH
963  ks->mda_key_id = session->opt->mda_context->mda_key_id_counter++;
964 #endif
965 }
966 
967 
981 static void
982 key_state_free(struct key_state *ks, bool clear)
983 {
984  ks->state = S_UNDEF;
985 
987 
991  free_buf(&ks->ack_write_buf);
993 
994  if (ks->send_reliable)
995  {
997  free(ks->send_reliable);
998  }
999 
1000  if (ks->rec_reliable)
1001  {
1003  free(ks->rec_reliable);
1004  }
1005 
1006  if (ks->rec_ack)
1007  {
1008  free(ks->rec_ack);
1009  }
1010 
1011  if (ks->key_src)
1012  {
1013  free(ks->key_src);
1014  }
1015 
1017 
1018 #ifdef PLUGIN_DEF_AUTH
1020 #endif
1021 
1022  if (clear)
1023  {
1024  secure_memzero(ks, sizeof(*ks));
1025  }
1026 }
1027 
1041 static inline bool
1043 {
1044  return (session->opt->auth_user_pass_verify_script
1046 #ifdef MANAGEMENT_DEF_AUTH
1047  || management_enable_def_auth(management)
1048 #endif
1049  );
1050 }
1051 
1052 
1073 static void
1075 {
1076  struct gc_arena gc = gc_new();
1077 
1078  dmsg(D_TLS_DEBUG, "TLS: tls_session_init: entry");
1079 
1080  CLEAR(*session);
1081 
1082  /* Set options data to point to parent's option structure */
1083  session->opt = &multi->opt;
1084 
1085  /* Randomize session # if it is 0 */
1086  while (!session_id_defined(&session->session_id))
1087  {
1088  session_id_random(&session->session_id);
1089  }
1090 
1091  /* Are we a TLS server or client? */
1092  ASSERT(session->opt->key_method >= 1);
1093  if (session->opt->key_method == 1)
1094  {
1095  session->initial_opcode = session->opt->server ?
1097  }
1098  else /* session->opt->key_method >= 2 */
1099  {
1100  if (session->opt->server)
1101  {
1103  }
1104  else
1105  {
1106  session->initial_opcode = session->opt->tls_crypt_v2 ?
1108  }
1109  }
1110 
1111  /* Initialize control channel authentication parameters */
1112  session->tls_wrap = session->opt->tls_wrap;
1113  session->tls_wrap.work = alloc_buf(BUF_SIZE(&session->opt->frame));
1114 
1115  /* initialize packet ID replay window for --tls-auth */
1117  session->opt->replay_window,
1118  session->opt->replay_time,
1119  "TLS_WRAP", session->key_id);
1120 
1121  /* load most recent packet-id to replay protect on --tls-auth */
1123  &session->tls_wrap.opt.packet_id);
1124 
1125  key_state_init(session, &session->key[KS_PRIMARY]);
1126 
1127  dmsg(D_TLS_DEBUG, "TLS: tls_session_init: new session object, sid=%s",
1128  session_id_print(&session->session_id, &gc));
1129 
1130  gc_free(&gc);
1131 }
1132 
1145 static void
1147 {
1148  tls_wrap_free(&session->tls_wrap);
1149 
1150  for (size_t i = 0; i < KS_SIZE; ++i)
1151  {
1152  key_state_free(&session->key[i], false);
1153  }
1154 
1155  if (session->common_name)
1156  {
1157  free(session->common_name);
1158  }
1159 
1160  cert_hash_free(session->cert_hash_set);
1161 
1162  if (clear)
1163  {
1164  secure_memzero(session, sizeof(*session));
1165  }
1166 }
1167 
1173 static void
1174 move_session(struct tls_multi *multi, int dest, int src, bool reinit_src)
1175 {
1176  msg(D_TLS_DEBUG_LOW, "TLS: move_session: dest=%s src=%s reinit_src=%d",
1177  session_index_name(dest),
1178  session_index_name(src),
1179  reinit_src);
1180  ASSERT(src != dest);
1181  ASSERT(src >= 0 && src < TM_SIZE);
1182  ASSERT(dest >= 0 && dest < TM_SIZE);
1183  tls_session_free(&multi->session[dest], false);
1184  multi->session[dest] = multi->session[src];
1185 
1186  if (reinit_src)
1187  {
1188  tls_session_init(multi, &multi->session[src]);
1189  }
1190  else
1191  {
1192  secure_memzero(&multi->session[src], sizeof(multi->session[src]));
1193  }
1194 
1195  dmsg(D_TLS_DEBUG, "TLS: move_session: exit");
1196 }
1197 
1198 static void
1200 {
1201  tls_session_free(session, false);
1202  tls_session_init(multi, session);
1203 }
1204 
1205 /*
1206  * Used to determine in how many seconds we should be
1207  * called again.
1208  */
1209 static inline void
1210 compute_earliest_wakeup(interval_t *earliest, interval_t seconds_from_now)
1211 {
1212  if (seconds_from_now < *earliest)
1213  {
1214  *earliest = seconds_from_now;
1215  }
1216  if (*earliest < 0)
1217  {
1218  *earliest = 0;
1219  }
1220 }
1221 
1222 /*
1223  * Return true if "lame duck" or retiring key has expired and can
1224  * no longer be used.
1225  */
1226 static inline bool
1228 {
1229  const struct key_state *lame = &session->key[KS_LAME_DUCK];
1230  if (lame->state >= S_INITIAL)
1231  {
1232  const time_t local_now = now;
1233  ASSERT(lame->must_die); /* a lame duck key must always have an expiration */
1234  if (local_now < lame->must_die)
1235  {
1236  compute_earliest_wakeup(wakeup, lame->must_die - local_now);
1237  return false;
1238  }
1239  else
1240  {
1241  return true;
1242  }
1243  }
1244  else if (lame->state == S_ERROR)
1245  {
1246  return true;
1247  }
1248  else
1249  {
1250  return false;
1251  }
1252 }
1253 
1254 struct tls_multi *
1256 {
1257  struct tls_multi *ret;
1258 
1259  ALLOC_OBJ_CLEAR(ret, struct tls_multi);
1260 
1261  /* get command line derived options */
1262  ret->opt = *tls_options;
1263 
1264  /* set up list of keys to be scanned by data channel encrypt and decrypt routines */
1265  ASSERT(SIZE(ret->key_scan) == 3);
1266  ret->key_scan[0] = &ret->session[TM_ACTIVE].key[KS_PRIMARY];
1267  ret->key_scan[1] = &ret->session[TM_ACTIVE].key[KS_LAME_DUCK];
1268  ret->key_scan[2] = &ret->session[TM_LAME_DUCK].key[KS_LAME_DUCK];
1269 
1270  /* By default not use P_DATA_V2 */
1271  ret->use_peer_id = false;
1272 
1273  return ret;
1274 }
1275 
1276 void
1277 tls_multi_init_finalize(struct tls_multi *multi, const struct frame *frame)
1278 {
1280 
1281  /* initialize the active and untrusted sessions */
1282 
1283  tls_session_init(multi, &multi->session[TM_ACTIVE]);
1284 
1285  if (!multi->opt.single_session)
1286  {
1287  tls_session_init(multi, &multi->session[TM_UNTRUSTED]);
1288  }
1289 }
1290 
1291 /*
1292  * Initialize and finalize a standalone tls-auth verification object.
1293  */
1294 
1295 struct tls_auth_standalone *
1297  struct gc_arena *gc)
1298 {
1299  struct tls_auth_standalone *tas;
1300 
1301  ALLOC_OBJ_CLEAR_GC(tas, struct tls_auth_standalone, gc);
1302 
1303  tas->tls_wrap = tls_options->tls_wrap;
1304 
1305  /*
1306  * Standalone tls-auth is in read-only mode with respect to TLS
1307  * control channel state. After we build a new client instance
1308  * object, we will process this session-initiating packet for real.
1309  */
1311 
1312  /* get initial frame parms, still need to finalize */
1313  tas->frame = tls_options->frame;
1314 
1315  return tas;
1316 }
1317 
1318 void
1320  const struct frame *frame)
1321 {
1323 }
1324 
1325 /*
1326  * Set local and remote option compatibility strings.
1327  * Used to verify compatibility of local and remote option
1328  * sets.
1329  */
1330 void
1332  const char *local,
1333  const char *remote)
1334 {
1335 #ifdef ENABLE_OCC
1336  /* initialize options string */
1337  multi->opt.local_options = local;
1338  multi->opt.remote_options = remote;
1339 #endif
1340 }
1341 
1342 /*
1343  * Cleanup a tls_multi structure and free associated memory allocations.
1344  */
1345 void
1346 tls_multi_free(struct tls_multi *multi, bool clear)
1347 {
1348  int i;
1349 
1350  ASSERT(multi);
1351 
1352 #if P2MP_SERVER
1353  auth_set_client_reason(multi, NULL);
1354 
1355  free(multi->peer_info);
1356 #endif
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 
1917 bool
1918 tls_item_in_cipher_list(const char *item, const char *list)
1919 {
1920  char *tmp_ciphers = string_alloc(list, NULL);
1921  char *tmp_ciphers_orig = tmp_ciphers;
1922 
1923  const char *token = strtok(tmp_ciphers, ":");
1924  while (token)
1925  {
1926  if (0 == strcmp(token, item))
1927  {
1928  break;
1929  }
1930  token = strtok(NULL, ":");
1931  }
1932  free(tmp_ciphers_orig);
1933 
1934  return token != NULL;
1935 }
1936 
1937 void
1938 tls_poor_mans_ncp(struct options *o, const char *remote_ciphername)
1939 {
1940  if (o->ncp_enabled && remote_ciphername
1941  && 0 != strcmp(o->ciphername, remote_ciphername))
1942  {
1943  if (tls_item_in_cipher_list(remote_ciphername, o->ncp_ciphers))
1944  {
1945  o->ciphername = string_alloc(remote_ciphername, &o->gc);
1946  msg(D_TLS_DEBUG_LOW, "Using peer cipher '%s'", o->ciphername);
1947  }
1948  }
1949 }
1950 
1957 static bool
1959 {
1960  bool ret = false;
1961  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1962  const struct session_id *client_sid = session->opt->server ?
1963  &ks->session_id_remote : &session->session_id;
1964  const struct session_id *server_sid = !session->opt->server ?
1965  &ks->session_id_remote : &session->session_id;
1966 
1967  ASSERT(ks->authenticated);
1968 
1969  ks->crypto_options.flags = session->opt->crypto_flags;
1971  &session->opt->key_type, ks->key_src, client_sid, server_sid,
1972  session->opt->server))
1973  {
1974  msg(D_TLS_ERRORS, "TLS Error: generate_key_expansion failed");
1975  goto cleanup;
1976  }
1978  &session->opt->renegotiate_bytes);
1979 
1980  ret = true;
1981 cleanup:
1982  secure_memzero(ks->key_src, sizeof(*ks->key_src));
1983  return ret;
1984 }
1985 
1986 bool
1988  struct options *options, struct frame *frame,
1989  struct frame *frame_fragment)
1990 {
1991  if (!session->opt->server
1992  && 0 != strcmp(options->ciphername, session->opt->config_ciphername)
1993  && !tls_item_in_cipher_list(options->ciphername, options->ncp_ciphers))
1994  {
1995  msg(D_TLS_ERRORS, "Error: pushed cipher not allowed - %s not in %s or %s",
1996  options->ciphername, session->opt->config_ciphername,
1997  options->ncp_ciphers);
1998  /* undo cipher push, abort connection setup */
1999  options->ciphername = session->opt->config_ciphername;
2000  return false;
2001  }
2002 
2003  if (strcmp(options->ciphername, session->opt->config_ciphername))
2004  {
2005  msg(D_HANDSHAKE, "Data Channel: using negotiated cipher '%s'",
2006  options->ciphername);
2007  if (options->keysize)
2008  {
2009  msg(D_HANDSHAKE, "NCP: overriding user-set keysize with default");
2010  options->keysize = 0;
2011  }
2012  }
2013 
2014  init_key_type(&session->opt->key_type, options->ciphername,
2015  options->authname, options->keysize, true, true);
2016 
2017  bool packet_id_long_form = cipher_kt_mode_ofb_cfb(session->opt->key_type.cipher);
2018  session->opt->crypto_flags &= ~(CO_PACKET_ID_LONG_FORM);
2019  if (packet_id_long_form)
2020  {
2022  }
2023 
2024  /* Update frame parameters: undo worst-case overhead, add actual overhead */
2026  crypto_adjust_frame_parameters(frame, &session->opt->key_type,
2027  options->replay, packet_id_long_form);
2028  frame_finalize(frame, options->ce.link_mtu_defined, options->ce.link_mtu,
2029  options->ce.tun_mtu_defined, options->ce.tun_mtu);
2030  frame_init_mssfix(frame, options);
2031  frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
2032 
2033  /*
2034  * mssfix uses data channel framing, which at this point contains
2035  * actual overhead. Fragmentation logic uses frame_fragment, which
2036  * still contains worst case overhead. Replace it with actual overhead
2037  * to prevent unneeded fragmentation.
2038  */
2039 
2040  if (frame_fragment)
2041  {
2043  crypto_adjust_frame_parameters(frame_fragment, &session->opt->key_type,
2044  options->replay, packet_id_long_form);
2045  frame_set_mtu_dynamic(frame_fragment, options->ce.fragment, SET_MTU_UPPER_BOUND);
2046  frame_print(frame_fragment, D_MTU_INFO, "Fragmentation MTU parms");
2047  }
2048 
2050 }
2051 
2052 static bool
2054  uint8_t *out,
2055  int outlen)
2056 {
2057  if (!rand_bytes(out, outlen))
2058  {
2059  msg(M_FATAL, "ERROR: Random number generator cannot obtain entropy for key generation [SSL]");
2060  }
2061  if (!buf_write(buf, out, outlen))
2062  {
2063  return false;
2064  }
2065  return true;
2066 }
2067 
2068 static bool
2070  struct buffer *buf,
2071  bool server)
2072 {
2073  struct key_source *k = &k2->client;
2074  if (server)
2075  {
2076  k = &k2->server;
2077  }
2078 
2079  CLEAR(*k);
2080 
2081  if (!server)
2082  {
2083  if (!random_bytes_to_buf(buf, k->pre_master, sizeof(k->pre_master)))
2084  {
2085  return false;
2086  }
2087  }
2088 
2089  if (!random_bytes_to_buf(buf, k->random1, sizeof(k->random1)))
2090  {
2091  return false;
2092  }
2093  if (!random_bytes_to_buf(buf, k->random2, sizeof(k->random2)))
2094  {
2095  return false;
2096  }
2097 
2098  return true;
2099 }
2100 
2101 static int
2103  struct buffer *buf,
2104  bool server)
2105 {
2106  struct key_source *k = &k2->client;
2107 
2108  if (!server)
2109  {
2110  k = &k2->server;
2111  }
2112 
2113  CLEAR(*k);
2114 
2115  if (server)
2116  {
2117  if (!buf_read(buf, k->pre_master, sizeof(k->pre_master)))
2118  {
2119  return 0;
2120  }
2121  }
2122 
2123  if (!buf_read(buf, k->random1, sizeof(k->random1)))
2124  {
2125  return 0;
2126  }
2127  if (!buf_read(buf, k->random2, sizeof(k->random2)))
2128  {
2129  return 0;
2130  }
2131 
2132  return 1;
2133 }
2134 
2135 static void
2137 {
2138  struct buffer *b;
2139 
2140  while ((b = buffer_list_peek(ks->paybuf)))
2141  {
2143  buffer_list_pop(ks->paybuf);
2144  }
2145 }
2146 
2147 /* true if no in/out acknowledgements pending */
2148 #define FULL_SYNC \
2149  (reliable_empty(ks->send_reliable) && reliable_ack_empty(ks->rec_ack))
2150 
2151 /*
2152  * Move the active key to the lame duck key and reinitialize the
2153  * active key.
2154  */
2155 static void
2157 {
2158  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2159  struct key_state *ks_lame = &session->key[KS_LAME_DUCK]; /* retiring key */
2160 
2161  ks->must_die = now + session->opt->transition_window; /* remaining lifetime of old key */
2162  key_state_free(ks_lame, false);
2163  *ks_lame = *ks;
2164 
2165  key_state_init(session, ks);
2166  ks->session_id_remote = ks_lame->session_id_remote;
2167  ks->remote_addr = ks_lame->remote_addr;
2168 }
2169 
2170 /*
2171  * Read/write strings from/to a struct buffer with a u16 length prefix.
2172  */
2173 
2174 static bool
2176 {
2177  if (!buf_write_u16(buf, 0))
2178  {
2179  return false;
2180  }
2181  return true;
2182 }
2183 
2184 static bool
2185 write_string(struct buffer *buf, const char *str, const int maxlen)
2186 {
2187  const int len = strlen(str) + 1;
2188  if (len < 1 || (maxlen >= 0 && len > maxlen))
2189  {
2190  return false;
2191  }
2192  if (!buf_write_u16(buf, len))
2193  {
2194  return false;
2195  }
2196  if (!buf_write(buf, str, len))
2197  {
2198  return false;
2199  }
2200  return true;
2201 }
2202 
2203 static bool
2204 read_string(struct buffer *buf, char *str, const unsigned int capacity)
2205 {
2206  const int len = buf_read_u16(buf);
2207  if (len < 1 || len > (int)capacity)
2208  {
2209  return false;
2210  }
2211  if (!buf_read(buf, str, len))
2212  {
2213  return false;
2214  }
2215  str[len-1] = '\0';
2216  return true;
2217 }
2218 
2219 static char *
2221 {
2222  const int len = buf_read_u16(buf);
2223  char *str;
2224 
2225  if (len < 1)
2226  {
2227  return NULL;
2228  }
2229  str = (char *) malloc(len);
2230  check_malloc_return(str);
2231  if (!buf_read(buf, str, len))
2232  {
2233  free(str);
2234  return NULL;
2235  }
2236  str[len-1] = '\0';
2237  return str;
2238 }
2239 
2240 /*
2241  * Handle the reading and writing of key data to and from
2242  * the TLS control channel (cleartext).
2243  */
2244 
2245 static bool
2247 {
2248  struct key key;
2249  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2250 
2251  ASSERT(session->opt->key_method == 1);
2252  ASSERT(buf_init(buf, 0));
2253 
2254  generate_key_random(&key, &session->opt->key_type);
2255  if (!check_key(&key, &session->opt->key_type))
2256  {
2257  msg(D_TLS_ERRORS, "TLS Error: Bad encrypting key generated");
2258  return false;
2259  }
2260 
2261  if (!write_key(&key, &session->opt->key_type, buf))
2262  {
2263  msg(D_TLS_ERRORS, "TLS Error: write_key failed");
2264  return false;
2265  }
2266 
2268  &session->opt->key_type, OPENVPN_OP_ENCRYPT,
2269  "Data Channel Encrypt");
2270  secure_memzero(&key, sizeof(key));
2271 
2272  /* send local options string */
2273  {
2274  const char *local_options = local_options_string(session);
2275  const int optlen = strlen(local_options) + 1;
2276  if (!buf_write(buf, local_options, optlen))
2277  {
2278  msg(D_TLS_ERRORS, "TLS Error: KM1 write options failed");
2279  return false;
2280  }
2281  }
2282 
2283  return true;
2284 }
2285 
2286 static bool
2288 {
2289  struct gc_arena gc = gc_new();
2290  bool ret = false;
2291 
2292  if (session->opt->push_peer_info_detail > 0)
2293  {
2294  struct env_set *es = session->opt->es;
2295  struct env_item *e;
2296  struct buffer out = alloc_buf_gc(512*3, &gc);
2297 
2298  /* push version */
2299  buf_printf(&out, "IV_VER=%s\n", PACKAGE_VERSION);
2300 
2301  /* push platform */
2302 #if defined(TARGET_LINUX)
2303  buf_printf(&out, "IV_PLAT=linux\n");
2304 #elif defined(TARGET_SOLARIS)
2305  buf_printf(&out, "IV_PLAT=solaris\n");
2306 #elif defined(TARGET_OPENBSD)
2307  buf_printf(&out, "IV_PLAT=openbsd\n");
2308 #elif defined(TARGET_DARWIN)
2309  buf_printf(&out, "IV_PLAT=mac\n");
2310 #elif defined(TARGET_NETBSD)
2311  buf_printf(&out, "IV_PLAT=netbsd\n");
2312 #elif defined(TARGET_FREEBSD)
2313  buf_printf(&out, "IV_PLAT=freebsd\n");
2314 #elif defined(TARGET_ANDROID)
2315  buf_printf(&out, "IV_PLAT=android\n");
2316 #elif defined(_WIN32)
2317  buf_printf(&out, "IV_PLAT=win\n");
2318 #endif
2319 
2320  /* support for P_DATA_V2 */
2321  buf_printf(&out, "IV_PROTO=2\n");
2322 
2323  /* support for Negotiable Crypto Parameters */
2324  if (session->opt->ncp_enabled
2325  && (session->opt->mode == MODE_SERVER || session->opt->pull))
2326  {
2327  buf_printf(&out, "IV_NCP=2\n");
2328  }
2329 
2330  /* push compression status */
2331 #ifdef USE_COMP
2332  comp_generate_peer_info_string(&session->opt->comp_options, &out);
2333 #endif
2334 
2335  if (session->opt->push_peer_info_detail >= 2)
2336  {
2337  /* push mac addr */
2338  struct route_gateway_info rgi;
2339  get_default_gateway(&rgi, session->opt->net_ctx);
2340  if (rgi.flags & RGI_HWADDR_DEFINED)
2341  {
2342  buf_printf(&out, "IV_HWADDR=%s\n", format_hex_ex(rgi.hwaddr, 6, 0, 1, ":", &gc));
2343  }
2344  buf_printf(&out, "IV_SSL=%s\n", get_ssl_library_version() );
2345 #if defined(_WIN32)
2346  buf_printf(&out, "IV_PLAT_VER=%s\n", win32_version_string(&gc, false));
2347 #endif
2348  }
2349 
2350  /* push env vars that begin with UV_, IV_PLAT_VER and IV_GUI_VER */
2351  for (e = es->list; e != NULL; e = e->next)
2352  {
2353  if (e->string)
2354  {
2355  if ((((strncmp(e->string, "UV_", 3)==0
2356  || strncmp(e->string, "IV_PLAT_VER=", sizeof("IV_PLAT_VER=")-1)==0)
2357  && session->opt->push_peer_info_detail >= 2)
2358  || (strncmp(e->string,"IV_GUI_VER=",sizeof("IV_GUI_VER=")-1)==0))
2359  && buf_safe(&out, strlen(e->string)+1))
2360  {
2361  buf_printf(&out, "%s\n", e->string);
2362  }
2363  }
2364  }
2365 
2366  if (!write_string(buf, BSTR(&out), -1))
2367  {
2368  goto error;
2369  }
2370  }
2371  else
2372  {
2373  if (!write_empty_string(buf)) /* no peer info */
2374  {
2375  goto error;
2376  }
2377  }
2378  ret = true;
2379 
2380 error:
2381  gc_free(&gc);
2382  return ret;
2383 }
2384 
2385 static bool
2387 {
2388  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2389 
2390  ASSERT(session->opt->key_method == 2);
2391  ASSERT(buf_init(buf, 0));
2392 
2393  /* write a uint32 0 */
2394  if (!buf_write_u32(buf, 0))
2395  {
2396  goto error;
2397  }
2398 
2399  /* write key_method + flags */
2400  if (!buf_write_u8(buf, (session->opt->key_method & KEY_METHOD_MASK)))
2401  {
2402  goto error;
2403  }
2404 
2405  /* write key source material */
2406  if (!key_source2_randomize_write(ks->key_src, buf, session->opt->server))
2407  {
2408  goto error;
2409  }
2410 
2411  /* write options string */
2412  {
2413  if (!write_string(buf, local_options_string(session), TLS_OPTIONS_LEN))
2414  {
2415  goto error;
2416  }
2417  }
2418 
2419  /* write username/password if specified */
2421  {
2422 #ifdef ENABLE_MANAGEMENT
2423  auth_user_pass_setup(session->opt->auth_user_pass_file, session->opt->sci);
2424 #else
2425  auth_user_pass_setup(session->opt->auth_user_pass_file, NULL);
2426 #endif
2427  struct user_pass *up = &auth_user_pass;
2428 
2429  /*
2430  * If we have a valid auth-token, send that instead of real
2431  * username/password
2432  */
2433  if (auth_token.defined)
2434  up = &auth_token;
2435 
2436  if (!write_string(buf, up->username, -1))
2437  {
2438  goto error;
2439  }
2440  else if (!write_string(buf, up->password, -1))
2441  {
2442  goto error;
2443  }
2444  /* if auth-nocache was specified, the auth_user_pass object reaches
2445  * a "complete" state only after having received the push-reply
2446  * message.
2447  *
2448  * For this reason, skip the purge operation here if no push-reply
2449  * message has been received yet.
2450  *
2451  * This normally happens upon first negotiation only.
2452  */
2454  {
2456  }
2457  }
2458  else
2459  {
2460  if (!write_empty_string(buf)) /* no username */
2461  {
2462  goto error;
2463  }
2464  if (!write_empty_string(buf)) /* no password */
2465  {
2466  goto error;
2467  }
2468  }
2469 
2470  if (!push_peer_info(buf, session))
2471  {
2472  goto error;
2473  }
2474 
2475  /* Generate tunnel keys if we're a TLS server.
2476  * If we're a p2mp server and IV_NCP >= 2 is negotiated, the first key
2477  * generation is postponed until after the pull/push, so we can process pushed
2478  * cipher directives.
2479  */
2480  if (session->opt->server && !(session->opt->ncp_enabled
2481  && session->opt->mode == MODE_SERVER && ks->key_id <= 0))
2482  {
2483  if (ks->authenticated)
2484  {
2486  {
2487  msg(D_TLS_ERRORS, "TLS Error: server generate_key_expansion failed");
2488  goto error;
2489  }
2490  }
2491  }
2492 
2493  return true;
2494 
2495 error:
2496  msg(D_TLS_ERRORS, "TLS Error: Key Method #2 write failed");
2497  secure_memzero(ks->key_src, sizeof(*ks->key_src));
2498  return false;
2499 }
2500 
2501 static bool
2503 {
2504  int status;
2505  struct key key;
2506  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2507 
2508  ASSERT(session->opt->key_method == 1);
2509 
2510  if (!session->verified)
2511  {
2512  msg(D_TLS_ERRORS,
2513  "TLS Error: Certificate verification failed (key-method 1)");
2514  goto error;
2515  }
2516 
2517  status = read_key(&key, &session->opt->key_type, buf);
2518  if (status != 1)
2519  {
2520  msg(D_TLS_ERRORS,
2521  "TLS Error: Error reading data channel key from plaintext buffer");
2522  goto error;
2523  }
2524 
2525  if (!check_key(&key, &session->opt->key_type))
2526  {
2527  msg(D_TLS_ERRORS, "TLS Error: Bad decrypting key received from peer");
2528  goto error;
2529  }
2530 
2531  if (buf->len < 1)
2532  {
2533  msg(D_TLS_ERRORS, "TLS Error: Missing options string");
2534  goto error;
2535  }
2536 
2537 #ifdef ENABLE_OCC
2538  /* compare received remote options string
2539  * with our locally computed options string */
2540  if (!session->opt->disable_occ
2541  && !options_cmp_equal_safe((char *) BPTR(buf), session->opt->remote_options, buf->len))
2542  {
2543  options_warning_safe((char *) BPTR(buf), session->opt->remote_options, buf->len);
2544  }
2545 #endif
2546 
2547  buf_clear(buf);
2548 
2550  &session->opt->key_type, OPENVPN_OP_DECRYPT,
2551  "Data Channel Decrypt");
2552  secure_memzero(&key, sizeof(key));
2553  ks->authenticated = true;
2554  return true;
2555 
2556 error:
2557  buf_clear(buf);
2558  secure_memzero(&key, sizeof(key));
2559  return false;
2560 }
2561 
2562 static bool
2563 key_method_2_read(struct buffer *buf, struct tls_multi *multi, struct tls_session *session)
2564 {
2565  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2566 
2567  int key_method_flags;
2568  bool username_status, password_status;
2569 
2570  struct gc_arena gc = gc_new();
2571  char *options;
2572  struct user_pass *up = NULL;
2573 
2574  /* allocate temporary objects */
2575  ALLOC_ARRAY_CLEAR_GC(options, char, TLS_OPTIONS_LEN, &gc);
2576 
2577  ASSERT(session->opt->key_method == 2);
2578 
2579  /* discard leading uint32 */
2580  if (!buf_advance(buf, 4))
2581  {
2582  msg(D_TLS_ERRORS, "TLS ERROR: Plaintext buffer too short (%d bytes).",
2583  buf->len);
2584  goto error;
2585  }
2586 
2587  /* get key method */
2588  key_method_flags = buf_read_u8(buf);
2589  if ((key_method_flags & KEY_METHOD_MASK) != 2)
2590  {
2591  msg(D_TLS_ERRORS,
2592  "TLS ERROR: Unknown key_method/flags=%d received from remote host",
2593  key_method_flags);
2594  goto error;
2595  }
2596 
2597  /* get key source material (not actual keys yet) */
2598  if (!key_source2_read(ks->key_src, buf, session->opt->server))
2599  {
2600  msg(D_TLS_ERRORS, "TLS Error: Error reading remote data channel key source entropy from plaintext buffer");
2601  goto error;
2602  }
2603 
2604  /* get options */
2605  if (!read_string(buf, options, TLS_OPTIONS_LEN))
2606  {
2607  msg(D_TLS_ERRORS, "TLS Error: Failed to read required OCC options string");
2608  goto error;
2609  }
2610 
2611  ks->authenticated = false;
2612 
2613  /* always extract username + password fields from buf, even if not
2614  * authenticating for it, because otherwise we can't get at the
2615  * peer_info data which follows behind
2616  */
2617  ALLOC_OBJ_CLEAR_GC(up, struct user_pass, &gc);
2618  username_status = read_string(buf, up->username, USER_PASS_LEN);
2619  password_status = read_string(buf, up->password, USER_PASS_LEN);
2620 
2621 #if P2MP_SERVER
2622  /* get peer info from control channel */
2623  free(multi->peer_info);
2624  multi->peer_info = read_string_alloc(buf);
2625  if (multi->peer_info)
2626  {
2627  output_peer_info_env(session->opt->es, multi->peer_info);
2628  }
2629 
2630  free(multi->remote_ciphername);
2631  multi->remote_ciphername =
2632  options_string_extract_option(options, "cipher", NULL);
2633 
2634  if (tls_peer_info_ncp_ver(multi->peer_info) < 2)
2635  {
2636  /* Peer does not support NCP, but leave NCP enabled if the local and
2637  * remote cipher do not match to attempt 'poor-man's NCP'.
2638  */
2639  if (multi->remote_ciphername == NULL
2640  || 0 == strcmp(multi->remote_ciphername, multi->opt.config_ciphername))
2641  {
2642  session->opt->ncp_enabled = false;
2643  }
2644  }
2645 #endif /* if P2MP_SERVER */
2646 
2647  if (tls_session_user_pass_enabled(session))
2648  {
2649  /* Perform username/password authentication */
2650  if (!username_status || !password_status)
2651  {
2652  CLEAR(*up);
2653  if (!(session->opt->ssl_flags & SSLF_AUTH_USER_PASS_OPTIONAL))
2654  {
2655  msg(D_TLS_ERRORS, "TLS Error: Auth Username/Password was not provided by peer");
2656  goto error;
2657  }
2658  }
2659 
2660  verify_user_pass(up, multi, session);
2661  }
2662  else
2663  {
2664  /* Session verification should have occurred during TLS negotiation*/
2665  if (!session->verified)
2666  {
2667  msg(D_TLS_ERRORS,
2668  "TLS Error: Certificate verification failed (key-method 2)");
2669  goto error;
2670  }
2671  ks->authenticated = true;
2672  }
2673 
2674  /* clear username and password from memory */
2675  secure_memzero(up, sizeof(*up));
2676 
2677  /* Perform final authentication checks */
2678  if (ks->authenticated)
2679  {
2680  verify_final_auth_checks(multi, session);
2681  }
2682 
2683 #ifdef ENABLE_OCC
2684  /* check options consistency */
2685  if (!session->opt->disable_occ
2686  && !options_cmp_equal(options, session->opt->remote_options))
2687  {
2688  options_warning(options, session->opt->remote_options);
2689  if (session->opt->ssl_flags & SSLF_OPT_VERIFY)
2690  {
2691  msg(D_TLS_ERRORS, "Option inconsistency warnings triggering disconnect due to --opt-verify");
2692  ks->authenticated = false;
2693  }
2694  }
2695 #endif
2696 
2697  buf_clear(buf);
2698 
2699  /*
2700  * Call OPENVPN_PLUGIN_TLS_FINAL plugin if defined, for final
2701  * veto opportunity over authentication decision.
2702  */
2704  {
2706 
2707  if (plugin_call(session->opt->plugins, OPENVPN_PLUGIN_TLS_FINAL, NULL, NULL, session->opt->es) != OPENVPN_PLUGIN_FUNC_SUCCESS)
2708  {
2709  ks->authenticated = false;
2710  }
2711 
2712  setenv_del(session->opt->es, "exported_keying_material");
2713  }
2714 
2715  /*
2716  * Generate tunnel keys if we're a client.
2717  * If --pull is enabled, the first key generation is postponed until after the
2718  * pull/push, so we can process pushed cipher directives.
2719  */
2720  if (!session->opt->server && (!session->opt->pull || ks->key_id > 0))
2721  {
2723  {
2724  msg(D_TLS_ERRORS, "TLS Error: client generate_key_expansion failed");
2725  goto error;
2726  }
2727  }
2728 
2729  gc_free(&gc);
2730  return true;
2731 
2732 error:
2733  secure_memzero(ks->key_src, sizeof(*ks->key_src));
2734  if (up)
2735  {
2736  secure_memzero(up, sizeof(*up));
2737  }
2738  buf_clear(buf);
2739  gc_free(&gc);
2740  return false;
2741 }
2742 
2743 static int
2745 {
2746  int ret = o->handshake_window;
2747  const int r2 = o->renegotiate_seconds / 2;
2748 
2749  if (o->renegotiate_seconds && r2 < ret)
2750  {
2751  ret = r2;
2752  }
2753  return ret;
2754 }
2755 
2756 /*
2757  * This is the primary routine for processing TLS stuff inside the
2758  * the main event loop. When this routine exits
2759  * with non-error status, it will set *wakeup to the number of seconds
2760  * when it wants to be called again.
2761  *
2762  * Return value is true if we have placed a packet in *to_link which we
2763  * want to send to our peer.
2764  */
2765 static bool
2766 tls_process(struct tls_multi *multi,
2767  struct tls_session *session,
2768  struct buffer *to_link,
2769  struct link_socket_actual **to_link_addr,
2770  struct link_socket_info *to_link_socket_info,
2771  interval_t *wakeup)
2772 {
2773  struct gc_arena gc = gc_new();
2774  struct buffer *buf;
2775  bool state_change = false;
2776  bool active = false;
2777  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2778  struct key_state *ks_lame = &session->key[KS_LAME_DUCK]; /* retiring key */
2779 
2780  /* Make sure we were initialized and that we're not in an error state */
2781  ASSERT(ks->state != S_UNDEF);
2782  ASSERT(ks->state != S_ERROR);
2783  ASSERT(session_id_defined(&session->session_id));
2784 
2785  /* Should we trigger a soft reset? -- new key, keeps old key for a while */
2786  if (ks->state >= S_ACTIVE
2787  && ((session->opt->renegotiate_seconds
2788  && now >= ks->established + session->opt->renegotiate_seconds)
2789  || (session->opt->renegotiate_bytes > 0
2790  && ks->n_bytes >= session->opt->renegotiate_bytes)
2791  || (session->opt->renegotiate_packets
2792  && ks->n_packets >= session->opt->renegotiate_packets)
2794  {
2795  msg(D_TLS_DEBUG_LOW, "TLS: soft reset sec=%d/%d bytes=" counter_format
2796  "/%d pkts=" counter_format "/%d",
2797  (int) (now - ks->established), session->opt->renegotiate_seconds,
2798  ks->n_bytes, session->opt->renegotiate_bytes,
2799  ks->n_packets, session->opt->renegotiate_packets);
2800  key_state_soft_reset(session);
2801  }
2802 
2803  /* Kill lame duck key transition_window seconds after primary key negotiation */
2804  if (lame_duck_must_die(session, wakeup))
2805  {
2806  key_state_free(ks_lame, true);
2807  msg(D_TLS_DEBUG_LOW, "TLS: tls_process: killed expiring key");
2808  }
2809 
2810  do
2811  {
2812  update_time();
2813 
2814  dmsg(D_TLS_DEBUG, "TLS: tls_process: chg=%d ks=%s lame=%s to_link->len=%d wakeup=%d",
2815  state_change,
2816  state_name(ks->state),
2817  state_name(ks_lame->state),
2818  to_link->len,
2819  *wakeup);
2820 
2821  state_change = false;
2822 
2823  /*
2824  * TLS activity is finished once we get to S_ACTIVE,
2825  * though we will still process acknowledgements.
2826  *
2827  * CHANGED with 2.0 -> now we may send tunnel configuration
2828  * info over the control channel.
2829  */
2830 
2831  /* Initial handshake */
2832  if (ks->state == S_INITIAL)
2833  {
2835  if (buf)
2836  {
2837  ks->must_negotiate = now + session->opt->handshake_window;
2839 
2840  /* null buffer */
2843 
2844  ks->state = S_PRE_START;
2845  state_change = true;
2846  dmsg(D_TLS_DEBUG, "TLS: Initial Handshake, sid=%s",
2847  session_id_print(&session->session_id, &gc));
2848 
2849 #ifdef ENABLE_MANAGEMENT
2851  {
2854  NULL,
2855  NULL,
2856  NULL,
2857  NULL,
2858  NULL);
2859  }
2860 #endif
2861  }
2862  }
2863 
2864  /* Are we timed out on receive? */
2865  if (now >= ks->must_negotiate)
2866  {
2867  if (ks->state < S_ACTIVE)
2868  {
2869  msg(D_TLS_ERRORS,
2870  "TLS Error: TLS key negotiation failed to occur within %d seconds (check your network connectivity)",
2871  session->opt->handshake_window);
2872  goto error;
2873  }
2874  else /* assume that ks->state == S_ACTIVE */
2875  {
2876  dmsg(D_TLS_DEBUG_MED, "STATE S_NORMAL_OP");
2877  ks->state = S_NORMAL_OP;
2878  ks->must_negotiate = 0;
2879  }
2880  }
2881 
2882  /* Wait for Initial Handshake ACK */
2883  if (ks->state == S_PRE_START && FULL_SYNC)
2884  {
2885  ks->state = S_START;
2886  state_change = true;
2887 
2888  /*
2889  * Attempt CRL reload before TLS negotiation. Won't be performed if
2890  * the file was not modified since the last reload
2891  */
2892  if (session->opt->crl_file
2893  && !(session->opt->ssl_flags & SSLF_CRL_VERIFY_DIR))
2894  {
2895  tls_ctx_reload_crl(&session->opt->ssl_ctx,
2896  session->opt->crl_file, session->opt->crl_file_inline);
2897  }
2898 
2899  /* New connection, remove any old X509 env variables */
2900  tls_x509_clear_env(session->opt->es);
2901 
2902  dmsg(D_TLS_DEBUG_MED, "STATE S_START");
2903  }
2904 
2905  /* Wait for ACK */
2906  if (((ks->state == S_GOT_KEY && !session->opt->server)
2907  || (ks->state == S_SENT_KEY && session->opt->server)))
2908  {
2909  if (FULL_SYNC)
2910  {
2911  ks->established = now;
2912  dmsg(D_TLS_DEBUG_MED, "STATE S_ACTIVE");
2914  {
2915  print_details(&ks->ks_ssl, "Control Channel:");
2916  }
2917  state_change = true;
2918  ks->state = S_ACTIVE;
2919  INCR_SUCCESS;
2920 
2921  /* Set outgoing address for data channel packets */
2922  link_socket_set_outgoing_addr(NULL, to_link_socket_info, &ks->remote_addr, session->common_name, session->opt->es);
2923 
2924  /* Flush any payload packets that were buffered before our state transitioned to S_ACTIVE */
2926 
2927 #ifdef MEASURE_TLS_HANDSHAKE_STATS
2928  show_tls_performance_stats();
2929 #endif
2930  }
2931  }
2932 
2933  /* Reliable buffer to outgoing TCP/UDP (send up to CONTROL_SEND_ACK_MAX ACKs
2934  * for previously received packets) */
2935  if (!to_link->len && reliable_can_send(ks->send_reliable))
2936  {
2937  int opcode;
2938  struct buffer b;
2939 
2940  buf = reliable_send(ks->send_reliable, &opcode);
2941  ASSERT(buf);
2942  b = *buf;
2943  INCR_SENT;
2944 
2945  write_control_auth(session, ks, &b, to_link_addr, opcode,
2946  CONTROL_SEND_ACK_MAX, true);
2947  *to_link = b;
2948  active = true;
2949  state_change = true;
2950  dmsg(D_TLS_DEBUG, "Reliable -> TCP/UDP");
2951  break;
2952  }
2953 
2954  /* Write incoming ciphertext to TLS object */
2956  if (buf)
2957  {
2958  int status = 0;
2959  if (buf->len)
2960  {
2961  status = key_state_write_ciphertext(&ks->ks_ssl, buf);
2962  if (status == -1)
2963  {
2964  msg(D_TLS_ERRORS,
2965  "TLS Error: Incoming Ciphertext -> TLS object write error");
2966  goto error;
2967  }
2968  }
2969  else
2970  {
2971  status = 1;
2972  }
2973  if (status == 1)
2974  {
2975  reliable_mark_deleted(ks->rec_reliable, buf, true);
2976  state_change = true;
2977  dmsg(D_TLS_DEBUG, "Incoming Ciphertext -> TLS");
2978  }
2979  }
2980 
2981  /* Read incoming plaintext from TLS object */
2982  buf = &ks->plaintext_read_buf;
2983  if (!buf->len)
2984  {
2985  int status;
2986 
2987  ASSERT(buf_init(buf, 0));
2989  update_time();
2990  if (status == -1)
2991  {
2992  msg(D_TLS_ERRORS, "TLS Error: TLS object -> incoming plaintext read error");
2993  goto error;
2994  }
2995  if (status == 1)
2996  {
2997  state_change = true;
2998  dmsg(D_TLS_DEBUG, "TLS -> Incoming Plaintext");
2999 
3000  /* More data may be available, wake up again asap to check. */
3001  *wakeup = 0;
3002  }
3003  }
3004 
3005  /* Send Key */
3006  buf = &ks->plaintext_write_buf;
3007  if (!buf->len && ((ks->state == S_START && !session->opt->server)
3008  || (ks->state == S_GOT_KEY && session->opt->server)))
3009  {
3010  if (session->opt->key_method == 1)
3011  {
3012  if (!key_method_1_write(buf, session))
3013  {
3014  goto error;
3015  }
3016  }
3017  else if (session->opt->key_method == 2)
3018  {
3019  if (!key_method_2_write(buf, session))
3020  {
3021  goto error;
3022  }
3023  }
3024  else
3025  {
3026  ASSERT(0);
3027  }
3028 
3029  state_change = true;
3030  dmsg(D_TLS_DEBUG_MED, "STATE S_SENT_KEY");
3031  ks->state = S_SENT_KEY;
3032  }
3033 
3034  /* Receive Key */
3035  buf = &ks->plaintext_read_buf;
3036  if (buf->len
3037  && ((ks->state == S_SENT_KEY && !session->opt->server)
3038  || (ks->state == S_START && session->opt->server)))
3039  {
3040  if (session->opt->key_method == 1)
3041  {
3042  if (!key_method_1_read(buf, session))
3043  {
3044  goto error;
3045  }
3046  }
3047  else if (session->opt->key_method == 2)
3048  {
3049  if (!key_method_2_read(buf, multi, session))
3050  {
3051  goto error;
3052  }
3053  }
3054  else
3055  {
3056  ASSERT(0);
3057  }
3058 
3059  state_change = true;
3060  dmsg(D_TLS_DEBUG_MED, "STATE S_GOT_KEY");
3061  ks->state = S_GOT_KEY;
3062  }
3063 
3064  /* Write outgoing plaintext to TLS object */
3065  buf = &ks->plaintext_write_buf;
3066  if (buf->len)
3067  {
3068  int status = key_state_write_plaintext(&ks->ks_ssl, buf);
3069  if (status == -1)
3070  {
3071  msg(D_TLS_ERRORS,
3072  "TLS ERROR: Outgoing Plaintext -> TLS object write error");
3073  goto error;
3074  }
3075  if (status == 1)
3076  {
3077  state_change = true;
3078  dmsg(D_TLS_DEBUG, "Outgoing Plaintext -> TLS");
3079  }
3080  }
3081 
3082  /* Outgoing Ciphertext to reliable buffer */
3083  if (ks->state >= S_START)
3084  {
3086  if (buf)
3087  {
3089  if (status == -1)
3090  {
3091  msg(D_TLS_ERRORS,
3092  "TLS Error: Ciphertext -> reliable TCP/UDP transport read error");
3093  goto error;
3094  }
3095  if (status == 1)
3096  {
3099  state_change = true;
3100  dmsg(D_TLS_DEBUG, "Outgoing Ciphertext -> Reliable");
3101  }
3102  }
3103  }
3104  }
3105  while (state_change);
3106 
3107  update_time();
3108 
3109  /* Send 1 or more ACKs (each received control packet gets one ACK) */
3110  if (!to_link->len && !reliable_ack_empty(ks->rec_ack))
3111  {
3112  struct buffer buf = ks->ack_write_buf;
3113  ASSERT(buf_init(&buf, FRAME_HEADROOM(&multi->opt.frame)));
3114  write_control_auth(session, ks, &buf, to_link_addr, P_ACK_V1,
3115  RELIABLE_ACK_SIZE, false);
3116  *to_link = buf;
3117  active = true;
3118  dmsg(D_TLS_DEBUG, "Dedicated ACK -> TCP/UDP");
3119  }
3120 
3121  /* When should we wake up again? */
3122  {
3123  if (ks->state >= S_INITIAL)
3124  {
3125  compute_earliest_wakeup(wakeup,
3127 
3128  if (ks->must_negotiate)
3129  {
3131  }
3132  }
3133 
3134  if (ks->established && session->opt->renegotiate_seconds)
3135  {
3136  compute_earliest_wakeup(wakeup,
3137  ks->established + session->opt->renegotiate_seconds - now);
3138  }
3139 
3140  /* prevent event-loop spinning by setting minimum wakeup of 1 second */
3141  if (*wakeup <= 0)
3142  {
3143  *wakeup = 1;
3144 
3145  /* if we had something to send to remote, but to_link was busy,
3146  * let caller know we need to be called again soon */
3147  active = true;
3148  }
3149 
3150  dmsg(D_TLS_DEBUG, "TLS: tls_process: timeout set to %d", *wakeup);
3151 
3152  gc_free(&gc);
3153  return active;
3154  }
3155 
3156 error:
3157  tls_clear_error();
3158  ks->state = S_ERROR;
3159  msg(D_TLS_ERRORS, "TLS Error: TLS handshake failed");
3160  INCR_ERROR;
3161  gc_free(&gc);
3162  return false;
3163 }
3164 
3165 /*
3166  * Called by the top-level event loop.
3167  *
3168  * Basically decides if we should call tls_process for
3169  * the active or untrusted sessions.
3170  */
3171 
3172 int
3174  struct buffer *to_link,
3175  struct link_socket_actual **to_link_addr,
3176  struct link_socket_info *to_link_socket_info,
3177  interval_t *wakeup)
3178 {
3179  struct gc_arena gc = gc_new();
3180  int i;
3181  int active = TLSMP_INACTIVE;
3182  bool error = false;
3183  int tas;
3184 
3186 
3187  tls_clear_error();
3188 
3189  /*
3190  * Process each session object having state of S_INITIAL or greater,
3191  * and which has a defined remote IP addr.
3192  */
3193 
3194  for (i = 0; i < TM_SIZE; ++i)
3195  {
3196  struct tls_session *session = &multi->session[i];
3197  struct key_state *ks = &session->key[KS_PRIMARY];
3198  struct key_state *ks_lame = &session->key[KS_LAME_DUCK];
3199 
3200  /* set initial remote address */
3201  if (i == TM_ACTIVE && ks->state == S_INITIAL
3202  && link_socket_actual_defined(&to_link_socket_info->lsa->actual))
3203  {
3204  ks->remote_addr = to_link_socket_info->lsa->actual;
3205  }
3206 
3207  dmsg(D_TLS_DEBUG,
3208  "TLS: tls_multi_process: i=%d state=%s, mysid=%s, stored-sid=%s, stored-ip=%s",
3209  i,
3210  state_name(ks->state),
3211  session_id_print(&session->session_id, &gc),
3214 
3216  {
3217  struct link_socket_actual *tla = NULL;
3218 
3219  update_time();
3220 
3221  if (tls_process(multi, session, to_link, &tla,
3222  to_link_socket_info, wakeup))
3223  {
3224  active = TLSMP_ACTIVE;
3225  }
3226 
3227  /*
3228  * If tls_process produced an outgoing packet,
3229  * return the link_socket_actual object (which
3230  * contains the outgoing address).
3231  */
3232  if (tla)
3233  {
3234  multi->to_link_addr = *tla;
3235  *to_link_addr = &multi->to_link_addr;
3236  }
3237 
3238  /*
3239  * If tls_process hits an error:
3240  * (1) If the session has an unexpired lame duck key, preserve it.
3241  * (2) Reinitialize the session.
3242  * (3) Increment soft error count
3243  */
3244  if (ks->state == S_ERROR)
3245  {
3246  ++multi->n_soft_errors;
3247 
3248  if (i == TM_ACTIVE)
3249  {
3250  error = true;
3251  }
3252 
3253  if (i == TM_ACTIVE
3254  && ks_lame->state >= S_ACTIVE
3255  && !multi->opt.single_session)
3256  {
3257  move_session(multi, TM_LAME_DUCK, TM_ACTIVE, true);
3258  }
3259  else
3260  {
3261  reset_session(multi, session);
3262  }
3263  }
3264  }
3265  }
3266 
3267  update_time();
3268 
3270 
3271  /*
3272  * If lame duck session expires, kill it.
3273  */
3274  if (lame_duck_must_die(&multi->session[TM_LAME_DUCK], wakeup))
3275  {
3276  tls_session_free(&multi->session[TM_LAME_DUCK], true);
3277  msg(D_TLS_DEBUG_LOW, "TLS: tls_multi_process: killed expiring key");
3278  }
3279 
3280  /*
3281  * If untrusted session achieves TLS authentication,
3282  * move it to active session, usurping any prior session.
3283  *
3284  * A semi-trusted session is one in which the certificate authentication
3285  * succeeded (if cert verification is enabled) but the username/password
3286  * verification failed. A semi-trusted session can forward data on the
3287  * TLS control channel but not on the tunnel channel.
3288  */
3289  if (DECRYPT_KEY_ENABLED(multi, &multi->session[TM_UNTRUSTED].key[KS_PRIMARY]))
3290  {
3291  move_session(multi, TM_ACTIVE, TM_UNTRUSTED, true);
3292  msg(D_TLS_DEBUG_LOW, "TLS: tls_multi_process: untrusted session promoted to %strusted",
3293  tas == TLS_AUTHENTICATION_SUCCEEDED ? "" : "semi-");
3294  }
3295 
3296  /*
3297  * A hard error means that TM_ACTIVE hit an S_ERROR state and that no
3298  * other key state objects are S_ACTIVE or higher.
3299  */
3300  if (error)
3301  {
3302  for (i = 0; i < (int) SIZE(multi->key_scan); ++i)
3303  {
3304  if (multi->key_scan[i]->state >= S_ACTIVE)
3305  {
3306  goto nohard;
3307  }
3308  }
3309  ++multi->n_hard_errors;
3310  }
3311 nohard:
3312 
3313 #ifdef ENABLE_DEBUG
3314  /* DEBUGGING -- flood peer with repeating connection attempts */
3315  {
3316  const int throw_level = GREMLIN_CONNECTION_FLOOD_LEVEL(multi->opt.gremlin);
3317  if (throw_level)
3318  {
3319  for (i = 0; i < (int) SIZE(multi->key_scan); ++i)
3320  {
3321  if (multi->key_scan[i]->state >= throw_level)
3322  {
3323  ++multi->n_hard_errors;
3324  ++multi->n_soft_errors;
3325  }
3326  }
3327  }
3328  }
3329 #endif
3330 
3331  perf_pop();
3332  gc_free(&gc);
3333 
3334  return (tas == TLS_AUTHENTICATION_FAILED) ? TLSMP_KILL : active;
3335 }
3336 
3337 /*
3338  * Pre and post-process the encryption & decryption buffers in order
3339  * to implement a multiplexed TLS channel over the TCP/UDP port.
3340  */
3341 
3342 /*
3343  *
3344  * When we are in TLS mode, this is the first routine which sees
3345  * an incoming packet.
3346  *
3347  * If it's a data packet, we set opt so that our caller can
3348  * decrypt it. We also give our caller the appropriate decryption key.
3349  *
3350  * If it's a control packet, we authenticate it and process it,
3351  * possibly creating a new tls_session if it represents the
3352  * first packet of a new session. For control packets, we will
3353  * also zero the size of *buf so that our caller ignores the
3354  * packet on our return.
3355  *
3356  * Note that openvpn only allows one active session at a time,
3357  * so a new session (once authenticated) will always usurp
3358  * an old session.
3359  *
3360  * Return true if input was an authenticated control channel
3361  * packet.
3362  *
3363  * If we are running in TLS thread mode, all public routines
3364  * below this point must be called with the L_TLS lock held.
3365  */
3366 
3367 bool
3369  const struct link_socket_actual *from,
3370  struct buffer *buf,
3371  struct crypto_options **opt,
3372  bool floated,
3373  const uint8_t **ad_start)
3374 {
3375  struct gc_arena gc = gc_new();
3376  bool ret = false;
3377 
3378  if (buf->len > 0)
3379  {
3380  int i;
3381  int op;
3382  int key_id;
3383 
3384  /* get opcode and key ID */
3385  {
3386  uint8_t c = *BPTR(buf);
3387  op = c >> P_OPCODE_SHIFT;
3388  key_id = c & P_KEY_ID_MASK;
3389  }
3390 
3391  if ((op == P_DATA_V1) || (op == P_DATA_V2))
3392  {
3393  /* data channel packet */
3394  for (i = 0; i < KEY_SCAN_SIZE; ++i)
3395  {
3396  struct key_state *ks = multi->key_scan[i];
3397 
3398  /*
3399  * This is the basic test of TLS state compatibility between a local OpenVPN
3400  * instance and its remote peer.
3401  *
3402  * If the test fails, it tells us that we are getting a packet from a source
3403  * which claims reference to a prior negotiated TLS session, but the local
3404  * OpenVPN instance has no memory of such a negotiation.
3405  *
3406  * It almost always occurs on UDP sessions when the passive side of the
3407  * connection is restarted without the active side restarting as well (the
3408  * passive side is the server which only listens for the connections, the
3409  * active side is the client which initiates connections).
3410  */
3411  if (DECRYPT_KEY_ENABLED(multi, ks)
3412  && key_id == ks->key_id
3413  && ks->authenticated
3414 #ifdef ENABLE_DEF_AUTH
3415  && !ks->auth_deferred
3416 #endif
3417  && (floated || link_socket_actual_match(from, &ks->remote_addr)))
3418  {
3420  {
3422  "Key %s [%d] not initialized (yet), dropping packet.",
3423  print_link_socket_actual(from, &gc), key_id);
3424  goto error_lite;
3425  }
3426 
3427  /* return appropriate data channel decrypt key in opt */
3428  *opt = &ks->crypto_options;
3429  if (op == P_DATA_V2)
3430  {
3431  *ad_start = BPTR(buf);
3432  }
3433  ASSERT(buf_advance(buf, 1));
3434  if (op == P_DATA_V1)
3435  {
3436  *ad_start = BPTR(buf);
3437  }
3438  else if (op == P_DATA_V2)
3439  {
3440  if (buf->len < 4)
3441  {
3442  msg(D_TLS_ERRORS, "Protocol error: received P_DATA_V2 from %s but length is < 4",
3443  print_link_socket_actual(from, &gc));
3444  goto error;
3445  }
3446  ASSERT(buf_advance(buf, 3));
3447  }
3448 
3449  ++ks->n_packets;
3450  ks->n_bytes += buf->len;
3452  "TLS: tls_pre_decrypt, key_id=%d, IP=%s",
3453  key_id, print_link_socket_actual(from, &gc));
3454  gc_free(&gc);
3455  return ret;
3456  }
3457  }
3458 
3459  msg(D_TLS_ERRORS,
3460  "TLS Error: local/remote TLS keys are out of sync: %s [%d]",
3461  print_link_socket_actual(from, &gc), key_id);
3462  goto error_lite;
3463  }
3464  else /* control channel packet */
3465  {
3466  bool do_burst = false;
3467  bool new_link = false;
3468  struct session_id sid; /* remote session ID */
3469 
3470  /* verify legal opcode */
3471  if (op < P_FIRST_OPCODE || op > P_LAST_OPCODE)
3472  {
3473  msg(D_TLS_ERRORS,
3474  "TLS Error: unknown opcode received from %s op=%d",
3475  print_link_socket_actual(from, &gc), op);
3476  goto error;
3477  }
3478 
3479  /* hard reset ? */
3480  if (is_hard_reset(op, 0))
3481  {
3482  /* verify client -> server or server -> client connection */
3483  if (((op == P_CONTROL_HARD_RESET_CLIENT_V1
3485  || op == P_CONTROL_HARD_RESET_CLIENT_V3) && !multi->opt.server)
3487  || op == P_CONTROL_HARD_RESET_SERVER_V2) && multi->opt.server))
3488  {
3489  msg(D_TLS_ERRORS,
3490  "TLS Error: client->client or server->server connection attempted from %s",
3491  print_link_socket_actual(from, &gc));
3492  goto error;
3493  }
3494  }
3495 
3496  /*
3497  * Authenticate Packet
3498  */
3499  dmsg(D_TLS_DEBUG, "TLS: control channel, op=%s, IP=%s",
3501 
3502  /* get remote session-id */
3503  {
3504  struct buffer tmp = *buf;
3505  buf_advance(&tmp, 1);
3506  if (!session_id_read(&sid, &tmp) || !session_id_defined(&sid))
3507  {
3508  msg(D_TLS_ERRORS,
3509  "TLS Error: session-id not found in packet from %s",
3510  print_link_socket_actual(from, &gc));
3511  goto error;
3512  }
3513  }
3514 
3515  /* use session ID to match up packet with appropriate tls_session object */
3516  for (i = 0; i < TM_SIZE; ++i)
3517  {
3518  struct tls_session *session = &multi->session[i];
3519  struct key_state *ks = &session->key[KS_PRIMARY];
3520 
3521  dmsg(D_TLS_DEBUG,
3522  "TLS: initial packet test, i=%d state=%s, mysid=%s, rec-sid=%s, rec-ip=%s, stored-sid=%s, stored-ip=%s",
3523  i,
3524  state_name(ks->state),
3525  session_id_print(&session->session_id, &gc),
3526  session_id_print(&sid, &gc),
3527  print_link_socket_actual(from, &gc),
3530 
3531  if (session_id_equal(&ks->session_id_remote, &sid))
3532  /* found a match */
3533  {
3534  if (i == TM_LAME_DUCK)
3535  {
3536  msg(D_TLS_ERRORS,
3537  "TLS ERROR: received control packet with stale session-id=%s",
3538  session_id_print(&sid, &gc));
3539  goto error;
3540  }
3541  dmsg(D_TLS_DEBUG,
3542  "TLS: found match, session[%d], sid=%s",
3543  i, session_id_print(&sid, &gc));
3544  break;
3545  }
3546  }
3547 
3548  /*
3549  * Initial packet received.
3550  */
3551 
3552  if (i == TM_SIZE && is_hard_reset(op, 0))
3553  {
3554  struct tls_session *session = &multi->session[TM_ACTIVE];
3555  struct key_state *ks = &session->key[KS_PRIMARY];
3556 
3557  if (!is_hard_reset(op, multi->opt.key_method))
3558  {
3559  msg(D_TLS_ERRORS, "TLS ERROR: initial packet local/remote key_method mismatch, local key_method=%d, op=%s",
3560  multi->opt.key_method,
3561  packet_opcode_name(op));
3562  goto error;
3563  }
3564 
3565  /*
3566  * If we have no session currently in progress, the initial packet will
3567  * open a new session in TM_ACTIVE rather than TM_UNTRUSTED.
3568  */
3570  {
3571  if (multi->opt.single_session && multi->n_sessions)
3572  {
3573  msg(D_TLS_ERRORS,
3574  "TLS Error: Cannot accept new session request from %s due to session context expire or --single-session [1]",
3575  print_link_socket_actual(from, &gc));
3576  goto error;
3577  }
3578 
3579 #ifdef ENABLE_MANAGEMENT
3580  if (management)
3581  {
3584  NULL,
3585  NULL,
3586  NULL,
3587  NULL,
3588  NULL);
3589  }
3590 #endif
3591 
3593  "TLS: Initial packet from %s, sid=%s",
3594  print_link_socket_actual(from, &gc),
3595  session_id_print(&sid, &gc));
3596 
3597  do_burst = true;
3598  new_link = true;
3599  i = TM_ACTIVE;
3600  session->untrusted_addr = *from;
3601  }
3602  }
3603 
3604  if (i == TM_SIZE && is_hard_reset(op, 0))
3605  {
3606  /*
3607  * No match with existing sessions,
3608  * probably a new session.
3609  */
3610  struct tls_session *session = &multi->session[TM_UNTRUSTED];
3611 
3612  /*
3613  * If --single-session, don't allow any hard-reset connection request
3614  * unless it the the first packet of the session.
3615  */
3616  if (multi->opt.single_session)
3617  {
3618  msg(D_TLS_ERRORS,
3619  "TLS Error: Cannot accept new session request from %s due to session context expire or --single-session [2]",
3620  print_link_socket_actual(from, &gc));
3621  goto error;
3622  }
3623 
3624  if (!is_hard_reset(op, multi->opt.key_method))
3625  {
3626  msg(D_TLS_ERRORS, "TLS ERROR: new session local/remote key_method mismatch, local key_method=%d, op=%s",
3627  multi->opt.key_method,
3628  packet_opcode_name(op));
3629  goto error;
3630  }
3631 
3632  if (!read_control_auth(buf, &session->tls_wrap, from,
3633  session->opt))
3634  {
3635  goto error;
3636  }
3637 
3638  /*
3639  * New session-initiating control packet is authenticated at this point,
3640  * assuming that the --tls-auth command line option was used.
3641  *
3642  * Without --tls-auth, we leave authentication entirely up to TLS.
3643  */
3645  "TLS: new session incoming connection from %s",
3646  print_link_socket_actual(from, &gc));
3647 
3648  new_link = true;
3649  i = TM_UNTRUSTED;
3650  session->untrusted_addr = *from;
3651  }
3652  else
3653  {
3654  struct tls_session *session = &multi->session[i];
3655  struct key_state *ks = &session->key[KS_PRIMARY];
3656 
3657  /*
3658  * Packet must belong to an existing session.
3659  */
3660  if (i != TM_ACTIVE && i != TM_UNTRUSTED)
3661  {
3662  msg(D_TLS_ERRORS,
3663  "TLS Error: Unroutable control packet received from %s (si=%d op=%s)",
3664  print_link_socket_actual(from, &gc),
3665  i,
3666  packet_opcode_name(op));
3667  goto error;
3668  }
3669 
3670  /*
3671  * Verify remote IP address
3672  */
3673  if (!new_link && !link_socket_actual_match(&ks->remote_addr, from))
3674  {
3675  msg(D_TLS_ERRORS, "TLS Error: Received control packet from unexpected IP addr: %s",
3676  print_link_socket_actual(from, &gc));
3677  goto error;
3678  }
3679 
3680  /*
3681  * Remote is requesting a key renegotiation
3682  */
3683  if (op == P_CONTROL_SOFT_RESET_V1
3684  && DECRYPT_KEY_ENABLED(multi, ks))
3685  {
3686  if (!read_control_auth(buf, &session->tls_wrap, from,
3687  session->opt))
3688  {
3689  goto error;
3690  }
3691 
3692  key_state_soft_reset(session);
3693 
3694  dmsg(D_TLS_DEBUG,
3695  "TLS: received P_CONTROL_SOFT_RESET_V1 s=%d sid=%s",
3696  i, session_id_print(&sid, &gc));
3697  }
3698  else
3699  {
3700  /*
3701  * Remote responding to our key renegotiation request?
3702  */
3703  if (op == P_CONTROL_SOFT_RESET_V1)
3704  {
3705  do_burst = true;
3706  }
3707 
3708  if (!read_control_auth(buf, &session->tls_wrap, from,
3709  session->opt))
3710  {
3711  goto error;
3712  }
3713 
3714  dmsg(D_TLS_DEBUG,
3715  "TLS: received control channel packet s#=%d sid=%s",
3716  i, session_id_print(&sid, &gc));
3717  }
3718  }
3719 
3720  /*
3721  * We have an authenticated control channel packet (if --tls-auth was set).
3722  * Now pass to our reliability layer which deals with
3723  * packet acknowledgements, retransmits, sequencing, etc.
3724  */
3725  {
3726  struct tls_session *session = &multi->session[i];
3727  struct key_state *ks = &session->key[KS_PRIMARY];
3728 
3729  /* Make sure we were initialized and that we're not in an error state */
3730  ASSERT(ks->state != S_UNDEF);
3731  ASSERT(ks->state != S_ERROR);
3732  ASSERT(session_id_defined(&session->session_id));
3733 
3734  /* Let our caller know we processed a control channel packet */
3735  ret = true;
3736 
3737  /*
3738  * Set our remote address and remote session_id
3739  */
3740  if (new_link)
3741  {
3742  ks->session_id_remote = sid;
3743  ks->remote_addr = *from;
3744  ++multi->n_sessions;
3745  }
3746  else if (!link_socket_actual_match(&ks->remote_addr, from))
3747  {
3748  msg(D_TLS_ERRORS,
3749  "TLS Error: Existing session control channel packet from unknown IP address: %s",
3750  print_link_socket_actual(from, &gc));
3751  goto error;
3752  }
3753 
3754  /*
3755  * Should we do a retransmit of all unacknowledged packets in
3756  * the send buffer? This improves the start-up efficiency of the
3757  * initial key negotiation after the 2nd peer comes online.
3758  */
3759  if (do_burst && !session->burst)
3760  {
3762  session->burst = true;
3763  }
3764 
3765  /* Check key_id */
3766  if (ks->key_id != key_id)
3767  {
3768  msg(D_TLS_ERRORS,
3769  "TLS ERROR: local/remote key IDs out of sync (%d/%d) ID: %s",
3770  ks->key_id, key_id, print_key_id(multi, &gc));
3771  goto error;
3772  }
3773 
3774  /*
3775  * Process incoming ACKs for packets we can now
3776  * delete from reliable send buffer
3777  */
3778  {
3779  /* buffers all packet IDs to delete from send_reliable */
3780  struct reliable_ack send_ack;
3781 
3782  send_ack.len = 0;
3783  if (!reliable_ack_read(&send_ack, buf, &session->session_id))
3784  {
3785  msg(D_TLS_ERRORS,
3786  "TLS Error: reading acknowledgement record from packet");
3787  goto error;
3788  }
3789  reliable_send_purge(ks->send_reliable, &send_ack);
3790  }
3791 
3792  if (op != P_ACK_V1 && reliable_can_get(ks->rec_reliable))
3793  {
3794  packet_id_type id;
3795 
3796  /* Extract the packet ID from the packet */
3797  if (reliable_ack_read_packet_id(buf, &id))
3798  {
3799  /* Avoid deadlock by rejecting packet that would de-sequentialize receive buffer */
3801  {
3802  if (reliable_not_replay(ks->rec_reliable, id))
3803  {
3804  /* Save incoming ciphertext packet to reliable buffer */
3805  struct buffer *in = reliable_get_buf(ks->rec_reliable);
3806  ASSERT(in);
3807  if (!buf_copy(in, buf))
3808  {
3810  "Incoming control channel packet too big, dropping.");
3811  goto error;
3812  }
3814  }
3815 
3816  /* Process outgoing acknowledgment for packet just received, even if it's a replay */
3818  }
3819  }
3820  }
3821  }
3822  }
3823  }
3824 
3825 done:
3826  buf->len = 0;
3827  *opt = NULL;
3828  gc_free(&gc);
3829  return ret;
3830 
3831 error:
3832  ++multi->n_soft_errors;
3833 error_lite:
3834  tls_clear_error();
3835  goto done;
3836 }
3837 
3838 /*
3839  * This function is similar to tls_pre_decrypt, except it is called
3840  * when we are in server mode and receive an initial incoming
3841  * packet. Note that we don't modify
3842  * any state in our parameter objects. The purpose is solely to
3843  * determine whether we should generate a client instance
3844  * object, in which case true is returned.
3845  *
3846  * This function is essentially the first-line HMAC firewall
3847  * on the UDP port listener in --mode server mode.
3848  */
3849 bool
3851  const struct link_socket_actual *from,
3852  const struct buffer *buf)
3853 
3854 {
3855  struct gc_arena gc = gc_new();
3856  bool ret = false;
3857 
3858  if (buf->len > 0)
3859  {
3860  int op;
3861  int key_id;
3862 
3863  /* get opcode and key ID */
3864  {
3865  uint8_t c = *BPTR(buf);
3866  op = c >> P_OPCODE_SHIFT;
3867  key_id = c & P_KEY_ID_MASK;
3868  }
3869 
3870  /* this packet is from an as-yet untrusted source, so
3871  * scrutinize carefully */
3872 
3875  {
3876  /*
3877  * This can occur due to bogus data or DoS packets.
3878  */
3880  "TLS State Error: No TLS state for client %s, opcode=%d",
3881  print_link_socket_actual(from, &gc),
3882  op);
3883  goto error;
3884  }
3885 
3886  if (key_id != 0)
3887  {
3889  "TLS State Error: Unknown key ID (%d) received from %s -- 0 was expected",
3890  key_id,
3891  print_link_socket_actual(from, &gc));
3892  goto error;
3893  }
3894 
3895  if (buf->len > EXPANDED_SIZE_DYNAMIC(&tas->frame))
3896  {
3898  "TLS State Error: Large packet (size %d) received from %s -- a packet no larger than %d bytes was expected",
3899  buf->len,
3900  print_link_socket_actual(from, &gc),
3901  EXPANDED_SIZE_DYNAMIC(&tas->frame));
3902  goto error;
3903  }
3904 
3905  {
3906  struct buffer newbuf = clone_buf(buf);
3907  struct tls_wrap_ctx tls_wrap_tmp = tas->tls_wrap;
3908  bool status;
3909 
3910  /* HMAC test, if --tls-auth was specified */
3911  status = read_control_auth(&newbuf, &tls_wrap_tmp, from, NULL);
3912  free_buf(&newbuf);
3913  free_buf(&tls_wrap_tmp.tls_crypt_v2_metadata);
3914  if (tls_wrap_tmp.cleanup_key_ctx)
3915  {
3916  free_key_ctx_bi(&tls_wrap_tmp.opt.key_ctx_bi);
3917  }
3918  if (!status)
3919  {
3920  goto error;
3921  }
3922 
3923  /*
3924  * At this point, if --tls-auth is being used, we know that
3925  * the packet has passed the HMAC test, but we don't know if
3926  * it is a replay yet. We will attempt to defeat replays
3927  * by not advancing to the S_START state until we
3928  * receive an ACK from our first reply to the client
3929  * that includes an HMAC of our randomly generated 64 bit
3930  * session ID.
3931  *
3932  * On the other hand if --tls-auth is not being used, we
3933  * will proceed to begin the TLS authentication
3934  * handshake with only cursory integrity checks having
3935  * been performed, since we will be leaving the task
3936  * of authentication solely up to TLS.
3937  */
3938 
3939  ret = true;
3940  }
3941  }
3942  gc_free(&gc);
3943  return ret;
3944 
3945 error:
3946  tls_clear_error();
3947  gc_free(&gc);
3948  return ret;
3949 }
3950 
3951 /* Choose the key with which to encrypt a data packet */
3952 void
3954  struct buffer *buf, struct crypto_options **opt)
3955 {
3956  multi->save_ks = NULL;
3957  if (buf->len > 0)
3958  {
3959  int i;
3960  struct key_state *ks_select = NULL;
3961  for (i = 0; i < KEY_SCAN_SIZE; ++i)
3962  {
3963  struct key_state *ks = multi->key_scan[i];
3964  if (ks->state >= S_ACTIVE
3965  && ks->authenticated
3967 #ifdef ENABLE_DEF_AUTH
3968  && !ks->auth_deferred
3969 #endif
3970  )
3971  {
3972  if (!ks_select)
3973  {
3974  ks_select = ks;
3975  }
3976  if (now >= ks->auth_deferred_expire)
3977  {
3978  ks_select = ks;
3979  break;
3980  }
3981  }
3982  }
3983 
3984  if (ks_select)
3985  {
3986  *opt = &ks_select->crypto_options;
3987  multi->save_ks = ks_select;
3988  dmsg(D_TLS_KEYSELECT, "TLS: tls_pre_encrypt: key_id=%d", ks_select->key_id);
3989  return;
3990  }
3991  else
3992  {
3993  struct gc_arena gc = gc_new();
3994  dmsg(D_TLS_KEYSELECT, "TLS Warning: no data channel send key available: %s",
3995  print_key_id(multi, &gc));
3996  gc_free(&gc);
3997  }
3998  }
3999 
4000  buf->len = 0;
4001  *opt = NULL;
4002 }
4003 
4004 void
4005 tls_prepend_opcode_v1(const struct tls_multi *multi, struct buffer *buf)
4006 {
4007  struct key_state *ks = multi->save_ks;
4008  uint8_t op;
4009 
4010  msg(D_TLS_DEBUG, __func__);
4011 
4012  ASSERT(ks);
4013 
4014  op = (P_DATA_V1 << P_OPCODE_SHIFT) | ks->key_id;
4015  ASSERT(buf_write_prepend(buf, &op, 1));
4016 }
4017 
4018 void
4019 tls_prepend_opcode_v2(const struct tls_multi *multi, struct buffer *buf)
4020 {
4021  struct key_state *ks = multi->save_ks;
4022  uint32_t peer;
4023 
4024  msg(D_TLS_DEBUG, __func__);
4025 
4026  ASSERT(ks);
4027 
4028  peer = htonl(((P_DATA_V2 << P_OPCODE_SHIFT) | ks->key_id) << 24
4029  | (multi->peer_id & 0xFFFFFF));
4030  ASSERT(buf_write_prepend(buf, &peer, 4));
4031 }
4032 
4033 void
4034 tls_post_encrypt(struct tls_multi *multi, struct buffer *buf)
4035 {
4036  struct key_state *ks = multi->save_ks;
4037  multi->save_ks = NULL;
4038 
4039  if (buf->len > 0)
4040  {
4041  ASSERT(ks);
4042 
4043  ++ks->n_packets;
4044  ks->n_bytes += buf->len;
4045  }
4046 }
4047 
4048 /*
4049  * Send a payload over the TLS control channel.
4050  * Called externally.
4051  */
4052 
4053 bool
4055  const uint8_t *data,
4056  int size)
4057 {
4058  struct tls_session *session;
4059  struct key_state *ks;
4060  bool ret = false;
4061 
4062  tls_clear_error();
4063 
4064  ASSERT(multi);
4065 
4066  session = &multi->session[TM_ACTIVE];
4067  ks = &session->key[KS_PRIMARY];
4068 
4069  if (ks->state >= S_ACTIVE)
4070  {
4071  if (key_state_write_plaintext_const(&ks->ks_ssl, data, size) == 1)
4072  {
4073  ret = true;
4074  }
4075  }
4076  else
4077  {
4078  if (!ks->paybuf)
4079  {
4080  ks->paybuf = buffer_list_new(0);
4081  }
4082  buffer_list_push_data(ks->paybuf, data, (size_t)size);
4083  ret = true;
4084  }
4085 
4086 
4087  tls_clear_error();
4088 
4089  return ret;
4090 }
4091 
4092 bool
4094  struct buffer *buf)
4095 {
4096  struct tls_session *session;
4097  struct key_state *ks;
4098  bool ret = false;
4099 
4100  tls_clear_error();
4101 
4102  ASSERT(multi);
4103 
4104  session = &multi->session[TM_ACTIVE];
4105  ks = &session->key[KS_PRIMARY];
4106 
4107  if (ks->state >= S_ACTIVE && BLEN(&ks->plaintext_read_buf))
4108  {
4109  if (buf_copy(buf, &ks->plaintext_read_buf))
4110  {
4111  ret = true;
4112  }
4113  ks->plaintext_read_buf.len = 0;
4114  }
4115 
4116  tls_clear_error();
4117 
4118  return ret;
4119 }
4120 
4121 void
4122 tls_update_remote_addr(struct tls_multi *multi, const struct link_socket_actual *addr)
4123 {
4124  struct gc_arena gc = gc_new();
4125  int i, j;
4126 
4127  for (i = 0; i < TM_SIZE; ++i)
4128  {
4129  struct tls_session *session = &multi->session[i];
4130 
4131  for (j = 0; j < KS_SIZE; ++j)
4132  {
4133  struct key_state *ks = &session->key[j];
4134 
4136  || link_socket_actual_match(addr, &ks->remote_addr))
4137  {
4138  continue;
4139  }
4140 
4141  dmsg(D_TLS_KEYSELECT, "TLS: tls_update_remote_addr from IP=%s to IP=%s",
4143  print_link_socket_actual(addr, &gc));
4144 
4145  ks->remote_addr = *addr;
4146  }
4147  }
4148  gc_free(&gc);
4149 }
4150 
4151 int
4152 tls_peer_info_ncp_ver(const char *peer_info)
4153 {
4154  const char *ncpstr = peer_info ? strstr(peer_info, "IV_NCP=") : NULL;
4155  if (ncpstr)
4156  {
4157  int ncp = 0;
4158  int r = sscanf(ncpstr, "IV_NCP=%d", &ncp);
4159  if (r == 1)
4160  {
4161  return ncp;
4162  }
4163  }
4164  return 0;
4165 }
4166 
4167 bool
4168 tls_check_ncp_cipher_list(const char *list)
4169 {
4170  bool unsupported_cipher_found = false;
4171 
4172  ASSERT(list);
4173 
4174  char *const tmp_ciphers = string_alloc(list, NULL);
4175  const char *token = strtok(tmp_ciphers, ":");
4176  while (token)
4177  {
4179  {
4180  msg(M_WARN, "Unsupported cipher in --ncp-ciphers: %s", token);
4181  unsupported_cipher_found = true;
4182  }
4183  token = strtok(NULL, ":");
4184  }
4185  free(tmp_ciphers);
4186 
4187  return 0 < strlen(list) && !unsupported_cipher_found;
4188 }
4189 
4190 void
4191 show_available_tls_ciphers(const char *cipher_list,
4192  const char *cipher_list_tls13,
4193  const char *tls_cert_profile)
4194 {
4195  printf("Available TLS Ciphers, listed in order of preference:\n");
4196 
4197 #if (ENABLE_CRYPTO_OPENSSL && OPENSSL_VERSION_NUMBER >= 0x1010100fL)
4198  printf("\nFor TLS 1.3 and newer (--tls-ciphersuites):\n\n");
4199  show_available_tls_ciphers_list(cipher_list_tls13, tls_cert_profile, true);
4200 #else
4201  (void) cipher_list_tls13; /* Avoid unused warning */
4202 #endif
4203 
4204  printf("\nFor TLS 1.2 and older (--tls-cipher):\n\n");
4205  show_available_tls_ciphers_list(cipher_list, tls_cert_profile, false);
4206 
4207  printf("\n"
4208  "Be aware that that whether a cipher suite in this list can actually work\n"
4209  "depends on the specific setup of both peers. See the man page entries of\n"
4210  "--tls-cipher and --show-tls for more details.\n\n"
4211  );
4212 }
4213 
4214 /*
4215  * Dump a human-readable rendition of an openvpn packet
4216  * into a garbage collectable string which is returned.
4217  */
4218 const char *
4219 protocol_dump(struct buffer *buffer, unsigned int flags, struct gc_arena *gc)
4220 {
4221  struct buffer out = alloc_buf_gc(256, gc);
4222  struct buffer buf = *buffer;
4223 
4224  uint8_t c;
4225  int op;
4226  int key_id;
4227 
4228  int tls_auth_hmac_size = (flags & PD_TLS_AUTH_HMAC_SIZE_MASK);
4229 
4230  if (buf.len <= 0)
4231  {
4232  buf_printf(&out, "DATA UNDEF len=%d", buf.len);
4233  goto done;
4234  }
4235 
4236  if (!(flags & PD_TLS))
4237  {
4238  goto print_data;
4239  }
4240 
4241  /*
4242  * Initial byte (opcode)
4243  */
4244  if (!buf_read(&buf, &c, sizeof(c)))
4245  {
4246  goto done;
4247  }
4248  op = (c >> P_OPCODE_SHIFT);
4249  key_id = c & P_KEY_ID_MASK;
4250  buf_printf(&out, "%s kid=%d", packet_opcode_name(op), key_id);
4251 
4252  if ((op == P_DATA_V1) || (op == P_DATA_V2))
4253  {
4254  goto print_data;
4255  }
4256 
4257  /*
4258  * Session ID
4259  */
4260  {
4261  struct session_id sid;
4262 
4263  if (!session_id_read(&sid, &buf))
4264  {
4265  goto done;
4266  }
4267  if (flags & PD_VERBOSE)
4268  {
4269  buf_printf(&out, " sid=%s", session_id_print(&sid, gc));
4270  }
4271  }
4272 
4273  /*
4274  * tls-auth hmac + packet_id
4275  */
4276  if (tls_auth_hmac_size)
4277  {
4278  struct packet_id_net pin;
4279  uint8_t tls_auth_hmac[MAX_HMAC_KEY_LENGTH];
4280 
4281  ASSERT(tls_auth_hmac_size <= MAX_HMAC_KEY_LENGTH);
4282 
4283  if (!buf_read(&buf, tls_auth_hmac, tls_auth_hmac_size))
4284  {
4285  goto done;
4286  }
4287  if (flags & PD_VERBOSE)
4288  {
4289  buf_printf(&out, " tls_hmac=%s", format_hex(tls_auth_hmac, tls_auth_hmac_size, 0, gc));
4290  }
4291 
4292  if (!packet_id_read(&pin, &buf, true))
4293  {
4294  goto done;
4295  }
4296  buf_printf(&out, " pid=%s", packet_id_net_print(&pin, (flags & PD_VERBOSE), gc));
4297  }
4298 
4299  /*
4300  * ACK list
4301  */
4302  buf_printf(&out, " %s", reliable_ack_print(&buf, (flags & PD_VERBOSE), gc));
4303 
4304  if (op == P_ACK_V1)
4305  {
4306  goto done;
4307  }
4308 
4309  /*
4310  * Packet ID
4311  */
4312  {
4313  packet_id_type l;
4314  if (!buf_read(&buf, &l, sizeof(l)))
4315  {
4316  goto done;
4317  }
4318  l = ntohpid(l);
4320  }
4321 
4322 print_data:
4323  if (flags & PD_SHOW_DATA)
4324  {
4325  buf_printf(&out, " DATA %s", format_hex(BPTR(&buf), BLEN(&buf), 80, gc));
4326  }
4327  else
4328  {
4329  buf_printf(&out, " DATA len=%d", buf.len);
4330  }
4331 
4332 done:
4333  return BSTR(&out);
4334 }
4335 
4336 void
4338 {
4339  auth_user_pass.wait_for_push = false;
4341 }
static void tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame, struct frame *frame)
Definition: ssl.c:326
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:3659
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:777
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:3747
const char * ecdh_curve
Definition: options.h:534
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:1635
#define EXPANDED_SIZE_DYNAMIC(f)
Definition: mtu.h:173
int tls_ctx_load_pkcs12(struct tls_root_ctx *ctx, const char *pkcs12_file, const char *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:706
#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:839
size_t implicit_iv_len
The length of implicit_iv.
Definition: crypto.h:170
const char * ca_file_inline
Definition: options.h:541
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:389
static void tls_wrap_free(struct tls_wrap_ctx *tls_wrap)
Free the elements of a tls_wrap_ctx structure.
Definition: ssl.h:537
#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:466
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:1210
bool tun_mtu_defined
Definition: options.h:108
struct buffer work
Work buffer (only for –tls-crypt)
Definition: ssl_common.h:228
bool ncp_enabled
Definition: options.h:504
unsigned int management_flags
Definition: options.h:388
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:446
#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:528
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:1319
bool tls_crypt_v2
Definition: ssl_common.h:308
#define SSLF_CRL_VERIFY_DIR
Definition: ssl_common.h:351
bool options_cmp_equal_safe(char *actual, const char *expected, size_t actual_n)
Definition: options.c:3882
void free_buf(struct buffer *buf)
Definition: buffer.c:185
void ssl_put_auth_challenge(const char *cr_str)
Definition: ssl.c:503
#define D_TLS_DEBUG_LOW
Definition: errlevel.h:77
const char * cipher_list
Definition: options.h:531
static bool write_string(struct buffer *buf, const char *str, const int maxlen)
Definition: ssl.c:2185
unsigned int flags
Bit-flags determining behavior of security operation functions.
Definition: crypto.h:257
bool tls_server
Definition: options.h:522
#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:835
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:817
static void tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, const char *crl_file_inline)
Load (or possibly reload) the CRL file into the SSL context.
Definition: ssl.c:558
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:160
#define USER_PASS_LEN
Definition: misc.h:72
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:821
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:296
const char * cert_file
Definition: options.h:527
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:3741
const char * tls_cert_profile
Definition: options.h:533
#define GET_USER_PASS_STATIC_CHALLENGE
Definition: misc.h:121
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:620
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:1877
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:520
#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:975
static void key_state_soft_reset(struct tls_session *session)
Definition: ssl.c:2156
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:983
const char * ca_path
Definition: options.h:525
struct tls_options * opt
Definition: ssl_common.h:411
time_t must_negotiate
Definition: ssl_common.h:174
#define SSLF_AUTH_USER_PASS_OPTIONAL
Definition: ssl_common.h:349
char * peer_info
Definition: ssl_common.h:559
const char * ca_file
Definition: options.h:524
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
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:1602
bool verified
Definition: ssl_common.h:441
void options_warning_safe(char *actual, const char *expected, size_t actual_n)
Definition: options.c:3905
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:4034
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:201
#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:529
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:503
#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:2766
int pem_password_callback(char *buf, int size, int rwflag, void *u)
Callback to retrieve the user&#39;s password.
Definition: ssl.c:382
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:1844
#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:1346
#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
void backend_tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, const char *crl_inline)
Reload the Certificate Revocation List for the SSL channel.
Definition: ssl_openssl.c:1003
const char * crl_file_inline
Definition: options.h:544
int handshake_window
Definition: ssl_common.h:273
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.c:1918
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:4122
#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:435
static bool lame_duck_must_die(const struct tls_session *session, interval_t *wakeup)
Definition: ssl.c:1227
#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:2204
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:4019
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:100
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:64
#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:152
const char * protocol_dump(struct buffer *buffer, unsigned int flags, struct gc_arena *gc)
Definition: ssl.c:4219
#define ENABLE_DEF_AUTH
Definition: config-msvc.h:5
const char * cryptoapi_cert
Definition: options.h:567
#define TLS_VER_1_1
Definition: ssl_backend.h:115
static void flush_payload_buffer(struct key_state *ks)
Definition: ssl.c:2136
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
const char * pkcs12_file_inline
Definition: options.h:547
static bool push_peer_info(struct buffer *buf, struct tls_session *session)
Definition: ssl.c:2287
#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:1859
#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:936
#define OPENVPN_STATE_AUTH
Definition: manage.h:492
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:3850
#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:593
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:2139
static struct user_pass auth_user_pass
Definition: ssl.c:401
void ssl_set_auth_token(const char *token)
Definition: ssl.c:458
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:1987
const char * pkcs12_file
Definition: options.h:530
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:350
bool link_mtu_defined
Definition: options.h:112
char username[USER_PASS_LEN]
Definition: misc.h:74
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:856
static bool buf_read(struct buffer *src, void *dest, int size)
Definition: buffer.h:800
#define MODE_SERVER
Definition: options.h:198
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:3953
bool use_peer_id
Definition: ssl_common.h:586
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:121
#define TLS_VER_1_2
Definition: ssl_backend.h:116
struct link_socket_actual to_link_addr
Definition: ssl_common.h:526
#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:491
#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:505
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:101
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:511
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:1730
struct tls_auth_standalone * tls_auth_standalone_init(struct tls_options *tls_options, struct gc_arena *gc)
Definition: ssl.c:1296
static char * auth_challenge
Definition: ssl.c:405
static bool key_source2_randomize_write(struct key_source2 *k2, struct buffer *buf, bool server)
Definition: ssl.c:2069
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:65
#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:3921
#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:2175
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:601
#define KEY_DIRECTION_NORMAL
Definition: crypto.h:174
void print_details(struct key_state_ssl *ks_ssl, const char *prefix)
Definition: ssl_openssl.c:1943
Control channel wrapping (–tls-auth/–tls-crypt) context.
Definition: ssl_common.h:220
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:1174
#define KEY_SCAN_SIZE
Definition: ssl_common.h:488
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:491
#define GET_USER_PASS_STATIC_CHALLENGE_ECHO
Definition: misc.h:122
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:4005
#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:388
int md_kt_size(const md_kt_t *kt)
Returns the size of the message digest, in bytes.
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:134
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:1804
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:1958
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
const char * crl_file_inline
Definition: ssl_common.h:285
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
#define KS_PRIMARY
Primary key state index.
Definition: ssl_common.h:384
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:506
#define S_START
Three-way handshake is complete, start of key exchange.
Definition: ssl_common.h:91
#define INCR_SUCCESS
Definition: ssl.c:104
openvpn_net_ctx_t * net_ctx
Definition: ssl_common.h:337
void tls_ctx_load_extra_certs(struct tls_root_ctx *ctx, const char *extra_certs_file, const char *extra_certs_file_inline)
Load extra certificate authority certificates from the given file or path.
Definition: ssl_openssl.c:1577
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:4191
#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:467
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:1255
char * locked_cn
Definition: ssl_common.h:540
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:357
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:365
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:534
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
#define INLINE_FILE_TAG
Definition: common.h:95
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:370
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:114
unsigned __int32 uint32_t
Definition: config-msvc.h:121
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:507
#define INCR_ERROR
Definition: ssl.c:105
const char * ciphername
Definition: options.h:503
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:400
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:373
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:478
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:427
static bool link_socket_actual_defined(const struct link_socket_actual *act)
Definition: socket.h:696
static struct user_pass auth_token
Definition: ssl.c:402
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:1199
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:462
#define TLS_RELIABLE_N_REC_BUFFERS
Definition: ssl.h:83
void tls_adjust_frame_parameters(struct frame *frame)
Definition: ssl.c:316
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:528
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:1921
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:874
void set_auth_token(struct user_pass *up, struct user_pass *tk, const char *token)
Definition: misc.c:480
static bool key_method_1_write(struct buffer *buf, struct tls_session *session)
Definition: ssl.c:2246
unsigned int ssl_flags
Definition: options.h:554
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:542
bool tls_check_ncp_cipher_list(const char *list)
Check whether the ciphers in the supplied list are supported.
Definition: ssl.c:4168
struct tls_wrap_ctx tls_wrap
Definition: ssl.h:132
void ssl_set_auth_nocache(void)
Definition: ssl.c:446
int hmac_ctx_size(const hmac_ctx_t *ctx)
struct tls_wrap_ctx tls_wrap
Definition: ssl_common.h:417
#define PERF_TLS_MULTI_PROCESS
Definition: perf.h:42
struct tls_options opt
Definition: ssl_common.h:509
void buf_clear(struct buffer *buf)
Definition: buffer.c:164
int n_soft_errors
Definition: ssl_common.h:535
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:101
const char * cert_file_inline
Definition: options.h:542
const struct plugin_list * plugins
Definition: ssl_common.h:338
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:444
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:911
#define PD_TLS_AUTH_HMAC_SIZE_MASK
Definition: ssl.h:596
#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:588
#define INCR_SENT
Definition: ssl.c:102
bool tls_rec_payload(struct tls_multi *multi, struct buffer *buf)
Definition: ssl.c:4093
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:1331
static const char * local_options_string(const struct tls_session *session)
Definition: ssl.c:71
struct connection_entry ce
Definition: options.h:223
#define SC_ECHO
Definition: misc.h:98
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:66
const char * local_options
Definition: ssl_common.h:258
#define TM_SIZE
Size of the tls_multi.session array.
Definition: ssl_common.h:471
#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:525
void show_available_tls_ciphers_list(const char *cipher_list, const char *tls_cert_profile, bool tls13)
Definition: ssl_openssl.c:2003
int tls_version_parse(const char *vstr, const char *extra)
Definition: ssl.c:517
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:1042
#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
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:1074
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:349
void tls_poor_mans_ncp(struct options *o, const char *remote_ciphername)
"Poor man&#39;s NCP": Use peer cipher if it is an allowed (NCP) cipher.
Definition: ssl.c:1938
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
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:409
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:133
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:120
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
void tls_ctx_load_ca(struct tls_root_ctx *ctx, const char *ca_file, const char *ca_file_inline, const char *ca_path, bool tls_server)
Load certificate authority certificates from the given file or path.
Definition: ssl_openssl.c:1424
unsigned __int8 uint8_t
Definition: config-msvc.h:123
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:420
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:124
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:3368
#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:459
Structure that wraps the TLS context.
Definition: ssl_mbedtls.h:90
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:99
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Definition: buffer.h:1087
static char * read_string_alloc(struct buffer *buf)
Definition: ssl.c:2220
#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY
bool plugin_defined(const struct plugin_list *pl, const int type)
Definition: plugin.c:894
bool ssl_clean_auth_token(void)
Definition: ssl.c:467
static void key_state_free(struct key_state *ks, bool clear)
Cleanup a key_state structure.
Definition: ssl.c:982
#define KS_LAME_DUCK
Key state index that will retire soon.
Definition: ssl_common.h:385
static void check_malloc_return(const void *p)
Definition: buffer.h:1093
#define P_CONTROL_SOFT_RESET_V1
Definition: ssl.h:56
const char * extra_certs_file_inline
Definition: options.h:543
struct key_source2 * key_src
Definition: ssl_common.h:183
int initial_opcode
Definition: ssl_common.h:419
void tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file, const char *dh_file_inline)
Load Diffie Hellman Parameters, and load them into the library-specific TLS context.
Definition: ssl_openssl.c:576
static bool key_method_1_read(struct buffer *buf, struct tls_session *session)
Definition: ssl.c:2502
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:4337
Security parameter state of a single session within a VPN tunnel.
Definition: ssl_common.h:408
#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:284
void output_peer_info_env(struct env_set *es, const char *peer_info)
Definition: misc.c:717
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:62
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:1277
#define TLS_AUTHENTICATION_SUCCEEDED
Definition: ssl_verify.h:68
#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:4054
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:2386
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:532
#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:541
char * common_name
Definition: ssl_common.h:433
void reliable_init(struct reliable *rel, int buf_size, int offset, int array_size, bool hold)
Initialize a reliable structure.
Definition: reliable.c:309
int tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file, const char *priv_key_file_inline)
Load private key file into the given TLS context.
Definition: ssl_openssl.c:936
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)
const char * translate_cipher_name_from_openvpn(const char *cipher_name)
Translate a data channel cipher name from the crypto library specific name to the OpenVPN config file...
Definition: crypto.c:1831
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:152
#define TM_LAME_DUCK
Old tls_session.
Definition: ssl_common.h:470
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:585
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:1906
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:599
static const char * state_name(int state)
Definition: ssl.c:740
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:496
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_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *cert_file, const char *cert_file_inline)
Use Windows cryptoapi for key and cert, and add to library-specific TLS context.
Definition: ssl_openssl.c:870
void tls_x509_clear_env(struct env_set *es)
Remove any X509_ env variables from env_set es.
Definition: ssl_verify.c:1496
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:215
#define PD_TLS
Definition: ssl.h:598
#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:1022
void key2_print(const struct key2 *k, const struct key_type *kt, const char *prefix0, const char *prefix1)
Definition: crypto.c:1066
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:883
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:1347
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:2148
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:1891
static const tls_cipher_name_pair tls_cipher_name_translation_table[]
SSL/TLS Cipher suite name translation table.
Definition: ssl.c:112
static void key_state_init(struct tls_session *session, struct key_state *ks)
Initialize a key_state structure.
Definition: ssl.c:902
static int key_source2_read(struct key_source2 *k2, struct buffer *buf, bool server)
Definition: ssl.c:2102
#define BSTR(buf)
Definition: buffer.h:129
static bool auth_user_pass_enabled
Definition: ssl.c:400
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:526
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:1146
char password[USER_PASS_LEN]
Definition: misc.h:75
#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:1434
static bool random_bytes_to_buf(struct buffer *buf, uint8_t *out, int outlen)
Definition: ssl.c:2053
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
struct gc_arena gc
Definition: options.h:190
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:511
static int auth_deferred_expire_window(const struct tls_options *o)
Definition: ssl.c:2744
char * dest
Definition: compat-lz4.h:431
counter_type n_packets
Definition: ssl_common.h:196
int tls_peer_info_ncp_ver(const char *peer_info)
Return the Negotiable Crypto Parameters version advertised in the peer info string, or 0 if none specified.
Definition: ssl.c:4152
#define TLS_CHANNEL_BUF_SIZE
Definition: common.h:78
#define MF_EXTERNAL_CERT
Definition: manage.h:349
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
const char * dh_file_inline
Definition: options.h:546
struct key_ctx tls_crypt_v2_server_key
Decrypts client keys.
Definition: ssl_common.h:229
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:270
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
const cipher_kt_t * cipher_kt_get(const char *ciphername)
Return cipher parameters, based on the given cipher name.
void crypto_init_lib(void)
unsigned int ssl_flags
Definition: ssl_common.h:356
char * priv_key_file_inline
Definition: options.h:545
struct _stat platform_stat_t
Definition: platform.h:141
#define PD_SHOW_DATA
Definition: ssl.h:597
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:336
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:2563
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:743
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:112
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
enum tls_wrap_ctx::@9 mode
Control channel wrapping mode.
int link_mtu
Definition: options.h:111
#define INCR_GENERATED
Definition: ssl.c:103
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:3173
const char * crl_file
Definition: options.h:539