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