Stateless three-way handshake and control channel improvements
cover Stateless three-way handshake and control channel improvements
OpenVPN currently has a bit of a weakness in its early three way handshake
A single client reset packet (first packet of the handshake) will
- trigger creating session on the server side leading to poential ressource exhaustian
- make the server respond with 3 answers trying to get an ACK for its answer making it a amplification
This patch series intends to solve this problem and since the patches revolve a lot around control channel behaviour, I discovered and fixed a number of other weaknesses in the control channel implementation:
- Implementing an HMAC based scheme to offer a stateless three way handshake for the server that avoids the previous mentioned problems.
- Restricting control channel packet size is now possible without breaking the protocol (--tls-mtu)
- Server and client will now always repeat previous ACKs to avoid the peer resending a packet if an ACK is gone missing. (Similar to what cumulative ACKs in other protocols achieve).
Arne Schwabe (28):
Remove tls_init_control_channel_frame_parameters wrapper function Remove dead PID_TEST code
Move pre decrypt lite check to its own function
Add documentation for swap_hmac function
Extend tls_pre_decrypt_lite to return type of packet and keep state Move ssl function related to control channel wrap/unwrap to
ssl_pkt.c/h
Add unit tests for test_tls_decrypt_lite
Split out reliable_ack_parse from reliable_ack_read
Remove inc_pid argument from reliable_mark_deleted that is always true Remove EXPONENTIAL_BACKOFF define
Refactor tls-auth/tls-crypt wrapping into into own function Extract session_move_pre_start as own function, use local buffer
variable
Change FULL_SYNC macro to no_pending_reliable_packets function Move tls_process_state into its own function
Remove pointless indentation from tls_process.
Move CRL reload to key_state_init from S_START transition Implement constructing a control channel reset client as standalone
fucntion
Implement stateless, HMAC basedsesssion id three way handshake Make buf_write_u8/16/32 take the type they pretend to take Change reliable_get_buf_sequenced to reliable_get_entry_sequenced Extract read_incoming_tls_ciphertext into function Implement HMAC based session id for tls-crypt v2
Optimise three-way handshake condition for S_PRE_START to S_START Extract read_incoming_tls_plaintext into its own function Ensure that control channel packet are respecting tls-mtu Allow setting control channel packet size with tls-mtu
Add unit test for reliable_get_num_output_sequenced_available Always include ACKs for the last seen control packets
Changes.rst | 16 + doc/doxygen/doc_protocol_overview.h | 2 + doc/man-sections/link-options.rst | 7 + doc/man-sections/tls-options.rst | 14 + doc/tls-crypt-v2.txt | 41 + src/openvpn/Makefile.am | 1 + src/openvpn/buffer.h | 13 +- src/openvpn/crypto.h | 8 + src/openvpn/init.c | 28 +- src/openvpn/mtu.h | 5 + src/openvpn/mudp.c | 164 ++- src/openvpn/multi.h | 3 + src/openvpn/openvpn.h | 6 + src/openvpn/openvpn.vcxproj | 2 + src/openvpn/openvpn.vcxproj.filters | 3 + src/openvpn/options.c | 27 + src/openvpn/options.h | 4 + src/openvpn/packet_id.c | 56 - src/openvpn/packet_id.h | 25 +- src/openvpn/reliable.c | 209 +++- src/openvpn/reliable.h | 84 +- src/openvpn/ssl.c | 1312 ++++++++++---------- src/openvpn/ssl.h | 97 +- src/openvpn/ssl_backend.h | 8 +- src/openvpn/ssl_common.h | 1 + src/openvpn/ssl_mbedtls.c | 19 +- src/openvpn/ssl_mbedtls.h | 4 + src/openvpn/ssl_openssl.c | 22 +- src/openvpn/ssl_openssl.h | 7 + src/openvpn/ssl_pkt.c | 549 ++++++++ src/openvpn/ssl_pkt.h | 295 +++++ tests/unit_tests/openvpn/Makefile.am | 29 +- tests/unit_tests/openvpn/mock_get_random.c | 10 + tests/unit_tests/openvpn/test_packet_id.c | 90 ++ tests/unit_tests/openvpn/test_pkt.c | 620 +++++++++ 35 files changed, 2825 insertions(+), 956 deletions(-) create mode 100644 src/openvpn/ssl_pkt.c create mode 100644 src/openvpn/ssl_pkt.h create mode 100644 tests/unit_tests/openvpn/test_pkt.c
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 1/28 Remove tls_init_control_channel_frame_parameters wrapper function
While calling this wrapper function is strictly more correct, these indirection layer with tiny wrapper make the code more complex and going through more layer than it really needs to.
--- src/openvpn/init.c | 2 +- src/openvpn/ssl.c | 14 +------------- src/openvpn/ssl.h | 7 ++++--- 3 files changed, 6 insertions(+), 17 deletions(-)
src/openvpn/init.c +1 -1
@@ -2994,7 +2994,7 @@ do_init_frame_tls(struct context *c) }if (c->c2.tls_auth_standalone) { -tls_auth_standalone_finalize (c->c2.tls_auth_standalone, &c->c2.frame); +tls_init_control_channel_frame_parameters (&c->c2.frame, &c->c2.tls_auth_standalone->frame);frame_print (&c->c2.tls_auth_standalone->frame,D_MTU_INFO ,"TLS-Auth MTU parms" ); }
src/openvpn/ssl.c +1 -13
@@ -295,11 +295,7 @@ tls_limit_reneg_bytes(const char *ciphername, int *reneg_bytes) } } -/* -* Max number of bytes we will add -* to control channel packet. -*/ -static void +void tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame,struct frame *frame) {
@@ -1284,7 +1280,6 @@ void tls_multi_init_finalize(struct tls_multi *multi, const struct frame *frame) { tls_init_control_channel_frame_parameters(frame, &multi->opt.frame); -/* initialize the active and untrusted sessions */ tls_session_init (multi, &multi->session[TM_ACTIVE ]);
@@ -1322,13 +1317,6 @@ tls_auth_standalone_init(struct tls_options *tls_options,return tas; } -void -tls_auth_standalone_finalize (struct tls_auth_standalone *tas, -const struct frame *frame) -{ -tls_init_control_channel_frame_parameters (frame, &tas->frame); -} -/* * Set local and remote option compatibility strings. * Used to verify compatibility of local and remote option
src/openvpn/ssl.h +4 -3
@@ -209,10 +209,11 @@ struct tls_auth_standalone *tls_auth_standalone_init(struct tls_options *tls_opt struct gc_arena *gc);/* -* Finalize a standalone tls-auth verification object. +* Setups up the control channel frame size parameters from the data channel +* parameters */ -void tls_auth_standalone_finalize (struct tls_auth_standalone *tas, -const struct frame *frame); +void tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, +struct frame *frame);/* * Set local and remote option compatibility strings.
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 2/28 Remove dead PID_TEST code
In reply to Arne SchwabeEnabling this test produces compile errors and by the looks of it the test has been broken for many years.
--- src/openvpn/init.c | 5 ---- src/openvpn/packet_id.c | 56 ----------------------------------------- src/openvpn/packet_id.h | 10 -------- 3 files changed, 71 deletions(-)
src/openvpn/init.c +0 -5
@@ -791,11 +791,6 @@ init_static(void)init_ssl_lib (); -#ifdef PID_TEST -packet_id_interactive_test ();/* test the sequence number code */ -return false; -#endif -#ifdef SCHEDULE_TEST schedule_test ();return false;
src/openvpn/packet_id.c +0 -56
59 lines not on this page · raw message
src/openvpn/packet_id.h +0 -10
@@ -35,11 +35,6 @@#include "error.h" #include "otime.h" -/* -* Enables OpenVPN to be compiled in special packet_id test mode. -*/ -/*#define PID_TEST*/ -#if 1 /* * These are the types that members of
@@ -296,11 +291,6 @@ packet_id_persist_save_obj(struct packet_id_persist *p, const struct packet_id *const char *packet_id_net_print (const struct packet_id_net *pin,bool print_timestamp,struct gc_arena *gc); -#ifdef PID_TEST -void packet_id_interactive_test (void ); - -#endif -static inline int packet_id_size (bool long_form) {
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 3/28 Move pre decrypt lite check to its own function
In reply to Arne SchwabeThis prepares for extending this function with the HMAC based session ID check.
--- src/openvpn/mudp.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
src/openvpn/mudp.c +15 -2
@@ -39,6 +39,20 @@#include <sys/inotify.h> #endif +static bool +do_pre_decrypt_check (struct multi_context *m) +{ +if (!m->top.c2.tls_auth_standalone) + { +return false; + } +if (!tls_pre_decrypt_lite (m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf)) + { +return false; + } +return true; +} +/* * Get a client instance based on real address. If * the instance doesn't exist, create it while
@@ -95,8 +109,7 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated) }if (!mi) { -if (!m->top.c2.tls_auth_standalone - ||tls_pre_decrypt_lite (m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf)) +if (do_pre_decrypt_check (m)) {if (frequency_limit_event_allowed (m->new_connection_limiter)) {
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 4/28 Add documentation for swap_hmac function
In reply to Arne Schwabe--- src/openvpn/ssl.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)
src/openvpn/ssl.c +18 -4
@@ -1366,10 +1366,7 @@ tls_multi_free(struct tls_multi *multi, bool clear) } -/* -* Move a packet authentication HMAC + related fields to or from the front -* of the buffer so it can be processed by encrypt/decrypt. -*/ +/* * Dependent on hmac size, opcode size, and session_id size.
@@ -1377,6 +1374,23 @@ tls_multi_free(struct tls_multi *multi, bool clear)*/ #define SWAP_BUF_SIZE 256 +/** +* Move a packet authentication HMAC + related fields to or from the front +* of the buffer so it can be processed by encrypt/decrypt. +* +* Turning the on wire format that starts with the opcode to a format +* that starts with the hmac +* e.g. "onwire" [opcode + packet id] [hmac] [remainder of packed] +* +* +* "internal" [hmac] [opcode + packet id] [remainer of packet] +* +* @param buf the buffer the swap operation is executed on +* @param incoming determines the direction of the swap +* @param co crypto options, determines the hmac to use in the swap +* +* @return if the swap was successful (buf was large enough) +*/ static bool swap_hmac (struct buffer *buf,const struct crypto_options *co,bool incoming) {
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 5/28 Extend tls_pre_decrypt_lite to return type of packet and keep state
In reply to Arne SchwabeThis allows us to keep the temporary data for a little bit longer so we can use this to make further checks and ultimatively use the state to craft the HMAC based RESET reply.
For now we do not use the extra information and keep behaviour identical.
--- src/openvpn/mudp.c | 11 ++++++- src/openvpn/ssl.c | 76 +++++++++++++++++++++++++++++++++------------- src/openvpn/ssl.h | 37 ++++++++++++++++++++-- 3 files changed, 99 insertions(+), 25 deletions(-)
src/openvpn/mudp.c +10 -1
@@ -46,7 +46,16 @@ do_pre_decrypt_check(struct multi_context *m) {return false; } -if (!tls_pre_decrypt_lite (m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf)) + +enum first_packet_verdict verdict; +struct tls_pre_decrypt_state state = {0 }; + + verdict =tls_pre_decrypt_lite (m->top.c2.tls_auth_standalone, &state, + &m->top.c2.from, &m->top.c2.buf); + +free_tls_pre_decrypt_state (&state); + +if (verdict ==VERDICT_INVALID || verdict ==VERDICT_VALID_CONTROL_V1 ) {return false; }
src/openvpn/ssl.c +55 -21
139 lines not on this page · raw message
src/openvpn/ssl.h +34 -3
@@ -323,6 +323,33 @@ bool tls_pre_decrypt(struct tls_multi *multi,/** @name Functions for managing security parameter state for data channel packets * @{ */ + +enum first_packet_verdict { +/** This packet is a valid reset packet from the peer */ +VERDICT_VALID_RESET , +/** This packet is a valid control packet from the peer, +* i.e. it has a valid session id hmac in it */ +VERDICT_VALID_CONTROL_V1 , +/** the packet failed on of the various checks */ +VERDICT_INVALID +}; + +/** +* struct that stores the temporary data for the tls lite decrypt +* functions +*/ +struct tls_pre_decrypt_state { +struct tls_wrap_ctx tls_wrap_tmp; +struct buffer newbuf; +struct session_id peer_session_id; +}; + +/** +* +* @param state +*/ +void free_tls_pre_decrypt_state (struct tls_pre_decrypt_state *state); +/** * Inspect an incoming packet for which no VPN tunnel is active, and * determine whether a new VPN tunnel should be created.
@@ -343,6 +370,8 @@ bool tls_pre_decrypt(struct tls_multi *multi,* whether a new VPN tunnel should be created. If so, that new VPN tunnel * instance will handle processing of the packet. * +* This function is only used in the UDP p2mp server code path +* * @param tas - The standalone TLS authentication setting structure for * this process. * @param from - The source address of the packet.
@@ -354,9 +383,11 @@ bool tls_pre_decrypt(struct tls_multi *multi,* @li False if the packet is not valid, did not pass the HMAC firewall * test, or some other error occurred. */ -bool tls_pre_decrypt_lite (const struct tls_auth_standalone *tas, -const struct link_socket_actual *from, -const struct buffer *buf); +enum first_packet_verdict +tls_pre_decrypt_lite (const struct tls_auth_standalone *tas, +struct tls_pre_decrypt_state *state, +const struct link_socket_actual *from, +const struct buffer *buf);/**
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 6/28 Move ssl function related to control channel wrap/unwrap to ssl_pkt.c/h
In reply to Arne SchwabeThis allows these functions to be relatively easily included into the unit test without pulling ssl.c and all the dependencies of ssl.c into a unit test.
--- src/openvpn/Makefile.am | 1 + src/openvpn/mudp.c | 1 + src/openvpn/openvpn.vcxproj | 2 + src/openvpn/openvpn.vcxproj.filters | 3 + src/openvpn/ssl.c | 392 ---------------------------- src/openvpn/ssl.h | 103 +------- src/openvpn/ssl_mbedtls.c | 5 - src/openvpn/ssl_mbedtls.h | 4 + src/openvpn/ssl_openssl.c | 6 - src/openvpn/ssl_openssl.h | 7 + src/openvpn/ssl_pkt.c | 382 +++++++++++++++++++++++++++ src/openvpn/ssl_pkt.h | 199 ++++++++++++++ 12 files changed, 600 insertions(+), 505 deletions(-) create mode 100644 src/openvpn/ssl_pkt.c create mode 100644 src/openvpn/ssl_pkt.h
src/openvpn/Makefile.am +1 -0
@@ -119,6 +119,7 @@ openvpn_SOURCES = \ ssl_openssl.c ssl_openssl.h \ ssl_mbedtls.c ssl_mbedtls.h \ ssl_ncp.c ssl_ncp.h \ + ssl_pkt.c ssl_pkt.h \ ssl_util.c ssl_util.h \ ssl_common.h \ ssl_verify.c ssl_verify.h ssl_verify_backend.h \
src/openvpn/mudp.c +1 -0
@@ -34,6 +34,7 @@#include "forward.h" #include "memdbg.h" +#include "ssl_pkt.h" #ifdef HAVE_SYS_INOTIFY_H #include <sys/inotify.h>
src/openvpn/openvpn.vcxproj +2 -0
@@ -322,6 +322,7 @@ <ClCompile Include="ssl.c" /> <ClCompile Include="ssl_openssl.c" /> <ClCompile Include="ssl_ncp.c" /> + <ClCompile Include="ssl_pkt.c" /> <ClCompile Include="ssl_util.c" /> <ClCompile Include="ssl_verify.c" /> <ClCompile Include="ssl_verify_openssl.c" />
@@ -414,6 +415,7 @@ <ClInclude Include="ssl_common.h" /> <ClInclude Include="ssl_ncp.h" /> <ClInclude Include="ssl_openssl.h" /> + <ClInclude Include="ssl_pkt.h" /> <ClInclude Include="ssl_util.h" /> <ClInclude Include="ssl_verify.h" /> <ClInclude Include="ssl_verify_backend.h" />
src/openvpn/openvpn.vcxproj.filters +3 -0
@@ -246,6 +246,9 @@ <ClCompile Include="ssl_ncp.c"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="ssl_pkt.c"> + <Filter>Source Files</Filter> + </ClCompile> <ClCompile Include="ssl_util.c"> <Filter>Source Files</Filter> </ClCompile>
src/openvpn/ssl.c +0 -392
410 lines not on this page · raw message
src/openvpn/ssl.h +1 -102
125 lines not on this page · raw message
src/openvpn/ssl_mbedtls.c +0 -5
@@ -117,11 +117,6 @@ tls_free_lib(void) { } -void -tls_clear_error (void ) -{ -} -void tls_ctx_server_new (struct tls_root_ctx *ctx) {
src/openvpn/ssl_mbedtls.h +4 -0
@@ -144,4 +144,8 @@ int tls_ctx_use_external_signing_func(struct tls_root_ctx *ctx,external_sign_func sign_func, void *sign_ctx); +static inline void +tls_clear_error (void ) +{ +}#endif /* SSL_MBEDTLS_H_ */
src/openvpn/ssl_openssl.c +0 -6
@@ -107,12 +107,6 @@ tls_free_lib(void)#endif } -void -tls_clear_error (void ) -{ -ERR_clear_error (); -} -void tls_ctx_server_new (struct tls_root_ctx *ctx) {
src/openvpn/ssl_openssl.h +7 -0
@@ -30,6 +30,7 @@#define SSL_OPENSSL_H_ #include <openssl/ssl.h> +#include <openssl/err.h> /** * Structure that wraps the TLS context. Contents differ depending on the
@@ -54,4 +55,10 @@ struct key_state_ssl {*/ extern int mydata_index;/* GLOBAL */ +static inline void +tls_clear_error (void ) +{ +ERR_clear_error (); +} +#endif /* SSL_OPENSSL_H_ */
src/openvpn/ssl_pkt.c new file +382 -0
383 lines not on this page · raw message
src/openvpn/ssl_pkt.h new file +199 -0
199 lines not on this page · raw message
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 7/28 Add unit tests for test_tls_decrypt_lite
In reply to Arne SchwabeThis tests currently the existing functionality of test_tls_decrypt_lite to check if a reset packet is valid or not.
--- tests/unit_tests/openvpn/Makefile.am | 24 +- tests/unit_tests/openvpn/test_pkt.c | 347 +++++++++++++++++++++++++++ 2 files changed, 370 insertions(+), 1 deletion(-) create mode 100644 tests/unit_tests/openvpn/test_pkt.c
tests/unit_tests/openvpn/Makefile.am +23 -1
@@ -6,7 +6,8 @@ if HAVE_LD_WRAP_SUPPORT test_binaries += argv_testdriver buffer_testdriver endif -test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver ncp_testdriver misc_testdriver +test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver ncp_testdriver misc_testdriver \ + pkt_testdriver if HAVE_LD_WRAP_SUPPORT test_binaries += tls_crypt_testdriver endif
@@ -62,6 +63,27 @@ packet_id_testdriver_SOURCES = test_packet_id.c mock_msg.c mock_msg.h \ $(openvpn_srcdir)/packet_id.c \ $(openvpn_srcdir)/platform.c +pkt_testdriver_CFLAGS = @TEST_CFLAGS@ \ + -I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir) +pkt_testdriver_LDFLAGS = @TEST_LDFLAGS@ +pkt_testdriver_SOURCES = test_pkt.c mock_msg.c mock_msg.h \ +$(openvpn_srcdir)/argv.c \ + $(openvpn_srcdir)/base64.c \ + $(openvpn_srcdir)/buffer.c \ + $(openvpn_srcdir)/crypto.c \ + $(openvpn_srcdir)/crypto_mbedtls.c \ + $(openvpn_srcdir)/crypto_openssl.c \ + $(openvpn_srcdir)/env_set.c \ + $(openvpn_srcdir)/otime.c \ + $(openvpn_srcdir)/packet_id.c \ + $(openvpn_srcdir)/platform.c \ + $(openvpn_srcdir)/reliable.c \ + $(openvpn_srcdir)/run_command.c \ + $(openvpn_srcdir)/session_id.c \ + $(openvpn_srcdir)/ssl_pkt.c \ + $(openvpn_srcdir)/tls_crypt.c + + tls_crypt_testdriver_CFLAGS = @TEST_CFLAGS@ \ -I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir) tls_crypt_testdriver_LDFLAGS = @TEST_LDFLAGS@ \
tests/unit_tests/openvpn/test_pkt.c new file +347 -0
347 lines not on this page · raw message
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 8/28 Split out reliable_ack_parse from reliable_ack_read
In reply to Arne SchwabeThis allows only the parsing without verification to be reused in other code parts.
--- src/openvpn/reliable.c | 60 ++++++++++++++++++++++++------------------ src/openvpn/reliable.h | 22 ++++++++++++++++ src/openvpn/ssl.c | 1 - 3 files changed, 56 insertions(+), 27 deletions(-)
src/openvpn/reliable.c +34 -26
@@ -153,56 +153,64 @@ reliable_ack_acknowledge_packet_id(struct reliable_ack *ack, packet_id_type pid)return false; } -/* read a packet ID acknowledgement record from buf into ack */ +bool reliable_ack_read (struct reliable_ack *ack,struct buffer *buf,const struct session_id *sid) { -struct gc_arena gc =gc_new (); -int i; -uint8_t count; -packet_id_type net_pid; -packet_id_type pid;struct session_id session_id_remote; +if (!reliable_ack_parse (buf, ack, &session_id_remote)) + { +return false; + } + +if (ack->len >=1 && (!session_id_defined (&session_id_remote) + || !session_id_equal (&session_id_remote, sid))) + { +struct gc_arena gc =gc_new (); +dmsg (D_REL_LOW , +"ACK read BAD SESSION-ID FROM REMOTE, local=%s, remote=%s" , +session_id_print (sid, &gc),session_id_print (&session_id_remote, &gc)); +gc_free (&gc); +return false; + } +return true; +} + +bool +reliable_ack_parse (struct buffer *buf,struct reliable_ack *ack, +struct session_id *session_id_remote) +{ +uint8_t count; + ack->len =0 ; +if (!buf_read (buf, &count,sizeof (count))) { - goto error; +return false; } -for (i =0 ; i < count; ++i) +for (int i =0 ; i < count; ++i) { +packet_id_type net_pid;if (!buf_read (buf, &net_pid,sizeof (net_pid))) { - goto error; +return false; }if (ack->len >=RELIABLE_ACK_SIZE ) { - goto error; +return false; } - pid =ntohpid (net_pid); +packet_id_type pid =ntohpid (net_pid); ack->packet_id[ack->len++] = pid; }if (count) { -if (!session_id_read (&session_id_remote, buf)) - { - goto error; - } -if (!session_id_defined (&session_id_remote) - || !session_id_equal (&session_id_remote, sid)) +if (!session_id_read (session_id_remote, buf)) { -dmsg (D_REL_LOW , -"ACK read BAD SESSION-ID FROM REMOTE, local=%s, remote=%s" , -session_id_print (sid, &gc),session_id_print (&session_id_remote, &gc)); - goto error; +return false; } } -gc_free (&gc);return true; - -error: -gc_free (&gc); -return false; }/* write a packet ID acknowledgement record to buf, */
src/openvpn/reliable.h +22 -0
@@ -126,6 +126,28 @@ struct reliablebool reliable_ack_read (struct reliable_ack *ack,struct buffer *buf,const struct session_id *sid); + +/** +* Parse an acknowledgment record from a received packet. +* +* This function parses the packet ID acknowledgment record from the packet +* contained in \a buf. If the record contains acknowledgments, these are +* stored in \a ack. This function also compares extracts packet's session ID +* and returns it in \a session_id_remote +* +* @param ack The acknowledgment structure in which received +* acknowledgments are to be stored. +* @param buf The buffer containing the packet. +* @param session_id_remote The parsed remote session id. This field is +* is only filled if ack->len >= 1 +* @return +* @li True, if processing was successful. +* @li False, if an error occurs during processing. +*/ +bool +reliable_ack_parse (struct buffer *buf,struct reliable_ack *ack, +struct session_id *session_id_remote); +/** * Remove acknowledged packets from a reliable structure. *
src/openvpn/ssl.c +0 -1
@@ -3435,7 +3435,6 @@ tls_pre_decrypt(struct tls_multi *multi,/* buffers all packet IDs to delete from send_reliable */ struct reliable_ack send_ack; - send_ack.len =0 ;if (!reliable_ack_read (&send_ack, buf, &session->session_id)) { msg(D_TLS_ERRORS,
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 9/28 Remove inc_pid argument from reliable_mark_deleted that is always true
In reply to Arne SchwabeThis is a small cleanup to remove a superfluous argument
--- src/openvpn/reliable.c | 7 ++----- src/openvpn/reliable.h | 2 +- src/openvpn/ssl.c | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-)
src/openvpn/reliable.c +2 -5
@@ -738,7 +738,7 @@ reliable_mark_active_outgoing(struct reliable *rel, struct buffer *buf, int opco/* delete a buffer previously activated by reliable_mark_active() */ void -reliable_mark_deleted (struct reliable *rel,struct buffer *buf,bool inc_pid) +reliable_mark_deleted (struct reliable *rel,struct buffer *buf) {int i;for (i =0 ; i < rel->size; ++i)
@@ -747,10 +747,7 @@ reliable_mark_deleted(struct reliable *rel, struct buffer *buf, bool inc_pid)if (buf == &e->buf) { e->active = false; -if (inc_pid) - { - rel->packet_id = e->packet_id +1 ; - } + rel->packet_id = e->packet_id +1 ;return ; } }
src/openvpn/reliable.h +1 -1
@@ -367,7 +367,7 @@ struct buffer *reliable_get_buf_sequenced(struct reliable *rel);* @param inc_pid If true, the reliable structure's packet ID counter * will be incremented. */ -void reliable_mark_deleted (struct reliable *rel,struct buffer *buf,bool inc_pid); +void reliable_mark_deleted (struct reliable *rel,struct buffer *buf);/** @} name Functions for extracting incoming packets */
src/openvpn/ssl.c +1 -1
@@ -2610,7 +2610,7 @@ tls_process(struct tls_multi *multi, }if (status ==1 ) { -reliable_mark_deleted (ks->rec_reliable, buf, true); +reliable_mark_deleted (ks->rec_reliable, buf); state_change = true;dmsg (D_TLS_DEBUG ,"Incoming Ciphertext -> TLS" ); }
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 10/28 Remove EXPONENTIAL_BACKOFF define
In reply to Arne SchwabeWe have EXPONENTIAL_BACKOFF as default forever (8c47de7, 2.1.1c, 2010). Remove the other code path that is dead code.
--- src/openvpn/reliable.c | 5 ----- src/openvpn/reliable.h | 2 -- 2 files changed, 7 deletions(-)
src/openvpn/reliable.c +0 -5
@@ -602,14 +602,9 @@ reliable_send(struct reliable *rel, int *opcode) }if (best) { -#ifdef EXPONENTIAL_BACKOFF /* exponential backoff */ best->next_try = local_now + best->timeout; best->timeout *=2 ; -#else -/* constant timeout, no backoff */ - best->next_try = local_now + best->timeout; -#endif best->n_acks =0 ; *opcode = best->opcode; dmsg(D_REL_DEBUG, "ACK reliable_send ID " packet_id_format " (size=%d to=%d)",
src/openvpn/reliable.h +0 -2
@@ -41,8 +41,6 @@* @{ */ -#define EXPONENTIAL_BACKOFF -#define RELIABLE_ACK_SIZE 8/**< The maximum number of packet IDs * waiting to be acknowledged which can * be stored in one \c reliable_ack
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 11/28 Refactor tls-auth/tls-crypt wrapping into into own function
This allows the the wrapping to be easier reused by a function that does not have access to a full TLS session.
--- src/openvpn/ssl_pkt.c | 82 ++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 32 deletions(-)
src/openvpn/ssl_pkt.c +50 -32
@@ -110,51 +110,48 @@ swap_hmac(struct buffer *buf, const struct crypto_options *co, bool incoming)#undef SWAP_BUF_SIZE -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) +/** +* Wraps a TLS control packet by adding tls-auth HMAC or tls-crypt(-v2) +* encryption and opcode header including session id. +* +* @param ctx tls wrapping context +* @param header first byte of the packet (opcode and key id) +* @param buf buffer to write the resulting packet to +* @param session_id session id to use as our session id +*/ +static void +tls_wrap_control (struct tls_wrap_ctx *ctx,uint8_t header,struct buffer *buf, +struct session_id *session_id) { -uint8_t header = ks->key_id | (opcode <<P_OPCODE_SHIFT ); -struct buffer null =clear_buf (); - -ASSERT (link_socket_actual_defined (&ks->remote_addr)); -ASSERT (reliable_ack_write - (ks->rec_ack, buf, &ks->session_id_remote, max_ack, prepend_ack)); - -msg (D_TLS_DEBUG ,"%s(): %s" , __func__,packet_opcode_name (opcode)); - -if (session->tls_wrap.mode ==TLS_WRAP_AUTH - || session->tls_wrap.mode ==TLS_WRAP_NONE ) +if (ctx->mode ==TLS_WRAP_AUTH + || ctx->mode ==TLS_WRAP_NONE ) { -ASSERT (session_id_write_prepend (&session->session_id, buf)); +ASSERT (session_id_write_prepend (session_id, buf));ASSERT (buf_write_prepend (buf, &header,sizeof (header))); } -if (session->tls_wrap.mode ==TLS_WRAP_AUTH ) +if (ctx->mode ==TLS_WRAP_AUTH ) { +struct buffer null =clear_buf (); +/* no encryption, only write hmac */ -openvpn_encrypt (buf, null, &session->tls_wrap.opt); -ASSERT (swap_hmac (buf, &session->tls_wrap.opt, false)); +openvpn_encrypt (buf, null, &ctx->opt); +ASSERT (swap_hmac (buf, &ctx->opt, false)); } -else if (session->tls_wrap.mode ==TLS_WRAP_CRYPT ) +else if (ctx->mode ==TLS_WRAP_CRYPT ) { -ASSERT (buf_init (&session->tls_wrap.work, buf->offset)); -ASSERT (buf_write (&session->tls_wrap.work, &header,sizeof (header))); -ASSERT (session_id_write (&session->session_id, &session->tls_wrap.work)); -if (!tls_crypt_wrap (buf, &session->tls_wrap.work, &session->tls_wrap.opt)) +ASSERT (buf_init (&ctx->work, buf->offset)); +ASSERT (buf_write (&ctx->work, &header,sizeof (header))); +ASSERT (session_id_write (session_id, &ctx->work)); +if (!tls_crypt_wrap (buf, &ctx->work, &ctx->opt)) { buf->len =0 ;return ; } -if (opcode ==P_CONTROL_HARD_RESET_CLIENT_V3 ) +if ((header >>P_OPCODE_SHIFT ) ==P_CONTROL_HARD_RESET_CLIENT_V3 ) { -if (!buf_copy (&session->tls_wrap.work, - session->tls_wrap.tls_crypt_v2_wkc)) +if (!buf_copy (&ctx->work, + ctx->tls_crypt_v2_wkc)) {msg (D_TLS_ERRORS ,"Could not append tls-crypt-v2 client key" ); buf->len =0 ;
@@ -164,8 +161,29 @@ write_control_auth(struct tls_session *session,/* Don't change the original data in buf, it's used by the reliability * layer to resend on failure. */ - *buf = session->tls_wrap.work; + *buf = ctx->work; } +} + +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) +{ +uint8_t header = ks->key_id | (opcode <<P_OPCODE_SHIFT ); + +ASSERT (link_socket_actual_defined (&ks->remote_addr)); +ASSERT (reliable_ack_write + (ks->rec_ack, buf, &ks->session_id_remote, max_ack, prepend_ack)); + +msg (D_TLS_DEBUG ,"%s(): %s" , __func__,packet_opcode_name (opcode)); + +tls_wrap_control (&session->tls_wrap, header, buf, &session->session_id); + *to_link_addr = &ks->remote_addr; }
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 12/28 Extract session_move_pre_start as own function, use local buffer variable
In reply to Arne SchwabeThis changes the C90 struct buffer declaration to a C99 style one. Also move the state transition from S_INITIAL to S_PE_START into its own function.
--- src/openvpn/ssl.c | 84 ++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 34 deletions(-)
src/openvpn/ssl.c +50 -34
@@ -2382,6 +2382,52 @@ auth_deferred_expire_window(const struct tls_options *o)return ret; } +/** +* Move the session from S_INITIAL to S_PRE_START. This will also generate +* the intial message based on ks->initial_opcode +* +* @return if the state change was succesful +*/ +static bool +session_move_pre_start (const struct tls_session *session, +struct key_state *ks) +{ +struct buffer *buf =reliable_get_buf_output_sequenced (ks->send_reliable); +if (!buf) + { +return false; + } + + ks->initial = now; + ks->must_negotiate = now + session->opt->handshake_window; + ks->auth_deferred_expire = now +auth_deferred_expire_window (session->opt); + +/* null buffer */ +reliable_mark_active_outgoing (ks->send_reliable, buf, ks->initial_opcode); +INCR_GENERATED ; + + ks->state =S_PRE_START ; + +struct gc_arena gc =gc_new (); +dmsg (D_TLS_DEBUG ,"TLS: Initial Handshake, sid=%s" , +session_id_print (&session->session_id, &gc)); +gc_free (&gc); + +#ifdef ENABLE_MANAGEMENT +if (management && ks->initial_opcode !=P_CONTROL_SOFT_RESET_V1 ) + { +management_set_state (management, +OPENVPN_STATE_WAIT , +NULL , +NULL , +NULL , +NULL , +NULL ); + } +#endif +return true; + +}/* * This is the primary routine for processing TLS stuff inside the * the main event loop. When this routine exits
@@ -2400,7 +2446,6 @@ tls_process(struct tls_multi *multi, interval_t *wakeup) {struct gc_arena gc =gc_new (); -struct buffer *buf;bool state_change = false;bool active = false;struct key_state *ks = &session->key[KS_PRIMARY ];/* primary key */
@@ -2460,35 +2505,7 @@ tls_process(struct tls_multi *multi,/* Initial handshake */ if (ks->state ==S_INITIAL ) { - buf =reliable_get_buf_output_sequenced (ks->send_reliable); -if (buf) - { - ks->initial = now; - ks->must_negotiate = now + session->opt->handshake_window; - ks->auth_deferred_expire = now +auth_deferred_expire_window (session->opt); - -/* null buffer */ -reliable_mark_active_outgoing (ks->send_reliable, buf, ks->initial_opcode); -INCR_GENERATED ; - - ks->state =S_PRE_START ; - state_change = true; -dmsg (D_TLS_DEBUG ,"TLS: Initial Handshake, sid=%s" , -session_id_print (&session->session_id, &gc)); - -#ifdef ENABLE_MANAGEMENT -if (management && ks->initial_opcode !=P_CONTROL_SOFT_RESET_V1 ) - { -management_set_state (management, -OPENVPN_STATE_WAIT , -NULL , -NULL , -NULL , -NULL , -NULL ); - } -#endif - } + state_change =session_move_pre_start (session, ks); }/* Are we timed out on receive? */
@@ -2573,11 +2590,10 @@ tls_process(struct tls_multi *multi,if (!to_link->len &&reliable_can_send (ks->send_reliable)) {int opcode; -struct buffer b; - buf =reliable_send (ks->send_reliable, &opcode); +struct buffer *buf =reliable_send (ks->send_reliable, &opcode);ASSERT (buf); - b = *buf; +struct buffer b = *buf;INCR_SENT ; write_control_auth(session, ks, &b, to_link_addr, opcode,
@@ -2590,7 +2606,7 @@ tls_process(struct tls_multi *multi, }/* Write incoming ciphertext to TLS object */ - buf =reliable_get_buf_sequenced (ks->rec_reliable); +struct buffer *buf =reliable_get_buf_sequenced (ks->rec_reliable);if (buf) {int status =0 ;
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 13/28 Change FULL_SYNC macro to no_pending_reliable_packets function
In reply to Arne SchwabeThis changes this macro to a better named inline function. This introduces a slight whitespace problem but the next refactoring will move the incorrectly intended block to its own function anyway.
--- src/openvpn/ssl.c | 100 ++++++++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 43 deletions(-)
src/openvpn/ssl.c +57 -43
@@ -1774,8 +1774,10 @@ flush_payload_buffer(struct key_state *ks) }/* true if no in/out acknowledgements pending */ -#define FULL_SYNC \ - (reliable_empty(ks->send_reliable) && reliable_ack_empty(ks->rec_ack)) +static bool no_pending_reliable_packets (struct key_state *ks) +{ +return (reliable_empty (ks->send_reliable) &&reliable_ack_empty (ks->rec_ack)); +}/* * Move the active key to the lame duck key and reinitialize the
@@ -2428,6 +2430,54 @@ session_move_pre_start(const struct tls_session *session,return true; } + +/** +* Moves the key to state to S_ACTIVE and also advances the multi_state state +* machine if this is the initial connection. +*/ +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) +{ +dmsg (D_TLS_DEBUG_MED ,"STATE S_ACTIVE" ); + + ks->established = now; +if (check_debug_level (D_HANDSHAKE )) + { +print_details (&ks->ks_ssl,"Control Channel:" ); + } + ks->state =S_ACTIVE ; +/* Cancel negotiation timeout */ + ks->must_negotiate =0 ; +INCR_SUCCESS ; + +/* Set outgoing address for data channel packets */ +link_socket_set_outgoing_addr (to_link_socket_info, &ks->remote_addr, + session->common_name, session->opt->es); + +/* Check if we need to advance the tls_multi state machine */ +if (multi->multi_state ==CAS_NOT_CONNECTED ) + { +if (session->opt->mode ==MODE_SERVER ) + { +/* On a server we continue with running connect scripts next */ + multi->multi_state =CAS_WAITING_AUTH ; + } +else + { +/* Skip the connect script related states */ + multi->multi_state =CAS_WAITING_OPTIONS_IMPORT ; + } + } + +/* Flush any payload packets that were buffered before our state transitioned to S_ACTIVE */ +flush_payload_buffer (ks); + +#ifdef MEASURE_TLS_HANDSHAKE_STATS +show_tls_performance_stats (); +#endif +}/* * This is the primary routine for processing TLS stuff inside the * the main event loop. When this routine exits
@@ -2518,7 +2568,7 @@ tls_process(struct tls_multi *multi, }/* Wait for Initial Handshake ACK */ -if (ks->state ==S_PRE_START &&FULL_SYNC ) +if (ks->state ==S_PRE_START &&no_pending_reliable_packets (ks)) { ks->state =S_START ; state_change = true;
@@ -2542,47 +2592,11 @@ tls_process(struct tls_multi *multi,/* Wait for ACK */ if (((ks->state ==S_GOT_KEY && !session->opt->server) - || (ks->state ==S_SENT_KEY && session->opt->server))) + || (ks->state ==S_SENT_KEY && session->opt->server)) + &&no_pending_reliable_packets (ks)) { -if (FULL_SYNC ) - { - ks->established = now; -dmsg (D_TLS_DEBUG_MED ,"STATE S_ACTIVE" ); -if (check_debug_level (D_HANDSHAKE )) - { -print_details (&ks->ks_ssl,"Control Channel:" ); - } - state_change = true; - ks->state =S_ACTIVE ; -/* Cancel negotiation timeout */ - ks->must_negotiate =0 ; -INCR_SUCCESS ; - -/* Set outgoing address for data channel packets */ -link_socket_set_outgoing_addr (to_link_socket_info, &ks->remote_addr, session->common_name, session->opt->es); - -/* Check if we need to advance the tls_multi state machine */ -if (multi->multi_state ==CAS_NOT_CONNECTED ) - { -if (session->opt->mode ==MODE_SERVER ) - { -/* On a server we continue with running connect scripts next */ - multi->multi_state =CAS_WAITING_AUTH ; - } -else - { -/* Skip the connect script related states */ - multi->multi_state =CAS_WAITING_OPTIONS_IMPORT ; - } - } - -/* Flush any payload packets that were buffered before our state transitioned to S_ACTIVE */ -flush_payload_buffer (ks); - -#ifdef MEASURE_TLS_HANDSHAKE_STATS -show_tls_performance_stats (); -#endif - } +session_move_active (multi, session, to_link_socket_info, ks); + state_change = true; }/* Reliable buffer to outgoing TCP/UDP (send up to CONTROL_SEND_ACK_MAX ACKs
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 14/28 Move tls_process_state into its own function
In reply to Arne SchwabeThis function does most of the state transitions in the TLS state machine. Moving it into its own function removes an intention area and makes tls_process function easier to understand as the loop is more obvious.
This is largely just a code move with small expection. bool active is no longer directly set but inferred from to_link->len
--- src/openvpn/ssl.c | 444 ++++++++++++++++++++++++---------------------- 1 file changed, 228 insertions(+), 216 deletions(-)
src/openvpn/ssl.c +228 -216
@@ -2433,7 +2433,7 @@ session_move_pre_start(const struct tls_session *session,/** * Moves the key to state to S_ACTIVE and also advances the multi_state state -* machine if this is the initial connection. +* machine if this is the initial connection. */ static void session_move_active(struct tls_multi *multi, struct tls_session *session,
@@ -2478,6 +2478,217 @@ session_move_active(struct tls_multi *multi, struct tls_session *session,show_tls_performance_stats ();#endif } + + +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) +{ +bool state_change = false; +struct key_state *ks = &session->key[KS_PRIMARY ];/* primary key */ + +/* Initial handshake */ +if (ks->state ==S_INITIAL ) + { + state_change =session_move_pre_start (session, ks); + } + +/* Are we timed out on receive? */ +if (now >= ks->must_negotiate && ks->state <S_ACTIVE ) + { +msg (D_TLS_ERRORS , +"TLS Error: TLS key negotiation failed to occur within %d seconds (check your network connectivity)" , + session->opt->handshake_window); + goto error; + } + +/* Wait for Initial Handshake ACK */ +if (ks->state ==S_PRE_START &&no_pending_reliable_packets (ks)) + { + ks->state =S_START ; + state_change = true; + +/* +* Attempt CRL reload before TLS negotiation. Won't be performed if +* the file was not modified since the last reload +*/ +if (session->opt->crl_file + && !(session->opt->ssl_flags &SSLF_CRL_VERIFY_DIR )) + { +tls_ctx_reload_crl (&session->opt->ssl_ctx, + session->opt->crl_file, session->opt->crl_file_inline); + } + +/* New connection, remove any old X509 env variables */ +tls_x509_clear_env (session->opt->es); + +dmsg (D_TLS_DEBUG_MED ,"STATE S_START" ); + } + +/* Wait for ACK */ +if (((ks->state ==S_GOT_KEY && !session->opt->server) + || (ks->state ==S_SENT_KEY && session->opt->server)) + &&no_pending_reliable_packets (ks)) + { +session_move_active (multi, session, to_link_socket_info, ks); + state_change = true; + } + +/* Reliable buffer to outgoing TCP/UDP (send up to CONTROL_SEND_ACK_MAX ACKs +* for previously received packets) */ +if (!to_link->len &&reliable_can_send (ks->send_reliable)) + { +int opcode; + +struct buffer *buf =reliable_send (ks->send_reliable, &opcode); +ASSERT (buf); +struct buffer b = *buf; +INCR_SENT ; + +write_control_auth (session, ks, &b, to_link_addr, opcode, +CONTROL_SEND_ACK_MAX , true); + *to_link = b; +dmsg (D_TLS_DEBUG ,"Reliable -> TCP/UDP" ); +return true; + } + +/* Write incoming ciphertext to TLS object */ +struct buffer *buf =reliable_get_buf_sequenced (ks->rec_reliable); +if (buf) + { +int status =0 ; +if (buf->len) + { + status =key_state_write_ciphertext (&ks->ks_ssl, buf); +if (status ==-1 ) + { +msg (D_TLS_ERRORS , +"TLS Error: Incoming Ciphertext -> TLS object write error" ); + goto error; + } + } +else + { + status =1 ; + } +if (status ==1 ) + { +reliable_mark_deleted (ks->rec_reliable, buf); + state_change = true; +dmsg (D_TLS_DEBUG ,"Incoming Ciphertext -> TLS" ); + } + } + +/* Read incoming plaintext from TLS object */ + buf = &ks->plaintext_read_buf; +if (!buf->len) + { +int status; + +ASSERT (buf_init (buf,0 )); + status =key_state_read_plaintext (&ks->ks_ssl, buf,TLS_CHANNEL_BUF_SIZE ); +update_time (); +if (status ==-1 ) + { +msg (D_TLS_ERRORS ,"TLS Error: TLS object -> incoming plaintext read error" ); + goto error; + } +if (status ==1 ) + { + state_change = true; +dmsg (D_TLS_DEBUG ,"TLS -> Incoming Plaintext" ); + +/* More data may be available, wake up again asap to check. */ + *wakeup =0 ; + } + } + +/* Send Key */ + buf = &ks->plaintext_write_buf; +if (!buf->len && ((ks->state ==S_START && !session->opt->server) + || (ks->state ==S_GOT_KEY && session->opt->server))) + { +if (!key_method_2_write (buf, multi, session)) + { + goto error; + } + + state_change = true; +dmsg (D_TLS_DEBUG_MED ,"STATE S_SENT_KEY" ); + ks->state =S_SENT_KEY ; + } + +/* Receive Key */ + buf = &ks->plaintext_read_buf; +if (buf->len + && ((ks->state ==S_SENT_KEY && !session->opt->server) + || (ks->state ==S_START && session->opt->server))) + { +if (!key_method_2_read (buf, multi, session)) + { + goto error; + } + + state_change = true; +dmsg (D_TLS_DEBUG_MED ,"STATE S_GOT_KEY" ); + ks->state =S_GOT_KEY ; + } + +/* Write outgoing plaintext to TLS object */ + buf = &ks->plaintext_write_buf; +if (buf->len) + { +int status =key_state_write_plaintext (&ks->ks_ssl, buf); +if (status ==-1 ) + { +msg (D_TLS_ERRORS , +"TLS ERROR: Outgoing Plaintext -> TLS object write error" ); + goto error; + } +if (status ==1 ) + { + state_change = true; +dmsg (D_TLS_DEBUG ,"Outgoing Plaintext -> TLS" ); + } + } + +/* Outgoing Ciphertext to reliable buffer */ +if (ks->state >=S_START ) + { + buf =reliable_get_buf_output_sequenced (ks->send_reliable); +if (buf) + { +int status =key_state_read_ciphertext (&ks->ks_ssl, buf, multi->opt.frame.tun_mtu); + +if (status ==-1 ) + { +msg (D_TLS_ERRORS , +"TLS Error: Ciphertext -> reliable TCP/UDP transport read error" ); + goto error; + } +if (status ==1 ) + { +reliable_mark_active_outgoing (ks->send_reliable, buf,P_CONTROL_V1 ); +INCR_GENERATED ; + state_change = true; +dmsg (D_TLS_DEBUG ,"Outgoing Ciphertext -> Reliable" ); + } + } + } + +return state_change; +error: +tls_clear_error (); + ks->state =S_ERROR ; +msg (D_TLS_ERRORS ,"TLS Error: TLS handshake failed" ); +INCR_ERROR ; +return false; + +}/* * This is the primary routine for processing TLS stuff inside the * the main event loop. When this routine exits
@@ -2495,9 +2706,6 @@ tls_process(struct tls_multi *multi,struct link_socket_info *to_link_socket_info, interval_t *wakeup) { - struct gc_arena gc = gc_new(); -bool state_change = false; -bool active = false; struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */struct key_state *ks_lame = &session->key[KS_LAME_DUCK ];/* retiring key */
@@ -2531,7 +2739,8 @@ tls_process(struct tls_multi *multi,msg (D_TLS_DEBUG_LOW ,"TLS: tls_process: killed expiring key" ); } - do +bool state_change = true; +while (state_change) {update_time ();
@@ -2541,209 +2750,15 @@ tls_process(struct tls_multi *multi,state_name (ks_lame->state), to_link->len, *wakeup); + state_change =tls_process_state (multi, session, to_link, to_link_addr, + to_link_socket_info, wakeup); - state_change = false; - -/* -* TLS activity is finished once we get to S_ACTIVE, -* though we will still process acknowledgements. -* -* CHANGED with 2.0 -> now we may send tunnel configuration -* info over the control channel. -*/ - -/* Initial handshake */ -if (ks->state ==S_INITIAL ) - { - state_change =session_move_pre_start (session, ks); - } - -/* Are we timed out on receive? */ -if (now >= ks->must_negotiate && ks->state <S_ACTIVE ) - { -msg (D_TLS_ERRORS , -"TLS Error: TLS key negotiation failed to occur within %d seconds (check your network connectivity)" , - session->opt->handshake_window); - goto error; - } - -/* Wait for Initial Handshake ACK */ -if (ks->state ==S_PRE_START &&no_pending_reliable_packets (ks)) - { - ks->state =S_START ; - state_change = true; - -/* -* Attempt CRL reload before TLS negotiation. Won't be performed if -* the file was not modified since the last reload -*/ -if (session->opt->crl_file - && !(session->opt->ssl_flags &SSLF_CRL_VERIFY_DIR )) - { -tls_ctx_reload_crl (&session->opt->ssl_ctx, - session->opt->crl_file, session->opt->crl_file_inline); - } - -/* New connection, remove any old X509 env variables */ -tls_x509_clear_env (session->opt->es); - -dmsg (D_TLS_DEBUG_MED ,"STATE S_START" ); - } - -/* Wait for ACK */ -if (((ks->state ==S_GOT_KEY && !session->opt->server) - || (ks->state ==S_SENT_KEY && session->opt->server)) - &&no_pending_reliable_packets (ks)) +if (ks->state ==S_ERROR ) { -session_move_active (multi, session, to_link_socket_info, ks); - state_change = true; - } - -/* Reliable buffer to outgoing TCP/UDP (send up to CONTROL_SEND_ACK_MAX ACKs -* for previously received packets) */ -if (!to_link->len &&reliable_can_send (ks->send_reliable)) - { -int opcode; - -struct buffer *buf =reliable_send (ks->send_reliable, &opcode); -ASSERT (buf); -struct buffer b = *buf; -INCR_SENT ; - -write_control_auth (session, ks, &b, to_link_addr, opcode, -CONTROL_SEND_ACK_MAX , true); - *to_link = b; - active = true; - state_change = true; -dmsg (D_TLS_DEBUG ,"Reliable -> TCP/UDP" ); -break ; - } - -/* Write incoming ciphertext to TLS object */ -struct buffer *buf =reliable_get_buf_sequenced (ks->rec_reliable); -if (buf) - { -int status =0 ; -if (buf->len) - { - status =key_state_write_ciphertext (&ks->ks_ssl, buf); -if (status ==-1 ) - { -msg (D_TLS_ERRORS , -"TLS Error: Incoming Ciphertext -> TLS object write error" ); - goto error; - } - } -else - { - status =1 ; - } -if (status ==1 ) - { -reliable_mark_deleted (ks->rec_reliable, buf); - state_change = true; -dmsg (D_TLS_DEBUG ,"Incoming Ciphertext -> TLS" ); - } - } - -/* Read incoming plaintext from TLS object */ - buf = &ks->plaintext_read_buf; -if (!buf->len) - { -int status; - -ASSERT (buf_init (buf,0 )); - status =key_state_read_plaintext (&ks->ks_ssl, buf,TLS_CHANNEL_BUF_SIZE ); -update_time (); -if (status ==-1 ) - { -msg (D_TLS_ERRORS ,"TLS Error: TLS object -> incoming plaintext read error" ); - goto error; - } -if (status ==1 ) - { - state_change = true; -dmsg (D_TLS_DEBUG ,"TLS -> Incoming Plaintext" ); - -/* More data may be available, wake up again asap to check. */ - *wakeup =0 ; - } - } - -/* Send Key */ - buf = &ks->plaintext_write_buf; -if (!buf->len && ((ks->state ==S_START && !session->opt->server) - || (ks->state ==S_GOT_KEY && session->opt->server))) - { -if (!key_method_2_write (buf, multi, session)) - { - goto error; - } - - state_change = true; -dmsg (D_TLS_DEBUG_MED ,"STATE S_SENT_KEY" ); - ks->state =S_SENT_KEY ; - } - -/* Receive Key */ - buf = &ks->plaintext_read_buf; -if (buf->len - && ((ks->state ==S_SENT_KEY && !session->opt->server) - || (ks->state ==S_START && session->opt->server))) - { -if (!key_method_2_read (buf, multi, session)) - { - goto error; - } - - state_change = true; -dmsg (D_TLS_DEBUG_MED ,"STATE S_GOT_KEY" ); - ks->state =S_GOT_KEY ; - } - -/* Write outgoing plaintext to TLS object */ - buf = &ks->plaintext_write_buf; -if (buf->len) - { -int status =key_state_write_plaintext (&ks->ks_ssl, buf); -if (status ==-1 ) - { -msg (D_TLS_ERRORS , -"TLS ERROR: Outgoing Plaintext -> TLS object write error" ); - goto error; - } -if (status ==1 ) - { - state_change = true; -dmsg (D_TLS_DEBUG ,"Outgoing Plaintext -> TLS" ); - } +return false; } -/* Outgoing Ciphertext to reliable buffer */ -if (ks->state >=S_START ) - { - buf =reliable_get_buf_output_sequenced (ks->send_reliable); -if (buf) - { -int status =key_state_read_ciphertext (&ks->ks_ssl, buf, multi->opt.frame.tun_mtu); - -if (status ==-1 ) - { -msg (D_TLS_ERRORS , -"TLS Error: Ciphertext -> reliable TCP/UDP transport read error" ); - goto error; - } -if (status ==1 ) - { -reliable_mark_active_outgoing (ks->send_reliable, buf,P_CONTROL_V1 ); -INCR_GENERATED ; - state_change = true; -dmsg (D_TLS_DEBUG ,"Outgoing Ciphertext -> Reliable" ); - } - } - } } -while (state_change);update_time ();
@@ -2755,7 +2770,6 @@ tls_process(struct tls_multi *multi,write_control_auth (session, ks, &buf, to_link_addr,P_ACK_V1 ,RELIABLE_ACK_SIZE , false); *to_link = buf; - active = true;dmsg (D_TLS_DEBUG ,"Dedicated ACK -> TCP/UDP" ); }
@@ -2778,6 +2792,8 @@ tls_process(struct tls_multi *multi, ks->established + session->opt->renegotiate_seconds - now); } +dmsg (D_TLS_DEBUG ,"TLS: tls_process: timeout set to %d" , *wakeup); +/* prevent event-loop spinning by setting minimum wakeup of 1 second */ if (*wakeup <=0 ) {
@@ -2785,22 +2801,18 @@ tls_process(struct tls_multi *multi,/* if we had something to send to remote, but to_link was busy, * let caller know we need to be called again soon */ - active = true; +return true; } -dmsg (D_TLS_DEBUG ,"TLS: tls_process: timeout set to %d" , *wakeup); +/* If any of the state changes resulted in the to_link buffer being +* set, we are also active */ +if (to_link->len) + { +return true; + } -gc_free (&gc); -return active; +return false; } - -error: -tls_clear_error (); - ks->state =S_ERROR ; -msg (D_TLS_ERRORS ,"TLS Error: TLS handshake failed" ); -INCR_ERROR ; -gc_free (&gc); -return false; }/*
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 15/28 Remove pointless indentation from tls_process.
In reply to Arne SchwabeThis is probably a result from earlier code that still needed to be C89 compatible add probably added this to allow variable decleration
--- src/openvpn/ssl.c | 56 +++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 29 deletions(-)
src/openvpn/ssl.c +27 -29
@@ -2774,45 +2774,43 @@ tls_process(struct tls_multi *multi, }/* When should we wake up again? */ +if (ks->state >=S_INITIAL ) { -if (ks->state >=S_INITIAL ) - { -compute_earliest_wakeup (wakeup, -reliable_send_timeout (ks->send_reliable)); - -if (ks->must_negotiate) - { -compute_earliest_wakeup (wakeup, ks->must_negotiate - now); - } - } +compute_earliest_wakeup (wakeup, +reliable_send_timeout (ks->send_reliable)); -if (ks->established && session->opt->renegotiate_seconds) +if (ks->must_negotiate) { -compute_earliest_wakeup (wakeup, - ks->established + session->opt->renegotiate_seconds - now); +compute_earliest_wakeup (wakeup, ks->must_negotiate - now); } + } -dmsg (D_TLS_DEBUG ,"TLS: tls_process: timeout set to %d" , *wakeup); +if (ks->established && session->opt->renegotiate_seconds) + { +compute_earliest_wakeup (wakeup, + ks->established + session->opt->renegotiate_seconds - now); + } -/* prevent event-loop spinning by setting minimum wakeup of 1 second */ -if (*wakeup <=0 ) - { - *wakeup =1 ; +dmsg (D_TLS_DEBUG ,"TLS: tls_process: timeout set to %d" , *wakeup); -/* if we had something to send to remote, but to_link was busy, -* let caller know we need to be called again soon */ -return true; - } +/* prevent event-loop spinning by setting minimum wakeup of 1 second */ +if (*wakeup <=0 ) + { + *wakeup =1 ; -/* If any of the state changes resulted in the to_link buffer being -* set, we are also active */ -if (to_link->len) - { -return true; - } +/* if we had something to send to remote, but to_link was busy, +* let caller know we need to be called again soon */ +return true; + } -return false; +/* If any of the state changes resulted in the to_link buffer being +* set, we are also active */ +if (to_link->len) + { +return true; } + +return false; }/*
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 16/28 Move CRL reload to key_state_init from S_START transition
In reply to Arne SchwabeThe current place that we reload is a bit more efficient since it only triggers reload after a completed 3way handshake. On the other hand the key_state_init is a much more logical place and with the upcoming HMAC based UDP code and TCP code, the initialisation will only be done after a 3way handshake.
--- src/openvpn/ssl.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
src/openvpn/ssl.c +11 -12
@@ -958,6 +958,17 @@ key_state_init(struct tls_session *session, struct key_state *ks)#ifdef ENABLE_MANAGEMENT ks->mda_key_id = session->opt->mda_context->mda_key_id_counter++;#endif + +/* +* Attempt CRL reload before TLS negotiation. Won't be performed if +* the file was not modified since the last reload +*/ +if (session->opt->crl_file + && !(session->opt->ssl_flags &SSLF_CRL_VERIFY_DIR )) + { +tls_ctx_reload_crl (&session->opt->ssl_ctx, + session->opt->crl_file, session->opt->crl_file_inline); + } }
@@ -2512,20 +2523,8 @@ tls_process_state(struct tls_multi *multi, ks->state =S_START ; state_change = true; -/* -* Attempt CRL reload before TLS negotiation. Won't be performed if -* the file was not modified since the last reload -*/ -if (session->opt->crl_file - && !(session->opt->ssl_flags &SSLF_CRL_VERIFY_DIR )) - { -tls_ctx_reload_crl (&session->opt->ssl_ctx, - session->opt->crl_file, session->opt->crl_file_inline); - } -/* New connection, remove any old X509 env variables */ tls_x509_clear_env (session->opt->es); -dmsg (D_TLS_DEBUG_MED ,"STATE S_START" ); }
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 17/28 Implement constructing a control channel reset client as standalone fucntion
In reply to Arne SchwabeThis implement creating a reset packet without needing to setup a full control session.
--- src/openvpn/packet_id.h | 15 ++++++ src/openvpn/ssl.h | 6 --- src/openvpn/ssl_pkt.c | 34 +++++++++++- src/openvpn/ssl_pkt.h | 19 +++++++ tests/unit_tests/openvpn/test_pkt.c | 84 ++++++++++++++++++++++++++++- 5 files changed, 149 insertions(+), 9 deletions(-)
src/openvpn/packet_id.h +15 -0
@@ -289,6 +289,21 @@ packet_id_persist_save_obj(struct packet_id_persist *p, const struct packet_id * } } +/** +* Reset the current send packet id to its initial state. +* Use very carefully only as (e.g. in the standalone reset packet context) +* to avoid sending more than one packet with the same packet id (that is not +* also a resend like the reset packet) +* +* @param p the packet structure to modify +*/ +static inline void +reset_packet_id_send (struct packet_id_send *p) +{ + p->time =0 ; + p->id =0 ; +} +const char *packet_id_net_print (const struct packet_id_net *pin,bool print_timestamp,struct gc_arena *gc); static inline int
src/openvpn/ssl.h +0 -6
@@ -53,12 +53,6 @@*/ #define CONTROL_SEND_ACK_MAX 4 -/* -* Define number of buffers for send and receive in the reliability layer. -*/ -#define TLS_RELIABLE_N_SEND_BUFFERS 4/* also window size for reliability layer */ -#define TLS_RELIABLE_N_REC_BUFFERS 8 -/* * Various timeouts */
src/openvpn/ssl_pkt.c +33 -1
@@ -397,4 +397,36 @@ error:tls_clear_error ();gc_free (&gc);return VERDICT_INVALID ; -} +} + +struct buffer +tls_reset_standalone (struct tls_auth_standalone *tas, +struct session_id *own_sid, +struct session_id *remote_sid, +uint8_t header) +{ +struct buffer buf =alloc_buf (tas->frame.buf.payload_size); +ASSERT (buf_init (&buf, tas->frame.buf.headroom)); + +/* Reliable ACK structure */ +/* Length of the ACK structure - 1 ACK */ +buf_write_u8 (&buf,1 ); + +/* ACKed packet - first packet's id is always 0 */ +buf_write_u32 (&buf,0 ); + +/* Remote session id */ +buf_write (&buf, remote_sid->id,SID_SIZE ); + +/* Packet ID of our own packet: Our reset packet is always using +* packet id 0 since it is the first packet */ +packet_id_type net_pid =htonpid (0 ); + +ASSERT (buf_write (&buf, &net_pid,sizeof (net_pid))); + +/* Add tls-auth/tls-crypt wrapping, this might replace buf */ +tls_wrap_control (&tas->tls_wrap, header, &buf, own_sid); + +return buf; +} +
src/openvpn/ssl_pkt.h +19 -0
@@ -60,6 +60,12 @@#define P_FIRST_OPCODE 3#define P_LAST_OPCODE 10 +/* +* Define number of buffers for send and receive in the reliability layer. +*/ +#define TLS_RELIABLE_N_SEND_BUFFERS 4/* also window size for reliability layer */ +#define TLS_RELIABLE_N_REC_BUFFERS 8 +/* * Used in --mode server mode to check tls-auth signature on initial * packets received from new clients.
@@ -157,6 +163,19 @@ read_control_auth(struct buffer *buf,const struct link_socket_actual *from, const struct tls_options *opt); + +/** +* This function creates a reset packet using the information +* from the tls pre decrypt state. +* +* The returned buf need to be free with \c free_buf +*/ +struct buffer +tls_reset_standalone (struct tls_auth_standalone *tas, +struct session_id *own_sid, +struct session_id *remote_sid, +uint8_t header); +static inline const char *packet_opcode_name (int op) {
tests/unit_tests/openvpn/test_pkt.c +82 -2
108 lines not on this page · raw message
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 18/28 Implement stateless, HMAC basedsesssion id three way handshake
In reply to Arne SchwabeOpenVPN currently has a bit of a weakness in its early three way handshake
A single client reset packet (first packet of the handshake) will
- trigger creating session on the server side leading to poential ressource exhaustian
- make the server respond with 3 answers trying to get an ACK for its answer making it a amplification
Instead of allocating a connection for each client on the initial packet OpenVPN will now send back a response that contains an HMAC based cookie that the client will need to respond to. This eliminates the amplification attack and resource exhaustion attacks. For tls-crypt-v2 client HMAC based handshake is not used yet
--- doc/doxygen/doc_protocol_overview.h | 2 + src/openvpn/init.c | 11 +- src/openvpn/mudp.c | 106 ++++++++++-- src/openvpn/multi.h | 3 + src/openvpn/openvpn.h | 6 + src/openvpn/reliable.c | 2 +- src/openvpn/ssl.c | 50 +++++- src/openvpn/ssl.h | 8 + src/openvpn/ssl_pkt.c | 104 +++++++++++- src/openvpn/ssl_pkt.h | 57 ++++++- tests/unit_tests/openvpn/test_pkt.c | 247 +++++++++++++++++++++++++--- 11 files changed, 542 insertions(+), 54 deletions(-)
doc/doxygen/doc_protocol_overview.h +2 -0
@@ -118,6 +118,8 @@* parts: * * - local \c session_id (random 64 bit value to identify TLS session). +* (the tls-server side uses a HMAC of the client to create a pseudo +* random number for a SYN Cookie like approach) * - HMAC signature of entire encapsulation header for HMAC firewall * [only if \c --tls-auth is specified] (usually 16 or 20 bytes). * - packet-id for replay protection (4 or 8 bytes, includes sequence
src/openvpn/init.c +10 -1
@@ -54,6 +54,7 @@#include "forward.h" #include "auth_token.h" #include "mss.h" +#include "mudp.h" #include "memdbg.h"
@@ -67,6 +68,7 @@ static const char *saved_pid_file_name; /* GLOBAL */#define CF_LOAD_PERSISTED_PACKET_ID (1<<0)#define CF_INIT_TLS_MULTI (1<<1)#define CF_INIT_TLS_AUTH_STANDALONE (1<<2) +#define CF_INIT_SESIONID_HMAC (1<<2)static void do_init_first_time (struct context *c);static bool do_deferred_p2p_ncp (struct context *c);
@@ -2974,6 +2976,12 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) { c->c2.tls_auth_standalone =tls_auth_standalone_init (&to, &c->c2.gc); } + +if (flags &CF_INIT_SESIONID_HMAC ) + { + c->c2.session_id_hmac =session_id_hmac_init (); + } + } static void
@@ -2992,6 +3000,7 @@ do_init_frame_tls(struct context *c)tls_init_control_channel_frame_parameters (&c->c2.frame, &c->c2.tls_auth_standalone->frame);frame_print (&c->c2.tls_auth_standalone->frame,D_MTU_INFO ,"TLS-Auth MTU parms" ); + c->c2.tls_auth_standalone->tls_wrap.work =alloc_buf_gc (BUF_SIZE (&c->c2.frame), &c->c2.gc); } }
@@ -4077,7 +4086,7 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int funsigned int crypto_flags =0 ;if (c->mode ==CM_TOP ) { - crypto_flags =CF_INIT_TLS_AUTH_STANDALONE ; + crypto_flags =CF_INIT_TLS_AUTH_STANDALONE |CF_INIT_SESIONID_HMAC ; }else if (c->mode ==CM_P2P ) {
src/openvpn/mudp.c +96 -10
168 lines not on this page · raw message
src/openvpn/multi.h +3 -0
@@ -191,6 +191,9 @@ struct multi_context {struct context top;/**< Storage structure for process-wide * configuration. */ +struct buffer hmac_reply; +struct link_socket_actual *hmac_reply_dest; +/* * Timer object for stale route check */
src/openvpn/openvpn.h +6 -0
@@ -330,6 +330,12 @@ struct context_2* received from a new client. See the * \c --tls-auth commandline option. */ + +hmac_ctx_t *session_id_hmac; +/**< the HMAC we use to generate and verify our syn cookie like +* session ids from the server. +*/ +/* used to optimize calls to tls_multi_process */ struct interval tmp_int;
src/openvpn/reliable.c +1 -1
@@ -166,7 +166,7 @@ reliable_ack_read(struct reliable_ack *ack, }if (ack->len >=1 && (!session_id_defined (&session_id_remote) - || !session_id_equal (&session_id_remote, sid))) + || !session_id_equal (&session_id_remote, sid))) {struct gc_arena gc =gc_new (); dmsg(D_REL_LOW,
src/openvpn/ssl.c +43 -7
109 lines not on this page · raw message
src/openvpn/ssl.h +8 -0
@@ -556,4 +556,12 @@ tls_session_generate_data_channel_keys(struct tls_session *session);void load_xkey_provider (void ); +/* Special method to skip the three way handshake RESET stages. This is +* used by the HMAC code when seeing a packet that matches the previous +* HMAC based stateless server state */ +bool +session_skip_to_pre_start (struct tls_session *session, +struct tls_pre_decrypt_state *state, +struct link_socket_actual *from); +#endif /* ifndef OPENVPN_SSL_H */
src/openvpn/ssl_pkt.c +101 -3
133 lines not on this page · raw message
src/openvpn/ssl_pkt.h +52 -5
82 lines not on this page · raw message
tests/unit_tests/openvpn/test_pkt.c +220 -27
355 lines not on this page · raw message
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 19/28 Make buf_write_u8/16/32 take the type they pretend to take
In reply to Arne SchwabeThis functions should accept the type of integer they say to write. Calling the u32 function with an integer that is actually 32 bit unsigned gives compiler warnings.
--- src/openvpn/buffer.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
src/openvpn/buffer.h +6 -7
@@ -694,23 +694,22 @@ buf_write_prepend(struct buffer *dest, const void *src, int size) }static inline bool -buf_write_u8 (struct buffer *dest,int data) +buf_write_u8 (struct buffer *dest,uint8_t data) { -uint8_t u8 = (uint8_t ) data; -return buf_write (dest, &u8,sizeof (uint8_t )); +return buf_write (dest, &data,sizeof (uint8_t )); }static inline bool -buf_write_u16 (struct buffer *dest,int data) +buf_write_u16 (struct buffer *dest,uint16_t data) { -uint16_t u16 =htons ((uint16_t ) data); +uint16_t u16 =htons (data);return buf_write (dest, &u16,sizeof (uint16_t )); }static inline bool -buf_write_u32 (struct buffer *dest,int data) +buf_write_u32 (struct buffer *dest,uint32_t data) { -uint32_t u32 =htonl ((uint32_t ) data); +uint32_t u32 =htonl (data);return buf_write (dest, &u32,sizeof (uint32_t )); }
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 20/28 Change reliable_get_buf_sequenced to reliable_get_entry_sequenced
In reply to Arne SchwabeThis returns not just the buffer of a reliable_entry but the whole entry. This allows the caller to also inspect the original opcode and packet id.
--- src/openvpn/reliable.c | 6 +++--- src/openvpn/reliable.h | 7 +++---- src/openvpn/ssl.c | 7 ++++--- 3 files changed, 10 insertions(+), 10 deletions(-)
src/openvpn/reliable.c +3 -3
@@ -533,8 +533,8 @@ reliable_get_buf_output_sequenced(struct reliable *rel) }/* get active buffer for next sequentially increasing key ID */ -struct buffer * -reliable_get_buf_sequenced (struct reliable *rel) +struct reliable_entry * +reliable_get_entry_sequenced (struct reliable *rel) {int i;for (i =0 ; i < rel->size; ++i)
@@ -542,7 +542,7 @@ reliable_get_buf_sequenced(struct reliable *rel)struct reliable_entry *e = &rel->array[i];if (e->active && e->packet_id == rel->packet_id) { -return &e->buf; +return e; } }return NULL ;
src/openvpn/reliable.h +3 -4
@@ -351,11 +351,10 @@ bool reliable_ack_acknowledge_packet_id(struct reliable_ack *ack, packet_id_type* @param rel The reliable structure from which to retrieve the * buffer. * -* @return A pointer to the buffer of the entry with the next -* sequential key ID. If no such entry is present, this function -* returns NULL. +* @return A pointer to the entry with the next sequential key ID. +* If no such entry is present, this function returns NULL. */ -struct buffer *reliable_get_buf_sequenced (struct reliable *rel); +struct reliable_entry *reliable_get_entry_sequenced (struct reliable *rel);/** * Remove an entry from a reliable structure.
src/openvpn/ssl.c +4 -3
@@ -2592,9 +2592,10 @@ tls_process_state(struct tls_multi *multi, }/* Write incoming ciphertext to TLS object */ -struct buffer *buf =reliable_get_buf_sequenced (ks->rec_reliable); -if (buf) +struct reliable_entry *entry =reliable_get_entry_sequenced (ks->rec_reliable); +if (entry) { +struct buffer *buf = &entry->buf;int status =0 ;if (buf->len) {
@@ -2619,7 +2620,7 @@ tls_process_state(struct tls_multi *multi, }/* Read incoming plaintext from TLS object */ - buf = &ks->plaintext_read_buf; +struct buffer *buf = &ks->plaintext_read_buf;if (!buf->len) {int status;
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 21/28 Extract read_incoming_tls_ciphertext into function
In reply to Arne SchwabeThis makes the code a bit more structured and easier to read.
--- src/openvpn/ssl.c | 53 +++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 20 deletions(-)
src/openvpn/ssl.c +33 -20
@@ -2525,6 +2525,37 @@ session_skip_to_pre_start(struct tls_session *session,return session_move_pre_start (session, ks, true); } +/** +* Read incoming ciphertext and passes it to the buffer of the SSL library. +* Returns false if an error is encountered that should abort the session. +*/ +static bool +read_incoming_tls_ciphertext (struct buffer *buf,struct key_state *ks, +bool *state_change) +{ +int status =0 ; +if (buf->len) + { + status =key_state_write_ciphertext (&ks->ks_ssl, buf); +if (status ==-1 ) + { +msg (D_TLS_ERRORS , +"TLS Error: Incoming Ciphertext -> TLS object write error" ); +return false; + } + } +else + { + status =1 ; + } +if (status ==1 ) + { +reliable_mark_deleted (ks->rec_reliable, buf); + *state_change = true; +dmsg (D_TLS_DEBUG ,"Incoming Ciphertext -> TLS" ); + } +return true; +} static bool
@@ -2595,27 +2626,9 @@ tls_process_state(struct tls_multi *multi,struct reliable_entry *entry =reliable_get_entry_sequenced (ks->rec_reliable);if (entry) { -struct buffer *buf = &entry->buf; -int status =0 ; -if (buf->len) - { - status =key_state_write_ciphertext (&ks->ks_ssl, buf); -if (status ==-1 ) - { -msg (D_TLS_ERRORS , -"TLS Error: Incoming Ciphertext -> TLS object write error" ); - goto error; - } - } -else - { - status =1 ; - } -if (status ==1 ) +if (!read_incoming_tls_ciphertext (&entry->buf, ks, &state_change)) { -reliable_mark_deleted (ks->rec_reliable, buf); - state_change = true; -dmsg (D_TLS_DEBUG ,"Incoming Ciphertext -> TLS" ); + goto error; } }
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 22/28 Implement HMAC based session id for tls-crypt v2
In reply to Arne SchwabeTls-crypt v2 is more complicated to implement a proper stateless handshake. To allow state handshake this commit does
- introduce a new packet CONTROL_WKC_V1 that repeats the wrapped client key.
- introduce a way to negotiate the support for this packet in the three way handshake
Details about the protocol changes are in tls-crypt-v2.txt. Optional arguments to the tls-crypt-v2 option have been added to explicitly allow or disallow client that do not support the stateless handshake.
--- Changes.rst | 8 ++ doc/man-sections/tls-options.rst | 14 ++++ doc/tls-crypt-v2.txt | 41 +++++++++ src/openvpn/crypto.h | 8 ++ src/openvpn/init.c | 4 + src/openvpn/mudp.c | 89 +++++++++++++++----- src/openvpn/options.c | 13 +++ src/openvpn/options.h | 3 + src/openvpn/reliable.h | 2 - src/openvpn/ssl.c | 125 +++++++++++++++++++++++++--- src/openvpn/ssl_pkt.c | 28 +++++-- src/openvpn/ssl_pkt.h | 36 +++++++- tests/unit_tests/openvpn/test_pkt.c | 8 +- 13 files changed, 333 insertions(+), 46 deletions(-)
Changes.rst +8 -0
@@ -69,6 +69,14 @@ Improved ``--mssfix`` and ``--fragment`` calculation account and the resulting size is specified as the total size of the VPN packets including IP and UDP headers. +Cookie based handshake for UDP server + Instead of allocating a connection for each client on the initial packet + OpenVPN will now send back a response that contains an HMAC based cookie + that the client will need to respond to. This eliminates the amplification + attack and resource exhaustion attacks. For tls-crypt-v2 clients, this + requires OpenVPN 2.6 clients or later and the tls-crypt-v2 option allows + controlling if older clients are accepted. + Deprecated features ------------------- ``inetd`` has been removed
doc/man-sections/tls-options.rst +14 -0
@@ -486,6 +486,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa 8000 years'. --tls-crypt-v2 keyfile + + Valid syntax: + :: + + tls-crypt-v2 keyfile [force-cookie] + tls-crypt-v2 keyfile [allow-noncookie] + Use client-specific tls-crypt keys. For clients, ``keyfile`` is a client-specific tls-crypt key. Such a key
@@ -501,6 +508,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa client is using client-specific keys, and automatically select the right mode. + The optional parameters :code:`force-cookie` allows only tls-crypt-v2 + clients that support a cookie based stateless three way handshake that + avoids replay attacks and state exhaustion on the server side (OpenVPN + 2.6 and later). The option :code:`allow-noncookie` explicilty allows + older tls-crypt-v2 clients. The default is (currently) + :code:`allow-noncookie`. + --tls-crypt-v2-verify cmd Run command ``cmd`` to verify the metadata of the client-specific tls-crypt-v2 key of a connecting client. This allows server
doc/tls-crypt-v2.txt +41 -0
47 lines not on this page · raw message
src/openvpn/crypto.h +8 -0
@@ -256,6 +256,14 @@ struct crypto_options/**< Bit-flag indicating that data channel key derivation * is done using TLS keying material export [RFC5705] */ +#define CO_RESEND_WKC (1<<4) +/**< Bit-flag indicating that the client is expected to +* resend the wrapped client key with the 2nd packet (packet-id 1) +* like with the HARD_RESET_CLIENT_V3 packet */ +#define CO_FORCE_TLSCRYPTV2_COOKIE (1<<5) +/**< Bit-flag indicating that we do not allow clients that do +* not support resending the wrapped client key (WKc) with the +* third packet of the three-way handshake */ unsigned int flags;/**< Bit-flags determining behavior of * security operation functions. */ };
src/openvpn/init.c +4 -0
@@ -2961,6 +2961,10 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) { to.tls_wrap.tls_crypt_v2_server_key = c->c1.ks.tls_crypt_v2_server_key; to.tls_crypt_v2_verify_script = c->options.tls_crypt_v2_verify_script; +if (options->ce.tls_crypt_v2_force_cookie) + { + to.tls_wrap.opt.flags |=CO_FORCE_TLSCRYPTV2_COOKIE ; + } } }
src/openvpn/mudp.c +68 -21
125 lines not on this page · raw message
src/openvpn/options.c +13 -0
@@ -8896,6 +8896,19 @@ add_option(struct options *options, options->ce.tls_crypt_v2_file = p[1 ]; options->ce.tls_crypt_v2_file_inline = is_inline; } + +if (p[2 ] &&streq (p[2 ],"force-cookie" )) + { + options->ce.tls_crypt_v2_force_cookie = true; + } +else if (p[2 ] &&streq (p[2 ],"allow-noncookie" )) + { + options->ce.tls_crypt_v2_force_cookie = false; + } +else if (p[2 ]) + { +msg (msglevel,"Unsupported tls-crypt-v2 argument: %s" , p[2 ]); + } }else if (streq (p[0 ],"tls-crypt-v2-verify" ) && p[1 ] && !p[2 ]) {
src/openvpn/options.h +3 -0
@@ -162,6 +162,9 @@ struct connection_entry* authenticated encryption v2 */ const char *tls_crypt_v2_file;bool tls_crypt_v2_file_inline; + +/* Allow only client that support resending the wrapped client key */ +bool tls_crypt_v2_force_cookie; };struct remote_entry
src/openvpn/reliable.h +0 -2
@@ -361,8 +361,6 @@ struct reliable_entry *reliable_get_entry_sequenced(struct reliable *rel);* * @param rel The reliable structure associated with the given buffer. * @param buf The buffer of the reliable entry which is to be removed. -* @param inc_pid If true, the reliable structure's packet ID counter -* will be incremented. */ void reliable_mark_deleted (struct reliable *rel,struct buffer *buf);
src/openvpn/ssl.c +114 -11
177 lines not on this page · raw message
src/openvpn/ssl_pkt.c +23 -5
@@ -148,7 +148,8 @@ tls_wrap_control(struct tls_wrap_ctx *ctx, uint8_t header, struct buffer *buf,return ; } -if ((header >>P_OPCODE_SHIFT ) ==P_CONTROL_HARD_RESET_CLIENT_V3 ) +if ((header >>P_OPCODE_SHIFT ) ==P_CONTROL_HARD_RESET_CLIENT_V3 + || (header >>P_OPCODE_SHIFT ) ==P_CONTROL_WKC_V1 ) {if (!buf_copy (&ctx->work, ctx->tls_crypt_v2_wkc))
@@ -197,7 +198,8 @@ read_control_auth(struct buffer *buf,bool ret = false;const uint8_t opcode = *(BPTR (buf)) >>P_OPCODE_SHIFT ; -if (opcode ==P_CONTROL_HARD_RESET_CLIENT_V3 +if ((opcode ==P_CONTROL_HARD_RESET_CLIENT_V3 + || opcode ==P_CONTROL_WKC_V1 ) && !tls_crypt_v2_extract_client_key (buf, ctx, opt)) { msg(D_TLS_ERRORS,
@@ -321,6 +323,7 @@ tls_pre_decrypt_lite(const struct tls_auth_standalone *tas,if (op !=P_CONTROL_HARD_RESET_CLIENT_V2 && op !=P_CONTROL_HARD_RESET_CLIENT_V3 && op !=P_CONTROL_V1 + && op !=P_CONTROL_WKC_V1 && op !=P_ACK_V1 ) {/*
@@ -397,6 +400,10 @@ tls_pre_decrypt_lite(const struct tls_auth_standalone *tas, {return VERDICT_VALID_RESET_V3 ; } + else if (op == P_CONTROL_WKC_V1) + { +return VERDICT_VALID_WKC_V1 ; + } else {return VERDICT_VALID_RESET_V2 ;
@@ -410,10 +417,12 @@ error:struct buffer -tls_reset_standalone (struct tls_auth_standalone *tas, +tls_reset_standalone (struct tls_wrap_ctx *ctx, +struct tls_auth_standalone *tas,struct session_id *own_sid,struct session_id *remote_sid, -uint8_t header) +uint8_t header, +bool tlscryptv2) {struct buffer buf =alloc_buf (tas->frame.buf.payload_size);ASSERT (buf_init (&buf, tas->frame.buf.headroom));
@@ -434,8 +443,17 @@ tls_reset_standalone(struct tls_auth_standalone *tas,ASSERT (buf_write (&buf, &net_pid,sizeof (net_pid))); +/* Add indication for tls-crypt-v2 to resend the packet with the with +* reply */ +if (tlscryptv2) + { +buf_write_u16 (&buf,EARLY_NEG_TLV_FLAG );/* TYPE: flags */ +buf_write_u16 (&buf,sizeof (uint16_t )); +buf_write_u16 (&buf,EARLY_NEG_FLAG_RESEND_WKC ); + } +/* Add tls-auth/tls-crypt wrapping, this might replace buf */ -tls_wrap_control (&tas->tls_wrap, header, &buf, own_sid); +tls_wrap_control (ctx, header, &buf, own_sid);return buf; }
src/openvpn/ssl_pkt.h +33 -3
@@ -54,11 +54,15 @@/* indicates key_method >= 2 and client-specific tls-crypt key */ #define P_CONTROL_HARD_RESET_CLIENT_V3 10/* initial key from client, forget previous state */ +/* Variant of P_CONTROL_V1 but with appended wrapped key +* like P_CONTROL_HARD_RESET_CLIENT_V3 */ +#define P_CONTROL_WKC_V1 11 +/* define the range of legal opcodes * Since we do no longer support key-method 1 we consider * the v1 op codes invalid */ #define P_FIRST_OPCODE 3 -#define P_LAST_OPCODE 10 +#define P_LAST_OPCODE 11/* * Define number of buffers for send and receive in the reliability layer.
@@ -86,6 +90,8 @@ enum first_packet_verdict {/** This packet is a valid ACK control packet from the peer, * i.e. it has a valid session id hmac in it */ VERDICT_VALID_ACK_V1 , +/** The packet is a valid control packet with appended wrapped client key */ +VERDICT_VALID_WKC_V1 ,/** the packet failed on of the various checks */ VERDICT_INVALID };
@@ -218,10 +224,12 @@ read_control_auth(struct buffer *buf,* The returned buf need to be free with \c free_buf */ struct buffer -tls_reset_standalone (struct tls_auth_standalone *tas, +tls_reset_standalone (struct tls_wrap_ctx *ctx, +struct tls_auth_standalone *tas,struct session_id *own_sid,struct session_id *remote_sid, -uint8_t header); +uint8_t header, +bool b);static inline const char *packet_opcode_name (int op)
@@ -249,6 +257,9 @@ packet_opcode_name(int op)case P_CONTROL_V1 :return "P_CONTROL_V1" ; +case P_CONTROL_WKC_V1 : +return "P_CONTROL_WKC_V1" ; +case P_ACK_V1 :return "P_ACK_V1" ;
@@ -262,4 +273,23 @@ packet_opcode_name(int op) return "P_???"; } } + +/* initial packet id (instead of 0) that indicates that the peer supports +* early protocol negotiation. This will make the packet id turn a bit faster +* but the network time part of the packet id could take care of that. And +* this is also a rather theoretical scenario as it still needs more than +* 2^31 control channel packets to happen */ +#define EARLY_NEG_MASK 0xff000000 +#define EARLY_NEG_START 0x0a000000 + +#define EARLY_NEG_RESENDWKC 0x00010000 + + +/* Early negotiation that part of the server response in the RESET_V2 packet. +* Since clients that announce early negotiation support will treat the payload +* of reset packets special and parse it as TLV messages. +* as TLV (type, length, value) */ +#define EARLY_NEG_TLV_FLAG 0x01 +#define EARLY_NEG_FLAG_RESEND_WKC 0x01#endif /* ifndef SSL_PKT_H */ +
tests/unit_tests/openvpn/test_pkt.c +4 -4
@@ -531,7 +531,7 @@ test_generate_reset_packet_plain(void **ut_state)uint8_t header =0 | (P_CONTROL_HARD_RESET_CLIENT_V2 <<P_OPCODE_SHIFT ); -struct buffer buf =tls_reset_standalone (&tas, &client_id, &server_id, header); +struct buffer buf =tls_reset_standalone (&tas, &client_id, &server_id, header,0 ,0 ); verdict =tls_pre_decrypt_lite (&tas, &state, &from, &buf);
@@ -539,7 +539,7 @@ test_generate_reset_packet_plain(void **ut_state)/* Assure repeated generation of reset is deterministic/stateless*/ assert_memory_equal (state.peer_session_id.id, client_id.id,SID_SIZE ); -struct buffer buf2 =tls_reset_standalone (&tas, &client_id, &server_id, header); +struct buffer buf2 =tls_reset_standalone (&tas, &client_id, &server_id, header,0 ,0 );assert_int_equal (BLEN (&buf),BLEN (&buf2));assert_memory_equal (BPTR (&buf),BPTR (&buf2),BLEN (&buf));free_buf (&buf2);
@@ -566,7 +566,7 @@ test_generate_reset_packet_tls_auth(void **ut_state) now =0x22446688 ;reset_packet_id_send (&tas_client.tls_wrap.opt.packet_id.send); -struct buffer buf =tls_reset_standalone (&tas_client, &client_id, &server_id, header); +struct buffer buf =tls_reset_standalone (&tas_client, &client_id, &server_id, header,0 ,0 );enum first_packet_verdict verdict =tls_pre_decrypt_lite (&tas_server, &state, &from, &buf);assert_int_equal (verdict,VERDICT_VALID_RESET_V2 );
@@ -575,7 +575,7 @@ test_generate_reset_packet_tls_auth(void **ut_state)/* Assure repeated generation of reset is deterministic/stateless*/ reset_packet_id_send (&tas_client.tls_wrap.opt.packet_id.send); -struct buffer buf2 =tls_reset_standalone (&tas_client, &client_id, &server_id, header); +struct buffer buf2 =tls_reset_standalone (&tas_client, &client_id, &server_id, header,0 ,0 );assert_int_equal (BLEN (&buf),BLEN (&buf2));assert_memory_equal (BPTR (&buf),BPTR (&buf2),BLEN (&buf));free_buf (&buf2);
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 23/28 Optimise three-way handshake condition for S_PRE_START to S_START
In reply to Arne SchwabeWe move to the S_START when we have finished the three-way handshake. After the three way handshake is done, the client will send the TLS Client Hello packet.
Currently we consider the three way handshake only complete if all outgoing packet have been acked (which in this case is the one HARD_RESET_CLIENT or HARD_RESET_SERVER) and also all akcs for incoming packets are send out.
The outgoing packet ack is important as it signal that the other side is really responding. The need to send out all ACKs before moving to the next state also breaks piggybacking the ACKs onto the next control packet.
With this change both server and client will only send a P_CONTROL_V1 with the TLS Client Hello and the TLS Server Hello with piggybacked ack instead sending an P_ACK_V1 + P_CONTROL_V1, recuding the number of packets in a handshake by 2.
This also allows the server to avoid resending P_CONTROL_HARD_RESET_V2 to complete the three-way handshake with HMAC. Only packets with an ACK contain the remote session id that we need for HMAC session id verification. The ACK_V1 packet that complets this three-way handshake can get lost. But the P_CONTROL_V1 with the piggybacked ACK will get retransmitted. This allows to put the burden of retransmission fully on the client.
The S_GOT_KEY/S_SENT_KEY -> S_ACTIVE is similar. We do not need to wait for the ack packet to be sent to move the state forward. This has however no effect on actual packets since there are normally no outstanding ACKs here.
--- src/openvpn/ssl.c | 15 +++++---------- tests/unit_tests/openvpn/test_pkt.c | 8 ++++---- 2 files changed, 9 insertions(+), 14 deletions(-)
src/openvpn/ssl.c +5 -10
@@ -1796,13 +1796,6 @@ flush_payload_buffer(struct key_state *ks) } } -/* true if no in/out acknowledgements pending */ -static bool -no_pending_reliable_packets (struct key_state *ks) -{ -return (reliable_empty (ks->send_reliable) &&reliable_ack_empty (ks->rec_ack)); -} -/* * Move the active key to the lame duck key and reinitialize the * active key.
@@ -2646,8 +2639,10 @@ tls_process_state(struct tls_multi *multi, goto error; } -/* Wait for Initial Handshake ACK */ -if (ks->state ==S_PRE_START &&no_pending_reliable_packets (ks)) +/* Check if the initial three-way Handshake is complete. +* This handshake can be considered to be complete when our own +* initial packet can been successfully acked. */ +if (ks->state ==S_PRE_START &&reliable_empty (ks->send_reliable)) { ks->state =S_START ; state_change = true;
@@ -2660,7 +2655,7 @@ tls_process_state(struct tls_multi *multi,/* Wait for ACK */ if (((ks->state ==S_GOT_KEY && !session->opt->server) || (ks->state ==S_SENT_KEY && session->opt->server)) - &&no_pending_reliable_packets (ks)) + &&reliable_empty (ks->send_reliable)) {session_move_active (multi, session, to_link_socket_info, ks); state_change = true;
tests/unit_tests/openvpn/test_pkt.c +4 -4
@@ -531,7 +531,7 @@ test_generate_reset_packet_plain(void **ut_state)uint8_t header =0 | (P_CONTROL_HARD_RESET_CLIENT_V2 <<P_OPCODE_SHIFT ); -struct buffer buf =tls_reset_standalone (&tas, &client_id, &server_id, header,0 ,0 ); +struct buffer buf =tls_reset_standalone (&tas.tls_wrap, &tas, &client_id, &server_id, header, false); verdict =tls_pre_decrypt_lite (&tas, &state, &from, &buf);
@@ -539,7 +539,7 @@ test_generate_reset_packet_plain(void **ut_state)/* Assure repeated generation of reset is deterministic/stateless*/ assert_memory_equal (state.peer_session_id.id, client_id.id,SID_SIZE ); -struct buffer buf2 =tls_reset_standalone (&tas, &client_id, &server_id, header,0 ,0 ); +struct buffer buf2 =tls_reset_standalone (&tas.tls_wrap, &tas, &client_id, &server_id, header, false);assert_int_equal (BLEN (&buf),BLEN (&buf2));assert_memory_equal (BPTR (&buf),BPTR (&buf2),BLEN (&buf));free_buf (&buf2);
@@ -566,7 +566,7 @@ test_generate_reset_packet_tls_auth(void **ut_state) now =0x22446688 ;reset_packet_id_send (&tas_client.tls_wrap.opt.packet_id.send); -struct buffer buf =tls_reset_standalone (&tas_client, &client_id, &server_id, header,0 ,0 ); +struct buffer buf =tls_reset_standalone (&tas_client.tls_wrap, &tas_client, &client_id, &server_id, header, false);enum first_packet_verdict verdict =tls_pre_decrypt_lite (&tas_server, &state, &from, &buf);assert_int_equal (verdict,VERDICT_VALID_RESET_V2 );
@@ -575,7 +575,7 @@ test_generate_reset_packet_tls_auth(void **ut_state)/* Assure repeated generation of reset is deterministic/stateless*/ reset_packet_id_send (&tas_client.tls_wrap.opt.packet_id.send); -struct buffer buf2 =tls_reset_standalone (&tas_client, &client_id, &server_id, header,0 ,0 ); +struct buffer buf2 =tls_reset_standalone (&tas_client.tls_wrap, &tas_client, &client_id, &server_id, header,false);assert_int_equal (BLEN (&buf),BLEN (&buf2));assert_memory_equal (BPTR (&buf),BPTR (&buf2),BLEN (&buf));free_buf (&buf2);
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 24/28 Extract read_incoming_tls_plaintext into its own function
In reply to Arne SchwabeThis makes the tls_process_state function a bit easier to read allows extending the read_incoming_tls_plaintext function later without making tls_process_state even longer.
--- src/openvpn/ssl.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-)
src/openvpn/ssl.c +23 -15
@@ -2612,6 +2612,28 @@ control_packet_needs_wkc(const struct key_state *ks) && (ks->send_reliable->packet_id ==1 ); } +static bool +read_incoming_tls_plaintext (struct buffer *buf,struct key_state *ks,interval_t *wakeup) +{ +ASSERT (buf_init (buf,0 )); +int status =key_state_read_plaintext (&ks->ks_ssl, buf,TLS_CHANNEL_BUF_SIZE ); +update_time (); +if (status ==-1 ) + { +msg (D_TLS_ERRORS ,"TLS Error: TLS object -> incoming plaintext read error" ); +return false; + } +if (status ==1 ) + { + *state_change = true; +dmsg (D_TLS_DEBUG ,"TLS -> Incoming Plaintext" ); + +/* More data may be available, wake up again asap to check. */ + *wakeup =0 ; + } +return true; +} + static bool tls_process_state(struct tls_multi *multi,
@@ -2705,24 +2727,10 @@ tls_process_state(struct tls_multi *multi,struct buffer *buf = &ks->plaintext_read_buf;if (!buf->len) { -int status; - -ASSERT (buf_init (buf,0 )); - status =key_state_read_plaintext (&ks->ks_ssl, buf,TLS_CHANNEL_BUF_SIZE ); -update_time (); -if (status ==-1 ) +if (!read_incoming_tls_plaintext (buf, ks, wakeup)) { -msg (D_TLS_ERRORS ,"TLS Error: TLS object -> incoming plaintext read error" ); goto error; } -if (status ==1 ) - { - state_change = true; -dmsg (D_TLS_DEBUG ,"TLS -> Incoming Plaintext" ); - -/* More data may be available, wake up again asap to check. */ - *wakeup =0 ; - } }/* Send Key */
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 25/28 Ensure that control channel packet are respecting tls-mtu
In reply to Arne SchwabeThis ensure that control packets are actually are actually smaller than tls-mtu. Since OpenVPN will consider a control message packet complete when the TLS record is complete, we have to ensure that the SSL library will still write one records, so the receiving side will only be able to get/read the control message content when a TLS records is complete. To achieve this goal, this commit does:
- Splitting one read from TLS library into multiple control channel packets, splitting one TLS record into multiple control packets.
- increase allowed number of outstanding packets to 6 from 4 on the sender side. This is still okay with older implementation as receivers will have room for 8.
- calculate the overhead for control channel message to allow staying below that threshold.
- remove maxlen from key_state_read_ciphertext and related functions as we now always allow control channel messages to be up to TLS_CHANNEL_BUF_SIZE in size and longer limit this by the mtu of control packets as the implemented splitting will take care of larger payloads from the SSL library
--- src/openvpn/reliable.c | 64 +++++++++++---- src/openvpn/reliable.h | 32 +++++++- src/openvpn/ssl.c | 162 ++++++++++++++++++++++++++++++++------ src/openvpn/ssl_backend.h | 8 +- src/openvpn/ssl_mbedtls.c | 14 +--- src/openvpn/ssl_openssl.c | 16 ++-- src/openvpn/ssl_pkt.h | 4 +- 7 files changed, 229 insertions(+), 71 deletions(-)
src/openvpn/reliable.c +48 -16
@@ -41,30 +41,30 @@#include "memdbg.h" -/* -* verify that test - base < extent while allowing for base or test wraparound -*/ -static inline bool -reliable_pid_in_range1 (const packet_id_type test, -const packet_id_type base, -const unsigned int extent) +/* calculates test - base while allowing for base or test wraparound. test is +* assume to be higher than base */ +static inline packet_id_type +subtract_pid (const packet_id_type test,const packet_id_type base) {if (test >= base) { -if (test - base < extent) - { -return true; - } +return test - base; }else { -if ((test+0x80000000u ) - (base+0x80000000u ) < extent) - { -return true; - } +return (test+0x80000000u ) - (base+0x80000000u ); } +} -return false; +/* +* verify that test - base < extent while allowing for base or test wraparound +*/ +static inline bool +reliable_pid_in_range1 (const packet_id_type test, +const packet_id_type base, +const unsigned int extent) +{ +return subtract_pid (test, base) < extent; }/*
@@ -496,6 +496,38 @@ reliable_get_buf(struct reliable *rel)return NULL ; } +/* Counts the number of free buffers in output that can be potientially used +* for sending */ +int +reliable_get_num_output_sequenced_available (struct reliable *rel) +{ +struct gc_arena gc =gc_new (); +packet_id_type min_id =0 ; +bool min_id_defined = false; + +/* find minimum active packet_id */ +for (int i =0 ; i < rel->size; ++i) + { +const struct reliable_entry *e = &rel->array[i]; +if (e->active) + { +if (!min_id_defined ||reliable_pid_min (e->packet_id, min_id)) + { + min_id_defined = true; + min_id = e->packet_id; + } + } + } + +int ret = rel->size; +if (min_id_defined) + { + ret -=subtract_pid (rel->packet_id, min_id); + } +gc_free (&gc); +return ret; +} +/* grab a free buffer, fail if buffer clogged by unacknowledged low packet IDs */ struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel)
src/openvpn/reliable.h +30 -2
@@ -46,7 +46,7 @@* be stored in one \c reliable_ack * structure. */ -#define RELIABLE_CAPACITY 8/**< The maximum number of packets that +#define RELIABLE_CAPACITY 12/**< The maximum number of packets that * the reliability layer for one VPN * tunnel in one direction can store. */
@@ -93,7 +93,7 @@ struct reliableint size;interval_t initial_timeout;packet_id_type packet_id; -int offset; +int offset;/**< Offset of the bufs in the reliable_entry array */ bool hold;/* don't xmit until reliable_schedule_now is called */ struct reliable_entry array[RELIABLE_CAPACITY ]; };
@@ -178,6 +178,20 @@ reliable_ack_empty(struct reliable_ack *ack)return !ack->len; } +/** +* Returns the number of packets that need to be acked. +* +* @param ack The acknowledgment structure to check. +* +* @returns the number of outstanding acks +*/ +static inline bool +reliable_ack_outstanding (struct reliable_ack *ack) +{ +return ack->len; +} + +/** * Write a packet ID acknowledgment record to a buffer. *
@@ -385,6 +399,20 @@ void reliable_mark_deleted(struct reliable *rel, struct buffer *buf);*/ struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel); + +/** +* Counts the number of free buffers in output that can be potientially used +* for sending +* +* @param rel The reliable structure in which to search for a free +* entry. +* +* @return the number of buffer that are available for sending without +* breaking ack sequence +* */ +int +reliable_get_num_output_sequenced_available (struct reliable *rel); +/** * Mark the reliable entry associated with the given buffer as * active outgoing.
src/openvpn/ssl.c +139 -23
204 lines not on this page · raw message
src/openvpn/ssl_backend.h +3 -5
@@ -461,7 +461,6 @@ int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl,* @param ks_ssl - The security parameter state for this %key * session. * @param buf - A buffer in which to store the ciphertext. -* @param maxlen - The maximum number of bytes to extract. * * @return The return value indicates whether the data was successfully * processed:
@@ -470,8 +469,8 @@ int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl,* later to retry. * - \c -1: An error occurred. */ -int key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen); +int key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf); +/** @} name Functions for packets to be sent to a remote OpenVPN peer */
@@ -517,8 +516,7 @@ int key_state_write_ciphertext(struct key_state_ssl *ks_ssl,* later to retry. * - \c -1: An error occurred. */ -int key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen); +int key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf);/** @} name Functions for packets received from a remote OpenVPN peer */
src/openvpn/ssl_mbedtls.c +2 -12
@@ -1285,8 +1285,7 @@ key_state_write_plaintext_const(struct key_state_ssl *ks, const uint8_t *data, i }int -key_state_read_ciphertext (struct key_state_ssl *ks,struct buffer *buf, -int maxlen) +key_state_read_ciphertext (struct key_state_ssl *ks,struct buffer *buf) {int retval =0 ;int len =0 ;
@@ -1304,10 +1303,6 @@ key_state_read_ciphertext(struct key_state_ssl *ks, struct buffer *buf, } len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - } retval =endless_buf_read (&ks->bio_ctx->out,BPTR (buf), len);
@@ -1388,8 +1383,7 @@ key_state_write_ciphertext(struct key_state_ssl *ks, struct buffer *buf) }int -key_state_read_plaintext (struct key_state_ssl *ks,struct buffer *buf, -int maxlen) +key_state_read_plaintext (struct key_state_ssl *ks,struct buffer *buf) {int retval =0 ;int len =0 ;
@@ -1407,10 +1401,6 @@ key_state_read_plaintext(struct key_state_ssl *ks, struct buffer *buf, } len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - } retval =mbedtls_ssl_read (ks->ctx,BPTR (buf), len);
src/openvpn/ssl_openssl.c +5 -11
@@ -1871,7 +1871,7 @@ bio_write_post(const int status, struct buffer *buf)* Read from an OpenSSL BIO in non-blocking mode. */ static int -bio_read (BIO *bio,struct buffer *buf,int maxlen,const char *desc) +bio_read (BIO *bio,struct buffer *buf,const char *desc) {int i;int ret =0 ;
@@ -1882,10 +1882,6 @@ bio_read(BIO *bio, struct buffer *buf, int maxlen, const char *desc)else {int len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - }/* * BIO_read brackets most of the serious RSA
@@ -2012,15 +2008,14 @@ key_state_write_plaintext_const(struct key_state_ssl *ks_ssl, const uint8_t *dat }int -key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen) +key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf) {int ret =0 ;perf_push (PERF_BIO_READ_CIPHERTEXT );ASSERT (NULL != ks_ssl); - ret =bio_read (ks_ssl->ct_out, buf, maxlen,"tls_read_ciphertext" ); + ret =bio_read (ks_ssl->ct_out, buf,"tls_read_ciphertext" );perf_pop ();return ret;
@@ -2042,15 +2037,14 @@ key_state_write_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf) }int -key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen) +key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf) {int ret =0 ;perf_push (PERF_BIO_READ_PLAINTEXT );ASSERT (NULL != ks_ssl); - ret =bio_read (ks_ssl->ssl_bio, buf, maxlen,"tls_read_plaintext" ); + ret =bio_read (ks_ssl->ssl_bio, buf,"tls_read_plaintext" );perf_pop ();return ret;
src/openvpn/ssl_pkt.h +2 -2
@@ -67,8 +67,8 @@/* * Define number of buffers for send and receive in the reliability layer. */ -#define TLS_RELIABLE_N_SEND_BUFFERS 4/* also window size for reliability layer */ -#define TLS_RELIABLE_N_REC_BUFFERS 8 +#define TLS_RELIABLE_N_SEND_BUFFERS 6/* also window size for reliability layer */ +#define TLS_RELIABLE_N_REC_BUFFERS 12/* * Used in --mode server mode to check tls-auth signature on initial
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 26/28 Allow setting control channel packet size with tls-mtu
In reply to Arne SchwabeCurrently control packet size is controlled by tun-mtu in a very non-obvious way since the control overhead is not taken into account and control channel packet will end up with a different size than data channel packet.
Instead we decouple this and introduce tls-mtu which defaults to 1250.
--- Changes.rst | 8 ++++++++ doc/man-sections/link-options.rst | 7 +++++++ src/openvpn/init.c | 8 ++++++-- src/openvpn/mtu.h | 5 +++++ src/openvpn/options.c | 14 ++++++++++++++ src/openvpn/options.h | 1 + src/openvpn/ssl.c | 25 +++++++++++++------------ src/openvpn/ssl.h | 8 +++----- 8 files changed, 57 insertions(+), 19 deletions(-)
Changes.rst +8 -0
@@ -77,6 +77,12 @@ Cookie based handshake for UDP server requires OpenVPN 2.6 clients or later and the tls-crypt-v2 option allows controlling if older clients are accepted. +Improved control channel packet size control (``--tls-mtu``) + The size of control channel is no longer tied to + ``--link-mtu``/``--tun-mtu`` and can be set using ``--tls-mtu``. Setting + the size to small sizes no longer breaks the OpenVPN protocol in certain + situation. + Deprecated features ------------------- ``inetd`` has been removed
@@ -139,6 +145,8 @@ User-visible Changes - Option ``--nobind`` is default when ``--client`` or ``--pull`` is used in the configuration - :code:`link_mtu` parameter is removed from environment or replaced with 0 when scripts are called with parameters. This parameter is unreliable and no longer internally calculated. +- control channel packet maximum size is no longer influenced by ``--link-mtu``/``--tun-mtu`` + and must be set by ``--tls-mtu`` now. Overview of changes in 2.5 ==========================
doc/man-sections/link-options.rst +7 -0
@@ -454,3 +454,10 @@ the local and the remote host. if mode server: socket-flags TCP_NODELAY push "socket-flags TCP_NODELAY" + +--tls-mtu size + This option sets the maximum size for control channel packets. OpenVPN will + try to keep its control channel messages below this size but due to some + constraints in the protocol this is not always possible. If the option is + not set, it default to 1250. Valid sizes are between 512 and 2048. + The maximum packet size includes encapsalution overhead like UDP and IP.
src/openvpn/init.c +6 -2
@@ -2452,6 +2452,10 @@ frame_finalize_options(struct context *c, const struct options *o)* space */ size_t payload_size =max_int (1500 , frame->tun_mtu); +/* we need to be also large enough to hold larger control channel packets +* if configured */ + payload_size =max_int (payload_size, o->ce.tls_mtu); +/* The extra tun needs to be added to the payload size */ if (o->ce.tun_mtu_defined) {
@@ -2993,7 +2997,7 @@ do_init_frame_tls(struct context *c) {if (c->c2.tls_multi) { -tls_multi_init_finalize (c->c2.tls_multi, &c->c2.frame); +tls_multi_init_finalize (c->c2.tls_multi, c->options.ce.tls_mtu);ASSERT (c->c2.tls_multi->opt.frame.buf.payload_size <= c->c2.frame.buf.payload_size); frame_print(&c->c2.tls_multi->opt.frame, D_MTU_INFO,
@@ -3001,7 +3005,7 @@ do_init_frame_tls(struct context *c) }if (c->c2.tls_auth_standalone) { -tls_init_control_channel_frame_parameters (&c->c2.frame, &c->c2.tls_auth_standalone->frame); +tls_init_control_channel_frame_parameters (&c->c2.tls_auth_standalone->frame, c->options.ce.tls_mtu);frame_print (&c->c2.tls_auth_standalone->frame,D_MTU_INFO ,"TLS-Auth MTU parms" ); c->c2.tls_auth_standalone->tls_wrap.work =alloc_buf_gc (BUF_SIZE (&c->c2.frame), &c->c2.gc);
src/openvpn/mtu.h +5 -0
@@ -79,6 +79,11 @@*/ #define MSSFIX_DEFAULT 1492 +/* +* Default maximum size of control channel packets +*/ +#define TLS_MTU_DEFAULT 1250 +/* * Alignment of payload data such as IP packet or * ethernet frame.
src/openvpn/options.c +14 -0
@@ -815,6 +815,7 @@ init_options(struct options *o, const bool init_gc) o->ce.bind_local = true; o->ce.tun_mtu =TUN_MTU_DEFAULT ; o->ce.link_mtu =LINK_MTU_DEFAULT ; + o->ce.tls_mtu =TLS_MTU_DEFAULT ; o->ce.mtu_discover_type =-1 ; o->ce.mssfix =0 ; o->ce.mssfix_default = true;
@@ -1582,6 +1583,7 @@ show_connection_entry(const struct connection_entry *o)SHOW_BOOL (link_mtu_defined);SHOW_INT (tun_mtu_extra);SHOW_BOOL (tun_mtu_extra_defined); +SHOW_INT (tls_mtu);SHOW_INT (mtu_discover_type);
@@ -6281,6 +6283,18 @@ add_option(struct options *options, options->ce.tun_mtu_extra =positive_atoi (p[1 ]); options->ce.tun_mtu_extra_defined = true; } +else if (streq (p[0 ],"tls-mtu" ) && p[1 ] && !p[2 ]) + { +VERIFY_PERMISSION (OPT_P_MTU |OPT_P_CONNECTION ); +int tls_mtu =atoi (p[1 ]); +if (tls_mtu <512 || tls_mtu >TLS_CHANNEL_BUF_SIZE ) + { +msg (msglevel,"Bad tls-mtu value, must be between %d and %d" , +512 ,TLS_CHANNEL_BUF_SIZE ); + goto err; + } + options->ce.tls_mtu =positive_atoi (p[1 ]); + }#ifdef ENABLE_FRAGMENT else if (streq(p[0], "mtu-dynamic")) {
src/openvpn/options.h +1 -0
@@ -123,6 +123,7 @@ struct connection_entrybool tun_mtu_extra_defined;int link_mtu;/* MTU of device over which tunnel packets pass via TCP/UDP */ bool link_mtu_defined;/* true if user overriding parm with command line option */ +int tls_mtu;/* Maximum MTU for the control channel messages */ /* Advanced MTU negotiation and datagram fragmentation options */ int mtu_discover_type;/* used if OS supports setting Path MTU discovery options on socket */
src/openvpn/ssl.c +13 -12
@@ -296,8 +296,7 @@ tls_limit_reneg_bytes(const char *ciphername, int *reneg_bytes) }void -tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, -struct frame *frame) +tls_init_control_channel_frame_parameters (struct frame *frame,int tls_mtu) {/* * frame->extra_frame is already initialized with tls_auth buffer requirements,
@@ -322,18 +321,20 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame/* Previous OpenVPN version calculated the maximum size and buffer of a * control frame depending on the overhead of the data channel frame -* overhead and limited its maximum size to 1250. We always allocate the -* TLS_CHANNEL_BUF_SIZE buffer size since a lot of code blindly assumes -* a large buffer (e.g. PUSH_BUNDLE_SIZE) and also our peer might have -* a higher size configure and we still want to be able to receive the -* packets. frame->mtu_mtu is set as suggestion for the maximum packet -* size */ - frame->buf.payload_size =TLS_CHANNEL_BUF_SIZE + overhead; +* overhead and limited its maximum size to 1250. Since control frame +* frames also need to fit into data channel buffer we have the same +* default of 1500 + 100 as data channel buffers have. Increasing +* tls-mtu beyond this limit also the data channel buffers */ + frame->buf.payload_size =max_int (1500 , tls_mtu) +100 ; frame->buf.headroom = overhead; frame->buf.tailroom = overhead; - frame->tun_mtu =min_int (data_channel_frame->tun_mtu,1250 ); + frame->tun_mtu = tls_mtu; + +/* Ensure the tun-mtu stays in a valid range */ + frame->tun_mtu =min_int (frame->tun_mtu,TLS_CHANNEL_BUF_SIZE ); + frame->tun_mtu =max_int (frame->tun_mtu,512 ); }/**
@@ -1300,9 +1301,9 @@ tls_multi_init(struct tls_options *tls_options) }void -tls_multi_init_finalize (struct tls_multi *multi,const struct frame *frame) +tls_multi_init_finalize (struct tls_multi *multi,int tls_mtu) { -tls_init_control_channel_frame_parameters (frame, &multi->opt.frame); +tls_init_control_channel_frame_parameters (&multi->opt.frame, tls_mtu);/* initialize the active and untrusted sessions */ tls_session_init (multi, &multi->session[TM_ACTIVE ]);
src/openvpn/ssl.h +3 -5
@@ -156,10 +156,9 @@ struct tls_multi *tls_multi_init(struct tls_options *tls_options);* * @param multi - The \c tls_multi structure of which to finalize * initialization. -* @param frame - The data channel's \c frame structure. +* @param tls_mtu - maximum allowed size for control channel packets */ -void tls_multi_init_finalize (struct tls_multi *multi, -const struct frame *frame); +void tls_multi_init_finalize (struct tls_multi *multi,int tls_mtu);/* * Initialize a standalone tls-auth verification object.
@@ -171,8 +170,7 @@ struct tls_auth_standalone *tls_auth_standalone_init(struct tls_options *tls_opt* Setups up the control channel frame size parameters from the data channel * parameters */ -void tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, -struct frame *frame); +void tls_init_control_channel_frame_parameters (struct frame *frame,int tls_mtu);/* * Set local and remote option compatibility strings.
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 27/28 Add unit test for reliable_get_num_output_sequenced_available
In reply to Arne Schwabe--- tests/unit_tests/openvpn/Makefile.am | 5 +- tests/unit_tests/openvpn/mock_get_random.c | 10 ++++ tests/unit_tests/openvpn/test_packet_id.c | 55 ++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-)
tests/unit_tests/openvpn/Makefile.am +4 -1
@@ -61,7 +61,10 @@ packet_id_testdriver_SOURCES = test_packet_id.c mock_msg.c mock_msg.h \ $(openvpn_srcdir)/buffer.c \ $(openvpn_srcdir)/otime.c \ $(openvpn_srcdir)/packet_id.c \ - $(openvpn_srcdir)/platform.c + $(openvpn_srcdir)/platform.c \ + $(openvpn_srcdir)/reliable.c \ + $(openvpn_srcdir)/session_id.c + pkt_testdriver_CFLAGS = @TEST_CFLAGS@ \ -I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir)
tests/unit_tests/openvpn/mock_get_random.c +10 -0
@@ -26,6 +26,7 @@#include <stdio.h> #include <stdlib.h> #include <setjmp.h> +#include <stdint.h> #include <cmocka.h> unsigned long
@@ -34,3 +35,12 @@ get_random(void)/* rand() is not very random, but it's C99 and this is just for testing */ return rand (); } + +void +prng_bytes (uint8_t *output,int len) +{ +for (int i =0 ; i < len; i++) + { + output[i] =rand (); + } +}
tests/unit_tests/openvpn/test_packet_id.c +55 -0
73 lines not on this page · raw message
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 28/28 Always include ACKs for the last seen control packets
In reply to Arne SchwabeThis adds an LRU cache for the last seen packets from the peer to send acks to all recently packets. This also packets to be acknowledged even if a single P_ACK_V1 gets lost, avoiding retransmissions. The downside is that we add up to 28 byte to an P_ACK_V1 (7* packet_id) and up to 24 bytes to other control channel packets (4* packet_id + peer session id). However these small increases in packet size are a small price to pay for increased reliability.
Currently OpenVPN will only send the absolute minimum of ACK messages. A single lost ACK message will trigger a resend from the peer and another ACK message.
--- src/openvpn/reliable.c | 69 +++++++++++++++++++++-- src/openvpn/reliable.h | 17 ++++++ src/openvpn/ssl.c | 7 ++- src/openvpn/ssl_common.h | 1 + src/openvpn/ssl_pkt.c | 3 +- tests/unit_tests/openvpn/test_packet_id.c | 37 +++++++++++- 6 files changed, 125 insertions(+), 9 deletions(-)
src/openvpn/reliable.c +64 -5
@@ -213,10 +213,56 @@ reliable_ack_parse(struct buffer *buf, struct reliable_ack *ack,return true; } +/** +* Copies the first n acks from \c ack to \c ack_lru +*/ +void +copy_acks_to_lru (struct reliable_ack *ack,struct reliable_ack *ack_lru,int n) +{ +ASSERT (ack->len >= n); +/* This loops is backward to ensure the same order as in ack */ +for (int i = n-1 ; i >=0 ; i--) + { +packet_id_type id = ack->packet_id[i]; + +/* Handle special case of ack_lru empty */ +if (ack_lru->len ==0 ) + { + ack_lru->len =1 ; + ack_lru->packet_id[0 ] = id; + } + +bool idfound = false; + +/* Move all existing entry one to the right */ +packet_id_type move = id; + +for (int j =0 ; j < ack_lru->len; j++) + { +packet_id_type tmp = ack_lru->packet_id[j]; + ack_lru->packet_id[j] = move; + move = tmp; + +if (move == id) + { + idfound = true; +break ; + } + } + +if (!idfound && ack_lru->len <RELIABLE_ACK_SIZE ) + { + ack_lru->packet_id[ack_lru->len] = move; + ack_lru->len++; + } + } +} +/* write a packet ID acknowledgement record to buf, */ /* removing all acknowledged entries from ack */ bool reliable_ack_write (struct reliable_ack *ack, +struct reliable_ack *ack_lru,struct buffer *buf,const struct session_id *sid,int max,bool prepend) {
@@ -229,23 +275,36 @@ reliable_ack_write(struct reliable_ack *ack, { n = max; } - sub =buf_sub (buf,ACK_SIZE (n), prepend); + +copy_acks_to_lru (ack, ack_lru, n); + +/* Number of acks we can resend that still fit into the packet */ +uint8_t total_acks =min_int (max, ack_lru->len); + + sub =buf_sub (buf,ACK_SIZE (total_acks), prepend);if (!BDEF (&sub)) { goto error; } -ASSERT (buf_write (&sub, &n,sizeof (n))); -for (i =0 ; i < n; ++i) +ASSERT (buf_write_u8 (&sub, total_acks)); + +/* Write the actual acks to the packets. Since we copied the acks that +* are going out now already to the front of ack_lru we can fetch all +* acks from ack_lru */ +for (i =0 ; i < total_acks; ++i) { -packet_id_type pid = ack->packet_id[i]; +packet_id_type pid = ack_lru->packet_id[i];packet_id_type net_pid =htonpid (pid);ASSERT (buf_write (&sub, &net_pid,sizeof (net_pid)));dmsg (D_REL_DEBUG ,"ACK write ID " packet_id_format" (ack->len=%d, n=%d)" , (packet_id_print_type )pid, ack->len, n); } -if (n) +if (total_acks) {ASSERT (session_id_defined (sid));ASSERT (session_id_write (sid, &sub)); + } +if (n) + {for (i =0 , j = n; j < ack->len; ) { ack->packet_id[i++] = ack->packet_id[j++];
src/openvpn/reliable.h +17 -0
@@ -197,6 +197,9 @@ reliable_ack_outstanding(struct reliable_ack *ack)* * @param ack The acknowledgment structure containing packet IDs to be * acknowledged. +* @param ack_lru List of packets we have acknowledged before. Packets from +* \c ack will be moved here and if there is space in our +* ack structure we will fill it with packets from this * @param buf The buffer into which the acknowledgment record will be * written. * @param sid The session ID of the VPN tunnel associated with the
@@ -211,6 +214,7 @@ reliable_ack_outstanding(struct reliable_ack *ack)* @li False, if an error occurs during processing. */ bool reliable_ack_write (struct reliable_ack *ack, +struct reliable_ack *ack_lru,struct buffer *buf,const struct session_id *sid,int max,bool prepend);
@@ -370,6 +374,19 @@ bool reliable_ack_acknowledge_packet_id(struct reliable_ack *ack, packet_id_type*/ struct reliable_entry *reliable_get_entry_sequenced (struct reliable *rel); + + +/** +* Copies the first n acks from \c ack to \c ack_lru +* +* @param ack The reliable structure to copy the acks from +* @param ack_lru the reliable structure to insert the acks into +* @param n The number of ACKS to copy +*/ +void +copy_acks_to_lru (struct reliable_ack *ack,struct reliable_ack *ack_lru,int n); + +/** * Remove an entry from a reliable structure. *
src/openvpn/ssl.c +5 -2
@@ -349,13 +349,14 @@ calc_control_channel_frame_overhead(const struct tls_session *session)int overhead =0 ;/* TCP length field and opcode */ - overhead+=3 ; + overhead +=3 ;/* our own session id */ overhead +=SID_SIZE ;/* ACK array and remote SESSION ID (part of the ACK array) */ - overhead +=ACK_SIZE (min_int (reliable_ack_outstanding (ks->rec_ack),CONTROL_SEND_ACK_MAX )); +int ackstosend =reliable_ack_outstanding (ks->rec_ack) + ks->lru_acks->len; + overhead +=ACK_SIZE (min_int (ackstosend,CONTROL_SEND_ACK_MAX ));/* Message packet id */ overhead +=sizeof (packet_id_type);
@@ -977,6 +978,7 @@ key_state_init(struct tls_session *session, struct key_state *ks) ALLOC_OBJ_CLEAR(ks->send_reliable, struct reliable); ALLOC_OBJ_CLEAR(ks->rec_reliable, struct reliable); ALLOC_OBJ_CLEAR(ks->rec_ack, struct reliable_ack); + ALLOC_OBJ_CLEAR(ks->lru_acks, struct reliable_ack);/* allocate buffers */ ks->plaintext_read_buf =alloc_buf (TLS_CHANNEL_BUF_SIZE );
@@ -1047,6 +1049,7 @@ key_state_free(struct key_state *ks, bool clear)reliable_free (ks->rec_reliable);free (ks->rec_ack); +free (ks->lru_acks);free (ks->key_src);packet_id_free (&ks->crypto_options.packet_id);
src/openvpn/ssl_common.h +1 -0
@@ -220,6 +220,7 @@ struct key_statestruct reliable *send_reliable;/* holds a copy of outgoing packets until ACK received */ struct reliable *rec_reliable;/* order incoming ciphertext packets before we pass to TLS */ struct reliable_ack *rec_ack;/* buffers all packet IDs we want to ACK back to sender */ +struct reliable_ack *lru_acks;/* keeps the most recently acked packages*/ /** Holds outgoing message for the control channel until ks->state reaches * S_ACTIVE */
src/openvpn/ssl_pkt.c +2 -1
@@ -179,7 +179,8 @@ write_control_auth(struct tls_session *session,ASSERT (link_socket_actual_defined (&ks->remote_addr));ASSERT (reliable_ack_write - (ks->rec_ack, buf, &ks->session_id_remote, max_ack, prepend_ack)); + (ks->rec_ack, ks->lru_acks, buf, &ks->session_id_remote, + max_ack, prepend_ack));msg (D_TLS_DEBUG ,"%s(): %s" , __func__,packet_opcode_name (opcode));
tests/unit_tests/openvpn/test_packet_id.c +36 -1
@@ -210,6 +210,39 @@ test_get_num_output_sequenced_available(void **state)reliable_free (rel); } + +static void +test_copy_acks_to_lru (void **state) +{ +struct reliable_ack ack = { .len =4 , .packet_id = {2 ,1 ,3 ,2 } }; + +struct reliable_ack lru_ack = {0 }; + +/* Test copying to empty ack structure */ +copy_acks_to_lru (&ack, &lru_ack,4 ); +assert_int_equal (lru_ack.len,3 ); +assert_int_equal (lru_ack.packet_id[0 ],2 ); +assert_int_equal (lru_ack.packet_id[1 ],1 ); +assert_int_equal (lru_ack.packet_id[2 ],3 ); + +/* Copying again should not change the result */ +copy_acks_to_lru (&ack, &lru_ack,4 ); +assert_int_equal (lru_ack.len,3 ); +assert_int_equal (lru_ack.packet_id[0 ],2 ); +assert_int_equal (lru_ack.packet_id[1 ],1 ); +assert_int_equal (lru_ack.packet_id[2 ],3 ); + +struct reliable_ack ack2 = { .len =7 , .packet_id = {5 ,6 ,7 ,8 ,9 ,10 ,11 } }; + +/* Adding multiple acks tests if the a full array is handled correctly */ +copy_acks_to_lru (&ack2, &lru_ack,7 ); + +struct reliable_ack expected_ack = { .len =8 , .packet_id = {5 ,6 ,7 ,8 ,9 ,10 ,11 ,2 }}; +assert_int_equal (lru_ack.len, expected_ack.len); + +assert_memory_equal (lru_ack.packet_id, expected_ack.packet_id,sizeof (expected_ack.packet_id)); +} +int main (void ) {
@@ -232,7 +265,9 @@ main(void)cmocka_unit_test_setup_teardown (test_packet_id_write_long_wrap, test_packet_id_write_setup, test_packet_id_write_teardown), - cmocka_unit_test(test_get_num_output_sequenced_available) +cmocka_unit_test (test_get_num_output_sequenced_available), + cmocka_unit_test(test_copy_acks_to_lru) + };return cmocka_run_group_tests_name ("packet_id tests" , tests,NULL ,NULL );
Signature · 1 line
2.32.0 (Apple Git-132)
Re: [Openvpn-devel] [PATCH 03/28] Move pre decrypt lite check to its own function
In reply to Arne SchwabeQuoted 13 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 15:40 geschrieben:
src/openvpn/mudp.c +2
-0@@ -39,6 +39,20 @@#include <sys/inotify.h> #endif +static bool +do_pre_decrypt_check (struct multi_context *m)
src/openvpn/mudp.c line 53
Quoted 10 lines
src/openvpn/mudp.c +10
-0+{ +if (!m->top.c2.tls_auth_standalone) + { +return false; + } +if (!tls_pre_decrypt_lite (m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf)) + { +return false; + } +return true;
Shouldn't this be
if (!m->top.c2.tls_auth_standalone)
{
return true;
}
if (tls_pre_decrypt_lite(m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf)) {
return true;
}
return false;
Your patch seems to mangle the logic in several different ways.
Quoted 6 lines
src/openvpn/mudp.c +2
-0+} +/* * Get a client instance based on real address. If * the instance doesn't exist, create it while
src/openvpn/mudp.c multi_get_create_instance_udp(struct multi_context *m, bool *floated) · line 115
Quoted 9 lines
src/openvpn/mudp.c +1
-2@@ -95,8 +109,7 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated) }if (!mi) { -if (!m->top.c2.tls_auth_standalone - ||tls_pre_decrypt_lite (m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf)) +if (do_pre_decrypt_check (m)) {if (frequency_limit_event_allowed (m->new_connection_limiter)) {
--
Frank Lichtenheld
[Openvpn-devel] [PATCH applied] Re: Remove dead PID_TEST code
In reply to Arne SchwabeTrivial enough, and indeed, this sort of test code is better suited in the cmocka unit tests nowadays than compiling a special openvpn binary... have not tested it, just verified that only stuff between #ifdef PID_TEST ... #endif gets ripped out.
Your patch has been applied to the master branch.
commit e21425a7ae7728e16634fd0e8cfba90b48a5309e
Author: Arne Schwabe Date: Fri Apr 22 15:40:31 2022 +0200
Remove dead PID_TEST code
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/search?l=mid&[email protected]...
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Remove inc_pid argument from reliable_mark_deleted that is always true
In reply to Arne SchwabeTrivial enough. Single caller, constant argument ("true"), away it goes.
Only lightly tested.
Your patch has been applied to the master branch.
commit 911224dbac0f11bb22e007db0ae424e864489ef8
Author: Arne Schwabe Date: Fri Apr 22 15:40:38 2022 +0200
Remove inc_pid argument from reliable_mark_deleted that is always true
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/search?l=mid&[email protected]...
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Remove EXPONENTIAL_BACKOFF define
In reply to Arne Schwabetrivial enough :-) - lightly tested.
Your patch has been applied to the master branch.
commit b372463931ed706005214c61ffeb75ff313bee22
Author: Arne Schwabe Date: Fri Apr 22 16:29:35 2022 +0200
Remove EXPONENTIAL_BACKOFF define
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/search?l=mid&[email protected]...
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Re: [Openvpn-devel] [PATCH 04/28] Add documentation for swap_hmac function
In reply to Arne SchwabeHi,
Quoted 11 lines · Arne Schwabe
+/**
+ * Move a packet authentication HMAC + related fields to or from the front
+ * of the buffer so it can be processed by encrypt/decrypt.
+ *
+ * Turning the on wire format that starts with the opcode to a format
+ * that starts with the hmac
+ * e.g. "onwire" [opcode + packet id] [hmac] [remainder of packed]
+ *
+ *
+ * "internal" [hmac] [opcode + packet id] [remainer of packet]
+ *
I was about to merge this as "it is easy, and does not change code", but if we add documentation, it should be correct :-) - and this schematic does not match my understanding of the actual code - which seems to swap
[opcode + session id] [hmac + packet id]
could you double check that? The size of the "things it swaps" is
/* hmac + packet_id (8 bytes) */
const int hmac_size = hmac_ctx_size(ctx->hmac) + packet_id_size(true);
/* opcode + session_id */
const int osid_size = 1 + SID_SIZE;... so that would make it "[hmac + packet id]" not "[hmac]" and "[opcode + packet id]"...?!
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Re: [Openvpn-devel] [PATCH 04/28] Add documentation for swap_hmac function
Hi,
Quoted 8 lines · Gert Doering
I was about to merge this as "it is easy, and does not change code", but if we add documentation, it should be correct :-) - and this schematic does not match my understanding of the actual code - which seems to swap
[opcode + session id] [hmac + packet id]
could you double check that? The size of the "things it swaps" is
Ah, found it. This comment gets changed in 05/28, to the values I also figured out.
So, ACK on this one, will merge together with 05/ ASAPish (maybe I'll move the "fix comment hunk" from 05 to 04).
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...[Openvpn-devel] [PATCH applied] Re: Remove tls_init_control_channel_frame_parameters wrapper function
In reply to Arne SchwabeEasy enough, though this wrapper actually does more than usual ("turning around the argument and only passing on tas->frame").
One typo in ssl.h comment fixed.
Besides the text in the commit message, this patch also removes a pre-function comment that was leftover from the dark ages, and did not have any relation to the following function...
I've left the spurious blank line removal in, to avoid later conflicts.
Your patch has been applied to the master branch.
commit 5e9fea71182137018a9f50b1ee8449b8d83ddc25
Author: Arne Schwabe Date: Fri Apr 22 15:40:30 2022 +0200
Remove tls_init_control_channel_frame_parameters wrapper function
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24172.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Add documentation for swap_hmac function
In reply to Arne SchwabeI have taken the hunks from 05/28 that relate to swap_hmac (change the comment, add byte sizes to the "osid_size" comment) into this one, so this one is now factually correct, and 05(bis)/28 does no longer contain "swap_hmac" related changes.
As this brings no code change, I've just verified that the comment matches my understanding of the function (it now does), not tested anything.
Your patch has been applied to the master branch.
commit e061ec5800af746cb86ccbb9b571e1cb18f8ad99
Author: Arne Schwabe Date: Fri Apr 22 15:40:33 2022 +0200
Add documentation for swap_hmac function
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24177.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Re: [Openvpn-devel] [PATCH 19/28] Make buf_write_u8/16/32 take the type they pretend to take
In reply to Arne SchwabeTrivial enough and can be applied independently from the rest of the series.
Quoted 6 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
This functions should accept the type of integer they say to write. Calling the u32 function with an integer that is actually 32 bit unsigned gives compiler warnings.
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 08/28] Split out reliable_ack_parse from reliable_ack_read
In reply to Arne SchwabeChanges look good to me, applies on current master and compiles. One error in function documentation, but probably fixable on apply. See below.
Quoted 1 line
Arne Schwabe <[email protected]...> hat am 22.04.2022 15:40 geschrieben:
[...]
Quoted 5 lines
src/openvpn/reliable.h +0
-0
src/openvpn/reliable.h struct reliable · line 135
Quoted 10 lines
src/openvpn/reliable.h +7
-0@@ -126,6 +126,28 @@ struct reliablebool reliable_ack_read (struct reliable_ack *ack,struct buffer *buf,const struct session_id *sid); + +/** +* Parse an acknowledgment record from a received packet. +* +* This function parses the packet ID acknowledgment record from the packet +* contained in \a buf. If the record contains acknowledgments, these are +* stored in \a ack. This function also compares extracts packet's session ID
Remove "compares".
src/openvpn/reliable.h struct reliable · line 136
Quoted 1 line
src/openvpn/reliable.h +1
-0+* and returns it in \a session_id_remote
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 03/28] Move pre decrypt lite check to its own function
In reply to Frank LichtenheldAm 22.04.22 um 17:52 schrieb Frank Lichtenheld:
Quoted 11 lines
Quoted 11 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 15:40 geschrieben:
src/openvpn/mudp.c +0
-0@@ -39,6 +39,20 @@#include <sys/inotify.h> #endif
Quoted 15 lines
src/openvpn/mudp.c line 53
Quoted 12 lines
src/openvpn/mudp.c +12
-0+static bool +do_pre_decrypt_check (struct multi_context *m) +{ +if (!m->top.c2.tls_auth_standalone) + { +return false; + } +if (!tls_pre_decrypt_lite (m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf)) + { +return false; + } +return true;Shouldn't this be
if (!m->top.c2.tls_auth_standalone)
Yes. But then again the whole line is dead code anyway because m->top.c2.tls_auth_standalone is always initialised.
Arne
[Openvpn-devel] [PATCH applied] Re: Make buf_write_u8/16/32 take the type they pretend to take
In reply to Arne SchwabeIndeed :-) - code looks good, test compiled on Linux and FreeBSD ("does it bring any surprise warnings?")
Your patch has been applied to the master branch.
commit dc6e00e2e75eae58bf94bcf384ae2ba68c5c6bd3
Author: Arne Schwabe Date: Fri Apr 22 16:29:44 2022 +0200
Make buf_write_u8/16/32 take the type they pretend to take
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24165.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
PATCH v2 1/1 Move pre decrypt lite check to its own function
In reply to Arne SchwabeThis prepares for extending this function with the HMAC based session ID check.
Replace the check for m->top.c2.tls_auth_standalone with an ASSERT as this code path is only used in multi udp server and OpenVPN initialises the tls_auth_standalone always for the TOP context (CF_INIT_TLS_AUTH_STANDALONE), even for the tcp m2mp server that does not use it).
Patch v2: replace if with ASSERT
--- src/openvpn/mudp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
src/openvpn/mudp.c +12 -2
@@ -39,6 +39,17 @@#include <sys/inotify.h> #endif +static bool +do_pre_decrypt_check (struct multi_context *m) +{ +ASSERT (m->top.c2.tls_auth_standalone); +if (!tls_pre_decrypt_lite (m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf)) + { +return false; + } +return true; +} +/* * Get a client instance based on real address. If * the instance doesn't exist, create it while
@@ -95,8 +106,7 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated) }if (!mi) { -if (!m->top.c2.tls_auth_standalone - ||tls_pre_decrypt_lite (m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf)) +if (do_pre_decrypt_check (m)) {if (frequency_limit_event_allowed (m->new_connection_limiter)) {
Signature · 1 line
2.32.0 (Apple Git-132)
Re: [Openvpn-devel] [PATCH v2] Move pre decrypt lite check to its own function
Quoted 1 line
Arne Schwabe <[email protected]...> hat am 25.04.2022 14:27 geschrieben:
[...]
Quoted 7 lines
src/openvpn/mudp.c +0
-0@@ -39,6 +39,17 @@#include <sys/inotify.h> #endif
src/openvpn/mudp.c line 50
Quoted 10 lines
src/openvpn/mudp.c +9
-0+static bool +do_pre_decrypt_check (struct multi_context *m) +{ +ASSERT (m->top.c2.tls_auth_standalone); +if (!tls_pre_decrypt_lite (m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf)) + { +return false; + } +return true;
You could replace four lines with one:
return tls_pre_decrypt_lite(m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf));
But that might not make sense due to changes in later patches? Haven't checked.
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH v2] Move pre decrypt lite check to its own function
Am 25.04.22 um 15:08 schrieb Frank Lichtenheld:
Quoted 22 lines
Quoted 1 line
Arne Schwabe <[email protected]...> hat am 25.04.2022 14:27 geschrieben:
[...]
Quoted 7 lines
src/openvpn/mudp.c +0
-0@@ -39,6 +39,17 @@#include <sys/inotify.h> #endif src/openvpn/mudp.c line 50
Quoted 10 lines
src/openvpn/mudp.c +9
-0+static bool +do_pre_decrypt_check (struct multi_context *m) +{ +ASSERT (m->top.c2.tls_auth_standalone); +if (!tls_pre_decrypt_lite (m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf)) + { +return false; + } +return true;You could replace four lines with one:
Quoted 4 lines
return
tls_pre_decrypt_lite(m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf));But that might not make sense due to changes in later patches? Haven't checked.
The function grows to a 70 line function over the course of the patches. :)
Arne
Frank Lichtenheld
Acked-byRe: [Openvpn-devel] [PATCH v2] Move pre decrypt lite check to its own function
Quoted 2 lines
Arne Schwabe <[email protected]...> hat am 25.04.2022 15:12 geschrieben:
Am 25.04.22 um 15:08 schrieb Frank Lichtenheld:
[...]
Quoted 8 lines
Quoted 6 lines
You could replace four lines with one:
return
tls_pre_decrypt_lite(m->top.c2.tls_auth_standalone, &m->top.c2.from, &m->top.c2.buf));But that might not make sense due to changes in later patches? Haven't checked.
The function grows to a 70 line function over the course of the patches. :)
Fair enough. Acked.
Regards,
--
Frank Lichtenheld
[Openvpn-devel] [PATCH applied] Re: Move pre decrypt lite check to its own function
In reply to Arne SchwabeYour patch has been applied to the master branch.
commit 73713debf56c06ed54a378f9b3d1d742c5f1ed45
Author: Arne Schwabe Date: Mon Apr 25 14:27:09 2022 +0200
Move pre decrypt lite check to its own function
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24193.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Extend tls_pre_decrypt_lite to return type of packet and keep state
In reply to Arne SchwabeThis is 05 without the "comment and whitespace" bits that fix 04.
Stared at code, ran client/server tests (including UDP server).
It should be noted that this adds recognition of P_CONTROL_V1 packets to tls_pre_decrypt_lite() - but they are handled by the caller the same way as VERDICT_INVALID, so the effective difference is none ("only accept packets that are P_CONTROL_HARD_RESET_CLIENT_V2/V3").
The "free_tls_pre_decrypt_state()" is actually nothing new, it's all existing free() calls, just moved to that new function (so, not much consideration on "what sort of data is manipulated here?").
The comment in ssl.h for tls_pre_decrypt_lite() is missing an explanation of what "struct tls_pre_decrypt_state *state" is for - but I guess this will come in a followup patch?
Your patch has been applied to the master branch.
commit b67d670b2dedd9a4d39d927956b385903107f82b
Author: Arne Schwabe Date: Fri Apr 22 15:40:34 2022 +0200
Extend tls_pre_decrypt_lite to return type of packet and keep state
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24148.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Move ssl function related to control channel wrap/unwrap to ssl_pkt.c/h
In reply to Arne Schwabe"git show --color-moved=zebra" confirms that this is just code being moved around, *except* for the swap_hmac() comment (*that* comment), which actually gets changed in the process. But it's still correct :-)
"make check" and "make distcheck" agree that all configure/autoconf related bits are fine with the new files. "make check" tested on a OpenSSL and mbedTLS build.
Whitespace in Makefile.am fixed, missing end-of-file newline in ssl_pkt.c fixed.
Your patch has been applied to the master branch.
commit 788ce35cf09aff09b79f428cdd6cfc0ff8627934
Author: Arne Schwabe Date: Fri Apr 22 15:40:35 2022 +0200
Move ssl function related to control channel wrap/unwrap to ssl_pkt.c/h
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24149.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Add unit tests for test_tls_decrypt_lite
In reply to Arne SchwabeUnit tests are always good :-) - and of course 06/28 is only there to make this happen. I have only done a "semi-deep" look into the code, but it seems to cover the usual cases (good packet, short packet, corrupt packet for nothing/tls-auth/tls-crypt) which is way more than we have now.
Passes "make distcheck" + "make check with mbedTLS and openssl"
[==========] Running 3 test(s).
[ RUN ] test_tls_decrypt_lite_none
[ OK ] test_tls_decrypt_lite_none
[ RUN ] test_tls_decrypt_lite_auth
[ OK ] test_tls_decrypt_lite_auth
[ RUN ] test_tls_decrypt_lite_crypt
[ OK ] test_tls_decrypt_lite_crypt
[==========] 3 test(s) run.
[ PASSED ] 3 test(s).
PASS: pkt_testdriver
Your patch has been applied to the master branch.
commit 34694688f43ba1cb5827d921df0b374c916119ce
Author: Arne Schwabe Date: Fri Apr 22 15:40:36 2022 +0200
Add unit tests for test_tls_decrypt_lite
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24168.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Split out reliable_ack_parse from reliable_ack_read
In reply to Arne SchwabeYour patch has been applied to the master branch.
Tested on client and server ("for good measure") even if stare-at-code suggests it's really the same code, split in half. The change in ssl.c is due to "ack->len is now always zeroed".
Uncrustify (0.74.0_f) has opinions on "if ( a && (b || c))" constructs, so a bit of whitespace was changed.
Comment in reliable.h adjusted ("compares extracts").
commit ac97e16123aba7d4792ca978a4e2e6875973b8ed
Author: Arne Schwabe Date: Fri Apr 22 15:40:37 2022 +0200
Split out reliable_ack_parse from reliable_ack_read
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24145.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Re: [Openvpn-devel] [PATCH 11/28] Refactor tls-auth/tls-crypt wrapping into into own function
In reply to Arne SchwabeVerified visually that this only moves code around and doesn't change behavior. Only compile/UT tested.
Quoted 8 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
This allows the the wrapping to be easier reused by a function that does not have access to a full TLS session.
--- src/openvpn/ssl_pkt.c | 82 ++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 32 deletions(-)
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 12/28] Extract session_move_pre_start as own function, use local buffer variable
In reply to Arne SchwabeVerified visually that this only moves code around and doesn't change behavior. Only compile/UT tested.
Quoted 9 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
This changes the C90 struct buffer declaration to a C99 style one. Also move the state transition from
S_INITIALtoS_PE_STARTinto its own function.--- src/openvpn/ssl.c | 84 ++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 34 deletions(-)
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 13/28] Change FULL_SYNC macro to no_pending_reliable_packets function
In reply to Arne SchwabeQuoted 6 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
This changes this macro to a better named inline function. This introduces a slight whitespace problem but the next refactoring will move the incorrectly intended block to its own function anyway.
Could it be that you forgot to update the commit message when you squashed this commit? Looking at the diff this already moves the block to its own function?
Apart from that and a small whitespace oddity noted below the changes look good (in the sense that they shouldn't change behavior and just move code around).
Quoted 8 lines
--- src/openvpn/ssl.c | 100 ++++++++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 43 deletions(-)src/openvpn/ssl.c +0
-0
[...]
src/openvpn/ssl.c session_move_pre_start(const struct tls_session *session, · line 2436
Quoted 8 lines
src/openvpn/ssl.c +4
-0@@ -2428,6 +2430,54 @@ session_move_pre_start(const struct tls_session *session,return true; } + +/** +* Moves the key to state to S_ACTIVE and also advances the multi_state state +* machine if this is the initial connection.
Spurious whitespace at the end of the line
Regards,
--
Frank Lichtenheld
[Openvpn-devel] [PATCH applied] Re: Refactor tls-auth/tls-crypt wrapping into into own function
In reply to Arne SchwabeI've done a bit of "extra eyes stare at the code" and things look good (due to the newly named options, git is not that helpful in confirming "it's all the same code"). Will test together with 12.
Your patch has been applied to the master branch.
commit 9bbebf100a07f4dca3f088dce19cd87608e82b4c
Author: Arne Schwabe Date: Fri Apr 22 16:29:36 2022 +0200
Refactor tls-auth/tls-crypt wrapping into into own function
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24150.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Re: [Openvpn-devel] [PATCH 14/28] Move tls_process_state into its own function
In reply to Arne SchwabeVerified visually that this only moves code around and doesn't change behavior. Only compile/UT tested.
This one actually fixes the spurious whitespace I complained about in 13/28 but that hunk should just be moved to there.
You removed one comment from the function. See below. But it might not have been helpful to begin with.
Quoted 19 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
This function does most of the state transitions in the TLS state machine. Moving it into its own function removes an intention area and makes
tls_processfunction easier to understand as the loop is more obvious.This is largely just a code move with small expection. bool active is no longer directly set but inferred from
to_link->len--- src/openvpn/ssl.c | 444 ++++++++++++++++++++++++---------------------- 1 file changed, 228 insertions(+), 216 deletions(-)src/openvpn/ssl.c +0
-0
src/openvpn/ssl.c session_move_pre_start(const struct tls_session *session, · line 2436
Quoted 5 lines
src/openvpn/ssl.c +1
-1@@ -2433,7 +2433,7 @@ session_move_pre_start(const struct tls_session *session,/** * Moves the key to state to S_ACTIVE and also advances the multi_state state -* machine if this is the initial connection. +* machine if this is the initial connection.
Mentioned whitespace fix.
Quoted 6 lines
src/openvpn/ssl.c +2
-0@@ -2541,209 +2750,15 @@ tls_process(struct tls_multi *multi,state_name (ks_lame->state), to_link->len, *wakeup); + state_change =tls_process_state (multi, session, to_link, to_link_addr, + to_link_socket_info, wakeup);
src/openvpn/ssl.c tls_process(struct tls_multi *multi, · line 2553
Quoted 10 lines
src/openvpn/ssl.c +0
-9- state_change = false; - -/* -* TLS activity is finished once we get to S_ACTIVE, -* though we will still process acknowledgements. -* -* CHANGED with 2.0 -> now we may send tunnel configuration -* info over the control channel. -*/
You completely removed this comment.
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 15/28] Remove pointless indentation from tls_process.
In reply to Arne SchwabeTrivial.
The commit message is a bit garbled though. Changing the first "add" to "and" makes it better, I think. "decleration" should be "declaration". Also remove the full stop from the summary line and move it at the end of the body.
$ git show -w commit fdbf34d2e4e28c334e18a677b670b8ade9f87ded Author: Arne Schwabe <[email protected]...> Date: Fri Apr 22 16:29:40 2022 +0200
Remove pointless indentation from tls_process.
This is probably a result from earlier code that still needed to be C89 compatible add probably added this to allow variable decleration
src/openvpn/ssl.c +0 -2
@@ -2774,7 +2774,6 @@ tls_process(struct tls_multi *multi, }/* When should we wake up again? */ - {if (ks->state >=S_INITIAL ) { compute_earliest_wakeup(wakeup,
@@ -2813,7 +2812,6 @@ tls_process(struct tls_multi *multi,return false; } -}/* * Called by the top-level event loop.
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 20/28] Change reliable_get_buf_sequenced to reliable_get_entry_sequenced
In reply to Arne SchwabeTrivial change. Applies on top of 15/28 (probably sooner, but this is the one I tested).
Only compile/UT tested.
Quoted 11 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
This returns not just the buffer of a
reliable_entrybut the whole entry. This allows the caller to also inspect the original opcode and packet id.--- src/openvpn/reliable.c | 6 +++--- src/openvpn/reliable.h | 7 +++---- src/openvpn/ssl.c | 7 ++++--- 3 files changed, 10 insertions(+), 10 deletions(-)
Regards,
--
Frank Lichtenheld
[Openvpn-devel] [PATCH applied] Re: Extract session_move_pre_start as own function, use local buffer variable
In reply to Arne SchwabeStared at the code for a bit, and ran the t_server tests, for good measure.
All fine.
"git diff --color-moved=zebra" is non-helpful here, due to the indent change.
Your patch has been applied to the master branch.
commit edc3e9f5c939ca40841b8054732bfae47aad7e11
Author: Arne Schwabe Date: Fri Apr 22 16:29:37 2022 +0200
Extract session_move_pre_start as own function, use local buffer variable
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24151.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Move tls_process_state into its own function
In reply to Arne SchwabeACK from Frankf, but I've done another stare-at-code and submitted to server + client tests (all good).
Uncrustify complained about 2-3 minor things -> adjusted (so possibly there might be conflicts later on).
Your patch has been applied to the master branch.
commit f1b002fb19cc99f1a527cf95b343b5f082b78045
Author: Arne Schwabe Date: Fri Apr 22 16:29:39 2022 +0200
Move tls_process_state into its own function
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24157.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Re: [Openvpn-devel] [PATCH applied] Re: Move tls_process_state into its own function
Hi,
Quoted 9 lines · Gert Doering
ACK from Frankf, but I've done another stare-at-code and submitted to server + client tests (all good).
Uncrustify complained about 2-3 minor things -> adjusted (so possibly there might be conflicts later on).
Your patch has been applied to the master branch.
commit
f1b002fb19cc99f1a527cf95b343b5f082b78045
This is the commit ID "without uncrustify". The correct one is
commit 9a7b95fda56127df6de6fe7c60e08fb5b67a9919 (HEAD -> master)
and the latter one is what will be pushed.
Sorry.
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...[Openvpn-devel] [PATCH applied] Re: Remove pointless indentation from tls_process.
In reply to Arne SchwabeThat was an easy one :-) ("git show -w").
Your patch has been applied to the master branch.
commit 48ed1a3dd6e1dc2849a0dd909f1c848cc2c05f91
Author: Arne Schwabe Date: Fri Apr 22 16:29:40 2022 +0200
Remove pointless indentation from tls_process.
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24166.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Re: [Openvpn-devel] [PATCH 16/28] Move CRL reload to key_state_init from S_START transition
In reply to Arne SchwabeHi,
Quoted 5 lines · Arne Schwabe
The current place that we reload is a bit more efficient since it only triggers reload after a completed 3way handshake. On the other hand the
key_state_initis a much more logical place and with the upcoming HMAC based UDP code and TCP code, the initialisation will only be done after a 3way handshake.
There is something strange. Upon client reconnection the CRL is not always reloaded. It feels as if "some stuff" are already initialized (because we have a session for this client floating around) so we skip that initialization and we also skip reloading the CRL.
Regards,
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH 16/28] Move CRL reload to key_state_init from S_START transition
Hi,
Quoted 11 lines · Antonio Quartulli
Quoted 5 lines · Arne Schwabe
The current place that we reload is a bit more efficient since it only triggers reload after a completed 3way handshake. On the other hand the
key_state_initis a much more logical place and with the upcoming HMAC based UDP code and TCP code, the initialisation will only be done after a 3way handshake.There is something strange. Upon client reconnection the CRL is not always reloaded. It feels as if "some stuff" are already initialized (because we have a session for this client floating around) so we skip that initialization and we also skip reloading the CRL.
Is that different from "without the patch"?
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Antonio Quartulli
Acked-byRe: [Openvpn-devel] [PATCH 16/28] Move CRL reload to key_state_init from S_START transition
Quoted 15 lines · Gert Doering
Hi,
Quoted 11 lines · Antonio Quartulli
Quoted 5 lines · Arne Schwabe
The current place that we reload is a bit more efficient since it only triggers reload after a completed 3way handshake. On the other hand the
key_state_initis a much more logical place and with the upcoming HMAC based UDP code and TCP code, the initialisation will only be done after a 3way handshake.There is something strange. Upon client reconnection the CRL is not always reloaded. It feels as if "some stuff" are already initialized (because we have a session for this client floating around) so we skip that initialization and we also skip reloading the CRL.
I take this back.
I managed to fool myself (and OpenVPN) because instead of really updating the CRL file, I was rather switching between two CRLs (one with client revoked, one with client allowed) using a symlink.
However, as reported in stat(2), stat() will follow the symlink and report stats about the linked file (which had a constant mtime).
To properly test the CRL-reload behaviour, I therefore had to change the symlink and then touch the linked file. This made my test correct and I could check that also the OpenVPN behaviour, with this patch, is actually correct.
Regards,
Signature · 1 line
Antonio Quartulli
[Openvpn-devel] [PATCH applied] Re: Move CRL reload to key_state_init from S_START transition
In reply to Arne SchwabeThanks, Antonio, for the detailed testing. I already checked that "it's only code being moved" but since this is all magic event driven functions, I found "verifying functionality" important here.
missing S-O-B added according to our developer docs.
Your patch has been applied to the master branch.
commit fb6e6c2ae3e6ecdc6ac0b69a79741d52189c0c70
Author: Arne Schwabe Date: Fri Apr 22 16:29:41 2022 +0200
Move CRL reload to key_state_init from S_START transition
Acked-by: Antonio Quartulli <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24156.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Change reliable_get_buf_sequenced to reliable_get_entry_sequenced
In reply to Arne SchwabeStared a bit at the code, for good measure. Does what it says.
S-O-B added.
Your patch has been applied to the master branch.
commit 32f13c1e8902bd44c26b54de45f12e0cab642156
Author: Arne Schwabe Date: Fri Apr 22 16:29:45 2022 +0200
Change reliable_get_buf_sequenced to reliable_get_entry_sequenced
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24153.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Re: [Openvpn-devel] [PATCH 17/28] Implement constructing a control channel reset client as standalone fucntion
In reply to Arne SchwabeHi,
Quoted 2 lines · Arne Schwabe
This implement creating a reset packet without needing to setup a full control session.
I was about to merge this, with "it has a unit test, so what could go wrong".
But...
[ RUN ] test_generate_reset_packet_plain
[ OK ] test_generate_reset_packet_plain
[ RUN ] test_generate_reset_packet_tls_auth [ ERROR ] --- ASSERT: buf_write(&buf, &net_pid, sizeof(net_pid)) [ LINE ] --- ../../../src/openvpn/ssl_pkt.c:425: error: Failure! [ FAILED ] test_generate_reset_packet_tls_auth
Gentoo/OpenSSL 1.1.1n build
[ ERROR ] --- ASSERT: [ LINE ] --- ../../../../openvpn/src/openvpn/ssl_pkt.c:425: error: Failure! [ FAILED ] test_generate_reset_packet_tls_auth
Gentoo/mbedTLS build (with --enable-small, thus, no message)
[ ERROR ] --- ASSERT: buf_write(&buf, &net_pid, sizeof(net_pid)) [ LINE ] --- ../../../../openvpn/src/openvpn/ssl_pkt.c:425: error: Failure! [ FAILED ] test_generate_reset_packet_tls_auth
FreeBSD/OpenSSL 1.1.1l build
Could you have a look?
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Re: [Openvpn-devel] [PATCH 17/28] Implement constructing a control channel reset client as standalone fucntion
Hi,
Quoted 3 lines · Gert Doering
[ ERROR ] --- ASSERT: buf_write(&buf, &net_pid, sizeof(net_pid)) [ LINE ] --- ../../../../openvpn/src/openvpn/ssl_pkt.c:425: error: Failure! [ FAILED ] test_generate_reset_packet_tls_auth
It needs this hunk from 18/28
+struct tls_auth_standalone
+init_tas_auth(int key_direction)
{
struct tls_auth_standalone tas = { 0 };+ struct frame frame = { {.headroom = 200, .payload_size = 1400}, 0};
+ tas.frame = frame;
... and then the tests pass. (This is not really surprising, as it fails buf_write() == the buffer is too small, and without .payload_size, it will be zero-sized...)
Can you move that one over, rebase, and send 17 v2 + 18 v2? (18 will also benefit from rebasing to partially already whitespace-fixed master)
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...PATCH v2 1/1 Implement constructing a control channel reset client as standalone fucntion
In reply to Arne SchwabeThis implement creating a reset packet without needing to setup a full control session.
Patch v2: fix unit test not working without further commits
--- src/openvpn/packet_id.h | 15 ++++ src/openvpn/ssl.h | 6 -- src/openvpn/ssl_pkt.c | 32 +++++++ src/openvpn/ssl_pkt.h | 19 +++++ tests/unit_tests/openvpn/test_pkt.c | 127 +++++++++++++++++++++++----- 5 files changed, 172 insertions(+), 27 deletions(-)
src/openvpn/packet_id.h +15 -0
@@ -289,6 +289,21 @@ packet_id_persist_save_obj(struct packet_id_persist *p, const struct packet_id * } } +/** +* Reset the current send packet id to its initial state. +* Use very carefully only as (e.g. in the standalone reset packet context) +* to avoid sending more than one packet with the same packet id (that is not +* also a resend like the reset packet) +* +* @param p the packet structure to modify +*/ +static inline void +reset_packet_id_send (struct packet_id_send *p) +{ + p->time =0 ; + p->id =0 ; +} +const char *packet_id_net_print (const struct packet_id_net *pin,bool print_timestamp,struct gc_arena *gc); static inline int
src/openvpn/ssl.h +0 -6
@@ -53,12 +53,6 @@*/ #define CONTROL_SEND_ACK_MAX 4 -/* -* Define number of buffers for send and receive in the reliability layer. -*/ -#define TLS_RELIABLE_N_SEND_BUFFERS 4/* also window size for reliability layer */ -#define TLS_RELIABLE_N_REC_BUFFERS 8 -/* * Various timeouts */
src/openvpn/ssl_pkt.c +32 -0
@@ -398,3 +398,35 @@ error:gc_free (&gc);return VERDICT_INVALID ; } + +struct buffer +tls_reset_standalone (struct tls_auth_standalone *tas, +struct session_id *own_sid, +struct session_id *remote_sid, +uint8_t header) +{ +struct buffer buf =alloc_buf (tas->frame.buf.payload_size); +ASSERT (buf_init (&buf, tas->frame.buf.headroom)); + +/* Reliable ACK structure */ +/* Length of the ACK structure - 1 ACK */ +buf_write_u8 (&buf,1 ); + +/* ACKed packet - first packet's id is always 0 */ +buf_write_u32 (&buf,0 ); + +/* Remote session id */ +buf_write (&buf, remote_sid->id,SID_SIZE ); + +/* Packet ID of our own packet: Our reset packet is always using +* packet id 0 since it is the first packet */ +packet_id_type net_pid =htonpid (0 ); + +ASSERT (buf_write (&buf, &net_pid,sizeof (net_pid))); + +/* Add tls-auth/tls-crypt wrapping, this might replace buf */ +tls_wrap_control (&tas->tls_wrap, header, &buf, own_sid); + +return buf; +} +
src/openvpn/ssl_pkt.h +19 -0
@@ -60,6 +60,12 @@#define P_FIRST_OPCODE 3#define P_LAST_OPCODE 10 +/* +* Define number of buffers for send and receive in the reliability layer. +*/ +#define TLS_RELIABLE_N_SEND_BUFFERS 4/* also window size for reliability layer */ +#define TLS_RELIABLE_N_REC_BUFFERS 8 +/* * Used in --mode server mode to check tls-auth signature on initial * packets received from new clients.
@@ -157,6 +163,19 @@ read_control_auth(struct buffer *buf,const struct link_socket_actual *from, const struct tls_options *opt); + +/** +* This function creates a reset packet using the information +* from the tls pre decrypt state. +* +* The returned buf need to be free with \c free_buf +*/ +struct buffer +tls_reset_standalone (struct tls_auth_standalone *tas, +struct session_id *own_sid, +struct session_id *remote_sid, +uint8_t header); +static inline const char *packet_opcode_name (int op) {
tests/unit_tests/openvpn/test_pkt.c +106 -21
176 lines not on this page · raw message
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH v2 1/1 Implement stateless, HMAC basedsesssion id three way handshake
OpenVPN currently has a bit of a weakness in its early three way handshake
A single client reset packet (first packet of the handshake) will
- trigger creating session on the server side leading to poential ressource exhaustian
- make the server respond with 3 answers trying to get an ACK for its answer making it a amplification
Instead of allocating a connection for each client on the initial packet OpenVPN will now send back a response that contains an HMAC based cookie that the client will need to respond to. This eliminates the amplification attack and resource exhaustion attacks. For tls-crypt-v2 client HMAC based handshake is not used yet.
Patch v2: rebase on master
--- doc/doxygen/doc_protocol_overview.h | 2 + src/openvpn/init.c | 4 + src/openvpn/mudp.c | 105 ++++++++++++-- src/openvpn/multi.h | 3 + src/openvpn/openvpn.h | 6 + src/openvpn/ssl.c | 41 +++++- src/openvpn/ssl.h | 8 ++ src/openvpn/ssl_pkt.c | 102 +++++++++++++- src/openvpn/ssl_pkt.h | 55 +++++++- tests/unit_tests/openvpn/test_pkt.c | 204 ++++++++++++++++++++++++++-- 10 files changed, 503 insertions(+), 27 deletions(-)
doc/doxygen/doc_protocol_overview.h +2 -0
@@ -118,6 +118,8 @@* parts: * * - local \c session_id (random 64 bit value to identify TLS session). +* (the tls-server side uses a HMAC of the client to create a pseudo +* random number for a SYN Cookie like approach) * - HMAC signature of entire encapsulation header for HMAC firewall * [only if \c --tls-auth is specified] (usually 16 or 20 bytes). * - packet-id for replay protection (4 or 8 bytes, includes sequence
src/openvpn/init.c +4 -0
@@ -54,6 +54,7 @@#include "forward.h" #include "auth_token.h" #include "mss.h" +#include "mudp.h" #include "memdbg.h"
@@ -2973,7 +2974,9 @@ do_init_crypto_tls(struct context *c, const unsigned int flags)if (flags &CF_INIT_TLS_AUTH_STANDALONE ) { c->c2.tls_auth_standalone =tls_auth_standalone_init (&to, &c->c2.gc); + c->c2.session_id_hmac =session_id_hmac_init (); } + } static void
@@ -2992,6 +2995,7 @@ do_init_frame_tls(struct context *c)tls_init_control_channel_frame_parameters (&c->c2.frame, &c->c2.tls_auth_standalone->frame);frame_print (&c->c2.tls_auth_standalone->frame,D_MTU_INFO ,"TLS-Auth MTU parms" ); + c->c2.tls_auth_standalone->tls_wrap.work =alloc_buf_gc (BUF_SIZE (&c->c2.frame), &c->c2.gc); } }
src/openvpn/mudp.c +95 -10
164 lines not on this page · raw message
src/openvpn/multi.h +3 -0
@@ -191,6 +191,9 @@ struct multi_context {struct context top;/**< Storage structure for process-wide * configuration. */ +struct buffer hmac_reply; +struct link_socket_actual *hmac_reply_dest; +/* * Timer object for stale route check */
src/openvpn/openvpn.h +6 -0
@@ -330,6 +330,12 @@ struct context_2* received from a new client. See the * \c --tls-auth commandline option. */ + +hmac_ctx_t *session_id_hmac; +/**< the HMAC we use to generate and verify our syn cookie like +* session ids from the server. +*/ +/* used to optimize calls to tls_multi_process */ struct interval tmp_int;
src/openvpn/ssl.c +38 -3
76 lines not on this page · raw message
src/openvpn/ssl.h +8 -0
@@ -556,4 +556,12 @@ tls_session_generate_data_channel_keys(struct tls_session *session);void load_xkey_provider (void ); +/* Special method to skip the three way handshake RESET stages. This is +* used by the HMAC code when seeing a packet that matches the previous +* HMAC based stateless server state */ +bool +session_skip_to_pre_start (struct tls_session *session, +struct tls_pre_decrypt_state *state, +struct link_socket_actual *from); +#endif /* ifndef OPENVPN_SSL_H */
src/openvpn/ssl_pkt.c +100 -2
125 lines not on this page · raw message
src/openvpn/ssl_pkt.h +51 -4
74 lines not on this page · raw message
tests/unit_tests/openvpn/test_pkt.c +196 -8
291 lines not on this page · raw message
Signature · 1 line
2.32.0 (Apple Git-132)
Re: [Openvpn-devel] [PATCH 20/28] Change reliable_get_buf_sequenced to reliable_get_entry_sequenced
In reply to Arne SchwabeQuoted 9 lines
* buffer.
*
- * @return A pointer to the buffer of the entry with the next
- * sequential key ID. If no such entry is present, this function
- * returns NULL.
+ * @return A pointer to the entry with the next sequential key ID.
+ * If no such entry is present, this function returns NULL.
*/
As pointed out in the IRC by Gert, this comment is wrong. It should be message or packet id instead of key id.
Arne
Re: [Openvpn-devel] [PATCH v2] Implement constructing a control channel reset client as standalone fucntion
In reply to Arne SchwabeSome small issues Gert might decide to fix on apply:
Typo "fucntion" in summary line of commit message.
Quoted 2 lines
Arne Schwabe <[email protected]...> hat am 28.04.2022 00:34 geschrieben:
This implement creating a reset packet without needing to setup a full control
"implements"
Quoted 1 line
session.
[...]
Quoted 4 lines
src/openvpn/ssl_pkt.c +0
-0
[...]
src/openvpn/ssl_pkt.c error: · line 432
Quoted 6 lines
src/openvpn/ssl_pkt.c +6
-0+/* Add tls-auth/tls-crypt wrapping, this might replace buf */ +tls_wrap_control (&tas->tls_wrap, header, &buf, own_sid); + +return buf; +} +
git am complains about "new blank line at EOF."
Quoted 4 lines
src/openvpn/ssl_pkt.h +0
-0
[...]
src/openvpn/ssl_pkt.h read_control_auth(struct buffer *buf, · line 171
Quoted 10 lines
src/openvpn/ssl_pkt.h +6
-0@@ -157,6 +163,19 @@ read_control_auth(struct buffer *buf,const struct link_socket_actual *from, const struct tls_options *opt); + +/** +* This function creates a reset packet using the information +* from the tls pre decrypt state. +* +* The returned buf need to be free with \c free_buf
"needs"
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH v2] Implement stateless, HMAC basedsesssion id three way handshake
In reply to Arne SchwabeSummary line: "HMAC-based session-id three-way-handshake" maybe? Just to help one parse the word pile ;)
Quoted 5 lines
Arne Schwabe <[email protected]...> hat am 28.04.2022 00:34 geschrieben:
OpenVPN currently has a bit of a weakness in its early three way handshakeA single client reset packet (first packet of the handshake) will
- trigger creating session on the server side leading to poential
"creating a session", "potential"
Quoted 1 line
ressource exhaustian
"exhaustion"
Quoted 1 line
- make the server respond with 3 answers trying to get an ACK for its
Could we maybe slightly elaborate on that? What are those "3" answers?
Quoted 1 line
answer making it a amplification
"an"
Quoted 6 lines
Instead of allocating a connection for each client on the initial packet OpenVPN will now send back a response that contains an HMAC based cookie that the client will need to respond to. This eliminates the amplification attack and resource exhaustion attacks. For tls-crypt-v2 client HMAC based handshake is not used yet.
I think this is not very helpful in understanding the change. In trying to understand it, this is my personal explanation I came up with. Not sure whether it is correct, but maybe it can be useful:
"Instead of allocating a connection for each client on the initial HARD_RESET_CLIENT packet OpenVPN server will now create its own session id for the HARD_RESET_SERVER packet as an HMAC of client data. This way it can verify the session id on the second packet of
the client (ACK or CONTROL) and only create the connection then."
"This eliminates the amplification [...]" (unchanged)
Quoted 17 lines
Patch v2: rebase on master
Signed-off-by Arne Schwabe <[email protected]...>--- doc/doxygen/doc_protocol_overview.h | 2 + src/openvpn/init.c | 4 + src/openvpn/mudp.c | 105 ++++++++++++-- src/openvpn/multi.h | 3 + src/openvpn/openvpn.h | 6 + src/openvpn/ssl.c | 41 +++++- src/openvpn/ssl.h | 8 ++ src/openvpn/ssl_pkt.c | 102 +++++++++++++- src/openvpn/ssl_pkt.h | 55 +++++++- tests/unit_tests/openvpn/test_pkt.c | 204 ++++++++++++++++++++++++++-- 10 files changed, 503 insertions(+), 27 deletions(-)
[...]
src/openvpn/mudp.c line 43
Quoted 9 lines
src/openvpn/mudp.c +1
-0@@ -40,24 +40,81 @@#include <sys/inotify.h> #endif +/* Return if this packet should create a new session */
missing "true" after "Return".
Quoted 56 lines
src/openvpn/mudp.c +40
-6static bool -do_pre_decrypt_check (struct multi_context *m) +do_pre_decrypt_check (struct multi_context *m, +struct tls_pre_decrypt_state *state, +struct mroute_addr addr) {ASSERT (m->top.c2.tls_auth_standalone);enum first_packet_verdict verdict; -struct tls_pre_decrypt_state state = {0 }; - verdict =tls_pre_decrypt_lite (m->top.c2.tls_auth_standalone, &state, - &m->top.c2.from, &m->top.c2.buf); +struct tls_auth_standalone *tas = m->top.c2.tls_auth_standalone; -free_tls_pre_decrypt_state (&state); + verdict =tls_pre_decrypt_lite (tas, state, &m->top.c2.from, &m->top.c2.buf); -if (verdict ==VERDICT_INVALID || verdict ==VERDICT_VALID_CONTROL_V1 ) +hmac_ctx_t *hmac = m->top.c2.session_id_hmac; +struct openvpn_sockaddr *from = &m->top.c2.from.dest; +int handwindow = m->top.options.handshake_window; + + +if (verdict ==VERDICT_VALID_RESET_V3 ) + { +/* For tls-crypt-v2 we need to keep the state of the first packet to +* store the unwrapped key */ +return true; + } +else if (verdict ==VERDICT_VALID_RESET_V2 ) { +/* Calculate the session ID HMAC for our reply and create reset packet */ +struct session_id sid =calculate_session_id_hmac (state->peer_session_id, + from, hmac, handwindow,0 ); +reset_packet_id_send (&tas->tls_wrap.opt.packet_id.send); + tas->tls_wrap.opt.packet_id.rec.initialized = true; +uint8_t header =0 | (P_CONTROL_HARD_RESET_SERVER_V2 <<P_OPCODE_SHIFT ); +struct buffer buf =tls_reset_standalone (tas, &sid, + &state->peer_session_id, header); + + +struct context *c = &m->top; + +buf_reset_len (&c->c2.buffers->aux_buf); +buf_copy (&c->c2.buffers->aux_buf, &buf); + m->hmac_reply = c->c2.buffers->aux_buf; + m->hmac_reply_dest = &m->top.c2.from; +msg (D_MULTI_DEBUG ,"Reset packet from client, sending HMAC based reset challenge" ); +/* We have a reply do not create a new session */ return false; + + } +else if (verdict ==VERDICT_VALID_CONTROL_V1 || verdict ==VERDICT_VALID_ACK_V1 ) + { +/* ACK_V1 contains the peer id (our id) while CONTROL_V1 can but does not
src/openvpn/mudp.c line 103
Quoted 10 lines
src/openvpn/mudp.c +10
-0+* need to contain the peer id */ +struct gc_arena gc =gc_new (); + +bool ret =check_session_id_hmac (state, from, hmac, handwindow); + +const char *peer =print_link_socket_actual (&m->top.c2.from, &gc); +if (!ret) + { + +msg (D_MULTI_MEDIUM ,"Packet with invalid or missing SID from %s" , peer);
Not a review, but rather a question: What actually happens to the connection in this code path?
src/openvpn/mudp.c line 109
Quoted 6 lines
src/openvpn/mudp.c +6
-0+ + } +else + { +msg (D_MULTI_DEBUG ,"Reset packet from client (%s), " +"sending HMAC based reset challenge" , peer);
Huh? This is not a reset packet? Also we're not sending a reset challenge, we just completed it? Should this message say something different?
src/openvpn/mudp.c line 120
Quoted 12 lines
src/openvpn/mudp.c +7
-1+ } +gc_free (&gc); + +return ret; } -return true; + +/* VERDICT_INVALID */ +return false; }/*
[... rest of the code not reviewed, yet ...]
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH v2] Implement stateless, HMAC basedsesssion id three way handshake
Am 29.04.22 um 12:28 schrieb Frank Lichtenheld:
Quoted 10 lines
Summary line: "HMAC-based session-id three-way-handshake" maybe? Just to help one parse the word pile ;)
Quoted 5 lines
Arne Schwabe <[email protected]...> hat am 28.04.2022 00:34 geschrieben:
OpenVPN currently has a bit of a weakness in its early three way handshakeA single client reset packet (first packet of the handshake) will
- trigger creating session on the server side leading to poential"creating a session", "potential"
Quoted 7 lines
Quoted 1 line
ressource exhaustian
"exhaustion"
Quoted 1 line
- make the server respond with 3 answers trying to get an ACK for its
Could we maybe slightly elaborate on that? What are those "3" answers?
Retransmits. If the server does not get an ACK for its response it will resend it 2 times to trigger a client response.
Quoted 16 lines
Quoted 2 lines
answer making it a amplification
"an"
Quoted 6 lines
Instead of allocating a connection for each client on the initial packet OpenVPN will now send back a response that contains an HMAC based cookie that the client will need to respond to. This eliminates the amplification attack and resource exhaustion attacks. For tls-crypt-v2 client HMAC based handshake is not used yet.
I think this is not very helpful in understanding the change. In trying to understand it, this is my personal explanation I came up with. Not sure whether it is correct, but maybe it can be useful:
Quoted 7 lines
"Instead of allocating a connection for each client on the initial
HARD_RESET_CLIENTpacket OpenVPN server will now create its own session id for theHARD_RESET_SERVERpacket as an HMAC of client data. This way it can verify the session id on the second packet of
the client (ACK or CONTROL) and only create the connection then.""This eliminates the amplification [...]" (unchanged)
New try:
Instead of allocating a connection for each client on the initial packet OpenVPN will now calculate a session id based on a HMAC that serves as verifiable cookie that can be checked for authenticity when the client responds with it. This eliminates the amplification attack and resource exhaustion attacks. For tls-crypt-v2 clients the HMAC based handshake is not used yet.
Quoted 11 lines
src/openvpn/mudp.c line 103
Quoted 8 lines
src/openvpn/mudp.c +8
-0+ +bool ret =check_session_id_hmac (state, from, hmac, handwindow); + +const char *peer =print_link_socket_actual (&m->top.c2.from, &gc); +if (!ret) + { + +msg (D_MULTI_MEDIUM ,"Packet with invalid or missing SID from %s" , peer);Not a review, but rather a question: What actually happens to the connection in this code path?
The packet is simply dropped. This can happen when clients send ACK + CONTROL_V1 (clienthello) as response and the ACK (that contains the session id) and the ACK gets lost. OpenVPN 3 client already combine the packet and do not suffer the problem and for OpenVPN 2.x the later patches in the series also ensure that all packets have the session id in them.
This is will be an extra resend for the affected clients.
Quoted 9 lines
src/openvpn/mudp.c line 109
Quoted 6 lines
src/openvpn/mudp.c +6
-0+ + } +else + { +msg (D_MULTI_DEBUG ,"Reset packet from client (%s), " +"sending HMAC based reset challenge" , peer);Huh? This is not a reset packet? Also we're not sending a reset challenge, we just completed it? Should this message say something different?
Yes. I actually fixed the message in "Implement HMAC based session id for tls-crypt v2" but forgot to fix up this commit:
msg(D_MULTI_DEBUG, "Valid packet with HMAC challenge from peer (%s), "
"accepting new connection.", peer);
Arne
Re: [Openvpn-devel] [PATCH v2] Implement stateless, HMAC basedsesssion id three way handshake
In reply to Arne SchwabeThe "offset" part of the review :)
Quoted 1 line
Arne Schwabe <[email protected]...> hat am 28.04.2022 00:34 geschrieben:
[...]
Quoted 4 lines
src/openvpn/ssl_pkt.c +0
-0
[...]
Quoted 23 lines
src/openvpn/ssl_pkt.c +19
-0@@ -430,3 +440,91 @@ tls_reset_standalone(struct tls_auth_standalone *tas,return buf; } + +hmac_ctx_t * +session_id_hmac_init (void ) +{ +/* We assume that SHA256 is always available */ +ASSERT (md_valid ("SHA256" )); +hmac_ctx_t *hmac_ctx =hmac_ctx_new (); + +uint8_t key[SHA256_DIGEST_LENGTH ]; +ASSERT (rand_bytes (key,sizeof (key))); + +hmac_ctx_init (hmac_ctx, key,"SHA256" ); +return hmac_ctx; +} + +struct session_id +calculate_session_id_hmac(struct session_id client_sid, + const struct openvpn_sockaddr *from, + hmac_ctx_t *hmac,
src/openvpn/ssl_pkt.c tls_reset_standalone(struct tls_auth_standalone *tas, · line 471
Quoted 10 lines
src/openvpn/ssl_pkt.c +10
-0+ int handwindow, int offset) +{ +union { +uint8_t hmac_result[SHA256_DIGEST_LENGTH ]; +struct session_id sid; + } result; + +/* Get the valid time quantisation for our hmac, +* we divide time by handwindow/2 and allow the current +* and the previous timestamp */
This comment is misleading. There is no logic here for "current" and "previous" here. The caller makes this decision via offset. So maybe "[...] we divide time by handwindow/2. offset can be used to allow previous timestamps."
Quoted 37 lines
src/openvpn/ssl_pkt.c +37
-0+uint32_t session_id_time = now/((handwindow+1 )/2 ) + offset; + +hmac_ctx_reset (hmac); +/* We do not care about endian here since it does not need to be +* portable */ +hmac_ctx_update (hmac, (const uint8_t *) &session_id_time, +sizeof (session_id_time)); + +/* add client IP and port */ +switch (af_addr_size (from->addr.sa.sa_family)) + { +case AF_INET : +hmac_ctx_update (hmac, (const uint8_t *) &from->addr.in4,sizeof (struct sockaddr_in )); +break ; + +case AF_INET6 : +hmac_ctx_update (hmac, (const uint8_t *) &from->addr.in6,sizeof (struct sockaddr_in6 )); +break ; + } + +/* add session id of client */ +hmac_ctx_update (hmac, client_sid.id,SID_SIZE ); + +hmac_ctx_final (hmac, result.hmac_result); + +return result.sid; +} + +bool +check_session_id_hmac (struct tls_pre_decrypt_state *state, +const struct openvpn_sockaddr *from, +hmac_ctx_t *hmac, +int handwindow) +{ +if (!from) + { +return false;
src/openvpn/ssl_pkt.c tls_reset_standalone(struct tls_auth_standalone *tas, · line 518
Quoted 10 lines
src/openvpn/ssl_pkt.c +10
-0+ } + +struct buffer buf = state->newbuf; +struct reliable_ack ack; + +if (!reliable_ack_parse (&buf, &ack, &state->server_session_id)) + { +return false; + } +
Here we could put a comment like "allow adjacent timestamps".
src/openvpn/ssl_pkt.c tls_reset_standalone(struct tls_auth_standalone *tas, · line 519
Quoted 1 line
src/openvpn/ssl_pkt.c +1
-0+for (int i =-1 ; i<=1 ; i++)
Why not call this variable "offset"?
Quoted 15 lines
src/openvpn/ssl_pkt.c +11
-0+ { +struct session_id expected_id = +calculate_session_id_hmac (state->peer_session_id, from, hmac, handwindow, i); + +if (memcmp_constant_time (&expected_id, &state->server_session_id,SID_SIZE )) + { +return true; + } + } +return false; +}src/openvpn/ssl_pkt.h +0
-0
[...]
Quoted 28 lines
src/openvpn/ssl_pkt.h +24
-0@@ -141,6 +146,48 @@ tls_pre_decrypt_lite(const struct tls_auth_standalone *tas,const struct link_socket_actual *from, const struct buffer *buf); +/* Creates an SHA256 HMAC context with a random key that is used for the +* session id. +* +* We do not support loading this from a config file since continuing session +* between restarts of OpenVPN has never been supported and that includes +* early session setup +*/ +hmac_ctx_t *session_id_hmac_init (void ); + +/** +* Calculates the a HMAC based server session id based on a client session id +* and socket addr. +* +* @param client_sid session id of the client +* @param from link_socket from the client +* @param hmac the hmac context to use for the calculation +* @param handwindow the quantisation of the current time +* @param offset offset to 'now' to use +* @return the expected server session id +*/ +struct session_id +calculate_session_id_hmac(struct session_id client_sid, + const struct openvpn_sockaddr *from, + hmac_ctx_t *hmac,
src/openvpn/ssl_pkt.h tls_pre_decrypt_lite(const struct tls_auth_standalone *tas, · line 182
Quoted 10 lines
src/openvpn/ssl_pkt.h +10
-0+ int handwindow, int offset); + +/** +* Checks if a control packet has a correct HMAC server session id +* +* @param client_sid session id of the client +* @param from link_socket from the client +* @param hmac the hmac context to use for the calculation +* @param handwindow the quantisation of the current time +* @param offset offset to 'now' to use
This function has no parameter "offset".
Quoted 15 lines
src/openvpn/ssl_pkt.h +8
-0+* @return the expected server session id +*/ +bool +check_session_id_hmac (struct tls_pre_decrypt_state *state, +const struct openvpn_sockaddr *from, +hmac_ctx_t *hmac, +int handwindow); +/* * Write a control channel authentication record. */ tests/unit_tests/openvpn/test_pkt.c +0
-0
[...]
Quoted 1 line
tests/unit_tests/openvpn/test_pkt.c +0
-0@@ -325,8 +347,170 @@ test_tls_decrypt_lite_none(void **ut_state)
[...]
Quoted 10 lines
tests/unit_tests/openvpn/test_pkt.c +10
-0+static void +test_verify_hmac_none (void **ut_state) +{ +hmac_ctx_t *hmac =session_id_hmac_init (); + +struct link_socket_actual from = {0 }; +struct tls_auth_standalone tas = {0 }; +struct tls_pre_decrypt_state state = {0 }; + +struct buffer buf =alloc_buf (1024 );
tests/unit_tests/openvpn/test_pkt.c test_tls_decrypt_lite_none(void **ut_state) · line 449
Quoted 10 lines
tests/unit_tests/openvpn/test_pkt.c +10
-0+enum first_packet_verdict verdict; + + tas.tls_wrap.mode =TLS_WRAP_NONE ; + +buf_reset_len (&buf); +buf_write (&buf, client_ack_none_random_id,sizeof (client_ack_none_random_id)); + verdict =tls_pre_decrypt_lite (&tas, &state, &from, &buf); +assert_int_equal (verdict,VERDICT_VALID_ACK_V1 ); + +check_session_id_hmac (&state, &from.dest, hmac,30 );
Shouldn't this have some associated assert?
Quoted 27 lines
tests/unit_tests/openvpn/test_pkt.c +25
-0+free_tls_pre_decrypt_state (&state);free_buf (&buf); +hmac_ctx_cleanup (hmac); +hmac_ctx_free (hmac); +} + +static hmac_ctx_t * +init_static_hmac (void ) +{ +ASSERT (md_valid ("SHA256" )); +hmac_ctx_t *hmac_ctx =hmac_ctx_new (); + +uint8_t key[SHA256_DIGEST_LENGTH ] = {1 ,2 ,3 }; + +hmac_ctx_init (hmac_ctx, key,"SHA256" ); +return hmac_ctx; +} + +static void +test_calc_session_id_hmac_static (void **ut_state) +{ +hmac_ctx_t *hmac =init_static_hmac (); + +struct openvpn_sockaddr addr = {0 }; + +/* we do not use htons functions here since the hmac calculate function
tests/unit_tests/openvpn/test_pkt.c test_tls_decrypt_lite_none(void **ut_state) · line 486
Quoted 10 lines
tests/unit_tests/openvpn/test_pkt.c +10
-0+* also does not care about the endianness of the data but just assumes +* the endianness doesn't change between calls */ + addr.addr.in4.sin_family =AF_INET ; + addr.addr.in4.sin_addr.s_addr =0xff000ff ; + addr.addr.in4.sin_port =1194 ; + + +struct session_id client_id = { {0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 }}; + + now =1005 ;
I would suggest creating a variable or define here for the "100" handwindow. I think that would improve readability and since it must be synced it also makes the code more implicitely correct.
tests/unit_tests/openvpn/test_pkt.c test_tls_decrypt_lite_none(void **ut_state) · line 499
Quoted 13 lines
tests/unit_tests/openvpn/test_pkt.c +13
-0+struct session_id server_id =calculate_session_id_hmac (client_id, &addr, hmac,100 ,0 ); + +struct session_id expected_server_id = { {0xba ,0x83 ,0xa9 ,0x00 ,0x72 ,0xbd ,0x93 ,0xba }}; +assert_memory_equal (expected_server_id.id, server_id.id,SID_SIZE ); + +struct session_id server_id_m1 =calculate_session_id_hmac (client_id, &addr, hmac,100 ,-1 ); +struct session_id server_id_p1 =calculate_session_id_hmac (client_id, &addr, hmac,100 ,1 ); +struct session_id server_id_p2 =calculate_session_id_hmac (client_id, &addr, hmac,100 ,2 ); + +assert_memory_not_equal (expected_server_id.id, server_id_m1.id,SID_SIZE ); +assert_memory_not_equal (expected_server_id.id, server_id_p1.id,SID_SIZE ); + +/* moving now should also move the calculated ids */
"moving now by more than half of handwindow should also move the calculated ids by offset 1"
Quoted 7 lines
tests/unit_tests/openvpn/test_pkt.c +7
-0+ now =1062 ; + +struct session_id server_id2_m2 =calculate_session_id_hmac (client_id, &addr, hmac,100 ,-2 ); +struct session_id server_id2_m1 =calculate_session_id_hmac (client_id, &addr, hmac,100 ,-1 ); +struct session_id server_id2 =calculate_session_id_hmac (client_id, &addr, hmac,100 ,0 ); +struct session_id server_id2_p1 =calculate_session_id_hmac (client_id, &addr, hmac,100 ,1 ); +
tests/unit_tests/openvpn/test_pkt.c test_tls_decrypt_lite_none(void **ut_state) · line 516
Quoted 10 lines
tests/unit_tests/openvpn/test_pkt.c +7
-0+assert_memory_equal (server_id2_m2.id, server_id_m1.id,SID_SIZE ); +assert_memory_equal (server_id2_m1.id, expected_server_id.id,SID_SIZE ); +assert_memory_equal (server_id2.id, server_id_p1.id,SID_SIZE ); +assert_memory_equal (server_id2_p1.id, server_id_p2.id,SID_SIZE ); + +hmac_ctx_cleanup (hmac); +hmac_ctx_free (hmac); } static void
[...]
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 21/28] Extract read_incoming_tls_ciphertext into function
In reply to Arne SchwabeTrivial code move. Applied and compile-tested on top of master + 17 v2 + 18 v2.
Quoted 4 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
This makes the code a bit more structured and easier to read.
[...]
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH v2] Implement stateless, HMAC basedsesssion id three way handshake
In reply to Frank LichtenheldQuoted 2 lines
Frank Lichtenheld <[email protected]...> hat am 29.04.2022 12:28 geschrieben:
Quoted 1 line
Arne Schwabe <[email protected]...> hat am 28.04.2022 00:34 geschrieben:
[...]
Quoted 9 lines
src/openvpn/mudp.c line 109
Quoted 6 lines
src/openvpn/mudp.c +6
-0+ + } +else + { +msg (D_MULTI_DEBUG ,"Reset packet from client (%s), " +"sending HMAC based reset challenge" , peer);Huh? This is not a reset packet? Also we're not sending a reset challenge, we just completed it? Should this message say something different?
Note: the fix for this is already in 22/28.
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 22/28] Implement HMAC based session id for tls-crypt v2
In reply to Arne SchwabeQuoted 14 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
Tls-crypt v2 is more complicated to implement a proper stateless handshake. To allow state handshake this commit does
- introduce a new packet
CONTROL_WKC_V1that repeats the wrapped client key.
- introduce a way to negotiate the support for this packet in the three way handshakeDetails about the protocol changes are in tls-crypt-v2.txt. Optional arguments to the tls-crypt-v2 option have been added to explicitly allow or disallow client that do not support the stateless handshake.
[...]
Quoted 5 lines
Changes.rst +0
-0
Changes.rst Improved ``--mssfix`` and ``--fragment`` calculation · line 78
Quoted 10 lines
Changes.rst +7
-0@@ -69,6 +69,14 @@ Improved ``--mssfix`` and ``--fragment`` calculation account and the resulting size is specified as the total size of the VPN packets including IP and UDP headers. +Cookie based handshake for UDP server + Instead of allocating a connection for each client on the initial packet + OpenVPN will now send back a response that contains an HMAC based cookie + that the client will need to respond to. This eliminates the amplification + attack and resource exhaustion attacks. For tls-crypt-v2 clients, this + requires OpenVPN 2.6 clients or later and the tls-crypt-v2 option allows + controlling if older clients are accepted.
I think this sounds like much more intrusive protocol changes than are actually happening. What about something more like:
Cookie based handshake for UDP server
Instead of allocating a connection for each client on the initial packet OpenVPN server will now use an HMAC based cookie as its session id. This way the server can verify it on completing the handshake without keeping state. This eliminates the amplification and resource exhaustion attacks. For tls-crypt-v2 clients, this requires OpenVPN 2.6 clients or later because the client needs to resend its client key on completing the hand shake. The tls-crypt-v2 option allows controlling if older clients are accepted.
Quoted 5 lines
doc/man-sections/tls-options.rst +0
-0
doc/man-sections/tls-options.rst certificates and keys: https://github.com/OpenVPN/easy-rsa · line 494
Quoted 9 lines
doc/man-sections/tls-options.rst +6
-0@@ -486,6 +486,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa 8000 years'. --tls-crypt-v2 keyfile + + Valid syntax: + :: + + tls-crypt-v2 keyfile [force-cookie] + tls-crypt-v2 keyfile [allow-noncookie]
I find that a slightly confusing mixture of examples. I would go either for
tls-crypt-v2 keyfile [force-cookie|allow-noncookie]
or
tls-crypt-v2 keyfile force-cookie
tls-crypt-v2 keyfile allow-noncookie
tls-crypt-v2 keyfile
doc/man-sections/tls-options.rst certificates and keys: https://github.com/OpenVPN/easy-rsa · line 514
Quoted 12 lines
doc/man-sections/tls-options.rst +5
-0+ Use client-specific tls-crypt keys. For clients, ``keyfile`` is a client-specific tls-crypt key. Such a key@@ -501,6 +508,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa client is using client-specific keys, and automatically select the right mode. + The optional parameters :code:`force-cookie` allows only tls-crypt-v2 + clients that support a cookie based stateless three way handshake that + avoids replay attacks and state exhaustion on the server side (OpenVPN + 2.6 and later). The option :code:`allow-noncookie` explicilty allows
"explicitly"
Quoted 11 lines
doc/man-sections/tls-options.rst +3
-0+ older tls-crypt-v2 clients. The default is (currently) + :code:`allow-noncookie`. + --tls-crypt-v2-verify cmd Run command ``cmd`` to verify the metadata of the client-specific tls-crypt-v2 key of a connecting client. This allows serverdoc/tls-crypt-v2.txt +0
-0
doc/tls-crypt-v2.txt When setting up the openvpn connection: · line 162
Quoted 6 lines
doc/tls-crypt-v2.txt +3
-0@@ -157,6 +157,47 @@ When setting up the openvpn connection: messages. +HMAC Cookie support +------------------- +To avoid exhaustion attack and keeping state for connections that fail to
"and" -> "by" ?
doc/tls-crypt-v2.txt When setting up the openvpn connection: · line 163
Quoted 1 line
doc/tls-crypt-v2.txt +1
-0+complete thethree way handshake, the OpenVPN server will use its own session
"the three"
doc/tls-crypt-v2.txt When setting up the openvpn connection: · line 172
Quoted 9 lines
doc/tls-crypt-v2.txt +9
-0+id as challenge that the client must repeat in the third packet of the +handshake. This introduces a problem. If the server does not keep the wrapped +client key from the initial packet, the server cannot decode the third packet. +Therefore, tls-crypt-v2 in 2.6 allows resending the wrapped key in the third +packet of the handshake with the P_CONTROL_WKC_V1 message. The modified +handshake is as follows (the rest of the handshake is unmodified): + +1. The client creates the P_CONTROL_HARD_RESET_CLIENT_V3 message as before + but to indicate that it supports resending the wrapped key by setting the
"to indicate" -> "indicates"
doc/tls-crypt-v2.txt When setting up the openvpn connection: · line 174
Quoted 2 lines
doc/tls-crypt-v2.txt +2
-0+ packet id of the replay id to 0x0f010000 where the first byte indicates the + early negotiation support and the next bytes the flags.
Uff, that sentence goes on and on :)
Maybe at least split it after 0x0f010000?
"packet id of the replay id to 0x0f010000. The first byte indicates the"
doc/tls-crypt-v2.txt When setting up the openvpn connection: · line 184
Quoted 9 lines
doc/tls-crypt-v2.txt +9
-0+2. The server responds with a P_CONTROL_HARD_RESET_V2 message. Instead of having + an empty payload like normally, the payload consists of TLV (type (uint16), + length (uint16), value) packets. TLV was chosen + to allow extensibility in the future. Currently only the following TLV is + defined: + + flags - type 0x01, length 2. + + Bit 1 indicates that the client needs to resend the WKC in the third packet.
"WKc"
Quoted 4 lines
doc/tls-crypt-v2.txt +4
-0+ +3. Instead of normal P_ACK_V1 or P_CONTROL_V1 packet, the client will send a + P_CONTROL_WKC_V1 packet. The P_CONTROL_WKC_V1 is identical to a normal + P_CONTROL_V1 packet but with the WKc appended.
doc/tls-crypt-v2.txt When setting up the openvpn connection: · line 198
Quoted 10 lines
doc/tls-crypt-v2.txt +10
-0+ + Normally the first message of the client is either P_ACK_V1, directly + followed by a P_CONTROL_V1 message that contains the TLS Client Hello or + just a P_CONTROL_V1 message. Instead of a P_ACK_V1 message the client should + send a P_CONTROL_WKC_V1 message with an empty payload. This message must + also include an ACK for the P_CONTROL_HARD_RESET_V2 message. + + When directly sending the TLS Client Hello message in the P_CONTROL_WKC_V1 + message, the client must ensure that the resulting P_CONTROL_WKC_V1 message + with the appended Wkc does not extend the control message length.
"WKc"
[...]
Quoted 4 lines
src/openvpn/ssl_pkt.h +0
-0
[...]
Quoted 14 lines
src/openvpn/ssl_pkt.h +10
-0@@ -262,4 +273,23 @@ packet_opcode_name(int op)return "P_???" ; } } + +/* initial packet id (instead of 0) that indicates that the peer supports +* early protocol negotiation. This will make the packet id turn a bit faster +* but the network time part of the packet id could take care of that. And +* this is also a rather theoretical scenario as it still needs more than +* 2^31 control channel packets to happen */ +#define EARLY_NEG_MASK 0xff000000 +#define EARLY_NEG_START 0x0a000000 + +#define EARLY_NEG_RESENDWKC 0x00010000
src/openvpn/ssl_pkt.h packet_opcode_name(int op) · line 295
Quoted 10 lines
src/openvpn/ssl_pkt.h +9
-0+ + +/* Early negotiation that part of the server response in the RESET_V2 packet. +* Since clients that announce early negotiation support will treat the payload +* of reset packets special and parse it as TLV messages. +* as TLV (type, length, value) */ +#define EARLY_NEG_TLV_FLAG 0x01 +#define EARLY_NEG_FLAG_RESEND_WKC 0x01#endif /* ifndef SSL_PKT_H */ +
new blank line at EOF.
tests/unit_tests/openvpn/test_pkt.c test_generate_reset_packet_plain(void **ut_state) · line 534
Quoted 10 lines
tests/unit_tests/openvpn/test_pkt.c +1
-1@@ -531,7 +531,7 @@ test_generate_reset_packet_plain(void **ut_state)uint8_t header =0 | (P_CONTROL_HARD_RESET_CLIENT_V2 <<P_OPCODE_SHIFT ); -struct buffer buf =tls_reset_standalone (&tas, &client_id, &server_id, header); +struct buffer buf =tls_reset_standalone (&tas, &client_id, &server_id, header,0 ,0 );
Nitpick: the second 0 should be "false" instead. Same for all further calls in this file.
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 22/28] Implement HMAC based session id for tls-crypt v2
Quoted 2 lines
Frank Lichtenheld <[email protected]...> hat am 29.04.2022 15:11 geschrieben:
Quoted 1 line
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
[...]
Quoted 6 lines
Quoted 6 lines
tests/unit_tests/openvpn/test_pkt.c +0
-0@@ -531,7 +531,7 @@ test_generate_reset_packet_plain(void **ut_state)
Quoted 6 lines
tests/unit_tests/openvpn/test_pkt.c test_generate_reset_packet_plain(void **ut_state) · line 534
Quoted 4 lines
tests/unit_tests/openvpn/test_pkt.c +1
-1uint8_t header =0 | (P_CONTROL_HARD_RESET_CLIENT_V2 <<P_OPCODE_SHIFT ); -struct buffer buf =tls_reset_standalone (&tas, &client_id, &server_id, header); +struct buffer buf =tls_reset_standalone (&tas, &client_id, &server_id, header,0 ,0 );Nitpick: the second 0 should be "false" instead. Same for all further calls in this file.
Note: fixed in 23/28
Regards,
--
Frank Lichtenheld
[Openvpn-devel] [PATCH applied] Re: Implement constructing a control channel reset client as standalone function
In reply to Arne SchwabeStared at the code for a bit, seems to make sense and the unit test finds it a valid packet (mbedTLS and OpenSSL).
v2 also adds uncrustify fixes to test_pkt.c, so the tree is clean wrt uncrustify 0.72 now again. Great! (Even though the array indent does not look nice yet this way)
Fixed the 3 typos Frank pointed out plus the "carefully only as" comment text that was discussed on IRC (for v1 already).
Your patch has been applied to the master branch.
commit 870af5f54967821c72074a7c5c60e10a4561d95e
Author: Arne Schwabe Date: Thu Apr 28 00:34:18 2022 +0200
Implement constructing a control channel reset client as standalone function
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24240.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Re: [Openvpn-devel] [PATCH 22/28] Implement HMAC based session id for tls-crypt v2
In reply to Frank LichtenheldQuoted 10 lines
Frank Lichtenheld <[email protected]...> hat am 29.04.2022 15:18 geschrieben:
Quoted 2 lines
Frank Lichtenheld <[email protected]...> hat am 29.04.2022 15:11 geschrieben:
Quoted 1 line
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
[...]
Quoted 6 lines
Quoted 6 lines
tests/unit_tests/openvpn/test_pkt.c +0
-0@@ -531,7 +531,7 @@ test_generate_reset_packet_plain(void **ut_state)
Quoted 8 lines
Quoted 6 lines
tests/unit_tests/openvpn/test_pkt.c test_generate_reset_packet_plain(void **ut_state) · line 534
Quoted 4 lines
tests/unit_tests/openvpn/test_pkt.c +1
-1uint8_t header =0 | (P_CONTROL_HARD_RESET_CLIENT_V2 <<P_OPCODE_SHIFT ); -struct buffer buf =tls_reset_standalone (&tas, &client_id, &server_id, header); +struct buffer buf =tls_reset_standalone (&tas, &client_id, &server_id, header,0 ,0 );Nitpick: the second 0 should be "false" instead. Same for all further calls in this file.
Note: fixed in 23/28
Note: the issue here is worse than I initially assumed. Not only should the second 0 be false, the first 0 is just completely wrong, since the second new parameter was added as the first parameter.
So the UT is actually broken in 22/28 and needs the fix from 23/28:
[ RUN ] test_generate_reset_packet_plain [ ERROR ] --- ASSERT: buf_write(&buf, &net_pid, sizeof(net_pid)) [ LINE ] --- ../../../src/openvpn/ssl_pkt.c:444: error: Failure! [ FAILED ] test_generate_reset_packet_plain [ RUN ] test_generate_reset_packet_tls_auth [ ERROR ] --- ASSERT: buf_write(&buf, &net_pid, sizeof(net_pid)) [ LINE ] --- ../../../src/openvpn/ssl_pkt.c:444: error: Failure! [ FAILED ] test_generate_reset_packet_tls_auth
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH v2] Implement stateless, HMAC basedsesssion id three way handshake
In reply to Arne SchwabeQuoted 21 lines
Arne Schwabe <[email protected]...> hat am 29.04.2022 13:31 geschrieben:
Am 29.04.22 um 12:28 schrieb Frank Lichtenheld:Quoted 10 lines
Quoted 5 lines
Instead of allocating a connection for each client on the initial packet OpenVPN will now send back a response that contains an HMAC based cookie that the client will need to respond to. This eliminates the amplification attack and resource exhaustion attacks. For tls-crypt-v2 client HMAC based handshake is not used yet.
I think this is not very helpful in understanding the change. In trying to understand it, this is my personal explanation I came up with. Not sure whether it is correct, but maybe it can be useful:
Quoted 7 lines
"Instead of allocating a connection for each client on the initial
HARD_RESET_CLIENTpacket OpenVPN server will now create its own session id for theHARD_RESET_SERVERpacket as an HMAC of client data. This way it can verify the session id on the second packet of
the client (ACK or CONTROL) and only create the connection then.""This eliminates the amplification [...]" (unchanged)
Quoted 8 lines
New try:
Instead of allocating a connection for each client on the initial packet OpenVPN will now calculate a session id based on a HMAC that serves as verifiable cookie that can be checked for authenticity when the client responds with it. This eliminates the amplification attack and resource exhaustion attacks. For tls-crypt-v2 clients the HMAC based handshake is not used yet.
I'm not sure why you are against including more low-level details here, but I think this is good enough to not warrant further discussion.
Quoted 12 lines
Quoted 11 lines
src/openvpn/mudp.c line 103
Quoted 8 lines
src/openvpn/mudp.c +8
-0+ +bool ret =check_session_id_hmac (state, from, hmac, handwindow); + +const char *peer =print_link_socket_actual (&m->top.c2.from, &gc); +if (!ret) + { + +msg (D_MULTI_MEDIUM ,"Packet with invalid or missing SID from %s" , peer);Not a review, but rather a question: What actually happens to the connection in this code path?
Quoted 8 lines
The packet is simply dropped. This can happen when clients send ACK +
CONTROL_V1(clienthello) as response and the ACK (that contains the session id) and the ACK gets lost. OpenVPN 3 client already combine the packet and do not suffer the problem and for OpenVPN 2.x the later patches in the series also ensure that all packets have the session id in them.This is will be an extra resend for the affected clients.
Okay, thanks.
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 22/28] Implement HMAC based session id for tls-crypt v2
In reply to Arne SchwabeOne additional small issue:
Quoted 1 line
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
[...]
Quoted 6 lines
src/openvpn/ssl_pkt.h +0
-0@@ -218,10 +224,12 @@ read_control_auth(struct buffer *buf,* The returned buf need to be free with \c free_buf
src/openvpn/ssl_pkt.h read_control_auth(struct buffer *buf, · line 232
Quoted 10 lines
src/openvpn/ssl_pkt.h +4
-2*/ struct buffer -tls_reset_standalone (struct tls_auth_standalone *tas, +tls_reset_standalone (struct tls_wrap_ctx *ctx, +struct tls_auth_standalone *tas,struct session_id *own_sid,struct session_id *remote_sid, -uint8_t header); +uint8_t header, +bool b);
"b" -> "tlscryptv2"
Regards,
--
Frank Lichtenheld
PATCH v3 1/1 Implement stateless, HMAC basedsesssion id three way handshake
In reply to Arne SchwabeOpenVPN currently has a bit of a weakness in its early three way handshake
A single client reset packet (first packet of the handshake) will
- trigger creating a session on the server side leading to potential ressource exhaustion
- make the server respond with 3 answers trying to get an ACK for its P_CONTROL_HARD_RESET_SERVER_V2 answer making it an amplification
Instead of allocating a connection for each client on the initial packet OpenVPN will now calculate a session id based on a HMAC that serves as verifiable cookie that can be checked for authenticity when the client responds with it. This eliminates the amplification attack and resource exhaustion attacks. For tls-crypt-v2 clients the HMAC based handshake is not used yet.
Patch v2: rebase on master
patch v3: fix unit tests, improve comment/style of code
--- doc/doxygen/doc_protocol_overview.h | 2 + src/openvpn/init.c | 4 + src/openvpn/mudp.c | 105 ++++++++++++-- src/openvpn/multi.h | 3 + src/openvpn/openvpn.h | 6 + src/openvpn/ssl.c | 41 +++++- src/openvpn/ssl.h | 8 ++ src/openvpn/ssl_pkt.c | 104 +++++++++++++- src/openvpn/ssl_pkt.h | 54 ++++++- tests/unit_tests/openvpn/test_pkt.c | 210 ++++++++++++++++++++++++++-- 10 files changed, 509 insertions(+), 28 deletions(-)
doc/doxygen/doc_protocol_overview.h +2 -0
@@ -118,6 +118,8 @@* parts: * * - local \c session_id (random 64 bit value to identify TLS session). +* (the tls-server side uses a HMAC of the client to create a pseudo +* random number for a SYN Cookie like approach) * - HMAC signature of entire encapsulation header for HMAC firewall * [only if \c --tls-auth is specified] (usually 16 or 20 bytes). * - packet-id for replay protection (4 or 8 bytes, includes sequence
src/openvpn/init.c +4 -0
@@ -54,6 +54,7 @@#include "forward.h" #include "auth_token.h" #include "mss.h" +#include "mudp.h" #include "memdbg.h"
@@ -2973,7 +2974,9 @@ do_init_crypto_tls(struct context *c, const unsigned int flags)if (flags &CF_INIT_TLS_AUTH_STANDALONE ) { c->c2.tls_auth_standalone =tls_auth_standalone_init (&to, &c->c2.gc); + c->c2.session_id_hmac =session_id_hmac_init (); } + } static void
@@ -2992,6 +2995,7 @@ do_init_frame_tls(struct context *c)tls_init_control_channel_frame_parameters (&c->c2.frame, &c->c2.tls_auth_standalone->frame);frame_print (&c->c2.tls_auth_standalone->frame,D_MTU_INFO ,"TLS-Auth MTU parms" ); + c->c2.tls_auth_standalone->tls_wrap.work =alloc_buf_gc (BUF_SIZE (&c->c2.frame), &c->c2.gc); } }
src/openvpn/mudp.c +95 -10
164 lines not on this page · raw message
src/openvpn/multi.h +3 -0
@@ -191,6 +191,9 @@ struct multi_context {struct context top;/**< Storage structure for process-wide * configuration. */ +struct buffer hmac_reply; +struct link_socket_actual *hmac_reply_dest; +/* * Timer object for stale route check */
src/openvpn/openvpn.h +6 -0
@@ -330,6 +330,12 @@ struct context_2* received from a new client. See the * \c --tls-auth commandline option. */ + +hmac_ctx_t *session_id_hmac; +/**< the HMAC we use to generate and verify our syn cookie like +* session ids from the server. +*/ +/* used to optimize calls to tls_multi_process */ struct interval tmp_int;
src/openvpn/ssl.c +38 -3
76 lines not on this page · raw message
src/openvpn/ssl.h +8 -0
@@ -556,4 +556,12 @@ tls_session_generate_data_channel_keys(struct tls_session *session);void load_xkey_provider (void ); +/* Special method to skip the three way handshake RESET stages. This is +* used by the HMAC code when seeing a packet that matches the previous +* HMAC based stateless server state */ +bool +session_skip_to_pre_start (struct tls_session *session, +struct tls_pre_decrypt_state *state, +struct link_socket_actual *from); +#endif /* ifndef OPENVPN_SSL_H */
src/openvpn/ssl_pkt.c +102 -2
127 lines not on this page · raw message
src/openvpn/ssl_pkt.h +50 -4
73 lines not on this page · raw message
tests/unit_tests/openvpn/test_pkt.c +201 -9
305 lines not on this page · raw message
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH v3 1/1 Implement HMAC based session id for tls-crypt v2
In reply to Arne SchwabeTls-crypt v2 is more complicated to implement a proper stateless handshake. To allow state handshake this commit does
- introduce a new packet CONTROL_WKC_V1 that repeats the wrapped client key.
- introduce a way to negotiate the support for this packet in the three way handshake
Details about the protocol changes are in tls-crypt-v2.txt. Optional arguments to the tls-crypt-v2 option have been added to explicitly allow or disallow client that do not support the stateless handshake.
Patch v3: improve grammar, style, comments, fix unit tests
--- Changes.rst | 10 +++ doc/man-sections/tls-options.rst | 14 ++++ doc/tls-crypt-v2.txt | 41 +++++++++ src/openvpn/crypto.h | 8 ++ src/openvpn/init.c | 4 + src/openvpn/mudp.c | 85 ++++++++++++++----- src/openvpn/options.c | 13 +++ src/openvpn/options.h | 3 + src/openvpn/reliable.h | 2 - src/openvpn/ssl.c | 125 +++++++++++++++++++++++++--- src/openvpn/ssl_pkt.c | 28 +++++-- src/openvpn/ssl_pkt.h | 36 +++++++- tests/unit_tests/openvpn/test_pkt.c | 10 +-- 13 files changed, 334 insertions(+), 45 deletions(-)
Changes.rst +10 -0
@@ -69,6 +69,16 @@ Improved ``--mssfix`` and ``--fragment`` calculation account and the resulting size is specified as the total size of the VPN packets including IP and UDP headers. +Cookie based handshake for UDP server + Instead of allocating a connection for each client on the initial packet + OpenVPN server will now use an HMAC based cookie as its session id. This + way the server can verify it on completing the handshake without keeping + state. This eliminates the amplification and resource exhaustion attacks. + For tls-crypt-v2 clients, this requires OpenVPN 2.6 clients or later + because the client needs to resend its client key on completing the hand + shake. The tls-crypt-v2 option allows controlling if older clients are + accepted. + Deprecated features ------------------- ``inetd`` has been removed
doc/man-sections/tls-options.rst +14 -0
@@ -486,6 +486,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa 8000 years'. --tls-crypt-v2 keyfile + + Valid syntax: + :: + tls-crypt-v2 keyfile + tls-crypt-v2 keyfile force-cookie + tls-crypt-v2 keyfile allow-noncookie + Use client-specific tls-crypt keys. For clients, ``keyfile`` is a client-specific tls-crypt key. Such a key
@@ -501,6 +508,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa client is using client-specific keys, and automatically select the right mode. + The optional parameters :code:`force-cookie` allows only tls-crypt-v2 + clients that support a cookie based stateless three way handshake that + avoids replay attacks and state exhaustion on the server side (OpenVPN + 2.6 and later). The option :code:`allow-noncookie` explicitly allows + older tls-crypt-v2 clients. The default is (currently) + :code:`allow-noncookie`. + --tls-crypt-v2-verify cmd Run command ``cmd`` to verify the metadata of the client-specific tls-crypt-v2 key of a connecting client. This allows server
doc/tls-crypt-v2.txt +41 -0
47 lines not on this page · raw message
src/openvpn/crypto.h +8 -0
@@ -256,6 +256,14 @@ struct crypto_options/**< Bit-flag indicating that data channel key derivation * is done using TLS keying material export [RFC5705] */ +#define CO_RESEND_WKC (1<<4) +/**< Bit-flag indicating that the client is expected to +* resend the wrapped client key with the 2nd packet (packet-id 1) +* like with the HARD_RESET_CLIENT_V3 packet */ +#define CO_FORCE_TLSCRYPTV2_COOKIE (1<<5) +/**< Bit-flag indicating that we do not allow clients that do +* not support resending the wrapped client key (WKc) with the +* third packet of the three-way handshake */ unsigned int flags;/**< Bit-flags determining behavior of * security operation functions. */ };
src/openvpn/init.c +4 -0
@@ -2960,6 +2960,10 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) { to.tls_wrap.tls_crypt_v2_server_key = c->c1.ks.tls_crypt_v2_server_key; to.tls_crypt_v2_verify_script = c->options.tls_crypt_v2_verify_script; +if (options->ce.tls_crypt_v2_force_cookie) + { + to.tls_wrap.opt.flags |=CO_FORCE_TLSCRYPTV2_COOKIE ; + } } }
src/openvpn/mudp.c +66 -19
118 lines not on this page · raw message
src/openvpn/options.c +13 -0
@@ -8896,6 +8896,19 @@ add_option(struct options *options, options->ce.tls_crypt_v2_file = p[1 ]; options->ce.tls_crypt_v2_file_inline = is_inline; } + +if (p[2 ] &&streq (p[2 ],"force-cookie" )) + { + options->ce.tls_crypt_v2_force_cookie = true; + } +else if (p[2 ] &&streq (p[2 ],"allow-noncookie" )) + { + options->ce.tls_crypt_v2_force_cookie = false; + } +else if (p[2 ]) + { +msg (msglevel,"Unsupported tls-crypt-v2 argument: %s" , p[2 ]); + } }else if (streq (p[0 ],"tls-crypt-v2-verify" ) && p[1 ] && !p[2 ]) {
src/openvpn/options.h +3 -0
@@ -162,6 +162,9 @@ struct connection_entry* authenticated encryption v2 */ const char *tls_crypt_v2_file;bool tls_crypt_v2_file_inline; + +/* Allow only client that support resending the wrapped client key */ +bool tls_crypt_v2_force_cookie; };struct remote_entry
src/openvpn/reliable.h +0 -2
@@ -361,8 +361,6 @@ struct reliable_entry *reliable_get_entry_sequenced(struct reliable *rel);* * @param rel The reliable structure associated with the given buffer. * @param buf The buffer of the reliable entry which is to be removed. -* @param inc_pid If true, the reliable structure's packet ID counter -* will be incremented. */ void reliable_mark_deleted (struct reliable *rel,struct buffer *buf);
src/openvpn/ssl.c +114 -11
177 lines not on this page · raw message
src/openvpn/ssl_pkt.c +23 -5
@@ -148,7 +148,8 @@ tls_wrap_control(struct tls_wrap_ctx *ctx, uint8_t header, struct buffer *buf,return ; } -if ((header >>P_OPCODE_SHIFT ) ==P_CONTROL_HARD_RESET_CLIENT_V3 ) +if ((header >>P_OPCODE_SHIFT ) ==P_CONTROL_HARD_RESET_CLIENT_V3 + || (header >>P_OPCODE_SHIFT ) ==P_CONTROL_WKC_V1 ) {if (!buf_copy (&ctx->work, ctx->tls_crypt_v2_wkc))
@@ -197,7 +198,8 @@ read_control_auth(struct buffer *buf,bool ret = false;const uint8_t opcode = *(BPTR (buf)) >>P_OPCODE_SHIFT ; -if (opcode ==P_CONTROL_HARD_RESET_CLIENT_V3 +if ((opcode ==P_CONTROL_HARD_RESET_CLIENT_V3 + || opcode ==P_CONTROL_WKC_V1 ) && !tls_crypt_v2_extract_client_key (buf, ctx, opt)) { msg(D_TLS_ERRORS,
@@ -321,6 +323,7 @@ tls_pre_decrypt_lite(const struct tls_auth_standalone *tas,if (op !=P_CONTROL_HARD_RESET_CLIENT_V2 && op !=P_CONTROL_HARD_RESET_CLIENT_V3 && op !=P_CONTROL_V1 + && op !=P_CONTROL_WKC_V1 && op !=P_ACK_V1 ) {/*
@@ -397,6 +400,10 @@ tls_pre_decrypt_lite(const struct tls_auth_standalone *tas, {return VERDICT_VALID_RESET_V3 ; } + else if (op == P_CONTROL_WKC_V1) + { +return VERDICT_VALID_WKC_V1 ; + } else {return VERDICT_VALID_RESET_V2 ;
@@ -410,10 +417,12 @@ error:struct buffer -tls_reset_standalone (struct tls_auth_standalone *tas, +tls_reset_standalone (struct tls_wrap_ctx *ctx, +struct tls_auth_standalone *tas,struct session_id *own_sid,struct session_id *remote_sid, -uint8_t header) +uint8_t header, +bool tlscryptv2) {struct buffer buf =alloc_buf (tas->frame.buf.payload_size);ASSERT (buf_init (&buf, tas->frame.buf.headroom));
@@ -434,8 +443,17 @@ tls_reset_standalone(struct tls_auth_standalone *tas,ASSERT (buf_write (&buf, &net_pid,sizeof (net_pid))); +/* Add indication for tls-crypt-v2 to resend the packet with the with +* reply */ +if (tlscryptv2) + { +buf_write_u16 (&buf,EARLY_NEG_TLV_FLAG );/* TYPE: flags */ +buf_write_u16 (&buf,sizeof (uint16_t )); +buf_write_u16 (&buf,EARLY_NEG_FLAG_RESEND_WKC ); + } +/* Add tls-auth/tls-crypt wrapping, this might replace buf */ -tls_wrap_control (&tas->tls_wrap, header, &buf, own_sid); +tls_wrap_control (ctx, header, &buf, own_sid);return buf; }
src/openvpn/ssl_pkt.h +33 -3
@@ -54,11 +54,15 @@/* indicates key_method >= 2 and client-specific tls-crypt key */ #define P_CONTROL_HARD_RESET_CLIENT_V3 10/* initial key from client, forget previous state */ +/* Variant of P_CONTROL_V1 but with appended wrapped key +* like P_CONTROL_HARD_RESET_CLIENT_V3 */ +#define P_CONTROL_WKC_V1 11 +/* define the range of legal opcodes * Since we do no longer support key-method 1 we consider * the v1 op codes invalid */ #define P_FIRST_OPCODE 3 -#define P_LAST_OPCODE 10 +#define P_LAST_OPCODE 11/* * Define number of buffers for send and receive in the reliability layer.
@@ -86,6 +90,8 @@ enum first_packet_verdict {/** This packet is a valid ACK control packet from the peer, * i.e. it has a valid session id hmac in it */ VERDICT_VALID_ACK_V1 , +/** The packet is a valid control packet with appended wrapped client key */ +VERDICT_VALID_WKC_V1 ,/** the packet failed on of the various checks */ VERDICT_INVALID };
@@ -217,10 +223,12 @@ read_control_auth(struct buffer *buf,* The returned buf needs to be free with \c free_buf */ struct buffer -tls_reset_standalone (struct tls_auth_standalone *tas, +tls_reset_standalone (struct tls_wrap_ctx *ctx, +struct tls_auth_standalone *tas,struct session_id *own_sid,struct session_id *remote_sid, -uint8_t header); +uint8_t header, +bool tlscryptv2);static inline const char *packet_opcode_name (int op)
@@ -248,6 +256,9 @@ packet_opcode_name(int op)case P_CONTROL_V1 :return "P_CONTROL_V1" ; +case P_CONTROL_WKC_V1 : +return "P_CONTROL_WKC_V1" ; +case P_ACK_V1 :return "P_ACK_V1" ;
@@ -261,4 +272,23 @@ packet_opcode_name(int op) return "P_???"; } } + +/* initial packet id (instead of 0) that indicates that the peer supports +* early protocol negotiation. This will make the packet id turn a bit faster +* but the network time part of the packet id could take care of that. And +* this is also a rather theoretical scenario as it still needs more than +* 2^31 control channel packets to happen */ +#define EARLY_NEG_MASK 0xff000000 +#define EARLY_NEG_START 0x0a000000 + +#define EARLY_NEG_RESENDWKC 0x00010000 + + +/* Early negotiation that part of the server response in the RESET_V2 packet. +* Since clients that announce early negotiation support will treat the payload +* of reset packets special and parse it as TLV messages. +* as TLV (type, length, value) */ +#define EARLY_NEG_TLV_FLAG 0x01 +#define EARLY_NEG_FLAG_RESEND_WKC 0x01#endif /* ifndef SSL_PKT_H */ +
tests/unit_tests/openvpn/test_pkt.c +5 -5
@@ -536,7 +536,7 @@ test_generate_reset_packet_plain(void **ut_state)uint8_t header =0 | (P_CONTROL_HARD_RESET_CLIENT_V2 <<P_OPCODE_SHIFT ); -struct buffer buf =tls_reset_standalone (&tas, &client_id, &server_id, header); +struct buffer buf =tls_reset_standalone (&tas.tls_wrap, &tas, &client_id, &server_id, header, false); verdict =tls_pre_decrypt_lite (&tas, &state, &from, &buf);
@@ -544,7 +544,7 @@ test_generate_reset_packet_plain(void **ut_state)/* Assure repeated generation of reset is deterministic/stateless*/ assert_memory_equal (state.peer_session_id.id, client_id.id,SID_SIZE ); -struct buffer buf2 =tls_reset_standalone (&tas, &client_id, &server_id, header); +struct buffer buf2 =tls_reset_standalone (&tas.tls_wrap, &tas, &client_id, &server_id, header, false);assert_int_equal (BLEN (&buf),BLEN (&buf2));assert_memory_equal (BPTR (&buf),BPTR (&buf2),BLEN (&buf));free_buf (&buf2);
@@ -571,7 +571,7 @@ test_generate_reset_packet_tls_auth(void **ut_state) now =0x22446688 ;reset_packet_id_send (&tas_client.tls_wrap.opt.packet_id.send); -struct buffer buf =tls_reset_standalone (&tas_client, &client_id, &server_id, header); +struct buffer buf =tls_reset_standalone (&tas_client.tls_wrap, &tas_client, &client_id, &server_id, header, false);enum first_packet_verdict verdict =tls_pre_decrypt_lite (&tas_server, &state, &from, &buf);assert_int_equal (verdict,VERDICT_VALID_RESET_V2 );
@@ -580,11 +580,11 @@ test_generate_reset_packet_tls_auth(void **ut_state)/* Assure repeated generation of reset is deterministic/stateless*/ reset_packet_id_send (&tas_client.tls_wrap.opt.packet_id.send); -struct buffer buf2 =tls_reset_standalone (&tas_client, &client_id, &server_id, header); +struct buffer buf2 =tls_reset_standalone (&tas_client.tls_wrap, &tas_client, &client_id, &server_id, header,false);assert_int_equal (BLEN (&buf),BLEN (&buf2));assert_memory_equal (BPTR (&buf),BPTR (&buf2),BLEN (&buf)); +free_buf (&buf2); -free_tls_pre_decrypt_state (&state);packet_id_free (&tas_client.tls_wrap.opt.packet_id);
Signature · 1 line
2.32.0 (Apple Git-132)
Re: [Openvpn-devel] [PATCH v3] Implement stateless, HMAC basedsesssion id three way handshake
In reply to Arne SchwabeUsed this patch to make myself familiar with the OpenVPN RFC documentation enough to say that this change overall makes sense to me. No further issues found in the code. Just some more text/whitespace issues, see below.
Compile/UT tested only from my side.
Small issues:
You have ignored my comment about the commit's summary line:
Summary line: "HMAC-based session-id three-way-handshake" maybe? Just to help one parse the word pile ;)
Quoted 1 line
Arne Schwabe <[email protected]...> hat am 02.05.2022 17:43 geschrieben:
[...]
Quoted 5 lines
src/openvpn/mudp.c +0
-0@@ -40,24 +40,81 @@
[...]
Quoted 9 lines
src/openvpn/mudp.c +9
-0+else if (verdict ==VERDICT_VALID_CONTROL_V1 || verdict ==VERDICT_VALID_ACK_V1 ) + { +/* ACK_V1 contains the peer id (our id) while CONTROL_V1 can but does not +* need to contain the peer id */ +struct gc_arena gc =gc_new (); + +bool ret =check_session_id_hmac (state, from, hmac, handwindow); + +const char *peer =print_link_socket_actual (&m->top.c2.from, &gc);
src/openvpn/mudp.c
Quoted 10 lines
src/openvpn/mudp.c +10
-0+if (!ret) + { + +msg (D_MULTI_MEDIUM ,"Packet with invalid or missing SID from %s" , peer); + + } +else + { +msg (D_MULTI_DEBUG ,"Valid packet with HMAC challenge from peer (%s), " +"accepting new connection." , peer);
uncrustify is not happy with this one:
src/openvpn/mudp.c" 2022-05-03 10:51:05.215071400 +0200 renamed from src/openvpn/mudp.c" 2022-05-03 10:51:05.039037300 +0200 +1 -1
@@ -106,7 +106,7 @@ else { msg(D_MULTI_DEBUG, "Valid packet with HMAC challenge from peer (%s), " - "accepting new connection.", peer); + "accepting new connection.", peer); } gc_free(&gc);
src/openvpn/mudp.c multi_get_create_instance_udp(struct multi_context *m, bool *floated) · line 175
Quoted 6 lines
src/openvpn/mudp.c +2
-0@@ -114,10 +171,18 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated) mi = (struct multi_instance *) he->value; } } + +/* we not have no existing multi instance for this connection */
Remove "not"
Quoted 15 lines
src/openvpn/mudp.c +7
-1if (!mi) { -if (do_pre_decrypt_check (m)) +struct tls_pre_decrypt_state state = {0 }; + +if (do_pre_decrypt_check (m, &state, real)) { +/* This is an unknown session but with valid tls-auth/tls-crypt (or no auth at all), +* if this is the initial packet of a session, we just send a reply with a HMAC session id and do not +* generate a session slot */ +if (frequency_limit_event_allowed (m->new_connection_limiter)) { mi =multi_create_instance (m, &real);
src/openvpn/mudp.c multi_get_create_instance_udp(struct multi_context *m, bool *floated) · line 195
Quoted 4 lines
src/openvpn/mudp.c +1
-0@@ -127,6 +192,14 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated) mi->did_real_hash = true;multi_assign_peer_id (m, mi); } +/* If we have a session ids already, ensure that the state is using the same */
"id", not "ids"
Quoted 10 lines
src/openvpn/mudp.c +7
-0+if (session_id_defined (&state.server_session_id) + &&session_id_defined ((&state.peer_session_id))) + { + mi->context.c2.tls_multi->n_sessions++; +struct tls_session *session = &mi->context.c2.tls_multi->session[TM_ACTIVE ]; +session_skip_to_pre_start (session, &state, &m->top.c2.from); + } }else {
[...]
Quoted 21 lines
src/openvpn/ssl.h +8
-0@@ -556,4 +556,12 @@ tls_session_generate_data_channel_keys(struct tls_session *session);void load_xkey_provider (void ); +/* Special method to skip the three way handshake RESET stages. This is +* used by the HMAC code when seeing a packet that matches the previous +* HMAC based stateless server state */ +bool +session_skip_to_pre_start (struct tls_session *session, +struct tls_pre_decrypt_state *state, +struct link_socket_actual *from); +#endif /* ifndef OPENVPN_SSL_H */ src/openvpn/ssl_pkt.c +0
-0
[...]
Quoted 4 lines
src/openvpn/ssl_pkt.c +4
-0+/* check adjacent timestamps too */ +for (int offset =-2 ; offset <=1 ; offset++) + { + struct session_id expected_id =
src/openvpn/ssl_pkt.c
Quoted 10 lines
src/openvpn/ssl_pkt.c +10
-0+calculate_session_id_hmac (state->peer_session_id, from, hmac, handwindow, offset); + +if (memcmp_constant_time (&expected_id, &state->server_session_id,SID_SIZE )) + { +return true; + } + } +return false; +} +
new blank line at EOF.
Quoted 4 lines
src/openvpn/ssl_pkt.h +0
-0
[...]
src/openvpn/ssl_pkt.h tls_pre_decrypt_lite(const struct tls_auth_standalone *tas, · line 154
Quoted 10 lines
src/openvpn/ssl_pkt.h +6
-0@@ -141,6 +146,47 @@ tls_pre_decrypt_lite(const struct tls_auth_standalone *tas,const struct link_socket_actual *from, const struct buffer *buf); +/* Creates an SHA256 HMAC context with a random key that is used for the +* session id. +* +* We do not support loading this from a config file since continuing session +* between restarts of OpenVPN has never been supported and that includes +* early session setup
Nitpick: missing full stop.
+
+/**
+ * Calculates the a HMAC based server session id based on a client session id
either "the" or "a" but not both.
+ * and socket addr.
+ *
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH v3] Implement HMAC based session id for tls-crypt v2
In reply to Arne SchwabeStill found a few typos and whitespace issues, but no code issues.
Overall this looks okay to me but I'm hesitant to actually ack it because I'm not sure of the potential implications of all the protocol hacks (i.e. the packet id flags and the TLV payload).
Quoted 1 line
Arne Schwabe <[email protected]...> hat am 02.05.2022 18:09 geschrieben:
[...]
Quoted 8 lines
(quoted diff) +0
-2@@ -58,37 +82,62 @@ do_pre_decrypt_check(struct multi_context *m, struct openvpn_sockaddr *from = &m->top.c2.from.dest; int handwindow = m->top.options.handshake_window; - if (verdict == VERDICT_VALID_RESET_V3) { - /* For tls-crypt-v2 we need to keep the state of the first packet to
Review do_pre_decrypt_check(struct multi_context *m, · line 94
Quoted 10 lines
(quoted diff) +8
-2- * store the unwrapped key */ - return true; + /* Extract the packet id to check if it has the special format that + * indicates early negotiation support */ + struct packet_id_net pin; + struct buffer tmp = m->top.c2.buf; + ASSERT(buf_advance(&tmp, 1 + SID_SIZE)); + ASSERT(packet_id_read(&pin, &tmp, true)); + + /* The most significant byte ist 0x0f if early negotiation is supported */
Your German is showing ;) "is"
[...]
Quoted 4 lines
src/openvpn/ssl_pkt.c +0
-0
[...]
src/openvpn/ssl_pkt.c tls_reset_standalone(struct tls_auth_standalone *tas, · line 446
Quoted 5 lines
src/openvpn/ssl_pkt.c +1
-0@@ -434,8 +443,17 @@ tls_reset_standalone(struct tls_auth_standalone *tas,ASSERT (buf_write (&buf, &net_pid,sizeof (net_pid))); +/* Add indication for tls-crypt-v2 to resend the packet with the with
Redundant "with"
Quoted 1 line
src/openvpn/ssl_pkt.c +1
-0+* reply */
[...]
Quoted 4 lines
src/openvpn/ssl_pkt.h +0
-0
[...]
Quoted 14 lines
src/openvpn/ssl_pkt.h +10
-0@@ -261,4 +272,23 @@ packet_opcode_name(int op)return "P_???" ; } } + +/* initial packet id (instead of 0) that indicates that the peer supports +* early protocol negotiation. This will make the packet id turn a bit faster +* but the network time part of the packet id could take care of that. And +* this is also a rather theoretical scenario as it still needs more than +* 2^31 control channel packets to happen */ +#define EARLY_NEG_MASK 0xff000000 +#define EARLY_NEG_START 0x0a000000 + +#define EARLY_NEG_RESENDWKC 0x00010000
src/openvpn/ssl_pkt.h packet_opcode_name(int op) · line 294
Quoted 10 lines
src/openvpn/ssl_pkt.h +9
-0+ + +/* Early negotiation that part of the server response in the RESET_V2 packet. +* Since clients that announce early negotiation support will treat the payload +* of reset packets special and parse it as TLV messages. +* as TLV (type, length, value) */ +#define EARLY_NEG_TLV_FLAG 0x01 +#define EARLY_NEG_FLAG_RESEND_WKC 0x01#endif /* ifndef SSL_PKT_H */ +
new blank line at EOF.
Quoted 4 lines
tests/unit_tests/openvpn/test_pkt.c +0
-0
[...]
tests/unit_tests/openvpn/test_pkt.c test_generate_reset_packet_tls_auth(void **ut_state) · line 586
Quoted 9 lines
tests/unit_tests/openvpn/test_pkt.c +2
-1@@ -580,11 +580,11 @@ test_generate_reset_packet_tls_auth(void **ut_state)/* Assure repeated generation of reset is deterministic/stateless*/ reset_packet_id_send (&tas_client.tls_wrap.opt.packet_id.send); -struct buffer buf2 =tls_reset_standalone (&tas_client, &client_id, &server_id, header); +struct buffer buf2 =tls_reset_standalone (&tas_client.tls_wrap, &tas_client, &client_id, &server_id, header,false);assert_int_equal (BLEN (&buf),BLEN (&buf2));assert_memory_equal (BPTR (&buf),BPTR (&buf2),BLEN (&buf)); +
Trailing whitespace:
tests/unit_tests/openvpn/test_pkt.c"^I2022-05-03 11:17:02.013292900 +0200$ renamed from tests/unit_tests/openvpn/test_pkt.c"^I2022-05-03 11:17:01.179037300 +0200$ +1 -1
@@ -583,7 +583,7 @@$ struct buffer buf2 = tls_reset_standalone(&tas_client.tls_wrap, &tas_client, &client_id, &server_id, header,false);$ assert_int_equal(BLEN(&buf), BLEN(&buf2));$ assert_memory_equal(BPTR(&buf), BPTR(&buf2), BLEN(&buf));$ - $ +$ free_buf(&buf2);$ free_tls_pre_decrypt_state(&state);$
tests/unit_tests/openvpn/test_pkt.c test_generate_reset_packet_tls_auth(void **ut_state) · line 590
Quoted 5 lines
tests/unit_tests/openvpn/test_pkt.c +0
-1free_buf (&buf2); -free_tls_pre_decrypt_state (&state);packet_id_free (&tas_client.tls_wrap.opt.packet_id);
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 23/28] Optimise three-way handshake condition for S_PRE_START to S_START
In reply to Arne SchwabeChange makes sense to me, only some commit message/comment suggestions.
Still applies on top of 18 v3 + 22 v3 (with git am -3). Compile/UT tested only.
Quoted 7 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
We move to theS_STARTwhen we have finished the three-way handshake. After the three way handshake is done, the client will send the TLS Client Hello packet.Currently we consider the three way handshake only complete if all outgoing packet have been acked (which in this case is the one
"packets"
Quoted 1 line
HARD_RESET_CLIENTorHARD_RESET_SERVER) and also all akcs for incoming
"ACKs"
Quoted 1 line
packets are send out.
"sent" I think?
Quoted 4 lines
The outgoing packet ack is important as it signal that the other side is really responding. The need to send out all ACKs before moving to the next state also breaks piggybacking the ACKs onto the next control packet.
I found this paragraph confusing. Maybe something like:
"Waiting for the ack of our own packet is important as it signals that the other side is really responding. However, the need to also send out all ACKs for packets we received before moving to the next state breaks piggybacking the ACKs onto the next control packet."
Quoted 4 lines
With this change both server and client will only send a
P_CONTROL_V1with the TLS Client Hello and the TLS Server Hello with piggybacked ack instead sending anP_ACK_V1+P_CONTROL_V1, recuding the number of packets in a
"reducing"
Quoted 23 lines
handshake by 2.
This also allows the server to avoid resending
P_CONTROL_HARD_RESET_V2to complete the three-way handshake with HMAC. Only packets with an ACK contain the remote session id that we need for HMAC session id verification. TheACK_V1packet that complets this three-way handshake can get lost. But theP_CONTROL_V1with the piggybacked ACK will get retransmitted. This allows to put the burden of retransmission fully on the client.The S_GOT_KEY/S_SENT_KEY ->
S_ACTIVEis similar. We do not need to wait for the ack packet to be sent to move the state forward. This has however no effect on actual packets since there are normally no outstanding ACKs here.--- src/openvpn/ssl.c | 15 +++++---------- tests/unit_tests/openvpn/test_pkt.c | 8 ++++---- 2 files changed, 9 insertions(+), 14 deletions(-)src/openvpn/ssl.c +0
-0
[...]
src/openvpn/ssl.c tls_process_state(struct tls_multi *multi, · line 2644
Quoted 9 lines
src/openvpn/ssl.c +3
-2@@ -2646,8 +2639,10 @@ tls_process_state(struct tls_multi *multi, goto error; } -/* Wait for Initial Handshake ACK */ -if (ks->state ==S_PRE_START &&no_pending_reliable_packets (ks)) +/* Check if the initial three-way Handshake is complete. +* This handshake can be considered to be complete when our own +* initial packet can been successfully acked. */
"has been successfully acked" ?
src/openvpn/ssl.c tls_process_state(struct tls_multi *multi, · line 2648
Quoted 4 lines
src/openvpn/ssl.c +1
-0+if (ks->state ==S_PRE_START &&reliable_empty (ks->send_reliable)) { ks->state =S_START ; state_change = true;
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 24/28] Extract read_incoming_tls_plaintext into its own function
In reply to Arne SchwabeNACK, compile error.
Quoted 4 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
This makes the
tls_process_statefunction a bit easier to read allows
missing "and" after read?
Quoted 18 lines
extending the
read_incoming_tls_plaintextfunction later without makingtls_process_stateeven longer.--- src/openvpn/ssl.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-)src/openvpn/ssl.c +4
-0@@ -2612,6 +2612,28 @@ control_packet_needs_wkc(const struct key_state *ks) && (ks->send_reliable->packet_id ==1 ); } +static bool +read_incoming_tls_plaintext (struct buffer *buf,struct key_state *ks,interval_t *wakeup) +{ +ASSERT (buf_init (buf,0 ));
src/openvpn/ssl.c control_packet_needs_wkc(const struct key_state *ks) · line 2628
Quoted 10 lines
src/openvpn/ssl.c +10
-0+int status =key_state_read_plaintext (&ks->ks_ssl, buf,TLS_CHANNEL_BUF_SIZE ); +update_time (); +if (status ==-1 ) + { +msg (D_TLS_ERRORS ,"TLS Error: TLS object -> incoming plaintext read error" ); +return false; + } +if (status ==1 ) + { + *state_change = true;
ssl.c: In function ‘read_incoming_tls_plaintext’:
ssl.c:2628:10: error: ‘state_change’ undeclared (first use in this function); did you mean ‘state_name’?
2628 | *state_change = true;
| ^~~~~~~~~~~~
| state_namesrc/openvpn/ssl.c control_packet_needs_wkc(const struct key_state *ks) · line 2639
Quoted 11 lines
src/openvpn/ssl.c +8
-0+dmsg (D_TLS_DEBUG ,"TLS -> Incoming Plaintext" ); + +/* More data may be available, wake up again asap to check. */ + *wakeup =0 ; + } +return true; +} + static bool tls_process_state(struct tls_multi *multi,
Regards,
--
Frank Lichtenheld
PATCH v2 1/1 Extract read_incoming_tls_plaintext into its own function
In reply to Arne SchwabeThis makes the tls_process_state function a bit easier to read allows extending the read_incoming_tls_plaintext function later without making tls_process_state even longer.
Patch v2: fix compile error.
--- src/openvpn/ssl.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-)
src/openvpn/ssl.c +27 -15
@@ -2613,6 +2613,32 @@ control_packet_needs_wkc(const struct key_state *ks) } +static bool +read_incoming_tls_plaintext (struct key_state *ks,struct buffer *buf, +interval_t *wakeup,bool *state_change) +{ +ASSERT (buf_init (buf,0 )); + +int status =key_state_read_plaintext (&ks->ks_ssl, buf,TLS_CHANNEL_BUF_SIZE ); + +update_time (); +if (status ==-1 ) + { +msg (D_TLS_ERRORS ,"TLS Error: TLS object -> incoming plaintext read error" ); +return false; + } +if (status ==1 ) + { + *state_change = true; +dmsg (D_TLS_DEBUG ,"TLS -> Incoming Plaintext" ); + +/* More data may be available, wake up again asap to check. */ + *wakeup =0 ; + } +return true; +} + + static bool tls_process_state(struct tls_multi *multi, struct tls_session *session,
@@ -2705,24 +2731,10 @@ tls_process_state(struct tls_multi *multi,struct buffer *buf = &ks->plaintext_read_buf;if (!buf->len) { -int status; - -ASSERT (buf_init (buf,0 )); - status =key_state_read_plaintext (&ks->ks_ssl, buf,TLS_CHANNEL_BUF_SIZE ); -update_time (); -if (status ==-1 ) +if (!read_incoming_tls_plaintext (ks, buf, wakeup, &state_change)) { -msg (D_TLS_ERRORS ,"TLS Error: TLS object -> incoming plaintext read error" ); goto error; } -if (status ==1 ) - { - state_change = true; -dmsg (D_TLS_DEBUG ,"TLS -> Incoming Plaintext" ); - -/* More data may be available, wake up again asap to check. */ - *wakeup =0 ; - } }/* Send Key */
Signature · 1 line
2.32.0 (Apple Git-132)
Re: [Openvpn-devel] [PATCH v2] Extract read_incoming_tls_plaintext into its own function
Trivial code move. Compiles now.
Quoted 4 lines
Arne Schwabe <[email protected]...> hat am 03.05.2022 13:29 geschrieben:
This makes the
tls_process_statefunction a bit easier to read allows
I still think there is an "and" missing after "read"
Quoted 4 lines
extending the
read_incoming_tls_plaintextfunction later without makingtls_process_stateeven longer.Patch v2: fix compile error.
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH v3] Implement HMAC based session id for tls-crypt v2
In reply to Arne SchwabeHi,
Quoted 96 lines · Arne Schwabe
Tls-crypt v2 is more complicated to implement a proper stateless handshake. To allow state handshake this commit does
- introduce a new packet
CONTROL_WKC_V1that repeats the wrapped client key.
- introduce a way to negotiate the support for this packet in the three way handshakeDetails about the protocol changes are in tls-crypt-v2.txt. Optional arguments to the tls-crypt-v2 option have been added to explicitly allow or disallow client that do not support the stateless handshake.
Signed-off-by Arne Schwabe <[email protected]...>Patch v3: improve grammar, style, comments, fix unit tests
--- Changes.rst | 10 +++ doc/man-sections/tls-options.rst | 14 ++++ doc/tls-crypt-v2.txt | 41 +++++++++ src/openvpn/crypto.h | 8 ++ src/openvpn/init.c | 4 + src/openvpn/mudp.c | 85 ++++++++++++++----- src/openvpn/options.c | 13 +++ src/openvpn/options.h | 3 + src/openvpn/reliable.h | 2 - src/openvpn/ssl.c | 125 +++++++++++++++++++++++++--- src/openvpn/ssl_pkt.c | 28 +++++-- src/openvpn/ssl_pkt.h | 36 +++++++- tests/unit_tests/openvpn/test_pkt.c | 10 +-- 13 files changed, 334 insertions(+), 45 deletions(-)Changes.rst +10
-0@@ -69,6 +69,16 @@ Improved ``--mssfix`` and ``--fragment`` calculation account and the resulting size is specified as the total size of the VPN packets including IP and UDP headers. +Cookie based handshake for UDP server + Instead of allocating a connection for each client on the initial packet + OpenVPN server will now use an HMAC based cookie as its session id. This + way the server can verify it on completing the handshake without keeping + state. This eliminates the amplification and resource exhaustion attacks. + For tls-crypt-v2 clients, this requires OpenVPN 2.6 clients or later + because the client needs to resend its client key on completing the hand + shake. The tls-crypt-v2 option allows controlling if older clients are + accepted. + Deprecated features ------------------- ``inetd`` has been removeddoc/man-sections/tls-options.rst +14
-0@@ -486,6 +486,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa 8000 years'. --tls-crypt-v2 keyfile + + Valid syntax: + :: + tls-crypt-v2 keyfile + tls-crypt-v2 keyfile force-cookie + tls-crypt-v2 keyfile allow-noncookie + Use client-specific tls-crypt keys. For clients, ``keyfile`` is a client-specific tls-crypt key. Such a key@@ -501,6 +508,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa client is using client-specific keys, and automatically select the right mode. + The optional parameters :code:`force-cookie` allows only tls-crypt-v2 + clients that support a cookie based stateless three way handshake that + avoids replay attacks and state exhaustion on the server side (OpenVPN + 2.6 and later). The option :code:`allow-noncookie` explicitly allows + older tls-crypt-v2 clients. The default is (currently) + :code:`allow-noncookie`. + --tls-crypt-v2-verify cmd Run command ``cmd`` to verify the metadata of the client-specific tls-crypt-v2 key of a connecting client. This allows serverdoc/tls-crypt-v2.txt +4
-0@@ -157,6 +157,47 @@ When setting up the openvpn connection: messages. +HMAC Cookie support +------------------- +To avoid exhaustion attack and keeping state for connections that fail to +complete the three-way handshake, the OpenVPN server will use its own session
doc/tls-crypt-v2.txt When setting up the openvpn connection: · line 173
Quoted 10 lines
doc/tls-crypt-v2.txt +10
-0+id as challenge that the client must repeat in the third packet of the +handshake. This introduces a problem. If the server does not keep the wrapped +client key from the initial packet, the server cannot decode the third packet. +Therefore, tls-crypt-v2 in 2.6 allows resending the wrapped key in the third +packet of the handshake with the P_CONTROL_WKC_V1 message. The modified +handshake is as follows (the rest of the handshake is unmodified): + +1. The client creates the P_CONTROL_HARD_RESET_CLIENT_V3 message as before + but indicates that it supports resending the wrapped key. This is done + by setting the packet id of the replay id to 0x0f010000. The first byte
Here you say that the replay id starts with 0x0f, but later you set EARLY_NEG_START to 0x0a.... Either the doc is wrong or the value of that constant should be changed.
Quoted 110 lines
doc/tls-crypt-v2.txt +27
-0+ indicates the early negotiation support and the next byte the flags. + +2. The server responds with a P_CONTROL_HARD_RESET_V2 message. Instead of having + an empty payload like normally, the payload consists of TLV (type (uint16), + length (uint16), value) packets. TLV was chosen + to allow extensibility in the future. Currently only the following TLV is + defined: + + flags - type 0x01, length 2. + + Bit 1 indicates that the client needs to resend the WKc in the third packet. + +3. Instead of normal P_ACK_V1 or P_CONTROL_V1 packet, the client will send a + P_CONTROL_WKC_V1 packet. The P_CONTROL_WKC_V1 is identical to a normal + P_CONTROL_V1 packet but with the WKc appended. + + Normally the first message of the client is either P_ACK_V1, directly + followed by a P_CONTROL_V1 message that contains the TLS Client Hello or + just a P_CONTROL_V1 message. Instead of a P_ACK_V1 message the client should + send a P_CONTROL_WKC_V1 message with an empty payload. This message must + also include an ACK for the P_CONTROL_HARD_RESET_V2 message. + + When directly sending the TLS Client Hello message in the P_CONTROL_WKC_V1 + message, the client must ensure that the resulting P_CONTROL_WKC_V1 message + with the appended WKc does not extend the control message length. + + Considerations --------------src/openvpn/crypto.h +8
-0@@ -256,6 +256,14 @@ struct crypto_options/**< Bit-flag indicating that data channel key derivation * is done using TLS keying material export [RFC5705] */ +#define CO_RESEND_WKC (1<<4) +/**< Bit-flag indicating that the client is expected to +* resend the wrapped client key with the 2nd packet (packet-id 1) +* like with the HARD_RESET_CLIENT_V3 packet */ +#define CO_FORCE_TLSCRYPTV2_COOKIE (1<<5) +/**< Bit-flag indicating that we do not allow clients that do +* not support resending the wrapped client key (WKc) with the +* third packet of the three-way handshake */ unsigned int flags;/**< Bit-flags determining behavior of * security operation functions. */ };src/openvpn/init.c +4
-0@@ -2960,6 +2960,10 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) { to.tls_wrap.tls_crypt_v2_server_key = c->c1.ks.tls_crypt_v2_server_key; to.tls_crypt_v2_verify_script = c->options.tls_crypt_v2_verify_script; +if (options->ce.tls_crypt_v2_force_cookie) + { + to.tls_wrap.opt.flags |=CO_FORCE_TLSCRYPTV2_COOKIE ; + } } }src/openvpn/mudp.c +25
-4@@ -40,6 +40,30 @@#include <sys/inotify.h> #endif +static void +send_hmac_reset_packet (struct multi_context *m, +struct tls_pre_decrypt_state *state, +struct tls_auth_standalone *tas, +struct session_id *sid, +bool tlscryptv2) +{ +reset_packet_id_send (&state->tls_wrap_tmp.opt.packet_id.send); + state->tls_wrap_tmp.opt.packet_id.rec.initialized = true; +uint8_t header =0 | (P_CONTROL_HARD_RESET_SERVER_V2 <<P_OPCODE_SHIFT ); +struct buffer buf =tls_reset_standalone (&state->tls_wrap_tmp, tas, sid, + &state->peer_session_id, header, + tlscryptv2); + +struct context *c = &m->top; + +buf_reset_len (&c->c2.buffers->aux_buf); +buf_copy (&c->c2.buffers->aux_buf, &buf); + m->hmac_reply = c->c2.buffers->aux_buf; + m->hmac_reply_dest = &m->top.c2.from; +msg (D_MULTI_DEBUG ,"Reset packet from client, sending HMAC based reset challenge" ); +} + +/* Return if this packet should create a new session */ static bool do_pre_decrypt_check(struct multi_context *m,@@ -58,37 +82,62 @@ do_pre_decrypt_check(struct multi_context *m,struct openvpn_sockaddr *from = &m->top.c2.from.dest;int handwindow = m->top.options.handshake_window; -if (verdict ==VERDICT_VALID_RESET_V3 ) { -/* For tls-crypt-v2 we need to keep the state of the first packet to -* store the unwrapped key */ -return true; +/* Extract the packet id to check if it has the special format that
src/openvpn/mudp.c do_pre_decrypt_check(struct multi_context *m, · line 97
Quoted 10 lines
src/openvpn/mudp.c +10
-0+* indicates early negotiation support */ +struct packet_id_net pin; +struct buffer tmp = m->top.c2.buf; +ASSERT (buf_advance (&tmp,1 +SID_SIZE )); +ASSERT (packet_id_read (&pin, &tmp, true)); + +/* The most significant byte ist 0x0f if early negotiation is supported */ +bool early_neg_support = (pin.id &EARLY_NEG_MASK ) ==EARLY_NEG_START ; + +if (early_neg_support && (pin.id &EARLY_NEG_RESENDWKC ))
EARLY_NEG_RESENDWKC is always set when EARLY_NEG_START is set too - the two features basically always exist at the same time. I suggest to remove EARLY_NEG_RESENDWKC and just rely on EARLY_NEG_START. A client sending the latter will always support the former. And this is specific to tls-crypt-v2 anyway.
src/openvpn/mudp.c do_pre_decrypt_check(struct multi_context *m, · line 110
Quoted 13 lines
src/openvpn/mudp.c +13
-0+ { +/* Calculate the session ID HMAC for our reply and create reset packet */ +struct session_id sid =calculate_session_id_hmac (state->peer_session_id, + from, hmac, handwindow,0 ); +send_hmac_reset_packet (m, state, tas, &sid, true); + +return false; + } + else + { +/* For tls-crypt-v2 we need to keep the state of the first packet +* to store the unwrapped key if the client doesn't support resending +* the wrapped key */
to make this comment more comprehensive of what follows I'd add: ", unless the user specifically disallowed such clients"
Quoted 158 lines
src/openvpn/mudp.c +18
-15+if (tas->tls_wrap.opt.flags &CO_FORCE_TLSCRYPTV2_COOKIE ) + { +struct gc_arena gc =gc_new (); +const char *peer =print_link_socket_actual (&m->top.c2.from, &gc); +msg (D_MULTI_DEBUG ,"tls-crypt-v2 force-cookie is enabled," +"ignoring connection attempt from old client" +" (%s)" , peer); +gc_free (&gc); +return false; + } +else + { +return true; + } + } }else if (verdict ==VERDICT_VALID_RESET_V2 ) {/* Calculate the session ID HMAC for our reply and create reset packet */ struct session_id sid =calculate_session_id_hmac (state->peer_session_id, from, hmac, handwindow,0 ); -reset_packet_id_send (&tas->tls_wrap.opt.packet_id.send); - tas->tls_wrap.opt.packet_id.rec.initialized = true; -uint8_t header =0 | (P_CONTROL_HARD_RESET_SERVER_V2 <<P_OPCODE_SHIFT ); -struct buffer buf =tls_reset_standalone (tas, &sid, - &state->peer_session_id, header); - -struct context *c = &m->top; +send_hmac_reset_packet (m, state, tas, &sid, false); -buf_reset_len (&c->c2.buffers->aux_buf); -buf_copy (&c->c2.buffers->aux_buf, &buf); - m->hmac_reply = c->c2.buffers->aux_buf; - m->hmac_reply_dest = &m->top.c2.from; -msg (D_MULTI_DEBUG ,"Reset packet from client, sending HMAC based reset challenge" );/* We have a reply do not create a new session */ return false; } -else if (verdict ==VERDICT_VALID_CONTROL_V1 || verdict ==VERDICT_VALID_ACK_V1 ) +else if (verdict ==VERDICT_VALID_CONTROL_V1 || verdict ==VERDICT_VALID_ACK_V1 + || verdict ==VERDICT_VALID_WKC_V1 ) {/* ACK_V1 contains the peer id (our id) while CONTROL_V1 can but does not * need to contain the peer id */ @@ -99,9 +148,7 @@ do_pre_decrypt_check(struct multi_context *m,const char *peer =print_link_socket_actual (&m->top.c2.from, &gc);if (!ret) { -msg (D_MULTI_MEDIUM ,"Packet with invalid or missing SID from %s" , peer); - }else {src/openvpn/options.c +13
-0@@ -8896,6 +8896,19 @@ add_option(struct options *options, options->ce.tls_crypt_v2_file = p[1 ]; options->ce.tls_crypt_v2_file_inline = is_inline; } + +if (p[2 ] &&streq (p[2 ],"force-cookie" )) + { + options->ce.tls_crypt_v2_force_cookie = true; + } +else if (p[2 ] &&streq (p[2 ],"allow-noncookie" )) + { + options->ce.tls_crypt_v2_force_cookie = false; + } +else if (p[2 ]) + { +msg (msglevel,"Unsupported tls-crypt-v2 argument: %s" , p[2 ]); + } }else if (streq (p[0 ],"tls-crypt-v2-verify" ) && p[1 ] && !p[2 ]) {src/openvpn/options.h +3
-0@@ -162,6 +162,9 @@ struct connection_entry* authenticated encryption v2 */ const char *tls_crypt_v2_file;bool tls_crypt_v2_file_inline; + +/* Allow only client that support resending the wrapped client key */ +bool tls_crypt_v2_force_cookie; };struct remote_entry src/openvpn/reliable.h +0
-2@@ -361,8 +361,6 @@ struct reliable_entry *reliable_get_entry_sequenced(struct reliable *rel);* * @param rel The reliable structure associated with the given buffer. * @param buf The buffer of the reliable entry which is to be removed. -* @param inc_pid If true, the reliable structure's packet ID counter -* will be incremented. */ void reliable_mark_deleted (struct reliable *rel,struct buffer *buf);src/openvpn/ssl.c +36
-0@@ -1099,6 +1099,14 @@ tls_session_init(struct tls_multi *multi, struct tls_session *session) session->opt->replay_time, "TLS_WRAP", session->key_id); +/* If we are using tls-crypt-v2 we manipulate the packet id to be (ab)used +* to indicate early protocol negotiation */ +if (session->opt->tls_crypt_v2) + { + session->tls_wrap.opt.packet_id.send.time = now; + session->tls_wrap.opt.packet_id.send.id =EARLY_NEG_START |EARLY_NEG_RESENDWKC ; + } +/* load most recent packet-id to replay protect on --tls-auth */ packet_id_persist_load_obj (session->tls_wrap.opt.pid_persist, &session->tls_wrap.opt.packet_id);@@ -2525,6 +2533,53 @@ session_skip_to_pre_start(struct tls_session *session,return session_move_pre_start (session, ks, true); } +/** +* Parses the TLVs (type, length, value) in the early negotiation +*/ +static bool +parse_early_negotiation_tlvs (struct buffer *buf,struct key_state *ks) +{ +while (buf->len >0 ) + { +if (buf_len (buf) <4 ) + { + goto error; + } +/* read type */ +uint16_t type =buf_read_u16 (buf); +uint16_t len =buf_read_u16 (buf); +if (buf_len (buf) < len) + { + goto error; + } + +if (type ==EARLY_NEG_TLV_FLAG ) + { +if (len !=sizeof (uint16_t )) + { + goto error; + } +uint16_t flags =buf_read_u16 (buf); +
src/openvpn/ssl.c session_skip_to_pre_start(struct tls_session *session, · line 2573
Quoted 10 lines
src/openvpn/ssl.c +10
-0+if (flags &EARLY_NEG_FLAG_RESEND_WKC ) + { + ks->crypto_options.flags |=CO_RESEND_WKC ; + } + } +else + { +/* Skip types we do not parse */ +buf_advance (buf, len); + }
IMHO a switch/case would be more elegant..but we can do that when we'll add more TLV types.
Quoted 65 lines
src/openvpn/ssl.c +36
-3+ } +reliable_mark_deleted (ks->rec_reliable, buf); + +return true; +error: +msg (D_TLS_ERRORS ,"TLS Error: Early negotiation malformed packet" ); +return false; +} +/** * Read incoming ciphertext and passes it to the buffer of the SSL library. * Returns false if an error is encountered that should abort the session. @@ -2557,6 +2612,13 @@ read_incoming_tls_ciphertext(struct buffer *buf, struct key_state *ks,return true; } +static bool +control_packet_needs_wkc (const struct key_state *ks) +{ +return (ks->crypto_options.flags &CO_RESEND_WKC ) + && (ks->send_reliable->packet_id ==1 ); +} + static bool tls_process_state(struct tls_multi *multi,@@ -2626,9 +2688,21 @@ tls_process_state(struct tls_multi *multi,struct reliable_entry *entry =reliable_get_entry_sequenced (ks->rec_reliable);if (entry) { -if (!read_incoming_tls_ciphertext (&entry->buf, ks, &state_change)) +/* The first packet from the peer (the reset packet) is special and +* contains early protocol negotiation */ +if (entry->packet_id ==0 &&is_hard_reset_method2 (entry->opcode)) { - goto error; +if (!parse_early_negotiation_tlvs (&entry->buf, ks)) + { + goto error; + } + } +else + { +if (!read_incoming_tls_ciphertext (&entry->buf, ks, &state_change)) + { + goto error; + } } }@@ -2721,7 +2795,12 @@ tls_process_state(struct tls_multi *multi, }if (status ==1 ) { -reliable_mark_active_outgoing (ks->send_reliable, buf,P_CONTROL_V1 ); +int opcode =P_CONTROL_V1 ; +if (control_packet_needs_wkc (ks)) + { + opcode =P_CONTROL_WKC_V1 ; + } +reliable_mark_active_outgoing (ks->send_reliable, buf, opcode);INCR_GENERATED ; state_change = true;dmsg (D_TLS_DEBUG ,"Outgoing Ciphertext -> Reliable" );
src/openvpn/ssl.c tls_process(struct tls_multi *multi, · line 2899
Quoted 10 lines
src/openvpn/ssl.c +7
-0@@ -2811,15 +2890,38 @@ tls_process(struct tls_multi *multi,update_time (); +/* We often send acks back to back to a following control packet. This +* normally does not create a problem (apart from an extra packet. However, +* with the P_CONTROL_WKC_V1 we need to ensure that the packet gets resend +* if not received by remote, so instead we use an empty control packet in +* this special case */ + +
one empty line should be enough
Quoted 98 lines
src/openvpn/ssl.c +25
-8/* Send 1 or more ACKs (each received control packet gets one ACK) */ if (!to_link->len && !reliable_ack_empty (ks->rec_ack)) { -struct buffer buf = ks->ack_write_buf; -ASSERT (buf_init (&buf, multi->opt.frame.buf.headroom)); -write_control_auth (session, ks, &buf, to_link_addr,P_ACK_V1 , -RELIABLE_ACK_SIZE , false); - *to_link = buf; -dmsg (D_TLS_DEBUG ,"Dedicated ACK -> TCP/UDP" ); +if (control_packet_needs_wkc (ks)) + { +struct buffer *buf =reliable_get_buf_output_sequenced (ks->send_reliable); +if (!buf) + { +return false; + } + +/* We do not write anything to the buffer, this way this will be +* an empty control packet that gets the ack piggybacked and +* also appended the wrapped client key since it has a WCK opcode */ +reliable_mark_active_outgoing (ks->send_reliable, buf,P_CONTROL_WKC_V1 ); + } +else + { +struct buffer buf = ks->ack_write_buf; +ASSERT (buf_init (&buf, multi->opt.frame.buf.headroom)); +write_control_auth (session, ks, &buf, to_link_addr,P_ACK_V1 , +RELIABLE_ACK_SIZE , false); + *to_link = buf; +dmsg (D_TLS_DEBUG ,"Dedicated ACK -> TCP/UDP" ); + } }/* When should we wake up again? */ @@ -3464,7 +3566,8 @@ tls_pre_decrypt(struct tls_multi *multi, }/* -* We have an authenticated control channel packet (if --tls-auth was set). +* We have an authenticated control channel packet (if --tls-auth/tls-crypt +* or tls-crypt-v2 was set). * Now pass to our reliability layer which deals with * packet acknowledgements, retransmits, sequencing, etc. */ @@ -3893,7 +3996,7 @@ protocol_dump(struct buffer *buffer, unsigned int flags, struct gc_arena *gc)if (op ==P_ACK_V1 ) { - goto done; + goto print_data; }/* src/openvpn/ssl_pkt.c +9
-2@@ -148,7 +148,8 @@ tls_wrap_control(struct tls_wrap_ctx *ctx, uint8_t header, struct buffer *buf,return ; } -if ((header >>P_OPCODE_SHIFT ) ==P_CONTROL_HARD_RESET_CLIENT_V3 ) +if ((header >>P_OPCODE_SHIFT ) ==P_CONTROL_HARD_RESET_CLIENT_V3 + || (header >>P_OPCODE_SHIFT ) ==P_CONTROL_WKC_V1 ) {if (!buf_copy (&ctx->work, ctx->tls_crypt_v2_wkc))@@ -197,7 +198,8 @@ read_control_auth(struct buffer *buf,bool ret = false;const uint8_t opcode = *(BPTR (buf)) >>P_OPCODE_SHIFT ; -if (opcode ==P_CONTROL_HARD_RESET_CLIENT_V3 +if ((opcode ==P_CONTROL_HARD_RESET_CLIENT_V3 + || opcode ==P_CONTROL_WKC_V1 ) && !tls_crypt_v2_extract_client_key (buf, ctx, opt)) { msg(D_TLS_ERRORS,@@ -321,6 +323,7 @@ tls_pre_decrypt_lite(const struct tls_auth_standalone *tas,if (op !=P_CONTROL_HARD_RESET_CLIENT_V2 && op !=P_CONTROL_HARD_RESET_CLIENT_V3 && op !=P_CONTROL_V1 + && op !=P_CONTROL_WKC_V1 && op !=P_ACK_V1 ) {/* @@ -397,6 +400,10 @@ tls_pre_decrypt_lite(const struct tls_auth_standalone *tas, {return VERDICT_VALID_RESET_V3 ; } + else if (op == P_CONTROL_WKC_V1) + { +return VERDICT_VALID_WKC_V1 ; + } else {return VERDICT_VALID_RESET_V2 ;@@ -410,10 +417,12 @@ error:
src/openvpn/ssl_pkt.c error: · line 425
Quoted 10 lines
src/openvpn/ssl_pkt.c +4
-2struct buffer -tls_reset_standalone (struct tls_auth_standalone *tas, +tls_reset_standalone (struct tls_wrap_ctx *ctx, +struct tls_auth_standalone *tas,struct session_id *own_sid,struct session_id *remote_sid, -uint8_t header) +uint8_t header, +bool tlscryptv2)
I know this bool is used only by tlscryptv2, but what this function does is kinda unrelated to having tlscryptv2 or not. Should we rather call it "bool early_neg_resend_wkc" or "bool request_resend_wkc"?
Quoted 89 lines
src/openvpn/ssl_pkt.c +10
-1{struct buffer buf =alloc_buf (tas->frame.buf.payload_size);ASSERT (buf_init (&buf, tas->frame.buf.headroom));@@ -434,8 +443,17 @@ tls_reset_standalone(struct tls_auth_standalone *tas,ASSERT (buf_write (&buf, &net_pid,sizeof (net_pid))); +/* Add indication for tls-crypt-v2 to resend the packet with the with +* reply */ +if (tlscryptv2) + { +buf_write_u16 (&buf,EARLY_NEG_TLV_FLAG );/* TYPE: flags */ +buf_write_u16 (&buf,sizeof (uint16_t )); +buf_write_u16 (&buf,EARLY_NEG_FLAG_RESEND_WKC ); + } +/* Add tls-auth/tls-crypt wrapping, this might replace buf */ -tls_wrap_control (&tas->tls_wrap, header, &buf, own_sid); +tls_wrap_control (ctx, header, &buf, own_sid);return buf; }src/openvpn/ssl_pkt.h +22
-3@@ -54,11 +54,15 @@/* indicates key_method >= 2 and client-specific tls-crypt key */ #define P_CONTROL_HARD_RESET_CLIENT_V3 10/* initial key from client, forget previous state */ +/* Variant of P_CONTROL_V1 but with appended wrapped key +* like P_CONTROL_HARD_RESET_CLIENT_V3 */ +#define P_CONTROL_WKC_V1 11 +/* define the range of legal opcodes * Since we do no longer support key-method 1 we consider * the v1 op codes invalid */ #define P_FIRST_OPCODE 3 -#define P_LAST_OPCODE 10 +#define P_LAST_OPCODE 11/* * Define number of buffers for send and receive in the reliability layer. @@ -86,6 +90,8 @@ enum first_packet_verdict {/** This packet is a valid ACK control packet from the peer, * i.e. it has a valid session id hmac in it */ VERDICT_VALID_ACK_V1 , +/** The packet is a valid control packet with appended wrapped client key */ +VERDICT_VALID_WKC_V1 ,/** the packet failed on of the various checks */ VERDICT_INVALID };@@ -217,10 +223,12 @@ read_control_auth(struct buffer *buf,* The returned buf needs to be free with \c free_buf */ struct buffer -tls_reset_standalone (struct tls_auth_standalone *tas, +tls_reset_standalone (struct tls_wrap_ctx *ctx, +struct tls_auth_standalone *tas,struct session_id *own_sid,struct session_id *remote_sid, -uint8_t header); +uint8_t header, +bool tlscryptv2);static inline const char *packet_opcode_name (int op)@@ -248,6 +256,9 @@ packet_opcode_name(int op)case P_CONTROL_V1 :return "P_CONTROL_V1" ; +case P_CONTROL_WKC_V1 : +return "P_CONTROL_WKC_V1" ; +case P_ACK_V1 :return "P_ACK_V1" ;@@ -261,4 +272,23 @@ packet_opcode_name(int op)return "P_???" ; } } + +/* initial packet id (instead of 0) that indicates that the peer supports +* early protocol negotiation. This will make the packet id turn a bit faster +* but the network time part of the packet id could take care of that. And +* this is also a rather theoretical scenario as it still needs more than +* 2^31 control channel packets to happen */ +#define EARLY_NEG_MASK 0xff000000 +#define EARLY_NEG_START 0x0a000000
src/openvpn/ssl_pkt.h packet_opcode_name(int op) · line 292
Quoted 10 lines
src/openvpn/ssl_pkt.h +10
-0+ +#define EARLY_NEG_RESENDWKC 0x00010000 + + +/* Early negotiation that part of the server response in the RESET_V2 packet. +* Since clients that announce early negotiation support will treat the payload +* of reset packets special and parse it as TLV messages. +* as TLV (type, length, value) */ +#define EARLY_NEG_TLV_FLAG 0x01 +#define EARLY_NEG_FLAG_RESEND_WKC 0x01
The two constants above are used to fill uint16_t fields, therefore I suggest to represent the full form, for clarity. I.e. 0x0001
Moreover, these constants belong to the TLV space and we may add more in the future, therefore I suggest changing the name format to something like "TLV_TYPE_*" and "TLV_VALUE_*". So they'll become:
TLV_TYPE_EARLY_NEG_FLAGTLV_VALUE_EARLY_NEG_FLAG_RESEND_WKC
In the fuure we will have more TLV_TYPE_* and TLV_VALUE_* and so they'll be properly namescope'd.
src/openvpn/ssl_pkt.h packet_opcode_name(int op) · line 294
Quoted 2 lines
src/openvpn/ssl_pkt.h +1
-0#endif /* ifndef SSL_PKT_H */ +
Spurious empty line at the end of the file
Quoted 32 lines
tests/unit_tests/openvpn/test_pkt.c +3
-3@@ -536,7 +536,7 @@ test_generate_reset_packet_plain(void **ut_state)uint8_t header =0 | (P_CONTROL_HARD_RESET_CLIENT_V2 <<P_OPCODE_SHIFT ); -struct buffer buf =tls_reset_standalone (&tas, &client_id, &server_id, header); +struct buffer buf =tls_reset_standalone (&tas.tls_wrap, &tas, &client_id, &server_id, header, false); verdict =tls_pre_decrypt_lite (&tas, &state, &from, &buf);@@ -544,7 +544,7 @@ test_generate_reset_packet_plain(void **ut_state)/* Assure repeated generation of reset is deterministic/stateless*/ assert_memory_equal (state.peer_session_id.id, client_id.id,SID_SIZE ); -struct buffer buf2 =tls_reset_standalone (&tas, &client_id, &server_id, header); +struct buffer buf2 =tls_reset_standalone (&tas.tls_wrap, &tas, &client_id, &server_id, header, false);assert_int_equal (BLEN (&buf),BLEN (&buf2));assert_memory_equal (BPTR (&buf),BPTR (&buf2),BLEN (&buf));free_buf (&buf2);@@ -571,7 +571,7 @@ test_generate_reset_packet_tls_auth(void **ut_state) now =0x22446688 ;reset_packet_id_send (&tas_client.tls_wrap.opt.packet_id.send); -struct buffer buf =tls_reset_standalone (&tas_client, &client_id, &server_id, header); +struct buffer buf =tls_reset_standalone (&tas_client.tls_wrap, &tas_client, &client_id, &server_id, header, false);enum first_packet_verdict verdict =tls_pre_decrypt_lite (&tas_server, &state, &from, &buf);assert_int_equal (verdict,VERDICT_VALID_RESET_V2 );
tests/unit_tests/openvpn/test_pkt.c test_generate_reset_packet_tls_auth(void **ut_state) · line 587
Quoted 10 lines
tests/unit_tests/openvpn/test_pkt.c +2
-2@@ -580,11 +580,11 @@ test_generate_reset_packet_tls_auth(void **ut_state)/* Assure repeated generation of reset is deterministic/stateless*/ reset_packet_id_send (&tas_client.tls_wrap.opt.packet_id.send); -struct buffer buf2 =tls_reset_standalone (&tas_client, &client_id, &server_id, header); +struct buffer buf2 =tls_reset_standalone (&tas_client.tls_wrap, &tas_client, &client_id, &server_id, header,false);assert_int_equal (BLEN (&buf),BLEN (&buf2));assert_memory_equal (BPTR (&buf),BPTR (&buf2),BLEN (&buf)); +free_buf (&buf2); -
Not sure why you are changing this in this patch? Seems irrelevant, but if you really want to, please remove the empty spaces in the new empty line.
tests/unit_tests/openvpn/test_pkt.c test_generate_reset_packet_tls_auth(void **ut_state) · line 590
Quoted 3 lines
tests/unit_tests/openvpn/test_pkt.c +0
-0free_tls_pre_decrypt_state (&state);packet_id_free (&tas_client.tls_wrap.opt.packet_id);
Rest looks good and the TLV is a very nice thing to have!
Regards,
Signature · 1 line
Antonio Quartulli
[Openvpn-devel] [PATCH applied] Re: Implement stateless HMAC-based sesssion-id three-way-handshake
In reply to Arne SchwabeI have not reviewied this in full depth, trusting Frank here.
I *have* tested this on the server side testbed with "plain", "tls-auth" and "tls-crypt" instances - and besides the fact that tls-crypt breaks the 2.3 client (no support ;-) ) it all works nicely.
All the comments and whitespace bits have been fixed-on-the-fly as instructed. I have also fixed the "Return *true* if this packet..." comment before do_pre_decrypt_check() and rewrapped the very long comment lines in mudp.c (why did uncrustify not find this?).
I have stared a bit at the code and I think I understand what the code does, and it seems to make sense. Plus, it has unit tests :-)
Your patch has been applied to the master branch.
commit b364711486dc6371ad2659a5aa190941136f4f04
Author: Arne Schwabe Date: Mon May 2 17:43:10 2022 +0200
Implement stateless HMAC-based sesssion-id three-way-handshake
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24262.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Re: [Openvpn-devel] [PATCH applied] Re: Implement stateless HMAC-based sesssion-id three-way-handshake
Hi,
Quoted 4 lines · Gert Doering
All the comments and whitespace bits have been fixed-on-the-fly as instructed. I have also fixed the "Return *true* if this packet..." comment before
do_pre_decrypt_check()and rewrapped the very long comment lines inmudp.c(why did uncrustify not find this?).
I should not have done that, because my tree wasn't set up properly with pre-commit-check yet - so I introduced new whitespace errors.
These have been fixed in a followup commit, which (it is only whitespace) I'm not running through the formal process.
The patch is appended below. It's committed as
commit 3282632d9325267c850072db7545a884a1637f51 (HEAD -> master)
Author: Gert Doering <[email protected]...> Date: Thu May 5 13:38:12 2022 +0200
Fix trailing-whitespace errors in last patch.
When rewrapping comments and a single code line in b364711486dc6, some trailing whitespace escaped. Fix.
sorry,
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...From 3282632d9325267c850072db7545a884a1637f51 Mon Sep 17 00:00:00 2001
From: Gert Doering <[email protected]...>
Date: Thu, 5 May 2022 13:38:12 +0200
Subject: [PATCH] Fix trailing-whitespace errors in last patch.
When rewrapping comments and a single code line in b364711486dc6, some trailing whitespace escaped. Fix.
--- src/openvpn/mudp.c | 6 +++--- src/openvpn/ssl.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-)
src/openvpn/mudp.c +3 -3
@@ -178,8 +178,8 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated)if (do_pre_decrypt_check (m, &state, real)) {/* This is an unknown session but with valid tls-auth/tls-crypt -* (or no auth at all). If this is the initial packet of a -* session, we just send a reply with a HMAC session id and +* (or no auth at all). If this is the initial packet of a +* session, we just send a reply with a HMAC session id and * do not generate a session slot */ if (frequency_limit_event_allowed (m->new_connection_limiter))
@@ -191,7 +191,7 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated) mi->did_real_hash = true;multi_assign_peer_id (m, mi); } -/* If we have a session id already, ensure that the +/* If we have a session id already, ensure that the * state is using the same */ if (session_id_defined (&state.server_session_id) &&session_id_defined ((&state.peer_session_id)))
src/openvpn/ssl.c +1 -1
@@ -1285,7 +1285,7 @@ tls_auth_standalone_init(struct tls_options *tls_options,/* get initial frame parms, still need to finalize */ tas->frame = tls_options->frame; -packet_id_init (&tas->tls_wrap.opt.packet_id, tls_options->replay_window, +packet_id_init (&tas->tls_wrap.opt.packet_id, tls_options->replay_window, tls_options->replay_time,"TAS" ,0 );return tas;
Signature · 1 line
2.36.0
[Openvpn-devel] [PATCH applied] Re: Extract read_incoming_tls_ciphertext into function
In reply to Arne SchwabeStare-at-code and running client/server tests confirm that this is, indeed, only trivial move-around.
Your patch has been applied to the master branch.
commit 8b9b8f91b76af59e26edee46f1a1f4eebdca762b
Author: Arne Schwabe Date: Fri Apr 22 16:29:46 2022 +0200
Extract read_incoming_tls_ciphertext into function
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24152.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
PATCH v4 1/1 Implement HMAC based session id for tls-crypt v2
In reply to Arne SchwabeTls-crypt v2 is more complicated to implement a proper stateless handshake. To allow state handshake this commit does
- introduce a new packet CONTROL_WKC_V1 that repeats the wrapped client key.
- introduce a way to negotiate the support for this packet in the three way handshake
Details about the protocol changes are in tls-crypt-v2.txt. Optional arguments to the tls-crypt-v2 option have been added to explicitly allow or disallow client that do not support the stateless handshake.
Patch v3: improve grammar, style, comments, fix unit tests Patch v4: remove explicit flag for ability to resend WKc,
clean up comments, improve code style in some instances
--- Changes.rst | 10 +++ doc/man-sections/tls-options.rst | 14 ++++ doc/tls-crypt-v2.txt | 44 ++++++++++ src/openvpn/crypto.h | 8 ++ src/openvpn/init.c | 4 + src/openvpn/mudp.c | 88 ++++++++++++++++---- src/openvpn/options.c | 13 +++ src/openvpn/options.h | 3 + src/openvpn/reliable.h | 2 - src/openvpn/ssl.c | 125 +++++++++++++++++++++++++--- src/openvpn/ssl_pkt.c | 28 +++++-- src/openvpn/ssl_pkt.h | 33 +++++++- tests/unit_tests/openvpn/test_pkt.c | 10 +-- 13 files changed, 338 insertions(+), 44 deletions(-)
Changes.rst +10 -0
@@ -69,6 +69,16 @@ Improved ``--mssfix`` and ``--fragment`` calculation account and the resulting size is specified as the total size of the VPN packets including IP and UDP headers. +Cookie based handshake for UDP server + Instead of allocating a connection for each client on the initial packet + OpenVPN server will now use an HMAC based cookie as its session id. This + way the server can verify it on completing the handshake without keeping + state. This eliminates the amplification and resource exhaustion attacks. + For tls-crypt-v2 clients, this requires OpenVPN 2.6 clients or later + because the client needs to resend its client key on completing the hand + shake. The tls-crypt-v2 option allows controlling if older clients are + accepted. + Deprecated features ------------------- ``inetd`` has been removed
doc/man-sections/tls-options.rst +14 -0
@@ -486,6 +486,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa 8000 years'. --tls-crypt-v2 keyfile + + Valid syntax: + :: + tls-crypt-v2 keyfile + tls-crypt-v2 keyfile force-cookie + tls-crypt-v2 keyfile allow-noncookie + Use client-specific tls-crypt keys. For clients, ``keyfile`` is a client-specific tls-crypt key. Such a key
@@ -501,6 +508,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa client is using client-specific keys, and automatically select the right mode. + The optional parameters :code:`force-cookie` allows only tls-crypt-v2 + clients that support a cookie based stateless three way handshake that + avoids replay attacks and state exhaustion on the server side (OpenVPN + 2.6 and later). The option :code:`allow-noncookie` explicitly allows + older tls-crypt-v2 clients. The default is (currently) + :code:`allow-noncookie`. + --tls-crypt-v2-verify cmd Run command ``cmd`` to verify the metadata of the client-specific tls-crypt-v2 key of a connecting client. This allows server
doc/tls-crypt-v2.txt +44 -0
50 lines not on this page · raw message
src/openvpn/crypto.h +8 -0
@@ -256,6 +256,14 @@ struct crypto_options/**< Bit-flag indicating that data channel key derivation * is done using TLS keying material export [RFC5705] */ +#define CO_RESEND_WKC (1<<4) +/**< Bit-flag indicating that the client is expected to +* resend the wrapped client key with the 2nd packet (packet-id 1) +* like with the HARD_RESET_CLIENT_V3 packet */ +#define CO_FORCE_TLSCRYPTV2_COOKIE (1<<5) +/**< Bit-flag indicating that we do not allow clients that do +* not support resending the wrapped client key (WKc) with the +* third packet of the three-way handshake */ unsigned int flags;/**< Bit-flags determining behavior of * security operation functions. */ };
src/openvpn/init.c +4 -0
@@ -2960,6 +2960,10 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) { to.tls_wrap.tls_crypt_v2_server_key = c->c1.ks.tls_crypt_v2_server_key; to.tls_crypt_v2_verify_script = c->options.tls_crypt_v2_verify_script; +if (options->ce.tls_crypt_v2_force_cookie) + { + to.tls_wrap.opt.flags |=CO_FORCE_TLSCRYPTV2_COOKIE ; + } } }
src/openvpn/mudp.c +70 -18
114 lines not on this page · raw message
src/openvpn/options.c +13 -0
@@ -8896,6 +8896,19 @@ add_option(struct options *options, options->ce.tls_crypt_v2_file = p[1 ]; options->ce.tls_crypt_v2_file_inline = is_inline; } + +if (p[2 ] &&streq (p[2 ],"force-cookie" )) + { + options->ce.tls_crypt_v2_force_cookie = true; + } +else if (p[2 ] &&streq (p[2 ],"allow-noncookie" )) + { + options->ce.tls_crypt_v2_force_cookie = false; + } +else if (p[2 ]) + { +msg (msglevel,"Unsupported tls-crypt-v2 argument: %s" , p[2 ]); + } }else if (streq (p[0 ],"tls-crypt-v2-verify" ) && p[1 ] && !p[2 ]) {
src/openvpn/options.h +3 -0
@@ -162,6 +162,9 @@ struct connection_entry* authenticated encryption v2 */ const char *tls_crypt_v2_file;bool tls_crypt_v2_file_inline; + +/* Allow only client that support resending the wrapped client key */ +bool tls_crypt_v2_force_cookie; };struct remote_entry
src/openvpn/reliable.h +0 -2
@@ -361,8 +361,6 @@ struct reliable_entry *reliable_get_entry_sequenced(struct reliable *rel);* * @param rel The reliable structure associated with the given buffer. * @param buf The buffer of the reliable entry which is to be removed. -* @param inc_pid If true, the reliable structure's packet ID counter -* will be incremented. */ void reliable_mark_deleted (struct reliable *rel,struct buffer *buf);
src/openvpn/ssl.c +114 -11
177 lines not on this page · raw message
src/openvpn/ssl_pkt.c +23 -5
@@ -148,7 +148,8 @@ tls_wrap_control(struct tls_wrap_ctx *ctx, uint8_t header, struct buffer *buf,return ; } -if ((header >>P_OPCODE_SHIFT ) ==P_CONTROL_HARD_RESET_CLIENT_V3 ) +if ((header >>P_OPCODE_SHIFT ) ==P_CONTROL_HARD_RESET_CLIENT_V3 + || (header >>P_OPCODE_SHIFT ) ==P_CONTROL_WKC_V1 ) {if (!buf_copy (&ctx->work, ctx->tls_crypt_v2_wkc))
@@ -197,7 +198,8 @@ read_control_auth(struct buffer *buf,bool ret = false;const uint8_t opcode = *(BPTR (buf)) >>P_OPCODE_SHIFT ; -if (opcode ==P_CONTROL_HARD_RESET_CLIENT_V3 +if ((opcode ==P_CONTROL_HARD_RESET_CLIENT_V3 + || opcode ==P_CONTROL_WKC_V1 ) && !tls_crypt_v2_extract_client_key (buf, ctx, opt)) { msg(D_TLS_ERRORS,
@@ -321,6 +323,7 @@ tls_pre_decrypt_lite(const struct tls_auth_standalone *tas,if (op !=P_CONTROL_HARD_RESET_CLIENT_V2 && op !=P_CONTROL_HARD_RESET_CLIENT_V3 && op !=P_CONTROL_V1 + && op !=P_CONTROL_WKC_V1 && op !=P_ACK_V1 ) {/*
@@ -397,6 +400,10 @@ tls_pre_decrypt_lite(const struct tls_auth_standalone *tas, {return VERDICT_VALID_RESET_V3 ; } + else if (op == P_CONTROL_WKC_V1) + { +return VERDICT_VALID_WKC_V1 ; + } else {return VERDICT_VALID_RESET_V2 ;
@@ -410,10 +417,12 @@ error:struct buffer -tls_reset_standalone (struct tls_auth_standalone *tas, +tls_reset_standalone (struct tls_wrap_ctx *ctx, +struct tls_auth_standalone *tas,struct session_id *own_sid,struct session_id *remote_sid, -uint8_t header) +uint8_t header, +bool request_resend_wkc) {struct buffer buf =alloc_buf (tas->frame.buf.payload_size);ASSERT (buf_init (&buf, tas->frame.buf.headroom));
@@ -434,8 +443,17 @@ tls_reset_standalone(struct tls_auth_standalone *tas,ASSERT (buf_write (&buf, &net_pid,sizeof (net_pid))); +/* Add indication for tls-crypt-v2 to resend the packet with the with +* reply */ +if (request_resend_wkc) + { +buf_write_u16 (&buf,TLV_TYPE_EARLY_NEG_FLAGS );/* TYPE: flags */ +buf_write_u16 (&buf,sizeof (uint16_t )); +buf_write_u16 (&buf,EARLY_NEG_FLAG_RESEND_WKC ); + } +/* Add tls-auth/tls-crypt wrapping, this might replace buf */ -tls_wrap_control (&tas->tls_wrap, header, &buf, own_sid); +tls_wrap_control (ctx, header, &buf, own_sid);return buf; }
src/openvpn/ssl_pkt.h +30 -3
@@ -54,11 +54,15 @@/* indicates key_method >= 2 and client-specific tls-crypt key */ #define P_CONTROL_HARD_RESET_CLIENT_V3 10/* initial key from client, forget previous state */ +/* Variant of P_CONTROL_V1 but with appended wrapped key +* like P_CONTROL_HARD_RESET_CLIENT_V3 */ +#define P_CONTROL_WKC_V1 11 +/* define the range of legal opcodes * Since we do no longer support key-method 1 we consider * the v1 op codes invalid */ #define P_FIRST_OPCODE 3 -#define P_LAST_OPCODE 10 +#define P_LAST_OPCODE 11/* * Define number of buffers for send and receive in the reliability layer.
@@ -86,6 +90,8 @@ enum first_packet_verdict {/** This packet is a valid ACK control packet from the peer, * i.e. it has a valid session id hmac in it */ VERDICT_VALID_ACK_V1 , +/** The packet is a valid control packet with appended wrapped client key */ +VERDICT_VALID_WKC_V1 ,/** the packet failed on of the various checks */ VERDICT_INVALID };
@@ -217,10 +223,12 @@ read_control_auth(struct buffer *buf,* The returned buf needs to be free with \c free_buf */ struct buffer -tls_reset_standalone (struct tls_auth_standalone *tas, +tls_reset_standalone (struct tls_wrap_ctx *ctx, +struct tls_auth_standalone *tas,struct session_id *own_sid,struct session_id *remote_sid, -uint8_t header); +uint8_t header, +bool request_resend_wkc);static inline const char *packet_opcode_name (int op)
@@ -248,6 +256,9 @@ packet_opcode_name(int op)case P_CONTROL_V1 :return "P_CONTROL_V1" ; +case P_CONTROL_WKC_V1 : +return "P_CONTROL_WKC_V1" ; +case P_ACK_V1 :return "P_ACK_V1" ;
@@ -261,4 +272,20 @@ packet_opcode_name(int op) return "P_???"; } } + +/* initial packet id (instead of 0) that indicates that the peer supports +* early protocol negotiation. This will make the packet id turn a bit faster +* but the network time part of the packet id takes care of that. And +* this is also a rather theoretical scenario as it still needs more than +* 2^31 control channel packets to happen */ +#define EARLY_NEG_MASK 0xff000000 +#define EARLY_NEG_START 0x0f000000 + + +/* Early negotiation that part of the server response in the RESET_V2 packet. +* Since clients that announce early negotiation support will treat the payload +* of reset packets special and parse it as TLV messages. +* as TLV (type, length, value) */ +#define TLV_TYPE_EARLY_NEG_FLAGS 0x0001 +#define EARLY_NEG_FLAG_RESEND_WKC 0x0001#endif /* ifndef SSL_PKT_H */
tests/unit_tests/openvpn/test_pkt.c +5 -5
@@ -536,7 +536,7 @@ test_generate_reset_packet_plain(void **ut_state)uint8_t header =0 | (P_CONTROL_HARD_RESET_CLIENT_V2 <<P_OPCODE_SHIFT ); -struct buffer buf =tls_reset_standalone (&tas, &client_id, &server_id, header); +struct buffer buf =tls_reset_standalone (&tas.tls_wrap, &tas, &client_id, &server_id, header, false); verdict =tls_pre_decrypt_lite (&tas, &state, &from, &buf);
@@ -544,7 +544,7 @@ test_generate_reset_packet_plain(void **ut_state)/* Assure repeated generation of reset is deterministic/stateless*/ assert_memory_equal (state.peer_session_id.id, client_id.id,SID_SIZE ); -struct buffer buf2 =tls_reset_standalone (&tas, &client_id, &server_id, header); +struct buffer buf2 =tls_reset_standalone (&tas.tls_wrap, &tas, &client_id, &server_id, header, false);assert_int_equal (BLEN (&buf),BLEN (&buf2));assert_memory_equal (BPTR (&buf),BPTR (&buf2),BLEN (&buf));free_buf (&buf2);
@@ -571,7 +571,7 @@ test_generate_reset_packet_tls_auth(void **ut_state) now =0x22446688 ;reset_packet_id_send (&tas_client.tls_wrap.opt.packet_id.send); -struct buffer buf =tls_reset_standalone (&tas_client, &client_id, &server_id, header); +struct buffer buf =tls_reset_standalone (&tas_client.tls_wrap, &tas_client, &client_id, &server_id, header, false);enum first_packet_verdict verdict =tls_pre_decrypt_lite (&tas_server, &state, &from, &buf);assert_int_equal (verdict,VERDICT_VALID_RESET_V2 );
@@ -580,11 +580,11 @@ test_generate_reset_packet_tls_auth(void **ut_state)/* Assure repeated generation of reset is deterministic/stateless*/ reset_packet_id_send (&tas_client.tls_wrap.opt.packet_id.send); -struct buffer buf2 =tls_reset_standalone (&tas_client, &client_id, &server_id, header); +struct buffer buf2 =tls_reset_standalone (&tas_client.tls_wrap, &tas_client, &client_id, &server_id, header,false);assert_int_equal (BLEN (&buf),BLEN (&buf2));assert_memory_equal (BPTR (&buf),BPTR (&buf2),BLEN (&buf)); -free_buf (&buf2); +free_buf (&buf2);free_tls_pre_decrypt_state (&state);packet_id_free (&tas_client.tls_wrap.opt.packet_id);
Signature · 1 line
2.32.0 (Apple Git-132)
Re: [Openvpn-devel] [PATCH v4] Implement HMAC based session id for tls-crypt v2
Hi,
Quoted 8 lines · Arne Schwabe
Tls-crypt v2 is more complicated to implement a proper stateless handshake. To allow state handshake this commit does
- introduce a new packet
CONTROL_WKC_V1that repeats the wrapped client key.
- introduce a way to negotiate the support for this packet in the three way handshake
Quoted 5 lines
Details about the protocol changes are in tls-crypt-v2.txt. Optional arguments to the tls-crypt-v2 option have been added to explicitly allow or disallow client that do not support the stateless handshake.
Signed-off-by Arne Schwabe <[email protected]...>
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH v4] Implement HMAC based session id for tls-crypt v2
In reply to Arne SchwabeQuoted 2 lines
Arne Schwabe <[email protected]...> hat am 05.05.2022 15:03 geschrieben:
[...]
Quoted 4 lines
src/openvpn/ssl_pkt.c +0
-0
[...]
src/openvpn/ssl_pkt.c tls_reset_standalone(struct tls_auth_standalone *tas, · line 447
Quoted 6 lines
src/openvpn/ssl_pkt.c +2
-0@@ -434,8 +443,17 @@ tls_reset_standalone(struct tls_auth_standalone *tas,ASSERT (buf_write (&buf, &net_pid,sizeof (net_pid))); +/* Add indication for tls-crypt-v2 to resend the packet with the with +* reply */
You didn't fix this comment. In my v3 review I thought the second "with" was redundant, but now I think you probably meant "with the key with the reply"?
src/openvpn/ssl_pkt.c tls_reset_standalone(struct tls_auth_standalone *tas, · line 459
Quoted 13 lines
src/openvpn/ssl_pkt.c +8
-1+if (request_resend_wkc) + { +buf_write_u16 (&buf,TLV_TYPE_EARLY_NEG_FLAGS );/* TYPE: flags */ +buf_write_u16 (&buf,sizeof (uint16_t )); +buf_write_u16 (&buf,EARLY_NEG_FLAG_RESEND_WKC ); + } +/* Add tls-auth/tls-crypt wrapping, this might replace buf */ -tls_wrap_control (&tas->tls_wrap, header, &buf, own_sid); +tls_wrap_control (ctx, header, &buf, own_sid);return buf; }
Regards,
--
Frank Lichtenheld
[Openvpn-devel] [PATCH applied] Re: Implement HMAC based session id for tls-crypt v2
In reply to Arne SchwabeI have stared at the code for a bit (and fixed "the comment"), all looks reasonable.
Building a test setup took quite a bit of time (different client-side keys, different client versions, --verify script, etc.) but the server did exactly what was documented -> good :-)
I have also tested "force-cookie", "default" and "allow-noncookie" with 2.5.6 and "same codebase master" clients - it works as documented, *but* in the "force-cookie" case, the logging is unsatisfactory - --verb 4 will show "yes, there is activity", but the
tls-crypt-v2 force-cookie is enabled,ignoring connection attempt from old client...
message needs --verb 7 -> this needs to go to a more clearly visible log level, so admins have a better chance to see what is happening.
Discussed on IRC, agreed on "let's make the logging more useful in a followup patch".
I have rewrapped one msg() line in mudp.c, because the 3 lines nicely fit in two, making the result more readable. Also, added a ")" to a comment in ssl.c, and rewrapped slightly.
Your patch has been applied to the master branch.
commit e7d8c4a72002cbaa7542ea0cff8acca1b971b1f5
Author: Arne Schwabe Date: Thu May 5 15:03:48 2022 +0200
Implement HMAC based session id for tls-crypt v2
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Antonio Quartulli <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24287.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Optimise three-way handshake condition for S_PRE_START to S_START
In reply to Arne SchwabeAs instructed, I have done quite a bit of wording changes in the commit message and the tls_process_state() comment. No code change.
Also, I've left off the hunk for test_pkt.c, as that is already in (v2 or v3 of one of the previous patches in the series).
S-O-B added.
I have not explicitly counted handshake packets, but the explanation seems to match the actual code change, and the explanation makes sense.
Lightly (client-side only) tested.
Your patch has been applied to the master branch.
commit 3f5626891e6bc569456ab168b3a5e5f76e0538bd
Author: Arne Schwabe Date: Fri Apr 22 16:29:48 2022 +0200
Optimise three-way handshake condition for S_PRE_START to S_START
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24161.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Extract read_incoming_tls_plaintext into its own function
In reply to Arne SchwabeStared at code for a bit (looks trivial enough), added "and" to the commit message, ran client test.
Your patch has been applied to the master branch.
commit 413877f522e89ccc6dd2543bc585ac553df3bd42
Author: Arne Schwabe Date: Tue May 3 13:29:00 2022 +0200
Extract read_incoming_tls_plaintext into its own function
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24268.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
PATCH 1/2 Add uncrustify check to github actions
In reply to Arne SchwabeThis adds checking if the code style is still clean github actions with the exact version of uncrustify that is required and might also be helpful for external commiters to get notified about code style problem when running the Github actions on their own repository.
--- .github/workflows/build.yaml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
.github/workflows/build.yaml +29 -0
@@ -3,6 +3,35 @@ name: Build on: [push, pull_request] jobs: + checkuncrustify: + name: "Check code style with Uncrustify" + # Ubuntu 22.04 has uncrustify 0.72_f + runs-on: ubuntu-22.04 + steps: + - name: Install dependencies + run: sudo apt update && sudo apt install -y uncrustify + - name: Checkout OpenVPN + uses: actions/[email protected]... + with: + path: openvpn + - name: Show uncrustify version + run: uncrustify --version + - name: Run uncrustify + run: ./dev-tools/reformat-all.sh + working-directory: openvpn + - name: Check for changes + run: git diff --output=uncrustify-changes.patch + working-directory: openvpn + - name: Show changes on standard output + run: git diff + working-directory: openvpn + - uses: actions/[email protected]... + with: + name: uncrustify-changes.patch + path: 'openvpn/uncrustify-changes.patch' + - name: Set job status + run: test ! -s uncrustify-changes.patch + working-directory: openvpn mingw: strategy: fail-fast: false
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH 2/2 Add ubuntu 22.04 to Github Actions
This adds Ubuntu 22.04 to the Github actions. mbed TLS in 22.04 is still old enough (2.28) to build with OpenVPN and GPL licensed.
--- .github/workflows/build.yaml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
.github/workflows/build.yaml +5 -1
@@ -159,7 +159,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-18.04, ubuntu-20.04] + os: [ubuntu-18.04, ubuntu-20.04, ubuntu-22.04] sslpkg: [libmbedtls-dev] ssllib: [mbedtls] libname: [mbed TLS]
@@ -177,6 +177,10 @@ jobs: sslpkg: "libssl-dev" libname: OpenSSL 1.1.1 ssllib: openssl + - os: ubuntu-22.04 + sslpkg: "libssl-dev" + libname: OpenSSL 3.0.2 + ssllib: openssl - os: ubuntu-20.04 sslpkg: "libssl-dev" libname: OpenSSL 1.1.1
Signature · 1 line
2.32.0 (Apple Git-132)
Re: [Openvpn-devel] [PATCH 25/28] Ensure that control channel packet are respecting tls-mtu
In reply to Arne SchwabeNote: Patch has some conflicts against current master.
Note2: I get "make check" failures:
2022-05-06 16:29:23 Assertion failed at init.c:2991 (c->c2.tls_multi->opt.frame.buf.payload_size <= c->c2.frame.buf.payload_size) 2022-05-06 16:29:23 Exiting due to fatal error
Quoted 4 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
This ensure that control packets are actually are actually smaller than
redundant "are actually"
Quoted 3 lines
tls-mtu. Since OpenVPN will consider a control message packet complete when the TLS record is complete, we have to ensure that the SSL library will still write one records, so the receiving side will only be able
"record"
Quoted 1 line
to get/read the control message content when a TLS records is
"record"
Quoted 32 lines
complete. To achieve this goal, this commit does:
- Splitting one read from TLS library into multiple control channel packets, splitting one TLS record into multiple control packets.
- increase allowed number of outstanding packets to 6 from 4 on the sender side. This is still okay with older implementation as receivers will have room for 8.
- calculate the overhead for control channel message to allow staying below that threshold.
- remove maxlen fromkey_state_read_ciphertextand related functions as we now always allow control channel messages to be up toTLS_CHANNEL_BUF_SIZEin size and longer limit this by the mtu of control packets as the implemented splitting will take care of larger payloads from the SSL library--- src/openvpn/reliable.c | 64 +++++++++++---- src/openvpn/reliable.h | 32 +++++++- src/openvpn/ssl.c | 162 ++++++++++++++++++++++++++++++++------ src/openvpn/ssl_backend.h | 8 +- src/openvpn/ssl_mbedtls.c | 14 +--- src/openvpn/ssl_openssl.c | 16 ++-- src/openvpn/ssl_pkt.h | 4 +- 7 files changed, 229 insertions(+), 71 deletions(-)src/openvpn/reliable.c +0
-0@@ -41,30 +41,30 @@#include "memdbg.h"
src/openvpn/reliable.c line 45
Quoted 10 lines
src/openvpn/reliable.c +2
-7-/* -* verify that test - base < extent while allowing for base or test wraparound -*/ -static inline bool -reliable_pid_in_range1 (const packet_id_type test, -const packet_id_type base, -const unsigned int extent) +/* calculates test - base while allowing for base or test wraparound. test is +* assume to be higher than base */
"assumed"
Quoted 8 lines
src/openvpn/reliable.c +2
-3+static inline packet_id_type +subtract_pid (const packet_id_type test,const packet_id_type base) {if (test >= base) { -if (test - base < extent) - { -return true;
src/openvpn/reliable.c line 55
Quoted 10 lines
src/openvpn/reliable.c +2
-5- } +return test - base; }else { -if ((test+0x80000000u ) - (base+0x80000000u ) < extent) - { -return true; - } +return (test+0x80000000u ) - (base+0x80000000u );
As discussed on IRC, I think this is bogus. test - base is the same as (test+0x80000000u) - (base+0x80000000u), so I don't see what the if accomplishes.
Quoted 37 lines
src/openvpn/reliable.c +27
-1} +} -return false; +/* +* verify that test - base < extent while allowing for base or test wraparound +*/ +static inline bool +reliable_pid_in_range1 (const packet_id_type test, +const packet_id_type base, +const unsigned int extent) +{ +return subtract_pid (test, base) < extent; }/* @@ -496,6 +496,38 @@ reliable_get_buf(struct reliable *rel)return NULL ; } +/* Counts the number of free buffers in output that can be potientially used +* for sending */ +int +reliable_get_num_output_sequenced_available (struct reliable *rel) +{ +struct gc_arena gc =gc_new (); +packet_id_type min_id =0 ; +bool min_id_defined = false; + +/* find minimum active packet_id */ +for (int i =0 ; i < rel->size; ++i) + { +const struct reliable_entry *e = &rel->array[i]; +if (e->active) + { +if (!min_id_defined ||reliable_pid_min (e->packet_id, min_id)) + {
src/openvpn/reliable.c reliable_get_buf(struct reliable *rel) · line 525
Quoted 10 lines
src/openvpn/reliable.c +10
-0+ min_id_defined = true; + min_id = e->packet_id; + } + } + } + +int ret = rel->size; +if (min_id_defined) + { + ret -=subtract_pid (rel->packet_id, min_id);
Okay... Why does this work? packet_id always increases by 1 exactly?
Quoted 32 lines
src/openvpn/reliable.c +5
-0+ } +gc_free (&gc); +return ret; +} +/* grab a free buffer, fail if buffer clogged by unacknowledged low packet IDs */ struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel)src/openvpn/reliable.h +2
-2@@ -46,7 +46,7 @@* be stored in one \c reliable_ack * structure. */ -#define RELIABLE_CAPACITY 8/**< The maximum number of packets that +#define RELIABLE_CAPACITY 12/**< The maximum number of packets that * the reliability layer for one VPN * tunnel in one direction can store. */ @@ -93,7 +93,7 @@ struct reliableint size;interval_t initial_timeout;packet_id_type packet_id; -int offset; +int offset;/**< Offset of the bufs in the reliable_entry array */ bool hold;/* don't xmit until reliable_schedule_now is called */ struct reliable_entry array[RELIABLE_CAPACITY ]; };@@ -178,6 +178,20 @@ reliable_ack_empty(struct reliable_ack *ack)return !ack->len;
src/openvpn/reliable.h reliable_ack_empty(struct reliable_ack *ack) · line 188
Quoted 10 lines
src/openvpn/reliable.h +8
-0} +/** +* Returns the number of packets that need to be acked. +* +* @param ack The acknowledgment structure to check. +* +* @returns the number of outstanding acks +*/ +static inline bool
bool? you probably meant int.
Quoted 35 lines
src/openvpn/reliable.h +20
-0+reliable_ack_outstanding(struct reliable_ack *ack) +{ +return ack->len; +} + +/** * Write a packet ID acknowledgment record to a buffer. * @@ -385,6 +399,20 @@ void reliable_mark_deleted(struct reliable *rel, struct buffer *buf);*/ struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel); + +/** +* Counts the number of free buffers in output that can be potientially used +* for sending +* +* @param rel The reliable structure in which to search for a free +* entry. +* +* @return the number of buffer that are available for sending without +* breaking ack sequence +* */ +int +reliable_get_num_output_sequenced_available (struct reliable *rel); +/** * Mark the reliable entry associated with the given buffer as * active outgoing. src/openvpn/ssl.c +0
-0
src/openvpn/ssl.c tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame · line 328
Quoted 8 lines
src/openvpn/ssl.c +3
-2@@ -323,10 +323,12 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame/* Previous OpenVPN version calculated the maximum size and buffer of a * control frame depending on the overhead of the data channel frame * overhead and limited its maximum size to 1250. We always allocate the -* 1250 buffer size since a lot of code blindly assumes a large buffer -* (e.g. PUSH_BUNDLE_SIZE) and set frame->mtu_mtu as suggestion for the +* TLS_CHANNEL_BUF_SIZE buffer size since a lot of code blindly assumes +* a large buffer (e.g. PUSH_BUNDLE_SIZE) and also our peer might have +* a higher size configure and we still want to be able to receive the
"configured"
Quoted 8 lines
src/openvpn/ssl.c +2
-1+* packets. frame->mtu_mtu is set as suggestion for the maximum packet * size */ - frame->buf.payload_size =1250 + overhead; + frame->buf.payload_size =TLS_CHANNEL_BUF_SIZE + overhead; frame->buf.headroom = overhead; frame->buf.tailroom = overhead;
src/openvpn/ssl.c tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame · line 342
Quoted 7 lines
src/openvpn/ssl.c +4
-0@@ -334,6 +336,48 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame frame->tun_mtu =min_int (data_channel_frame->tun_mtu,1250 ); } +/** +* calculate the maximum overhead that control channel frames have +* This includes header, op code and everything apart from the +* payload itself. This method is a bit pessmistic and might give higher
"pessimistic"
src/openvpn/ssl.c tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame · line 343
Quoted 1 line
src/openvpn/ssl.c +1
-0+* overhead that we actually have */
"than"
Quoted 12 lines
src/openvpn/ssl.c +12
-0+static int +calc_control_channel_frame_overhead (const struct tls_session *session) +{ +const struct key_state *ks = &session->key[KS_PRIMARY ]; +int overhead =0 ; + +/* TCP length field and opcode */ + overhead+=3 ; + +/* our own session id */ + overhead +=SID_SIZE ; +
src/openvpn/ssl.c tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame · line 365
Quoted 10 lines
src/openvpn/ssl.c +10
-0+/* ACK array and remote SESSION ID (part of the ACK array) */ + overhead +=ACK_SIZE (min_int (reliable_ack_outstanding (ks->rec_ack),CONTROL_SEND_ACK_MAX )); + +/* Message packet id */ + overhead +=sizeof (packet_id_type); + +if (session->tls_wrap.mode ==TLS_WRAP_CRYPT ) + { + overhead +=tls_crypt_buf_overhead (); + overhead +=packet_id_size (true);
tls_crypt_buf_overhead already contains packet_id_size(true)
src/openvpn/ssl.c control_packet_needs_wkc(const struct key_state *ks)
Quoted 19 lines
src/openvpn/ssl.c +15
-0+ } +else if (session->tls_wrap.mode ==TLS_WRAP_AUTH ) + { + overhead +=hmac_ctx_size (session->tls_wrap.opt.key_ctx_bi.encrypt.hmac); + overhead +=packet_id_size (true); + } + +/* Add the typical UDP overhead for an IPv6 UDP packet. TCP+IPv6 has a +* larger overhead but the risk of a TCP connection getting dropped because +* we try to send a too large packet is basically zero */ + overhead +=datagram_overhead (AF_INET6 ,PROTO_UDP ); + +return overhead; +} +void init_ssl_lib (void ) {@@ -2613,10 +2657,13 @@ control_packet_needs_wkc(const struct key_state *ks)
[...]
Rest of patch not yet reviewed.
Regards,
--
Frank Lichtenheld
[Openvpn-devel] [PATCH applied] Re: Add uncrustify check to github actions
In reply to Arne SchwabeNice one. Tested on my github repo with the clean "master" tree, and then applied a crappy change and tested again.
(https://github.com/cron2/openvpn/actions/runs/2282765938 ->
https://github.com/cron2/openvpn/runs/6325784768?check_suite_focus)
Your patch has been applied to the master branch.
commit 8eeb4751767c5c2f6a192fc917330b2974dac9de
Author: Arne Schwabe Date: Fri May 6 15:28:35 2022 +0200
Add uncrustify check to github actions
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24300.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Add ubuntu 22.04 to Github Actions
In reply to Arne SchwabeLooks good. Not tested anything, github can do that :-)
Added SOB.
Your patch has been applied to the master branch.
commit baa1e67f310b1db3c5b7b9b9f12867a86c947648
Author: Arne Schwabe Date: Fri May 6 15:28:36 2022 +0200
Add ubuntu 22.04 to Github Actions
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24299.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
PATCH v2 25/28 Ensure that control channel packet are respecting tls-mtu
In reply to Arne SchwabeThis ensure that control packets are actually smaller than tls-mtu. Since OpenVPN will consider a control message packet complete when the TLS record is complete, we have to ensure that the SSL library will still write one record, so the receiving side will only be able to get/read the control message content when a TLS record is complete. To achieve this goal, this commit does:
- Splitting one read from TLS library into multiple control channel packets, splitting one TLS record into multiple control packets.
- increase allowed number of outstanding packets to 6 from 4 on the sender side. This is still okay with older implementation as receivers will have room for 8.
- calculate the overhead for control channel message to allow staying below that threshold.
- remove maxlen from key_state_read_ciphertext and related functions as we now always allow control channel messages to be up to TLS_CHANNEL_BUF_SIZE in size and longer limit this by the mtu of control packets as the implemented splitting will take care of larger payloads from the SSL library
Patch v2: avoid assertion about to large buffer by sticking to 1250 max control size
in this commit and leaving larger sizes for the --tls-mtu commit. Also fix various other small problems and grammer fixes.
--- src/openvpn/reliable.c | 57 ++++++++++---- src/openvpn/reliable.h | 32 +++++++- src/openvpn/ssl.c | 152 +++++++++++++++++++++++++++++++++----- src/openvpn/ssl_backend.h | 8 +- src/openvpn/ssl_mbedtls.c | 14 +--- src/openvpn/ssl_openssl.c | 16 ++-- src/openvpn/ssl_pkt.h | 4 +- 7 files changed, 215 insertions(+), 68 deletions(-)
src/openvpn/reliable.c +41 -16
@@ -41,6 +41,14 @@#include "memdbg.h" +/* calculates test - base while allowing for base or test wraparound. test is +* assumed to be higher than base */ +static inline packet_id_type +subtract_pid (const packet_id_type test,const packet_id_type base) +{ +return test - base; +} +/* * verify that test - base < extent while allowing for base or test wraparound */
@@ -49,22 +57,7 @@ reliable_pid_in_range1(const packet_id_type test, const packet_id_type base, const unsigned int extent) { - if (test >= base) - { - if (test - base < extent) - { -return true; - } - } - else - { -if ((test+0x80000000u ) - (base+0x80000000u ) < extent) - { -return true; - } - } - -return false; + return subtract_pid(test, base) < extent; }/*
@@ -496,6 +489,38 @@ reliable_get_buf(struct reliable *rel)return NULL ; } +/* Counts the number of free buffers in output that can be potientially used +* for sending */ +int +reliable_get_num_output_sequenced_available (struct reliable *rel) +{ +struct gc_arena gc =gc_new (); +packet_id_type min_id =0 ; +bool min_id_defined = false; + +/* find minimum active packet_id */ +for (int i =0 ; i < rel->size; ++i) + { +const struct reliable_entry *e = &rel->array[i]; +if (e->active) + { +if (!min_id_defined ||reliable_pid_min (e->packet_id, min_id)) + { + min_id_defined = true; + min_id = e->packet_id; + } + } + } + +int ret = rel->size; +if (min_id_defined) + { + ret -=subtract_pid (rel->packet_id, min_id); + } +gc_free (&gc); +return ret; +} +/* grab a free buffer, fail if buffer clogged by unacknowledged low packet IDs */ struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel)
src/openvpn/reliable.h +30 -2
@@ -46,7 +46,7 @@* be stored in one \c reliable_ack * structure. */ -#define RELIABLE_CAPACITY 8/**< The maximum number of packets that +#define RELIABLE_CAPACITY 12/**< The maximum number of packets that * the reliability layer for one VPN * tunnel in one direction can store. */
@@ -93,7 +93,7 @@ struct reliableint size;interval_t initial_timeout;packet_id_type packet_id; -int offset; +int offset;/**< Offset of the bufs in the reliable_entry array */ bool hold;/* don't xmit until reliable_schedule_now is called */ struct reliable_entry array[RELIABLE_CAPACITY ]; };
@@ -178,6 +178,20 @@ reliable_ack_empty(struct reliable_ack *ack)return !ack->len; } +/** +* Returns the number of packets that need to be acked. +* +* @param ack The acknowledgment structure to check. +* +* @returns the number of outstanding acks +*/ +static inline int +reliable_ack_outstanding (struct reliable_ack *ack) +{ +return ack->len; +} + +/** * Write a packet ID acknowledgment record to a buffer. *
@@ -385,6 +399,20 @@ void reliable_mark_deleted(struct reliable *rel, struct buffer *buf);*/ struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel); + +/** +* Counts the number of free buffers in output that can be potientially used +* for sending +* +* @param rel The reliable structure in which to search for a free +* entry. +* +* @return the number of buffer that are available for sending without +* breaking ack sequence +* */ +int +reliable_get_num_output_sequenced_available (struct reliable *rel); +/** * Mark the reliable entry associated with the given buffer as * active outgoing.
src/openvpn/ssl.c +132 -20
185 lines not on this page · raw message
src/openvpn/ssl_backend.h +3 -5
@@ -461,7 +461,6 @@ int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl,* @param ks_ssl - The security parameter state for this %key * session. * @param buf - A buffer in which to store the ciphertext. -* @param maxlen - The maximum number of bytes to extract. * * @return The return value indicates whether the data was successfully * processed:
@@ -470,8 +469,8 @@ int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl,* later to retry. * - \c -1: An error occurred. */ -int key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen); +int key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf); +/** @} name Functions for packets to be sent to a remote OpenVPN peer */
@@ -517,8 +516,7 @@ int key_state_write_ciphertext(struct key_state_ssl *ks_ssl,* later to retry. * - \c -1: An error occurred. */ -int key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen); +int key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf);/** @} name Functions for packets received from a remote OpenVPN peer */
src/openvpn/ssl_mbedtls.c +2 -12
@@ -1285,8 +1285,7 @@ key_state_write_plaintext_const(struct key_state_ssl *ks, const uint8_t *data, i }int -key_state_read_ciphertext (struct key_state_ssl *ks,struct buffer *buf, -int maxlen) +key_state_read_ciphertext (struct key_state_ssl *ks,struct buffer *buf) {int retval =0 ;int len =0 ;
@@ -1304,10 +1303,6 @@ key_state_read_ciphertext(struct key_state_ssl *ks, struct buffer *buf, } len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - } retval =endless_buf_read (&ks->bio_ctx->out,BPTR (buf), len);
@@ -1388,8 +1383,7 @@ key_state_write_ciphertext(struct key_state_ssl *ks, struct buffer *buf) }int -key_state_read_plaintext (struct key_state_ssl *ks,struct buffer *buf, -int maxlen) +key_state_read_plaintext (struct key_state_ssl *ks,struct buffer *buf) {int retval =0 ;int len =0 ;
@@ -1407,10 +1401,6 @@ key_state_read_plaintext(struct key_state_ssl *ks, struct buffer *buf, } len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - } retval =mbedtls_ssl_read (ks->ctx,BPTR (buf), len);
src/openvpn/ssl_openssl.c +5 -11
@@ -1871,7 +1871,7 @@ bio_write_post(const int status, struct buffer *buf)* Read from an OpenSSL BIO in non-blocking mode. */ static int -bio_read (BIO *bio,struct buffer *buf,int maxlen,const char *desc) +bio_read (BIO *bio,struct buffer *buf,const char *desc) {int i;int ret =0 ;
@@ -1882,10 +1882,6 @@ bio_read(BIO *bio, struct buffer *buf, int maxlen, const char *desc)else {int len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - }/* * BIO_read brackets most of the serious RSA
@@ -2012,15 +2008,14 @@ key_state_write_plaintext_const(struct key_state_ssl *ks_ssl, const uint8_t *dat }int -key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen) +key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf) {int ret =0 ;perf_push (PERF_BIO_READ_CIPHERTEXT );ASSERT (NULL != ks_ssl); - ret =bio_read (ks_ssl->ct_out, buf, maxlen,"tls_read_ciphertext" ); + ret =bio_read (ks_ssl->ct_out, buf,"tls_read_ciphertext" );perf_pop ();return ret;
@@ -2042,15 +2037,14 @@ key_state_write_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf) }int -key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen) +key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf) {int ret =0 ;perf_push (PERF_BIO_READ_PLAINTEXT );ASSERT (NULL != ks_ssl); - ret =bio_read (ks_ssl->ssl_bio, buf, maxlen,"tls_read_plaintext" ); + ret =bio_read (ks_ssl->ssl_bio, buf,"tls_read_plaintext" );perf_pop ();return ret;
src/openvpn/ssl_pkt.h +2 -2
@@ -67,8 +67,8 @@/* * Define number of buffers for send and receive in the reliability layer. */ -#define TLS_RELIABLE_N_SEND_BUFFERS 4/* also window size for reliability layer */ -#define TLS_RELIABLE_N_REC_BUFFERS 8 +#define TLS_RELIABLE_N_SEND_BUFFERS 6/* also window size for reliability layer */ +#define TLS_RELIABLE_N_REC_BUFFERS 12/* * Used in --mode server mode to check tls-auth signature on initial
Signature · 1 line
2.32.0 (Apple Git-132)
PATCH v2 26/28 Allow setting control channel packet size with tls-mtu
In reply to Arne SchwabeCurrently control packet size is controlled by tun-mtu in a very non-obvious way since the control overhead is not taken into account and control channel packet will end up with a different size than data channel packet.
Instead we decouple this and introduce tls-mtu which defaults to 1250.
Patch v2: rebase on latest patch set
--- Changes.rst | 8 ++++++++ doc/man-sections/link-options.rst | 7 +++++++ src/openvpn/init.c | 8 ++++++-- src/openvpn/mtu.h | 5 +++++ src/openvpn/options.c | 14 ++++++++++++++ src/openvpn/options.h | 1 + src/openvpn/ssl.c | 25 +++++++++++++------------ src/openvpn/ssl.h | 8 +++----- 8 files changed, 57 insertions(+), 19 deletions(-)
Changes.rst +8 -0
@@ -79,6 +79,12 @@ Cookie based handshake for UDP server shake. The tls-crypt-v2 option allows controlling if older clients are accepted. +Improved control channel packet size control (``--tls-mtu``) + The size of control channel is no longer tied to + ``--link-mtu``/``--tun-mtu`` and can be set using ``--tls-mtu``. Setting + the size to small sizes no longer breaks the OpenVPN protocol in certain + situation. + Deprecated features ------------------- ``inetd`` has been removed
@@ -141,6 +147,8 @@ User-visible Changes - Option ``--nobind`` is default when ``--client`` or ``--pull`` is used in the configuration - :code:`link_mtu` parameter is removed from environment or replaced with 0 when scripts are called with parameters. This parameter is unreliable and no longer internally calculated. +- control channel packet maximum size is no longer influenced by ``--link-mtu``/``--tun-mtu`` + and must be set by ``--tls-mtu`` now. Overview of changes in 2.5 ==========================
doc/man-sections/link-options.rst +7 -0
@@ -454,3 +454,10 @@ the local and the remote host. if mode server: socket-flags TCP_NODELAY push "socket-flags TCP_NODELAY" + +--tls-mtu size + This option sets the maximum size for control channel packets. OpenVPN will + try to keep its control channel messages below this size but due to some + constraints in the protocol this is not always possible. If the option is + not set, it default to 1250. Valid sizes are between 512 and 2048. + The maximum packet size includes encapsalution overhead like UDP and IP.
src/openvpn/init.c +6 -2
@@ -2451,6 +2451,10 @@ frame_finalize_options(struct context *c, const struct options *o)* space */ size_t payload_size =max_int (1500 , frame->tun_mtu); +/* we need to be also large enough to hold larger control channel packets +* if configured */ + payload_size =max_int (payload_size, o->ce.tls_mtu); +/* The extra tun needs to be added to the payload size */ if (o->ce.tun_mtu_defined) {
@@ -2987,7 +2991,7 @@ do_init_frame_tls(struct context *c) {if (c->c2.tls_multi) { -tls_multi_init_finalize (c->c2.tls_multi, &c->c2.frame); +tls_multi_init_finalize (c->c2.tls_multi, c->options.ce.tls_mtu);ASSERT (c->c2.tls_multi->opt.frame.buf.payload_size <= c->c2.frame.buf.payload_size); frame_print(&c->c2.tls_multi->opt.frame, D_MTU_INFO,
@@ -2995,7 +2999,7 @@ do_init_frame_tls(struct context *c) }if (c->c2.tls_auth_standalone) { -tls_init_control_channel_frame_parameters (&c->c2.frame, &c->c2.tls_auth_standalone->frame); +tls_init_control_channel_frame_parameters (&c->c2.tls_auth_standalone->frame, c->options.ce.tls_mtu);frame_print (&c->c2.tls_auth_standalone->frame,D_MTU_INFO ,"TLS-Auth MTU parms" ); c->c2.tls_auth_standalone->tls_wrap.work =alloc_buf_gc (BUF_SIZE (&c->c2.frame), &c->c2.gc);
src/openvpn/mtu.h +5 -0
@@ -79,6 +79,11 @@*/ #define MSSFIX_DEFAULT 1492 +/* +* Default maximum size of control channel packets +*/ +#define TLS_MTU_DEFAULT 1250 +/* * Alignment of payload data such as IP packet or * ethernet frame.
src/openvpn/options.c +14 -0
@@ -815,6 +815,7 @@ init_options(struct options *o, const bool init_gc) o->ce.bind_local = true; o->ce.tun_mtu =TUN_MTU_DEFAULT ; o->ce.link_mtu =LINK_MTU_DEFAULT ; + o->ce.tls_mtu =TLS_MTU_DEFAULT ; o->ce.mtu_discover_type =-1 ; o->ce.mssfix =0 ; o->ce.mssfix_default = true;
@@ -1582,6 +1583,7 @@ show_connection_entry(const struct connection_entry *o)SHOW_BOOL (link_mtu_defined);SHOW_INT (tun_mtu_extra);SHOW_BOOL (tun_mtu_extra_defined); +SHOW_INT (tls_mtu);SHOW_INT (mtu_discover_type);
@@ -6281,6 +6283,18 @@ add_option(struct options *options, options->ce.tun_mtu_extra =positive_atoi (p[1 ]); options->ce.tun_mtu_extra_defined = true; } +else if (streq (p[0 ],"tls-mtu" ) && p[1 ] && !p[2 ]) + { +VERIFY_PERMISSION (OPT_P_MTU |OPT_P_CONNECTION ); +int tls_mtu =atoi (p[1 ]); +if (tls_mtu <512 || tls_mtu >TLS_CHANNEL_BUF_SIZE ) + { +msg (msglevel,"Bad tls-mtu value, must be between %d and %d" , +512 ,TLS_CHANNEL_BUF_SIZE ); + goto err; + } + options->ce.tls_mtu =positive_atoi (p[1 ]); + }#ifdef ENABLE_FRAGMENT else if (streq(p[0], "mtu-dynamic")) {
src/openvpn/options.h +1 -0
@@ -123,6 +123,7 @@ struct connection_entrybool tun_mtu_extra_defined;int link_mtu;/* MTU of device over which tunnel packets pass via TCP/UDP */ bool link_mtu_defined;/* true if user overriding parm with command line option */ +int tls_mtu;/* Maximum MTU for the control channel messages */ /* Advanced MTU negotiation and datagram fragmentation options */ int mtu_discover_type;/* used if OS supports setting Path MTU discovery options on socket */
src/openvpn/ssl.c +13 -12
@@ -296,8 +296,7 @@ tls_limit_reneg_bytes(const char *ciphername, int *reneg_bytes) }void -tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, -struct frame *frame) +tls_init_control_channel_frame_parameters (struct frame *frame,int tls_mtu) {/* * frame->extra_frame is already initialized with tls_auth buffer requirements,
@@ -322,18 +321,20 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame/* Previous OpenVPN version calculated the maximum size and buffer of a * control frame depending on the overhead of the data channel frame -* overhead and limited its maximum size to 1250. We always allocate the -* TLS_CHANNEL_BUF_SIZE buffer size since a lot of code blindly assumes -* a large buffer (e.g. PUSH_BUNDLE_SIZE) and also our peer might have -* a higher size configured and we still want to be able to receive the -* packets. frame->mtu_mtu is set as suggestion for the maximum packet -* size */ - frame->buf.payload_size =1250 + overhead; +* overhead and limited its maximum size to 1250. Since control frame +* frames also need to fit into data channel buffer we have the same +* default of 1500 + 100 as data channel buffers have. Increasing +* tls-mtu beyond this limit also increases the data channel buffers */ + frame->buf.payload_size =max_int (1500 , tls_mtu) +100 ; frame->buf.headroom = overhead; frame->buf.tailroom = overhead; - frame->tun_mtu =min_int (data_channel_frame->tun_mtu,1250 ); + frame->tun_mtu = tls_mtu; + +/* Ensure the tun-mtu stays in a valid range */ + frame->tun_mtu =min_int (frame->tun_mtu,TLS_CHANNEL_BUF_SIZE ); + frame->tun_mtu =max_int (frame->tun_mtu,512 ); }/**
@@ -1299,9 +1300,9 @@ tls_multi_init(struct tls_options *tls_options) }void -tls_multi_init_finalize (struct tls_multi *multi,const struct frame *frame) +tls_multi_init_finalize (struct tls_multi *multi,int tls_mtu) { -tls_init_control_channel_frame_parameters (frame, &multi->opt.frame); +tls_init_control_channel_frame_parameters (&multi->opt.frame, tls_mtu);/* initialize the active and untrusted sessions */ tls_session_init (multi, &multi->session[TM_ACTIVE ]);
src/openvpn/ssl.h +3 -5
@@ -156,10 +156,9 @@ struct tls_multi *tls_multi_init(struct tls_options *tls_options);* * @param multi - The \c tls_multi structure of which to finalize * initialization. -* @param frame - The data channel's \c frame structure. +* @param tls_mtu - maximum allowed size for control channel packets */ -void tls_multi_init_finalize (struct tls_multi *multi, -const struct frame *frame); +void tls_multi_init_finalize (struct tls_multi *multi,int tls_mtu);/* * Initialize a standalone tls-auth verification object.
@@ -171,8 +170,7 @@ struct tls_auth_standalone *tls_auth_standalone_init(struct tls_options *tls_opt* Setups the control channel frame size parameters from the data channel * parameters */ -void tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, -struct frame *frame); +void tls_init_control_channel_frame_parameters (struct frame *frame,int tls_mtu);/* * Set local and remote option compatibility strings.
Signature · 1 line
2.32.0 (Apple Git-132)
Re: [Openvpn-devel] [PATCH v2 25/28] Ensure that control channel packet are respecting tls-mtu
In reply to Arne SchwabeTypo/grammar fixes only for now.
Quoted 2 lines
Arne Schwabe <[email protected]...> hat am 10.05.2022 19:04 geschrieben:
This ensure that control packets are actually smaller than tls-mtu.
"ensures"
Quoted 7 lines
Since OpenVPN will consider a control message packet complete when the TLS record is complete, we have to ensure that the SSL library will still write one record, so the receiving side will only be able to get/read the control message content when a TLS record is complete. To achieve this goal, this commit does:
- Splitting one read from TLS library into multiple control
"split" to be consistent with other bullet points.
Quoted 4 lines
channel packets, splitting one TLS record into multiple control packets.
- increase allowed number of outstanding packets to 6 from 4 on the sender side. This is still okay with older implementation as
"implementations"
Quoted 6 lines
receivers will have room for 8.
- calculate the overhead for control channel message to allow staying below that threshold.
- remove maxlen fromkey_state_read_ciphertextand related functions as we now always allow control channel messages to be up toTLS_CHANNEL_BUF_SIZEin size and longer limit this by the mtu of
"no longer"
Quoted 4 lines
control packets as the implemented splitting will take care of larger payloads from the SSL library
Patch v2: avoid assertion about to large buffer by sticking to 1250 max control size
"too"
Quoted 2 lines
in this commit and leaving larger sizes for the --tls-mtu commit. Also fix various other small problems and grammer fixes.
"grammar"
[...]
src/openvpn/reliable.c reliable_get_buf(struct reliable *rel) · line 492
Quoted 5 lines
src/openvpn/reliable.c +1
-0@@ -496,6 +489,38 @@ reliable_get_buf(struct reliable *rel)return NULL ; } +/* Counts the number of free buffers in output that can be potientially used
"potentially"
Quoted 4 lines
src/openvpn/reliable.c +4
-0+* for sending */ +int +reliable_get_num_output_sequenced_available (struct reliable *rel) +{
[...]
Quoted 4 lines
src/openvpn/reliable.h +0
-0
[...]
src/openvpn/reliable.h void reliable_mark_deleted(struct reliable *rel, struct buffer *buf); · line 404
Quoted 7 lines
src/openvpn/reliable.h +3
-0@@ -385,6 +399,20 @@ void reliable_mark_deleted(struct reliable *rel, struct buffer *buf);*/ struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel); + +/** +* Counts the number of free buffers in output that can be potientially used
"potentially"
Quoted 18 lines
src/openvpn/reliable.h +11
-0+* for sending +* +* @param rel The reliable structure in which to search for a free +* entry. +* +* @return the number of buffer that are available for sending without +* breaking ack sequence +* */ +int +reliable_get_num_output_sequenced_available (struct reliable *rel); +/** * Mark the reliable entry associated with the given buffer as * active outgoing. src/openvpn/ssl.c +0
-0
[...]
src/openvpn/ssl.c tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame · line 340
Quoted 6 lines
src/openvpn/ssl.c +2
-0@@ -334,6 +336,47 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame frame->tun_mtu =min_int (data_channel_frame->tun_mtu,1250 ); } +/** +* calculate the maximum overhead that control channel frames have
"."
Quoted 6 lines
src/openvpn/ssl.c +6
-0+* This includes header, op code and everything apart from the +* payload itself. This method is a bit pessimistic and might give higher +* overhead than we actually have */ +static int +calc_control_channel_frame_overhead (const struct tls_session *session) +{
[...]
Quoted 29 lines
src/openvpn/ssl.c +25
-0@@ -2638,6 +2681,91 @@ read_incoming_tls_plaintext(struct key_state *ks, struct buffer *buf,return true; } +static bool +write_outgoing_tls_ciphertext (struct tls_session *session,bool *state_change) +{ +struct key_state *ks = &session->key[KS_PRIMARY ]; + +int rel_avail =reliable_get_num_output_sequenced_available (ks->send_reliable); +if (rel_avail ==0 ) + { +return true; + } + +/* We need to determine how much space is actually available in the control +* channel frame */ + +int max_pkt_len =min_int (TLS_CHANNEL_BUF_SIZE , session->opt->frame.tun_mtu); + + +/* Subtract overhead */ + max_pkt_len -=calc_control_channel_frame_overhead (session); + +/* calculate total available length for outgoing tls ciphertext */ +int maxlen = max_pkt_len * rel_avail; + +/* Is first packet one that will have a WKC appended? */ +if (control_packet_needs_wkc (ks))
src/openvpn/ssl.c read_incoming_tls_plaintext(struct key_state *ks, struct buffer *buf, · line 2718
Quoted 10 lines
src/openvpn/ssl.c +10
-0+ { + maxlen -=buf_len (session->tls_wrap.tls_crypt_v2_wkc); + } + +/* Not enough space available to send a full control channel packet */ +if (maxlen <TLS_CHANNEL_BUF_SIZE ) + { +if (rel_avail ==TLS_RELIABLE_N_SEND_BUFFERS ) + { +msg (D_TLS_ERRORS ,"--tls-mtu setting to low. Unable to send TLS packets" );
"too"
--tls-mtu is not actually defined, yet. But not sure whether it is worth fixing this just to restore the text in the next commit.
src/openvpn/ssl.c read_incoming_tls_plaintext(struct key_state *ks, struct buffer *buf, · line 2722
Quoted 4 lines
src/openvpn/ssl.c +4
-0+ } +msg (D_REL_LOW ,"Reliable: Send queue full, postponing TLS send" ); +return true; + }
[...]
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH v2 26/28] Allow setting control channel packet size with tls-mtu
In reply to Arne SchwabeQuoted 1 line
Arne Schwabe <[email protected]...> hat am 10.05.2022 19:07 geschrieben:
[...]
Changes.rst Cookie based handshake for UDP server · line 86
Quoted 13 lines
Changes.rst +5
-0@@ -79,6 +79,12 @@ Cookie based handshake for UDP server shake. The tls-crypt-v2 option allows controlling if older clients are accepted. +Improved control channel packet size control (``--tls-mtu``) + The size of control channel is no longer tied to + ``--link-mtu``/``--tun-mtu`` and can be set using ``--tls-mtu``. Setting + the size to small sizes no longer breaks the OpenVPN protocol in certain + situation.
"situations"
Quoted 18 lines
Changes.rst +3
-0+ Deprecated features ------------------- ``inetd`` has been removed@@ -141,6 +147,8 @@ User-visible Changes - Option ``--nobind`` is default when ``--client`` or ``--pull`` is used in the configuration - :code:`link_mtu` parameter is removed from environment or replaced with 0 when scripts are called with parameters. This parameter is unreliable and no longer internally calculated. +- control channel packet maximum size is no longer influenced by ``--link-mtu``/``--tun-mtu`` + and must be set by ``--tls-mtu`` now. Overview of changes in 2.5 ==========================doc/man-sections/link-options.rst +0
-0
doc/man-sections/link-options.rst the local and the remote host. · line 463
Quoted 10 lines
doc/man-sections/link-options.rst +7
-0@@ -454,3 +454,10 @@ the local and the remote host. if mode server: socket-flags TCP_NODELAY push "socket-flags TCP_NODELAY" + +--tls-mtu size + This option sets the maximum size for control channel packets. OpenVPN will + try to keep its control channel messages below this size but due to some + constraints in the protocol this is not always possible. If the option is + not set, it default to 1250. Valid sizes are between 512 and 2048. + The maximum packet size includes encapsalution overhead like UDP and IP.
"encapsulation"
Quoted 1 line
\ No newline at end of file
[...]
doc/man-sections/link-options.rst add_option(struct options *options, · line 6293
Quoted 12 lines
doc/man-sections/link-options.rst +8
-0@@ -6281,6 +6283,18 @@ add_option(struct options *options, options->ce.tun_mtu_extra = positive_atoi(p[1]); options->ce.tun_mtu_extra_defined = true; } + else if (streq(p[0], "tls-mtu") && p[1] && !p[2]) + { + VERIFY_PERMISSION(OPT_P_MTU|OPT_P_CONNECTION); + int tls_mtu = atoi(p[1]); + if (tls_mtu < 512 || tls_mtu > TLS_CHANNEL_BUF_SIZE) + { + msg(msglevel, "Bad tls-mtu value, must be between %d and %d", + 512, TLS_CHANNEL_BUF_SIZE);
Would it be useful to have a define for the 512 as well?
Quoted 7 lines
doc/man-sections/link-options.rst +4
-0+ goto err; + } + options->ce.tls_mtu = positive_atoi(p[1]); + } #ifdef ENABLE_FRAGMENT else if (streq(p[0], "mtu-dynamic")) {
[...]
Quoted 30 lines
src/openvpn/ssl.c +6
-9@@ -296,8 +296,7 @@ tls_limit_reneg_bytes(const char *ciphername, int *reneg_bytes) }void -tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, -struct frame *frame) +tls_init_control_channel_frame_parameters (struct frame *frame,int tls_mtu) {/* * frame->extra_frame is already initialized with tls_auth buffer requirements, @@ -322,18 +321,20 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame/* Previous OpenVPN version calculated the maximum size and buffer of a * control frame depending on the overhead of the data channel frame -* overhead and limited its maximum size to 1250. We always allocate the -* TLS_CHANNEL_BUF_SIZE buffer size since a lot of code blindly assumes -* a large buffer (e.g. PUSH_BUNDLE_SIZE) and also our peer might have -* a higher size configured and we still want to be able to receive the -* packets. frame->mtu_mtu is set as suggestion for the maximum packet -* size */ - frame->buf.payload_size =1250 + overhead; +* overhead and limited its maximum size to 1250. Since control frame +* frames also need to fit into data channel buffer we have the same +* default of 1500 + 100 as data channel buffers have. Increasing +* tls-mtu beyond this limit also increases the data channel buffers */ + frame->buf.payload_size =max_int (1500 , tls_mtu) +100 ;
src/openvpn/ssl.c tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame · line 337
Quoted 10 lines
src/openvpn/ssl.c +5
-1frame->buf.headroom = overhead; frame->buf.tailroom = overhead; - frame->tun_mtu =min_int (data_channel_frame->tun_mtu,1250 ); + frame->tun_mtu = tls_mtu; + +/* Ensure the tun-mtu stays in a valid range */ + frame->tun_mtu =min_int (frame->tun_mtu,TLS_CHANNEL_BUF_SIZE ); + frame->tun_mtu =max_int (frame->tun_mtu,512 );
Is this 512 the same 512 as above? If yes, then I feel a define would definitely be a good idea.
src/openvpn/ssl.c tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame · line 340
Quoted 3 lines
src/openvpn/ssl.c +0
-0}/**
[...]
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 27/28] Add unit test for reliable_get_num_output_sequenced_available
In reply to Arne SchwabeNot sure whether it should be concerning that reliable_get_num_output_sequenced_available is happy to give results given the complete garbage we put into the test buffer. But anyway, an UT is better than no UT and the test code looks correct to me.
Obviously depends on previous patches.
Regards,
Frank
Quoted 9 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
--- tests/unit_tests/openvpn/Makefile.am | 5 +- tests/unit_tests/openvpn/mock_get_random.c | 10 ++++ tests/unit_tests/openvpn/test_packet_id.c | 55 ++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-)
--
Frank Lichtenheld
PATCH v3 26/28 Allow setting control channel packet size with tls-mtu
In reply to Arne SchwabeCurrently control packet size is controlled by tun-mtu in a very non-obvious way since the control overhead is not taken into account and control channel packet will end up with a different size than data channel packet.
Instead we decouple this and introduce tls-mtu which defaults to 1250.
Patch v2: rebase on latest patch set
Patch v3: Introduce TLS_CHANNEL_MTU_MIN define and give explaination
of its value.
--- Changes.rst | 8 ++++++++ doc/man-sections/link-options.rst | 7 +++++++ src/openvpn/common.h | 10 ++++++++++ src/openvpn/init.c | 8 ++++++-- src/openvpn/mtu.h | 5 +++++ src/openvpn/options.c | 14 ++++++++++++++ src/openvpn/options.h | 1 + src/openvpn/ssl.c | 25 +++++++++++++------------ src/openvpn/ssl.h | 8 +++----- 9 files changed, 67 insertions(+), 19 deletions(-)
Changes.rst +8 -0
@@ -79,6 +79,12 @@ Cookie based handshake for UDP server shake. The tls-crypt-v2 option allows controlling if older clients are accepted. +Improved control channel packet size control (``--tls-mtu``) + The size of control channel is no longer tied to + ``--link-mtu``/``--tun-mtu`` and can be set using ``--tls-mtu``. Setting + the size to small sizes no longer breaks the OpenVPN protocol in certain + situations. + Deprecated features ------------------- ``inetd`` has been removed
@@ -141,6 +147,8 @@ User-visible Changes - Option ``--nobind`` is default when ``--client`` or ``--pull`` is used in the configuration - :code:`link_mtu` parameter is removed from environment or replaced with 0 when scripts are called with parameters. This parameter is unreliable and no longer internally calculated. +- control channel packet maximum size is no longer influenced by ``--link-mtu``/``--tun-mtu`` + and must be set by ``--tls-mtu`` now. Overview of changes in 2.5 ==========================
doc/man-sections/link-options.rst +7 -0
@@ -454,3 +454,10 @@ the local and the remote host. if mode server: socket-flags TCP_NODELAY push "socket-flags TCP_NODELAY" + +--tls-mtu size + This option sets the maximum size for control channel packets. OpenVPN will + try to keep its control channel messages below this size but due to some + constraints in the protocol this is not always possible. If the option is + not set, it default to 1250. Valid sizes are between 512 and 2048. + The maximum packet size includes encapsulation overhead like UDP and IP.
src/openvpn/common.h +10 -0
@@ -68,6 +68,16 @@ typedef unsigned long ptr_type;*/ #define TLS_CHANNEL_BUF_SIZE 2048 +/* TLS control buffer minimum size, this size is not actually inherent to +* the protocol but. Our current sending window is 6 and the receive window +* is 8 or 12 depending on the OpenVPN version. We need to be able to send +* a TLS record of size TLS_CHANNEL_BUF_SIZE. Splitting this into more than +* 6 packets (with overhead) would complicate our sending logic a lot more, so +* we settle here for a "round" number that allow with overhead of ~100 bytes +* to be larger than TLS_CHANNEL_BUF_SIZE. E.g. 6x ~400 > 2048. +* */ +#define TLS_CHANNEL_MTU_MIN 512 +/* * This parameter controls the maximum size of a bundle * of pushed options.
src/openvpn/init.c +6 -2
@@ -2451,6 +2451,10 @@ frame_finalize_options(struct context *c, const struct options *o)* space */ size_t payload_size =max_int (1500 , frame->tun_mtu); +/* we need to be also large enough to hold larger control channel packets +* if configured */ + payload_size =max_int (payload_size, o->ce.tls_mtu); +/* The extra tun needs to be added to the payload size */ if (o->ce.tun_mtu_defined) {
@@ -2987,7 +2991,7 @@ do_init_frame_tls(struct context *c) {if (c->c2.tls_multi) { -tls_multi_init_finalize (c->c2.tls_multi, &c->c2.frame); +tls_multi_init_finalize (c->c2.tls_multi, c->options.ce.tls_mtu);ASSERT (c->c2.tls_multi->opt.frame.buf.payload_size <= c->c2.frame.buf.payload_size); frame_print(&c->c2.tls_multi->opt.frame, D_MTU_INFO,
@@ -2995,7 +2999,7 @@ do_init_frame_tls(struct context *c) }if (c->c2.tls_auth_standalone) { -tls_init_control_channel_frame_parameters (&c->c2.frame, &c->c2.tls_auth_standalone->frame); +tls_init_control_channel_frame_parameters (&c->c2.tls_auth_standalone->frame, c->options.ce.tls_mtu);frame_print (&c->c2.tls_auth_standalone->frame,D_MTU_INFO ,"TLS-Auth MTU parms" ); c->c2.tls_auth_standalone->tls_wrap.work =alloc_buf_gc (BUF_SIZE (&c->c2.frame), &c->c2.gc);
src/openvpn/mtu.h +5 -0
@@ -79,6 +79,11 @@*/ #define MSSFIX_DEFAULT 1492 +/* +* Default maximum size of control channel packets +*/ +#define TLS_MTU_DEFAULT 1250 +/* * Alignment of payload data such as IP packet or * ethernet frame.
src/openvpn/options.c +14 -0
@@ -815,6 +815,7 @@ init_options(struct options *o, const bool init_gc) o->ce.bind_local = true; o->ce.tun_mtu =TUN_MTU_DEFAULT ; o->ce.link_mtu =LINK_MTU_DEFAULT ; + o->ce.tls_mtu =TLS_MTU_DEFAULT ; o->ce.mtu_discover_type =-1 ; o->ce.mssfix =0 ; o->ce.mssfix_default = true;
@@ -1582,6 +1583,7 @@ show_connection_entry(const struct connection_entry *o)SHOW_BOOL (link_mtu_defined);SHOW_INT (tun_mtu_extra);SHOW_BOOL (tun_mtu_extra_defined); +SHOW_INT (tls_mtu);SHOW_INT (mtu_discover_type);
@@ -6281,6 +6283,18 @@ add_option(struct options *options, options->ce.tun_mtu_extra =positive_atoi (p[1 ]); options->ce.tun_mtu_extra_defined = true; } +else if (streq (p[0 ],"tls-mtu" ) && p[1 ] && !p[2 ]) + { +VERIFY_PERMISSION (OPT_P_MTU |OPT_P_CONNECTION ); +int tls_mtu =atoi (p[1 ]); +if (tls_mtu <TLS_CHANNEL_MTU_MIN || tls_mtu >TLS_CHANNEL_BUF_SIZE ) + { +msg (msglevel,"Bad tls-mtu value, must be between %d and %d" , +TLS_CHANNEL_MTU_MIN ,TLS_CHANNEL_BUF_SIZE ); + goto err; + } + options->ce.tls_mtu =positive_atoi (p[1 ]); + }#ifdef ENABLE_FRAGMENT else if (streq(p[0], "mtu-dynamic")) {
src/openvpn/options.h +1 -0
@@ -123,6 +123,7 @@ struct connection_entrybool tun_mtu_extra_defined;int link_mtu;/* MTU of device over which tunnel packets pass via TCP/UDP */ bool link_mtu_defined;/* true if user overriding parm with command line option */ +int tls_mtu;/* Maximum MTU for the control channel messages */ /* Advanced MTU negotiation and datagram fragmentation options */ int mtu_discover_type;/* used if OS supports setting Path MTU discovery options on socket */
src/openvpn/ssl.c +13 -12
@@ -296,8 +296,7 @@ tls_limit_reneg_bytes(const char *ciphername, int *reneg_bytes) }void -tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, -struct frame *frame) +tls_init_control_channel_frame_parameters (struct frame *frame,int tls_mtu) {/* * frame->extra_frame is already initialized with tls_auth buffer requirements,
@@ -322,18 +321,20 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame/* Previous OpenVPN version calculated the maximum size and buffer of a * control frame depending on the overhead of the data channel frame -* overhead and limited its maximum size to 1250. We always allocate the -* TLS_CHANNEL_BUF_SIZE buffer size since a lot of code blindly assumes -* a large buffer (e.g. PUSH_BUNDLE_SIZE) and also our peer might have -* a higher size configured and we still want to be able to receive the -* packets. frame->mtu_mtu is set as suggestion for the maximum packet -* size */ - frame->buf.payload_size =1250 + overhead; +* overhead and limited its maximum size to 1250. Since control frame +* frames also need to fit into data channel buffer we have the same +* default of 1500 + 100 as data channel buffers have. Increasing +* tls-mtu beyond this limit also increases the data channel buffers */ + frame->buf.payload_size =max_int (1500 , tls_mtu) +100 ; frame->buf.headroom = overhead; frame->buf.tailroom = overhead; - frame->tun_mtu =min_int (data_channel_frame->tun_mtu,1250 ); + frame->tun_mtu = tls_mtu; + +/* Ensure the tun-mtu stays in a valid range */ + frame->tun_mtu =min_int (frame->tun_mtu,TLS_CHANNEL_BUF_SIZE ); + frame->tun_mtu =max_int (frame->tun_mtu,TLS_CHANNEL_MTU_MIN ); }/**
@@ -1299,9 +1300,9 @@ tls_multi_init(struct tls_options *tls_options) }void -tls_multi_init_finalize (struct tls_multi *multi,const struct frame *frame) +tls_multi_init_finalize (struct tls_multi *multi,int tls_mtu) { -tls_init_control_channel_frame_parameters (frame, &multi->opt.frame); +tls_init_control_channel_frame_parameters (&multi->opt.frame, tls_mtu);/* initialize the active and untrusted sessions */ tls_session_init (multi, &multi->session[TM_ACTIVE ]);
src/openvpn/ssl.h +3 -5
@@ -156,10 +156,9 @@ struct tls_multi *tls_multi_init(struct tls_options *tls_options);* * @param multi - The \c tls_multi structure of which to finalize * initialization. -* @param frame - The data channel's \c frame structure. +* @param tls_mtu - maximum allowed size for control channel packets */ -void tls_multi_init_finalize (struct tls_multi *multi, -const struct frame *frame); +void tls_multi_init_finalize (struct tls_multi *multi,int tls_mtu);/* * Initialize a standalone tls-auth verification object.
@@ -171,8 +170,7 @@ struct tls_auth_standalone *tls_auth_standalone_init(struct tls_options *tls_opt* Setups the control channel frame size parameters from the data channel * parameters */ -void tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, -struct frame *frame); +void tls_init_control_channel_frame_parameters (struct frame *frame,int tls_mtu);/* * Set local and remote option compatibility strings.
Signature · 1 line
2.32.0 (Apple Git-132)
Re: [Openvpn-devel] [PATCH v3 26/28] Allow setting control channel packet size with tls-mtu
Quoted 10 lines
Arne Schwabe <[email protected]...> hat am 11.05.2022 13:08 geschrieben:
Currently control packet size is controlled by tun-mtu in a very non-obvious way since the control overhead is not taken into account and control channel packet will end up with a different size than data channel packet.
Quoted 6 lines
Instead we decouple this and introduce tls-mtu which defaults to 1250.
Patch v2: rebase on latest patch set
Patch v3: IntroduceTLS_CHANNEL_MTU_MINdefine and give explaination
of its value.
"explanation"
Quoted 14 lines
Signed-off-by Arne Schwabe <[email protected]...>--- Changes.rst | 8 ++++++++ doc/man-sections/link-options.rst | 7 +++++++ src/openvpn/common.h | 10 ++++++++++ src/openvpn/init.c | 8 ++++++-- src/openvpn/mtu.h | 5 +++++ src/openvpn/options.c | 14 ++++++++++++++ src/openvpn/options.h | 1 + src/openvpn/ssl.c | 25 +++++++++++++------------ src/openvpn/ssl.h | 8 +++----- 9 files changed, 67 insertions(+), 19 deletions(-)
[...]
src/openvpn/common.h typedef unsigned long ptr_type; · line 72
Quoted 10 lines
src/openvpn/common.h +2
-0@@ -68,6 +68,16 @@ typedef unsigned long ptr_type;*/ #define TLS_CHANNEL_BUF_SIZE 2048 +/* TLS control buffer minimum size, this size is not actually inherent to +* the protocol but. Our current sending window is 6 and the receive window
Maybe "the protocol. However, our" ?
src/openvpn/common.h typedef unsigned long ptr_type; · line 76
Quoted 4 lines
src/openvpn/common.h +4
-0+* is 8 or 12 depending on the OpenVPN version. We need to be able to send +* a TLS record of size TLS_CHANNEL_BUF_SIZE. Splitting this into more than +* 6 packets (with overhead) would complicate our sending logic a lot more, so +* we settle here for a "round" number that allow with overhead of ~100 bytes
remove "allow"
src/openvpn/common.h typedef unsigned long ptr_type; · line 77
Quoted 1 line
src/openvpn/common.h +1
-0+* to be larger than TLS_CHANNEL_BUF_SIZE. E.g. 6x ~400 > 2048.
"to be" -> "is"
src/openvpn/common.h typedef unsigned long ptr_type; · line 83
Quoted 6 lines
src/openvpn/common.h +3
-0+* */ +#define TLS_CHANNEL_MTU_MIN 512 +/* * This parameter controls the maximum size of a bundle * of pushed options.
[...]
Regards,
--
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH 28/28] Always include ACKs for the last seen control packets
In reply to Arne SchwabeI'm convinced that this does what it is supposed to do. Some typo fixes and one potential improvement to the UT noted below.
Quoted 3 lines
Arne Schwabe <[email protected]...> hat am 22.04.2022 16:29 geschrieben:
This adds an LRU cache for the last seen packets from the peer to send acks to all recently packets. This also packets to be acknowledged even if a single
"recent"? Or "recently received"?
"This also packets" -> "This leads to packets" ? Still not pretty but at least somewhat readable.
Quoted 14 lines
P_ACK_V1gets lost, avoiding retransmissions. The downside is that we add up to 28 byte to anP_ACK_V1(7*packet_id) and up to 24 bytes to other control channel packets (4*packet_id+ peer session id). However these small increases in packet size are a small price to pay for increased reliability.Currently OpenVPN will only send the absolute minimum of ACK messages. A single lost ACK message will trigger a resend from the peer and another ACK message.
src/openvpn/reliable.c +0
-0@@ -213,10 +213,56 @@ reliable_ack_parse(struct buffer *buf, struct reliable_ack *ack,return true;
src/openvpn/reliable.c reliable_ack_parse(struct buffer *buf, struct reliable_ack *ack, · line 223
Quoted 10 lines
src/openvpn/reliable.c +8
-0} +/** +* Copies the first n acks from \c ack to \c ack_lru +*/ +void +copy_acks_to_lru (struct reliable_ack *ack,struct reliable_ack *ack_lru,int n) +{ +ASSERT (ack->len >= n); +/* This loops is backward to ensure the same order as in ack */
Either "loop" or "loops backwards"
Quoted 4 lines
src/openvpn/reliable.c +4
-0+for (int i = n-1 ; i >=0 ; i--) + { +packet_id_type id = ack->packet_id[i]; +
src/openvpn/reliable.c reliable_ack_parse(struct buffer *buf, struct reliable_ack *ack, · line 237
Quoted 10 lines
src/openvpn/reliable.c +10
-0+/* Handle special case of ack_lru empty */ +if (ack_lru->len ==0 ) + { + ack_lru->len =1 ; + ack_lru->packet_id[0 ] = id; + } + +bool idfound = false; + +/* Move all existing entry one to the right */
"entries"
Quoted 31 lines
src/openvpn/reliable.c +24
-0+packet_id_type move = id; + +for (int j =0 ; j < ack_lru->len; j++) + { +packet_id_type tmp = ack_lru->packet_id[j]; + ack_lru->packet_id[j] = move; + move = tmp; + +if (move == id) + { + idfound = true; +break ; + } + } + +if (!idfound && ack_lru->len <RELIABLE_ACK_SIZE ) + { + ack_lru->packet_id[ack_lru->len] = move; + ack_lru->len++; + } + } +} +/* write a packet ID acknowledgement record to buf, */ /* removing all acknowledged entries from ack */ bool reliable_ack_write (struct reliable_ack *ack, +struct reliable_ack *ack_lru,struct buffer *buf,const struct session_id *sid,int max,bool prepend) {
[...]
Quoted 4 lines
src/openvpn/reliable.h +0
-0
[...]
src/openvpn/reliable.h bool reliable_ack_acknowledge_packet_id(struct reliable_ack *ack, packet_id_type · line 383
Quoted 11 lines
src/openvpn/reliable.h +7
-0@@ -370,6 +374,19 @@ bool reliable_ack_acknowledge_packet_id(struct reliable_ack *ack, packet_id_type*/ struct reliable_entry *reliable_get_entry_sequenced (struct reliable *rel); + + +/** +* Copies the first n acks from \c ack to \c ack_lru +* +* @param ack The reliable structure to copy the acks from +* @param ack_lru the reliable structure to insert the acks into
"The" for consistency
Quoted 9 lines
src/openvpn/reliable.h +6
-0+* @param n The number of ACKS to copy +*/ +void +copy_acks_to_lru (struct reliable_ack *ack,struct reliable_ack *ack_lru,int n); + +/** * Remove an entry from a reliable structure. *
[...]
Quoted 20 lines
tests/unit_tests/openvpn/test_packet_id.c +12
-0@@ -210,6 +210,39 @@ test_get_num_output_sequenced_available(void **state)reliable_free (rel); } + +static void +test_copy_acks_to_lru (void **state) +{ +struct reliable_ack ack = { .len =4 , .packet_id = {2 ,1 ,3 ,2 } }; + +struct reliable_ack lru_ack = {0 }; + +/* Test copying to empty ack structure */ +copy_acks_to_lru (&ack, &lru_ack,4 ); +assert_int_equal (lru_ack.len,3 ); +assert_int_equal (lru_ack.packet_id[0 ],2 );
tests/unit_tests/openvpn/test_packet_id.c test_get_num_output_sequenced_available(void **state) · line 234
Quoted 10 lines
tests/unit_tests/openvpn/test_packet_id.c +10
-0+assert_int_equal (lru_ack.packet_id[1 ],1 ); +assert_int_equal (lru_ack.packet_id[2 ],3 ); + +/* Copying again should not change the result */ +copy_acks_to_lru (&ack, &lru_ack,4 ); +assert_int_equal (lru_ack.len,3 ); +assert_int_equal (lru_ack.packet_id[0 ],2 ); +assert_int_equal (lru_ack.packet_id[1 ],1 ); +assert_int_equal (lru_ack.packet_id[2 ],3 ); +
I would suggest adding a call to copy_acks_to_lru where n is 0 and test that it leaves lru_ack unchanged.
AFAICT the current code is correct, but doesn't hurt to have a test.
Quoted 4 lines
tests/unit_tests/openvpn/test_packet_id.c +4
-0+struct reliable_ack ack2 = { .len =7 , .packet_id = {5 ,6 ,7 ,8 ,9 ,10 ,11 } }; + +/* Adding multiple acks tests if the a full array is handled correctly */ +copy_acks_to_lru (&ack2, &lru_ack,7 );
tests/unit_tests/openvpn/test_packet_id.c test_get_num_output_sequenced_available(void **state) · line 248
Quoted 10 lines
tests/unit_tests/openvpn/test_packet_id.c +7
-0+ +struct reliable_ack expected_ack = { .len =8 , .packet_id = {5 ,6 ,7 ,8 ,9 ,10 ,11 ,2 }}; +assert_int_equal (lru_ack.len, expected_ack.len); + +assert_memory_equal (lru_ack.packet_id, expected_ack.packet_id,sizeof (expected_ack.packet_id)); +} +int main (void ) {
Regards,
--
Frank Lichtenheld
PATCH v3 25/28 Ensure that control channel packet are respecting tls-mtu
In reply to Arne SchwabeThis ensures that control packets are actually smaller than tls-mtu. Since OpenVPN will consider a control message packet complete when the TLS record is complete, we have to ensure that the SSL library will still write one record, so the receiving side will only be able to get/read the control message content when a TLS record is complete. To achieve this goal, this commit does:
- Split one read from TLS library into multiple control channel packets, splitting one TLS record into multiple control packets.
- increase allowed number of outstanding packets to 6 from 4 on the sender side. This is still okay with older implementations as receivers will have room for 8.
- calculate the overhead for control channel message to allow staying below that threshold.
- remove maxlen from key_state_read_ciphertext and related functions as we now always allow control channel messages to be up to TLS_CHANNEL_BUF_SIZE in size and no longer limit this by the mtu of control packets as the implemented splitting will take care of larger payloads from the SSL library
Patch v2: avoid assertion about to large buffer by sticking to 1250 max control size
in this commit and leaving larger sizes for the --tls-mtu commit. Also fix various other small problems and grammar fixes.
Patch v3: grammar fixes
--- src/openvpn/reliable.c | 55 ++++++++++---- src/openvpn/reliable.h | 32 +++++++- src/openvpn/ssl.c | 154 ++++++++++++++++++++++++++++++++------ src/openvpn/ssl_backend.h | 8 +- src/openvpn/ssl_mbedtls.c | 14 +--- src/openvpn/ssl_openssl.c | 16 ++-- src/openvpn/ssl_pkt.h | 4 +- 7 files changed, 214 insertions(+), 69 deletions(-)
src/openvpn/reliable.c +39 -16
@@ -41,6 +41,14 @@#include "memdbg.h" +/* calculates test - base while allowing for base or test wraparound. test is +* assumed to be higher than base */ +static inline packet_id_type +subtract_pid (const packet_id_type test,const packet_id_type base) +{ +return test - base; +} +/* * verify that test - base < extent while allowing for base or test wraparound */
@@ -49,22 +57,7 @@ reliable_pid_in_range1(const packet_id_type test, const packet_id_type base, const unsigned int extent) { - if (test >= base) - { - if (test - base < extent) - { -return true; - } - } - else - { -if ((test+0x80000000u ) - (base+0x80000000u ) < extent) - { -return true; - } - } - -return false; + return subtract_pid(test, base) < extent; }/*
@@ -498,6 +491,36 @@ reliable_get_buf(struct reliable *rel)return NULL ; } +int +reliable_get_num_output_sequenced_available (struct reliable *rel) +{ +struct gc_arena gc =gc_new (); +packet_id_type min_id =0 ; +bool min_id_defined = false; + +/* find minimum active packet_id */ +for (int i =0 ; i < rel->size; ++i) + { +const struct reliable_entry *e = &rel->array[i]; +if (e->active) + { +if (!min_id_defined ||reliable_pid_min (e->packet_id, min_id)) + { + min_id_defined = true; + min_id = e->packet_id; + } + } + } + +int ret = rel->size; +if (min_id_defined) + { + ret -=subtract_pid (rel->packet_id, min_id); + } +gc_free (&gc); +return ret; +} +/* grab a free buffer, fail if buffer clogged by unacknowledged low packet IDs */ struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel)
src/openvpn/reliable.h +30 -2
@@ -46,7 +46,7 @@* be stored in one \c reliable_ack * structure. */ -#define RELIABLE_CAPACITY 8/**< The maximum number of packets that +#define RELIABLE_CAPACITY 12/**< The maximum number of packets that * the reliability layer for one VPN * tunnel in one direction can store. */
@@ -93,7 +93,7 @@ struct reliableint size;interval_t initial_timeout;packet_id_type packet_id; -int offset; +int offset;/**< Offset of the bufs in the reliable_entry array */ bool hold;/* don't xmit until reliable_schedule_now is called */ struct reliable_entry array[RELIABLE_CAPACITY ]; };
@@ -178,6 +178,20 @@ reliable_ack_empty(struct reliable_ack *ack)return !ack->len; } +/** +* Returns the number of packets that need to be acked. +* +* @param ack The acknowledgment structure to check. +* +* @returns the number of outstanding acks +*/ +static inline int +reliable_ack_outstanding (struct reliable_ack *ack) +{ +return ack->len; +} + +/** * Write a packet ID acknowledgment record to a buffer. *
@@ -385,6 +399,20 @@ void reliable_mark_deleted(struct reliable *rel, struct buffer *buf);*/ struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel); + +/** +* Counts the number of free buffers in output that can be potentially used +* for sending +* +* @param rel The reliable structure in which to search for a free +* entry. +* +* @return the number of buffer that are available for sending without +* breaking ack sequence +* */ +int +reliable_get_num_output_sequenced_available (struct reliable *rel); +/** * Mark the reliable entry associated with the given buffer as * active outgoing.
src/openvpn/ssl.c +133 -21
193 lines not on this page · raw message
src/openvpn/ssl_backend.h +3 -5
@@ -461,7 +461,6 @@ int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl,* @param ks_ssl - The security parameter state for this %key * session. * @param buf - A buffer in which to store the ciphertext. -* @param maxlen - The maximum number of bytes to extract. * * @return The return value indicates whether the data was successfully * processed:
@@ -470,8 +469,8 @@ int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl,* later to retry. * - \c -1: An error occurred. */ -int key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen); +int key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf); +/** @} name Functions for packets to be sent to a remote OpenVPN peer */
@@ -517,8 +516,7 @@ int key_state_write_ciphertext(struct key_state_ssl *ks_ssl,* later to retry. * - \c -1: An error occurred. */ -int key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen); +int key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf);/** @} name Functions for packets received from a remote OpenVPN peer */
src/openvpn/ssl_mbedtls.c +2 -12
@@ -1285,8 +1285,7 @@ key_state_write_plaintext_const(struct key_state_ssl *ks, const uint8_t *data, i }int -key_state_read_ciphertext (struct key_state_ssl *ks,struct buffer *buf, -int maxlen) +key_state_read_ciphertext (struct key_state_ssl *ks,struct buffer *buf) {int retval =0 ;int len =0 ;
@@ -1304,10 +1303,6 @@ key_state_read_ciphertext(struct key_state_ssl *ks, struct buffer *buf, } len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - } retval =endless_buf_read (&ks->bio_ctx->out,BPTR (buf), len);
@@ -1388,8 +1383,7 @@ key_state_write_ciphertext(struct key_state_ssl *ks, struct buffer *buf) }int -key_state_read_plaintext (struct key_state_ssl *ks,struct buffer *buf, -int maxlen) +key_state_read_plaintext (struct key_state_ssl *ks,struct buffer *buf) {int retval =0 ;int len =0 ;
@@ -1407,10 +1401,6 @@ key_state_read_plaintext(struct key_state_ssl *ks, struct buffer *buf, } len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - } retval =mbedtls_ssl_read (ks->ctx,BPTR (buf), len);
src/openvpn/ssl_openssl.c +5 -11
@@ -1871,7 +1871,7 @@ bio_write_post(const int status, struct buffer *buf)* Read from an OpenSSL BIO in non-blocking mode. */ static int -bio_read (BIO *bio,struct buffer *buf,int maxlen,const char *desc) +bio_read (BIO *bio,struct buffer *buf,const char *desc) {int i;int ret =0 ;
@@ -1882,10 +1882,6 @@ bio_read(BIO *bio, struct buffer *buf, int maxlen, const char *desc)else {int len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - }/* * BIO_read brackets most of the serious RSA
@@ -2012,15 +2008,14 @@ key_state_write_plaintext_const(struct key_state_ssl *ks_ssl, const uint8_t *dat }int -key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen) +key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf) {int ret =0 ;perf_push (PERF_BIO_READ_CIPHERTEXT );ASSERT (NULL != ks_ssl); - ret =bio_read (ks_ssl->ct_out, buf, maxlen,"tls_read_ciphertext" ); + ret =bio_read (ks_ssl->ct_out, buf,"tls_read_ciphertext" );perf_pop ();return ret;
@@ -2042,15 +2037,14 @@ key_state_write_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf) }int -key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen) +key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf) {int ret =0 ;perf_push (PERF_BIO_READ_PLAINTEXT );ASSERT (NULL != ks_ssl); - ret =bio_read (ks_ssl->ssl_bio, buf, maxlen,"tls_read_plaintext" ); + ret =bio_read (ks_ssl->ssl_bio, buf,"tls_read_plaintext" );perf_pop ();return ret;
src/openvpn/ssl_pkt.h +2 -2
@@ -67,8 +67,8 @@/* * Define number of buffers for send and receive in the reliability layer. */ -#define TLS_RELIABLE_N_SEND_BUFFERS 4/* also window size for reliability layer */ -#define TLS_RELIABLE_N_REC_BUFFERS 8 +#define TLS_RELIABLE_N_SEND_BUFFERS 6/* also window size for reliability layer */ +#define TLS_RELIABLE_N_REC_BUFFERS 12/* * Used in --mode server mode to check tls-auth signature on initial
Signature · 1 line
2.32.1 (Apple Git-133)
PATCH v2 28/28 Always include ACKs for the last seen control packets
In reply to Arne SchwabeThis adds an MRU cache for the last seen packets from the peer to send acks to all recently recently packets. This allows packets to be acknowledged even if a single P_ACK_V1 gets lost, avoiding retransmissions. The downside is that we add up to 28 byte to an P_ACK_V1 (7* packet_id) and up to 24 bytes to other control channel packets (4* packet_id + peer session id). However these small increases in packet size are a small price to pay for increased reliability.
Currently OpenVPN will only send the absolute minimum of ACK messages. A single lost ACK message will trigger a resend from the peer and another ACK message.
Patch v2: fix multiple typos/grammar. Change lru to mru (this is really an
MRU cache), add more unit test cases
--- src/openvpn/reliable.c | 69 +++++++++++++++++++++-- src/openvpn/reliable.h | 17 ++++++ src/openvpn/ssl.c | 5 +- src/openvpn/ssl_common.h | 1 + src/openvpn/ssl_pkt.c | 3 +- tests/unit_tests/openvpn/test_packet_id.c | 68 +++++++++++++++++++++- 6 files changed, 155 insertions(+), 8 deletions(-)
src/openvpn/reliable.c +64 -5
@@ -206,10 +206,56 @@ reliable_ack_parse(struct buffer *buf, struct reliable_ack *ack,return true; } +/** +* Copies the first n acks from \c ack to \c ack_mru +*/ +void +copy_acks_to_mru (struct reliable_ack *ack,struct reliable_ack *ack_mru,int n) +{ +ASSERT (ack->len >= n); +/* This loop is backward to ensure the same order as in ack */ +for (int i = n-1 ; i >=0 ; i--) + { +packet_id_type id = ack->packet_id[i]; + +/* Handle special case of ack_mru empty */ +if (ack_mru->len ==0 ) + { + ack_mru->len =1 ; + ack_mru->packet_id[0 ] = id; + } + +bool idfound = false; + +/* Move all existing entries one to the right */ +packet_id_type move = id; + +for (int j =0 ; j < ack_mru->len; j++) + { +packet_id_type tmp = ack_mru->packet_id[j]; + ack_mru->packet_id[j] = move; + move = tmp; + +if (move == id) + { + idfound = true; +break ; + } + } + +if (!idfound && ack_mru->len <RELIABLE_ACK_SIZE ) + { + ack_mru->packet_id[ack_mru->len] = move; + ack_mru->len++; + } + } +} +/* write a packet ID acknowledgement record to buf, */ /* removing all acknowledged entries from ack */ bool reliable_ack_write (struct reliable_ack *ack, +struct reliable_ack *ack_mru,struct buffer *buf,const struct session_id *sid,int max,bool prepend) {
@@ -222,23 +268,36 @@ reliable_ack_write(struct reliable_ack *ack, { n = max; } - sub =buf_sub (buf,ACK_SIZE (n), prepend); + +copy_acks_to_mru (ack, ack_mru, n); + +/* Number of acks we can resend that still fit into the packet */ +uint8_t total_acks =min_int (max, ack_mru->len); + + sub =buf_sub (buf,ACK_SIZE (total_acks), prepend);if (!BDEF (&sub)) { goto error; } -ASSERT (buf_write (&sub, &n,sizeof (n))); -for (i =0 ; i < n; ++i) +ASSERT (buf_write_u8 (&sub, total_acks)); + +/* Write the actual acks to the packets. Since we copied the acks that +* are going out now already to the front of ack_mru we can fetch all +* acks from ack_mru */ +for (i =0 ; i < total_acks; ++i) { -packet_id_type pid = ack->packet_id[i]; +packet_id_type pid = ack_mru->packet_id[i];packet_id_type net_pid =htonpid (pid);ASSERT (buf_write (&sub, &net_pid,sizeof (net_pid)));dmsg (D_REL_DEBUG ,"ACK write ID " packet_id_format" (ack->len=%d, n=%d)" , (packet_id_print_type )pid, ack->len, n); } -if (n) +if (total_acks) {ASSERT (session_id_defined (sid));ASSERT (session_id_write (sid, &sub)); + } +if (n) + {for (i =0 , j = n; j < ack->len; ) { ack->packet_id[i++] = ack->packet_id[j++];
src/openvpn/reliable.h +17 -0
@@ -197,6 +197,9 @@ reliable_ack_outstanding(struct reliable_ack *ack)* * @param ack The acknowledgment structure containing packet IDs to be * acknowledged. +* @param ack_mru List of packets we have acknowledged before. Packets from +* \c ack will be moved here and if there is space in our +* ack structure we will fill it with packets from this * @param buf The buffer into which the acknowledgment record will be * written. * @param sid The session ID of the VPN tunnel associated with the
@@ -211,6 +214,7 @@ reliable_ack_outstanding(struct reliable_ack *ack)* @li False, if an error occurs during processing. */ bool reliable_ack_write (struct reliable_ack *ack, +struct reliable_ack *ack_mru,struct buffer *buf,const struct session_id *sid,int max,bool prepend);
@@ -370,6 +374,19 @@ bool reliable_ack_acknowledge_packet_id(struct reliable_ack *ack, packet_id_type*/ struct reliable_entry *reliable_get_entry_sequenced (struct reliable *rel); + + +/** +* Copies the first n acks from \c ack to \c ack_mru +* +* @param ack The reliable structure to copy the acks from +* @param ack_mru The reliable structure to insert the acks into +* @param n The number of ACKS to copy +*/ +void +copy_acks_to_mru (struct reliable_ack *ack,struct reliable_ack *ack_mru,int n); + +/** * Remove an entry from a reliable structure. *
src/openvpn/ssl.c +4 -1
@@ -356,7 +356,8 @@ calc_control_channel_frame_overhead(const struct tls_session *session) overhead +=SID_SIZE ;/* ACK array and remote SESSION ID (part of the ACK array) */ - overhead +=ACK_SIZE (min_int (reliable_ack_outstanding (ks->rec_ack),CONTROL_SEND_ACK_MAX )); +int ackstosend =reliable_ack_outstanding (ks->rec_ack) + ks->lru_acks->len; + overhead +=ACK_SIZE (min_int (ackstosend,CONTROL_SEND_ACK_MAX ));/* Message packet id */ overhead +=sizeof (packet_id_type);
@@ -977,6 +978,7 @@ key_state_init(struct tls_session *session, struct key_state *ks) ALLOC_OBJ_CLEAR(ks->send_reliable, struct reliable); ALLOC_OBJ_CLEAR(ks->rec_reliable, struct reliable); ALLOC_OBJ_CLEAR(ks->rec_ack, struct reliable_ack); + ALLOC_OBJ_CLEAR(ks->lru_acks, struct reliable_ack);/* allocate buffers */ ks->plaintext_read_buf =alloc_buf (TLS_CHANNEL_BUF_SIZE );
@@ -1047,6 +1049,7 @@ key_state_free(struct key_state *ks, bool clear)reliable_free (ks->rec_reliable);free (ks->rec_ack); +free (ks->lru_acks);free (ks->key_src);packet_id_free (&ks->crypto_options.packet_id);
src/openvpn/ssl_common.h +1 -0
@@ -233,6 +233,7 @@ struct key_statestruct reliable *send_reliable;/* holds a copy of outgoing packets until ACK received */ struct reliable *rec_reliable;/* order incoming ciphertext packets before we pass to TLS */ struct reliable_ack *rec_ack;/* buffers all packet IDs we want to ACK back to sender */ +struct reliable_ack *lru_acks;/* keeps the most recently acked packages*/ /** Holds outgoing message for the control channel until ks->state reaches * S_ACTIVE */
src/openvpn/ssl_pkt.c +2 -1
@@ -179,7 +179,8 @@ write_control_auth(struct tls_session *session,ASSERT (link_socket_actual_defined (&ks->remote_addr));ASSERT (reliable_ack_write - (ks->rec_ack, buf, &ks->session_id_remote, max_ack, prepend_ack)); + (ks->rec_ack, ks->lru_acks, buf, &ks->session_id_remote, + max_ack, prepend_ack));msg (D_TLS_DEBUG ,"%s(): %s" , __func__,packet_opcode_name (opcode));
tests/unit_tests/openvpn/test_packet_id.c +67 -1
80 lines not on this page · raw message
Signature · 1 line
2.32.1 (Apple Git-133)
[Openvpn-devel] [PATCH 29/28] Add workaround for Softether server dropping P_ACK_V1 with >= 5 acks
Softether had the number of ACKs in ANY OpenVPN packet limited to 4 and dropped packets with more than 4 ACKs. This leads to Softether dropping P_ACK_V1 packets with more than 4 ACKs as invalid. As the recent change of always acking as many packets as possible, this leads to Softether server not being able to successfully establish a connection anymore as it never registers the ACKs.
This behaviour has been fixed on the Softether side with commit 37aa1ba5 but in order to allow clients to connect to older Softether servers, this commit implements a workaround for the case that the peer might be a Softether server (no tls-auth/tls-crypt and no other advanced protocol feature) and limits ACKs to 4 in this case.
--- src/openvpn/ssl_pkt.c | 9 +++++++++ 1 file changed, 9 insertions(+)
src/openvpn/ssl_pkt.c +9 -0
@@ -177,6 +177,15 @@ write_control_auth(struct tls_session *session, {uint8_t header = ks->key_id | (opcode <<P_OPCODE_SHIFT ); +/* Workaround for Softether servers. Softether has a bug that it only +* allows 4 ACks in packets and drops packets if more ACKs are contained +* in a packet (see commit a14d812dcbc in Softether) */ +if (session->tls_wrap.mode ==TLS_WRAP_NONE && !session->opt->server + && !(session->opt->crypto_flags &CO_USE_TLS_KEY_MATERIAL_EXPORT )) + { + max_ack =min_int (max_ack,4 ); + } +ASSERT (link_socket_actual_defined (&ks->remote_addr)); ASSERT(reliable_ack_write (ks->rec_ack, ks->lru_acks, buf, &ks->session_id_remote,
Signature · 1 line
2.32.1 (Apple Git-133)
Re: [Openvpn-devel] [PATCH 29/28] Add workaround for Softether server dropping P_ACK_V1 with >= 5 acks
Hi,
Quoted 1 line · Arne Schwabe
This behaviour has been fixed on the Softether side with commit
37aa1ba5
[..]
Quoted 1 line
+ * in a packet (see commit
a14d812dcbcin Softether) */
The code seems to do what the commit says, but seeing two different commit IDs here confuses me.
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Re: [Openvpn-devel] [PATCH v3 26/28] Allow setting control channel packet size with tls-mtu
In reply to Arne SchwabeThis patch need to be rebased again, does not apply to master anymore:
checking file src/openvpn/ssl.c
Hunk #1 succeeded at 297 (offset 1 line).
Hunk #2 FAILED at 321.
Hunk #3 succeeded at 1256 (offset -42 lines).
Review User-visible Changes · line 147
Quoted 2 lines · On Mittwoch, 11. Mai 2022 13:08:10 CEST Arne Schwabe wrote:
(quoted diff) +1
-0@@ -141,6 +147,8 @@ User-visible Changes +- control channel packet maximum size is no longer influenced by ``--link-
mtu``/``--tun-mtu``
Changes.rst User-visible Changes · line 151
Quoted 1 line
Changes.rst +1
-0+ and must be set by ``--tls-mtu`` now.
Must it or is there still some cc-mtu derivation in place in case it is not given? If not, wouldn't it make sense to at least catch obvious breakage. Usually link/tun-mtu is lowered for that reason. Further down in the patch there are min/max_int() calls in tls_init_control_channel_frame_parameters(), do they do what I suggested here already?
doc/man-sections/link-options.rst the local and the remote host. · line 458
Quoted 2 lines
doc/man-sections/link-options.rst +1
-0+--tls-mtu size
Is --tls-mtu the best name for the option we can come up with? From my outside perspective (can hardly spell mtu right) the "tls" part is somewhat misnamed, as this is an SSL VPN after all. When you know that the framing for the data channel is not TLS you can make sense of it, but who does.
Quoted 3 lines
+ The maximum packet size includes encapsulation overhead like UDP and IP.
\ No newline at end of file
^ this
Quoted 2 lines
+ int
tls_mtu; /* Maximum MTU for the
control channel messages */
The "M" already stands for "Maximum".
Re: [Openvpn-devel] [PATCH v3 26/28] Allow setting control channel packet size with tls-mtu
Am 16.09.2022 um 02:47 schrieb Heiko Hund:
Quoted 10 lines
This patch need to be rebased again, does not apply to master anymore:
checking filesrc/openvpn/ssl.c
Hunk #1 succeeded at 297 (offset 1 line).
Hunk #2 FAILED at 321.
Hunk #3 succeeded at 1256 (offset -42 lines).Review User-visible Changes · line 147
Quoted 2 lines · On Mittwoch, 11. Mai 2022 13:08:10 CEST Arne Schwabe wrote:
(quoted diff) +1
-0@@ -141,6 +147,8 @@ User-visible Changes +- control channel packet maximum size is no longer influenced by ``--link-mtu``/``--tun-mtu``
Quoted 6 lines
Changes.rst User-visible Changes · line 151
Quoted 1 line
Changes.rst +1
-0+ and must be set by ``--tls-mtu`` now.Must it or is there still some cc-mtu derivation in place in case it is not given? If not, wouldn't it make sense to at least catch obvious breakage. Usually link/tun-mtu is lowered for that reason. Further down in the patch there are min/max_int() calls in
tls_init_control_channel_frame_parameters(), do they do what I suggested here already?
The current code is really broken and also if you lower it too much it just stops completely working as it for example will try to send a push message that is too large to fit the size. So I decided to use a clean sheet approach here. And the current tls-mtu default is 1200 or something similar anyway. So it is unlikely to break on any normal connection.
Quoted 6 lines
doc/man-sections/link-options.rst the local and the remote host. · line 458
Quoted 2 lines
doc/man-sections/link-options.rst +1
-0+--tls-mtu sizeIs --tls-mtu the best name for the option we can come up with? From my outside perspective (can hardly spell mtu right) the "tls" part is somewhat misnamed, as this is an SSL VPN after all. When you know that the framing for the data channel is not TLS you can make sense of it, but who does.
It fits the naming of the other options we have. But if you can come up with a better naming, I am all ears.
Quoted 17 lines
Quoted 4 lines
+ The maximum packet size includes encapsulation overhead like UDP and IP.
\ No newline at end of file^ this
Quoted 2 lines
+ int
tls_mtu; /* Maximum MTU for the
control channel messages */The "M" already stands for "Maximum".
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
[Openvpn-devel] [PATCH applied] Re: Always include ACKs for the last seen control packets
In reply to Arne SchwabeApplying Frank's ACK on v1 to v2, since the code is the same - just context changes, and renaming of lru to mru.
Ran through server and client side testing, all works.
This also adds a unit test, which is very welcome :-) - complex stuff, this, and not really easy to test ("I need to drop ACK #3 of the handshake to see if the other end will do a retransmit or if it is ACKed en passant with the next packet").
Stared a bit at the code, and while I'm not sure I understand all details of the reliable code well enough, the parts that I *do* understand look reasonable enough (and no memory-unsafe stuff etc).
Your patch has been applied to the master branch.
(I will push this together with 29/28)
commit c879609534e455471bc1e8091c78cbd9427d402b
Author: Arne Schwabe Date: Wed Aug 31 15:41:39 2022 +0200
Always include ACKs for the last seen control packets
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg25143.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Gert Doering
Acked-by[Openvpn-devel] [PATCH applied] Re: Add workaround for Softether server dropping P_ACK_V1 with >= 5 acks
In reply to Arne SchwabeThis is straightforward. The detection "what makes a softether server?" is a bit annoying... and this code will be with us forever...
I have not specifically tested this ("no softether server around") but ran the normal server/client tests.
Your patch has been applied to the master branch.
commit 036517d5e06c1254233d51893b3a196a3cd37492
Author: Arne Schwabe Date: Wed Aug 31 15:41:40 2022 +0200
Add workaround for Softether server dropping P_ACK_V1 with >= 5 acks
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg25142.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering