Move tls_select_primary_key into its own function
PATCH 1/7 Move tls_select_primary_key into its own function
tls_pre_encrypt mainly performs the task of selecting the primary encryption key but also performs other minor tasks. To allow only querying for the key that should be used for encryption extract this part of the function into its own function.
--- src/openvpn/ssl.c | 36 +++++++++++++++++++++--------------- src/openvpn/ssl.h | 10 ++++++++++ 2 files changed, 31 insertions(+), 15 deletions(-)
src/openvpn/ssl.c +21 -15
@@ -3820,27 +3820,15 @@ error:return false; } -/* Choose the key with which to encrypt a data packet */ -void -tls_pre_encrypt (struct tls_multi *multi, -struct buffer *buf,struct crypto_options **opt) +struct key_state *tls_select_encryption_key (struct tls_multi *multi) { - multi->save_ks =NULL ; -if (buf->len <=0 ) - { - buf->len =0 ; - *opt =NULL ; -return ; - } -struct key_state *ks_select =NULL ;for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) {struct key_state *ks =get_key_scan (multi, i);if (ks->state >=S_ACTIVE - && (ks->authenticated ==KS_AUTH_TRUE ) - && ks->crypto_options.key_ctx_bi.initialized - ) + && ks->authenticated ==KS_AUTH_TRUE + && ks->crypto_options.key_ctx_bi.initialized) {if (!ks_select) {
@@ -3853,6 +3841,24 @@ tls_pre_encrypt(struct tls_multi *multi, } } } +return ks_select; +} + + +/* Choose the key with which to encrypt a data packet */ +void +tls_pre_encrypt (struct tls_multi *multi, +struct buffer *buf,struct crypto_options **opt) +{ + multi->save_ks =NULL ; +if (buf->len <=0 ) + { + buf->len =0 ; + *opt =NULL ; +return ; + } + +struct key_state *ks_select =tls_select_encryption_key (multi);if (ks_select) {
src/openvpn/ssl.h +10 -0
@@ -372,6 +372,16 @@ bool tls_pre_decrypt_lite(const struct tls_auth_standalone *tas,void tls_pre_encrypt (struct tls_multi *multi,struct buffer *buf,struct crypto_options **opt); +/** +* Selects the primary encryption that should be used to encrypt data of an +* outgoing packet. +* @ingroup data_crypto +* +* If no key is found NULL is returned instead. +* +* @param multi - The TLS state for this packet's destination VPN tunnel. +*/ +struct key_state *tls_select_encryption_key (struct tls_multi *multi);/** * Prepend a one-byte OpenVPN data channel P_DATA_V1 opcode to the packet.
Signature · 1 line
2.31.1
PATCH 2/7 Use functions to access key_state instead direct member access
This uses get_key_scan and get_primary key instead the directly accessing the members of the struct to improve readiability of the code.
--- src/openvpn/multi.c | 3 +-- src/openvpn/push.c | 9 ++++----- src/openvpn/ssl.c | 11 +++-------- src/openvpn/ssl.h | 2 +- src/openvpn/ssl_common.h | 9 +++++++++ 5 files changed, 18 insertions(+), 16 deletions(-)
src/openvpn/multi.c +1 -2
@@ -1800,8 +1800,7 @@ multi_client_set_protocol_options(struct context *c)* cipher -> so log the fact and push the "what we have now" cipher * (so the client is always told what we expect it to use) */ -const struct tls_session *session = &tls_multi->session[TM_ACTIVE ]; -if (session->key[KS_PRIMARY ].crypto_options.key_ctx_bi.initialized) +if (get_primary_key (tls_multi)->crypto_options.key_ctx_bi.initialized) {msg (M_INFO ,"PUSH: client wants to negotiate cipher (NCP), but " "server has already generated data channel keys, "
src/openvpn/push.c +4 -5
@@ -222,7 +222,7 @@ receive_cr_response(struct context *c, const struct buffer *buffer)struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE ];struct man_def_auth_context *mda = session->opt->mda_context;struct env_set *es = session->opt->es; -int key_id = session->key[KS_PRIMARY ].key_id; +int key_id =get_primary_key (c->c2.tls_multi)->key_id;management_notify_client_cr_response (key_id, mda, es, m);
@@ -304,7 +304,7 @@ receive_auth_pending(struct context *c, const struct buffer *buffer)"to %us" , c->options.handshake_window, min_uint(max_timeout, server_timeout)); -struct key_state *ks = &c->c2.tls_multi->session[TM_ACTIVE ].key[KS_PRIMARY ]; +const struct key_state *ks =get_primary_key (c->c2.tls_multi); c->c2.push_request_timeout = ks->established +min_uint (max_timeout, server_timeout); }
@@ -369,7 +369,7 @@ bool send_auth_pending_messages(struct tls_multi *tls_multi, const char *extra, unsigned int timeout) { - struct key_state *ks = &tls_multi->session[TM_ACTIVE].key[KS_PRIMARY]; + struct key_state *ks = get_key_scan(tls_multi, 0);static const char info_pre[] ="INFO_PRE," ;
@@ -476,8 +476,7 @@ cleanup:bool send_push_request (struct context *c) { -struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE ]; -struct key_state *ks = &session->key[KS_PRIMARY ]; +const struct key_state *ks =get_primary_key (c->c2.tls_multi);/* We timeout here under two conditions: * a) we reached the hard limit of push_request_timeout
src/openvpn/ssl.c +3 -8
@@ -3448,7 +3448,7 @@ tls_pre_decrypt(struct tls_multi *multi,if (i ==TM_SIZE &&is_hard_reset_method2 (op)) {struct tls_session *session = &multi->session[TM_ACTIVE ]; -struct key_state *ks = &session->key[KS_PRIMARY ]; +const struct key_state *ks =get_primary_key (multi);/* * If we have no session currently in progress, the initial packet will
@@ -3933,7 +3933,6 @@ tls_send_payload(struct tls_multi *multi, const uint8_t *data, int size) { -struct tls_session *session;struct key_state *ks;bool ret = false;
@@ -3941,8 +3940,7 @@ tls_send_payload(struct tls_multi *multi,ASSERT (multi); - session = &multi->session[TM_ACTIVE ]; - ks = &session->key[KS_PRIMARY ]; + ks =get_key_scan (multi,0 );if (ks->state >=S_ACTIVE ) {
@@ -3971,16 +3969,13 @@ bool tls_rec_payload(struct tls_multi *multi, struct buffer *buf) { - struct tls_session *session; -struct key_state *ks; bool ret = false;tls_clear_error ();ASSERT (multi); - session = &multi->session[TM_ACTIVE ]; - ks = &session->key[KS_PRIMARY ]; +struct key_state *ks =get_key_scan (multi,0 );if (ks->state >=S_ACTIVE &&BLEN (&ks->plaintext_read_buf)) {
src/openvpn/ssl.h +1 -1
@@ -547,7 +547,7 @@ tls_test_payload_len(const struct tls_multi *multi) {if (multi) { -const struct key_state *ks = &multi->session[TM_ACTIVE ].key[KS_PRIMARY ]; +const struct key_state *ks =get_primary_key (multi);if (ks->state >=S_ACTIVE ) {return BLEN (&ks->plaintext_read_buf);
src/openvpn/ssl_common.h +9 -0
@@ -631,4 +631,13 @@ get_key_scan(struct tls_multi *multi, int index) } } +/** gets an item of \c key_state objects in the +* order they should be scanned by data +* channel modules. */ +static inline const struct key_state * +get_primary_key (const struct tls_multi *multi) +{ +return &multi->session[TM_ACTIVE ].key[KS_PRIMARY ]; +} +#endif /* SSL_COMMON_H_ */
Signature · 1 line
2.31.1
Re: [Openvpn-devel] [PATCH 2/7] Use functions to access key_state instead direct member access
Hi,
Quoted 50 lines · Arne Schwabe
This uses
get_key_scanandget_primarykey instead the directly accessing the members of the struct to improve readiability of the code.Signed-off-by Arne Schwabe <[email protected]...>--- src/openvpn/multi.c | 3 +-- src/openvpn/push.c | 9 ++++----- src/openvpn/ssl.c | 11 +++-------- src/openvpn/ssl.h | 2 +- src/openvpn/ssl_common.h | 9 +++++++++ 5 files changed, 18 insertions(+), 16 deletions(-)src/openvpn/multi.c +1
-2@@ -1800,8 +1800,7 @@ multi_client_set_protocol_options(struct context *c)* cipher -> so log the fact and push the "what we have now" cipher * (so the client is always told what we expect it to use) */ -const struct tls_session *session = &tls_multi->session[TM_ACTIVE ]; -if (session->key[KS_PRIMARY ].crypto_options.key_ctx_bi.initialized) +if (get_primary_key (tls_multi)->crypto_options.key_ctx_bi.initialized) {msg (M_INFO ,"PUSH: client wants to negotiate cipher (NCP), but " "server has already generated data channel keys, "src/openvpn/push.c +2
-2@@ -222,7 +222,7 @@ receive_cr_response(struct context *c, const struct buffer *buffer)struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE ];struct man_def_auth_context *mda = session->opt->mda_context;struct env_set *es = session->opt->es; -int key_id = session->key[KS_PRIMARY ].key_id; +int key_id =get_primary_key (c->c2.tls_multi)->key_id;management_notify_client_cr_response (key_id, mda, es, m);@@ -304,7 +304,7 @@ receive_auth_pending(struct context *c, const struct buffer *buffer)"to %us" , c->options.handshake_window, min_uint(max_timeout, server_timeout)); -struct key_state *ks = &c->c2.tls_multi->session[TM_ACTIVE ].key[KS_PRIMARY ]; +const struct key_state *ks =get_primary_key (c->c2.tls_multi); c->c2.push_request_timeout = ks->established +min_uint (max_timeout, server_timeout); }
src/openvpn/push.c bool · line 372
Quoted 5 lines
src/openvpn/push.c +1
-1@@ -369,7 +369,7 @@ bool send_auth_pending_messages(struct tls_multi *tls_multi, const char *extra, unsigned int timeout) { - struct key_state *ks = &tls_multi->session[TM_ACTIVE].key[KS_PRIMARY]; + struct key_state *ks = get_key_scan(tls_multi, 0);
why not calling get_primary_key() here and in all other spots where we ask for the 0th key in the scan?
Quoted 81 lines
src/openvpn/push.c +1
-2static const char info_pre[] ="INFO_PRE," ;@@ -476,8 +476,7 @@ cleanup:bool send_push_request (struct context *c) { -struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE ]; -struct key_state *ks = &session->key[KS_PRIMARY ]; +const struct key_state *ks =get_primary_key (c->c2.tls_multi);/* We timeout here under two conditions: * a) we reached the hard limit of push_request_timeout src/openvpn/ssl.c +3
-8@@ -3448,7 +3448,7 @@ tls_pre_decrypt(struct tls_multi *multi,if (i ==TM_SIZE &&is_hard_reset_method2 (op)) {struct tls_session *session = &multi->session[TM_ACTIVE ]; -struct key_state *ks = &session->key[KS_PRIMARY ]; +const struct key_state *ks =get_primary_key (multi);/* * If we have no session currently in progress, the initial packet will @@ -3933,7 +3933,6 @@ tls_send_payload(struct tls_multi *multi, const uint8_t *data, int size) { -struct tls_session *session;struct key_state *ks;bool ret = false;@@ -3941,8 +3940,7 @@ tls_send_payload(struct tls_multi *multi,ASSERT (multi); - session = &multi->session[TM_ACTIVE ]; - ks = &session->key[KS_PRIMARY ]; + ks =get_key_scan (multi,0 );if (ks->state >=S_ACTIVE ) {@@ -3971,16 +3969,13 @@ bool tls_rec_payload(struct tls_multi *multi, struct buffer *buf) { - struct tls_session *session; -struct key_state *ks; bool ret = false;tls_clear_error ();ASSERT (multi); - session = &multi->session[TM_ACTIVE ]; - ks = &session->key[KS_PRIMARY ]; +struct key_state *ks =get_key_scan (multi,0 );if (ks->state >=S_ACTIVE &&BLEN (&ks->plaintext_read_buf)) {src/openvpn/ssl.h +1
-1@@ -547,7 +547,7 @@ tls_test_payload_len(const struct tls_multi *multi) {if (multi) { -const struct key_state *ks = &multi->session[TM_ACTIVE ].key[KS_PRIMARY ]; +const struct key_state *ks =get_primary_key (multi);if (ks->state >=S_ACTIVE ) {return BLEN (&ks->plaintext_read_buf);src/openvpn/ssl_common.h +0
-0@@ -631,4 +631,13 @@ get_key_scan(struct tls_multi *multi, int index) }
src/openvpn/ssl_common.h get_key_scan(struct tls_multi *multi, int index) · line 641
Quoted 10 lines
src/openvpn/ssl_common.h +8
-0} +/** gets an item of \c key_state objects in the +* order they should be scanned by data +* channel modules. */ +static inline const struct key_state * +get_primary_key (const struct tls_multi *multi) +{ +return &multi->session[TM_ACTIVE ].key[KS_PRIMARY ]; +}
Why not implementing this as get_key_scan(multi, 0); ?
src/openvpn/ssl_common.h get_key_scan(struct tls_multi *multi, int index) · line 643
Quoted 3 lines
src/openvpn/ssl_common.h +1
-0+#endif /* SSL_COMMON_H_ */
Regards,
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH 2/7] Use functions to access key_state instead direct member access
Quoted 12 lines
src/openvpn/push.c bool · line 372
Quoted 8 lines
src/openvpn/push.c +1
-1@@ -369,7 +369,7 @@ bool send_auth_pending_messages(struct tls_multi *tls_multi, const char *extra, unsigned int timeout) { - struct key_state *ks = &tls_multi->session[TM_ACTIVE].key[KS_PRIMARY]; + struct key_state *ks = get_key_scan(tls_multi, 0);why not calling
get_primary_key()here and in all other spots where we ask for the 0th key in the scan?
This function needs a non-const variant and get_primary_key returns a const struct.
Quoted 11 lines
src/openvpn/ssl_common.h get_key_scan(struct tls_multi *multi, int index) · line 641
Quoted 8 lines
src/openvpn/ssl_common.h +8
-0+/** gets an item of \c key_state objects in the +* order they should be scanned by data +* channel modules. */ +static inline const struct key_state * +get_primary_key (const struct tls_multi *multi) +{ +return &multi->session[TM_ACTIVE ].key[KS_PRIMARY ]; +}Why not implementing this as
get_key_scan(multi, 0); ?
That breaks the constness of the argument.
Arne
Antonio Quartulli
Acked-byRe: [Openvpn-devel] [PATCH 2/7] Use functions to access key_state instead direct member access
Hi,
Quoted 13 lines · Arne Schwabe
Quoted 12 lines
src/openvpn/push.c bool · line 372
Quoted 8 lines
src/openvpn/push.c +1
-1@@ -369,7 +369,7 @@ bool send_auth_pending_messages(struct tls_multi *tls_multi, const char *extra, unsigned int timeout) { - struct key_state *ks = &tls_multi->session[TM_ACTIVE].key[KS_PRIMARY]; + struct key_state *ks = get_key_scan(tls_multi, 0);why not calling
get_primary_key()here and in all other spots where we ask for the 0th key in the scan?
Quoted 4 lines
This function needs a non-const variant and
get_primary_keyreturns a const struct.
Right, I missed that.
Quoted 12 lines
Quoted 11 lines
src/openvpn/ssl_common.h get_key_scan(struct tls_multi *multi, int index) · line 641
Quoted 8 lines
src/openvpn/ssl_common.h +8
-0+/** gets an item of \c key_state objects in the +* order they should be scanned by data +* channel modules. */ +static inline const struct key_state * +get_primary_key (const struct tls_multi *multi) +{ +return &multi->session[TM_ACTIVE ].key[KS_PRIMARY ]; +}Why not implementing this as
get_key_scan(multi, 0); ?
Quoted 1 line
That breaks the constness of the argument.
I have seen that there is only one place where we call get_primary_key() with a const argument and that is tls_test_payload_len().
However, I think what you came up with is still the easiest thing...unless somebody else comes up with a smarter idea.
Maybe in the future we'll be able to clean this up further.
For me the patch looks good as it is.
Compile rig is happy.
Basic connectivity test and renegotiation test with OpenSSL passed.
Regards,
Signature · 1 line
Antonio Quartulli
[Openvpn-devel] [PATCH applied] Re: Use functions to access key_state instead direct member access
In reply to Arne SchwabeLooked a bit at the code, not sure if's really easier to read, or just an "looks unfamiliar" effect. But it works in my tests, not very surprisingly :-)
Your patch has been applied to the master branch.
commit a80bec331ec65efbbec1adb7892e242589c238ef
Author: Arne Schwabe Date: Thu Apr 22 17:17:19 2021 +0200
Use functions to access key_state instead direct member access
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]/msg22200.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
PATCH 3/7 Return cached result in tls_authentication_status
In reply to Arne Schwabetls_authentication_status does caching to avoid file I/O more than every TLS_MULTI_AUTH_STATUS_INTERVAL (10s) per connection. But counter-intuitively it does not return the cached result but rather TLS_AUTHENTICATION_UNDEFINED if the cache is not refreshed by the call.
This is workarounded by forcing a refresh in some areas of the code (latency = 0).
This patch changes the behaviour by always returning the last known status and only updating the file status when the i/o timeout for the caches is reached.
The patch also changes the DEFINE enum into a real enum.
--- src/openvpn/multi.c | 2 +- src/openvpn/push.c | 3 ++- src/openvpn/ssl_common.h | 11 +++++++++- src/openvpn/ssl_verify.c | 46 ++++++++++++++++++++-------------------- src/openvpn/ssl_verify.h | 3 +-- 5 files changed, 37 insertions(+), 28 deletions(-)
src/openvpn/multi.c +1 -1
@@ -2596,7 +2596,7 @@ static const multi_client_connect_handler client_connect_handlers[] = {static void multi_connection_established (struct multi_context *m,struct multi_instance *mi) { -if (tls_authentication_status (mi->context.c2.tls_multi,0 ) +if (tls_authentication_status (mi->context.c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) !=TLS_AUTHENTICATION_SUCCEEDED ) {return ;
src/openvpn/push.c +2 -1
@@ -855,7 +855,8 @@ process_incoming_push_request(struct context *c) {int ret =PUSH_MSG_ERROR ; -if (tls_authentication_status (c->c2.tls_multi,0 ) ==TLS_AUTHENTICATION_FAILED + +if (tls_authentication_status (c->c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) ==TLS_AUTHENTICATION_FAILED || c->c2.tls_multi->multi_state ==CAS_FAILED ) {const char *client_reason =tls_client_reason (c->c2.tls_multi);
src/openvpn/ssl_common.h +10 -1
@@ -152,6 +152,15 @@ struct auth_deferred_statusunsigned int auth_control_status; }; +/* key_state_test_auth_control_file return values, these specify the +* current status of a deferred authentication */ +enum auth_deferred_result { +ACF_PENDING ,/**< deferred auth still pending */ +ACF_SUCCEEDED ,/**< deferred auth has suceeded */ +ACF_DISABLED ,/**< deferred auth is not used */ +ACF_FAILED /**< deferred auth has failed */ +}; +/** * Security parameter state of one TLS and data channel %key session. * @ingroup control_processor
@@ -219,7 +228,7 @@ struct key_state#ifdef ENABLE_MANAGEMENT unsigned int mda_key_id; -unsigned int mda_status; +enum auth_deferred_result mda_status;#endif time_t acf_last_mod;
src/openvpn/ssl_verify.c +23 -23
86 lines not on this page · raw message
src/openvpn/ssl_verify.h +1 -2
@@ -69,8 +69,7 @@ enum tls_auth_status {TLS_AUTHENTICATION_SUCCEEDED =0 ,TLS_AUTHENTICATION_FAILED =1 , -TLS_AUTHENTICATION_DEFERRED =2 , - TLS_AUTHENTICATION_UNDEFINED=3 + TLS_AUTHENTICATION_DEFERRED=2 };/**
Signature · 1 line
2.31.1
Re: [Openvpn-devel] [PATCH 3/7] Return cached result in tls_authentication_status
Hi,
after applying this patch my deferred auth script test does not work anymore.
Regardless of deferred auth being succesful or not, the server never sends a push-reply or auth-failed to the client.
I tested also non-deferred auth and it seems to have the same broken behaviour.
Haven't checked yet what might be causing this.
Reverting this patch fixes the issue.
Regards,
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH 3/7] Return cached result in tls_authentication_status
Hi,
Quoted 7 lines · Antonio Quartulli
Hi,
after applying this patch my deferred auth script test does not work anymore.
Regardless of deferred auth being succesful or not, the server never sends a push-reply or auth-failed to the client.
I take this back. Pure auth-user-pass (no deferred) works.
But as soon as we have deferred auth the server does not reply to the client anymore.
I will comment on the patch with a possible explanation.
Regards,
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH 3/7] Return cached result in tls_authentication_status
In reply to Arne SchwabeHi,
Quoted 134 lines · Arne Schwabe
tls_authentication_statusdoes caching to avoid file I/O more than everyTLS_MULTI_AUTH_STATUS_INTERVAL(10s) per connection. But counter-intuitively it does not return the cached result but ratherTLS_AUTHENTICATION_UNDEFINEDif the cache is not refreshed by the call.This is workarounded by forcing a refresh in some areas of the code (latency = 0).
This patch changes the behaviour by always returning the last known status and only updating the file status when the i/o timeout for the caches is reached.
The patch also changes the DEFINE enum into a real enum.
Signed-off-by Arne Schwabe <[email protected]...>--- src/openvpn/multi.c | 2 +- src/openvpn/push.c | 3 ++- src/openvpn/ssl_common.h | 11 +++++++++- src/openvpn/ssl_verify.c | 46 ++++++++++++++++++++-------------------- src/openvpn/ssl_verify.h | 3 +-- 5 files changed, 37 insertions(+), 28 deletions(-)src/openvpn/multi.c +1
-1@@ -2596,7 +2596,7 @@ static const multi_client_connect_handler client_connect_handlers[] = {static void multi_connection_established (struct multi_context *m,struct multi_instance *mi) { -if (tls_authentication_status (mi->context.c2.tls_multi,0 ) +if (tls_authentication_status (mi->context.c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) !=TLS_AUTHENTICATION_SUCCEEDED ) {return ;src/openvpn/push.c +2
-1@@ -855,7 +855,8 @@ process_incoming_push_request(struct context *c) {int ret =PUSH_MSG_ERROR ; -if (tls_authentication_status (c->c2.tls_multi,0 ) ==TLS_AUTHENTICATION_FAILED + +if (tls_authentication_status (c->c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) ==TLS_AUTHENTICATION_FAILED || c->c2.tls_multi->multi_state ==CAS_FAILED ) {const char *client_reason =tls_client_reason (c->c2.tls_multi);src/openvpn/ssl_common.h +10
-1@@ -152,6 +152,15 @@ struct auth_deferred_statusunsigned int auth_control_status; }; +/* key_state_test_auth_control_file return values, these specify the +* current status of a deferred authentication */ +enum auth_deferred_result { +ACF_PENDING ,/**< deferred auth still pending */ +ACF_SUCCEEDED ,/**< deferred auth has suceeded */ +ACF_DISABLED ,/**< deferred auth is not used */ +ACF_FAILED /**< deferred auth has failed */ +}; +/** * Security parameter state of one TLS and data channel %key session. * @ingroup control_processor @@ -219,7 +228,7 @@ struct key_state#ifdef ENABLE_MANAGEMENT unsigned int mda_key_id; -unsigned int mda_status; +enum auth_deferred_result mda_status;#endif time_t acf_last_mod;src/openvpn/ssl_verify.c +14
-11@@ -845,13 +845,6 @@ cleanup:* user/password authentication. *************************************************************************** */ -/* key_state_test_auth_control_file return values, -* NOTE: acf_merge indexing depends on these values */ -#define ACF_UNDEFINED 0 -#define ACF_SUCCEEDED 1 -#define ACF_DISABLED 2 -#define ACF_FAILED 3 -void auth_set_client_reason (struct tls_multi *multi,const char *client_reason) {@@ -866,7 +859,7 @@ auth_set_client_reason(struct tls_multi *multi, const char *client_reason)#ifdef ENABLE_MANAGEMENT -static inline unsigned int +static inline enum auth_deferred_result man_def_auth_test (const struct key_state *ks) {if (management_enable_def_auth (management))@@ -1041,13 +1034,23 @@ key_state_gen_auth_control_files(struct auth_deferred_status *ads,return (acf && apf); } -static unsigned int -key_state_test_auth_control_file (struct auth_deferred_status *ads) +/** +* Checks the control status from a file. The function will try to read +* and update the cached status if the status is still pending and the paramter +* cached is false. The function returns the +* +* +* @param ads deferred status control structure +* @param cached Return only cached status +* @return +*/ +static enum auth_deferred_result +key_state_test_auth_control_file (struct auth_deferred_status *ads,bool cached) {if (ads->auth_control_file) {unsigned int ret = ads->auth_control_status; -if (ret ==ACF_UNDEFINED ) +if (ret ==ACF_PENDING && !cached) {FILE *fp =fopen (ads->auth_control_file,"r" );if (fp)
src/openvpn/ssl_verify.c tls_authentication_status(struct tls_multi *multi, const int latency) · line 1090
Quoted 8 lines
src/openvpn/ssl_verify.c +1
-4@@ -1084,10 +1087,7 @@ tls_authentication_status(struct tls_multi *multi, const int latency)/* at least one key already failed authentication */ bool failed_auth = false; -if (latency && multi->tas_last + latency >= now) - { -return TLS_AUTHENTICATION_UNDEFINED ; - } +bool cached = multi->tas_last + latency >= now;
There is an extra space before the '='.
src/openvpn/ssl_verify.c tls_authentication_status(struct tls_multi *multi, const int latency) · line 1091
Quoted 1 line
src/openvpn/ssl_verify.c +0
-0multi->tas_last = now;
I think keeping this line here is wrong.
Now that the logic has been changed, the code is setting tas_last to now every time this function is called (it wasn't the case before this patch).
For this reason "cached" will always be true and the ACF will never be checked.
A fix would be to add a condition above this line, like:
if (!cached)
multi->tas_last = now;
Does it make sense?
In my tests this fixes the problem and deferred auth works again.
[I wonder why it worked for your tests - maybe a later patch is touching this code again]
Cheers,
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH 3/7] Return cached result in tls_authentication_status
Hi,
Quoted 7 lines · Antonio Quartulli
Quoted 1 line
multi->tas_last = now;I think keeping this line here is wrong.
Now that the logic has been changed, the code is setting
tas_lastto now every time this function is called (it wasn't the case before this patch).
Quoted 7 lines
For this reason "cached" will always be true and the ACF will never be checked.
A fix would be to add a condition above this line, like:
if (!cached)
multi->tas_last = now;
I think it may even be better to move these two lines above right after having invoked key_state_test_auth_control_file() - unless there is some drawback that I can't see.
Cheers,
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH 3/7] Return cached result in tls_authentication_status
In reply to Antonio QuartulliQuoted 13 lines
Quoted 4 lines
multi->tas_last = now;I think keeping this line here is wrong.
Now that the logic has been changed, the code is setting
tas_lastto now every time this function is called (it wasn't the case before this patch).For this reason "cached" will always be true and the ACF will never be checked.
Quoted 7 lines
A fix would be to add a condition above this line, like:
if (!cached)
multi->tas_last = now;Does it make sense?
In my tests this fixes the problem and deferred auth works again.
Yes. It is. I am not sure how I missed that.
Arne
PATCH 4/7 Make waiting on auth an explicit state in the context state machine
In reply to Arne SchwabePreviously we relied on checking tls_authentication_status to check wether to determine if the context auth state is actually valid or not. This patch eliminates that check by introducing waiting on the authentication as extra state in the context auth, state machine.
--- src/openvpn/multi.c | 6 ------ src/openvpn/ssl.c | 9 ++++++++- src/openvpn/ssl_common.h | 1 + 3 files changed, 9 insertions(+), 7 deletions(-)
src/openvpn/multi.c +0 -6
@@ -2596,12 +2596,6 @@ static const multi_client_connect_handler client_connect_handlers[] = {static void multi_connection_established (struct multi_context *m,struct multi_instance *mi) { -if (tls_authentication_status (mi->context.c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) - !=TLS_AUTHENTICATION_SUCCEEDED ) - { -return ; - } -/* We are only called for the CAS_PENDING_x states, so we * can ignore other states here */ bool from_deferred = (mi->context.c2.tls_multi->multi_state !=CAS_PENDING );
src/openvpn/ssl.c +8 -1
@@ -2809,7 +2809,7 @@ tls_process(struct tls_multi *multi,if (session->opt->mode ==MODE_SERVER ) {/* On a server we continue with running connect scripts next */ - multi->multi_state =CAS_PENDING ; + multi->multi_state =CAS_WAITING_AUTH ; }else {
@@ -3135,6 +3135,13 @@ tls_multi_process(struct tls_multi *multi,enum tls_auth_status tas =tls_authentication_status (multi,TLS_MULTI_AUTH_STATUS_INTERVAL ); +/* If we have successfully authenticated and are still waiting for the authentication to finish +* move the state machine for the multi context forward */ +if (multi->multi_state ==CAS_WAITING_AUTH && tas ==TLS_AUTHENTICATION_SUCCEEDED ) + { + multi->multi_state =CAS_PENDING ; + } +/* * If lame duck session expires, kill it. */
src/openvpn/ssl_common.h +1 -0
@@ -512,6 +512,7 @@ struct tls_session* connect scripts/plugins */ enum multi_status {CAS_NOT_CONNECTED , +CAS_WAITING_AUTH ,/**< TLS connection established but deferred auth not finished */ CAS_PENDING ,CAS_PENDING_DEFERRED ,CAS_PENDING_DEFERRED_PARTIAL ,/**< at least handler succeeded, no result yet*/
Signature · 1 line
2.31.1
Re: [Openvpn-devel] [PATCH 4/7] Make waiting on auth an explicit state in the context state machine
Hi,
I am not sure what I Am missing, but this patch does not apply on top of master + 3/7.
Is there another patch that I need to apply first?
Regards,
Quoted 69 lines · Arne Schwabe
Previously we relied on checking
tls_authentication_statusto check wether to determine if the context auth state is actually valid or not. This patch eliminates that check by introducing waiting on the authentication as extra state in the context auth, state machine.Signed-off-by Arne Schwabe <[email protected]...>--- src/openvpn/multi.c | 6 ------ src/openvpn/ssl.c | 9 ++++++++- src/openvpn/ssl_common.h | 1 + 3 files changed, 9 insertions(+), 7 deletions(-)src/openvpn/multi.c +0
-6@@ -2596,12 +2596,6 @@ static const multi_client_connect_handler client_connect_handlers[] = {static void multi_connection_established (struct multi_context *m,struct multi_instance *mi) { -if (tls_authentication_status (mi->context.c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) - !=TLS_AUTHENTICATION_SUCCEEDED ) - { -return ; - } -/* We are only called for the CAS_PENDING_x states, so we * can ignore other states here */ bool from_deferred = (mi->context.c2.tls_multi->multi_state !=CAS_PENDING );src/openvpn/ssl.c +8
-1@@ -2809,7 +2809,7 @@ tls_process(struct tls_multi *multi,if (session->opt->mode ==MODE_SERVER ) {/* On a server we continue with running connect scripts next */ - multi->multi_state =CAS_PENDING ; + multi->multi_state =CAS_WAITING_AUTH ; }else {@@ -3135,6 +3135,13 @@ tls_multi_process(struct tls_multi *multi,enum tls_auth_status tas =tls_authentication_status (multi,TLS_MULTI_AUTH_STATUS_INTERVAL ); +/* If we have successfully authenticated and are still waiting for the authentication to finish +* move the state machine for the multi context forward */ +if (multi->multi_state ==CAS_WAITING_AUTH && tas ==TLS_AUTHENTICATION_SUCCEEDED ) + { + multi->multi_state =CAS_PENDING ; + } +/* * If lame duck session expires, kill it. */ src/openvpn/ssl_common.h +1
-0@@ -512,6 +512,7 @@ struct tls_session* connect scripts/plugins */ enum multi_status {CAS_NOT_CONNECTED , +CAS_WAITING_AUTH ,/**< TLS connection established but deferred auth not finished */ CAS_PENDING ,CAS_PENDING_DEFERRED ,CAS_PENDING_DEFERRED_PARTIAL ,/**< at least handler succeeded, no result yet*/
Signature · 1 line
Antonio Quartulli
PATCH 5/7 Extracting key_state deferred auth status update into function
In reply to Arne SchwabeThis extract the update of a deferred key status into into own function.
--- src/openvpn/ssl_verify.c | 90 ++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 32 deletions(-)
src/openvpn/ssl_verify.c +58 -32
@@ -1073,6 +1073,56 @@ key_state_test_auth_control_file(struct auth_deferred_status *ads, bool cached)return ACF_DISABLED ; } +/** +* This method takes a key_state and if updates the state +* of the key if it is deferred. +* @param cached If auth control files should be tried to be opened or th +* cached results should be used +* @param ks The key_state to update +*/ +static void +update_key_auth_status (bool cached,struct key_state *ks) +{ +if (ks->authenticated ==KS_AUTH_FALSE ) + { +return ; + } +else + { +enum auth_deferred_result auth_plugin =ACF_DISABLED ; +enum auth_deferred_result auth_script =ACF_DISABLED ; +enum auth_deferred_result auth_man =ACF_DISABLED ; + auth_plugin =key_state_test_auth_control_file (&ks->plugin_auth, cached); + auth_script =key_state_test_auth_control_file (&ks->script_auth, cached); +#ifdef ENABLE_MANAGEMENT + auth_man =man_def_auth_test (ks); +#endif +ASSERT (auth_plugin <4 && auth_script <4 && auth_man <4 ); + +if (auth_plugin ==ACF_FAILED || auth_script ==ACF_FAILED + || auth_man ==ACF_FAILED ) + { + ks->authenticated =KS_AUTH_FALSE ; +return ; + } +else if (auth_plugin ==ACF_PENDING + || auth_script ==ACF_PENDING + || auth_man ==ACF_PENDING ) + { +if (now < ks->auth_deferred_expire) + { +/* key state is remains in KS_DEFERRED */ + } + } +else + { +/* auth_plugin and auth_man are either ACF_DISABLED or ACF_SUCCEDED */ + ks->authenticated =KS_AUTH_TRUE ; + } + } +} + +enum tls_auth_status tls_authentication_status (struct tls_multi *multi,const int latency) {
@@ -1096,43 +1146,19 @@ tls_authentication_status(struct tls_multi *multi, const int latency)if (TLS_AUTHENTICATED (multi, ks)) { active++; +update_key_auth_status (cached, ks); +if (ks->authenticated ==KS_AUTH_FALSE ) { failed_auth = true; } -else +else if (ks->authenticated ==KS_AUTH_DEFERRED ) { -enum auth_deferred_result auth_plugin =ACF_DISABLED ; -enum auth_deferred_result auth_script =ACF_DISABLED ; -enum auth_deferred_result auth_man =ACF_DISABLED ; - auth_plugin =key_state_test_auth_control_file (&ks->plugin_auth, cached); - auth_script =key_state_test_auth_control_file (&ks->script_auth, cached); -#ifdef ENABLE_MANAGEMENT - auth_man =man_def_auth_test (ks); -#endif -ASSERT (auth_plugin <4 && auth_script <4 && auth_man <4 ); - -if (auth_plugin ==ACF_FAILED || auth_script ==ACF_FAILED - || auth_man ==ACF_FAILED ) - { - ks->authenticated =KS_AUTH_FALSE ; - failed_auth = true; - } -else if (auth_plugin ==ACF_PENDING - || auth_script ==ACF_PENDING - || auth_man ==ACF_PENDING ) - { -if (now < ks->auth_deferred_expire) - { - deferred = true; - } - } -else - { -/* auth_plugin and auth_man are either ACF_DISABLED or ACF_SUCCEDED */ - success = true; - ks->authenticated =KS_AUTH_TRUE ; - } + deferred = true; + } +else if (ks->authenticated ==KS_AUTH_TRUE ) + { + success = true; } } }
Signature · 1 line
2.31.1
PATCH 6/7 Introduce S_GENERATED_KEYS state and generate keys only when authenticated
In reply to Arne SchwabeSince generating data channel does not happen when we have reach the S_ACTIVE/S_GOT_KEY state anymore like it used to be before NCP, the state that data channel keys deserves its own state in the state machine.
The changes done by this commit are rather intrusive since they move the key generation to a completely different place and also rely on the state machine to decide if keys should be generated rather than on the complicated conditions that were implemented in the key_method_2_write/read methods.
A (intended) side effect of this change is that sessions that are still in deferred state (ks->authenticated == KS_DEFERRED) will not have data channel keys generated. This avoids corner cases where not fully authenticated sessions might leak data.
--- src/openvpn/forward.h | 2 +- src/openvpn/init.c | 1 + src/openvpn/ssl.c | 88 +++++++++++++++++----------------------- src/openvpn/ssl.h | 10 +++++ src/openvpn/ssl_common.h | 9 +++- 5 files changed, 57 insertions(+), 53 deletions(-)
src/openvpn/forward.h +1 -1
@@ -450,7 +450,7 @@ connection_established(struct context *c) {if (c->c2.tls_multi) { -return c->c2.tls_multi->multi_state >=CAS_CONNECT_DONE ; +return c->c2.tls_multi->multi_state >=CAS_WAITING_OPTIONS_IMPORT ; }else {
src/openvpn/init.c +1 -0
@@ -2202,6 +2202,7 @@ do_up(struct context *c, bool pulled_options, unsigned int option_types_found) } c->c2.do_up_ran = true; + c->c2.tls_multi->multi_state =CAS_CONNECT_DONE ; }return true; }
src/openvpn/ssl.c +38 -50
156 lines not on this page · raw message
src/openvpn/ssl.h +10 -0
@@ -615,4 +615,14 @@ show_available_tls_ciphers(const char *cipher_list,const char *cipher_list_tls13, const char *tls_cert_profile); + +/** +* Generate data channel keys for the supplied TLS session. +* +* This erases the source material used to generate the data channel keys, and +* can thus be called only once per session. +*/ +bool +tls_session_generate_data_channel_keys (struct tls_session *session); +#endif /* ifndef OPENVPN_SSL_H */
src/openvpn/ssl_common.h +7 -2
@@ -64,7 +64,8 @@* material. * -# \c S_GOT_KEY, have received remote part of \c key_source2 random * material. -* -# \c S_ACTIVE, normal operation +* -# \c S_ACTIVE, control channel successfully established +* -# \c S_GENERATED_KEYS, the * * Servers follow the same order, except for \c S_SENT_KEY and \c * S_GOT_KEY being reversed, because the server first receives the
@@ -92,7 +93,10 @@#define S_ACTIVE 6/**< Operational \c key_state state * immediately after negotiation has * completed while still within the -* handshake window. */ +* handshake window, deferred auth, client +* connect and can still +* be pending. */ +#define S_GENERATED_KEYS 7/**< The data channel keys have been generated */ /* Note that earlier versions also had a S_OP_NORMAL state that was * virtually identical with S_ACTIVE and the code still assumes everything * >= S_ACTIVE to be fully operational */
@@ -517,6 +521,7 @@ enum multi_status {CAS_PENDING_DEFERRED ,CAS_PENDING_DEFERRED_PARTIAL ,/**< at least handler succeeded, no result yet*/ CAS_FAILED , +CAS_WAITING_OPTIONS_IMPORT ,/**< client with pull or p2p waiting for first time options import */ CAS_CONNECT_DONE, };
Signature · 1 line
2.31.1
PATCH 7/7 Move auth_token_state_flags to tls_session and cleanup initial_token
In reply to Arne SchwabeThe usage of the auth_token_state_flags is tied to the authentication. The other authentication related flags and status are in the tls_session struct instead of the tls_multi struct. Move auth_token_state_flags to the right place.
This also changes that auth_token_initial is set when the token is initially generated instead when pushing the token. Even I don't know anymore why I did it in this way in the first place. Also use multi->auth_token_initial as source for the sesssion ID since it should now always be available.
--- src/openvpn/auth_token.c | 22 ++++++++++++++++++---- src/openvpn/push.c | 8 -------- src/openvpn/ssl_verify.c | 5 ++++- tests/unit_tests/openvpn/test_auth_token.c | 14 ++++++++++---- 4 files changed, 32 insertions(+), 17 deletions(-)
src/openvpn/auth_token.c +18 -4
@@ -109,11 +109,11 @@ add_session_token_env(struct tls_session *session, struct tls_multi *multi,/* * No session before, generate a new session token for the new session */ -if (!multi->auth_token) +if (!multi->auth_token_initial) {generate_auth_token (up, multi); } - session_id_source = multi->auth_token; + session_id_source = multi->auth_token_initial; }/* * In the auth-token the auth token is already base64 encoded
@@ -184,7 +184,7 @@ generate_auth_token(const struct user_pass *up, struct tls_multi *multi)uint8_t sessid[AUTH_TOKEN_SESSION_ID_LEN ]; -if (multi->auth_token) +if (multi->auth_token_initial) {/* Just enough space to fit 8 bytes+ 1 extra to decode a non padded * base64 string (multiple of 3 bytes). 9 bytes => 12 bytes base64
@@ -192,13 +192,18 @@ generate_auth_token(const struct user_pass *up, struct tls_multi *multi)*/ char old_tstamp_decode[9 ]; +/* Make a copy of the string to not modify multi->auth_token_initial */ +char * initial_token_copy =string_alloc (multi->auth_token_initial, &gc); +/* * reuse the same session id and timestamp and null terminate it at * for base64 decode it only decodes the session id part of it */ -char *old_sessid = multi->auth_token +strlen (SESSION_ID_PREFIX ); +char *old_sessid =initial_token_copy +strlen (SESSION_ID_PREFIX );char *old_tsamp_initial = old_sessid +AUTH_TOKEN_SESSION_ID_LEN *8 /6 ; + + old_tsamp_initial[12 ] ='\0' ;ASSERT (openvpn_base64_decode (old_tsamp_initial, old_tstamp_decode,9 ) ==9 );
@@ -277,6 +282,15 @@ generate_auth_token(const struct user_pass *up, struct tls_multi *multi)dmsg (D_SHOW_KEYS ,"Generated token for client: %s (%s)" , multi->auth_token, up->username); +if (!multi->auth_token_initial) + { +/* +* Save the initial auth token for clients that ignore +* the updates to the token +*/ + multi->auth_token_initial =strdup (multi->auth_token); + } +gc_free (&gc); }
src/openvpn/push.c +0 -8
@@ -523,14 +523,6 @@ prepare_auth_token_push_reply(struct tls_multi *tls_multi, struct gc_arena *gc,push_option_fmt (gc, push_list,M_USAGE ,"auth-token %s" , tls_multi->auth_token); -if (!tls_multi->auth_token_initial) - { -/* -* Save the initial auth token for clients that ignore -* the updates to the token -*/ - tls_multi->auth_token_initial =strdup (tls_multi->auth_token); - } } }
src/openvpn/ssl_verify.c +4 -1
@@ -1544,6 +1544,7 @@ verify_user_pass(struct user_pass *up, struct tls_multi *multi,*/ if (session->opt->auth_token_generate &&is_auth_token (up->password)) { + ks->auth_token_state_flags =verify_auth_token (up, multi, session);if (session->opt->auth_token_call_auth) {
@@ -1672,7 +1673,9 @@ verify_user_pass(struct user_pass *up, struct tls_multi *multi,* Otherwise the auth-token get pushed out as part of the "normal" * push-reply */ -if (multi->auth_token_initial) +bool initial_connect = session->key[KS_PRIMARY ].key_id ==0 ; + +if (multi->auth_token_initial && !initial_connect) {/* * We do not explicitly schedule the sending of the
tests/unit_tests/openvpn/test_auth_token.c +10 -4
@@ -174,7 +174,9 @@ auth_token_test_timeout(void **state) now =100000 ;generate_auth_token (&ctx->up, &ctx->multi); +strcpy (ctx->up.password, ctx->multi.auth_token); + ctx->multi.auth_token_initial =NULL ;/* No time has passed */ assert_int_equal(verify_auth_token(&ctx->up, &ctx->multi, ctx->session),
@@ -244,10 +246,10 @@ auth_token_test_known_keys(void **state) now =0 ;/* Preload the session id so the same session id is used here */ - ctx->multi.auth_token =strdup (now0key0); + ctx->multi.auth_token_initial =strdup (now0key0);/* Zero the hmac part to ensure we have a newly generated token */ -zerohmac (ctx->multi.auth_token); +zerohmac (ctx->multi.auth_token_initial);generate_auth_token (&ctx->up, &ctx->multi);
@@ -305,13 +307,16 @@ auth_token_test_env(void **state) {struct test_context *ctx = (struct test_context *) *state; +struct key_state *ks = &ctx->multi.session[TM_ACTIVE ].key[KS_PRIMARY ]; ks->auth_token_state_flags =0 ; + ctx->multi.auth_token =NULL ;add_session_token_env (ctx->session, &ctx->multi, &ctx->up);assert_string_equal (lastsesion_statevalue,"Initial" ); + ks->auth_token_state_flags =0 ;strcpy (ctx->up.password, now0key0);add_session_token_env (ctx->session, &ctx->multi, &ctx->up);
@@ -331,6 +336,7 @@ auth_token_test_env(void **state) ks->auth_token_state_flags =AUTH_TOKEN_HMAC_OK |AUTH_TOKEN_EXPIRED |AUTH_TOKEN_VALID_EMPTYUSER ;add_session_token_env (ctx->session, &ctx->multi, &ctx->up); +assert_string_equal (lastsesion_statevalue,"ExpiredEmptyUser" ); }
@@ -341,13 +347,13 @@ auth_token_test_random_keys(void **state) now =0x5c331e9c ;/* Preload the session id so the same session id is used here */ - ctx->multi.auth_token =strdup (random_token); + ctx->multi.auth_token_initial =strdup (random_token);free_key_ctx (&ctx->multi.opt.auth_token_key);auth_token_init_secret (&ctx->multi.opt.auth_token_key, random_key, true);/* Zero the hmac part to ensure we have a newly generated token */ -zerohmac (ctx->multi.auth_token); +zerohmac (ctx->multi.auth_token_initial);generate_auth_token (&ctx->up, &ctx->multi);
Signature · 1 line
2.31.1
Hi,
Quoted 6 lines · Arne Schwabe
tls_pre_encryptmainly performs the task of selecting the primary encryption key but also performs other minor tasks. To allow only querying for the key that should be used for encryption extract this part of the function into its own function.Signed-off-by Arne Schwabe <[email protected]...>
The patch is basically isolating a portion of code in a new helper function and then invoking it where it once was.
No functional change involved, no code change involved either (except for removing useless parenthesis in an if condition).
All compile tests passed.
Basic connectivity tests and basic renegotiation tests passed.
Regards,
Signature · 1 line
Antonio Quartulli
As Antonio says, "just moving around code" (and cleaning up a messy if() statement - so besides this, it's really trivial to see in diff)
Your patch has been applied to the master branch.
commit 26e40c48b89478cb53d6c2733b346e6dbdc7480b
Author: Arne Schwabe Date: Thu Apr 22 17:17:18 2021 +0200
Move tls_select_primary_key into its own function
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]/msg22198.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
PATCH v2 3/7 Return cached result in tls_authentication_status
In reply to Arne Schwabetls_authentication_status does caching to avoid file I/O more than every TLS_MULTI_AUTH_STATUS_INTERVAL (10s) per connection. But counter-intuitively it does not return the cached result but rather TLS_AUTHENTICATION_UNDEFINED if the cache is not refreshed by the call.
This is workarounded by forcing a refresh in some areas of the code (latency = 0).
This patch changes the behaviour by always returning the last known status and only updating the file status when the i/o timeout for the caches is reached.
The patch also changes the DEFINE enum into a real enum.
Patch v2: only update tas_cache_last_udpate when actually updating the cache.
--- src/openvpn/multi.c | 2 +- src/openvpn/push.c | 3 ++- src/openvpn/ssl_common.h | 16 +++++++++--- src/openvpn/ssl_verify.c | 53 ++++++++++++++++++++++------------------ src/openvpn/ssl_verify.h | 3 +-- 5 files changed, 46 insertions(+), 31 deletions(-)
src/openvpn/multi.c +1 -1
@@ -2596,7 +2596,7 @@ static const multi_client_connect_handler client_connect_handlers[] = {static void multi_connection_established (struct multi_context *m,struct multi_instance *mi) { -if (tls_authentication_status (mi->context.c2.tls_multi,0 ) +if (tls_authentication_status (mi->context.c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) !=TLS_AUTHENTICATION_SUCCEEDED ) {return ;
src/openvpn/push.c +2 -1
@@ -855,7 +855,8 @@ process_incoming_push_request(struct context *c) {int ret =PUSH_MSG_ERROR ; -if (tls_authentication_status (c->c2.tls_multi,0 ) ==TLS_AUTHENTICATION_FAILED + +if (tls_authentication_status (c->c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) ==TLS_AUTHENTICATION_FAILED || c->c2.tls_multi->multi_state ==CAS_FAILED ) {const char *client_reason =tls_client_reason (c->c2.tls_multi);
src/openvpn/ssl_common.h +13 -3
@@ -152,6 +152,15 @@ struct auth_deferred_statusunsigned int auth_control_status; }; +/* key_state_test_auth_control_file return values, these specify the +* current status of a deferred authentication */ +enum auth_deferred_result { +ACF_PENDING ,/**< deferred auth still pending */ +ACF_SUCCEEDED ,/**< deferred auth has suceeded */ +ACF_DISABLED ,/**< deferred auth is not used */ +ACF_FAILED /**< deferred auth has failed */ +}; +/** * Security parameter state of one TLS and data channel %key session. * @ingroup control_processor
@@ -219,7 +228,7 @@ struct key_state#ifdef ENABLE_MANAGEMENT unsigned int mda_key_id; -unsigned int mda_status; +enum auth_deferred_result mda_status;#endif time_t acf_last_mod;
@@ -561,8 +570,9 @@ struct tls_multichar *locked_username;struct cert_hash_set *locked_cert_hash_set; -/* Time of last call to tls_authentication_status */ -time_t tas_last; +/* Time of last when we updated the cached state of +* tls_authentication_status deferred files */ +time_t tas_cache_last_update;/* * An error message to send to client on AUTH_FAILED
src/openvpn/ssl_verify.c +29 -24
99 lines not on this page · raw message
src/openvpn/ssl_verify.h +1 -2
@@ -69,8 +69,7 @@ enum tls_auth_status {TLS_AUTHENTICATION_SUCCEEDED =0 ,TLS_AUTHENTICATION_FAILED =1 , -TLS_AUTHENTICATION_DEFERRED =2 , - TLS_AUTHENTICATION_UNDEFINED=3 + TLS_AUTHENTICATION_DEFERRED=2 };/**
Signature · 1 line
2.31.1
Re: [Openvpn-devel] [PATCH v2 3/7] Return cached result in tls_authentication_status
Hi,
Something in this patch is not working as expected.
I tested with master + this patch.
When a client connects and inputs the wrong password, the server goes into a loop:
2021-05-06 00:50:46 10.10.10.2:1194 Delayed exit in 5 seconds 2021-05-06 00:50:46 10.10.10.2:1194 SENT CONTROL [client]: 'AUTH_FAILED' (status=1) 2021-05-06 00:50:46 10.10.10.2:1194 Delayed exit in 5 seconds 2021-05-06 00:50:46 10.10.10.2:1194 SENT CONTROL [client]: 'AUTH_FAILED' (status=1) 2021-05-06 00:50:46 10.10.10.2:1194 Delayed exit in 5 seconds 2021-05-06 00:50:46 10.10.10.2:1194 SENT CONTROL [client]: 'AUTH_FAILED'
(status=1)
and goes on...
Without this patch I don't see this problem.
After adding some debug messages I saw that without this patch the return value of tls_authentication_status() in tls_multi_process() is *always* UNDEFINED, also after the auth failure.
This said I stopped digging there because the bed is calling...
Arne, maybe you can spot what's wrong?
Regards,
Quoted 218 lines · Arne Schwabe
tls_authentication_statusdoes caching to avoid file I/O more than everyTLS_MULTI_AUTH_STATUS_INTERVAL(10s) per connection. But counter-intuitively it does not return the cached result but ratherTLS_AUTHENTICATION_UNDEFINEDif the cache is not refreshed by the call.This is workarounded by forcing a refresh in some areas of the code (latency = 0).
This patch changes the behaviour by always returning the last known status and only updating the file status when the i/o timeout for the caches is reached.
The patch also changes the DEFINE enum into a real enum.
Patch v2: only update
tas_cache_last_udpatewhen actually updating the cache.Signed-off-by Arne Schwabe <[email protected]...>--- src/openvpn/multi.c | 2 +- src/openvpn/push.c | 3 ++- src/openvpn/ssl_common.h | 16 +++++++++--- src/openvpn/ssl_verify.c | 53 ++++++++++++++++++++++------------------ src/openvpn/ssl_verify.h | 3 +-- 5 files changed, 46 insertions(+), 31 deletions(-)src/openvpn/multi.c +1
-1@@ -2596,7 +2596,7 @@ static const multi_client_connect_handler client_connect_handlers[] = {static void multi_connection_established (struct multi_context *m,struct multi_instance *mi) { -if (tls_authentication_status (mi->context.c2.tls_multi,0 ) +if (tls_authentication_status (mi->context.c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) !=TLS_AUTHENTICATION_SUCCEEDED ) {return ;src/openvpn/push.c +2
-1@@ -855,7 +855,8 @@ process_incoming_push_request(struct context *c) {int ret =PUSH_MSG_ERROR ; -if (tls_authentication_status (c->c2.tls_multi,0 ) ==TLS_AUTHENTICATION_FAILED + +if (tls_authentication_status (c->c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) ==TLS_AUTHENTICATION_FAILED || c->c2.tls_multi->multi_state ==CAS_FAILED ) {const char *client_reason =tls_client_reason (c->c2.tls_multi);src/openvpn/ssl_common.h +13
-3@@ -152,6 +152,15 @@ struct auth_deferred_statusunsigned int auth_control_status; }; +/* key_state_test_auth_control_file return values, these specify the +* current status of a deferred authentication */ +enum auth_deferred_result { +ACF_PENDING ,/**< deferred auth still pending */ +ACF_SUCCEEDED ,/**< deferred auth has suceeded */ +ACF_DISABLED ,/**< deferred auth is not used */ +ACF_FAILED /**< deferred auth has failed */ +}; +/** * Security parameter state of one TLS and data channel %key session. * @ingroup control_processor @@ -219,7 +228,7 @@ struct key_state#ifdef ENABLE_MANAGEMENT unsigned int mda_key_id; -unsigned int mda_status; +enum auth_deferred_result mda_status;#endif time_t acf_last_mod;@@ -561,8 +570,9 @@ struct tls_multichar *locked_username;struct cert_hash_set *locked_cert_hash_set; -/* Time of last call to tls_authentication_status */ -time_t tas_last; +/* Time of last when we updated the cached state of +* tls_authentication_status deferred files */ +time_t tas_cache_last_update;/* * An error message to send to client on AUTH_FAILED src/openvpn/ssl_verify.c +29
-24@@ -845,13 +845,6 @@ cleanup:* user/password authentication. *************************************************************************** */ -/* key_state_test_auth_control_file return values, -* NOTE: acf_merge indexing depends on these values */ -#define ACF_UNDEFINED 0 -#define ACF_SUCCEEDED 1 -#define ACF_DISABLED 2 -#define ACF_FAILED 3 -void auth_set_client_reason (struct tls_multi *multi,const char *client_reason) {@@ -866,7 +859,7 @@ auth_set_client_reason(struct tls_multi *multi, const char *client_reason)#ifdef ENABLE_MANAGEMENT -static inline unsigned int +static inline enum auth_deferred_result man_def_auth_test (const struct key_state *ks) {if (management_enable_def_auth (management))@@ -1041,13 +1034,23 @@ key_state_gen_auth_control_files(struct auth_deferred_status *ads,return (acf && apf); } -static unsigned int -key_state_test_auth_control_file (struct auth_deferred_status *ads) +/** +* Checks the control status from a file. The function will try to read +* and update the cached status if the status is still pending and the paramter +* cached is false. The function returns the +* +* +* @param ads deferred status control structure +* @param cached Return only cached status +* @return +*/ +static enum auth_deferred_result +key_state_test_auth_control_file (struct auth_deferred_status *ads,bool cached) {if (ads->auth_control_file) {unsigned int ret = ads->auth_control_status; -if (ret ==ACF_UNDEFINED ) +if (ret ==ACF_PENDING && !cached) {FILE *fp =fopen (ads->auth_control_file,"r" );if (fp)@@ -1084,11 +1087,7 @@ tls_authentication_status(struct tls_multi *multi, const int latency)/* at least one key already failed authentication */ bool failed_auth = false; -if (latency && multi->tas_last + latency >= now) - { -return TLS_AUTHENTICATION_UNDEFINED ; - } - multi->tas_last = now; +bool cached = multi->tas_cache_last_update + latency >= now;for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) {@@ -1102,11 +1101,11 @@ tls_authentication_status(struct tls_multi *multi, const int latency) }else { -unsigned int auth_plugin =ACF_DISABLED ; -unsigned int auth_script =ACF_DISABLED ; -unsigned int auth_man =ACF_DISABLED ; - auth_plugin =key_state_test_auth_control_file (&ks->plugin_auth); - auth_script =key_state_test_auth_control_file (&ks->script_auth); +enum auth_deferred_result auth_plugin =ACF_DISABLED ; +enum auth_deferred_result auth_script =ACF_DISABLED ; +enum auth_deferred_result auth_man =ACF_DISABLED ; + auth_plugin =key_state_test_auth_control_file (&ks->plugin_auth, cached); + auth_script =key_state_test_auth_control_file (&ks->script_auth, cached);#ifdef ENABLE_MANAGEMENT auth_man =man_def_auth_test (ks);#endif @@ -1118,9 +1117,9 @@ tls_authentication_status(struct tls_multi *multi, const int latency) ks->authenticated =KS_AUTH_FALSE ; failed_auth = true; } -else if (auth_plugin ==ACF_UNDEFINED - || auth_script ==ACF_UNDEFINED - || auth_man ==ACF_UNDEFINED ) +else if (auth_plugin ==ACF_PENDING + || auth_script ==ACF_PENDING + || auth_man ==ACF_PENDING ) {if (now < ks->auth_deferred_expire) {@@ -1140,6 +1139,12 @@ tls_authentication_status(struct tls_multi *multi, const int latency) } } +/* we did not rely on a cached result, remember the cache update time */ +if (!cached) + { + multi->tas_cache_last_update = now; + } +#if 0 dmsg (D_TLS_ERRORS ,"TAS: a=%d s=%d d=%d f=%d" , active, success, deferred, failed_auth);#endif src/openvpn/ssl_verify.h +1
-2@@ -69,8 +69,7 @@ enum tls_auth_status {TLS_AUTHENTICATION_SUCCEEDED =0 ,TLS_AUTHENTICATION_FAILED =1 , -TLS_AUTHENTICATION_DEFERRED =2 , - TLS_AUTHENTICATION_UNDEFINED=3 + TLS_AUTHENTICATION_DEFERRED=2 };/**
Signature · 1 line
Antonio Quartulli
PATCH 1/1 Add connection_established as state in tls_multi->context_auth
In reply to Arne Schwabethe socket_info->connection_establish is set through link_socket_set_outgoing_addr when we reach FULL_SYNC. This patch introduces a new state in context_auth that replaces the connection_established state for TLS connections. This make the state machine easier to understand.
--- src/openvpn/forward.c | 6 +++--- src/openvpn/forward.h | 13 ++++++++++++- src/openvpn/multi.c | 15 ++++++++------- src/openvpn/occ.c | 2 +- src/openvpn/openvpn.h | 4 +++- src/openvpn/push.c | 2 +- src/openvpn/ssl.c | 37 +++++++++++++++++++++++++++---------- src/openvpn/ssl_common.h | 12 +++++++++--- 8 files changed, 64 insertions(+), 27 deletions(-)
src/openvpn/forward.c +3 -3
@@ -280,7 +280,7 @@ void check_connection_established(struct context *c) { -if (CONNECTION_ESTABLISHED (c)) +if (connection_established (c)) {/* if --pull was specified, send a push request to server */ if (c->c2.tls_multi && c->options.pull)
@@ -536,7 +536,7 @@ encrypt_sign(struct context *c, bool comp_frag)* has not yet succeeded. In non-TLS tls_multi mode is not defined * and we always pass packets. */ -if (c->c2.tls_multi && c->c2.tls_multi->multi_state !=CAS_SUCCEEDED ) +if (c->c2.tls_multi && c->c2.tls_multi->multi_state <CAS_CONNECT_DONE ) { c->c2.buf.len =0 ; }
@@ -971,7 +971,7 @@ process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, boo* has not yet succeeded. In non-TLS mode tls_multi is not defined * and we always pass packets. */ -if (c->c2.tls_multi && c->c2.tls_multi->multi_state !=CAS_SUCCEEDED ) +if (c->c2.tls_multi && c->c2.tls_multi->multi_state <CAS_CONNECT_DONE ) { c->c2.buf.len =0 ; }
src/openvpn/forward.h +12 -1
@@ -445,6 +445,17 @@ io_wait(struct context *c, const unsigned int flags) } } -#define CONNECTION_ESTABLISHED (c) (get_link_socket_info(c)->connection_established) +static inline bool +connection_established (struct context *c) +{ +if (c->c2.tls_multi) + { +return c->c2.tls_multi->multi_state >=CAS_CONNECT_DONE ; + } +else + { +return get_link_socket_info (c)->connection_established; + } +}#endif /* FORWARD_H */
src/openvpn/multi.c +8 -7
@@ -674,7 +674,8 @@ multi_close_instance(struct multi_context *m,#ifdef ENABLE_MANAGEMENT set_cc_config (mi,NULL );#endif -if (mi->context.c2.tls_multi->multi_state ==CAS_SUCCEEDED ) + +if (mi->context.c2.tls_multi->multi_state >=CAS_CONNECT_DONE ) {multi_client_disconnect_script (mi); }
@@ -775,7 +776,7 @@ multi_create_instance(struct multi_context *m, const struct mroute_addr *real) goto err; } - mi->context.c2.tls_multi->multi_state =CAS_PENDING ; + mi->context.c2.tls_multi->multi_state =CAS_NOT_CONNECTED ;if (hash_n_elements (m->hash) >= m->max_clients) {
@@ -2407,7 +2408,7 @@ multi_client_connect_late_setup(struct multi_context *m, mi->reporting_addr_ipv6 = mi->context.c2.push_ifconfig_ipv6_local;/* set context-level authentication flag */ - mi->context.c2.tls_multi->multi_state =CAS_SUCCEEDED ; + mi->context.c2.tls_multi->multi_state =CAS_CONNECT_DONE ;/* authentication complete, calculate dynamic client specific options */ if (!multi_client_set_protocol_options (&mi->context))
@@ -2650,9 +2651,9 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)case CC_RET_DEFERRED :/* -* we already set client_connect_status to DEFERRED_RESULT or +* we already set multi_status to DEFERRED_RESULT or * DEFERRED_NO_RESULT. We just return -* from the function as having client_connect_status +* from the function as having multi_status */ return ;
@@ -3004,8 +3005,8 @@ multi_process_post(struct multi_context *m, struct multi_instance *mi, const uns {/* connection is "established" when SSL/TLS key negotiation succeeds * and (if specified) auth user/pass succeeds */ -if (is_cas_pending (mi->context.c2.tls_multi->multi_state) - &&CONNECTION_ESTABLISHED (&mi->context)) + +if (is_cas_pending (mi->context.c2.tls_multi->multi_state)) {multi_connection_established (m, mi); }
src/openvpn/occ.c +1 -1
@@ -185,7 +185,7 @@ check_send_occ_req_dowork(struct context *c)void check_send_occ_load_test_dowork (struct context *c) { -if (CONNECTION_ESTABLISHED (c)) +if (connection_established (c)) {const struct mtu_load_test *entry;
src/openvpn/openvpn.h +3 -1
@@ -206,7 +206,7 @@ struct context_1static inline bool -is_cas_pending (enum client_connect_status cas) +is_cas_pending (enum multi_status cas) {return cas ==CAS_PENDING || cas ==CAS_PENDING_DEFERRED || cas ==CAS_PENDING_DEFERRED_PARTIAL ;
@@ -247,6 +247,8 @@ struct context_2struct link_socket *link_socket;/* socket used for TCP/UDP connection to remote */ bool link_socket_owned; + +/** This variable is used instead link_socket->info for P2MP UDP childs */ struct link_socket_info *link_socket_info;const struct link_socket *accept_from;/* possibly do accept() on a parent link_socket */
src/openvpn/push.c +1 -1
@@ -862,7 +862,7 @@ process_incoming_push_request(struct context *c)send_auth_failed (c, client_reason); ret =PUSH_MSG_AUTH_FAILURE ; } -else if (c->c2.tls_multi->multi_state ==CAS_SUCCEEDED ) +else if (c->c2.tls_multi->multi_state >=CAS_CONNECT_DONE ) {time_t now;
src/openvpn/ssl.c +27 -10
@@ -2381,19 +2381,21 @@ key_method_2_write(struct buffer *buf, struct tls_multi *multi, struct tls_sessi* If we're a p2mp server to allow NCP, the first key * generation is postponed until after the connect script finished and the * NCP options can be processed. Since that always happens at after connect -* script options are available the CAS_SUCCEEDED status is identical to -* NCP options are processed and we have no extra state for NCP finished. +* script options are available the CAS_CONNECT_DONE status is identical to +* NCP options are processed and do not wait for NCP being finished. */ -if (session->opt->server && (session->opt->mode !=MODE_SERVER - || multi->multi_state ==CAS_SUCCEEDED )) +if (ks->authenticated >KS_AUTH_FALSE && session->opt->mode ==MODE_SERVER + && (multi->multi_state >=CAS_CONNECT_DONE || ks->key_id >0 )) { -if (ks->authenticated >KS_AUTH_FALSE ) +/* if key_id >= 1, is a renegotiation, so we use the already established +* parameters and do not need to delay anything. */ + +/* key-id == 0 and multi_state >= CAS_CONNECT_DONE is a special case of +* the server reusing the session of a reconnecting client. */ +if (!tls_session_generate_data_channel_keys (session)) { -if (!tls_session_generate_data_channel_keys (session)) - { -msg (D_TLS_ERRORS ,"TLS Error: server generate_key_expansion failed" ); - goto error; - } +msg (D_TLS_ERRORS ,"TLS Error: server generate_key_expansion failed" ); + goto error; } }
@@ -2801,6 +2803,21 @@ tls_process(struct tls_multi *multi,/* 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_PENDING ; + } +else + { +/* Skip the connect script related states */ + multi->multi_state =CAS_CONNECT_DONE ; + } + } +/* Flush any payload packets that were buffered before our state transitioned to S_ACTIVE */ flush_payload_buffer (ks);
src/openvpn/ssl_common.h +9 -3
@@ -496,12 +496,18 @@ struct tls_session/* client authentication state, CAS_SUCCEEDED must be 0 since * non multi code path still checks this variable but does not initialise it * so the code depends on zero initialisation */ -enum client_connect_status { -CAS_SUCCEEDED =0 , + +/* CAS_NOT_CONNECTED is the initial state for every context. When the *first* +* tls_session reaches S_ACTIVE, this state machine moves to CAS_PENDING (server) +* or CAS_CONNECT_DONE (client/p2p) as clients skip the stages associated with +* connect scripts/plugins */ +enum multi_status { +CAS_NOT_CONNECTED ,CAS_PENDING ,CAS_PENDING_DEFERRED ,CAS_PENDING_DEFERRED_PARTIAL ,/**< at least handler succeeded, no result yet*/ CAS_FAILED , +CAS_CONNECT_DONE , };
@@ -540,7 +546,7 @@ struct tls_multiint n_sessions;/**< Number of sessions negotiated thus * far. */ -enum client_connect_status multi_state; +enum multi_status multi_state;/* * Number of errors.
Signature · 1 line
2.31.1
Re: [Openvpn-devel] [PATCH] Add connection_established as state in tls_multi->context_auth
Hi,
Quoted 7 lines · Arne Schwabe
the
socket_info->connection_establishis set throughlink_socket_set_outgoing_addrwhen we reachFULL_SYNC. This patch introduces a new state incontext_auththat replaces theconnection_establishedstate for TLS connections. This make the state machine easier to understand.Signed-off-by Arne Schwabe <[email protected]...>
This change indeed is another step towards the TLS state machine simplification.
It gives a better meaning to the various states, introducing a first concept of "order", with the first state being "offline" and the last state being "online".
Compiled on my zoo and tested in my basic linux setup with auth and deferred-auth.
Regards,
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH] Add connection_established as state in tls_multi->context_auth
Hi,
Quoted 8 lines · Antonio Quartulli
Quoted 7 lines · Arne Schwabe
the
socket_info->connection_establishis set throughlink_socket_set_outgoing_addrwhen we reachFULL_SYNC. This patch introduces a new state incontext_auththat replaces theconnection_establishedstate for TLS connections. This make the state machine easier to understand.Signed-off-by Arne Schwabe <[email protected]...>
[..]
Quoted 1 line
This patch, applied on top of d49df6bdde0592c ("master as of today, with the exponential backoff patch applied") breaks p2p TLS mode on the server side.
Authentication and PUSH_REPLY works fine, but afterwards the server complains about keys
May 15 20:01:44 gentoo tap-tcp-p2p[31731]: Key [AF_INET6]::ffff:194.97.140.21:60334 [0] not initialized (yet), dropping packet. May 15 20:01:47 gentoo tap-tcp-p2p[31731]: Key [AF_INET6]::ffff:194.97.140.21:60334 [0] not initialized (yet), dropping packet.
(specifially, the test case is: TCP, TAP, tcp-server on the server, and "--client" on the client for "9", and "--tls-client" for "9a" - both break)
The client in this test runs about 8 weeks old master (7064ccb9fd3578c0).
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 5/7 Extracting key_state deferred auth status update into function
In reply to Arne SchwabeThis extract the update of a deferred key status into into own function.
Patch v2: Do not ignore auth_deferred_expire. Minor format changes.
--- src/openvpn/ssl_verify.c | 91 ++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 32 deletions(-)
src/openvpn/ssl_verify.c +59 -32
@@ -1073,6 +1073,57 @@ key_state_test_auth_control_file(struct auth_deferred_status *ads, bool cached)return ACF_DISABLED ; } +/** +* This method takes a key_state and if updates the state +* of the key if it is deferred. +* @param cached If auth control files should be tried to be opened or th +* cached results should be used +* @param ks The key_state to update +*/ +static void +update_key_auth_status (bool cached,struct key_state *ks) +{ +if (ks->authenticated ==KS_AUTH_FALSE ) + { +return ; + } +else + { +enum auth_deferred_result auth_plugin =ACF_DISABLED ; +enum auth_deferred_result auth_script =ACF_DISABLED ; +enum auth_deferred_result auth_man =ACF_DISABLED ; + auth_plugin =key_state_test_auth_control_file (&ks->plugin_auth, cached); + auth_script =key_state_test_auth_control_file (&ks->script_auth, cached); +#ifdef ENABLE_MANAGEMENT + auth_man =man_def_auth_test (ks); +#endif +ASSERT (auth_plugin <4 && auth_script <4 && auth_man <4 ); + +if (auth_plugin ==ACF_FAILED || auth_script ==ACF_FAILED + || auth_man ==ACF_FAILED ) + { + ks->authenticated =KS_AUTH_FALSE ; +return ; + } +else if (auth_plugin ==ACF_PENDING || auth_script ==ACF_PENDING + || auth_man ==ACF_PENDING ) + { +if (now > ks->auth_deferred_expire) + { +/* Window to authenticate the key has expired, mark +* the key as unauthenticated */ + ks->authenticated =KS_AUTH_FALSE ; + } + } +else + { +/* auth_plugin and auth_man are either ACF_DISABLED or ACF_SUCCEDED */ + ks->authenticated =KS_AUTH_TRUE ; + } + } +} + +enum tls_auth_status tls_authentication_status (struct tls_multi *multi,const int latency) {
@@ -1096,43 +1147,19 @@ tls_authentication_status(struct tls_multi *multi, const int latency)if (TLS_AUTHENTICATED (multi, ks)) { active++; +update_key_auth_status (cached, ks); +if (ks->authenticated ==KS_AUTH_FALSE ) { failed_auth = true; } -else +else if (ks->authenticated ==KS_AUTH_DEFERRED ) { -enum auth_deferred_result auth_plugin =ACF_DISABLED ; -enum auth_deferred_result auth_script =ACF_DISABLED ; -enum auth_deferred_result auth_man =ACF_DISABLED ; - auth_plugin =key_state_test_auth_control_file (&ks->plugin_auth, cached); - auth_script =key_state_test_auth_control_file (&ks->script_auth, cached); -#ifdef ENABLE_MANAGEMENT - auth_man =man_def_auth_test (ks); -#endif -ASSERT (auth_plugin <4 && auth_script <4 && auth_man <4 ); - -if (auth_plugin ==ACF_FAILED || auth_script ==ACF_FAILED - || auth_man ==ACF_FAILED ) - { - ks->authenticated =KS_AUTH_FALSE ; - failed_auth = true; - } -else if (auth_plugin ==ACF_PENDING - || auth_script ==ACF_PENDING - || auth_man ==ACF_PENDING ) - { -if (now < ks->auth_deferred_expire) - { - deferred = true; - } - } -else - { -/* auth_plugin and auth_man are either ACF_DISABLED or ACF_SUCCEDED */ - success = true; - ks->authenticated =KS_AUTH_TRUE ; - } + deferred = true; + } +else if (ks->authenticated ==KS_AUTH_TRUE ) + { + success = true; } } }
Signature · 1 line
2.31.1
PATCH v3 1/1 Return cached result in tls_authentication_status
In reply to Arne Schwabetls_authentication_status does caching to avoid file I/O more than every TLS_MULTI_AUTH_STATUS_INTERVAL (10s) per connection. But counter-intuitively it does not return the cached result but rather TLS_AUTHENTICATION_UNDEFINED if the cache is not refreshed by the call.
This is workarounded by forcing a refresh in some areas of the code (latency = 0).
This patch changes the behaviour by always returning the last known status and only updating the file status when the i/o timeout for the caches is reached.
The old logic in send_auth_failed is fragile in the sense that if it is called again while an exit is scheduled it will reset the timer to 5s again. Since we now always report the status from tls_authentication_status() instead only every 10s, this caused OpenVPN to infinitively reset the timer. Fix this by only setting the status if no exit is scheduled. The function is still called multiple times but since it is with coarse timer frequency, the 4 extra calls (1 per second) are better than to add more extra code to avoid these calls.
The patch also changes the DEFINE enum into a real enum.
Patch v2: only update tas_cache_last_udpate when actually updating the cache. Patch v3: avoid rearming timer
--- src/openvpn/multi.c | 2 +- src/openvpn/push.c | 11 ++++++++- src/openvpn/ssl_common.h | 16 +++++++++--- src/openvpn/ssl_verify.c | 53 ++++++++++++++++++++++------------------ src/openvpn/ssl_verify.h | 3 +-- 5 files changed, 54 insertions(+), 31 deletions(-)
src/openvpn/multi.c +1 -1
@@ -2596,7 +2596,7 @@ static const multi_client_connect_handler client_connect_handlers[] = {static void multi_connection_established (struct multi_context *m,struct multi_instance *mi) { -if (tls_authentication_status (mi->context.c2.tls_multi,0 ) +if (tls_authentication_status (mi->context.c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) !=TLS_AUTHENTICATION_SUCCEEDED ) {return ;
src/openvpn/push.c +10 -1
@@ -335,10 +335,18 @@ __attribute__ ((format(__printf__, 4, 5)))/* * Send auth failed message from server to client. +* +* Does nothing if an exit is already scheduled */ void send_auth_failed (struct context *c,const char *client_reason) { +if (event_timeout_defined (&c->c2.scheduled_exit)) + { +msg (D_TLS_DEBUG ,"exit already scheduled for context" ); +return ; + } +struct gc_arena gc =gc_new ();static const char auth_failed[] ="AUTH_FAILED" ;size_t len;
@@ -855,7 +863,8 @@ process_incoming_push_request(struct context *c) {int ret =PUSH_MSG_ERROR ; -if (tls_authentication_status (c->c2.tls_multi,0 ) ==TLS_AUTHENTICATION_FAILED + +if (tls_authentication_status (c->c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) ==TLS_AUTHENTICATION_FAILED || c->c2.tls_multi->multi_state ==CAS_FAILED ) {const char *client_reason =tls_client_reason (c->c2.tls_multi);
src/openvpn/ssl_common.h +13 -3
@@ -152,6 +152,15 @@ struct auth_deferred_statusunsigned int auth_control_status; }; +/* key_state_test_auth_control_file return values, these specify the +* current status of a deferred authentication */ +enum auth_deferred_result { +ACF_PENDING ,/**< deferred auth still pending */ +ACF_SUCCEEDED ,/**< deferred auth has suceeded */ +ACF_DISABLED ,/**< deferred auth is not used */ +ACF_FAILED /**< deferred auth has failed */ +}; +/** * Security parameter state of one TLS and data channel %key session. * @ingroup control_processor
@@ -219,7 +228,7 @@ struct key_state#ifdef ENABLE_MANAGEMENT unsigned int mda_key_id; -unsigned int mda_status; +enum auth_deferred_result mda_status;#endif time_t acf_last_mod;
@@ -561,8 +570,9 @@ struct tls_multichar *locked_username;struct cert_hash_set *locked_cert_hash_set; -/* Time of last call to tls_authentication_status */ -time_t tas_last; +/* Time of last when we updated the cached state of +* tls_authentication_status deferred files */ +time_t tas_cache_last_update;/* * An error message to send to client on AUTH_FAILED
src/openvpn/ssl_verify.c +29 -24
99 lines not on this page · raw message
src/openvpn/ssl_verify.h +1 -2
@@ -69,8 +69,7 @@ enum tls_auth_status {TLS_AUTHENTICATION_SUCCEEDED =0 ,TLS_AUTHENTICATION_FAILED =1 , -TLS_AUTHENTICATION_DEFERRED =2 , - TLS_AUTHENTICATION_UNDEFINED=3 + TLS_AUTHENTICATION_DEFERRED=2 };/**
Signature · 1 line
2.31.1
Re: [Openvpn-devel] [PATCH v3] Return cached result in tls_authentication_status
Hi Arne,
after our discussion on IRC I understood you expected this patch to not change the server behaviour.
If something is suboptimal, it means it was suboptimal also before this patch.
However, with your patch I can clearly see a longer delay in returning the AUTH result to the client, in case of deferred auth.
Without your patch (initial lines are the output of my auth-verify script):
PASS=testno FILE=/tmp/openvpn_acf_662a1165ab3293aa3fb053050e1c2463.tmp FAIL
2021-05-06 23:40:28 10.10.10.2:1194 TLS: Username/Password authentication deferred for username 'test' 2021-05-06 23:40:28 10.10.10.2:1194 Control Channel: TLSv1.3, cipher TLSv1.3 TLS_AES_256_GCM_SHA384, peer certificate: 384 bit EC, curve secp384r1, signature: ecdsa-with-SHA256 2021-05-06 23:40:28 10.10.10.2:1194 [client] Peer Connection Initiated with [AF_INET6]::ffff:10.10.10.2:1194 2021-05-06 23:40:29 10.10.10.2:1194 PUSH: Received control message: 'PUSH_REQUEST' 2021-05-06 23:40:29 10.10.10.2:1194 Delayed exit in 5 seconds 2021-05-06 23:40:29 10.10.10.2:1194 SENT CONTROL [client]: 'AUTH_FAILED'
(status=1)
My script uses the deferred logic, but it is super fast (as you can see it fails before the server can even do anything else).
Later, as soon as the first PUSH_REQUEST is received, the server replies immediately with an AUTH_FAILED.
With your patch (script and everything else stays the same):
PASS=testno FILE=/tmp/openvpn_acf_4cdab09a9640e8eb4cacadfc1734bb2c.tmp FAIL
2021-05-06 23:45:22 10.10.10.2:1194 TLS: Username/Password authentication deferred for username 'test' 2021-05-06 23:45:22 10.10.10.2:1194 Control Channel: TLSv1.3, cipher TLSv1.3 TLS_AES_256_GCM_SHA384, peer certificate: 384 bit EC, curve secp384r1, signature: ecdsa-with-SHA256 2021-05-06 23:45:22 10.10.10.2:1194 [client] Peer Connection Initiated with [AF_INET6]::ffff:10.10.10.2:1194 2021-05-06 23:45:23 10.10.10.2:1194 PUSH: Received control message: 'PUSH_REQUEST' 2021-05-06 23:45:28 10.10.10.2:1194 PUSH: Received control message: 'PUSH_REQUEST' 2021-05-06 23:45:33 10.10.10.2:1194 Delayed exit in 5 seconds 2021-05-06 23:45:33 10.10.10.2:1194 SENT CONTROL [client]: 'AUTH_FAILED'
(status=1)
As you can see the script does the deferred auth immediately, but the server send the AUTH_FAILED only after the PUSH_REQUEST being received *after some (10?) seconds since the failure of the script*.
To me this sounds like the server is caching the current status for some (10?) seconds and it is not updating it with the actual result.
However, this does not seem to happen without your patch as the server is probably forcing a reload of the result from file everytime a PUSH_REQUEST is received.
I believe this behaviour should stay the same.
Regards,
Quoted 247 lines · Arne Schwabe
tls_authentication_statusdoes caching to avoid file I/O more than everyTLS_MULTI_AUTH_STATUS_INTERVAL(10s) per connection. But counter-intuitively it does not return the cached result but ratherTLS_AUTHENTICATION_UNDEFINEDif the cache is not refreshed by the call.This is workarounded by forcing a refresh in some areas of the code (latency = 0).
This patch changes the behaviour by always returning the last known status and only updating the file status when the i/o timeout for the caches is reached.
The old logic in
send_auth_failedis fragile in the sense that if it is called again while an exit is scheduled it will reset the timer to 5s again. Since we now always report the status fromtls_authentication_status()instead only every 10s, this caused OpenVPN to infinitively reset the timer. Fix this by only setting the status if no exit is scheduled. The function is still called multiple times but since it is with coarse timer frequency, the 4 extra calls (1 per second) are better than to add more extra code to avoid these calls.The patch also changes the DEFINE enum into a real enum.
Patch v2: only update
tas_cache_last_udpatewhen actually updating the cache. Patch v3: avoid rearming timerSigned-off-by Arne Schwabe <[email protected]...>--- src/openvpn/multi.c | 2 +- src/openvpn/push.c | 11 ++++++++- src/openvpn/ssl_common.h | 16 +++++++++--- src/openvpn/ssl_verify.c | 53 ++++++++++++++++++++++------------------ src/openvpn/ssl_verify.h | 3 +-- 5 files changed, 54 insertions(+), 31 deletions(-)src/openvpn/multi.c +1
-1@@ -2596,7 +2596,7 @@ static const multi_client_connect_handler client_connect_handlers[] = {static void multi_connection_established (struct multi_context *m,struct multi_instance *mi) { -if (tls_authentication_status (mi->context.c2.tls_multi,0 ) +if (tls_authentication_status (mi->context.c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) !=TLS_AUTHENTICATION_SUCCEEDED ) {return ;src/openvpn/push.c +10
-1@@ -335,10 +335,18 @@ __attribute__ ((format(__printf__, 4, 5)))/* * Send auth failed message from server to client. +* +* Does nothing if an exit is already scheduled */ void send_auth_failed (struct context *c,const char *client_reason) { +if (event_timeout_defined (&c->c2.scheduled_exit)) + { +msg (D_TLS_DEBUG ,"exit already scheduled for context" ); +return ; + } +struct gc_arena gc =gc_new ();static const char auth_failed[] ="AUTH_FAILED" ;size_t len;@@ -855,7 +863,8 @@ process_incoming_push_request(struct context *c) {int ret =PUSH_MSG_ERROR ; -if (tls_authentication_status (c->c2.tls_multi,0 ) ==TLS_AUTHENTICATION_FAILED + +if (tls_authentication_status (c->c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) ==TLS_AUTHENTICATION_FAILED || c->c2.tls_multi->multi_state ==CAS_FAILED ) {const char *client_reason =tls_client_reason (c->c2.tls_multi);src/openvpn/ssl_common.h +13
-3@@ -152,6 +152,15 @@ struct auth_deferred_statusunsigned int auth_control_status; }; +/* key_state_test_auth_control_file return values, these specify the +* current status of a deferred authentication */ +enum auth_deferred_result { +ACF_PENDING ,/**< deferred auth still pending */ +ACF_SUCCEEDED ,/**< deferred auth has suceeded */ +ACF_DISABLED ,/**< deferred auth is not used */ +ACF_FAILED /**< deferred auth has failed */ +}; +/** * Security parameter state of one TLS and data channel %key session. * @ingroup control_processor @@ -219,7 +228,7 @@ struct key_state#ifdef ENABLE_MANAGEMENT unsigned int mda_key_id; -unsigned int mda_status; +enum auth_deferred_result mda_status;#endif time_t acf_last_mod;@@ -561,8 +570,9 @@ struct tls_multichar *locked_username;struct cert_hash_set *locked_cert_hash_set; -/* Time of last call to tls_authentication_status */ -time_t tas_last; +/* Time of last when we updated the cached state of +* tls_authentication_status deferred files */ +time_t tas_cache_last_update;/* * An error message to send to client on AUTH_FAILED src/openvpn/ssl_verify.c +29
-24@@ -845,13 +845,6 @@ cleanup:* user/password authentication. *************************************************************************** */ -/* key_state_test_auth_control_file return values, -* NOTE: acf_merge indexing depends on these values */ -#define ACF_UNDEFINED 0 -#define ACF_SUCCEEDED 1 -#define ACF_DISABLED 2 -#define ACF_FAILED 3 -void auth_set_client_reason (struct tls_multi *multi,const char *client_reason) {@@ -866,7 +859,7 @@ auth_set_client_reason(struct tls_multi *multi, const char *client_reason)#ifdef ENABLE_MANAGEMENT -static inline unsigned int +static inline enum auth_deferred_result man_def_auth_test (const struct key_state *ks) {if (management_enable_def_auth (management))@@ -1041,13 +1034,23 @@ key_state_gen_auth_control_files(struct auth_deferred_status *ads,return (acf && apf); } -static unsigned int -key_state_test_auth_control_file (struct auth_deferred_status *ads) +/** +* Checks the control status from a file. The function will try to read +* and update the cached status if the status is still pending and the paramter +* cached is false. The function returns the +* +* +* @param ads deferred status control structure +* @param cached Return only cached status +* @return +*/ +static enum auth_deferred_result +key_state_test_auth_control_file (struct auth_deferred_status *ads,bool cached) {if (ads->auth_control_file) {unsigned int ret = ads->auth_control_status; -if (ret ==ACF_UNDEFINED ) +if (ret ==ACF_PENDING && !cached) {FILE *fp =fopen (ads->auth_control_file,"r" );if (fp)@@ -1084,11 +1087,7 @@ tls_authentication_status(struct tls_multi *multi, const int latency)/* at least one key already failed authentication */ bool failed_auth = false; -if (latency && multi->tas_last + latency >= now) - { -return TLS_AUTHENTICATION_UNDEFINED ; - } - multi->tas_last = now; +bool cached = multi->tas_cache_last_update + latency >= now;for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) {@@ -1102,11 +1101,11 @@ tls_authentication_status(struct tls_multi *multi, const int latency) }else { -unsigned int auth_plugin =ACF_DISABLED ; -unsigned int auth_script =ACF_DISABLED ; -unsigned int auth_man =ACF_DISABLED ; - auth_plugin =key_state_test_auth_control_file (&ks->plugin_auth); - auth_script =key_state_test_auth_control_file (&ks->script_auth); +enum auth_deferred_result auth_plugin =ACF_DISABLED ; +enum auth_deferred_result auth_script =ACF_DISABLED ; +enum auth_deferred_result auth_man =ACF_DISABLED ; + auth_plugin =key_state_test_auth_control_file (&ks->plugin_auth, cached); + auth_script =key_state_test_auth_control_file (&ks->script_auth, cached);#ifdef ENABLE_MANAGEMENT auth_man =man_def_auth_test (ks);#endif @@ -1118,9 +1117,9 @@ tls_authentication_status(struct tls_multi *multi, const int latency) ks->authenticated =KS_AUTH_FALSE ; failed_auth = true; } -else if (auth_plugin ==ACF_UNDEFINED - || auth_script ==ACF_UNDEFINED - || auth_man ==ACF_UNDEFINED ) +else if (auth_plugin ==ACF_PENDING + || auth_script ==ACF_PENDING + || auth_man ==ACF_PENDING ) {if (now < ks->auth_deferred_expire) {@@ -1140,6 +1139,12 @@ tls_authentication_status(struct tls_multi *multi, const int latency) } } +/* we did not rely on a cached result, remember the cache update time */ +if (!cached) + { + multi->tas_cache_last_update = now; + } +#if 0 dmsg (D_TLS_ERRORS ,"TAS: a=%d s=%d d=%d f=%d" , active, success, deferred, failed_auth);#endif src/openvpn/ssl_verify.h +1
-2@@ -69,8 +69,7 @@ enum tls_auth_status {TLS_AUTHENTICATION_SUCCEEDED =0 ,TLS_AUTHENTICATION_FAILED =1 , -TLS_AUTHENTICATION_DEFERRED =2 , - TLS_AUTHENTICATION_UNDEFINED=3 + TLS_AUTHENTICATION_DEFERRED=2 };/**
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH v3] Return cached result in tls_authentication_status
Hi Arne,
following your lead I recompiled with --enable-async-push and the problem went away.
Some ifdef is messing up the code path?
Cheers,
Quoted 322 lines · Antonio Quartulli
Hi Arne,
after our discussion on IRC I understood you expected this patch to not change the server behaviour.
If something is suboptimal, it means it was suboptimal also before this patch.
However, with your patch I can clearly see a longer delay in returning the AUTH result to the client, in case of deferred auth.
Without your patch (initial lines are the output of my auth-verify script):
PASS=testno FILE=/tmp/openvpn_acf_662a1165ab3293aa3fb053050e1c2463.tmp FAIL
2021-05-06 23:40:28 10.10.10.2:1194 TLS: Username/Password authentication deferred for username 'test' 2021-05-06 23:40:28 10.10.10.2:1194 Control Channel: TLSv1.3, cipher TLSv1.3 TLS_AES_256_GCM_SHA384, peer certificate: 384 bit EC, curve secp384r1, signature: ecdsa-with-SHA256 2021-05-06 23:40:28 10.10.10.2:1194 [client] Peer Connection Initiated with [AF_INET6]::ffff:10.10.10.2:1194 2021-05-06 23:40:29 10.10.10.2:1194 PUSH: Received control message: 'PUSH_REQUEST' 2021-05-06 23:40:29 10.10.10.2:1194 Delayed exit in 5 seconds 2021-05-06 23:40:29 10.10.10.2:1194 SENT CONTROL [client]: 'AUTH_FAILED'(status=1)
My script uses the deferred logic, but it is super fast (as you can see it fails before the server can even do anything else).
Later, as soon as the first
PUSH_REQUESTis received, the server replies immediately with anAUTH_FAILED.With your patch (script and everything else stays the same):
PASS=testno FILE=/tmp/openvpn_acf_4cdab09a9640e8eb4cacadfc1734bb2c.tmp FAIL
2021-05-06 23:45:22 10.10.10.2:1194 TLS: Username/Password authentication deferred for username 'test' 2021-05-06 23:45:22 10.10.10.2:1194 Control Channel: TLSv1.3, cipher TLSv1.3 TLS_AES_256_GCM_SHA384, peer certificate: 384 bit EC, curve secp384r1, signature: ecdsa-with-SHA256 2021-05-06 23:45:22 10.10.10.2:1194 [client] Peer Connection Initiated with [AF_INET6]::ffff:10.10.10.2:1194 2021-05-06 23:45:23 10.10.10.2:1194 PUSH: Received control message: 'PUSH_REQUEST' 2021-05-06 23:45:28 10.10.10.2:1194 PUSH: Received control message: 'PUSH_REQUEST' 2021-05-06 23:45:33 10.10.10.2:1194 Delayed exit in 5 seconds 2021-05-06 23:45:33 10.10.10.2:1194 SENT CONTROL [client]: 'AUTH_FAILED'(status=1)
As you can see the script does the deferred auth immediately, but the server send the
AUTH_FAILEDonly after thePUSH_REQUESTbeing received *after some (10?) seconds since the failure of the script*.To me this sounds like the server is caching the current status for some (10?) seconds and it is not updating it with the actual result.
However, this does not seem to happen without your patch as the server is probably forcing a reload of the result from file everytime a
PUSH_REQUESTis received.I believe this behaviour should stay the same.
Regards,
Quoted 247 lines · Arne Schwabe
tls_authentication_statusdoes caching to avoid file I/O more than everyTLS_MULTI_AUTH_STATUS_INTERVAL(10s) per connection. But counter-intuitively it does not return the cached result but ratherTLS_AUTHENTICATION_UNDEFINEDif the cache is not refreshed by the call.This is workarounded by forcing a refresh in some areas of the code (latency = 0).
This patch changes the behaviour by always returning the last known status and only updating the file status when the i/o timeout for the caches is reached.
The old logic in
send_auth_failedis fragile in the sense that if it is called again while an exit is scheduled it will reset the timer to 5s again. Since we now always report the status fromtls_authentication_status()instead only every 10s, this caused OpenVPN to infinitively reset the timer. Fix this by only setting the status if no exit is scheduled. The function is still called multiple times but since it is with coarse timer frequency, the 4 extra calls (1 per second) are better than to add more extra code to avoid these calls.The patch also changes the DEFINE enum into a real enum.
Patch v2: only update
tas_cache_last_udpatewhen actually updating the cache. Patch v3: avoid rearming timerSigned-off-by Arne Schwabe <[email protected]...>--- src/openvpn/multi.c | 2 +- src/openvpn/push.c | 11 ++++++++- src/openvpn/ssl_common.h | 16 +++++++++--- src/openvpn/ssl_verify.c | 53 ++++++++++++++++++++++------------------ src/openvpn/ssl_verify.h | 3 +-- 5 files changed, 54 insertions(+), 31 deletions(-)src/openvpn/multi.c +1
-1@@ -2596,7 +2596,7 @@ static const multi_client_connect_handler client_connect_handlers[] = {static void multi_connection_established (struct multi_context *m,struct multi_instance *mi) { -if (tls_authentication_status (mi->context.c2.tls_multi,0 ) +if (tls_authentication_status (mi->context.c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) !=TLS_AUTHENTICATION_SUCCEEDED ) {return ;src/openvpn/push.c +10
-1@@ -335,10 +335,18 @@ __attribute__ ((format(__printf__, 4, 5)))/* * Send auth failed message from server to client. +* +* Does nothing if an exit is already scheduled */ void send_auth_failed (struct context *c,const char *client_reason) { +if (event_timeout_defined (&c->c2.scheduled_exit)) + { +msg (D_TLS_DEBUG ,"exit already scheduled for context" ); +return ; + } +struct gc_arena gc =gc_new ();static const char auth_failed[] ="AUTH_FAILED" ;size_t len;@@ -855,7 +863,8 @@ process_incoming_push_request(struct context *c) {int ret =PUSH_MSG_ERROR ; -if (tls_authentication_status (c->c2.tls_multi,0 ) ==TLS_AUTHENTICATION_FAILED + +if (tls_authentication_status (c->c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) ==TLS_AUTHENTICATION_FAILED || c->c2.tls_multi->multi_state ==CAS_FAILED ) {const char *client_reason =tls_client_reason (c->c2.tls_multi);src/openvpn/ssl_common.h +13
-3@@ -152,6 +152,15 @@ struct auth_deferred_statusunsigned int auth_control_status; }; +/* key_state_test_auth_control_file return values, these specify the +* current status of a deferred authentication */ +enum auth_deferred_result { +ACF_PENDING ,/**< deferred auth still pending */ +ACF_SUCCEEDED ,/**< deferred auth has suceeded */ +ACF_DISABLED ,/**< deferred auth is not used */ +ACF_FAILED /**< deferred auth has failed */ +}; +/** * Security parameter state of one TLS and data channel %key session. * @ingroup control_processor @@ -219,7 +228,7 @@ struct key_state#ifdef ENABLE_MANAGEMENT unsigned int mda_key_id; -unsigned int mda_status; +enum auth_deferred_result mda_status;#endif time_t acf_last_mod;@@ -561,8 +570,9 @@ struct tls_multichar *locked_username;struct cert_hash_set *locked_cert_hash_set; -/* Time of last call to tls_authentication_status */ -time_t tas_last; +/* Time of last when we updated the cached state of +* tls_authentication_status deferred files */ +time_t tas_cache_last_update;/* * An error message to send to client on AUTH_FAILED src/openvpn/ssl_verify.c +29
-24@@ -845,13 +845,6 @@ cleanup:* user/password authentication. *************************************************************************** */ -/* key_state_test_auth_control_file return values, -* NOTE: acf_merge indexing depends on these values */ -#define ACF_UNDEFINED 0 -#define ACF_SUCCEEDED 1 -#define ACF_DISABLED 2 -#define ACF_FAILED 3 -void auth_set_client_reason (struct tls_multi *multi,const char *client_reason) {@@ -866,7 +859,7 @@ auth_set_client_reason(struct tls_multi *multi, const char *client_reason)#ifdef ENABLE_MANAGEMENT -static inline unsigned int +static inline enum auth_deferred_result man_def_auth_test (const struct key_state *ks) {if (management_enable_def_auth (management))@@ -1041,13 +1034,23 @@ key_state_gen_auth_control_files(struct auth_deferred_status *ads,return (acf && apf); } -static unsigned int -key_state_test_auth_control_file (struct auth_deferred_status *ads) +/** +* Checks the control status from a file. The function will try to read +* and update the cached status if the status is still pending and the paramter +* cached is false. The function returns the +* +* +* @param ads deferred status control structure +* @param cached Return only cached status +* @return +*/ +static enum auth_deferred_result +key_state_test_auth_control_file (struct auth_deferred_status *ads,bool cached) {if (ads->auth_control_file) {unsigned int ret = ads->auth_control_status; -if (ret ==ACF_UNDEFINED ) +if (ret ==ACF_PENDING && !cached) {FILE *fp =fopen (ads->auth_control_file,"r" );if (fp)@@ -1084,11 +1087,7 @@ tls_authentication_status(struct tls_multi *multi, const int latency)/* at least one key already failed authentication */ bool failed_auth = false; -if (latency && multi->tas_last + latency >= now) - { -return TLS_AUTHENTICATION_UNDEFINED ; - } - multi->tas_last = now; +bool cached = multi->tas_cache_last_update + latency >= now;for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) {@@ -1102,11 +1101,11 @@ tls_authentication_status(struct tls_multi *multi, const int latency) }else { -unsigned int auth_plugin =ACF_DISABLED ; -unsigned int auth_script =ACF_DISABLED ; -unsigned int auth_man =ACF_DISABLED ; - auth_plugin =key_state_test_auth_control_file (&ks->plugin_auth); - auth_script =key_state_test_auth_control_file (&ks->script_auth); +enum auth_deferred_result auth_plugin =ACF_DISABLED ; +enum auth_deferred_result auth_script =ACF_DISABLED ; +enum auth_deferred_result auth_man =ACF_DISABLED ; + auth_plugin =key_state_test_auth_control_file (&ks->plugin_auth, cached); + auth_script =key_state_test_auth_control_file (&ks->script_auth, cached);#ifdef ENABLE_MANAGEMENT auth_man =man_def_auth_test (ks);#endif @@ -1118,9 +1117,9 @@ tls_authentication_status(struct tls_multi *multi, const int latency) ks->authenticated =KS_AUTH_FALSE ; failed_auth = true; } -else if (auth_plugin ==ACF_UNDEFINED - || auth_script ==ACF_UNDEFINED - || auth_man ==ACF_UNDEFINED ) +else if (auth_plugin ==ACF_PENDING + || auth_script ==ACF_PENDING + || auth_man ==ACF_PENDING ) {if (now < ks->auth_deferred_expire) {@@ -1140,6 +1139,12 @@ tls_authentication_status(struct tls_multi *multi, const int latency) } } +/* we did not rely on a cached result, remember the cache update time */ +if (!cached) + { + multi->tas_cache_last_update = now; + } +#if 0 dmsg (D_TLS_ERRORS ,"TAS: a=%d s=%d d=%d f=%d" , active, success, deferred, failed_auth);#endif src/openvpn/ssl_verify.h +1
-2@@ -69,8 +69,7 @@ enum tls_auth_status {TLS_AUTHENTICATION_SUCCEEDED =0 ,TLS_AUTHENTICATION_FAILED =1 , -TLS_AUTHENTICATION_DEFERRED =2 , - TLS_AUTHENTICATION_UNDEFINED=3 + TLS_AUTHENTICATION_DEFERRED=2 };/**
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH v3] Return cached result in tls_authentication_status
In reply to Antonio QuartulliAm 06.05.21 um 23:49 schrieb Antonio Quartulli:
Quoted 60 lines
Hi Arne,
after our discussion on IRC I understood you expected this patch to not change the server behaviour.
If something is suboptimal, it means it was suboptimal also before this patch.
However, with your patch I can clearly see a longer delay in returning the AUTH result to the client, in case of deferred auth.
Without your patch (initial lines are the output of my auth-verify script):
PASS=testno FILE=/tmp/openvpn_acf_662a1165ab3293aa3fb053050e1c2463.tmp FAIL
2021-05-06 23:40:28 10.10.10.2:1194 TLS: Username/Password authentication deferred for username 'test' 2021-05-06 23:40:28 10.10.10.2:1194 Control Channel: TLSv1.3, cipher TLSv1.3 TLS_AES_256_GCM_SHA384, peer certificate: 384 bit EC, curve secp384r1, signature: ecdsa-with-SHA256 2021-05-06 23:40:28 10.10.10.2:1194 [client] Peer Connection Initiated with [AF_INET6]::ffff:10.10.10.2:1194 2021-05-06 23:40:29 10.10.10.2:1194 PUSH: Received control message: 'PUSH_REQUEST' 2021-05-06 23:40:29 10.10.10.2:1194 Delayed exit in 5 seconds 2021-05-06 23:40:29 10.10.10.2:1194 SENT CONTROL [client]: 'AUTH_FAILED'(status=1)
My script uses the deferred logic, but it is super fast (as you can see it fails before the server can even do anything else).
Later, as soon as the first
PUSH_REQUESTis received, the server replies immediately with anAUTH_FAILED.With your patch (script and everything else stays the same):
PASS=testno FILE=/tmp/openvpn_acf_4cdab09a9640e8eb4cacadfc1734bb2c.tmp FAIL
2021-05-06 23:45:22 10.10.10.2:1194 TLS: Username/Password authentication deferred for username 'test' 2021-05-06 23:45:22 10.10.10.2:1194 Control Channel: TLSv1.3, cipher TLSv1.3 TLS_AES_256_GCM_SHA384, peer certificate: 384 bit EC, curve secp384r1, signature: ecdsa-with-SHA256 2021-05-06 23:45:22 10.10.10.2:1194 [client] Peer Connection Initiated with [AF_INET6]::ffff:10.10.10.2:1194 2021-05-06 23:45:23 10.10.10.2:1194 PUSH: Received control message: 'PUSH_REQUEST' 2021-05-06 23:45:28 10.10.10.2:1194 PUSH: Received control message: 'PUSH_REQUEST' 2021-05-06 23:45:33 10.10.10.2:1194 Delayed exit in 5 seconds 2021-05-06 23:45:33 10.10.10.2:1194 SENT CONTROL [client]: 'AUTH_FAILED'(status=1)
As you can see the script does the deferred auth immediately, but the server send the
AUTH_FAILEDonly after thePUSH_REQUESTbeing received *after some (10?) seconds since the failure of the script*.
Quoted 7 lines
To me this sounds like the server is caching the current status for some (10?) seconds and it is not updating it with the actual result.
However, this does not seem to happen without your patch as the server is probably forcing a reload of the result from file everytime a
PUSH_REQUESTis received.
Yes it is that each PUSH_REQUEST forces a check on the deferred auth files. The question is if we want to lower the interval to check to avoid this long time. But then again on the other hand (at least this specific instance) only affects AUTH_FAILED being delayed.
The other instance of non-caching lookup in the older code is in multi_connection_established and this is a bit more tricky since it is actually in the positive code path and is called through various code paths.
In my own tests I had --enable-async-push which masks this by having inotify pick this up.
I feel we should maybe lower the interval to something lower? Or implement something like an exponential backoff like 0,0,1,2,5,10s?
Arne
Re: [Openvpn-devel] [PATCH v3] Return cached result in tls_authentication_status
In reply to Arne SchwabeHi,
Quoted 21 lines · Arne Schwabe
tls_authentication_statusdoes caching to avoid file I/O more than everyTLS_MULTI_AUTH_STATUS_INTERVAL(10s) per connection. But counter-intuitively it does not return the cached result but ratherTLS_AUTHENTICATION_UNDEFINEDif the cache is not refreshed by the call.This is workarounded by forcing a refresh in some areas of the code (latency = 0).
This patch changes the behaviour by always returning the last known status and only updating the file status when the i/o timeout for the caches is reached.
The old logic in
send_auth_failedis fragile in the sense that if it is called again while an exit is scheduled it will reset the timer to 5s again. Since we now always report the status fromtls_authentication_status()instead only every 10s, this caused OpenVPN to infinitively reset the timer. Fix this by only setting the status if no exit is scheduled. The function is still called multiple times but since it is with coarse timer frequency, the 4 extra calls (1 per second) are better than to add more extra code to avoid these calls.
Quoted 6 lines
The patch also changes the DEFINE enum into a real enum.
Patch v2: only update
tas_cache_last_udpatewhen actually updating the cache. Patch v3: avoid rearming timerSigned-off-by Arne Schwabe <[email protected]...>
It turned out that this area of the code was quite twisted and by improving its logic we uncovered some corner cases.
This patch does what it says (improves caching logic and reduces the voodoo magic), but also introduced some delay (well, due to caching) that we did not have before.
"[PATCH] Use exponential backoff for caching in tls_authentication_status" takes care of removing this delay by implementing a dynamic caching latency (exp backoff).
Compiled in my lib zoo and tested in my basic environment.
Signature · 1 line
Antonio Quartulli
[Openvpn-devel] [PATCH applied] Re: Return cached result in tls_authentication_status
In reply to Arne SchwabeI have tested this on the server test rig, and it did not change server side behaviour (in a way that breaks the defined tests).
I've also stared at the code a bit and tried to figure out what is going on, and came across a slightly incomplete comment for key_state_test_auth_control_file() which has escaped the dragon's wrath - rewritten to match my understanding of the function.
Ditto for the comment in ssl_verify.h for tls_authentication_status(), which was not updated to reflect the removal of TLS_AUTHENTICATION_UNDEFINED (slightly moot as the "exponential" patch will remove that parameter and the comment with it).
Your patch has been applied to the master branch.
commit 9a4305020777ce92d4143828b39027f43951ac32
Author: Arne Schwabe Date: Thu May 6 16:12:59 2021 +0200
Return cached result in tls_authentication_status
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]/msg22318.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
PATCH 1/1 Use exponential backoff for caching in tls_authentication_status
In reply to Arne SchwabeThe caching in tls_authentication_status broke the quick reaction to authentication status in the code paths that did not do caching like PUSH_REQUEST reply code path.
This patch introduces exponential backoff for the caching so we still retain the quick reaction while still keeping the benefit of caching.
--- src/openvpn/multi.c | 3 +-- src/openvpn/push.c | 2 +- src/openvpn/ssl.c | 2 +- src/openvpn/ssl.h | 3 --- src/openvpn/ssl_common.h | 5 ++++- src/openvpn/ssl_verify.c | 25 +++++++++++++++++++++++-- src/openvpn/ssl_verify.h | 8 +++----- 7 files changed, 33 insertions(+), 15 deletions(-)
src/openvpn/multi.c +1 -2
@@ -2596,8 +2596,7 @@ static const multi_client_connect_handler client_connect_handlers[] = {static void multi_connection_established (struct multi_context *m,struct multi_instance *mi) { -if (tls_authentication_status (mi->context.c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) - !=TLS_AUTHENTICATION_SUCCEEDED ) +if (tls_authentication_status (mi->context.c2.tls_multi) !=TLS_AUTHENTICATION_SUCCEEDED ) {return ; }
src/openvpn/push.c +1 -1
@@ -864,7 +864,7 @@ process_incoming_push_request(struct context *c)int ret =PUSH_MSG_ERROR ; -if (tls_authentication_status (c->c2.tls_multi,TLS_MULTI_AUTH_STATUS_INTERVAL ) ==TLS_AUTHENTICATION_FAILED +if (tls_authentication_status (c->c2.tls_multi) ==TLS_AUTHENTICATION_FAILED || c->c2.tls_multi->multi_state ==CAS_FAILED ) {const char *client_reason =tls_client_reason (c->c2.tls_multi);
src/openvpn/ssl.c +1 -1
@@ -3133,7 +3133,7 @@ tls_multi_process(struct tls_multi *multi,update_time (); -enum tls_auth_status tas =tls_authentication_status (multi,TLS_MULTI_AUTH_STATUS_INTERVAL ); +enum tls_auth_status tas =tls_authentication_status (multi);/* * If lame duck session expires, kill it.
src/openvpn/ssl.h +0 -3
@@ -91,9 +91,6 @@#define TLS_MULTI_HORIZON 2/* call tls_multi_process frequently for n seconds after * every packet sent/received action */ -/* Interval that tls_multi_process should call tls_authentication_status */ -#define TLS_MULTI_AUTH_STATUS_INTERVAL 10 -/* * Buffer sizes (also see mtu.h). */
src/openvpn/ssl_common.h +4 -1
@@ -570,10 +570,13 @@ struct tls_multichar *locked_username;struct cert_hash_set *locked_cert_hash_set; -/* Time of last when we updated the cached state of +/** Time of last when we updated the cached state of * tls_authentication_status deferred files */ time_t tas_cache_last_update; +/** The number of times we updated the cache */ +unsigned int tas_cache_num_updates; +/* * An error message to send to client on AUTH_FAILED */
src/openvpn/ssl_verify.c +23 -2
@@ -1073,8 +1073,28 @@ key_state_test_auth_control_file(struct auth_deferred_status *ads, bool cached)return ACF_DISABLED ; } +/** +* The minimum times to have passed to update the cache. Older versions +* of OpenVPN had code path that did not do any caching, so we start +* with no caching (0) here as well to have the same super quick initial +* reaction. +*/ +static time_t cache_intervals[] = {0 ,0 ,0 ,0 ,0 ,1 ,1 ,2 ,2 ,4 ,8 }; + +/** +* uses cache_intervals times to determine if we should update the +* cache. +*/ +static bool +tls_authentication_status_use_cache (struct tls_multi *multi) +{ +unsigned int idx =min_uint (multi->tas_cache_num_updates,SIZE (cache_intervals) -1 ); +time_t latency = cache_intervals[idx]; +return multi->tas_cache_last_update + latency >= now; +} +enum tls_auth_status -tls_authentication_status (struct tls_multi *multi,const int latency) +tls_authentication_status (struct tls_multi *multi) {bool deferred = false;
@@ -1087,7 +1107,7 @@ tls_authentication_status(struct tls_multi *multi, const int latency)/* at least one key already failed authentication */ bool failed_auth = false; -bool cached = multi->tas_cache_last_update + latency >= now; +bool cached =tls_authentication_status_use_cache (multi);for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) {
@@ -1143,6 +1163,7 @@ tls_authentication_status(struct tls_multi *multi, const int latency)if (!cached) { multi->tas_cache_last_update = now; + multi->tas_cache_num_updates++; }#if 0
src/openvpn/ssl_verify.h +3 -5
@@ -75,7 +75,7 @@ enum tls_auth_status/** * Return current session authentication state of the tls_multi structure * This will return TLS_AUTHENTICATION_SUCCEEDED only if the session is -* fully authenicated, i.e. VPN traffic is allowed over it. +* fully authenticated, i.e. VPN traffic is allowed over it. * * Checks the status of all active keys and checks if the deferred * authentication has succeeded.
@@ -84,15 +84,13 @@ enum tls_auth_status* from KS_AUTH_DEFERRED to KS_AUTH_FALSE/KS_AUTH_TRUE if the deferred * authentication has succeeded after last call. * -* @param latency if not null, return TLS_AUTHENTICATION_UNDEFINED if -* the last call for this multi struct has been less -* than latency seconds ago +* * @param multi the tls_multi struct to operate on * * @return Current authentication status of the tls_multi */ enum tls_auth_status -tls_authentication_status (struct tls_multi *multi,const int latency); +tls_authentication_status (struct tls_multi *multi);/** Check whether the \a ks \c key_state has finished the key exchange part * of the OpenVPN hand shake. This is that the key_method_2read/write
Signature · 1 line
2.31.1
Re: [Openvpn-devel] [PATCH] Use exponential backoff for caching in tls_authentication_status
Hi,
Quoted 8 lines · Arne Schwabe
The caching in
tls_authentication_statusbroke the quick reaction to authentication status in the code paths that did not do caching likePUSH_REQUESTreply code path.This patch introduces exponential backoff for the caching so we still retain the quick reaction while still keeping the benefit of caching.
Signed-off-by Arne Schwabe <[email protected]...>
Very simple and nice patch that removes the static caching interval and makes it dynamic, following an exp backoff.
Logic is very simple and various latency values are hardcoded in an array - so no need for any math.
Tests reveal that the delay introduced with the previous patch (Return cached result in tls_authentication_status) is indeed killed.
Should the deferred auth script/plugin take a bit longer, we will see some delay being introduced, but still very reasonable.
Bonus: if fixes a comment and a typ0.
Regards,
Signature · 1 line
Antonio Quartulli
[Openvpn-devel] [PATCH applied] Re: Use exponential backoff for caching in tls_authentication_status
In reply to Arne SchwabeAs discussed on IRC, this patch fixes most of the shortcomings in the previous patch (9a430502077).
I still find these code paths very complicated, but after running the server test side with added logging to see when the cache kicks in and what sort of delay is induced, I think I can say "this makes sense" - and it does not break any of the existing test scenarios, which is good :-)
While still complicated, it actually simplifies the overall flow, as there is no extra "latency" parameter anymore, which does magic things, depending on where it's called from.
Also, I was slightly confused on the actual effect of the cache/delay - this is really only relevant for the initial authentication (and not for TLS renegotiation) and only while the auth control file status is not yet known. So for all other code paths, and for "inotify" paths (triggered check of the ACFs) this just makes the code more simple -> more good.
Your patch has been applied to the master branch.
commit d49df6bdde0592c9f795a2a260f6f04255b32303
Author: Arne Schwabe Date: Mon May 10 15:13:56 2021 +0200
Use exponential backoff for caching in tls_authentication_status
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]/msg22327.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering