Skip to contentSkip to conversation

Move context_auth from context_2 to tls_multi

PATCH Superseded Arne Schwabe 3 participants 18 messages Dec 15, 2020 – Apr 20, 2021

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Move context_auth from context_2 to tls_multi

Some code currently is doing weird workarounds to figure out that side as side effect from other variables in tls_multi.

---
 src/openvpn/forward.c    |  4 ++--
 src/openvpn/multi.c      | 24 ++++++++++++------------
 src/openvpn/openvpn.h    | 14 --------------
 src/openvpn/push.c       |  6 +++---
 src/openvpn/ssl_common.h | 14 ++++++++++++++
 5 files changed, 31 insertions(+), 31 deletions(-)
src/openvpn/forward.c +2 -2
index 67615a6b..3807536e 100644
@@ -535,7 +535,7 @@ encrypt_sign(struct context *c, bool comp_frag)
      * Drop non-TLS outgoing packet if client-connect script/plugin
      * has not yet succeeded.
      */
-    if (c->c2.context_auth != CAS_SUCCEEDED)
+    if (c->c2.tls_multi->context_auth != CAS_SUCCEEDED)
     {
         c->c2.buf.len = 0;
     }
@@ -980,7 +980,7 @@ process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, boo
          * Drop non-TLS packet if client-connect script/plugin and cipher selection
          * has not yet succeeded.
          */
-        if (c->c2.context_auth != CAS_SUCCEEDED)
+        if (c->c2.tls_multi && c->c2.tls_multi->context_auth != CAS_SUCCEEDED)
         {
             c->c2.buf.len = 0;
         }
src/openvpn/multi.c +12 -12
index dd713049..821c750a 100644
@@ -675,7 +675,7 @@ multi_close_instance(struct multi_context *m,
 #ifdef ENABLE_MANAGEMENT
     set_cc_config(mi, NULL);
 #endif
-    if (mi->context.c2.context_auth == CAS_SUCCEEDED)
+    if (mi->context.c2.tls_multi->context_auth == CAS_SUCCEEDED)
     {
         multi_client_disconnect_script(mi);
     }
@@ -785,7 +785,7 @@ multi_create_instance(struct multi_context *m, const struct mroute_addr *real)
         goto err;
     }
 
-    mi->context.c2.context_auth = CAS_PENDING;
+    mi->context.c2.tls_multi->context_auth = CAS_PENDING;
 
     if (hash_n_elements(m->hash) >= m->max_clients)
     {
@@ -2437,18 +2437,18 @@ 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.context_auth = CAS_SUCCEEDED;
+    mi->context.c2.tls_multi->context_auth = CAS_SUCCEEDED;
 
     /* authentication complete, calculate dynamic client specific options */
     if (!multi_client_set_protocol_options(&mi->context))
     {
-        mi->context.c2.context_auth = CAS_FAILED;
+        mi->context.c2.tls_multi->context_auth = CAS_FAILED;
     }
     /* Generate data channel keys only if setting protocol options
      * has not failed */
     else if (!multi_client_generate_tls_keys(&mi->context))
     {
-        mi->context.c2.context_auth = CAS_FAILED;
+        mi->context.c2.tls_multi->context_auth = CAS_FAILED;
     }
 
     /* send push reply if ready */
@@ -2596,7 +2596,7 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
 
     /* We are only called for the CAS_PENDING_x states, so we
      * can ignore other states here */
-    bool from_deferred = (mi->context.c2.context_auth != CAS_PENDING);
+    bool from_deferred = (mi->context.c2.tls_multi->context_auth != CAS_PENDING);
 
     int *cur_handler_index = &mi->client_connect_defer_state.cur_handler_index;
     unsigned int *option_types_found =
@@ -2608,7 +2608,7 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
         *cur_handler_index = 0;
         *option_types_found = 0;
         /* Initially we have no handler that has returned a result */
-        mi->context.c2.context_auth = CAS_PENDING_DEFERRED;
+        mi->context.c2.tls_multi->context_auth = CAS_PENDING_DEFERRED;
 
         multi_client_connect_early_setup(m, mi);
     }
@@ -2631,7 +2631,7 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
                  * Remember that we already had at least one handler
                  * returning a result should we go to into deferred state
                  */
-                mi->context.c2.context_auth = CAS_PENDING_DEFERRED_PARTIAL;
+                mi->context.c2.tls_multi->context_auth = CAS_PENDING_DEFERRED_PARTIAL;
                 break;
 
             case CC_RET_SKIPPED:
@@ -2683,12 +2683,12 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
     {
         /* run the disconnect script if we had a connect script that
          * did not fail */
-        if (mi->context.c2.context_auth == CAS_PENDING_DEFERRED_PARTIAL)
+        if (mi->context.c2.tls_multi->context_auth == CAS_PENDING_DEFERRED_PARTIAL)
         {
             multi_client_disconnect_script(mi);
         }
 
-        mi->context.c2.context_auth = CAS_FAILED;
+        mi->context.c2.tls_multi->context_auth = CAS_FAILED;
     }
 
     /* increment number of current authenticated clients */
