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