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