@@ -2991,7 +2991,7 @@ 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.context_auth)
+            if (is_cas_pending(mi->context.c2.tls_multi->context_auth)
                 && CONNECTION_ESTABLISHED(&mi->context))
             {
                 multi_connection_established(m, mi);
@@ -3954,7 +3954,7 @@ management_client_auth(void *arg,
         {
             if (auth)
             {
-                if (is_cas_pending(mi->context.c2.context_auth))
+                if (is_cas_pending(mi->context.c2.tls_multi->context_auth))
                 {
                     set_cc_config(mi, cc_config);
                     cc_config_owned = false;
src/openvpn/openvpn.h +0 -14
index 4ca89ba9..0a246d4f 100644
@@ -211,17 +211,6 @@ struct context_1
 };
 
 
-/* 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_PENDING,
-    CAS_PENDING_DEFERRED,
-    CAS_PENDING_DEFERRED_PARTIAL,   /**< at least handler succeeded, no result yet*/
-    CAS_FAILED,
-};
-
 static inline bool
 is_cas_pending(enum client_connect_status cas)
 {
@@ -458,9 +447,6 @@ struct context_2
     int push_ifconfig_ipv6_netbits;
     struct in6_addr push_ifconfig_ipv6_remote;
 
-
-    enum client_connect_status context_auth;
-
     struct event_timeout push_request_interval;
     int n_sent_push_requests;
     bool did_pre_pull_restore;
src/openvpn/push.c +3 -3
index 26a6201f..25f96d6b 100644
@@ -736,14 +736,14 @@ process_incoming_push_request(struct context *c)
 {
     int ret = PUSH_MSG_ERROR;
 
-    if ((c->c2.tls_multi && tls_authentication_status(c->c2.tls_multi, 0) == TLS_AUTHENTICATION_FAILED)
-        || c->c2.context_auth == CAS_FAILED)
+    if (tls_authentication_status(c->c2.tls_multi, 0) == TLS_AUTHENTICATION_FAILED
+        || c->c2.tls_multi->context_auth == CAS_FAILED)
     {
         const char *client_reason = tls_client_reason(c->c2.tls_multi);
         send_auth_failed(c, client_reason);
         ret = PUSH_MSG_AUTH_FAILURE;
     }
-    else if (c->c2.context_auth == CAS_SUCCEEDED)
+    else if (c->c2.tls_multi->context_auth == CAS_SUCCEEDED)
     {
         time_t now;
 
src/openvpn/ssl_common.h +14 -0
index f435089a..41d98198 100644
@@ -480,6 +480,19 @@ struct tls_session
  */
 #define KEY_SCAN_SIZE 3
 
+
+/* 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_PENDING,
+    CAS_PENDING_DEFERRED,
+    CAS_PENDING_DEFERRED_PARTIAL,   /**< at least handler succeeded, no result yet*/
+    CAS_FAILED,
+};
+
+
 /**
  * Security parameter state for a single VPN tunnel.
  * @ingroup control_processor
@@ -515,6 +528,7 @@ struct tls_multi
 
     int n_sessions;             /**< Number of sessions negotiated thus
                                  *   far. */
+    enum client_connect_status context_auth;
 
     /*
      * Number of errors.
Signature · 1 line
2.26.2
#1 lore raw

PATCH 2/2 Fix condition to generate session keys

For --nobind clients OpenVPN reuses the context and tls_multi structs of the previous clients and does not rerun the connect scripts on connect. But since it is a new client connection, the key_id is 0 and we postpone the key generation but it will never happen.

This commit changes postponing the key generation to the right condition of NCP done for this session.

---
 src/openvpn/ssl.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
src/openvpn/ssl.c +12 -7
index efbf688f..5d322598 100644
@@ -2351,7 +2351,7 @@ error:
  * to the TLS control channel (cleartext).
  */
 static bool
-key_method_2_write(struct buffer *buf, struct tls_session *session)
+key_method_2_write(struct buffer *buf, struct tls_multi *multi, struct tls_session *session)
 {
     struct key_state *ks = &session->key[KS_PRIMARY];      /* primary key */
 
@@ -2442,12 +2442,17 @@ key_method_2_write(struct buffer *buf, struct tls_session *session)
         goto error;
     }
 
-    /* Generate tunnel keys if we're a TLS server.
-     * If we're a p2mp server and IV_NCP >= 2 is negotiated, the first key
-     * generation is postponed until after the pull/push, so we can process pushed
-     * cipher directives.
+    /*
+     * Generate tunnel keys if we're a TLS server.
+     *
+     * 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.
      */
-    if (session->opt->server && !(session->opt->mode == MODE_SERVER && ks->key_id <= 0))
+    if (session->opt->server && (session->opt->mode != MODE_SERVER
+            || multi->context_auth == CAS_SUCCEEDED))
     {
         if (ks->authenticated > KS_AUTH_FALSE)
         {
@@ -2936,7 +2941,7 @@ tls_process(struct tls_multi *multi,
         if (!buf->len && ((ks->state == S_START && !session->opt->server)
                           || (ks->state == S_GOT_KEY && session->opt->server)))
         {
-            if (!key_method_2_write(buf, session))
+            if (!key_method_2_write(buf, multi, session))
             {
                 goto error;
             }
Signature · 1 line
2.26.2
#6 lore raw
In reply to Arne Schwabe

Hi,

Quoted 3 lines · Arne Schwabe

Some code currently is doing weird workarounds to figure out that side as side effect from other variables in tls_multi.

Arne can you rephrase what this patch is trying to address? I think I understand the implementation, but I fail to see what this patch is improving.

Is this related to the tls_multi vs c2 lifecycle? or?

Cheers,

Signature · 1 line
Antonio Quartulli
#2 lore raw

Re: [Openvpn-devel] [PATCH 2/2] Fix condition to generate session keys

In reply to Arne Schwabe

Hi,

Quoted 4 lines · Arne Schwabe

For --nobind clients OpenVPN reuses the context and tls_multi structs of the previous clients and does not rerun the connect scripts on connect. But since it is a new client connection, the key_id is 0 and we postpone the key generation but it will never happen.

Can you explain how the --nobind on the client is related to the server behaviour?

Are you saying that a client connecting from the same IP of another client will share its session and tls_multi object?

Signature · 1 line
Antonio Quartulli
#3 lore raw

Re: [Openvpn-devel] [PATCH 2/2] Fix condition to generate session keys

Am 25.03.21 um 23:37 schrieb Antonio Quartulli:

Quoted 8 lines

Hi,

Quoted 4 lines · Arne Schwabe

For --nobind clients OpenVPN reuses the context and tls_multi structs of the previous clients and does not rerun the connect scripts on connect. But since it is a new client connection, the key_id is 0 and we postpone the key generation but it will never happen.

Quoted 5 lines

Can you explain how the --nobind on the client is related to the server behaviour?

Are you saying that a client connecting from the same IP of another client will share its session and tls_multi object?

(I will also copy that explanation to a v2 of the patch )

When OpenVPN sees a new (SSL) connection via HARD or SOFT_RESET with the same port/ip as an existing session, it will give it the slot of the renegotiation session (TM_UNTRUSTED). And when the authentication succeeds it will replace the current session. Since we already have gone through connect stages and set context_auth to CAS_SUCCEEDED, we don't call all the connect stages again, and therefore also never call multi_client_generate_tls_keys for this session.

Arne

#4 lore raw

Re: [Openvpn-devel] [PATCH 2/2] Fix condition to generate session keys

Hi,

Quoted 16 lines · Arne Schwabe

Am 25.03.21 um 23:37 schrieb Antonio Quartulli:

Quoted 8 lines

Hi,

Quoted 4 lines · Arne Schwabe

For --nobind clients OpenVPN reuses the context and tls_multi structs of the previous clients and does not rerun the connect scripts on connect. But since it is a new client connection, the key_id is 0 and we postpone the key generation but it will never happen.

Quoted 5 lines

Can you explain how the --nobind on the client is related to the server behaviour?

Are you saying that a client connecting from the same IP of another client will share its session and tls_multi object?

(I will also copy that explanation to a v2 of the patch )

Quoted 7 lines

When OpenVPN sees a new (SSL) connection via HARD or SOFT_RESET with the same port/ip as an existing session, it will give it the slot of the renegotiation session (TM_UNTRUSTED). And when the authentication succeeds it will replace the current session. Since we already have gone through connect stages and set context_auth to CAS_SUCCEEDED, we don't call all the connect stages again, and therefore also never call multi_client_generate_tls_keys for this session.

Thanks for explaining this further.
I agree that adding some more text to the commit message would be beneficial.

Other than that the patch looks logically correct to me.

Basically context_auth is set CAS_SUCCEEDED upon first connection cycle, therefore a new connection that re-uses the existing instance will already have context_auth set to CAS_SUCCEEDED and a new session key will be generated.

I wonder if the connection can really be fully working if the server believes that the client was already setup and does not performs the usual connection steps. But this is another story, unrelated to this patch...

This said, the new if condition does not look being properly aligned.

Cheers,

Signature · 1 line
Antonio Quartulli
#5 lore raw

Re: [Openvpn-devel] [PATCH 2/2] Fix condition to generate session keys

Quoted 11 lines

Basically context_auth is set CAS_SUCCEEDED upon first connection cycle, therefore a new connection that re-uses the existing instance will already have context_auth set to CAS_SUCCEEDED and a new session key will be generated.

I wonder if the connection can really be fully working if the server believes that the client was already setup and does not performs the usual connection steps. But this is another story, unrelated to this patch...

It works. From the server perspective this new connection is almost the same as a renegotiation. On a PUSH_REQUEST the server will just send the same configuration as before. Basically if the new client (which is more than often the same client) can do everything the previous client did, everything is well.

Arne

#7 lore raw

PATCH v2 1/2 Move context_auth from context_2 to tls_multi and name it multi_state

In reply to Arne Schwabe

context_2 and tls_multi have the same life cycle for TLS connections but so this move does not affect behaviour of the variable.

OpenVPN TLS multi code has a grown a lot more complex and code that handles multi objects needs to know the state that the object is in. Since not all code has access to the context_2 struct, the code that does not have access is often not checking the state directly but checks other parts of multi that have been affected from a state change.

This patch also renames it to multi_state as this variable represents t he multi state machine status rather than just the state of the connect authentication (more upcoming patches will move other states into this variable).

Patch V2: also rename context_auth to multi_state, explain a bit why this
change is done.

---
 src/openvpn/forward.c    |  4 ++--
 src/openvpn/multi.c      | 26 +++++++++++++-------------
 src/openvpn/openvpn.h    | 14 --------------
 src/openvpn/push.c       |  6 +++---
 src/openvpn/ssl_common.h | 14 ++++++++++++++
 5 files changed, 32 insertions(+), 32 deletions(-)
src/openvpn/forward.c +2 -2
index 6f7a50048..e3890f395 100644
@@ -544,7 +544,7 @@ encrypt_sign(struct context *c, bool comp_frag)
      * Drop non-TLS outgoing packet if client-connect script/plugin
      * has not yet succeeded.
      */
-    if (c->c2.context_auth != CAS_SUCCEEDED)
+    if (c->c2.tls_multi && c->c2.tls_multi->multi_state != CAS_SUCCEEDED)
     {
         c->c2.buf.len = 0;
     }
@@ -981,7 +981,7 @@ process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, boo
          * Drop non-TLS packet if client-connect script/plugin and cipher selection
          * has not yet succeeded.
          */
-        if (c->c2.context_auth != CAS_SUCCEEDED)
+        if (c->c2.tls_multi && c->c2.tls_multi->multi_state != CAS_SUCCEEDED)
         {
             c->c2.buf.len = 0;
         }
src/openvpn/multi.c +13 -13
index f7e0f6805..d33f58299 100644
@@ -676,7 +676,7 @@ multi_close_instance(struct multi_context *m,
 #ifdef ENABLE_MANAGEMENT
     set_cc_config(mi, NULL);
 #endif
-    if (mi->context.c2.context_auth == CAS_SUCCEEDED)
+    if (mi->context.c2.tls_multi->multi_state == CAS_SUCCEEDED)
     {
         multi_client_disconnect_script(mi);
     }
@@ -786,7 +786,7 @@ multi_create_instance(struct multi_context *m, const struct mroute_addr *real)
         goto err;
     }
 
-    mi->context.c2.context_auth = CAS_PENDING;
+    mi->context.c2.tls_multi->multi_state = CAS_PENDING;
 
     if (hash_n_elements(m->hash) >= m->max_clients)
     {
@@ -2416,18 +2416,18 @@ 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.context_auth = CAS_SUCCEEDED;
+    mi->context.c2.tls_multi->multi_state = CAS_SUCCEEDED;
 
     /* authentication complete, calculate dynamic client specific options */
     if (!multi_client_set_protocol_options(&mi->context))
     {
-        mi->context.c2.context_auth = CAS_FAILED;
+        mi->context.c2.tls_multi->multi_state = CAS_FAILED;
     }
     /* Generate data channel keys only if setting protocol options
      * has not failed */
     else if (!multi_client_generate_tls_keys(&mi->context))
     {
-        mi->context.c2.context_auth = CAS_FAILED;
+        mi->context.c2.tls_multi->multi_state = CAS_FAILED;
     }
 
     /* send push reply if ready */
@@ -2575,7 +2575,7 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
 
     /* We are only called for the CAS_PENDING_x states, so we
      * can ignore other states here */
-    bool from_deferred = (mi->context.c2.context_auth != CAS_PENDING);
+    bool from_deferred = (mi->context.c2.tls_multi->multi_state != CAS_PENDING);
 
     int *cur_handler_index = &mi->client_connect_defer_state.cur_handler_index;
     unsigned int *option_types_found =
@@ -2587,7 +2587,7 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
         *cur_handler_index = 0;
         *option_types_found = 0;
         /* Initially we have no handler that has returned a result */
-        mi->context.c2.context_auth = CAS_PENDING_DEFERRED;
+        mi->context.c2.tls_multi->multi_state = CAS_PENDING_DEFERRED;
 
         multi_client_connect_early_setup(m, mi);
     }
@@ -2610,7 +2610,7 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
                  * Remember that we already had at least one handler
                  * returning a result should we go to into deferred state
                  */
-                mi->context.c2.context_auth = CAS_PENDING_DEFERRED_PARTIAL;
+                mi->context.c2.tls_multi->multi_state = CAS_PENDING_DEFERRED_PARTIAL;
                 break;
 
             case CC_RET_SKIPPED:
@@ -2662,12 +2662,12 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
     {
         /* run the disconnect script if we had a connect script that
          * did not fail */
-        if (mi->context.c2.context_auth == CAS_PENDING_DEFERRED_PARTIAL)
+        if (mi->context.c2.tls_multi->multi_state == CAS_PENDING_DEFERRED_PARTIAL)
         {
             multi_client_disconnect_script(mi);
         }
 
-        mi->context.c2.context_auth = CAS_FAILED;
+        mi->context.c2.tls_multi->multi_state = CAS_FAILED;
     }
 
     /* increment number of current authenticated clients */
@@ -2970,13 +2970,13 @@ 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.context_auth)
+            if (is_cas_pending(mi->context.c2.tls_multi->multi_state)
                 && CONNECTION_ESTABLISHED(&mi->context))
             {
                 multi_connection_established(m, mi);
             }
 #if defined(ENABLE_ASYNC_PUSH)
-            if (is_cas_pending(mi->context.c2.context_auth)
+            if (is_cas_pending(mi->context.c2.authenticated)
                 && mi->client_connect_defer_state.deferred_ret_file)
             {
                 add_inotify_file_watch(m, mi, m->top.c2.inotify_fd,
@@ -3936,7 +3936,7 @@ management_client_auth(void *arg,
         {
             if (auth)
             {
-                if (is_cas_pending(mi->context.c2.context_auth))
+                if (is_cas_pending(mi->context.c2.tls_multi->multi_state))
                 {
                     set_cc_config(mi, cc_config);
                     cc_config_owned = false;
src/openvpn/openvpn.h +0 -14
index 3cef26381..d77ab223c 100644
@@ -207,17 +207,6 @@ struct context_1
 };
 
 
-/* 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_PENDING,
-    CAS_PENDING_DEFERRED,
-    CAS_PENDING_DEFERRED_PARTIAL,   /**< at least handler succeeded, no result yet*/
-    CAS_FAILED,
-};
-
 static inline bool
 is_cas_pending(enum client_connect_status cas)
 {
@@ -454,9 +443,6 @@ struct context_2
     int push_ifconfig_ipv6_netbits;
     struct in6_addr push_ifconfig_ipv6_remote;
 
-
-    enum client_connect_status context_auth;
-
     struct event_timeout push_request_interval;
     time_t push_request_timeout;
 
src/openvpn/push.c +3 -3
index 18d7c1e00..b43ffa364 100644
@@ -854,14 +854,14 @@ process_incoming_push_request(struct context *c)
 {
     int ret = PUSH_MSG_ERROR;
 
-    if ((c->c2.tls_multi && tls_authentication_status(c->c2.tls_multi, 0) == TLS_AUTHENTICATION_FAILED)
-        || c->c2.context_auth == CAS_FAILED)
+    if (tls_authentication_status(c->c2.tls_multi, 0) == TLS_AUTHENTICATION_FAILED
+        || c->c2.tls_multi->multi_state == CAS_FAILED)
     {
         const char *client_reason = tls_client_reason(c->c2.tls_multi);
         send_auth_failed(c, client_reason);
         ret = PUSH_MSG_AUTH_FAILURE;
     }
-    else if (c->c2.context_auth == CAS_SUCCEEDED)
+    else if (c->c2.tls_multi->multi_state == CAS_SUCCEEDED)
     {
         time_t now;
 
src/openvpn/ssl_common.h +14 -0
index 4e1ff6c84..ee8d80c0e 100644
@@ -486,6 +486,19 @@ struct tls_session
  */
 #define KEY_SCAN_SIZE 3
 
+
+/* 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_PENDING,
+    CAS_PENDING_DEFERRED,
+    CAS_PENDING_DEFERRED_PARTIAL,   /**< at least handler succeeded, no result yet*/
+    CAS_FAILED,
+};
+
+
 /**
  * Security parameter state for a single VPN tunnel.
  * @ingroup control_processor
@@ -521,6 +534,7 @@ struct tls_multi
 
     int n_sessions;             /**< Number of sessions negotiated thus
                                  *   far. */
+    enum client_connect_status multi_state;
 
     /*
      * Number of errors.
Signature · 1 line
2.30.1
#8 lore raw

PATCH v2 2/2 Fix condition to generate session keys

When OpenVPN sees a new (SSL) connection via HARD_RESET or SOFT_RESET with the same port/ip as an existing session, it will give it the slot of the renegotiation session (TM_UNTRUSTED). And when the authentication succeeds it will replace the current session. In the case of a SOFT_RESET this a renegotiation and we will generated data channel keys at the of key_method_2_write function as key-id > 0.

For a HARD RESET the key-id is 0. Since we already have gone through connect stages and set context_auth to CAS_SUCCEEDED, we don't call all the connect stages again, and therefore also never call multi_client_generate_tls_keys for this session.

This commit changes postponing the key generation to be done only if the multi_connect has not yet been finished.

Patch V2: Explain better in the commit message why this change is done.

---
 src/openvpn/ssl.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
src/openvpn/ssl.c +13 -7
index 893e5753d..0da558392 100644
@@ -2240,7 +2240,8 @@ error:
  * to the TLS control channel (cleartext).
  */
 static bool
-key_method_2_write(struct buffer *buf, struct tls_session *session)
+key_method_2_write(struct buffer *buf, struct tls_multi *multi,
+                   struct tls_session *session)
 {
     struct key_state *ks = &session->key[KS_PRIMARY];      /* primary key */
 
@@ -2331,12 +2332,17 @@ key_method_2_write(struct buffer *buf, struct tls_session *session)
         goto error;
     }
 
-    /* Generate tunnel keys if we're a TLS server.
-     * If we're a p2mp server and IV_NCP >= 2 is negotiated, the first key
-     * generation is postponed until after the pull/push, so we can process pushed
-     * cipher directives.
+    /*
+     * Generate tunnel keys if we're a TLS server.
+     *
+     * 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.
      */
-    if (session->opt->server && !(session->opt->mode == MODE_SERVER && ks->key_id <= 0))
+    if (session->opt->server && (session->opt->mode != MODE_SERVER
+            || multi->multi_state == CAS_SUCCEEDED))
     {
         if (ks->authenticated > KS_AUTH_FALSE)
         {
@@ -2826,7 +2832,7 @@ tls_process(struct tls_multi *multi,
         if (!buf->len && ((ks->state == S_START && !session->opt->server)
                           || (ks->state == S_GOT_KEY && session->opt->server)))
         {
-            if (!key_method_2_write(buf, session))
+            if (!key_method_2_write(buf, multi, session))
             {
                 goto error;
             }
Signature · 1 line
2.30.1
Acked-by on v2 1/2
#15 lore raw

Re: [Openvpn-devel] [PATCH v2 1/2] Move context_auth from context_2 to tls_multi and name it multi_state

In reply to Arne Schwabe

Hi,

Quoted 10 lines · Arne Schwabe

context_2 and tls_multi have the same life cycle for TLS connections but so this move does not affect behaviour of the variable.

OpenVPN TLS multi code has a grown a lot more complex and code that handles multi objects needs to know the state that the object is in. Since not all code has access to the context_2 struct, the code that does not have access is often not checking the state directly but checks other parts of multi that have been affected from a state change.

Quoted 7 lines

This patch also renames it to multi_state as this variable represents t he multi state machine status rather than just the state of the connect authentication (more upcoming patches will move other states into this variable).

Patch V2: also rename context_auth to multi_state, explain a bit why this
change is done.

Thanks a lot. Much better now!

Quoted 2 lines

As explained in the commit message, this patch is really just a field move and it's expected to not change any behaviour. On top of that it is being moved between two objects sharing the same lifecycle.

Basic tests pass.
Compiled tested against my usual SSL libs zoo.

Signature · 1 line
Antonio Quartulli
Acked-by on v2 2/2
#9 lore raw

Re: [Openvpn-devel] [PATCH v2 2/2] Fix condition to generate session keys

In reply to Arne Schwabe

Hi,

Quoted 12 lines · Arne Schwabe

When OpenVPN sees a new (SSL) connection via HARD_RESET or SOFT_RESET with the same port/ip as an existing session, it will give it the slot of the renegotiation session (TM_UNTRUSTED). And when the authentication succeeds it will replace the current session. In the case of a SOFT_RESET this a renegotiation and we will generated data channel keys at the of key_method_2_write function as key-id > 0.

For a HARD RESET the key-id is 0. Since we already have gone through connect stages and set context_auth to CAS_SUCCEEDED, we don't call all the connect stages again, and therefore also never call multi_client_generate_tls_keys for this session.

Quoted 6 lines

This commit changes postponing the key generation to be done only if the multi_connect has not yet been finished.

Patch V2: Explain better in the commit message why this change is done.

Thanks for the more exhaustive explanation.

After a short discussion with Arne, I was able to easily replicate this bug and double check that this patch is indeed fixing it.

Code looks good, except for one line that is not indented properly. See below:

Quoted 28 lines
---
 src/openvpn/ssl.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
src/openvpn/ssl.c +4 -5
index 893e5753d..0da558392 100644
@@ -2240,7 +2240,8 @@ error:
  * to the TLS control channel (cleartext).
  */
 static bool
-key_method_2_write(struct buffer *buf, struct tls_session *session)
+key_method_2_write(struct buffer *buf, struct tls_multi *multi,
+                   struct tls_session *session)
 {
     struct key_state *ks = &session->key[KS_PRIMARY];      /* primary key */
 
@@ -2331,12 +2332,17 @@ key_method_2_write(struct buffer *buf, struct tls_session *session)
         goto error;
     }
 
-    /* Generate tunnel keys if we're a TLS server.
-     * If we're a p2mp server and IV_NCP >= 2 is negotiated, the first key
-     * generation is postponed until after the pull/push, so we can process pushed
-     * cipher directives.
+    /*
+     * Generate tunnel keys if we're a TLS server.

src/openvpn/ssl.c key_method_2_write(struct buffer *buf, struct tls_session *session) · line 2345

Quoted 10 lines
src/openvpn/ssl.c +8 -1
+     *
+     * 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.
      */
-    if (session->opt->server && !(session->opt->mode == MODE_SERVER && ks->key_id <= 0))
+    if (session->opt->server && (session->opt->mode != MODE_SERVER
+            || multi->multi_state == CAS_SUCCEEDED))

Indentation is bogus here.

src/openvpn/ssl.c tls_process(struct tls_multi *multi, · line 2838

Quoted 13 lines
src/openvpn/ssl.c +1 -1
     {
         if (ks->authenticated > KS_AUTH_FALSE)
         {
@@ -2826,7 +2832,7 @@ tls_process(struct tls_multi *multi,
         if (!buf->len && ((ks->state == S_START && !session->opt->server)
                           || (ks->state == S_GOT_KEY && session->opt->server)))
         {
-            if (!key_method_2_write(buf, session))
+            if (!key_method_2_write(buf, multi, session))
             {
                 goto error;
             }

Other than that:

Signature · 1 line
Antonio Quartulli
#16 lore raw

Re: [Openvpn-devel] [PATCH v2 1/2] Move context_auth from context_2 to tls_multi and name it multi_state

In reply to Arne Schwabe

Hi,

I would have merged this now, but it breaks ENABLE_ASYNC_PUSH... and while at it, I have more questions.

On Sun, Mar 28, 2021 at 02:02:40PM +0200, Arne Schwabe wrote:
[..]

Quoted 2 lines

Patch V2: also rename context_auth to multi_state, explain a bit why this
change is done.

[..]

src/openvpn/forward.c encrypt_sign(struct context *c, bool comp_frag) · line 550

Quoted 13 lines
src/openvpn/forward.c +1 -1
index 6f7a50048..e3890f395 100644
@@ -544,7 +544,7 @@ encrypt_sign(struct context *c, bool comp_frag)
      * Drop non-TLS outgoing packet if client-connect script/plugin
      * has not yet succeeded.
      */
-    if (c->c2.context_auth != CAS_SUCCEEDED)
+    if (c->c2.tls_multi && c->c2.tls_multi->multi_state != CAS_SUCCEEDED)
     {
         c->c2.buf.len = 0;
     }

This code assumes that c2.tls_multi can be NULL, otherwise the check is not needed. OTOH, *if* it is NULL, I think we should actually drop the packet, because then it's certainly not CAS_SUCCEEDED. So maybe make this:

Quoted 1 line

+ if (!c->c2.tls_multi || c->c2.tls_multi->multi_state != CAS_SUCCEEDED)

(two instances in forward.c)

src/openvpn/forward.c

Quoted 6 lines

#if defined(ENABLE_ASYNC_PUSH)

src/openvpn/forward.c +1 -1
-            if (is_cas_pending(mi->context.c2.context_auth)
+            if (is_cas_pending(mi->context.c2.authenticated)
                 && mi->client_connect_defer_state.deferred_ret_file)
             {
                 add_inotify_file_watch(m, mi, m->top.c2.inotify_fd,

This was overlooked, and the second line should be

Quoted 1 line

+ if (is_cas_pending(mi->context.c2.tls_multi->multi_state)

I think. But this is a code change, so I'm not doing this on my own.

With this change, the code compiles and passes my server side torture chamber...

Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2g 2d 2e 2f 2z1 2z2 3 4 4a 4b 5 5a 5b 5c 5d 5v1 5v2 5v3 5w1 5w2 5w3 5w4 5x1 5x2 5x3 5x4 6 7 7x 7y 8 8a 9 9a. Test sets failed: 1x 2w 2x 2y.

(The failed tests are all "MTU <-> NCP non-AEAD" related, so "as expected")

src/openvpn/push.c process_incoming_push_request(struct context *c) · line 858

Quoted 12 lines
src/openvpn/push.c +2 -2
index 18d7c1e00..b43ffa364 100644
@@ -854,14 +854,14 @@ process_incoming_push_request(struct context *c)
 {
     int ret = PUSH_MSG_ERROR;
 
-    if ((c->c2.tls_multi && tls_authentication_status(c->c2.tls_multi, 0) == TLS_AUTHENTICATION_FAILED)
-        || c->c2.context_auth == CAS_FAILED)
+    if (tls_authentication_status(c->c2.tls_multi, 0) == TLS_AUTHENTICATION_FAILED
+        || c->c2.tls_multi->multi_state == CAS_FAILED)

I wonder if the removal of the "if ((c->c2.tls_multi &&" part here is safe. Is this guaranteed to be non-null even for the p2p-tls case?

I have added a case to the test rig for a "mode tls-server" server, and client instances that run with "--client" or "--tls-client" - so, asking for a PUSH or not. These did not cause a server crash, so it seems "it is not NULL". But it would be nice to be sure, though :-)

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]...
#10 lore raw

Re: [Openvpn-devel] [PATCH v2 2/2] Fix condition to generate session keys

In reply to Arne Schwabe

Hi,

On Sun, Mar 28, 2021 at 02:02:41PM +0200, Arne Schwabe wrote:
[..]

src/openvpn/ssl.c error: · line 2245

Quoted 8 lines
src/openvpn/ssl.c +2 -1
@@ -2240,7 +2240,8 @@ error:
  * to the TLS control channel (cleartext).
  */
 static bool
-key_method_2_write(struct buffer *buf, struct tls_session *session)
+key_method_2_write(struct buffer *buf, struct tls_multi *multi,
+                   struct tls_session *session)
 {

For master, this conflicts with commit 8fa8a17528c (compress migrate), which already introduces the "multi" parameter. Master only needs the second hunk (documentation plus CAS_SUCCEEDED check).

For release/2.5, this should be fine.

How do we want to proceed? Take this as "for 2.5", and you send a v3 "for master"?

I have tested this patch for master, on top of "1666 with the ENABLE_ASYNC_PUSH fixed", and it passes the server side test rig...

Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2g 2d 2e 2f 2z1 2z2 3 4 4a 4b 5 5a 5b 5c 5d 5v1 5v2 5v3 5w1 5w2 5w3 5w4 5x1 5x2 5x3 5x4 6 7 7x 7y 8 8a 9 9a.

... and also the "I have a client with --bind and renegotiate and then reconnect right after ctrl-c" test... so this looks ready to go.

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]...
#11 lore raw

Re: [Openvpn-devel] [PATCH v2 2/2] Fix condition to generate session keys

Hi,

Quoted 1 line · Gert Doering

For release/2.5, this should be fine.

Confirming :-)

The patch 1/2 v2 (1666) needs some amount of force to go into 2.5 due to context changes, and one extra is_cas_pending(). 2/2 v2 (1667) goes right in, and succeeds

Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2g 2d 2e 2f 2z1 2z2 3 4 4a 4b 5 5a 5b 5c 5d 5v1 5v2 5v3 5w1 5w2 5w3 5w4 5x1 5x2 5x3 5x4 6 7 7x 7y 8 8a 9 9a.

and also the "reconnect from same port" thing works with this patch on 2.5.

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]...
#12 lore raw

Re: [Openvpn-devel] [PATCH v2 2/2] Fix condition to generate session keys

In reply to Gert Doering

Am 18.04.2021 um 12:11 schrieb Gert Doering:

Quoted 4 lines

Hi,

On Sun, Mar 28, 2021 at 02:02:41PM +0200, Arne Schwabe wrote:
[..]

Quoted 11 lines

src/openvpn/ssl.c error: · line 2245

Quoted 8 lines
src/openvpn/ssl.c +2 -1
@@ -2240,7 +2240,8 @@ error:
   * to the TLS control channel (cleartext).
   */
  static bool
-key_method_2_write(struct buffer *buf, struct tls_session *session)
+key_method_2_write(struct buffer *buf, struct tls_multi *multi,
+                   struct tls_session *session)
  {

For master, this conflicts with commit 8fa8a17528c (compress migrate), which already introduces the "multi" parameter. Master only needs the second hunk (documentation plus CAS_SUCCEEDED check).

I can resend it if you want but this should be a trivial conflict. Maybe even 3way merge might fix it. If you don't count this as trivial conflict (i.e. does the same as the other patch) then I can also resend it.

Arne

#13 lore raw

Re: [Openvpn-devel] [PATCH v2 2/2] Fix condition to generate session keys

Hi,

Quoted 7 lines · Arne Schwabe
Quoted 3 lines

For master, this conflicts with commit 8fa8a17528c (compress migrate), which already introduces the "multi" parameter. Master only needs the second hunk (documentation plus CAS_SUCCEEDED check).

I can resend it if you want but this should be a trivial conflict. Maybe even 3way merge might fix it. If you don't count this as trivial conflict (i.e. does the same as the other patch) then I can also resend it.

It's a trivial conflict (basically, 2 of 3 hunks are "already in", though the first looks different due to line wrapping, so 3way can't autofix it).

I basically just wanted to know how to proceed here :-) - so I'll proceed to merge this to 2.5 "as it is" and to master "only hunk 2".

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]...
#17 lore raw

Re: [Openvpn-devel] [PATCH v2 1/2] Move context_auth from context_2 to tls_multi and name it multi_state

In reply to Gert Doering

Am 18.04.21 um 11:11 schrieb Gert Doering:

Quoted 29 lines

Hi,

I would have merged this now, but it breaks ENABLE_ASYNC_PUSH... and while at it, I have more questions.

On Sun, Mar 28, 2021 at 02:02:40PM +0200, Arne Schwabe wrote:
[..]

Quoted 2 lines

Patch V2: also rename context_auth to multi_state, explain a bit why this
change is done.

[..]

src/openvpn/forward.c encrypt_sign(struct context *c, bool comp_frag) · line 550

Quoted 13 lines
src/openvpn/forward.c +1 -1
index 6f7a50048..e3890f395 100644
@@ -544,7 +544,7 @@ encrypt_sign(struct context *c, bool comp_frag)
      * Drop non-TLS outgoing packet if client-connect script/plugin
      * has not yet succeeded.
      */
-    if (c->c2.context_auth != CAS_SUCCEEDED)
+    if (c->c2.tls_multi && c->c2.tls_multi->multi_state != CAS_SUCCEEDED)
     {
         c->c2.buf.len = 0;
     }

This code assumes that c2.tls_multi can be NULL, otherwise the check is not needed. OTOH, *if* it is NULL, I think we should actually drop the packet, because then it's certainly not CAS_SUCCEEDED. So maybe make this:

Quoted 3 lines
Quoted 1 line

+ if (!c->c2.tls_multi || c->c2.tls_multi->multi_state != CAS_SUCCEEDED)

(two instances in forward.c)

Yes these are all "We always check multi_state unless we have no tls at all (!c->c2.tls_multi) then we skip all TLS related checks. I will add comments about this in the v3.

Quoted 8 lines

src/openvpn/forward.c

Quoted 7 lines

#if defined(ENABLE_ASYNC_PUSH)

src/openvpn/forward.c +1 -1
-            if (is_cas_pending(mi->context.c2.context_auth)
+            if (is_cas_pending(mi->context.c2.authenticated)
                 && mi->client_connect_defer_state.deferred_ret_file)
             {
                 add_inotify_file_watch(m, mi, m->top.c2.inotify_fd,
Quoted 8 lines

This was overlooked, and the second line should be

Quoted 1 line

+ if (is_cas_pending(mi->context.c2.tls_multi->multi_state)

I think. But this is a code change, so I'm not doing this on my own.

With this change, the code compiles and passes my server side torture chamber...

Yes. That is the correct change.

Quoted 15 lines

Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2g 2d 2e 2f 2z1 2z2 3 4 4a 4b 5 5a 5b 5c 5d 5v1 5v2 5v3 5w1 5w2 5w3 5w4 5x1 5x2 5x3 5x4 6 7 7x 7y 8 8a 9 9a. Test sets failed: 1x 2w 2x 2y.

(The failed tests are all "MTU <-> NCP non-AEAD" related, so "as expected")

Quoted 8 lines
src/openvpn/push.c +0 -0
index 18d7c1e00..b43ffa364 100644
@@ -854,14 +854,14 @@ process_incoming_push_request(struct context *c)
 {
     int ret = PUSH_MSG_ERROR;
 
Quoted 7 lines

src/openvpn/push.c process_incoming_push_request(struct context *c) · line 858

Quoted 4 lines
src/openvpn/push.c +2 -2
-    if ((c->c2.tls_multi && tls_authentication_status(c->c2.tls_multi, 0) == TLS_AUTHENTICATION_FAILED)
-        || c->c2.context_auth == CAS_FAILED)
+    if (tls_authentication_status(c->c2.tls_multi, 0) == TLS_AUTHENTICATION_FAILED
+        || c->c2.tls_multi->multi_state == CAS_FAILED)

I wonder if the removal of the "if ((c->c2.tls_multi &&" part here is safe. Is this guaranteed to be non-null even for the p2p-tls case?

Yes. TLS session all live in the multi structure. And client is already basically p2p tls mode.

Quoted 4 lines

I have added a case to the test rig for a "mode tls-server" server, and client instances that run with "--client" or "--tls-client" - so, asking for a PUSH or not. These did not cause a server crash, so it seems "it is not NULL". But it would be nice to be sure, though :-)

Yes. All control channel and multi related code always has multi defined.

Arne

Acked-by on v2 2/2
#14 lore raw

[Openvpn-devel] [PATCH applied] Re: Fix condition to generate session keys

In reply to Arne Schwabe

Adding my own ACK because it needed a bit of whack to go in, and due to my own testing.

The patch "as it is" applies to release/2.5 just fine, but conflicts with master because 2 of 3 hunks are already in, in a slightly different formatting. So, master only received the middle hunk, and the 2.5 change was not done by cherrypicking but by directly applying the patch.

Tested master and release/2.5 on the server torture test bed, including the "client connects, renegotiates, disconnects, and starts anew from the same source port" test, which would yield a broken session and now works fine.

Your patch has been applied to the master and release/2.5 branch.

I have added a reference to Trac #1316 to the commit message.

commit a005044be9ca77ee8a47cb65a603d0b1c41b99f4 (master) commit 227fbc117d58a87465804f7b2a5cd95ef1b94da6 (release/2.5)

Author: Arne Schwabe
Date:   Sun Mar 28 14:02:41 2021 +0200
     Fix condition to generate session keys

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg21873.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering