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