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-2023 OpenVPN Inc <sales@openvpn.net>
9  * Copyright (C) 2010-2021 Fox Crypto B.V. <openvpn@foxcrypto.com>
10  * Copyright (C) 2008-2023 David Sommerseth <dazo@eurephia.org>
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 #endif
40 
41 #include "syshead.h"
42 #include "win32.h"
43 
44 #include "error.h"
45 #include "common.h"
46 #include "socket.h"
47 #include "misc.h"
48 #include "fdmisc.h"
49 #include "interval.h"
50 #include "perf.h"
51 #include "status.h"
52 #include "gremlin.h"
53 #include "pkcs11.h"
54 #include "route.h"
55 #include "tls_crypt.h"
56 
57 #include "ssl.h"
58 #include "ssl_verify.h"
59 #include "ssl_backend.h"
60 #include "ssl_ncp.h"
61 #include "ssl_util.h"
62 #include "auth_token.h"
63 #include "mss.h"
64 #include "dco.h"
65 
66 #include "memdbg.h"
67 #include "openvpn.h"
68 
69 #ifdef MEASURE_TLS_HANDSHAKE_STATS
70 
71 static int tls_handshake_success; /* GLOBAL */
72 static int tls_handshake_error; /* GLOBAL */
73 static int tls_packets_generated; /* GLOBAL */
74 static int tls_packets_sent; /* GLOBAL */
75 
76 #define INCR_SENT ++tls_packets_sent
77 #define INCR_GENERATED ++tls_packets_generated
78 #define INCR_SUCCESS ++tls_handshake_success
79 #define INCR_ERROR ++tls_handshake_error
80 
81 void
82 show_tls_performance_stats(void)
83 {
84  msg(D_TLS_DEBUG_LOW, "TLS Handshakes, success=%f%% (good=%d, bad=%d), retransmits=%f%%",
85  (double) tls_handshake_success / (tls_handshake_success + tls_handshake_error) * 100.0,
86  tls_handshake_success, tls_handshake_error,
87  (double) (tls_packets_sent - tls_packets_generated) / tls_packets_generated * 100.0);
88 }
89 #else /* ifdef MEASURE_TLS_HANDSHAKE_STATS */
90 
91 #define INCR_SENT
92 #define INCR_GENERATED
93 #define INCR_SUCCESS
94 #define INCR_ERROR
95 
96 #endif /* ifdef MEASURE_TLS_HANDSHAKE_STATS */
97 
109 static void
110 key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len);
111 
112 
120 static void
121 tls_limit_reneg_bytes(const char *ciphername, int *reneg_bytes)
122 {
123  if (cipher_kt_insecure(ciphername))
124  {
125  if (*reneg_bytes == -1) /* Not user-specified */
126  {
127  msg(M_WARN, "WARNING: cipher with small block size in use, "
128  "reducing reneg-bytes to 64MB to mitigate SWEET32 attacks.");
129  *reneg_bytes = 64 * 1024 * 1024;
130  }
131  }
132 }
133 
134 void
136 {
137  /*
138  * frame->extra_frame is already initialized with tls_auth buffer requirements,
139  * if --tls-auth is enabled.
140  */
141 
142  /* calculates the maximum overhead that control channel frames can have */
143  int overhead = 0;
144 
145  /* Socks */
146  overhead += 10;
147 
148  /* tls-auth and tls-crypt */
149  overhead += max_int(tls_crypt_buf_overhead(),
151 
152  /* TCP length field and opcode */
153  overhead += 3;
154 
155  /* ACK array and remote SESSION ID (part of the ACK array) */
156  overhead += ACK_SIZE(RELIABLE_ACK_SIZE);
157 
158  /* Previous OpenVPN version calculated the maximum size and buffer of a
159  * control frame depending on the overhead of the data channel frame
160  * overhead and limited its maximum size to 1250. Since control frames
161  * also need to fit into data channel buffer we have the same
162  * default of 1500 + 100 as data channel buffers have. Increasing
163  * control channel mtu beyond this limit also increases the data channel
164  * buffers */
165  frame->buf.payload_size = max_int(1500, tls_mtu) + 100;
166 
167  frame->buf.headroom = overhead;
168  frame->buf.tailroom = overhead;
169 
170  frame->tun_mtu = tls_mtu;
171 
172  /* Ensure the tun-mtu stays in a valid range */
175 }
176 
182 static int
184 {
185  const struct key_state *ks = &session->key[KS_PRIMARY];
186  int overhead = 0;
187 
188  /* opcode */
189  overhead += 1;
190 
191  /* our own session id */
192  overhead += SID_SIZE;
193 
194  /* ACK array and remote SESSION ID (part of the ACK array) */
195  int ackstosend = reliable_ack_outstanding(ks->rec_ack) + ks->lru_acks->len;
196  overhead += ACK_SIZE(min_int(ackstosend, CONTROL_SEND_ACK_MAX));
197 
198  /* Message packet id */
199  overhead += sizeof(packet_id_type);
200 
201  if (session->tls_wrap.mode == TLS_WRAP_CRYPT)
202  {
203  overhead += tls_crypt_buf_overhead();
204  }
205  else if (session->tls_wrap.mode == TLS_WRAP_AUTH)
206  {
207  overhead += hmac_ctx_size(session->tls_wrap.opt.key_ctx_bi.encrypt.hmac);
208  overhead += packet_id_size(true);
209  }
210 
211  /* Add the typical UDP overhead for an IPv6 UDP packet. TCP+IPv6 has a
212  * larger overhead but the risk of a TCP connection getting dropped because
213  * we try to send a too large packet is basically zero */
214  overhead += datagram_overhead(session->untrusted_addr.dest.addr.sa.sa_family,
215  PROTO_UDP);
216 
217  return overhead;
218 }
219 
220 void
222 {
223  tls_init_lib();
224 
225  crypto_init_lib();
226 }
227 
228 void
230 {
232 
233  tls_free_lib();
234 }
235 
236 /*
237  * OpenSSL library calls pem_password_callback if the
238  * private key is protected by a password.
239  */
240 
241 static struct user_pass passbuf; /* GLOBAL */
242 
243 void
244 pem_password_setup(const char *auth_file)
245 {
246  if (!strlen(passbuf.password))
247  {
249  }
250 }
251 
252 int
253 pem_password_callback(char *buf, int size, int rwflag, void *u)
254 {
255  if (buf)
256  {
257  /* prompt for password even if --askpass wasn't specified */
258  pem_password_setup(NULL);
259  strncpynt(buf, passbuf.password, size);
260  purge_user_pass(&passbuf, false);
261 
262  return strlen(buf);
263  }
264  return 0;
265 }
266 
267 /*
268  * Auth username/password handling
269  */
270 
271 static bool auth_user_pass_enabled; /* GLOBAL */
272 static struct user_pass auth_user_pass; /* GLOBAL */
273 static struct user_pass auth_token; /* GLOBAL */
274 
275 #ifdef ENABLE_MANAGEMENT
276 static char *auth_challenge; /* GLOBAL */
277 #endif
278 
279 void
281 {
282  auth_user_pass_enabled = true;
283 }
284 
285 void
286 auth_user_pass_setup(const char *auth_file, bool is_inline,
287  const struct static_challenge_info *sci)
288 {
289  unsigned int flags = GET_USER_PASS_MANAGEMENT;
290 
291  if (is_inline)
292  {
294  }
295 
297  {
298 #ifdef ENABLE_MANAGEMENT
299  if (auth_challenge) /* dynamic challenge/response */
300  {
303  auth_file,
304  UP_TYPE_AUTH,
305  flags,
307  }
308  else if (sci) /* static challenge response */
309  {
311  if (sci->flags & SC_ECHO)
312  {
314  }
316  auth_file,
317  UP_TYPE_AUTH,
318  flags,
319  sci->challenge_text);
320  }
321  else
322 #endif /* ifdef ENABLE_MANAGEMENT */
323  get_user_pass(&auth_user_pass, auth_file, UP_TYPE_AUTH, flags);
324  }
325 }
326 
327 /*
328  * Disable password caching
329  */
330 void
332 {
333  passbuf.nocache = true;
334  auth_user_pass.nocache = true;
335 }
336 
337 /*
338  * Set an authentication token
339  */
340 void
341 ssl_set_auth_token(const char *token)
342 {
344 }
345 
346 void
348 {
350 }
351 
352 /*
353  * Cleans an auth token and checks if it was active
354  */
355 bool
357 {
358  bool wasdefined = auth_token.defined;
359  purge_user_pass(&auth_token, true);
360  return wasdefined;
361 }
362 
363 /*
364  * Forget private key password AND auth-user-pass username/password.
365  */
366 void
367 ssl_purge_auth(const bool auth_user_pass_only)
368 {
369  if (!auth_user_pass_only)
370  {
371 #ifdef ENABLE_PKCS11
372  pkcs11_logout();
373 #endif
374  purge_user_pass(&passbuf, true);
375  }
377 #ifdef ENABLE_MANAGEMENT
379 #endif
380 }
381 
382 #ifdef ENABLE_MANAGEMENT
383 
384 void
386 {
387  free(auth_challenge);
388  auth_challenge = NULL;
389 }
390 
391 void
392 ssl_put_auth_challenge(const char *cr_str)
393 {
395  auth_challenge = string_alloc(cr_str, NULL);
396 }
397 
398 #endif
399 
400 /*
401  * Parse a TLS version string, returning a TLS_VER_x constant.
402  * If version string is not recognized and extra == "or-highest",
403  * return tls_version_max().
404  */
405 int
406 tls_version_parse(const char *vstr, const char *extra)
407 {
408  const int max_version = tls_version_max();
409  if (!strcmp(vstr, "1.0") && TLS_VER_1_0 <= max_version)
410  {
411  return TLS_VER_1_0;
412  }
413  else if (!strcmp(vstr, "1.1") && TLS_VER_1_1 <= max_version)
414  {
415  return TLS_VER_1_1;
416  }
417  else if (!strcmp(vstr, "1.2") && TLS_VER_1_2 <= max_version)
418  {
419  return TLS_VER_1_2;
420  }
421  else if (!strcmp(vstr, "1.3") && TLS_VER_1_3 <= max_version)
422  {
423  return TLS_VER_1_3;
424  }
425  else if (extra && !strcmp(extra, "or-highest"))
426  {
427  return max_version;
428  }
429  else
430  {
431  return TLS_VER_BAD;
432  }
433 }
434 
446 static void
447 tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file,
448  bool crl_file_inline)
449 {
450  /* if something goes wrong with stat(), we'll store 0 as mtime */
451  platform_stat_t crl_stat = {0};
452 
453  /*
454  * an inline CRL can't change at runtime, therefore there is no need to
455  * reload it. It will be reloaded upon config change + SIGHUP.
456  * Use always '1' as dummy timestamp in this case: it will trigger the
457  * first load, but will prevent any future reload.
458  */
459  if (crl_file_inline)
460  {
461  crl_stat.st_mtime = 1;
462  }
463  else if (platform_stat(crl_file, &crl_stat) < 0)
464  {
465  /* If crl_last_mtime is zero, the CRL file has not been read before. */
466  if (ssl_ctx->crl_last_mtime == 0)
467  {
468  msg(M_FATAL, "ERROR: Failed to stat CRL file during initialization, exiting.");
469  }
470  else
471  {
472  msg(M_WARN, "WARNING: Failed to stat CRL file, not reloading CRL.");
473  }
474  return;
475  }
476 
477  /*
478  * Store the CRL if this is the first time or if the file was changed since
479  * the last load.
480  * Note: Windows does not support tv_nsec.
481  */
482  if ((ssl_ctx->crl_last_size == crl_stat.st_size)
483  && (ssl_ctx->crl_last_mtime == crl_stat.st_mtime))
484  {
485  return;
486  }
487 
488  ssl_ctx->crl_last_mtime = crl_stat.st_mtime;
489  ssl_ctx->crl_last_size = crl_stat.st_size;
490  backend_tls_ctx_reload_crl(ssl_ctx, crl_file, crl_file_inline);
491 }
492 
493 /*
494  * Initialize SSL context.
495  * All files are in PEM format.
496  */
497 void
498 init_ssl(const struct options *options, struct tls_root_ctx *new_ctx, bool in_chroot)
499 {
500  ASSERT(NULL != new_ctx);
501 
502  tls_clear_error();
503 
505  {
507  }
508 
509  if (options->tls_server)
510  {
511  tls_ctx_server_new(new_ctx);
512 
513  if (options->dh_file)
514  {
517  }
518  }
519  else /* if client */
520  {
521  tls_ctx_client_new(new_ctx);
522  }
523 
524  /* Restrict allowed certificate crypto algorithms */
526 
527  /* Allowable ciphers */
528  /* Since @SECLEVEL also influences loading of certificates, set the
529  * cipher restrictions before loading certificates */
532 
533  /* Set the allow groups/curves for TLS if we want to override them */
534  if (options->tls_groups)
535  {
537  }
538 
539  if (!tls_ctx_set_options(new_ctx, options->ssl_flags))
540  {
541  goto err;
542  }
543 
544  if (options->pkcs12_file)
545  {
546  if (0 != tls_ctx_load_pkcs12(new_ctx, options->pkcs12_file,
548  {
549  goto err;
550  }
551  }
552 #ifdef ENABLE_PKCS11
553  else if (options->pkcs11_providers[0])
554  {
555  if (!tls_ctx_use_pkcs11(new_ctx, options->pkcs11_id_management, options->pkcs11_id))
556  {
557  msg(M_WARN, "Cannot load certificate \"%s\" using PKCS#11 interface",
558  options->pkcs11_id);
559  goto err;
560  }
561  }
562 #endif
563 #ifdef ENABLE_CRYPTOAPI
564  else if (options->cryptoapi_cert)
565  {
567  }
568 #endif
569 #ifdef ENABLE_MANAGEMENT
571  {
572  char *cert = management_query_cert(management,
574  tls_ctx_load_cert_file(new_ctx, cert, true);
575  free(cert);
576  }
577 #endif
578  else if (options->cert_file)
579  {
581  }
582 
583  if (options->priv_key_file)
584  {
585  if (0 != tls_ctx_load_priv_file(new_ctx, options->priv_key_file,
587  {
588  goto err;
589  }
590  }
591 #ifdef ENABLE_MANAGEMENT
593  {
595  {
596  msg(M_WARN, "Cannot initialize mamagement-external-key");
597  goto err;
598  }
599  }
600 #endif
601 
602  if (options->ca_file || options->ca_path)
603  {
606  }
607 
608  /* Load extra certificates that are part of our own certificate
609  * chain but shouldn't be included in the verify chain */
611  {
613  }
614 
615  /* Check certificate notBefore and notAfter */
616  tls_ctx_check_cert_time(new_ctx);
617 
618  /* Read CRL */
620  {
621  /* If we're running with the chroot option, we may run init_ssl() before
622  * and after chroot-ing. We can use the crl_file path as-is if we're
623  * not going to chroot, or if we already are inside the chroot.
624  *
625  * If we're going to chroot later, we need to prefix the path of the
626  * chroot directory to crl_file.
627  */
628  if (!options->chroot_dir || in_chroot || options->crl_file_inline)
629  {
631  }
632  else
633  {
634  struct gc_arena gc = gc_new();
635  struct buffer crl_file_buf = prepend_dir(options->chroot_dir, options->crl_file, &gc);
636  tls_ctx_reload_crl(new_ctx, BSTR(&crl_file_buf), options->crl_file_inline);
637  gc_free(&gc);
638  }
639  }
640 
641  /* Once keys and cert are loaded, load ECDH parameters */
642  if (options->tls_server)
643  {
645  }
646 
647 #ifdef ENABLE_CRYPTO_MBEDTLS
648  /* Personalise the random by mixing in the certificate */
649  tls_ctx_personalise_random(new_ctx);
650 #endif
651 
652  tls_clear_error();
653  return;
654 
655 err:
656  tls_clear_error();
657  tls_ctx_free(new_ctx);
658  return;
659 }
660 
661 /*
662  * Map internal constants to ascii names.
663  */
664 static const char *
665 state_name(int state)
666 {
667  switch (state)
668  {
669  case S_UNDEF:
670  return "S_UNDEF";
671 
672  case S_INITIAL:
673  return "S_INITIAL";
674 
675  case S_PRE_START:
676  return "S_PRE_START";
677 
678  case S_START:
679  return "S_START";
680 
681  case S_SENT_KEY:
682  return "S_SENT_KEY";
683 
684  case S_GOT_KEY:
685  return "S_GOT_KEY";
686 
687  case S_ACTIVE:
688  return "S_ACTIVE";
689 
690  case S_ERROR:
691  return "S_ERROR";
692 
693  case S_GENERATED_KEYS:
694  return "S_GENERATED_KEYS";
695 
696  default:
697  return "S_???";
698  }
699 }
700 
701 static const char *
703 {
704  switch (auth)
705  {
706  case KS_AUTH_TRUE:
707  return "KS_AUTH_TRUE";
708 
709  case KS_AUTH_DEFERRED:
710  return "KS_AUTH_DEFERRED";
711 
712  case KS_AUTH_FALSE:
713  return "KS_AUTH_FALSE";
714 
715  default:
716  return "KS_????";
717  }
718 }
719 
720 static const char *
722 {
723  switch (index)
724  {
725  case TM_ACTIVE:
726  return "TM_ACTIVE";
727 
728  case TM_INITIAL:
729  return "TM_INITIAL";
730 
731  case TM_LAME_DUCK:
732  return "TM_LAME_DUCK";
733 
734  default:
735  return "TM_???";
736  }
737 }
738 
739 /*
740  * For debugging.
741  */
742 static const char *
743 print_key_id(struct tls_multi *multi, struct gc_arena *gc)
744 {
745  struct buffer out = alloc_buf_gc(256, gc);
746 
747  for (int i = 0; i < KEY_SCAN_SIZE; ++i)
748  {
749  struct key_state *ks = get_key_scan(multi, i);
750  buf_printf(&out, " [key#%d state=%s auth=%s id=%d sid=%s]", i,
752  ks->key_id,
754  }
755 
756  return BSTR(&out);
757 }
758 
759 bool
761 {
764  {
765  return true;
766  }
767 
768  return false;
769 }
770 
794 static void
796 {
797  update_time();
798 
799  CLEAR(*ks);
800 
801  /*
802  * Build TLS object that reads/writes ciphertext
803  * to/from memory BIOs.
804  */
805  key_state_ssl_init(&ks->ks_ssl, &session->opt->ssl_ctx, session->opt->server,
806  session);
807 
808  /* Set control-channel initiation mode */
809  ks->initial_opcode = session->initial_opcode;
810  session->initial_opcode = P_CONTROL_SOFT_RESET_V1;
811  ks->state = S_INITIAL;
812  ks->key_id = session->key_id;
813 
814  /*
815  * key_id increments to KEY_ID_MASK then recycles back to 1.
816  * This way you know that if key_id is 0, it is the first key.
817  */
818  ++session->key_id;
819  session->key_id &= P_KEY_ID_MASK;
820  if (!session->key_id)
821  {
822  session->key_id = 1;
823  }
824 
825  /* allocate key source material object */
826  ALLOC_OBJ_CLEAR(ks->key_src, struct key_source2);
827 
828  /* allocate reliability objects */
831  ALLOC_OBJ_CLEAR(ks->rec_ack, struct reliable_ack);
833 
834  /* allocate buffers */
837  ks->ack_write_buf = alloc_buf(BUF_SIZE(&session->opt->frame));
838  reliable_init(ks->send_reliable, BUF_SIZE(&session->opt->frame),
839  session->opt->frame.buf.headroom, TLS_RELIABLE_N_SEND_BUFFERS,
840  ks->key_id ? false : session->opt->xmit_hold);
841  reliable_init(ks->rec_reliable, BUF_SIZE(&session->opt->frame),
842  session->opt->frame.buf.headroom, TLS_RELIABLE_N_REC_BUFFERS,
843  false);
844  reliable_set_timeout(ks->send_reliable, session->opt->packet_timeout);
845 
846  /* init packet ID tracker */
848  session->opt->replay_window, session->opt->replay_time, "SSL",
849  ks->key_id);
850 
851  ks->crypto_options.pid_persist = NULL;
852 
853 #ifdef ENABLE_MANAGEMENT
854  ks->mda_key_id = session->opt->mda_context->mda_key_id_counter++;
855 #endif
856 
857  /*
858  * Attempt CRL reload before TLS negotiation. Won't be performed if
859  * the file was not modified since the last reload
860  */
861  if (session->opt->crl_file
862  && !(session->opt->ssl_flags & SSLF_CRL_VERIFY_DIR))
863  {
864  tls_ctx_reload_crl(&session->opt->ssl_ctx,
865  session->opt->crl_file, session->opt->crl_file_inline);
866  }
867 }
868 
869 
883 static void
884 key_state_free(struct key_state *ks, bool clear)
885 {
886  ks->state = S_UNDEF;
887 
889 
893  free_buf(&ks->ack_write_buf);
895 
898 
899  free(ks->rec_ack);
900  free(ks->lru_acks);
901  free(ks->key_src);
902 
904 
907 
908  if (clear)
909  {
910  secure_memzero(ks, sizeof(*ks));
911  }
912 }
913 
927 static inline bool
929 {
930  return (session->opt->auth_user_pass_verify_script
932 #ifdef ENABLE_MANAGEMENT
934 #endif
935  );
936 }
937 
938 
959 static void
961 {
962  struct gc_arena gc = gc_new();
963 
964  dmsg(D_TLS_DEBUG, "TLS: tls_session_init: entry");
965 
966  CLEAR(*session);
967 
968  /* Set options data to point to parent's option structure */
969  session->opt = &multi->opt;
970 
971  /* Randomize session # if it is 0 */
972  while (!session_id_defined(&session->session_id))
973  {
974  session_id_random(&session->session_id);
975  }
976 
977  /* Are we a TLS server or client? */
978  if (session->opt->server)
979  {
980  session->initial_opcode = P_CONTROL_HARD_RESET_SERVER_V2;
981  }
982  else
983  {
984  session->initial_opcode = session->opt->tls_crypt_v2 ?
986  }
987 
988  /* Initialize control channel authentication parameters */
989  session->tls_wrap = session->opt->tls_wrap;
990  session->tls_wrap.work = alloc_buf(BUF_SIZE(&session->opt->frame));
991 
992  /* initialize packet ID replay window for --tls-auth */
993  packet_id_init(&session->tls_wrap.opt.packet_id,
994  session->opt->replay_window,
995  session->opt->replay_time,
996  "TLS_WRAP", session->key_id);
997 
998  /* If we are using tls-crypt-v2 we manipulate the packet id to be (ab)used
999  * to indicate early protocol negotiation */
1000  if (session->opt->tls_crypt_v2)
1001  {
1002  session->tls_wrap.opt.packet_id.send.time = now;
1003  session->tls_wrap.opt.packet_id.send.id = EARLY_NEG_START;
1004  }
1005 
1006  /* load most recent packet-id to replay protect on --tls-auth */
1007  packet_id_persist_load_obj(session->tls_wrap.opt.pid_persist,
1008  &session->tls_wrap.opt.packet_id);
1009 
1011 
1012  dmsg(D_TLS_DEBUG, "TLS: tls_session_init: new session object, sid=%s",
1013  session_id_print(&session->session_id, &gc));
1014 
1015  gc_free(&gc);
1016 }
1017 
1033 static void
1035 {
1036  tls_wrap_free(&session->tls_wrap);
1037  tls_wrap_free(&session->tls_wrap_reneg);
1038 
1039  for (size_t i = 0; i < KS_SIZE; ++i)
1040  {
1041  /* we don't need clear=true for this call since
1042  * the structs are part of session and get cleared
1043  * as part of session */
1044  key_state_free(&session->key[i], false);
1045  }
1046 
1047  free(session->common_name);
1048 
1049  cert_hash_free(session->cert_hash_set);
1050 
1051  if (clear)
1052  {
1053  secure_memzero(session, sizeof(*session));
1054  }
1055 }
1056 
1062 static void
1063 move_session(struct tls_multi *multi, int dest, int src, bool reinit_src)
1064 {
1065  msg(D_TLS_DEBUG_LOW, "TLS: move_session: dest=%s src=%s reinit_src=%d",
1066  session_index_name(dest),
1067  session_index_name(src),
1068  reinit_src);
1069  ASSERT(src != dest);
1070  ASSERT(src >= 0 && src < TM_SIZE);
1071  ASSERT(dest >= 0 && dest < TM_SIZE);
1072  tls_session_free(&multi->session[dest], false);
1073  multi->session[dest] = multi->session[src];
1074 
1075  if (reinit_src)
1076  {
1077  tls_session_init(multi, &multi->session[src]);
1078  }
1079  else
1080  {
1081  secure_memzero(&multi->session[src], sizeof(multi->session[src]));
1082  }
1083 
1084  dmsg(D_TLS_DEBUG, "TLS: move_session: exit");
1085 }
1086 
1087 static void
1089 {
1090  tls_session_free(session, false);
1091  tls_session_init(multi, session);
1092 }
1093 
1094 /*
1095  * Used to determine in how many seconds we should be
1096  * called again.
1097  */
1098 static inline void
1099 compute_earliest_wakeup(interval_t *earliest, interval_t seconds_from_now)
1100 {
1101  if (seconds_from_now < *earliest)
1102  {
1103  *earliest = seconds_from_now;
1104  }
1105  if (*earliest < 0)
1106  {
1107  *earliest = 0;
1108  }
1109 }
1110 
1111 /*
1112  * Return true if "lame duck" or retiring key has expired and can
1113  * no longer be used.
1114  */
1115 static inline bool
1117 {
1118  const struct key_state *lame = &session->key[KS_LAME_DUCK];
1119  if (lame->state >= S_INITIAL)
1120  {
1121  ASSERT(lame->must_die); /* a lame duck key must always have an expiration */
1122  if (now < lame->must_die)
1123  {
1124  compute_earliest_wakeup(wakeup, lame->must_die - now);
1125  return false;
1126  }
1127  else
1128  {
1129  return true;
1130  }
1131  }
1132  else if (lame->state == S_ERROR)
1133  {
1134  return true;
1135  }
1136  else
1137  {
1138  return false;
1139  }
1140 }
1141 
1142 struct tls_multi *
1144 {
1145  struct tls_multi *ret;
1146 
1147  ALLOC_OBJ_CLEAR(ret, struct tls_multi);
1148 
1149  /* get command line derived options */
1150  ret->opt = *tls_options;
1151  ret->dco_peer_id = -1;
1152  ret->peer_id = MAX_PEER_ID;
1153 
1154  return ret;
1155 }
1156 
1157 void
1158 tls_multi_init_finalize(struct tls_multi *multi, int tls_mtu)
1159 {
1161  /* initialize the active and untrusted sessions */
1162 
1163  tls_session_init(multi, &multi->session[TM_ACTIVE]);
1164  tls_session_init(multi, &multi->session[TM_INITIAL]);
1165 }
1166 
1167 /*
1168  * Initialize and finalize a standalone tls-auth verification object.
1169  */
1170 
1171 struct tls_auth_standalone *
1173  struct gc_arena *gc)
1174 {
1175  struct tls_auth_standalone *tas;
1176 
1177  ALLOC_OBJ_CLEAR_GC(tas, struct tls_auth_standalone, gc);
1178 
1179  tas->tls_wrap = tls_options->tls_wrap;
1180 
1181  /*
1182  * Standalone tls-auth is in read-only mode with respect to TLS
1183  * control channel state. After we build a new client instance
1184  * object, we will process this session-initiating packet for real.
1185  */
1187 
1188  /* get initial frame parms, still need to finalize */
1189  tas->frame = tls_options->frame;
1190 
1192  tls_options->replay_time, "TAS", 0);
1193 
1194  return tas;
1195 }
1196 
1197 void
1199 {
1200  if (!tas)
1201  {
1202  return;
1203  }
1204 
1206 }
1207 
1208 /*
1209  * Set local and remote option compatibility strings.
1210  * Used to verify compatibility of local and remote option
1211  * sets.
1212  */
1213 void
1215  const char *local,
1216  const char *remote)
1217 {
1218  /* initialize options string */
1219  multi->opt.local_options = local;
1220  multi->opt.remote_options = remote;
1221 }
1222 
1223 /*
1224  * Cleanup a tls_multi structure and free associated memory allocations.
1225  */
1226 void
1227 tls_multi_free(struct tls_multi *multi, bool clear)
1228 {
1229  ASSERT(multi);
1230 
1231  auth_set_client_reason(multi, NULL);
1232 
1233  free(multi->peer_info);
1234  free(multi->locked_cn);
1235  free(multi->locked_username);
1236 
1238 
1239  wipe_auth_token(multi);
1240 
1241  free(multi->remote_ciphername);
1242 
1243  for (int i = 0; i < TM_SIZE; ++i)
1244  {
1245  tls_session_free(&multi->session[i], false);
1246  }
1247 
1248  if (clear)
1249  {
1250  secure_memzero(multi, sizeof(*multi));
1251  }
1252 
1253  free(multi);
1254 }
1255 
1256 /*
1257  * For debugging, print contents of key_source2 structure.
1258  */
1259 
1260 static void
1262  const char *prefix)
1263 {
1264  struct gc_arena gc = gc_new();
1265 
1266  VALGRIND_MAKE_READABLE((void *)k->pre_master, sizeof(k->pre_master));
1267  VALGRIND_MAKE_READABLE((void *)k->random1, sizeof(k->random1));
1268  VALGRIND_MAKE_READABLE((void *)k->random2, sizeof(k->random2));
1269 
1271  "%s pre_master: %s",
1272  prefix,
1273  format_hex(k->pre_master, sizeof(k->pre_master), 0, &gc));
1275  "%s random1: %s",
1276  prefix,
1277  format_hex(k->random1, sizeof(k->random1), 0, &gc));
1279  "%s random2: %s",
1280  prefix,
1281  format_hex(k->random2, sizeof(k->random2), 0, &gc));
1282 
1283  gc_free(&gc);
1284 }
1285 
1286 static void
1288 {
1289  key_source_print(&k->client, "Client");
1290  key_source_print(&k->server, "Server");
1291 }
1292 
1293 static bool
1294 openvpn_PRF(const uint8_t *secret,
1295  int secret_len,
1296  const char *label,
1297  const uint8_t *client_seed,
1298  int client_seed_len,
1299  const uint8_t *server_seed,
1300  int server_seed_len,
1301  const struct session_id *client_sid,
1302  const struct session_id *server_sid,
1303  uint8_t *output,
1304  int output_len)
1305 {
1306  /* concatenate seed components */
1307 
1308  struct buffer seed = alloc_buf(strlen(label)
1309  + client_seed_len
1310  + server_seed_len
1311  + SID_SIZE * 2);
1312 
1313  ASSERT(buf_write(&seed, label, strlen(label)));
1314  ASSERT(buf_write(&seed, client_seed, client_seed_len));
1315  ASSERT(buf_write(&seed, server_seed, server_seed_len));
1316 
1317  if (client_sid)
1318  {
1319  ASSERT(buf_write(&seed, client_sid->id, SID_SIZE));
1320  }
1321  if (server_sid)
1322  {
1323  ASSERT(buf_write(&seed, server_sid->id, SID_SIZE));
1324  }
1325 
1326  /* compute PRF */
1327  bool ret = ssl_tls1_PRF(BPTR(&seed), BLEN(&seed), secret, secret_len,
1328  output, output_len);
1329 
1330  buf_clear(&seed);
1331  free_buf(&seed);
1332 
1333  VALGRIND_MAKE_READABLE((void *)output, output_len);
1334  return ret;
1335 }
1336 
1337 static void
1339  struct tls_multi *multi,
1340  const struct key_type *key_type,
1341  bool server,
1342  struct key2 *key2,
1343  bool dco_enabled)
1344 {
1345  struct key_ctx_bi *key = &ks->crypto_options.key_ctx_bi;
1346 
1347  /* Initialize key contexts */
1348  int key_direction = server ? KEY_DIRECTION_INVERSE : KEY_DIRECTION_NORMAL;
1349 
1350  if (dco_enabled)
1351  {
1352  if (key->encrypt.hmac)
1353  {
1354  msg(M_FATAL, "FATAL: DCO does not support --auth");
1355  }
1356 
1357  int ret = init_key_dco_bi(multi, ks, key2, key_direction,
1358  key_type->cipher, server);
1359  if (ret < 0)
1360  {
1361  msg(M_FATAL, "Impossible to install key material in DCO: %s",
1362  strerror(-ret));
1363  }
1364 
1365  /* encrypt/decrypt context are unused with DCO */
1366  CLEAR(key->encrypt);
1367  CLEAR(key->decrypt);
1368  key->initialized = true;
1369  }
1370  else
1371  {
1372  init_key_ctx_bi(key, key2, key_direction, key_type, "Data Channel");
1373  /* Initialize implicit IVs */
1374  key_ctx_update_implicit_iv(&key->encrypt, key2->keys[(int)server].hmac,
1376  key_ctx_update_implicit_iv(&key->decrypt,
1377  key2->keys[1 - (int)server].hmac,
1379  }
1380 }
1381 
1382 static bool
1384 {
1386  strlen(EXPORT_KEY_DATA_LABEL),
1387  key2->keys, sizeof(key2->keys)))
1388  {
1389  return false;
1390  }
1391  key2->n = 2;
1392 
1393  return true;
1394 }
1395 
1396 static bool
1398 {
1399  uint8_t master[48] = { 0 };
1400 
1401  const struct key_state *ks = &session->key[KS_PRIMARY];
1402  const struct key_source2 *key_src = ks->key_src;
1403 
1404  const struct session_id *client_sid = session->opt->server ?
1405  &ks->session_id_remote : &session->session_id;
1406  const struct session_id *server_sid = !session->opt->server ?
1407  &ks->session_id_remote : &session->session_id;
1408 
1409  /* debugging print of source key material */
1410  key_source2_print(key_src);
1411 
1412  /* compute master secret */
1413  if (!openvpn_PRF(key_src->client.pre_master,
1414  sizeof(key_src->client.pre_master),
1415  KEY_EXPANSION_ID " master secret",
1416  key_src->client.random1,
1417  sizeof(key_src->client.random1),
1418  key_src->server.random1,
1419  sizeof(key_src->server.random1),
1420  NULL,
1421  NULL,
1422  master,
1423  sizeof(master)))
1424  {
1425  return false;
1426  }
1427 
1428  /* compute key expansion */
1429  if (!openvpn_PRF(master,
1430  sizeof(master),
1431  KEY_EXPANSION_ID " key expansion",
1432  key_src->client.random2,
1433  sizeof(key_src->client.random2),
1434  key_src->server.random2,
1435  sizeof(key_src->server.random2),
1436  client_sid,
1437  server_sid,
1438  (uint8_t *)key2->keys,
1439  sizeof(key2->keys)))
1440  {
1441  return false;
1442  }
1443  secure_memzero(&master, sizeof(master));
1444 
1445  key2->n = 2;
1446 
1447  return true;
1448 }
1449 
1450 /*
1451  * Using source entropy from local and remote hosts, mix into
1452  * master key.
1453  */
1454 static bool
1455 generate_key_expansion(struct tls_multi *multi, struct key_state *ks,
1456  struct tls_session *session)
1457 {
1458  struct key_ctx_bi *key = &ks->crypto_options.key_ctx_bi;
1459  bool ret = false;
1460  struct key2 key2;
1461 
1462  if (key->initialized)
1463  {
1464  msg(D_TLS_ERRORS, "TLS Error: key already initialized");
1465  goto exit;
1466  }
1467 
1468  bool server = session->opt->server;
1469 
1470  if (session->opt->crypto_flags & CO_USE_TLS_KEY_MATERIAL_EXPORT)
1471  {
1473  {
1474  msg(D_TLS_ERRORS, "TLS Error: Keying material export failed");
1475  goto exit;
1476  }
1477  }
1478  else
1479  {
1481  {
1482  msg(D_TLS_ERRORS, "TLS Error: PRF calculation failed. Your system "
1483  "might not support the old TLS 1.0 PRF calculation anymore or "
1484  "the policy does not allow it (e.g. running in FIPS mode). "
1485  "The peer did not announce support for the modern TLS Export "
1486  "feature that replaces the TLS 1.0 PRF (requires OpenVPN "
1487  "2.6.x or higher)");
1488  goto exit;
1489  }
1490  }
1491 
1492  key2_print(&key2, &session->opt->key_type,
1493  "Master Encrypt", "Master Decrypt");
1494 
1495  /* check for weak keys */
1496  for (int i = 0; i < 2; ++i)
1497  {
1498  if (!check_key(&key2.keys[i], &session->opt->key_type))
1499  {
1500  msg(D_TLS_ERRORS, "TLS Error: Bad dynamic key generated");
1501  goto exit;
1502  }
1503  }
1504 
1505  init_key_contexts(ks, multi, &session->opt->key_type, server, &key2,
1506  session->opt->dco_enabled);
1507  ret = true;
1508 
1509 exit:
1510  secure_memzero(&key2, sizeof(key2));
1511 
1512  return ret;
1513 }
1514 
1515 static void
1516 key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len)
1517 {
1518  /* Only use implicit IV in AEAD cipher mode, where HMAC key is not used */
1519  if (cipher_ctx_mode_aead(ctx->cipher))
1520  {
1521  size_t impl_iv_len = 0;
1523  impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
1524  ASSERT(impl_iv_len <= OPENVPN_MAX_IV_LENGTH);
1525  ASSERT(impl_iv_len <= key_len);
1526  memcpy(ctx->implicit_iv, key, impl_iv_len);
1527  ctx->implicit_iv_len = impl_iv_len;
1528  }
1529 }
1530 
1537 bool
1539  struct tls_session *session)
1540 {
1541  bool ret = false;
1542  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1543 
1544  if (ks->authenticated <= KS_AUTH_FALSE)
1545  {
1546  msg(D_TLS_ERRORS, "TLS Error: key_state not authenticated");
1547  goto cleanup;
1548  }
1549 
1550  ks->crypto_options.flags = session->opt->crypto_flags;
1551 
1552  if (!generate_key_expansion(multi, ks, session))
1553  {
1554  msg(D_TLS_ERRORS, "TLS Error: generate_key_expansion failed");
1555  goto cleanup;
1556  }
1557  tls_limit_reneg_bytes(session->opt->key_type.cipher,
1558  &session->opt->renegotiate_bytes);
1559 
1560  /* set the state of the keys for the session to generated */
1561  ks->state = S_GENERATED_KEYS;
1562 
1563  ret = true;
1564 cleanup:
1565  secure_memzero(ks->key_src, sizeof(*ks->key_src));
1566  return ret;
1567 }
1568 
1569 bool
1571  struct tls_session *session,
1572  struct options *options,
1573  struct frame *frame,
1574  struct frame *frame_fragment,
1575  struct link_socket_info *lsi)
1576 {
1577  if (session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized)
1578  {
1579  /* keys already generated, nothing to do */
1580  return true;
1581 
1582  }
1583 
1584  init_key_type(&session->opt->key_type, options->ciphername,
1585  options->authname, true, true);
1586 
1587  bool packet_id_long_form = cipher_kt_mode_ofb_cfb(session->opt->key_type.cipher);
1588  session->opt->crypto_flags &= ~(CO_PACKET_ID_LONG_FORM);
1589  if (packet_id_long_form)
1590  {
1591  session->opt->crypto_flags |= CO_PACKET_ID_LONG_FORM;
1592  }
1593 
1594  frame_calculate_dynamic(frame, &session->opt->key_type, options, lsi);
1595 
1596  frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
1597 
1598  /*
1599  * mssfix uses data channel framing, which at this point contains
1600  * actual overhead. Fragmentation logic uses frame_fragment, which
1601  * still contains worst case overhead. Replace it with actual overhead
1602  * to prevent unneeded fragmentation.
1603  */
1604 
1605  if (frame_fragment)
1606  {
1607  frame_calculate_dynamic(frame_fragment, &session->opt->key_type, options, lsi);
1608  frame_print(frame_fragment, D_MTU_INFO, "Fragmentation MTU parms");
1609  }
1610 
1611  if (session->key[KS_PRIMARY].key_id == 0
1612  && session->opt->crypto_flags & CO_USE_DYNAMIC_TLS_CRYPT)
1613  {
1614  /* If dynamic tls-crypt has been negotiated, and we are on the
1615  * first session (key_id = 0), generate a tls-crypt key for the
1616  * following renegotiations */
1618  {
1619  return false;
1620  }
1621  }
1623 }
1624 
1625 bool
1627  struct tls_session *session,
1628  struct options *options, struct frame *frame,
1629  struct frame *frame_fragment,
1630  struct link_socket_info *lsi)
1631 {
1633  {
1634  return false;
1635  }
1636 
1637  /* Import crypto settings that might be set by pull/push */
1638  session->opt->crypto_flags |= options->imported_protocol_flags;
1639 
1641  frame, frame_fragment, lsi);
1642 }
1643 
1644 
1645 static bool
1647  uint8_t *out,
1648  int outlen)
1649 {
1650  if (!rand_bytes(out, outlen))
1651  {
1652  msg(M_FATAL, "ERROR: Random number generator cannot obtain entropy for key generation [SSL]");
1653  }
1654  if (!buf_write(buf, out, outlen))
1655  {
1656  return false;
1657  }
1658  return true;
1659 }
1660 
1661 static bool
1663  struct buffer *buf,
1664  bool server)
1665 {
1666  struct key_source *k = &k2->client;
1667  if (server)
1668  {
1669  k = &k2->server;
1670  }
1671 
1672  CLEAR(*k);
1673 
1674  if (!server)
1675  {
1676  if (!random_bytes_to_buf(buf, k->pre_master, sizeof(k->pre_master)))
1677  {
1678  return false;
1679  }
1680  }
1681 
1682  if (!random_bytes_to_buf(buf, k->random1, sizeof(k->random1)))
1683  {
1684  return false;
1685  }
1686  if (!random_bytes_to_buf(buf, k->random2, sizeof(k->random2)))
1687  {
1688  return false;
1689  }
1690 
1691  return true;
1692 }
1693 
1694 static int
1696  struct buffer *buf,
1697  bool server)
1698 {
1699  struct key_source *k = &k2->client;
1700 
1701  if (!server)
1702  {
1703  k = &k2->server;
1704  }
1705 
1706  CLEAR(*k);
1707 
1708  if (server)
1709  {
1710  if (!buf_read(buf, k->pre_master, sizeof(k->pre_master)))
1711  {
1712  return 0;
1713  }
1714  }
1715 
1716  if (!buf_read(buf, k->random1, sizeof(k->random1)))
1717  {
1718  return 0;
1719  }
1720  if (!buf_read(buf, k->random2, sizeof(k->random2)))
1721  {
1722  return 0;
1723  }
1724 
1725  return 1;
1726 }
1727 
1728 static void
1730 {
1731  struct buffer *b;
1732 
1733  while ((b = buffer_list_peek(ks->paybuf)))
1734  {
1736  buffer_list_pop(ks->paybuf);
1737  }
1738 }
1739 
1740 /*
1741  * Move the active key to the lame duck key and reinitialize the
1742  * active key.
1743  */
1744 static void
1746 {
1747  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
1748  struct key_state *ks_lame = &session->key[KS_LAME_DUCK]; /* retiring key */
1749 
1750  ks->must_die = now + session->opt->transition_window; /* remaining lifetime of old key */
1751  key_state_free(ks_lame, false);
1752  *ks_lame = *ks;
1753 
1754  key_state_init(session, ks);
1755  ks->session_id_remote = ks_lame->session_id_remote;
1756  ks->remote_addr = ks_lame->remote_addr;
1757 }
1758 
1759 void
1761 {
1763 }
1764 
1765 /*
1766  * Read/write strings from/to a struct buffer with a u16 length prefix.
1767  */
1768 
1769 static bool
1771 {
1772  if (!buf_write_u16(buf, 0))
1773  {
1774  return false;
1775  }
1776  return true;
1777 }
1778 
1779 static bool
1780 write_string(struct buffer *buf, const char *str, const int maxlen)
1781 {
1782  const int len = strlen(str) + 1;
1783  if (len < 1 || (maxlen >= 0 && len > maxlen))
1784  {
1785  return false;
1786  }
1787  if (!buf_write_u16(buf, len))
1788  {
1789  return false;
1790  }
1791  if (!buf_write(buf, str, len))
1792  {
1793  return false;
1794  }
1795  return true;
1796 }
1797 
1798 static bool
1799 read_string(struct buffer *buf, char *str, const unsigned int capacity)
1800 {
1801  const int len = buf_read_u16(buf);
1802  if (len < 1 || len > (int)capacity)
1803  {
1804  return false;
1805  }
1806  if (!buf_read(buf, str, len))
1807  {
1808  return false;
1809  }
1810  str[len-1] = '\0';
1811  return true;
1812 }
1813 
1814 static char *
1816 {
1817  const int len = buf_read_u16(buf);
1818  char *str;
1819 
1820  if (len < 1)
1821  {
1822  return NULL;
1823  }
1824  str = (char *) malloc(len);
1825  check_malloc_return(str);
1826  if (!buf_read(buf, str, len))
1827  {
1828  free(str);
1829  return NULL;
1830  }
1831  str[len-1] = '\0';
1832  return str;
1833 }
1834 
1850 static bool
1852 {
1853  struct gc_arena gc = gc_new();
1854  bool ret = false;
1855  struct buffer out = alloc_buf_gc(512 * 3, &gc);
1856 
1857  if (session->opt->push_peer_info_detail > 1)
1858  {
1859  /* push version */
1860  buf_printf(&out, "IV_VER=%s\n", PACKAGE_VERSION);
1861 
1862  /* push platform */
1863 #if defined(TARGET_LINUX)
1864  buf_printf(&out, "IV_PLAT=linux\n");
1865 #elif defined(TARGET_SOLARIS)
1866  buf_printf(&out, "IV_PLAT=solaris\n");
1867 #elif defined(TARGET_OPENBSD)
1868  buf_printf(&out, "IV_PLAT=openbsd\n");
1869 #elif defined(TARGET_DARWIN)
1870  buf_printf(&out, "IV_PLAT=mac\n");
1871 #elif defined(TARGET_NETBSD)
1872  buf_printf(&out, "IV_PLAT=netbsd\n");
1873 #elif defined(TARGET_FREEBSD)
1874  buf_printf(&out, "IV_PLAT=freebsd\n");
1875 #elif defined(TARGET_ANDROID)
1876  buf_printf(&out, "IV_PLAT=android\n");
1877 #elif defined(_WIN32)
1878  buf_printf(&out, "IV_PLAT=win\n");
1879 #endif
1880  /* Announce that we do not require strict sequence numbers with
1881  * TCP. (TCP non-linear) */
1882  buf_printf(&out, "IV_TCPNL=1\n");
1883  }
1884 
1885  /* These are the IV variable that are sent to peers in p2p mode */
1886  if (session->opt->push_peer_info_detail > 0)
1887  {
1888  /* support for P_DATA_V2 */
1889  int iv_proto = IV_PROTO_DATA_V2;
1890 
1891  /* support for the --dns option */
1892  iv_proto |= IV_PROTO_DNS_OPTION;
1893 
1894  /* support for exit notify via control channel */
1895  iv_proto |= IV_PROTO_CC_EXIT_NOTIFY;
1896 
1897  if (session->opt->pull)
1898  {
1899  /* support for receiving push_reply before sending
1900  * push request, also signal that the client wants
1901  * to get push-reply messages without requiring a round
1902  * trip for a push request message*/
1903  iv_proto |= IV_PROTO_REQUEST_PUSH;
1904 
1905  /* Support keywords in the AUTH_PENDING control message */
1906  iv_proto |= IV_PROTO_AUTH_PENDING_KW;
1907 
1908  /* support for AUTH_FAIL,TEMP control message */
1909  iv_proto |= IV_PROTO_AUTH_FAIL_TEMP;
1910 
1911  /* support for tun-mtu as part of the push message */
1912  buf_printf(&out, "IV_MTU=%d\n", session->opt->frame.tun_max_mtu);
1913  }
1914 
1915  /* support for Negotiable Crypto Parameters */
1916  if (session->opt->mode == MODE_SERVER || session->opt->pull)
1917  {
1918  if (tls_item_in_cipher_list("AES-128-GCM", session->opt->config_ncp_ciphers)
1919  && tls_item_in_cipher_list("AES-256-GCM", session->opt->config_ncp_ciphers))
1920  {
1921 
1922  buf_printf(&out, "IV_NCP=2\n");
1923  }
1924  }
1925  else
1926  {
1927  /* We are not using pull or p2mp server, instead do P2P NCP */
1928  iv_proto |= IV_PROTO_NCP_P2P;
1929  }
1930 
1931  buf_printf(&out, "IV_CIPHERS=%s\n", session->opt->config_ncp_ciphers);
1932 
1933 #ifdef HAVE_EXPORT_KEYING_MATERIAL
1934  iv_proto |= IV_PROTO_TLS_KEY_EXPORT;
1935  iv_proto |= IV_PROTO_DYN_TLS_CRYPT;
1936 #endif
1937 
1938  buf_printf(&out, "IV_PROTO=%d\n", iv_proto);
1939 
1940  if (session->opt->push_peer_info_detail > 1)
1941  {
1942  /* push compression status */
1943 #ifdef USE_COMP
1944  comp_generate_peer_info_string(&session->opt->comp_options, &out);
1945 #endif
1946  }
1947 
1948  if (session->opt->push_peer_info_detail > 2)
1949  {
1950  /* push mac addr */
1951  struct route_gateway_info rgi;
1952  get_default_gateway(&rgi, session->opt->net_ctx);
1953  if (rgi.flags & RGI_HWADDR_DEFINED)
1954  {
1955  buf_printf(&out, "IV_HWADDR=%s\n", format_hex_ex(rgi.hwaddr, 6, 0, 1, ":", &gc));
1956  }
1957  buf_printf(&out, "IV_SSL=%s\n", get_ssl_library_version() );
1958 #if defined(_WIN32)
1959  buf_printf(&out, "IV_PLAT_VER=%s\n", win32_version_string(&gc, false));
1960 #endif
1961  }
1962 
1963  if (session->opt->push_peer_info_detail > 1)
1964  {
1965  struct env_set *es = session->opt->es;
1966  /* push env vars that begin with UV_, IV_PLAT_VER and IV_GUI_VER */
1967  for (struct env_item *e = es->list; e != NULL; e = e->next)
1968  {
1969  if (e->string)
1970  {
1971  if ((((strncmp(e->string, "UV_", 3) == 0
1972  || strncmp(e->string, "IV_PLAT_VER=", sizeof("IV_PLAT_VER=") - 1) == 0)
1973  && session->opt->push_peer_info_detail > 2)
1974  || (strncmp(e->string, "IV_GUI_VER=", sizeof("IV_GUI_VER=") - 1) == 0)
1975  || (strncmp(e->string, "IV_SSO=", sizeof("IV_SSO=") - 1) == 0)
1976  )
1977  && buf_safe(&out, strlen(e->string) + 1))
1978  {
1979  buf_printf(&out, "%s\n", e->string);
1980  }
1981  }
1982  }
1983  }
1984 
1985  if (!write_string(buf, BSTR(&out), -1))
1986  {
1987  goto error;
1988  }
1989  }
1990  else
1991  {
1992  if (!write_empty_string(buf)) /* no peer info */
1993  {
1994  goto error;
1995  }
1996  }
1997  ret = true;
1998 
1999 error:
2000  gc_free(&gc);
2001  return ret;
2002 }
2003 
2004 #ifdef USE_COMP
2005 static bool
2006 write_compat_local_options(struct buffer *buf, const char *options)
2007 {
2008  struct gc_arena gc = gc_new();
2009  const char *local_options = options_string_compat_lzo(options, &gc);
2010  bool ret = write_string(buf, local_options, TLS_OPTIONS_LEN);
2011  gc_free(&gc);
2012  return ret;
2013 }
2014 #endif
2015 
2020 static bool
2021 key_method_2_write(struct buffer *buf, struct tls_multi *multi, struct tls_session *session)
2022 {
2023  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2024 
2025  ASSERT(buf_init(buf, 0));
2026 
2027  /* write a uint32 0 */
2028  if (!buf_write_u32(buf, 0))
2029  {
2030  goto error;
2031  }
2032 
2033  /* write key_method + flags */
2034  if (!buf_write_u8(buf, KEY_METHOD_2))
2035  {
2036  goto error;
2037  }
2038 
2039  /* write key source material */
2040  if (!key_source2_randomize_write(ks->key_src, buf, session->opt->server))
2041  {
2042  goto error;
2043  }
2044 
2045  /* write options string */
2046  {
2047 #ifdef USE_COMP
2048  if (multi->remote_usescomp && session->opt->mode == MODE_SERVER
2049  && multi->opt.comp_options.flags & COMP_F_MIGRATE)
2050  {
2051  if (!write_compat_local_options(buf, session->opt->local_options))
2052  {
2053  goto error;
2054  }
2055  }
2056  else
2057 #endif
2058  if (!write_string(buf, session->opt->local_options, TLS_OPTIONS_LEN))
2059  {
2060  goto error;
2061  }
2062  }
2063 
2064  /* write username/password if specified or we are using a auth-token */
2066  {
2067 #ifdef ENABLE_MANAGEMENT
2068  auth_user_pass_setup(session->opt->auth_user_pass_file,
2069  session->opt->auth_user_pass_file_inline,
2070  session->opt->sci);
2071 #else
2072  auth_user_pass_setup(session->opt->auth_user_pass_file,
2073  session->opt->auth_user_pass_file_inline, NULL);
2074 #endif
2075  struct user_pass *up = &auth_user_pass;
2076 
2077  /*
2078  * If we have a valid auth-token, send that instead of real
2079  * username/password
2080  */
2082  {
2083  up = &auth_token;
2084  }
2085 
2086  if (!write_string(buf, up->username, -1))
2087  {
2088  goto error;
2089  }
2090  else if (!write_string(buf, up->password, -1))
2091  {
2092  goto error;
2093  }
2094  /* save username for auth-token which may get pushed later */
2095  if (session->opt->pull && up != &auth_token)
2096  {
2098  }
2099  /* respect auth-nocache */
2101  }
2102  else
2103  {
2104  if (!write_empty_string(buf)) /* no username */
2105  {
2106  goto error;
2107  }
2108  if (!write_empty_string(buf)) /* no password */
2109  {
2110  goto error;
2111  }
2112  }
2113 
2114  if (!push_peer_info(buf, session))
2115  {
2116  goto error;
2117  }
2118 
2119  if (session->opt->server && session->opt->mode != MODE_SERVER
2120  && ks->key_id == 0)
2121  {
2122  /* tls-server option set and not P2MP server, so we
2123  * are a P2P client running in tls-server mode */
2124  p2p_mode_ncp(multi, session);
2125  }
2126 
2127  return true;
2128 
2129 error:
2130  msg(D_TLS_ERRORS, "TLS Error: Key Method #2 write failed");
2131  secure_memzero(ks->key_src, sizeof(*ks->key_src));
2132  return false;
2133 }
2134 
2135 static void
2137  struct tls_session *session)
2138 {
2139  if (session->opt->ekm_size > 0)
2140  {
2141  unsigned int size = session->opt->ekm_size;
2142  struct gc_arena gc = gc_new();
2143 
2144  unsigned char *ekm = gc_malloc(session->opt->ekm_size, true, &gc);
2146  session->opt->ekm_label,
2147  session->opt->ekm_label_size,
2148  ekm, session->opt->ekm_size))
2149  {
2150  unsigned int len = (size * 2) + 2;
2151 
2152  const char *key = format_hex_ex(ekm, size, len, 0, NULL, &gc);
2153  setenv_str(session->opt->es, "exported_keying_material", key);
2154 
2155  dmsg(D_TLS_DEBUG_MED, "%s: exported keying material: %s",
2156  __func__, key);
2157  secure_memzero(ekm, size);
2158  }
2159  else
2160  {
2161  msg(M_WARN, "WARNING: Export keying material failed!");
2162  setenv_del(session->opt->es, "exported_keying_material");
2163  }
2164  gc_free(&gc);
2165  }
2166 }
2167 
2172 static bool
2173 key_method_2_read(struct buffer *buf, struct tls_multi *multi, struct tls_session *session)
2174 {
2175  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2176 
2177  bool username_status, password_status;
2178 
2179  struct gc_arena gc = gc_new();
2180  char *options;
2181  struct user_pass *up = NULL;
2182 
2183  /* allocate temporary objects */
2185 
2186  /* discard leading uint32 */
2187  if (!buf_advance(buf, 4))
2188  {
2189  msg(D_TLS_ERRORS, "TLS ERROR: Plaintext buffer too short (%d bytes).",
2190  buf->len);
2191  goto error;
2192  }
2193 
2194  /* get key method */
2195  int key_method_flags = buf_read_u8(buf);
2196  if ((key_method_flags & KEY_METHOD_MASK) != 2)
2197  {
2198  msg(D_TLS_ERRORS,
2199  "TLS ERROR: Unknown key_method/flags=%d received from remote host",
2200  key_method_flags);
2201  goto error;
2202  }
2203 
2204  /* get key source material (not actual keys yet) */
2205  if (!key_source2_read(ks->key_src, buf, session->opt->server))
2206  {
2207  msg(D_TLS_ERRORS, "TLS Error: Error reading remote data channel key source entropy from plaintext buffer");
2208  goto error;
2209  }
2210 
2211  /* get options */
2212  if (!read_string(buf, options, TLS_OPTIONS_LEN))
2213  {
2214  msg(D_TLS_ERRORS, "TLS Error: Failed to read required OCC options string");
2215  goto error;
2216  }
2217 
2219 
2220  /* always extract username + password fields from buf, even if not
2221  * authenticating for it, because otherwise we can't get at the
2222  * peer_info data which follows behind
2223  */
2224  ALLOC_OBJ_CLEAR_GC(up, struct user_pass, &gc);
2225  username_status = read_string(buf, up->username, USER_PASS_LEN);
2226  password_status = read_string(buf, up->password, USER_PASS_LEN);
2227 
2228  /* get peer info from control channel */
2229  free(multi->peer_info);
2230  multi->peer_info = read_string_alloc(buf);
2231  if (multi->peer_info)
2232  {
2233  output_peer_info_env(session->opt->es, multi->peer_info);
2234  }
2235 
2236  free(multi->remote_ciphername);
2237  multi->remote_ciphername =
2238  options_string_extract_option(options, "cipher", NULL);
2239  multi->remote_usescomp = strstr(options, ",comp-lzo,");
2240 
2241  /* In OCC we send '[null-cipher]' instead 'none' */
2242  if (multi->remote_ciphername
2243  && strcmp(multi->remote_ciphername, "[null-cipher]") == 0)
2244  {
2245  free(multi->remote_ciphername);
2246  multi->remote_ciphername = string_alloc("none", NULL);
2247  }
2248 
2250  {
2251  /* Perform username/password authentication */
2252  if (!username_status || !password_status)
2253  {
2254  CLEAR(*up);
2255  if (!(session->opt->ssl_flags & SSLF_AUTH_USER_PASS_OPTIONAL))
2256  {
2257  msg(D_TLS_ERRORS, "TLS Error: Auth Username/Password was not provided by peer");
2258  goto error;
2259  }
2260  }
2261 
2262  verify_user_pass(up, multi, session);
2263  }
2264  else
2265  {
2266  /* Session verification should have occurred during TLS negotiation*/
2267  if (!session->verified)
2268  {
2269  msg(D_TLS_ERRORS,
2270  "TLS Error: Certificate verification failed (key-method 2)");
2271  goto error;
2272  }
2274  }
2275 
2276  /* clear username and password from memory */
2277  secure_memzero(up, sizeof(*up));
2278 
2279  /* Perform final authentication checks */
2280  if (ks->authenticated > KS_AUTH_FALSE)
2281  {
2283  }
2284 
2285  /* check options consistency */
2286  if (!options_cmp_equal(options, session->opt->remote_options))
2287  {
2288  const char *remote_options = session->opt->remote_options;
2289 #ifdef USE_COMP
2290  if (multi->opt.comp_options.flags & COMP_F_MIGRATE && multi->remote_usescomp)
2291  {
2292  msg(D_PUSH, "Note: 'compress migrate' detected remote peer "
2293  "with compression enabled.");
2294  remote_options = options_string_compat_lzo(remote_options, &gc);
2295  }
2296 #endif
2297 
2298  options_warning(options, remote_options);
2299 
2300  if (session->opt->ssl_flags & SSLF_OPT_VERIFY)
2301  {
2302  msg(D_TLS_ERRORS, "Option inconsistency warnings triggering disconnect due to --opt-verify");
2304  }
2305  }
2306 
2307  buf_clear(buf);
2308 
2309  /*
2310  * Call OPENVPN_PLUGIN_TLS_FINAL plugin if defined, for final
2311  * veto opportunity over authentication decision.
2312  */
2313  if ((ks->authenticated > KS_AUTH_FALSE)
2314  && plugin_defined(session->opt->plugins, OPENVPN_PLUGIN_TLS_FINAL))
2315  {
2317 
2318  if (plugin_call(session->opt->plugins, OPENVPN_PLUGIN_TLS_FINAL, NULL, NULL, session->opt->es) != OPENVPN_PLUGIN_FUNC_SUCCESS)
2319  {
2321  }
2322 
2323  setenv_del(session->opt->es, "exported_keying_material");
2324  }
2325 
2326  if (!session->opt->server && !session->opt->pull && ks->key_id == 0)
2327  {
2328  /* We are a p2p tls-client without pull, enable common
2329  * protocol options */
2330  p2p_mode_ncp(multi, session);
2331  }
2332 
2333  gc_free(&gc);
2334  return true;
2335 
2336 error:
2338  secure_memzero(ks->key_src, sizeof(*ks->key_src));
2339  if (up)
2340  {
2341  secure_memzero(up, sizeof(*up));
2342  }
2343  buf_clear(buf);
2344  gc_free(&gc);
2345  return false;
2346 }
2347 
2348 static int
2350 {
2351  int ret = o->handshake_window;
2352  const int r2 = o->renegotiate_seconds / 2;
2353 
2354  if (o->renegotiate_seconds && r2 < ret)
2355  {
2356  ret = r2;
2357  }
2358  return ret;
2359 }
2360 
2367 static bool
2369  struct key_state *ks, bool skip_initial_send)
2370 {
2372  if (!buf)
2373  {
2374  return false;
2375  }
2376 
2377  ks->initial = now;
2378  ks->must_negotiate = now + session->opt->handshake_window;
2380 
2381  /* null buffer */
2383 
2384  /* If we want to skip sending the initial handshake packet we still generate
2385  * it to increase internal counters etc. but immediately mark it as done */
2386  if (skip_initial_send)
2387  {
2389  }
2391 
2392  ks->state = S_PRE_START;
2393 
2394  struct gc_arena gc = gc_new();
2395  dmsg(D_TLS_DEBUG, "TLS: Initial Handshake, sid=%s",
2396  session_id_print(&session->session_id, &gc));
2397  gc_free(&gc);
2398 
2399 #ifdef ENABLE_MANAGEMENT
2401  {
2404  NULL,
2405  NULL,
2406  NULL,
2407  NULL,
2408  NULL);
2409  }
2410 #endif
2411  return true;
2412 
2413 }
2414 
2419 static void
2421  struct link_socket_info *to_link_socket_info,
2422  struct key_state *ks)
2423 {
2424  dmsg(D_TLS_DEBUG_MED, "STATE S_ACTIVE");
2425 
2426  ks->established = now;
2428  {
2429  print_details(&ks->ks_ssl, "Control Channel:");
2430  }
2431  ks->state = S_ACTIVE;
2432  /* Cancel negotiation timeout */
2433  ks->must_negotiate = 0;
2434  INCR_SUCCESS;
2435 
2436  /* Set outgoing address for data channel packets */
2437  link_socket_set_outgoing_addr(to_link_socket_info, &ks->remote_addr,
2438  session->common_name, session->opt->es);
2439 
2440  /* Check if we need to advance the tls_multi state machine */
2441  if (multi->multi_state == CAS_NOT_CONNECTED)
2442  {
2443  if (session->opt->mode == MODE_SERVER)
2444  {
2445  /* On a server we continue with running connect scripts next */
2446  multi->multi_state = CAS_WAITING_AUTH;
2447  }
2448  else
2449  {
2450  /* Skip the connect script related states */
2452  }
2453  }
2454 
2455  /* Flush any payload packets that were buffered before our state transitioned to S_ACTIVE */
2457 
2458 #ifdef MEASURE_TLS_HANDSHAKE_STATS
2459  show_tls_performance_stats();
2460 #endif
2461 }
2462 
2463 bool
2465  struct tls_pre_decrypt_state *state,
2466  struct link_socket_actual *from)
2467 {
2468  struct key_state *ks = &session->key[KS_PRIMARY];
2469  ks->session_id_remote = state->peer_session_id;
2470  ks->remote_addr = *from;
2471  session->session_id = state->server_session_id;
2472  session->untrusted_addr = *from;
2473  session->burst = true;
2474 
2475  /* The OpenVPN protocol implicitly mandates that packet id always start
2476  * from 0 in the RESET packets as OpenVPN 2.x will not allow gaps in the
2477  * ids and starts always from 0. Since we skip/ignore one (RESET) packet
2478  * in each direction, we need to set the ids to 1 */
2479  ks->rec_reliable->packet_id = 1;
2480  /* for ks->send_reliable->packet_id, session_move_pre_start moves the
2481  * counter to 1 */
2482  session->tls_wrap.opt.packet_id.send.id = 1;
2483  return session_move_pre_start(session, ks, true);
2484 }
2485 
2489 static bool
2491 {
2492  while (buf->len > 0)
2493  {
2494  if (buf_len(buf) < 4)
2495  {
2496  goto error;
2497  }
2498  /* read type */
2499  uint16_t type = buf_read_u16(buf);
2500  uint16_t len = buf_read_u16(buf);
2501  if (buf_len(buf) < len)
2502  {
2503  goto error;
2504  }
2505 
2506  switch (type)
2507  {
2509  if (len != sizeof(uint16_t))
2510  {
2511  goto error;
2512  }
2513  uint16_t flags = buf_read_u16(buf);
2514 
2515  if (flags & EARLY_NEG_FLAG_RESEND_WKC)
2516  {
2518  }
2519  break;
2520 
2521  default:
2522  /* Skip types we do not parse */
2523  buf_advance(buf, len);
2524  }
2525  }
2527 
2528  return true;
2529 error:
2530  msg(D_TLS_ERRORS, "TLS Error: Early negotiation malformed packet");
2531  return false;
2532 }
2533 
2538 static bool
2540  bool *continue_tls_process)
2541 {
2542  int status = 0;
2543  if (buf->len)
2544  {
2546  if (status == -1)
2547  {
2548  msg(D_TLS_ERRORS,
2549  "TLS Error: Incoming Ciphertext -> TLS object write error");
2550  return false;
2551  }
2552  }
2553  else
2554  {
2555  status = 1;
2556  }
2557  if (status == 1)
2558  {
2560  *continue_tls_process = true;
2561  dmsg(D_TLS_DEBUG, "Incoming Ciphertext -> TLS");
2562  }
2563  return true;
2564 }
2565 
2566 static bool
2568 {
2569  return (ks->crypto_options.flags & CO_RESEND_WKC)
2570  && (ks->send_reliable->packet_id == 1);
2571 }
2572 
2573 
2574 static bool
2576  interval_t *wakeup, bool *continue_tls_process)
2577 {
2578  ASSERT(buf_init(buf, 0));
2579 
2580  int status = key_state_read_plaintext(&ks->ks_ssl, buf);
2581 
2582  update_time();
2583  if (status == -1)
2584  {
2585  msg(D_TLS_ERRORS, "TLS Error: TLS object -> incoming plaintext read error");
2586  return false;
2587  }
2588  if (status == 1)
2589  {
2590  *continue_tls_process = true;
2591  dmsg(D_TLS_DEBUG, "TLS -> Incoming Plaintext");
2592 
2593  /* More data may be available, wake up again asap to check. */
2594  *wakeup = 0;
2595  }
2596  return true;
2597 }
2598 
2599 static bool
2600 write_outgoing_tls_ciphertext(struct tls_session *session, bool *continue_tls_process)
2601 {
2602  struct key_state *ks = &session->key[KS_PRIMARY];
2603 
2605  if (rel_avail == 0)
2606  {
2607  return true;
2608  }
2609 
2610  /* We need to determine how much space is actually available in the control
2611  * channel frame */
2612  int max_pkt_len = min_int(TLS_CHANNEL_BUF_SIZE, session->opt->frame.tun_mtu);
2613 
2614  /* Subtract overhead */
2616 
2617  /* calculate total available length for outgoing tls ciphertext */
2618  int maxlen = max_pkt_len * rel_avail;
2619 
2620  /* Is first packet one that will have a WKC appended? */
2621  if (control_packet_needs_wkc(ks))
2622  {
2623  maxlen -= buf_len(session->tls_wrap.tls_crypt_v2_wkc);
2624  }
2625 
2626  /* If we end up with a size that leaves no room for payload, ignore the
2627  * constraints to still be to send a packet. This might have gone negative
2628  * if we have a large wrapped client key. */
2629  if (maxlen < 16)
2630  {
2631  msg(D_TLS_ERRORS, "Warning: --max-packet-size (%d) setting too low. "
2632  "Sending minimum sized packet.",
2633  session->opt->frame.tun_mtu);
2634  maxlen = 16;
2635  /* We set the maximum length here to ensure a packet with a wrapped
2636  * key can actually carry the 16 byte of payload */
2637  max_pkt_len = TLS_CHANNEL_BUF_SIZE;
2638  }
2639 
2640  /* This seems a bit wasteful to allocate every time */
2641  struct gc_arena gc = gc_new();
2642  struct buffer tmp = alloc_buf_gc(maxlen, &gc);
2643 
2644  int status = key_state_read_ciphertext(&ks->ks_ssl, &tmp);
2645 
2646  if (status == -1)
2647  {
2648  msg(D_TLS_ERRORS,
2649  "TLS Error: Ciphertext -> reliable TCP/UDP transport read error");
2650  gc_free(&gc);
2651  return false;
2652  }
2653  if (status == 1)
2654  {
2655  /* Split the TLS ciphertext (TLS record) into multiple small packets
2656  * that respect tls_mtu */
2657  while (tmp.len > 0)
2658  {
2659  int len = max_pkt_len;
2660  int opcode = P_CONTROL_V1;
2661  if (control_packet_needs_wkc(ks))
2662  {
2663  opcode = P_CONTROL_WKC_V1;
2664  len = max_int(0, len - buf_len(session->tls_wrap.tls_crypt_v2_wkc));
2665  }
2666  /* do not send more than available */
2667  len = min_int(len, tmp.len);
2668 
2670  /* we assert here since we checked for its availability before */
2671  ASSERT(buf);
2672  buf_copy_n(buf, &tmp, len);
2673 
2674  reliable_mark_active_outgoing(ks->send_reliable, buf, opcode);
2676  *continue_tls_process = true;
2677  }
2678  dmsg(D_TLS_DEBUG, "Outgoing Ciphertext -> Reliable");
2679  }
2680 
2681  gc_free(&gc);
2682  return true;
2683 }
2684 
2685 static bool
2687  struct tls_session *session,
2688  struct buffer *to_link,
2689  struct link_socket_actual **to_link_addr,
2690  struct link_socket_info *to_link_socket_info,
2691  interval_t *wakeup)
2692 {
2693  /* This variable indicates if we should call this method
2694  * again to process more incoming/outgoing TLS state/data
2695  * We want to repeat this until we either determined that there
2696  * is nothing more to process or that further processing
2697  * should only be done after the outer loop (sending packets etc.)
2698  * has run once more */
2699  bool continue_tls_process = false;
2700  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2701 
2702  /* Initial handshake */
2703  if (ks->state == S_INITIAL)
2704  {
2705  continue_tls_process = session_move_pre_start(session, ks, false);
2706  }
2707 
2708  /* Are we timed out on receive? */
2709  if (now >= ks->must_negotiate && ks->state < S_ACTIVE)
2710  {
2711  msg(D_TLS_ERRORS,
2712  "TLS Error: TLS key negotiation failed to occur within %d seconds (check your network connectivity)",
2713  session->opt->handshake_window);
2714  goto error;
2715  }
2716 
2717  /* Check if the initial three-way Handshake is complete.
2718  * We consider the handshake to be complete when our own initial
2719  * packet has been successfully ACKed. */
2720  if (ks->state == S_PRE_START && reliable_empty(ks->send_reliable))
2721  {
2722  ks->state = S_START;
2723  continue_tls_process = true;
2724 
2725  /* New connection, remove any old X509 env variables */
2726  tls_x509_clear_env(session->opt->es);
2727  dmsg(D_TLS_DEBUG_MED, "STATE S_START");
2728  }
2729 
2730  /* Wait for ACK */
2731  if (((ks->state == S_GOT_KEY && !session->opt->server)
2732  || (ks->state == S_SENT_KEY && session->opt->server))
2733  && reliable_empty(ks->send_reliable))
2734  {
2735  session_move_active(multi, session, to_link_socket_info, ks);
2736  continue_tls_process = true;
2737  }
2738 
2739  /* Reliable buffer to outgoing TCP/UDP (send up to CONTROL_SEND_ACK_MAX ACKs
2740  * for previously received packets) */
2741  if (!to_link->len && reliable_can_send(ks->send_reliable))
2742  {
2743  int opcode;
2744 
2745  struct buffer *buf = reliable_send(ks->send_reliable, &opcode);
2746  ASSERT(buf);
2747  struct buffer b = *buf;
2748  INCR_SENT;
2749 
2750  write_control_auth(session, ks, &b, to_link_addr, opcode,
2751  CONTROL_SEND_ACK_MAX, true);
2752  *to_link = b;
2753  dmsg(D_TLS_DEBUG, "Reliable -> TCP/UDP");
2754 
2755  /* This changed the state of the outgoing buffer. In order to avoid
2756  * running this function again/further and invalidating the key_state
2757  * buffer and accessing the buffer that is now in to_link after it being
2758  * freed for a potential error, we shortcircuit exiting of the outer
2759  * process here. */
2760  return false;
2761  }
2762 
2763  /* Write incoming ciphertext to TLS object */
2765  if (entry)
2766  {
2767  /* The first packet from the peer (the reset packet) is special and
2768  * contains early protocol negotiation */
2769  if (entry->packet_id == 0 && is_hard_reset_method2(entry->opcode))
2770  {
2771  if (!parse_early_negotiation_tlvs(&entry->buf, ks))
2772  {
2773  goto error;
2774  }
2775  }
2776  else
2777  {
2778  if (!read_incoming_tls_ciphertext(&entry->buf, ks, &continue_tls_process))
2779  {
2780  goto error;
2781  }
2782  }
2783  }
2784 
2785  /* Read incoming plaintext from TLS object */
2786  struct buffer *buf = &ks->plaintext_read_buf;
2787  if (!buf->len)
2788  {
2789  if (!read_incoming_tls_plaintext(ks, buf, wakeup, &continue_tls_process))
2790  {
2791  goto error;
2792  }
2793  }
2794 
2795  /* Send Key */
2796  buf = &ks->plaintext_write_buf;
2797  if (!buf->len && ((ks->state == S_START && !session->opt->server)
2798  || (ks->state == S_GOT_KEY && session->opt->server)))
2799  {
2800  if (!key_method_2_write(buf, multi, session))
2801  {
2802  goto error;
2803  }
2804 
2805  continue_tls_process = true;
2806  dmsg(D_TLS_DEBUG_MED, "STATE S_SENT_KEY");
2807  ks->state = S_SENT_KEY;
2808  }
2809 
2810  /* Receive Key */
2811  buf = &ks->plaintext_read_buf;
2812  if (buf->len
2813  && ((ks->state == S_SENT_KEY && !session->opt->server)
2814  || (ks->state == S_START && session->opt->server)))
2815  {
2816  if (!key_method_2_read(buf, multi, session))
2817  {
2818  goto error;
2819  }
2820 
2821  continue_tls_process = true;
2822  dmsg(D_TLS_DEBUG_MED, "STATE S_GOT_KEY");
2823  ks->state = S_GOT_KEY;
2824  }
2825 
2826  /* Write outgoing plaintext to TLS object */
2827  buf = &ks->plaintext_write_buf;
2828  if (buf->len)
2829  {
2830  int status = key_state_write_plaintext(&ks->ks_ssl, buf);
2831  if (status == -1)
2832  {
2833  msg(D_TLS_ERRORS,
2834  "TLS ERROR: Outgoing Plaintext -> TLS object write error");
2835  goto error;
2836  }
2837  if (status == 1)
2838  {
2839  continue_tls_process = true;
2840  dmsg(D_TLS_DEBUG, "Outgoing Plaintext -> TLS");
2841  }
2842  }
2843 
2844  /* Outgoing Ciphertext to reliable buffer */
2845  if (ks->state >= S_START)
2846  {
2848  if (buf)
2849  {
2850  if (!write_outgoing_tls_ciphertext(session, &continue_tls_process))
2851  {
2852  goto error;
2853  }
2854  }
2855  }
2856 
2857  return continue_tls_process;
2858 error:
2859  tls_clear_error();
2860  ks->state = S_ERROR;
2861  msg(D_TLS_ERRORS, "TLS Error: TLS handshake failed");
2862  INCR_ERROR;
2863  return false;
2864 
2865 }
2866 /*
2867  * This is the primary routine for processing TLS stuff inside the
2868  * the main event loop. When this routine exits
2869  * with non-error status, it will set *wakeup to the number of seconds
2870  * when it wants to be called again.
2871  *
2872  * Return value is true if we have placed a packet in *to_link which we
2873  * want to send to our peer.
2874  */
2875 static bool
2876 tls_process(struct tls_multi *multi,
2877  struct tls_session *session,
2878  struct buffer *to_link,
2879  struct link_socket_actual **to_link_addr,
2880  struct link_socket_info *to_link_socket_info,
2881  interval_t *wakeup)
2882 {
2883  struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */
2884  struct key_state *ks_lame = &session->key[KS_LAME_DUCK]; /* retiring key */
2885 
2886  /* Make sure we were initialized and that we're not in an error state */
2887  ASSERT(ks->state != S_UNDEF);
2888  ASSERT(ks->state != S_ERROR);
2889  ASSERT(session_id_defined(&session->session_id));
2890 
2891  /* Should we trigger a soft reset? -- new key, keeps old key for a while */
2892  if (ks->state >= S_GENERATED_KEYS
2893  && ((session->opt->renegotiate_seconds
2894  && now >= ks->established + session->opt->renegotiate_seconds)
2895  || (session->opt->renegotiate_bytes > 0
2896  && ks->n_bytes >= session->opt->renegotiate_bytes)
2897  || (session->opt->renegotiate_packets
2898  && ks->n_packets >= session->opt->renegotiate_packets)
2900  {
2901  msg(D_TLS_DEBUG_LOW, "TLS: soft reset sec=%d/%d bytes=" counter_format
2902  "/%d pkts=" counter_format "/%d",
2903  (int) (now - ks->established), session->opt->renegotiate_seconds,
2904  ks->n_bytes, session->opt->renegotiate_bytes,
2905  ks->n_packets, session->opt->renegotiate_packets);
2907  }
2908 
2909  /* Kill lame duck key transition_window seconds after primary key negotiation */
2910  if (lame_duck_must_die(session, wakeup))
2911  {
2912  key_state_free(ks_lame, true);
2913  msg(D_TLS_DEBUG_LOW, "TLS: tls_process: killed expiring key");
2914  }
2915 
2916  bool continue_tls_process = true;
2917  while (continue_tls_process)
2918  {
2919  update_time();
2920 
2921  dmsg(D_TLS_DEBUG, "TLS: tls_process: chg=%d ks=%s lame=%s to_link->len=%d wakeup=%d",
2922  continue_tls_process,
2923  state_name(ks->state),
2924  state_name(ks_lame->state),
2925  to_link->len,
2926  *wakeup);
2927  continue_tls_process = tls_process_state(multi, session, to_link, to_link_addr,
2928  to_link_socket_info, wakeup);
2929 
2930  if (ks->state == S_ERROR)
2931  {
2932  return false;
2933  }
2934 
2935  }
2936 
2937  update_time();
2938 
2939  /* We often send acks back to back to a following control packet. This
2940  * normally does not create a problem (apart from an extra packet).
2941  * However, with the P_CONTROL_WKC_V1 we need to ensure that the packet
2942  * gets resent if not received by remote, so instead we use an empty
2943  * control packet in this special case */
2944 
2945  /* Send 1 or more ACKs (each received control packet gets one ACK) */
2946  if (!to_link->len && !reliable_ack_empty(ks->rec_ack))
2947  {
2948  if (control_packet_needs_wkc(ks))
2949  {
2951  if (!buf)
2952  {
2953  return false;
2954  }
2955 
2956  /* We do not write anything to the buffer, this way this will be
2957  * an empty control packet that gets the ack piggybacked and
2958  * also appended the wrapped client key since it has a WCK opcode */
2960  }
2961  else
2962  {
2963  struct buffer buf = ks->ack_write_buf;
2964  ASSERT(buf_init(&buf, multi->opt.frame.buf.headroom));
2965  write_control_auth(session, ks, &buf, to_link_addr, P_ACK_V1,
2966  RELIABLE_ACK_SIZE, false);
2967  *to_link = buf;
2968  dmsg(D_TLS_DEBUG, "Dedicated ACK -> TCP/UDP");
2969  }
2970  }
2971 
2972  /* When should we wake up again? */
2973  if (ks->state >= S_INITIAL)
2974  {
2975  compute_earliest_wakeup(wakeup,
2977 
2978  if (ks->must_negotiate)
2979  {
2981  }
2982  }
2983 
2984  if (ks->established && session->opt->renegotiate_seconds)
2985  {
2986  compute_earliest_wakeup(wakeup,
2987  ks->established + session->opt->renegotiate_seconds - now);
2988  }
2989 
2990  dmsg(D_TLS_DEBUG, "TLS: tls_process: timeout set to %d", *wakeup);
2991 
2992  /* prevent event-loop spinning by setting minimum wakeup of 1 second */
2993  if (*wakeup <= 0)
2994  {
2995  *wakeup = 1;
2996 
2997  /* if we had something to send to remote, but to_link was busy,
2998  * let caller know we need to be called again soon */
2999  return true;
3000  }
3001 
3002  /* If any of the state changes resulted in the to_link buffer being
3003  * set, we are also active */
3004  if (to_link->len)
3005  {
3006  return true;
3007  }
3008 
3009  return false;
3010 }
3011 
3012 
3020 static void
3022 {
3023  uint8_t *dataptr = to_link->data;
3024  if (!dataptr)
3025  {
3026  return;
3027  }
3028 
3029  /* Checks buffers in tls_wrap */
3030  if (session->tls_wrap.work.data == dataptr)
3031  {
3032  msg(M_INFO, "Warning buffer of freed TLS session is "
3033  "still in use (tls_wrap.work.data)");
3034  goto used;
3035  }
3036 
3037  for (int i = 0; i < KS_SIZE; i++)
3038  {
3039  struct key_state *ks = &session->key[i];
3040  if (ks->state == S_UNDEF)
3041  {
3042  continue;
3043  }
3044 
3045  /* we don't expect send_reliable to be NULL when state is
3046  * not S_UNDEF, but people have reported crashes nonetheless,
3047  * therefore we better catch this event, report and exit.
3048  */
3049  if (!ks->send_reliable)
3050  {
3051  msg(M_FATAL, "ERROR: session->key[%d]->send_reliable is NULL "
3052  "while key state is %s. Exiting.",
3053  i, state_name(ks->state));
3054  }
3055 
3056  for (int j = 0; j < ks->send_reliable->size; j++)
3057  {
3058  if (ks->send_reliable->array[j].buf.data == dataptr)
3059  {
3060  msg(M_INFO, "Warning buffer of freed TLS session is still in"
3061  " use (session->key[%d].send_reliable->array[%d])",
3062  i, j);
3063 
3064  goto used;
3065  }
3066  }
3067  }
3068  return;
3069 
3070 used:
3071  to_link->len = 0;
3072  to_link->data = 0;
3073  /* for debugging, you can add an ASSERT(0); here to trigger an abort */
3074 }
3075 /*
3076  * Called by the top-level event loop.
3077  *
3078  * Basically decides if we should call tls_process for
3079  * the active or untrusted sessions.
3080  */
3081 
3082 int
3084  struct buffer *to_link,
3085  struct link_socket_actual **to_link_addr,
3086  struct link_socket_info *to_link_socket_info,
3087  interval_t *wakeup)
3088 {
3089  struct gc_arena gc = gc_new();
3090  int active = TLSMP_INACTIVE;
3091  bool error = false;
3092 
3094 
3095  tls_clear_error();
3096 
3097  /*
3098  * Process each session object having state of S_INITIAL or greater,
3099  * and which has a defined remote IP addr.
3100  */
3101 
3102  for (int i = 0; i < TM_SIZE; ++i)
3103  {
3104  struct tls_session *session = &multi->session[i];
3105  struct key_state *ks = &session->key[KS_PRIMARY];
3106  struct key_state *ks_lame = &session->key[KS_LAME_DUCK];
3107 
3108  /* set initial remote address. This triggers connecting with that
3109  * session. So we only do that if the TM_ACTIVE session is not
3110  * established */
3111  if (i == TM_INITIAL && ks->state == S_INITIAL
3112  && get_primary_key(multi)->state <= S_INITIAL
3113  && link_socket_actual_defined(&to_link_socket_info->lsa->actual))
3114  {
3115  ks->remote_addr = to_link_socket_info->lsa->actual;
3116  }
3117 
3118  dmsg(D_TLS_DEBUG,
3119  "TLS: tls_multi_process: i=%d state=%s, mysid=%s, stored-sid=%s, stored-ip=%s",
3120  i,
3121  state_name(ks->state),
3122  session_id_print(&session->session_id, &gc),
3125 
3127  {
3128  struct link_socket_actual *tla = NULL;
3129 
3130  update_time();
3131 
3132  if (tls_process(multi, session, to_link, &tla,
3133  to_link_socket_info, wakeup))
3134  {
3135  active = TLSMP_ACTIVE;
3136  }
3137 
3138  /*
3139  * If tls_process produced an outgoing packet,
3140  * return the link_socket_actual object (which
3141  * contains the outgoing address).
3142  */
3143  if (tla)
3144  {
3145  multi->to_link_addr = *tla;
3146  *to_link_addr = &multi->to_link_addr;
3147  }
3148 
3149  /*
3150  * If tls_process hits an error:
3151  * (1) If the session has an unexpired lame duck key, preserve it.
3152  * (2) Reinitialize the session.
3153  * (3) Increment soft error count
3154  */
3155  if (ks->state == S_ERROR)
3156  {
3157  ++multi->n_soft_errors;
3158 
3159  if (i == TM_ACTIVE
3160  || (i == TM_INITIAL && get_primary_key(multi)->state < S_ACTIVE))
3161  {
3162  error = true;
3163  }
3164 
3165  if (i == TM_ACTIVE
3166  && ks_lame->state >= S_GENERATED_KEYS
3167  && !multi->opt.single_session)
3168  {
3169  move_session(multi, TM_LAME_DUCK, TM_ACTIVE, true);
3170  }
3171  else
3172  {
3174  reset_session(multi, session);
3175  }
3176  }
3177  }
3178  }
3179 
3180  update_time();
3181 
3182  enum tls_auth_status tas = tls_authentication_status(multi);
3183 
3184  /* If we have successfully authenticated and are still waiting for the authentication to finish
3185  * move the state machine for the multi context forward */
3186 
3187  if (multi->multi_state >= CAS_CONNECT_DONE)
3188  {
3189  /* Only generate keys for the TM_ACTIVE session. We defer generating
3190  * keys for TM_INITIAL until we actually trust it.
3191  * For TM_LAME_DUCK it makes no sense to generate new keys. */
3192  struct tls_session *session = &multi->session[TM_ACTIVE];
3193  struct key_state *ks = &session->key[KS_PRIMARY];
3194 
3195  if (ks->state == S_ACTIVE && ks->authenticated == KS_AUTH_TRUE)
3196  {
3197  /* Session is now fully authenticated.
3198  * tls_session_generate_data_channel_keys will move ks->state
3199  * from S_ACTIVE to S_GENERATED_KEYS */
3201  {
3202  msg(D_TLS_ERRORS, "TLS Error: generate_key_expansion failed");
3204  ks->state = S_ERROR;
3205  }
3206 
3207  /* Update auth token on the client if needed on renegotiation
3208  * (key id !=0) */
3209  if (session->key[KS_PRIMARY].key_id != 0)
3210  {
3212  }
3213  }
3214  }
3215 
3217  {
3218  multi->multi_state = CAS_PENDING;
3219  }
3220 
3221  /*
3222  * If lame duck session expires, kill it.
3223  */
3224  if (lame_duck_must_die(&multi->session[TM_LAME_DUCK], wakeup))
3225  {
3226  tls_session_free(&multi->session[TM_LAME_DUCK], true);
3227  msg(D_TLS_DEBUG_LOW, "TLS: tls_multi_process: killed expiring key");
3228  }
3229 
3230  /*
3231  * If untrusted session achieves TLS authentication,
3232  * move it to active session, usurping any prior session.
3233  *
3234  * A semi-trusted session is one in which the certificate authentication
3235  * succeeded (if cert verification is enabled) but the username/password
3236  * verification failed. A semi-trusted session can forward data on the
3237  * TLS control channel but not on the tunnel channel.
3238  */
3239  if (TLS_AUTHENTICATED(multi, &multi->session[TM_INITIAL].key[KS_PRIMARY]))
3240  {
3241  move_session(multi, TM_ACTIVE, TM_INITIAL, true);
3242  tas = tls_authentication_status(multi);
3243  msg(D_TLS_DEBUG_LOW, "TLS: tls_multi_process: initial untrusted "
3244  "session promoted to %strusted",
3245  tas == TLS_AUTHENTICATION_SUCCEEDED ? "" : "semi-");
3246 
3247  if (multi->multi_state == CAS_CONNECT_DONE)
3248  {
3250  active = TLSMP_RECONNECT;
3251  }
3252  }
3253 
3254  /*
3255  * A hard error means that TM_ACTIVE hit an S_ERROR state and that no
3256  * other key state objects are S_ACTIVE or higher.
3257  */
3258  if (error)
3259  {
3260  for (int i = 0; i < KEY_SCAN_SIZE; ++i)
3261  {
3262  if (get_key_scan(multi, i)->state >= S_ACTIVE)
3263  {
3264  goto nohard;
3265  }
3266  }
3267  ++multi->n_hard_errors;
3268  }
3269 nohard:
3270 
3271 #ifdef ENABLE_DEBUG
3272  /* DEBUGGING -- flood peer with repeating connection attempts */
3273  {
3274  const int throw_level = GREMLIN_CONNECTION_FLOOD_LEVEL(multi->opt.gremlin);
3275  if (throw_level)
3276  {
3277  for (int i = 0; i < KEY_SCAN_SIZE; ++i)
3278  {
3279  if (get_key_scan(multi, i)->state >= throw_level)
3280  {
3281  ++multi->n_hard_errors;
3282  ++multi->n_soft_errors;
3283  }
3284  }
3285  }
3286  }
3287 #endif
3288 
3289  perf_pop();
3290  gc_free(&gc);
3291 
3292  return (tas == TLS_AUTHENTICATION_FAILED) ? TLSMP_KILL : active;
3293 }
3294 
3299 static void
3301  const struct link_socket_actual *from, int key_id)
3302 {
3303  struct gc_arena gc = gc_new();
3304  const char *source = print_link_socket_actual(from, &gc);
3305 
3306 
3307  for (int i = 0; i < KEY_SCAN_SIZE; ++i)
3308  {
3309  struct key_state *ks = get_key_scan(multi, i);
3310  if (ks->key_id != key_id)
3311  {
3312  continue;
3313  }
3314 
3315  /* Our key state has been progressed far enough to be part of a valid
3316  * session but has not generated keys. */
3317  if (ks->state >= S_INITIAL && ks->state < S_GENERATED_KEYS)
3318  {
3320  "Key %s [%d] not initialized (yet), dropping packet.",
3321  source, key_id);
3322  gc_free(&gc);
3323  return;
3324  }
3325  if (ks->state >= S_ACTIVE && ks->authenticated != KS_AUTH_TRUE)
3326  {
3328  "Key %s [%d] not authorized%s, dropping packet.",
3329  source, key_id,
3330  (ks->authenticated == KS_AUTH_DEFERRED) ? " (deferred)" : "");
3331  gc_free(&gc);
3332  return;
3333  }
3334  }
3335 
3336  msg(D_TLS_ERRORS,
3337  "TLS Error: local/remote TLS keys are out of sync: %s "
3338  "(received key id: %d, known key ids: %s)",
3339  source, key_id,
3340  print_key_id(multi, &gc));
3341  gc_free(&gc);
3342 }
3343 
3351 static inline void
3353  const struct link_socket_actual *from,
3354  struct buffer *buf,
3355  struct crypto_options **opt,
3356  bool floated,
3357  const uint8_t **ad_start)
3358 {
3359  struct gc_arena gc = gc_new();
3360 
3361  uint8_t c = *BPTR(buf);
3362  int op = c >> P_OPCODE_SHIFT;
3363  int key_id = c & P_KEY_ID_MASK;
3364 
3365  for (int i = 0; i < KEY_SCAN_SIZE; ++i)
3366  {
3367  struct key_state *ks = get_key_scan(multi, i);
3368 
3369  /*
3370  * This is the basic test of TLS state compatibility between a local OpenVPN
3371  * instance and its remote peer.
3372  *
3373  * If the test fails, it tells us that we are getting a packet from a source
3374  * which claims reference to a prior negotiated TLS session, but the local
3375  * OpenVPN instance has no memory of such a negotiation.
3376  *
3377  * It almost always occurs on UDP sessions when the passive side of the
3378  * connection is restarted without the active side restarting as well (the
3379  * passive side is the server which only listens for the connections, the
3380  * active side is the client which initiates connections).
3381  */
3382  if (ks->state >= S_GENERATED_KEYS && key_id == ks->key_id
3383  && ks->authenticated == KS_AUTH_TRUE
3384  && (floated || link_socket_actual_match(from, &ks->remote_addr)))
3385  {
3387  /* return appropriate data channel decrypt key in opt */
3388  *opt = &ks->crypto_options;
3389  if (op == P_DATA_V2)
3390  {
3391  *ad_start = BPTR(buf);
3392  }
3393  ASSERT(buf_advance(buf, 1));
3394  if (op == P_DATA_V1)
3395  {
3396  *ad_start = BPTR(buf);
3397  }
3398  else if (op == P_DATA_V2)
3399  {
3400  if (buf->len < 4)
3401  {
3402  msg(D_TLS_ERRORS, "Protocol error: received P_DATA_V2 from %s but length is < 4",
3403  print_link_socket_actual(from, &gc));
3404  ++multi->n_soft_errors;
3405  goto done;
3406  }
3407  ASSERT(buf_advance(buf, 3));
3408  }
3409 
3410  ++ks->n_packets;
3411  ks->n_bytes += buf->len;
3413  "TLS: tls_pre_decrypt, key_id=%d, IP=%s",
3414  key_id, print_link_socket_actual(from, &gc));
3415  gc_free(&gc);
3416  return;
3417  }
3418  }
3419 
3420  print_key_id_not_found_reason(multi, from, key_id);
3421 
3422 done:
3423  gc_free(&gc);
3424  tls_clear_error();
3425  buf->len = 0;
3426  *opt = NULL;
3427 }
3428 
3429 /*
3430  *
3431  * When we are in TLS mode, this is the first routine which sees
3432  * an incoming packet.
3433  *
3434  * If it's a data packet, we set opt so that our caller can
3435  * decrypt it. We also give our caller the appropriate decryption key.
3436  *
3437  * If it's a control packet, we authenticate it and process it,
3438  * possibly creating a new tls_session if it represents the
3439  * first packet of a new session. For control packets, we will
3440  * also zero the size of *buf so that our caller ignores the
3441  * packet on our return.
3442  *
3443  * Note that openvpn only allows one active session at a time,
3444  * so a new session (once authenticated) will always usurp
3445  * an old session.
3446  *
3447  * Return true if input was an authenticated control channel
3448  * packet.
3449  *
3450  * If we are running in TLS thread mode, all public routines
3451  * below this point must be called with the L_TLS lock held.
3452  */
3453 
3454 bool
3456  const struct link_socket_actual *from,
3457  struct buffer *buf,
3458  struct crypto_options **opt,
3459  bool floated,
3460  const uint8_t **ad_start)
3461 {
3462 
3463  if (buf->len <= 0)
3464  {
3465  buf->len = 0;
3466  *opt = NULL;
3467  return false;
3468  }
3469 
3470  struct gc_arena gc = gc_new();
3471  bool ret = false;
3472 
3473  /* get opcode */
3474  uint8_t pkt_firstbyte = *BPTR(buf);
3475  int op = pkt_firstbyte >> P_OPCODE_SHIFT;
3476 
3477  if ((op == P_DATA_V1) || (op == P_DATA_V2))
3478  {
3479  handle_data_channel_packet(multi, from, buf, opt, floated, ad_start);
3480  return false;
3481  }
3482 
3483  /* get key_id */
3484  int key_id = pkt_firstbyte & P_KEY_ID_MASK;
3485 
3486  /* control channel packet */
3487  bool do_burst = false;
3488  bool new_link = false;
3489  struct session_id sid; /* remote session ID */
3490 
3491  /* verify legal opcode */
3492  if (op < P_FIRST_OPCODE || op > P_LAST_OPCODE)
3493  {
3496  {
3497  msg(D_TLS_ERRORS, "Peer tried unsupported key-method 1");
3498  }
3499  msg(D_TLS_ERRORS,
3500  "TLS Error: unknown opcode received from %s op=%d",
3501  print_link_socket_actual(from, &gc), op);
3502  goto error;
3503  }
3504 
3505  /* hard reset ? */
3506  if (is_hard_reset_method2(op))
3507  {
3508  /* verify client -> server or server -> client connection */
3509  if (((op == P_CONTROL_HARD_RESET_CLIENT_V2
3510  || op == P_CONTROL_HARD_RESET_CLIENT_V3) && !multi->opt.server)
3511  || ((op == P_CONTROL_HARD_RESET_SERVER_V2) && multi->opt.server))
3512  {
3513  msg(D_TLS_ERRORS,
3514  "TLS Error: client->client or server->server connection attempted from %s",
3515  print_link_socket_actual(from, &gc));
3516  goto error;
3517  }
3518  }
3519 
3520  /*
3521  * Authenticate Packet
3522  */
3523  dmsg(D_TLS_DEBUG, "TLS: control channel, op=%s, IP=%s",
3525 
3526  /* get remote session-id */
3527  {
3528  struct buffer tmp = *buf;
3529  buf_advance(&tmp, 1);
3530  if (!session_id_read(&sid, &tmp) || !session_id_defined(&sid))
3531  {
3532  msg(D_TLS_ERRORS,
3533  "TLS Error: session-id not found in packet from %s",
3534  print_link_socket_actual(from, &gc));
3535  goto error;
3536  }
3537  }
3538 
3539  int i;
3540  /* use session ID to match up packet with appropriate tls_session object */
3541  for (i = 0; i < TM_SIZE; ++i)
3542  {
3543  struct tls_session *session = &multi->session[i];
3544  struct key_state *ks = &session->key[KS_PRIMARY];
3545 
3546  dmsg(D_TLS_DEBUG,
3547  "TLS: initial packet test, i=%d state=%s, mysid=%s, rec-sid=%s, rec-ip=%s, stored-sid=%s, stored-ip=%s",
3548  i,
3549  state_name(ks->state),
3550  session_id_print(&session->session_id, &gc),
3551  session_id_print(&sid, &gc),
3552  print_link_socket_actual(from, &gc),
3555 
3556  if (session_id_equal(&ks->session_id_remote, &sid))
3557  /* found a match */
3558  {
3559  if (i == TM_LAME_DUCK)
3560  {
3561  msg(D_TLS_ERRORS,
3562  "TLS ERROR: received control packet with stale session-id=%s",
3563  session_id_print(&sid, &gc));
3564  goto error;
3565  }
3566  dmsg(D_TLS_DEBUG,
3567  "TLS: found match, session[%d], sid=%s",
3568  i, session_id_print(&sid, &gc));
3569  break;
3570  }
3571  }
3572 
3573  /*
3574  * Hard reset and session id does not match any session in
3575  * multi->session: Possible initial packet. New sessions always start
3576  * as TM_INITIAL
3577  */
3578  if (i == TM_SIZE && is_hard_reset_method2(op))
3579  {
3580  /*
3581  * No match with existing sessions,
3582  * probably a new session.
3583  */
3584  struct tls_session *session = &multi->session[TM_INITIAL];
3585 
3586  /*
3587  * If --single-session, don't allow any hard-reset connection request
3588  * unless it is the first packet of the session.
3589  */
3590  if (multi->opt.single_session && multi->n_sessions)
3591  {
3592  msg(D_TLS_ERRORS,
3593  "TLS Error: Cannot accept new session request from %s due "
3594  "to session context expire or --single-session",
3595  print_link_socket_actual(from, &gc));
3596  goto error;
3597  }
3598 
3600  session->opt))
3601  {
3602  goto error;
3603  }
3604 
3605 #ifdef ENABLE_MANAGEMENT
3606  if (management)
3607  {
3610  NULL,
3611  NULL,
3612  NULL,
3613  NULL,
3614  NULL);
3615  }
3616 #endif
3617 
3618  /*
3619  * New session-initiating control packet is authenticated at this point,
3620  * assuming that the --tls-auth command line option was used.
3621  *
3622  * Without --tls-auth, we leave authentication entirely up to TLS.
3623  */
3625  "TLS: Initial packet from %s, sid=%s",
3626  print_link_socket_actual(from, &gc),
3627  session_id_print(&sid, &gc));
3628 
3629  do_burst = true;
3630  new_link = true;
3631  i = TM_INITIAL;
3632  session->untrusted_addr = *from;
3633  }
3634  else
3635  {
3636  struct tls_session *session = &multi->session[i];
3637  struct key_state *ks = &session->key[KS_PRIMARY];
3638 
3639  /*
3640  * Packet must belong to an existing session.
3641  */
3642  if (i != TM_ACTIVE && i != TM_INITIAL)
3643  {
3644  msg(D_TLS_ERRORS,
3645  "TLS Error: Unroutable control packet received from %s (si=%d op=%s)",
3646  print_link_socket_actual(from, &gc),
3647  i,
3648  packet_opcode_name(op));
3649  goto error;
3650  }
3651 
3652  /*
3653  * Verify remote IP address
3654  */
3655  if (!new_link && !link_socket_actual_match(&ks->remote_addr, from))
3656  {
3657  msg(D_TLS_ERRORS, "TLS Error: Received control packet from unexpected IP addr: %s",
3658  print_link_socket_actual(from, &gc));
3659  goto error;
3660  }
3661 
3662  /*
3663  * Remote is requesting a key renegotiation. We only allow renegotiation
3664  * when the previous session is fully established to avoid weird corner
3665  * cases.
3666  */
3667  if (op == P_CONTROL_SOFT_RESET_V1 && ks->state >= S_GENERATED_KEYS)
3668  {
3670  from, session->opt))
3671  {
3672  goto error;
3673  }
3674 
3676 
3677  dmsg(D_TLS_DEBUG,
3678  "TLS: received P_CONTROL_SOFT_RESET_V1 s=%d sid=%s",
3679  i, session_id_print(&sid, &gc));
3680  }
3681  else
3682  {
3683  /*
3684  * Remote responding to our key renegotiation request?
3685  */
3686  if (op == P_CONTROL_SOFT_RESET_V1)
3687  {
3688  do_burst = true;
3689  }
3690 
3692  from, session->opt))
3693  {
3694  goto error;
3695  }
3696 
3697  dmsg(D_TLS_DEBUG,
3698  "TLS: received control channel packet s#=%d sid=%s",
3699  i, session_id_print(&sid, &gc));
3700  }
3701  }
3702 
3703  /*
3704  * We have an authenticated control channel packet (if --tls-auth/tls-crypt
3705  * or tls-crypt-v2 was set).
3706  * Now pass to our reliability layer which deals with
3707  * packet acknowledgements, retransmits, sequencing, etc.
3708  */
3709  struct tls_session *session = &multi->session[i];
3710  struct key_state *ks = &session->key[KS_PRIMARY];
3711 
3712  /* Make sure we were initialized and that we're not in an error state */
3713  ASSERT(ks->state != S_UNDEF);
3714  ASSERT(ks->state != S_ERROR);
3715  ASSERT(session_id_defined(&session->session_id));
3716 
3717  /* Let our caller know we processed a control channel packet */
3718  ret = true;
3719 
3720  /*
3721  * Set our remote address and remote session_id
3722  */
3723  if (new_link)
3724  {
3725  ks->session_id_remote = sid;
3726  ks->remote_addr = *from;
3727  ++multi->n_sessions;
3728  }
3729  else if (!link_socket_actual_match(&ks->remote_addr, from))
3730  {
3731  msg(D_TLS_ERRORS,
3732  "TLS Error: Existing session control channel packet from unknown IP address: %s",
3733  print_link_socket_actual(from, &gc));
3734  goto error;
3735  }
3736 
3737  /*
3738  * Should we do a retransmit of all unacknowledged packets in
3739  * the send buffer? This improves the start-up efficiency of the
3740  * initial key negotiation after the 2nd peer comes online.
3741  */
3742  if (do_burst && !session->burst)
3743  {
3745  session->burst = true;
3746  }
3747 
3748  /* Check key_id */
3749  if (ks->key_id != key_id)
3750  {
3751  msg(D_TLS_ERRORS,
3752  "TLS ERROR: local/remote key IDs out of sync (%d/%d) ID: %s",
3753  ks->key_id, key_id, print_key_id(multi, &gc));
3754  goto error;
3755  }
3756 
3757  /*
3758  * Process incoming ACKs for packets we can now
3759  * delete from reliable send buffer
3760  */
3761  {
3762  /* buffers all packet IDs to delete from send_reliable */
3763  struct reliable_ack send_ack;
3764 
3765  if (!reliable_ack_read(&send_ack, buf, &session->session_id))
3766  {
3767  msg(D_TLS_ERRORS,
3768  "TLS Error: reading acknowledgement record from packet");
3769  goto error;
3770  }
3771  reliable_send_purge(ks->send_reliable, &send_ack);
3772  }
3773 
3774  if (op != P_ACK_V1 && reliable_can_get(ks->rec_reliable))
3775  {
3776  packet_id_type id;
3777 
3778  /* Extract the packet ID from the packet */
3779  if (reliable_ack_read_packet_id(buf, &id))
3780  {
3781  /* Avoid deadlock by rejecting packet that would de-sequentialize receive buffer */
3783  {
3784  if (reliable_not_replay(ks->rec_reliable, id))
3785  {
3786  /* Save incoming ciphertext packet to reliable buffer */
3787  struct buffer *in = reliable_get_buf(ks->rec_reliable);
3788  ASSERT(in);
3789  if (!buf_copy(in, buf))
3790  {
3792  "Incoming control channel packet too big, dropping.");
3793  goto error;
3794  }
3796  }
3797 
3798  /* Process outgoing acknowledgment for packet just received, even if it's a replay */
3800  }
3801  }
3802  }
3803  /* Remember that we received a valid control channel packet */
3804  ks->peer_last_packet = now;
3805 
3806 done:
3807  buf->len = 0;
3808  *opt = NULL;
3809  gc_free(&gc);
3810  return ret;
3811 
3812 error:
3813  ++multi->n_soft_errors;
3814  tls_clear_error();
3815  goto done;
3816 }
3817 
3818 
3819 struct key_state *
3821 {
3822  struct key_state *ks_select = NULL;
3823  for (int i = 0; i < KEY_SCAN_SIZE; ++i)
3824  {
3825  struct key_state *ks = get_key_scan(multi, i);
3826  if (ks->state >= S_GENERATED_KEYS && ks->authenticated == KS_AUTH_TRUE)
3827  {
3829 
3830  if (!ks_select)
3831  {
3832  ks_select = ks;
3833  }
3834  if (now >= ks->auth_deferred_expire)
3835  {
3836  ks_select = ks;
3837  break;
3838  }
3839  }
3840  }
3841  return ks_select;
3842 }
3843 
3844 
3845 /* Choose the key with which to encrypt a data packet */
3846 void
3848  struct buffer *buf, struct crypto_options **opt)
3849 {
3850  multi->save_ks = NULL;
3851  if (buf->len <= 0)
3852  {
3853  buf->len = 0;
3854  *opt = NULL;
3855  return;
3856  }
3857 
3858  struct key_state *ks_select = tls_select_encryption_key(multi);
3859 
3860  if (ks_select)
3861  {
3862  *opt = &ks_select->crypto_options;
3863  multi->save_ks = ks_select;
3864  dmsg(D_TLS_KEYSELECT, "TLS: tls_pre_encrypt: key_id=%d", ks_select->key_id);
3865  return;
3866  }
3867  else
3868  {
3869  struct gc_arena gc = gc_new();
3870  dmsg(D_TLS_KEYSELECT, "TLS Warning: no data channel send key available: %s",
3871  print_key_id(multi, &gc));
3872  gc_free(&gc);
3873 
3874  *opt = NULL;
3875  buf->len = 0;
3876  }
3877 }
3878 
3879 void
3880 tls_prepend_opcode_v1(const struct tls_multi *multi, struct buffer *buf)
3881 {
3882  struct key_state *ks = multi->save_ks;
3883  uint8_t op;
3884 
3885  msg(D_TLS_DEBUG, __func__);
3886 
3887  ASSERT(ks);
3888 
3889  op = (P_DATA_V1 << P_OPCODE_SHIFT) | ks->key_id;
3890  ASSERT(buf_write_prepend(buf, &op, 1));
3891 }
3892 
3893 void
3894 tls_prepend_opcode_v2(const struct tls_multi *multi, struct buffer *buf)
3895 {
3896  struct key_state *ks = multi->save_ks;
3897  uint32_t peer;
3898 
3899  msg(D_TLS_DEBUG, __func__);
3900 
3901  ASSERT(ks);
3902 
3903  peer = htonl(((P_DATA_V2 << P_OPCODE_SHIFT) | ks->key_id) << 24
3904  | (multi->peer_id & 0xFFFFFF));
3905  ASSERT(buf_write_prepend(buf, &peer, 4));
3906 }
3907 
3908 void
3909 tls_post_encrypt(struct tls_multi *multi, struct buffer *buf)
3910 {
3911  struct key_state *ks = multi->save_ks;
3912  multi->save_ks = NULL;
3913 
3914  if (buf->len > 0)
3915  {
3916  ASSERT(ks);
3917 
3918  ++ks->n_packets;
3919  ks->n_bytes += buf->len;
3920  }
3921 }
3922 
3923 /*
3924  * Send a payload over the TLS control channel.
3925  * Called externally.
3926  */
3927 
3928 bool
3930  const uint8_t *data,
3931  int size)
3932 {
3933  bool ret = false;
3934 
3935  tls_clear_error();
3936 
3937  ASSERT(ks);
3938 
3939  if (ks->state >= S_ACTIVE)
3940  {
3941  if (key_state_write_plaintext_const(&ks->ks_ssl, data, size) == 1)
3942  {
3943  ret = true;
3944  }
3945  }
3946  else
3947  {
3948  if (!ks->paybuf)
3949  {
3950  ks->paybuf = buffer_list_new();
3951  }
3952  buffer_list_push_data(ks->paybuf, data, (size_t)size);
3953  ret = true;
3954  }
3955 
3956 
3957  tls_clear_error();
3958 
3959  return ret;
3960 }
3961 
3962 bool
3964  struct buffer *buf)
3965 {
3966  bool ret = false;
3967 
3968  tls_clear_error();
3969 
3970  ASSERT(multi);
3971 
3972  struct key_state *ks = get_key_scan(multi, 0);
3973 
3974  if (ks->state >= S_ACTIVE && BLEN(&ks->plaintext_read_buf))
3975  {
3976  if (buf_copy(buf, &ks->plaintext_read_buf))
3977  {
3978  ret = true;
3979  }
3980  ks->plaintext_read_buf.len = 0;
3981  }
3982 
3983  tls_clear_error();
3984 
3985  return ret;
3986 }
3987 
3988 void
3989 tls_update_remote_addr(struct tls_multi *multi, const struct link_socket_actual *addr)
3990 {
3991  struct gc_arena gc = gc_new();
3992  for (int i = 0; i < TM_SIZE; ++i)
3993  {
3994  struct tls_session *session = &multi->session[i];
3995 
3996  for (int j = 0; j < KS_SIZE; ++j)
3997  {
3998  struct key_state *ks = &session->key[j];
3999 
4001  || link_socket_actual_match(addr, &ks->remote_addr))
4002  {
4003  continue;
4004  }
4005 
4006  dmsg(D_TLS_KEYSELECT, "TLS: tls_update_remote_addr from IP=%s to IP=%s",
4008  print_link_socket_actual(addr, &gc));
4009 
4010  ks->remote_addr = *addr;
4011  }
4012  }
4013  gc_free(&gc);
4014 }
4015 
4016 void
4017 show_available_tls_ciphers(const char *cipher_list,
4018  const char *cipher_list_tls13,
4019  const char *tls_cert_profile)
4020 {
4021  printf("Available TLS Ciphers, listed in order of preference:\n");
4022 
4023  if (tls_version_max() >= TLS_VER_1_3)
4024  {
4025  printf("\nFor TLS 1.3 and newer (--tls-ciphersuites):\n\n");
4026  show_available_tls_ciphers_list(cipher_list_tls13, tls_cert_profile, true);
4027  }
4028 
4029  printf("\nFor TLS 1.2 and older (--tls-cipher):\n\n");
4030  show_available_tls_ciphers_list(cipher_list, tls_cert_profile, false);
4031 
4032  printf("\n"
4033  "Be aware that that whether a cipher suite in this list can actually work\n"
4034  "depends on the specific setup of both peers. See the man page entries of\n"
4035  "--tls-cipher and --show-tls for more details.\n\n"
4036  );
4037 }
4038 
4039 /*
4040  * Dump a human-readable rendition of an openvpn packet
4041  * into a garbage collectable string which is returned.
4042  */
4043 const char *
4044 protocol_dump(struct buffer *buffer, unsigned int flags, struct gc_arena *gc)
4045 {
4046  struct buffer out = alloc_buf_gc(256, gc);
4047  struct buffer buf = *buffer;
4048 
4049  uint8_t c;
4050  int op;
4051  int key_id;
4052 
4053  int tls_auth_hmac_size = (flags & PD_TLS_AUTH_HMAC_SIZE_MASK);
4054 
4055  if (buf.len <= 0)
4056  {
4057  buf_printf(&out, "DATA UNDEF len=%d", buf.len);
4058  goto done;
4059  }
4060 
4061  if (!(flags & PD_TLS))
4062  {
4063  goto print_data;
4064  }
4065 
4066  /*
4067  * Initial byte (opcode)
4068  */
4069  if (!buf_read(&buf, &c, sizeof(c)))
4070  {
4071  goto done;
4072  }
4073  op = (c >> P_OPCODE_SHIFT);
4074  key_id = c & P_KEY_ID_MASK;
4075  buf_printf(&out, "%s kid=%d", packet_opcode_name(op), key_id);
4076 
4077  if ((op == P_DATA_V1) || (op == P_DATA_V2))
4078  {
4079  goto print_data;
4080  }
4081 
4082  /*
4083  * Session ID
4084  */
4085  {
4086  struct session_id sid;
4087 
4088  if (!session_id_read(&sid, &buf))
4089  {
4090  goto done;
4091  }
4092  if (flags & PD_VERBOSE)
4093  {
4094  buf_printf(&out, " sid=%s", session_id_print(&sid, gc));
4095  }
4096  }
4097 
4098  /*
4099  * tls-auth hmac + packet_id
4100  */
4101  if (tls_auth_hmac_size)
4102  {
4103  struct packet_id_net pin;
4104  uint8_t tls_auth_hmac[MAX_HMAC_KEY_LENGTH];
4105 
4106  ASSERT(tls_auth_hmac_size <= MAX_HMAC_KEY_LENGTH);
4107 
4108  if (!buf_read(&buf, tls_auth_hmac, tls_auth_hmac_size))
4109  {
4110  goto done;
4111  }
4112  if (flags & PD_VERBOSE)
4113  {
4114  buf_printf(&out, " tls_hmac=%s", format_hex(tls_auth_hmac, tls_auth_hmac_size, 0, gc));
4115  }
4116 
4117  if (!packet_id_read(&pin, &buf, true))
4118  {
4119  goto done;
4120  }
4121  buf_printf(&out, " pid=%s", packet_id_net_print(&pin, (flags & PD_VERBOSE), gc));
4122  }
4123  /*
4124  * packet_id + tls-crypt hmac
4125  */
4126  if (flags & PD_TLS_CRYPT)
4127  {
4128  struct packet_id_net pin;
4129  uint8_t tls_crypt_hmac[TLS_CRYPT_TAG_SIZE];
4130 
4131  if (!packet_id_read(&pin, &buf, true))
4132  {
4133  goto done;
4134  }
4135  buf_printf(&out, " pid=%s", packet_id_net_print(&pin, (flags & PD_VERBOSE), gc));
4136  if (!buf_read(&buf, tls_crypt_hmac, TLS_CRYPT_TAG_SIZE))
4137  {
4138  goto done;
4139  }
4140  if (flags & PD_VERBOSE)
4141  {
4142  buf_printf(&out, " tls_crypt_hmac=%s", format_hex(tls_crypt_hmac, TLS_CRYPT_TAG_SIZE, 0, gc));
4143  }
4144  /*
4145  * Remainder is encrypted and optional wKc
4146  */
4147  goto done;
4148  }
4149 
4150  /*
4151  * ACK list
4152  */
4153  buf_printf(&out, " %s", reliable_ack_print(&buf, (flags & PD_VERBOSE), gc));
4154 
4155  if (op == P_ACK_V1)
4156  {
4157  goto print_data;
4158  }
4159 
4160  /*
4161  * Packet ID
4162  */
4163  {
4164  packet_id_type l;
4165  if (!buf_read(&buf, &l, sizeof(l)))
4166  {
4167  goto done;
4168  }
4169  l = ntohpid(l);
4171  }
4172 
4173 print_data:
4174  if (flags & PD_SHOW_DATA)
4175  {
4176  buf_printf(&out, " DATA %s", format_hex(BPTR(&buf), BLEN(&buf), 80, gc));
4177  }
4178  else
4179  {
4180  buf_printf(&out, " DATA len=%d", buf.len);
4181  }
4182 
4183 done:
4184  return BSTR(&out);
4185 }
output_peer_info_env
void output_peer_info_env(struct env_set *es, const char *peer_info)
Definition: misc.c:755
P_LAST_OPCODE
#define P_LAST_OPCODE
Definition: ssl_pkt.h:65
load_xkey_provider
void load_xkey_provider(void)
Load ovpn.xkey provider used for external key signing.
Definition: ssl_openssl.c:2377
buf_safe
static bool buf_safe(const struct buffer *buf, size_t len)
Definition: buffer.h:538
S_SENT_KEY
#define S_SENT_KEY
Local OpenVPN process has sent its part of the key material.
Definition: ssl_common.h:88
TLSMP_ACTIVE
#define TLSMP_ACTIVE
Definition: ssl.h:222
reliable_schedule_now
void reliable_schedule_now(struct reliable *rel)
Reschedule all entries of a reliable structure to be ready for (re)sending immediately.
Definition: reliable.c:702
D_TLS_DEBUG
#define D_TLS_DEBUG
Definition: errlevel.h:165
S_GENERATED_KEYS
#define S_GENERATED_KEYS
The data channel keys have been generated The TLS session is fully authenticated when reaching this s...
Definition: ssl_common.h:98
key_state_ssl_free
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:1965
set_auth_token_user
void set_auth_token_user(struct user_pass *tk, const char *username)
Sets the auth-token username by base64 decoding the passed username.
Definition: misc.c:516
management_set_state
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:2737
push_peer_info
static bool push_peer_info(struct buffer *buf, struct tls_session *session)
Prepares the IV_ and UV_ variables that are part of the exchange to signal the peer's capabilities.
Definition: ssl.c:1851
tls_ctx_load_cert_file
void tls_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *cert_file, bool cert_file_inline)
Use Windows cryptoapi for key and cert, and add to library-specific TLS context.
Definition: ssl_openssl.c:970
P_DATA_V2
#define P_DATA_V2
Definition: ssl_pkt.h:48
crypto_uninit_lib
void crypto_uninit_lib(void)
Definition: crypto_openssl.c:212
options::ssl_flags
unsigned int ssl_flags
Definition: options.h:608
key2::n
int n
The number of key objects stored in the key2.keys array.
Definition: crypto.h:181
reliable_ack_acknowledge_packet_id
bool reliable_ack_acknowledge_packet_id(struct reliable_ack *ack, packet_id_type pid)
Record a packet ID for later acknowledgment.
Definition: reliable.c:132
tls_init_control_channel_frame_parameters
void tls_init_control_channel_frame_parameters(struct frame *frame, int tls_mtu)
Definition: ssl.c:135
options_cmp_equal
bool options_cmp_equal(char *actual, const char *expected)
Definition: options.c:4465
tls_limit_reneg_bytes
static void tls_limit_reneg_bytes(const char *ciphername, int *reneg_bytes)
Limit the reneg_bytes value when using a small-block (<128 bytes) cipher.
Definition: ssl.c:121
tls_authentication_status
enum tls_auth_status tls_authentication_status(struct tls_multi *multi)
Return current session authentication state of the tls_multi structure This will return TLS_AUTHENTIC...
Definition: ssl_verify.c:1154
key_state::n_bytes
counter_type n_bytes
Definition: ssl_common.h:241
M_INFO
#define M_INFO
Definition: errlevel.h:55
cipher_ctx_iv_length
int cipher_ctx_iv_length(const cipher_ctx_t *ctx)
Returns the size of the IV used by the cipher, in bytes, or 0 if no IV is used.
generate_key_expansion
static bool generate_key_expansion(struct tls_multi *multi, struct key_state *ks, struct tls_session *session)
Definition: ssl.c:1455
key_state_ssl
Definition: ssl_mbedtls.h:125
env_item::next
struct env_item * next
Definition: env_set.h:39
buf_read
static bool buf_read(struct buffer *src, void *dest, int size)
Definition: buffer.h:796
env_set::gc
struct gc_arena * gc
Definition: env_set.h:43
P_KEY_ID_MASK
#define P_KEY_ID_MASK
Definition: ssl_pkt.h:38
error.h
ssl_backend.h
read_string_alloc
static char * read_string_alloc(struct buffer *buf)
Definition: ssl.c:1815
TLS_RELIABLE_N_REC_BUFFERS
#define TLS_RELIABLE_N_REC_BUFFERS
Definition: ssl_pkt.h:71
random_bytes_to_buf
static bool random_bytes_to_buf(struct buffer *buf, uint8_t *out, int outlen)
Definition: ssl.c:1646
gc_new
static struct gc_arena gc_new(void)
Definition: buffer.h:1031
IV_PROTO_DATA_V2
#define IV_PROTO_DATA_V2
Support P_DATA_V2.
Definition: ssl.h:79
key_state::n_packets
counter_type n_packets
Definition: ssl_common.h:242
auth_token.h
tls_options::frame
struct frame frame
Definition: ssl_common.h:370
tls_init_lib
void tls_init_lib(void)
Perform any static initialisation necessary by the library.
Definition: ssl_openssl.c:85
print_key_id
static const char * print_key_id(struct tls_multi *multi, struct gc_arena *gc)
Definition: ssl.c:743
P_CONTROL_HARD_RESET_CLIENT_V1
#define P_CONTROL_HARD_RESET_CLIENT_V1
Definition: ssl_pkt.h:42
OPENVPN_AEAD_MIN_IV_LEN
#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:294
gremlin.h
RELIABLE_ACK_SIZE
#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:44
ssl_set_auth_token_user
void ssl_set_auth_token_user(const char *username)
Definition: ssl.c:347
key_state::ks_ssl
struct key_state_ssl ks_ssl
Definition: ssl_common.h:213
CO_RESEND_WKC
#define CO_RESEND_WKC
Bit-flag indicating that the client is expected to resend the wrapped client key with the 2nd packet ...
Definition: crypto.h:266
get_user_pass_cr
bool get_user_pass_cr(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags, const char *auth_challenge)
Retrieves the user credentials from various sources depending on the flags.
Definition: misc.c:211
tls_ctx_load_priv_file
int tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file, bool priv_key_file_inline)
Load private key file into the given TLS context.
Definition: ssl_openssl.c:1032
buffer::len
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
frame_print
void frame_print(const struct frame *frame, int level, const char *prefix)
Definition: mtu.c:195
prepend_dir
struct buffer prepend_dir(const char *dir, const char *path, struct gc_arena *gc)
Prepend a directory to a path.
Definition: misc.c:777
GET_USER_PASS_STATIC_CHALLENGE_ECHO
#define GET_USER_PASS_STATIC_CHALLENGE_ECHO
Definition: misc.h:117
packet_id_init
void packet_id_init(struct packet_id *p, int seq_backtrack, int time_backtrack, const char *name, int unit)
Definition: packet_id.c:79
M_FATAL
#define M_FATAL
Definition: error.h:95
tls_item_in_cipher_list
bool tls_item_in_cipher_list(const char *item, const char *list)
Return true iff item is present in the colon-separated zero-terminated cipher list.
Definition: ssl_ncp.c:207
win32.h
calc_control_channel_frame_overhead
static int calc_control_channel_frame_overhead(const struct tls_session *session)
calculate the maximum overhead that control channel frames have This includes header,...
Definition: ssl.c:183
route_gateway_info
Definition: route.h:146
session_move_pre_start
static bool session_move_pre_start(const struct tls_session *session, struct key_state *ks, bool skip_initial_send)
Move the session from S_INITIAL to S_PRE_START.
Definition: ssl.c:2368
P_CONTROL_HARD_RESET_CLIENT_V2
#define P_CONTROL_HARD_RESET_CLIENT_V2
Definition: ssl_pkt.h:51
TM_SIZE
#define TM_SIZE
Size of the tls_multi.session array.
Definition: ssl_common.h:530
TLS_AUTHENTICATED
#define TLS_AUTHENTICATED(multi, ks)
Check whether the ks key_state has finished the key exchange part of the OpenVPN hand shake.
Definition: ssl_verify.h:109
ks_auth_state
ks_auth_state
This reflects the (server side) authentication state after the TLS session has been established and k...
Definition: ssl_common.h:143
options::ca_path
const char * ca_path
Definition: options.h:578
interval_t
int interval_t
Definition: common.h:36
KS_PRIMARY
#define KS_PRIMARY
Primary key state index.
Definition: ssl_common.h:445
KEY_DIRECTION_NORMAL
#define KEY_DIRECTION_NORMAL
Definition: crypto.h:172
key_state::rec_ack
struct reliable_ack * rec_ack
Definition: ssl_common.h:235
tls_multi::locked_cert_hash_set
struct cert_hash_set * locked_cert_hash_set
Definition: ssl_common.h:622
buf_init
#define buf_init(buf, offset)
Definition: buffer.h:209
options::cert_file
const char * cert_file
Definition: options.h:581
tls_ctx_load_ecdh_params
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:722
es
struct env_set * es
Definition: test_pkcs11.c:133
buf_write_u32
static bool buf_write_u32(struct buffer *dest, uint32_t data)
Definition: buffer.h:723
D_TLS_DEBUG_LOW
#define D_TLS_DEBUG_LOW
Definition: errlevel.h:77
p2p_mode_ncp
void p2p_mode_ncp(struct tls_multi *multi, struct tls_session *session)
Determines if there is common cipher of both peer by looking at the IV_CIPHER peer info.
Definition: ssl_ncp.c:470
tls_session::key_id
int key_id
The current active key id, used to keep track of renegotiations.
Definition: ssl_common.h:491
resend_auth_token_renegotiation
void resend_auth_token_renegotiation(struct tls_multi *multi, struct tls_session *session)
Checks if a client should be sent a new auth token to update its current auth-token.
Definition: auth_token.c:462
tls_multi::session
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer.
Definition: ssl_common.h:672
S_START
#define S_START
Three-way handshake is complete, start of key exchange.
Definition: ssl_common.h:86
BSTR
#define BSTR(buf)
Definition: buffer.h:129
INCR_GENERATED
#define INCR_GENERATED
Definition: ssl.c:92
options::authname
const char * authname
Definition: options.h:561
user_pass::username
char username[USER_PASS_LEN]
Definition: misc.h:71
read_string
static bool read_string(struct buffer *buf, char *str, const unsigned int capacity)
Definition: ssl.c:1799
key_source2::server
struct key_source server
Random provided by server.
Definition: ssl_common.h:132
get_ssl_library_version
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:2344
key_state::must_negotiate
time_t must_negotiate
Definition: ssl_common.h:217
print_details
void print_details(struct key_state_ssl *ks_ssl, const char *prefix)
Definition: ssl_openssl.c:2177
D_MTU_INFO
#define D_MTU_INFO
Definition: errlevel.h:105
tls_ctx_load_extra_certs
void tls_ctx_load_extra_certs(struct tls_root_ctx *ctx, const char *extra_certs_file, bool extra_certs_file_inline)
Load extra certificate authority certificates from the given file or path.
Definition: ssl_openssl.c:1706
reliable_empty
bool reliable_empty(const struct reliable *rel)
Check whether a reliable structure is empty.
Definition: reliable.c:393
D_HANDSHAKE
#define D_HANDSHAKE
Definition: errlevel.h:72
tls_ctx_client_new
void tls_ctx_client_new(struct tls_root_ctx *ctx)
Initialises a library-specific TLS context for a client.
Definition: ssl_openssl.c:128
TLS_VER_1_1
#define TLS_VER_1_1
Definition: ssl_backend.h:106
TLS_AUTHENTICATION_SUCCEEDED
@ TLS_AUTHENTICATION_SUCCEEDED
Definition: ssl_verify.h:70
options::extra_certs_file_inline
bool extra_certs_file_inline
Definition: options.h:584
tls_ctx_server_new
void tls_ctx_server_new(struct tls_root_ctx *ctx)
Initialise a library-specific TLS context for a server.
Definition: ssl_openssl.c:110
alloc_buf_gc
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition: buffer.c:88
reliable_entry::buf
struct buffer buf
Definition: reliable.h:84
key_state::rec_reliable
struct reliable * rec_reliable
Definition: ssl_common.h:234
key_ctx::cipher
cipher_ctx_t * cipher
Generic cipher context.
Definition: crypto.h:164
key_state_soft_reset
static void key_state_soft_reset(struct tls_session *session)
Definition: ssl.c:1745
session_id_equal
static bool session_id_equal(const struct session_id *sid1, const struct session_id *sid2)
Definition: session_id.h:48
user_pass::defined
bool defined
Definition: misc.h:58
show_available_tls_ciphers_list
void show_available_tls_ciphers_list(const char *cipher_list, const char *tls_cert_profile, bool tls13)
Definition: ssl_openssl.c:2204
key_state::lru_acks
struct reliable_ack * lru_acks
Definition: ssl_common.h:236
session::key
char key[48]
Definition: keyingmaterialexporter.c:58
OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY
#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY
Definition: openvpn-plugin.h:122
P_CONTROL_WKC_V1
#define P_CONTROL_WKC_V1
Definition: ssl_pkt.h:59
tls_options::replay_time
int replay_time
Definition: ssl_common.h:358
TLS_CHANNEL_MTU_MIN
#define TLS_CHANNEL_MTU_MIN
Definition: common.h:82
openvpn.h
tls_ctx_set_options
bool tls_ctx_set_options(struct tls_root_ctx *ctx, unsigned int ssl_flags)
Set any library specific options.
Definition: ssl_openssl.c:316
options::cipher_list
const char * cipher_list
Definition: options.h:589
reset_session
static void reset_session(struct tls_multi *multi, struct tls_session *session)
Definition: ssl.c:1088
IV_PROTO_TLS_KEY_EXPORT
#define IV_PROTO_TLS_KEY_EXPORT
Supports key derivation via TLS key material exporter [RFC5705].
Definition: ssl.h:86
frame::tailroom
int tailroom
the tailroom in the buffer.
Definition: mtu.h:112
tls_options::single_session
bool single_session
Definition: ssl_common.h:313
flush_payload_buffer
static void flush_payload_buffer(struct key_state *ks)
Definition: ssl.c:1729
verify_final_auth_checks
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:1774
buffer_list_peek
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
Definition: buffer.c:1266
CAS_PENDING
@ CAS_PENDING
Options import (Connect script/plugin, ccd,...)
Definition: ssl_common.h:561
VALGRIND_MAKE_READABLE
#define VALGRIND_MAKE_READABLE(addr, len)
Definition: memdbg.h:53
PACKAGE_VERSION
#define PACKAGE_VERSION
Definition: config.h:504
get_primary_key
static const struct key_state * get_primary_key(const struct tls_multi *multi)
gets an item of key_state objects in the order they should be scanned by data channel modules.
Definition: ssl_common.h:719
buf_clear
void buf_clear(struct buffer *buf)
Definition: buffer.c:162
KS_AUTH_FALSE
@ KS_AUTH_FALSE
Key state is not authenticated
Definition: ssl_common.h:144
session_id_print
const char * session_id_print(const struct session_id *sid, struct gc_arena *gc)
Definition: session_id.c:55
key2_print
void key2_print(const struct key2 *k, const struct key_type *kt, const char *prefix0, const char *prefix1)
Prints the keys in a key2 structure.
Definition: crypto.c:988
key_ctx_bi::initialized
bool initialized
Definition: crypto.h:223
INCR_SUCCESS
#define INCR_SUCCESS
Definition: ssl.c:93
dmsg
#define dmsg(flags,...)
Definition: error.h:154
reliable_get_buf
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:536
ENABLE_MANAGEMENT
#define ENABLE_MANAGEMENT
Definition: config.h:53
tls_free_lib
void tls_free_lib(void)
Free any global SSL library-specific data structures.
Definition: ssl_openssl.c:99
tls_session_update_crypto_params_do_work
bool tls_session_update_crypto_params_do_work(struct tls_multi *multi, struct tls_session *session, struct options *options, struct frame *frame, struct frame *frame_fragment, struct link_socket_info *lsi)
Definition: ssl.c:1570
KEY_DIRECTION_INVERSE
#define KEY_DIRECTION_INVERSE
Definition: crypto.h:173
key_source2_read
static int key_source2_read(struct key_source2 *k2, struct buffer *buf, bool server)
Definition: ssl.c:1695
backend_tls_ctx_reload_crl
void backend_tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, bool crl_inline)
Reload the Certificate Revocation List for the SSL channel.
Definition: ssl_openssl.c:1089
init_key_contexts
static void init_key_contexts(struct key_state *ks, struct tls_multi *multi, const struct key_type *key_type, bool server, struct key2 *key2, bool dco_enabled)
Definition: ssl.c:1338
tls_ctx_use_management_external_key
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:1477
fdmisc.h
KS_LAME_DUCK
#define KS_LAME_DUCK
Key state index that will retire soon.
Definition: ssl_common.h:446
buf_copy
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Definition: buffer.h:730
generate_key_expansion_openvpn_prf
static bool generate_key_expansion_openvpn_prf(const struct tls_session *session, struct key2 *key2)
Definition: ssl.c:1397
format_hex_ex
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:527
KEY_METHOD_MASK
#define KEY_METHOD_MASK
Definition: ssl.h:115
SSLF_AUTH_USER_PASS_OPTIONAL
#define SSLF_AUTH_USER_PASS_OPTIONAL
Definition: ssl_common.h:407
tls_session_generate_data_channel_keys
bool tls_session_generate_data_channel_keys(struct tls_multi *multi, struct tls_session *session)
Generate data channel keys for the supplied TLS session.
Definition: ssl.c:1538
tls_process_state
static bool tls_process_state(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:2686
tls_multi::n_soft_errors
int n_soft_errors
Definition: ssl_common.h:615
tls_multi_process
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:3083
move_session
static void move_session(struct tls_multi *multi, int dest, int src, bool reinit_src)
Definition: ssl.c:1063
reliable::array
struct reliable_entry array[RELIABLE_CAPACITY]
Definition: reliable.h:98
P_OPCODE_SHIFT
#define P_OPCODE_SHIFT
Definition: ssl_pkt.h:39
tls_options::tls_wrap
struct tls_wrap_ctx tls_wrap
TLS handshake wrapping state.
Definition: ssl_common.h:368
RGI_HWADDR_DEFINED
#define RGI_HWADDR_DEFINED
Definition: route.h:149
tls_crypt.h
tls_root_ctx::crl_last_mtime
time_t crl_last_mtime
CRL last modification time.
Definition: ssl_mbedtls.h:114
key::hmac
uint8_t hmac[MAX_HMAC_KEY_LENGTH]
Key material for HMAC operations.
Definition: crypto.h:153
lame_duck_must_die
static bool lame_duck_must_die(const struct tls_session *session, interval_t *wakeup)
Definition: ssl.c:1116
tls_options::renegotiate_seconds
interval_t renegotiate_seconds
Definition: ssl_common.h:333
print_key_id_not_found_reason
static void print_key_id_not_found_reason(struct tls_multi *multi, const struct link_socket_actual *from, int key_id)
We have not found a matching key to decrypt data channel packet, try to generate a sensible error mes...
Definition: ssl.c:3300
TLS_VER_1_0
#define TLS_VER_1_0
Definition: ssl_backend.h:105
tls_crypt_buf_overhead
int tls_crypt_buf_overhead(void)
Returns the maximum overhead (in bytes) added to the destination buffer by tls_crypt_wrap().
Definition: tls_crypt.c:55
MODE_SERVER
#define MODE_SERVER
Definition: options.h:246
frame
Packet geometry parameters.
Definition: mtu.h:98
read_control_auth
bool read_control_auth(struct buffer *buf, struct tls_wrap_ctx *ctx, const struct link_socket_actual *from, const struct tls_options *opt)
Definition: ssl_pkt.c:200
tls_multi
Security parameter state for a single VPN tunnel.
Definition: ssl_common.h:587
MAX_PEER_ID
#define MAX_PEER_ID
Definition: openvpn.h:549
key_source
Container for one half of random material to be used in key method 2 data channel key generation.
Definition: ssl_common.h:113
tls_wrap_free
static void tls_wrap_free(struct tls_wrap_ctx *tls_wrap)
Free the elements of a tls_wrap_ctx structure.
Definition: ssl.h:470
options::tls_server
bool tls_server
Definition: options.h:574
compute_earliest_wakeup
static void compute_earliest_wakeup(interval_t *earliest, interval_t seconds_from_now)
Definition: ssl.c:1099
GET_USER_PASS_MANAGEMENT
#define GET_USER_PASS_MANAGEMENT
Definition: misc.h:107
key_state::authenticated
enum ks_auth_state authenticated
Definition: ssl_common.h:247
key_state_write_ciphertext
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:2024
key_state
Security parameter state of one TLS and data channel key session.
Definition: ssl_common.h:195
init_key_ctx_bi
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:869
interval.h
KEY_SCAN_SIZE
#define KEY_SCAN_SIZE
Definition: ssl_common.h:546
key
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:149
CAS_WAITING_AUTH
@ CAS_WAITING_AUTH
Initial TLS connection established but deferred auth is not yet finished.
Definition: ssl_common.h:560
show_available_tls_ciphers
void show_available_tls_ciphers(const char *cipher_list, const char *cipher_list_tls13, const char *tls_cert_profile)
Definition: ssl.c:4017
options::tls_cert_profile
const char * tls_cert_profile
Definition: options.h:592
EARLY_NEG_FLAG_RESEND_WKC
#define EARLY_NEG_FLAG_RESEND_WKC
Definition: ssl_pkt.h:316
plugin_call
static int plugin_call(const struct plugin_list *pl, const int type, const struct argv *av, struct plugin_return *pr, struct env_set *es)
Definition: plugin.h:202
KEY_METHOD_2
#define KEY_METHOD_2
Definition: ssl.h:112
write_string
static bool write_string(struct buffer *buf, const char *str, const int maxlen)
Definition: ssl.c:1780
buffer_list_push_data
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:1239
reliable_set_timeout
static void reliable_set_timeout(struct reliable *rel, interval_t timeout)
Definition: reliable.h:531
reliable_entry::opcode
int opcode
Definition: reliable.h:83
tls_auth_status
tls_auth_status
Definition: ssl_verify.h:68
tls_version_parse
int tls_version_parse(const char *vstr, const char *extra)
Definition: ssl.c:406
session_index_name
static const char * session_index_name(int index)
Definition: ssl.c:721
key_state::crypto_options
struct crypto_options crypto_options
Definition: ssl_common.h:225
CLEAR
#define CLEAR(x)
Definition: basic.h:33
IV_PROTO_REQUEST_PUSH
#define IV_PROTO_REQUEST_PUSH
Assume client will send a push request and server does not need to wait for a push-request to send a ...
Definition: ssl.h:83
session_id
Definition: session_id.h:38
tls_multi::multi_state
enum multi_status multi_state
Definition: ssl_common.h:609
options::ecdh_curve
const char * ecdh_curve
Definition: options.h:593
check_session_cipher
bool check_session_cipher(struct tls_session *session, struct options *options)
Checks if the cipher is allowed, otherwise returns false and reset the cipher to the config cipher.
Definition: ssl_ncp.c:511
reliable_ack::len
int len
Definition: reliable.h:63
PD_TLS_AUTH_HMAC_SIZE_MASK
#define PD_TLS_AUTH_HMAC_SIZE_MASK
Definition: ssl.h:519
tls_auth_standalone::frame
struct frame frame
Definition: ssl_pkt.h:81
ssl_util.h
options::cipher_list_tls13
const char * cipher_list_tls13
Definition: options.h:590
passbuf
static struct user_pass passbuf
Definition: ssl.c:241
key_state::send_reliable
struct reliable * send_reliable
Definition: ssl_common.h:233
free_key_ctx_bi
void free_key_ctx_bi(struct key_ctx_bi *ctx)
Definition: crypto.c:906
OPENVPN_STATE_AUTH
#define OPENVPN_STATE_AUTH
Definition: manage.h:481
env_set::list
struct env_item * list
Definition: env_set.h:44
session_move_active
static void session_move_active(struct tls_multi *multi, struct tls_session *session, struct link_socket_info *to_link_socket_info, struct key_state *ks)
Moves the key to state to S_ACTIVE and also advances the multi_state state machine if this is the ini...
Definition: ssl.c:2420
key_state_read_plaintext
int key_state_read_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Extract plaintext data from the TLS module.
Definition: ssl_openssl.c:2039
key_ctx_bi
Container for two sets of OpenSSL cipher and/or HMAC contexts for both sending and receiving directio...
Definition: crypto.h:217
TM_ACTIVE
#define TM_ACTIVE
Active tls_session.
Definition: ssl_common.h:526
platform_stat_t
struct _stat platform_stat_t
Definition: platform.h:146
string_alloc
char * string_alloc(const char *str, struct gc_arena *gc)
Definition: buffer.c:693
secure_memzero
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition: buffer.h:414
write_outgoing_tls_ciphertext
static bool write_outgoing_tls_ciphertext(struct tls_session *session, bool *continue_tls_process)
Definition: ssl.c:2600
SC_ECHO
#define SC_ECHO
Definition: misc.h:93
options::dh_file
const char * dh_file
Definition: options.h:579
tls_wrap_ctx::opt
struct crypto_options opt
Crypto state.
Definition: ssl_common.h:270
tls_update_remote_addr
void tls_update_remote_addr(struct tls_multi *multi, const struct link_socket_actual *addr)
Updates remote address in TLS sessions.
Definition: ssl.c:3989
tls_multi::remote_usescomp
bool remote_usescomp
remote announced comp-lzo in OCC string
Definition: ssl_common.h:667
P_CONTROL_HARD_RESET_CLIENT_V3
#define P_CONTROL_HARD_RESET_CLIENT_V3
Definition: ssl_pkt.h:55
ssl_verify.h
link_socket_actual_defined
static bool link_socket_actual_defined(const struct link_socket_actual *act)
Definition: socket.h:713
CAS_RECONNECT_PENDING
@ CAS_RECONNECT_PENDING
session has already successful established (CAS_CONNECT_DONE) but has a reconnect and needs to redo s...
Definition: ssl_common.h:566
ASSERT
#define ASSERT(x)
Definition: error.h:201
CAS_NOT_CONNECTED
@ CAS_NOT_CONNECTED
Definition: ssl_common.h:559
tls_session_soft_reset
void tls_session_soft_reset(struct tls_multi *tls_multi)
Definition: ssl.c:1760
generate_key_expansion_tls_export
static bool generate_key_expansion_tls_export(struct tls_session *session, struct key2 *key2)
Definition: ssl.c:1383
tls_options::remote_options
const char * remote_options
Definition: ssl_common.h:310
MAX_HMAC_KEY_LENGTH
#define MAX_HMAC_KEY_LENGTH
Definition: crypto_backend.h:495
P_CONTROL_SOFT_RESET_V1
#define P_CONTROL_SOFT_RESET_V1
Definition: ssl_pkt.h:44
key_method_2_write
static bool key_method_2_write(struct buffer *buf, struct tls_multi *multi, struct tls_session *session)
Handle the writing of key data, peer-info, username/password, OCC to the TLS control channel (clearte...
Definition: ssl.c:2021
buf_advance
static bool buf_advance(struct buffer *buf, int size)
Definition: buffer.h:636
tls_x509_clear_env
void tls_x509_clear_env(struct env_set *es)
Remove any X509_ env variables from env_set es.
Definition: ssl_verify.c:1836
control_packet_needs_wkc
static bool control_packet_needs_wkc(const struct key_state *ks)
Definition: ssl.c:2567
get_key_scan
static struct key_state * get_key_scan(struct tls_multi *multi, int index)
gets an item of key_state objects in the order they should be scanned by data channel modules.
Definition: ssl_common.h:696
reliable_get_num_output_sequenced_available
int reliable_get_num_output_sequenced_available(struct reliable *rel)
Counts the number of free buffers in output that can be potentially used for sending.
Definition: reliable.c:552
buf_write_u16
static bool buf_write_u16(struct buffer *dest, uint16_t data)
Definition: buffer.h:716
reliable_mark_active_outgoing
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:791
tls_options
Definition: ssl_common.h:293
ssl_clean_auth_token
bool ssl_clean_auth_token(void)
Definition: ssl.c:356
tls_root_ctx::crl_last_size
off_t crl_last_size
size of last loaded CRL
Definition: ssl_mbedtls.h:115
tls_session_init
static void tls_session_init(struct tls_multi *multi, struct tls_session *session)
Initialize a tls_session structure.
Definition: ssl.c:960
BLEN
#define BLEN(buf)
Definition: buffer.h:127
COMP_F_MIGRATE
#define COMP_F_MIGRATE
Definition: comp.h:40
PERF_TLS_MULTI_PROCESS
#define PERF_TLS_MULTI_PROCESS
Definition: perf.h:42
ssl_put_auth_challenge
void ssl_put_auth_challenge(const char *cr_str)
Definition: ssl.c:392
SID_SIZE
#define SID_SIZE
Definition: session_id.h:45
SSLF_OPT_VERIFY
#define SSLF_OPT_VERIFY
Definition: ssl_common.h:408
reliable_get_buf_output_sequenced
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:583
purge_user_pass
void purge_user_pass(struct user_pass *up, const bool force)
Definition: misc.c:473
tls_multi::to_link_addr
struct link_socket_actual to_link_addr
Definition: ssl_common.h:605
buf_write_prepend
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Definition: buffer.h:698
reliable_not_replay
bool reliable_not_replay(const struct reliable *rel, packet_id_type id)
Check that a received packet's ID is not a replay.
Definition: reliable.c:489
S_ACTIVE
#define S_ACTIVE
Operational key_state state immediately after negotiation has completed while still within the handsh...
Definition: ssl_common.h:93
buf_write_u8
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Definition: buffer.h:710
OPENVPN_STATE_WAIT
#define OPENVPN_STATE_WAIT
Definition: manage.h:480
frame::payload_size
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
Definition: mtu.h:102
options::imported_protocol_flags
unsigned int imported_protocol_flags
Definition: options.h:705
auth_challenge
static char * auth_challenge
Definition: ssl.c:276
key_source2_randomize_write
static bool key_source2_randomize_write(struct key_source2 *k2, struct buffer *buf, bool server)
Definition: ssl.c:1662
init_ssl_lib
void init_ssl_lib(void)
Definition: ssl.c:221
GET_USER_PASS_DYNAMIC_CHALLENGE
#define GET_USER_PASS_DYNAMIC_CHALLENGE
Definition: misc.h:115
tls_session::key
struct key_state key[KS_SIZE]
Definition: ssl_common.h:506
key_state::remote_addr
struct link_socket_actual remote_addr
Definition: ssl_common.h:223
update_time
static void update_time(void)
Definition: otime.h:77
key_source::random1
uint8_t random1[32]
Seed used for master secret generation, provided by both client and server.
Definition: ssl_common.h:117
SSLF_CRL_VERIFY_DIR
#define SSLF_CRL_VERIFY_DIR
Definition: ssl_common.h:409
is_hard_reset_method2
bool is_hard_reset_method2(int op)
Given a key_method, return true if opcode represents the one of the hard_reset op codes for key-metho...
Definition: ssl.c:760
options_string_extract_option
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:4644
ALLOC_ARRAY_CLEAR_GC
#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc)
Definition: buffer.h:1088
tls_multi::opt
struct tls_options opt
Definition: ssl_common.h:593
session_id_read
static bool session_id_read(struct session_id *sid, struct buffer *buf)
Definition: session_id.h:61
INCR_SENT
#define INCR_SENT
Definition: ssl.c:91
misc.h
ssl_set_auth_token
void ssl_set_auth_token(const char *token)
Definition: ssl.c:341
frame_calculate_dynamic
void frame_calculate_dynamic(struct frame *frame, struct key_type *kt, const struct options *options, struct link_socket_info *lsi)
Set the –mssfix option.
Definition: mss.c:335
route.h
reliable_send_timeout
interval_t reliable_send_timeout(const struct reliable *rel)
Determined how many seconds until the earliest resend should be attempted.
Definition: reliable.c:721
M_WARN
#define M_WARN
Definition: error.h:97
perf_pop
static void perf_pop(void)
Definition: perf.h:82
key_state_free
static void key_state_free(struct key_state *ks, bool clear)
Cleanup a key_state structure.
Definition: ssl.c:884
OPENVPN_MAX_HMAC_SIZE
#define OPENVPN_MAX_HMAC_SIZE
Definition: crypto_backend.h:48
tls_pre_decrypt_state
struct that stores the temporary data for the tls lite decrypt functions
Definition: ssl_pkt.h:104
ALLOC_OBJ_CLEAR_GC
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Definition: buffer.h:1103
reliable_mark_deleted
void reliable_mark_deleted(struct reliable *rel, struct buffer *buf)
Remove an entry from a reliable structure.
Definition: reliable.c:818
key_ctx
Container for one set of cipher and/or HMAC contexts.
Definition: crypto.h:162
tls_multi::n_hard_errors
int n_hard_errors
Definition: ssl_common.h:614
tls_version_max
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:213
tls_send_payload
bool tls_send_payload(struct key_state *ks, const uint8_t *data, int size)
Definition: ssl.c:3929
tls_multi::locked_cn
char * locked_cn
Definition: ssl_common.h:620
write_control_auth
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_pkt.c:168
options::pkcs12_file_inline
bool pkcs12_file_inline
Definition: options.h:588
options
Definition: options.h:236
KEY_EXPANSION_ID
#define KEY_EXPANSION_ID
Definition: ssl.h:49
crypto_init_lib
void crypto_init_lib(void)
Definition: crypto_openssl.c:193
tls_ctx_free
void tls_ctx_free(struct tls_root_ctx *ctx)
Frees the library-specific TLSv1 context.
Definition: ssl_openssl.c:146
tls_options::local_options
const char * local_options
Definition: ssl_common.h:309
key_state::session_id_remote
struct session_id session_id_remote
Definition: ssl_common.h:222
tls_multi::dco_peer_id
int dco_peer_id
This is the handle that DCO uses to identify this session with the kernel.
Definition: ssl_common.h:687
TLS_AUTHENTICATION_FAILED
@ TLS_AUTHENTICATION_FAILED
Definition: ssl_verify.h:71
key_state::established
time_t established
Definition: ssl_common.h:216
tls_options::handshake_window
int handshake_window
Definition: ssl_common.h:329
tls_ctx_set_cert_profile
void tls_ctx_set_cert_profile(struct tls_root_ctx *ctx, const char *profile)
Set the TLS certificate profile.
Definition: ssl_openssl.c:527
static_challenge_info::flags
unsigned int flags
Definition: misc.h:94
S_UNDEF
#define S_UNDEF
Undefined state, used after a key_state is cleaned up.
Definition: ssl_common.h:78
route_gateway_info::flags
unsigned int flags
Definition: route.h:153
key_source::pre_master
uint8_t pre_master[48]
Random used for master secret generation, provided only by client OpenVPN peer.
Definition: ssl_common.h:114
MF_EXTERNAL_KEY
#define MF_EXTERNAL_KEY
Definition: manage.h:37
buf_read_u16
static int buf_read_u16(struct buffer *buf)
Definition: buffer.h:821
route_gateway_info::hwaddr
uint8_t hwaddr[6]
Definition: route.h:163
TM_LAME_DUCK
#define TM_LAME_DUCK
Old tls_session.
Definition: ssl_common.h:529
tls_prepend_opcode_v2
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:3894
session_skip_to_pre_start
bool session_skip_to_pre_start(struct tls_session *session, struct tls_pre_decrypt_state *state, struct link_socket_actual *from)
Definition: ssl.c:2464
get_user_pass
static bool get_user_pass(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags)
Retrieves the user credentials from various sources depending on the flags.
Definition: misc.h:147
EARLY_NEG_START
#define EARLY_NEG_START
Definition: ssl_pkt.h:308
user_pass::nocache
bool nocache
Definition: misc.h:62
check_key
bool check_key(struct key *key, const struct key_type *kt)
Definition: crypto.c:931
TLSMP_INACTIVE
#define TLSMP_INACTIVE
Definition: ssl.h:221
tls_multi::save_ks
struct key_state * save_ks
Definition: ssl_common.h:599
key_source_print
static void key_source_print(const struct key_source *k, const char *prefix)
Definition: ssl.c:1261
reliable_ack_print
const char * reliable_ack_print(struct buffer *buf, bool verbose, struct gc_arena *gc)
Definition: reliable.c:314
write_empty_string
static bool write_empty_string(struct buffer *buf)
Definition: ssl.c:1770
key_source2_print
static void key_source2_print(const struct key_source2 *k)
Definition: ssl.c:1287
KS_AUTH_TRUE
@ KS_AUTH_TRUE
Key state is authenticated.
Definition: ssl_common.h:147
packet_id_read
bool packet_id_read(struct packet_id_net *pin, struct buffer *buf, bool long_form)
Definition: packet_id.c:299
static_challenge_info::challenge_text
const char * challenge_text
Definition: misc.h:96
cipher_ctx_mode_aead
bool cipher_ctx_mode_aead(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported AEAD mode cipher.
Definition: crypto_openssl.c:922
packet_id_type
uint32_t packet_id_type
Definition: packet_id.h:44
management_enable_def_auth
static bool management_enable_def_auth(const struct management *man)
Definition: manage.h:461
ntohpid
#define ntohpid(x)
Definition: packet_id.h:59
options::pkcs12_file
const char * pkcs12_file
Definition: options.h:587
key_state_ssl_init
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:1925
key_state_export_keying_material
bool key_state_export_keying_material(struct tls_session *session, const char *label, size_t label_size, void *ekm, size_t ekm_size)
Keying Material Exporters [RFC 5705] allows additional keying material to be derived from existing TL...
Definition: ssl_openssl.c:162
tls_multi::peer_info
char * peer_info
Definition: ssl_common.h:640
tls_session_update_crypto_params
bool tls_session_update_crypto_params(struct tls_multi *multi, struct tls_session *session, struct options *options, struct frame *frame, struct frame *frame_fragment, struct link_socket_info *lsi)
Update TLS session crypto parameters (cipher and auth) and derive data channel keys based on the supp...
Definition: ssl.c:1626
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
packet_id_net_print
const char * packet_id_net_print(const struct packet_id_net *pin, bool print_timestamp, struct gc_arena *gc)
Definition: packet_id.c:389
key_state_write_plaintext
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:1980
OPENVPN_MAX_IV_LENGTH
#define OPENVPN_MAX_IV_LENGTH
Maximum length of an IV.
Definition: crypto_mbedtls.h:52
INCR_ERROR
#define INCR_ERROR
Definition: ssl.c:94
TLS_RELIABLE_N_SEND_BUFFERS
#define TLS_RELIABLE_N_SEND_BUFFERS
Definition: ssl_pkt.h:70
tls_options::gremlin
int gremlin
Definition: ssl_common.h:428
static_challenge_info
Definition: misc.h:92
options::management_certificate
const char * management_certificate
Definition: options.h:441
key_ctx::implicit_iv_len
size_t implicit_iv_len
The length of implicit_iv.
Definition: crypto.h:168
protocol_dump
const char * protocol_dump(struct buffer *buffer, unsigned int flags, struct gc_arena *gc)
Definition: ssl.c:4044
reliable::packet_id
packet_id_type packet_id
Definition: reliable.h:95
rand_bytes
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
Definition: crypto_openssl.c:571
buffer_list_new
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
Definition: buffer.c:1185
CO_USE_DYNAMIC_TLS_CRYPT
#define CO_USE_DYNAMIC_TLS_CRYPT
Bit-flag indicating that renegotiations are using tls-crypt with a TLS-EKM derived key.
Definition: crypto.h:278
auth_deferred_expire_window
static int auth_deferred_expire_window(const struct tls_options *o)
Definition: ssl.c:2349
D_TLS_ERRORS
#define D_TLS_ERRORS
Definition: errlevel.h:59
reliable_ack_empty
static bool reliable_ack_empty(struct reliable_ack *ack)
Check whether an acknowledgment structure contains any packet IDs to be acknowledged.
Definition: reliable.h:176
tls_ctx_load_cryptoapi
void tls_ctx_load_cryptoapi(struct tls_root_ctx *ctx, const char *cryptoapi_cert)
Definition: ssl_openssl.c:923
key_type
Definition: crypto.h:139
tls_ctx_set_tls_groups
void tls_ctx_set_tls_groups(struct tls_root_ctx *ctx, const char *groups)
Set the (elliptic curve) group allowed for signatures and key exchange.
Definition: ssl_openssl.c:565
tls_session_free
static void tls_session_free(struct tls_session *session, bool clear)
Clean up a tls_session structure.
Definition: ssl.c:1034
options::crl_file_inline
bool crl_file_inline
Definition: options.h:599
IV_PROTO_DNS_OPTION
#define IV_PROTO_DNS_OPTION
Supports the –dns option introduced in version 2.6.
Definition: ssl.h:97
buf_write
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Definition: buffer.h:686
TLS_VER_1_2
#define TLS_VER_1_2
Definition: ssl_backend.h:107
TLS_OPTIONS_LEN
#define TLS_OPTIONS_LEN
Definition: ssl.h:68
link_socket_set_outgoing_addr
static void link_socket_set_outgoing_addr(struct link_socket_info *info, const struct link_socket_actual *act, const char *common_name, struct env_set *es)
Definition: socket.h:987
ssl.h
ks_auth_name
static const char * ks_auth_name(enum ks_auth_state auth)
Definition: ssl.c:702
tls_session
Security parameter state of a single session within a VPN tunnel.
Definition: ssl_common.h:468
key_state::state
int state
Definition: ssl_common.h:197
key_ctx::implicit_iv
uint8_t implicit_iv[OPENVPN_MAX_IV_LENGTH]
The implicit part of the IV.
Definition: crypto.h:166
key_state::mda_key_id
unsigned int mda_key_id
Definition: ssl_common.h:251
frame::buf
struct frame::@6 buf
enable_auth_user_pass
void enable_auth_user_pass()
Definition: ssl.c:280
CAS_CONNECT_DONE
@ CAS_CONNECT_DONE
Definition: ssl_common.h:570
syshead.h
key_ctx_update_implicit_iv
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:1516
options::dh_file_inline
bool dh_file_inline
Definition: options.h:580
BPTR
#define BPTR(buf)
Definition: buffer.h:124
pkcs11.h
CONTROL_SEND_ACK_MAX
#define CONTROL_SEND_ACK_MAX
Definition: ssl.h:55
options::management_flags
unsigned int management_flags
Definition: options.h:444
session_id::id
uint8_t id[8]
Definition: session_id.h:40
auth_user_pass
static struct user_pass auth_user_pass
Definition: ssl.c:272
packet_id_print_type
unsigned int packet_id_print_type
Definition: packet_id.h:91
session_id_defined
static bool session_id_defined(const struct session_id *sid1)
Definition: session_id.h:55
options::cert_file_inline
bool cert_file_inline
Definition: options.h:582
reliable_send
struct buffer * reliable_send(struct reliable *rel, int *opcode)
Get the next packet to send to the remote peer.
Definition: reliable.c:663
D_PUSH
#define D_PUSH
Definition: errlevel.h:83
cipher_kt_mode_ofb_cfb
bool cipher_kt_mode_ofb_cfb(const char *ciphername)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
Definition: crypto_openssl.c:783
IV_PROTO_AUTH_PENDING_KW
#define IV_PROTO_AUTH_PENDING_KW
Supports signaling keywords with AUTH_PENDING, e.g.
Definition: ssl.h:89
auth_user_pass_enabled
static bool auth_user_pass_enabled
Definition: ssl.c:271
tls_auth_standalone_free
void tls_auth_standalone_free(struct tls_auth_standalone *tas)
Frees a standalone tls-auth verification object.
Definition: ssl.c:1198
UP_TYPE_AUTH
#define UP_TYPE_AUTH
Definition: ssl_common.h:41
tls_ctx_restrict_ciphers_tls13
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:498
key_source2
Container for both halves of random material to be used in key method 2 data channel key generation.
Definition: ssl_common.h:130
cipher_kt_insecure
bool cipher_kt_insecure(const char *ciphername)
Returns true if we consider this cipher to be insecure.
Definition: crypto_openssl.c:738
TLSMP_KILL
#define TLSMP_KILL
Definition: ssl.h:223
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
key_state::key_id
int key_id
Key id for this key_state, inherited from struct tls_session.
Definition: ssl_common.h:205
key_state_rm_auth_control_files
void key_state_rm_auth_control_files(struct auth_deferred_status *ads)
Removes auth_pending and auth_control files from file system and key_state structure.
Definition: ssl_verify.c:967
key_is_external
bool key_is_external(const struct options *options)
Definition: options.c:5710
setenv_str
void setenv_str(struct env_set *es, const char *name, const char *value)
Definition: env_set.c:283
key_type::cipher
const char * cipher
const name of the cipher
Definition: crypto.h:141
PD_TLS_CRYPT
#define PD_TLS_CRYPT
Definition: ssl.h:523
key_state::key_src
struct key_source2 * key_src
Definition: ssl_common.h:227
pem_password_setup
void pem_password_setup(const char *auth_file)
Definition: ssl.c:244
IV_PROTO_DYN_TLS_CRYPT
#define IV_PROTO_DYN_TLS_CRYPT
Support to dynamic tls-crypt (renegotiation with TLS-EKM derived tls-crypt key)
Definition: ssl.h:107
options::ca_file
const char * ca_file
Definition: options.h:576
EXPORT_KEY_DATA_LABEL
#define EXPORT_KEY_DATA_LABEL
Definition: ssl_backend.h:383
tls_auth_standalone_init
struct tls_auth_standalone * tls_auth_standalone_init(struct tls_options *tls_options, struct gc_arena *gc)
Definition: ssl.c:1172
read_incoming_tls_ciphertext
static bool read_incoming_tls_ciphertext(struct buffer *buf, struct key_state *ks, bool *continue_tls_process)
Read incoming ciphertext and passes it to the buffer of the SSL library.
Definition: ssl.c:2539
tls_ctx_check_cert_time
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:625
strncpynt
static void strncpynt(char *dest, const char *src, size_t maxlen)
Definition: buffer.h:361
win32_version_string
const char * win32_version_string(struct gc_arena *gc, bool add_name)
Definition: win32.c:1409
auth_token
static struct user_pass auth_token
Definition: ssl.c:273
env_set
Definition: env_set.h:42
get_default_gateway
void get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx)
Definition: route.c:2674
reliable
The reliability layer storage structure for one VPN tunnel's control channel in one direction.
Definition: reliable.h:91
tls_multi_free
void tls_multi_free(struct tls_multi *multi, bool clear)
Cleanup a tls_multi structure and free associated memory allocations.
Definition: ssl.c:1227
buffer_list_pop
void buffer_list_pop(struct buffer_list *ol)
Definition: buffer.c:1331
key_state::plugin_auth
struct auth_deferred_status plugin_auth
Definition: ssl_common.h:256
platform_stat
int platform_stat(const char *path, platform_stat_t *buf)
Definition: platform.c:540
key_state::initial
time_t initial
Definition: ssl_common.h:215
key_state::paybuf
struct buffer_list * paybuf
Holds outgoing message for the control channel until ks->state reaches S_ACTIVE.
Definition: ssl_common.h:240
options::priv_key_file_inline
bool priv_key_file_inline
Definition: options.h:586
counter_format
#define counter_format
Definition: common.h:31
perf.h
set_auth_token
void set_auth_token(struct user_pass *up, struct user_pass *tk, const char *token)
Sets the auth-token to token.
Definition: misc.c:494
dco_enabled
static bool dco_enabled(const struct options *o)
Returns whether the current configuration has dco enabled.
Definition: options.h:908
tls_auth_standalone::tls_wrap
struct tls_wrap_ctx tls_wrap
Definition: ssl_pkt.h:79
free_buf
void free_buf(struct buffer *buf)
Definition: buffer.c:183
dco.h
common.h
ssl_set_auth_nocache
void ssl_set_auth_nocache(void)
Definition: ssl.c:331
reliable_entry::packet_id
packet_id_type packet_id
Definition: reliable.h:79
check_debug_level
static bool check_debug_level(unsigned int level)
Definition: error.h:226
tls_multi::n_sessions
int n_sessions
Number of sessions negotiated thus far.
Definition: ssl_common.h:607
reliable_ack_read_packet_id
bool reliable_ack_read_packet_id(struct buffer *buf, packet_id_type *pid)
Read the packet ID of a received packet.
Definition: reliable.c:114
tls_select_encryption_key
struct key_state * tls_select_encryption_key(struct tls_multi *multi)
Selects the primary encryption that should be used to encrypt data of an outgoing packet.
Definition: ssl.c:3820
key_state::plaintext_write_buf
struct buffer plaintext_write_buf
Definition: ssl_common.h:230
tls_multi_init_finalize
void tls_multi_init_finalize(struct tls_multi *multi, int tls_mtu)
Finalize initialization of a tls_multi structure.
Definition: ssl.c:1158
reliable_wont_break_sequentiality
bool reliable_wont_break_sequentiality(const struct reliable *rel, packet_id_type id)
Check that a received packet's ID can safely be stored in the reliable structure's processing window.
Definition: reliable.c:516
check_session_buf_not_used
static void check_session_buf_not_used(struct buffer *to_link, struct tls_session *session)
This is a safe guard function to double check that a buffer from a session is not used in a session t...
Definition: ssl.c:3021
options::tls_groups
const char * tls_groups
Definition: options.h:591
tls_multi_init
struct tls_multi * tls_multi_init(struct tls_options *tls_options)
Allocate and initialize a tls_multi structure.
Definition: ssl.c:1143
buf_len
static int buf_len(const struct buffer *buf)
Definition: buffer.h:253
D_MULTI_DROPPED
#define D_MULTI_DROPPED
Definition: errlevel.h:101
tls_prepend_opcode_v1
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:3880
tls_ctx_load_ca
void tls_ctx_load_ca(struct tls_root_ctx *ctx, const char *ca_file, bool ca_file_inline, const char *ca_path, bool tls_server)
Load certificate authority certificates from the given file or path.
Definition: ssl_openssl.c:1557
IV_PROTO_NCP_P2P
#define IV_PROTO_NCP_P2P
Support doing NCP in P2P mode.
Definition: ssl.h:94
key_state::script_auth
struct auth_deferred_status script_auth
Definition: ssl_common.h:257
gc_malloc
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition: buffer.c:380
max_int
static int max_int(int x, int y)
Definition: integer.h:76
D_TLS_DEBUG_MED
#define D_TLS_DEBUG_MED
Definition: errlevel.h:157
TLS_CRYPT_TAG_SIZE
#define TLS_CRYPT_TAG_SIZE
Definition: tls_crypt.h:89
tls_root_ctx
Structure that wraps the TLS context.
Definition: ssl_mbedtls.h:104
ssl_purge_auth
void ssl_purge_auth(const bool auth_user_pass_only)
Definition: ssl.c:367
tls_rec_payload
bool tls_rec_payload(struct tls_multi *multi, struct buffer *buf)
Definition: ssl.c:3963
key_state::must_die
time_t must_die
Definition: ssl_common.h:218
packet_id::send
struct packet_id_send send
Definition: packet_id.h:202
user_pass::token_defined
bool token_defined
Definition: misc.h:61
CAS_WAITING_OPTIONS_IMPORT
@ CAS_WAITING_OPTIONS_IMPORT
client with pull or p2p waiting for first time options import
Definition: ssl_common.h:565
key_source2::client
struct key_source client
Random provided by client.
Definition: ssl_common.h:131
D_SHOW_KEY_SOURCE
#define D_SHOW_KEY_SOURCE
Definition: errlevel.h:122
cert_hash_free
void cert_hash_free(struct cert_hash_set *chs)
Frees the given set of certificate hashes.
Definition: ssl_verify.c:220
status
static SERVICE_STATUS status
Definition: interactive.c:52
packet_id_size
static int packet_id_size(bool long_form)
Definition: packet_id.h:310
auth_set_client_reason
void auth_set_client_reason(struct tls_multi *multi, const char *client_reason)
Sets the reason why authentication of a client failed.
Definition: ssl_verify.c:817
CO_PACKET_ID_LONG_FORM
#define CO_PACKET_ID_LONG_FORM
Bit-flag indicating whether to use OpenVPN's long packet ID format.
Definition: crypto.h:250
print_link_socket_actual
const char * print_link_socket_actual(const struct link_socket_actual *act, struct gc_arena *gc)
Definition: socket.c:2820
packet_id_persist_load_obj
void packet_id_persist_load_obj(const struct packet_id_persist *p, struct packet_id *pid)
Definition: packet_id.c:522
S_PRE_START
#define S_PRE_START
Waiting for the remote OpenVPN peer to acknowledge during the initial three-way handshake.
Definition: ssl_common.h:83
management
Definition: manage.h:335
min_int
static int min_int(int x, int y)
Definition: integer.h:89
gc_free
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1039
PD_VERBOSE
#define PD_VERBOSE
Definition: ssl.h:522
tls_options::server
bool server
Definition: ssl_common.h:302
reliable_can_get
bool reliable_can_get(const struct reliable *rel)
Check whether a reliable structure has any free buffers available for use.
Definition: reliable.c:470
BUF_SIZE
#define BUF_SIZE(f)
Definition: mtu.h:172
tls_options::replay_window
int replay_window
Definition: ssl_common.h:357
GET_USER_PASS_PASSWORD_ONLY
#define GET_USER_PASS_PASSWORD_ONLY
Definition: misc.h:109
options::chroot_dir
const char * chroot_dir
Definition: options.h:362
USER_PASS_LEN
#define USER_PASS_LEN
Definition: misc.h:68
parse_early_negotiation_tlvs
static bool parse_early_negotiation_tlvs(struct buffer *buf, struct key_state *ks)
Parses the TLVs (type, length, value) in the early negotiation.
Definition: ssl.c:2490
mss.h
KS_AUTH_DEFERRED
@ KS_AUTH_DEFERRED
Key state authentication is being deferred, by async auth.
Definition: ssl_common.h:145
TLS_VER_1_3
#define TLS_VER_1_3
Definition: ssl_backend.h:108
socket.h
P_ACK_V1
#define P_ACK_V1
Definition: ssl_pkt.h:46
options::cryptoapi_cert
const char * cryptoapi_cert
Definition: options.h:621
options_string_compat_lzo
const char * options_string_compat_lzo(const char *options, struct gc_arena *gc)
Takes a locally produced OCC string for TLS server mode and modifies as if the option comp-lzo was en...
Definition: ssl_util.c:78
ALLOC_OBJ_CLEAR
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition: buffer.h:1066
crypto_options::key_ctx_bi
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
Definition: crypto.h:232
openvpn_PRF
static bool 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:1294
free_ssl_lib
void free_ssl_lib(void)
Definition: ssl.c:229
reliable_ack_read
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:149
now
time_t now
Definition: otime.c:34
S_INITIAL
#define S_INITIAL
Initial key_state state after initialization by key_state_init() before start of three-way handshake.
Definition: ssl_common.h:80
wipe_auth_token
void wipe_auth_token(struct tls_multi *multi)
Wipes the authentication token out of the memory, frees and cleans up related buffers and flags.
Definition: auth_token.c:403
packet_id_format
#define packet_id_format
Definition: packet_id.h:90
PD_TLS
#define PD_TLS
Definition: ssl.h:521
P_CONTROL_V1
#define P_CONTROL_V1
Definition: ssl_pkt.h:45
env_item
Definition: env_set.h:37
key_state::auth_deferred_expire
time_t auth_deferred_expire
Definition: ssl_common.h:248
OPENVPN_PLUGIN_FUNC_SUCCESS
#define OPENVPN_PLUGIN_FUNC_SUCCESS
Definition: openvpn-plugin.h:148
config.h
handle_data_channel_packet
static void handle_data_channel_packet(struct tls_multi *multi, const struct link_socket_actual *from, struct buffer *buf, struct crypto_options **opt, bool floated, const uint8_t **ad_start)
Check the keyid of the an incoming data channel packet and return the matching crypto parameters in o...
Definition: ssl.c:3352
session_id_random
void session_id_random(struct session_id *sid)
Definition: session_id.c:49
options::extra_certs_file
const char * extra_certs_file
Definition: options.h:583
ssl_ncp.h
key_state::peer_last_packet
time_t peer_last_packet
Definition: ssl_common.h:219
pem_password_callback
int pem_password_callback(char *buf, int size, int rwflag, void *u)
Callback to retrieve the user's password.
Definition: ssl.c:253
OPENVPN_PLUGIN_TLS_FINAL
#define OPENVPN_PLUGIN_TLS_FINAL
Definition: openvpn-plugin.h:127
packet_id_net
Definition: packet_id.h:194
buffer_list_free
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition: buffer.c:1194
tls_multi::remote_ciphername
char * remote_ciphername
cipher specified in peer's config file
Definition: ssl_common.h:666
tls_multi::peer_id
uint32_t peer_id
Definition: ssl_common.h:663
tls_session_get_tls_wrap
static struct tls_wrap_ctx * tls_session_get_tls_wrap(struct tls_session *session, int key_id)
Determines if the current session should use the renegotiation tls wrap struct instead the normal one...
Definition: ssl_pkt.h:285
init_key_dco_bi
static int init_key_dco_bi(struct tls_multi *multi, struct key_state *ks, const struct key2 *key2, int key_direction, const char *ciphername, bool server)
Definition: dco.h:316
key2
Container for bidirectional cipher and HMAC key material.
Definition: crypto.h:179
reliable_mark_active_incoming
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:758
reliable_ack_outstanding
static int reliable_ack_outstanding(struct reliable_ack *ack)
Returns the number of packets that need to be acked.
Definition: reliable.h:189
reliable::size
int size
Definition: reliable.h:93
reliable_get_entry_sequenced
struct reliable_entry * reliable_get_entry_sequenced(struct reliable *rel)
Get the buffer of the next sequential and active entry.
Definition: reliable.c:619
format_hex
static char * format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
Definition: buffer.h:523
tls_post_encrypt
void tls_post_encrypt(struct tls_multi *multi, struct buffer *buf)
Perform some accounting for the key state used.
Definition: ssl.c:3909
CO_IGNORE_PACKET_ID
#define CO_IGNORE_PACKET_ID
Bit-flag indicating whether to ignore the packet ID of a received packet.
Definition: crypto.h:253
reliable_can_send
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:635
check_malloc_return
static void check_malloc_return(void *p)
Definition: buffer.h:1109
user_pass::password
char password[USER_PASS_LEN]
Definition: misc.h:72
reliable_init
void reliable_init(struct reliable *rel, int buf_size, int offset, int array_size, bool hold)
Initialize a reliable structure.
Definition: reliable.c:358
TM_INITIAL
#define TM_INITIAL
As yet un-trusted tls_session being negotiated.
Definition: ssl_common.h:527
options::priv_key_file
const char * priv_key_file
Definition: options.h:585
GET_USER_PASS_INLINE_CREDS
#define GET_USER_PASS_INLINE_CREDS
Definition: misc.h:119
init_key_type
void init_key_type(struct key_type *kt, const char *ciphername, const char *authname, bool tls_mode, bool warn)
Initialize a key_type structure with.
Definition: crypto.c:744
tls_pre_encrypt
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:3847
session
Definition: keyingmaterialexporter.c:56
datagram_overhead
static int datagram_overhead(sa_family_t af, int proto)
Definition: socket.h:616
buf_read_u8
static int buf_read_u8(struct buffer *buf)
Definition: buffer.h:808
plugin_defined
bool plugin_defined(const struct plugin_list *pl, const int type)
Definition: plugin.c:920
P_CONTROL_HARD_RESET_SERVER_V1
#define P_CONTROL_HARD_RESET_SERVER_V1
Definition: ssl_pkt.h:43
management_query_cert
char * management_query_cert(struct management *man, const char *cert_name)
Definition: manage.c:3774
options::crl_file
const char * crl_file
Definition: options.h:598
crypto_options::packet_id
struct packet_id packet_id
Current packet ID state for both sending and receiving directions.
Definition: crypto.h:236
tls_ctx_reload_crl
static void tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, bool crl_file_inline)
Load (or possibly reload) the CRL file into the SSL context.
Definition: ssl.c:447
S_GOT_KEY
#define S_GOT_KEY
Local OpenVPN process has received the remote's part of the key material.
Definition: ssl_common.h:90
tls_session_user_pass_enabled
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:928
key_state::plaintext_read_buf
struct buffer plaintext_read_buf
Definition: ssl_common.h:229
tls_multi::locked_username
char * locked_username
Definition: ssl_common.h:621
reliable_send_purge
void reliable_send_purge(struct reliable *rel, const struct reliable_ack *ack)
Remove acknowledged packets from a reliable structure.
Definition: reliable.c:409
key_state_write_plaintext_const
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:1996
crypto_options::flags
unsigned int flags
Bit-flags determining behavior of security operation functions.
Definition: crypto.h:283
tls_multi_init_set_options
void tls_multi_init_set_options(struct tls_multi *multi, const char *local, const char *remote)
Definition: ssl.c:1214
user_pass
Definition: misc.h:56
S_ERROR
#define S_ERROR
Error state.
Definition: ssl_common.h:77
auth_user_pass_setup
void auth_user_pass_setup(const char *auth_file, bool is_inline, const struct static_challenge_info *sci)
Definition: ssl.c:286
KS_SIZE
#define KS_SIZE
Size of the tls_session.key array.
Definition: ssl_common.h:448
CO_USE_TLS_KEY_MATERIAL_EXPORT
#define CO_USE_TLS_KEY_MATERIAL_EXPORT
Bit-flag indicating that data channel key derivation is done using TLS keying material export [RFC570...
Definition: crypto.h:262
key2::keys
struct key keys[2]
Two unidirectional sets of key material.
Definition: crypto.h:183
alloc_buf
struct buffer alloc_buf(size_t size)
Definition: buffer.c:62
tls_ctx_load_pkcs12
int tls_ctx_load_pkcs12(struct tls_root_ctx *ctx, const char *pkcs12_file, bool pkcs12_file_inline, bool load_ca_file)
Load PKCS #12 file for key, cert and (optionally) CA certs, and add to library-specific TLS context.
Definition: ssl_openssl.c:793
TLS_VER_BAD
#define TLS_VER_BAD
Parse a TLS version specifier.
Definition: ssl_backend.h:103
key_state::ack_write_buf
struct buffer ack_write_buf
Definition: ssl_common.h:231
crypto_options::pid_persist
struct packet_id_persist * pid_persist
Persistent packet ID state for keeping state between successive OpenVPN process startups.
Definition: crypto.h:245
setenv_del
void setenv_del(struct env_set *es, const char *name)
Definition: env_set.c:328
packet_id_close_to_wrapping
static bool packet_id_close_to_wrapping(const struct packet_id_send *p)
Definition: packet_id.h:316
export_user_keying_material
static void export_user_keying_material(struct key_state_ssl *ssl, struct tls_session *session)
Definition: ssl.c:2136
memdbg.h
packet_id_free
void packet_id_free(struct packet_id *p)
Definition: packet_id.c:102
UP_TYPE_PRIVATE_KEY
#define UP_TYPE_PRIVATE_KEY
Definition: ssl_common.h:42
key_method_2_read
static bool key_method_2_read(struct buffer *buf, struct tls_multi *multi, struct tls_session *session)
Handle reading key data, peer-info, username/password, OCC from the TLS control channel (cleartext).
Definition: ssl.c:2173
key_source::random2
uint8_t random2[32]
Seed used for key expansion, provided by both client and server.
Definition: ssl_common.h:120
key_state::initial_opcode
int initial_opcode
Definition: ssl_common.h:221
options::ciphername
const char * ciphername
Definition: options.h:557
tls_process
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:2876
IV_PROTO_AUTH_FAIL_TEMP
#define IV_PROTO_AUTH_FAIL_TEMP
Support for AUTH_FAIL,TEMP messages.
Definition: ssl.h:104
GET_USER_PASS_STATIC_CHALLENGE
#define GET_USER_PASS_STATIC_CHALLENGE
Definition: misc.h:116
TLSMP_RECONNECT
#define TLSMP_RECONNECT
Definition: ssl.h:224
reliable_ack
The acknowledgment structure in which packet IDs are stored for later acknowledgment.
Definition: reliable.h:61
PD_SHOW_DATA
#define PD_SHOW_DATA
Definition: ssl.h:520
msg
#define msg(flags,...)
Definition: error.h:150
P_DATA_V1
#define P_DATA_V1
Definition: ssl_pkt.h:47
ACK_SIZE
#define ACK_SIZE(n)
Definition: reliable.h:68
IV_PROTO_CC_EXIT_NOTIFY
#define IV_PROTO_CC_EXIT_NOTIFY
Support for explicit exit notify via control channel This also includes support for the protocol-flag...
Definition: ssl.h:101
verify_user_pass
void verify_user_pass(struct user_pass *up, struct tls_multi *multi, struct tls_session *session)
Main username/password verification entry point.
Definition: ssl_verify.c:1592
tls_clear_error
void tls_clear_error(void)
Clear the underlying SSL library's error state.
frame::tun_mtu
int tun_mtu
the (user) configured tun-mtu.
Definition: mtu.h:131
reliable_free
void reliable_free(struct reliable *rel)
Free allocated memory associated with a reliable structure and the pointer itself.
Definition: reliable.c:376
key_state_init
static void key_state_init(struct tls_session *session, struct key_state *ks)
Initialize a key_state structure.
Definition: ssl.c:795
tls_ctx_load_dh_params
void tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file, bool dh_file_inline)
Load Diffie Hellman Parameters, and load them into the library-specific TLS context.
Definition: ssl_openssl.c:661
MF_EXTERNAL_CERT
#define MF_EXTERNAL_CERT
Definition: manage.h:43
TLS_CHANNEL_BUF_SIZE
#define TLS_CHANNEL_BUF_SIZE
Definition: common.h:69
status.h
perf_push
static void perf_push(int type)
Definition: perf.h:78
tls_ctx_restrict_ciphers
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:436
buf_printf
bool buf_printf(struct buffer *buf, const char *format,...)
Definition: buffer.c:240
ssl_tls1_PRF
bool ssl_tls1_PRF(const uint8_t *seed, int seed_len, const uint8_t *secret, int secret_len, uint8_t *output, int output_len)
Calculates the TLS 1.0-1.1 PRF function.
Definition: crypto_openssl.c:1518
init_ssl
void init_ssl(const struct options *options, struct tls_root_ctx *new_ctx, bool in_chroot)
Build master SSL context object that serves for the whole of OpenVPN instantiation.
Definition: ssl.c:498
D_TLS_KEYSELECT
#define D_TLS_KEYSELECT
Definition: errlevel.h:146
key_state_read_ciphertext
int key_state_read_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Extract ciphertext data from the TLS module.
Definition: ssl_openssl.c:2010
tls_session_generate_dynamic_tls_crypt_key
bool tls_session_generate_dynamic_tls_crypt_key(struct tls_multi *multi, struct tls_session *session)
Generates a TLS-Crypt key to be used with dynamic tls-crypt using the TLS EKM exporter function.
Definition: tls_crypt.c:98
options_warning
void options_warning(char *actual, const char *expected)
Definition: options.c:4471
frame::headroom
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition: mtu.h:108
options::ca_file_inline
bool ca_file_inline
Definition: options.h:577
buf_copy_n
static bool buf_copy_n(struct buffer *dest, struct buffer *src, int n)
Definition: buffer.h:736
state_name
static const char * state_name(int state)
Definition: ssl.c:665
hmac_ctx_size
int hmac_ctx_size(hmac_ctx_t *ctx)
crypto_options
Security parameter state for processing data channel packets.
Definition: crypto.h:230
read_incoming_tls_plaintext
static bool read_incoming_tls_plaintext(struct key_state *ks, struct buffer *buf, interval_t *wakeup, bool *continue_tls_process)
Definition: ssl.c:2575
ssl_purge_auth_challenge
void ssl_purge_auth_challenge(void)
Definition: ssl.c:385
PROTO_UDP
@ PROTO_UDP
Definition: socket.h:555
P_CONTROL_HARD_RESET_SERVER_V2
#define P_CONTROL_HARD_RESET_SERVER_V2
Definition: ssl_pkt.h:52
link_socket_actual_match
static bool link_socket_actual_match(const struct link_socket_actual *a1, const struct link_socket_actual *a2)
Definition: socket.h:886
packet_opcode_name
static const char * packet_opcode_name(int op)
Definition: ssl_pkt.h:234
tls_auth_standalone
Definition: ssl_pkt.h:77
buffer::data
uint8_t * data
Pointer to the allocated memory.
Definition: buffer.h:68
TLV_TYPE_EARLY_NEG_FLAGS
#define TLV_TYPE_EARLY_NEG_FLAGS
Definition: ssl_pkt.h:315
tls_pre_decrypt
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:3455
reliable_entry
The structure in which the reliability layer stores a single incoming or outgoing packet.
Definition: reliable.h:74
cleanup
static int cleanup(void **state)
Definition: test_pkcs11.c:280