Introduce get_key_by_management_key_id helper function
PATCH 1/2 Introduce get_key_by_management_key_id helper function
This function allows us to map from a management key id to a key structure and also allows this function to be reused.
--- src/openvpn/ssl_common.h | 20 ++++++++++++++++++++ src/openvpn/ssl_verify.c | 17 +++++++---------- 2 files changed, 27 insertions(+), 10 deletions(-)
src/openvpn/ssl_common.h +20 -0
@@ -722,4 +722,24 @@ get_primary_key(const struct tls_multi *multi) return &multi->session[TM_ACTIVE].key[KS_PRIMARY]; } +#ifdef ENABLE_MANAGEMENT +/** +* Gets the \c key_state object that belong to the management key id or +* return NULL if not found. +*/ +static inline struct key_state * +get_key_by_management_key_id (struct tls_multi *multi,unsigned int mda_key_id) +{ +for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) + { +struct key_state *ks =get_key_scan (multi, i); +if (ks->mda_key_id == mda_key_id) + { +return ks; + } + } +return NULL ; +} +#endif +#endif /* SSL_COMMON_H_ */
src/openvpn/ssl_verify.c +7 -10
@@ -1268,22 +1268,19 @@ tls_authentication_status(struct tls_multi *multi)bool tls_authenticate_key (struct tls_multi *multi,const unsigned int mda_key_id,const bool auth,const char *client_reason) { -bool ret = false; +struct key_state *ks =NULL ;if (multi) { -int i; +auth_set_client_reason (multi, client_reason); -for (i =0 ; i <KEY_SCAN_SIZE ; ++i) + ks =get_key_by_management_key_id (multi, mda_key_id); + +if (ks) { -struct key_state *ks =get_key_scan (multi, i); -if (ks->mda_key_id == mda_key_id) - { - ks->mda_status = auth ?ACF_SUCCEEDED :ACF_FAILED ; - ret = true; - } + ks->mda_status = auth ?ACF_SUCCEEDED :ACF_FAILED ; } } -return ret; +return (bool ) ks; }#endif /* ifdef ENABLE_MANAGEMENT */
Signature · 1 line
2.39.2 (Apple Git-143)
PATCH 2/2 Fix CR_RESPONSE mangaement message using wrong key_id
the management interface expects the management key id instead of the openvpn key id. In the past they often were the same for low ids which hid the bug quite well.
Also do not pick uninitialised keystates (management key_id is not valid in these) and provide better logging when a key state is not found.
--- src/openvpn/push.c | 4 ++-- src/openvpn/ssl_common.h | 2 +- src/openvpn/ssl_verify.c | 5 +++++ 3 files changed, 8 insertions(+), 3 deletions(-)
src/openvpn/push.c +2 -2
@@ -267,9 +267,9 @@ 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 =get_primary_key (c->c2.tls_multi)->key_id; +unsigned int mda_key_id =get_primary_key (c->c2.tls_multi)->mda_key_id; -management_notify_client_cr_response (key_id, mda, es, m); +management_notify_client_cr_response (mda_key_id, mda, es, m);#endif #if ENABLE_PLUGIN verify_crresponse_plugin (c->c2.tls_multi, m);
src/openvpn/ssl_common.h +1 -1
@@ -733,7 +733,7 @@ get_key_by_management_key_id(struct tls_multi *multi, unsigned int mda_key_id)for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) {struct key_state *ks =get_key_scan (multi, i); -if (ks->mda_key_id == mda_key_id) +if (ks->mda_key_id == mda_key_id && ks->state >S_UNDEF ) {return ks; }
src/openvpn/ssl_verify.c +5 -0
@@ -1279,6 +1279,11 @@ tls_authenticate_key(struct tls_multi *multi, const unsigned int mda_key_id, con { ks->mda_status = auth ?ACF_SUCCEEDED :ACF_FAILED ; } + else + { +msg (D_TLS_DEBUG_LOW ,"%s: no key state found for management key id " +"%d" , __func__, mda_key_id); + } }return (bool ) ks; }
Signature · 1 line
2.39.2 (Apple Git-143)
Re: [Openvpn-devel] [PATCH 2/2] Fix CR_RESPONSE mangaement message using wrong key_id
Hi,
While this bugfix should be merged, I'm a conflicted about the way these two patches are split up. It just makes reviewing harder than it should be. They actually form two independent changes but with one half intersecting with the other for no reason.
Quoted 31 lines · Arne Schwabe
the management interface expects the management key id instead of the openvpn key id. In the past they often were the same for low ids which hid the bug quite well.
Also do not pick uninitialised keystates (management
key_idis not valid in these) and provide better logging when a key state is not found.Change-Id If9fa1165a0e886b570b3738546ed810a32367cbeSigned-off-by Arne Schwabe <[email protected]...>--- src/openvpn/push.c | 4 ++-- src/openvpn/ssl_common.h | 2 +- src/openvpn/ssl_verify.c | 5 +++++ 3 files changed, 8 insertions(+), 3 deletions(-)src/openvpn/push.c +2
-2@@ -267,9 +267,9 @@ 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 =get_primary_key (c->c2.tls_multi)->key_id; +unsigned int mda_key_id =get_primary_key (c->c2.tls_multi)->mda_key_id; -management_notify_client_cr_response (key_id, mda, es, m); +management_notify_client_cr_response (mda_key_id, mda, es, m);#endif
Quoted 1 line
#if
ENABLE_PLUGIN
verify_crresponse_plugin(c->c2.tls_multi, m);
Quoted 1 line
I think, this patch could end here the bugfix is complete without the need for 1/2.
The rest of the patch depends on 1/2, but is not required for the above. It could be combined with 1/2 and submitted as a follow up refactoring patch.
Quoted 27 lines
src/openvpn/ssl_common.h +1
-1@@ -733,7 +733,7 @@ get_key_by_management_key_id(struct tls_multi *multi, unsigned int mda_key_id)for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) {struct key_state *ks =get_key_scan (multi, i); -if (ks->mda_key_id == mda_key_id) +if (ks->mda_key_id == mda_key_id && ks->state >S_UNDEF ) {return ks;}src/openvpn/ssl_verify.c +0
-0@@ -1279,6 +1279,11 @@ tls_authenticate_key(struct tls_multi *multi, constunsigned int
mda_key_id, con{ ks->mda_status = auth ? ACF_SUCCEEDED : ACF_FAILED; }src/openvpn/ssl_verify.c +3
-0+else + { + msg(D_TLS_DEBUG_LOW, "%s: no key state found for managementkey id "
src/openvpn/ssl_verify.c tls_authenticate_key(struct tls_multi *multi, const unsigned int mda_key_id, con · line 1289
Quoted 5 lines
src/openvpn/ssl_verify.c +2
-0+ "%d", __func__, mda_key_id); + } }return (bool ) ks; }
Selva
Re: [Openvpn-devel] [PATCH 2/2] Fix CR_RESPONSE mangaement message using wrong key_id
Am 19.05.23 um 15:45 schrieb Selva Nair:
Quoted 3 lines
Hi,
While this bugfix should be merged, I'm a conflicted about the way these two patches are split up. It just makes reviewing harder than it should be. They actually form two independent changes but with one half intersecting with the other for no reason.
Yeah. I moved the logging to 1/2. It seems to fit much better in that patch.
Arne
PATCH v2 1/2 Introduce get_key_by_management_key_id helper function
In reply to Arne SchwabeThis function allows us to map from a management key id to a key structure and also allows this function to be reused.
Patch v2: add message when key is not found.
--- src/openvpn/ssl_common.h | 20 ++++++++++++++++++++ src/openvpn/ssl_verify.c | 23 +++++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-)
src/openvpn/ssl_common.h +20 -0
@@ -722,4 +722,24 @@ get_primary_key(const struct tls_multi *multi) return &multi->session[TM_ACTIVE].key[KS_PRIMARY]; } +#ifdef ENABLE_MANAGEMENT +/** +* Gets the \c key_state object that belong to the management key id or +* return NULL if not found. +*/ +static inline struct key_state * +get_key_by_management_key_id (struct tls_multi *multi,unsigned int mda_key_id) +{ +for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) + { +struct key_state *ks =get_key_scan (multi, i); +if (ks->mda_key_id == mda_key_id) + { +return ks; + } + } +return NULL ; +} +#endif +#endif /* SSL_COMMON_H_ */
src/openvpn/ssl_verify.c +13 -10
@@ -1268,22 +1268,25 @@ tls_authentication_status(struct tls_multi *multi)bool tls_authenticate_key (struct tls_multi *multi,const unsigned int mda_key_id,const bool auth,const char *client_reason) { -bool ret = false; +struct key_state *ks =NULL ;if (multi) { -int i; +auth_set_client_reason (multi, client_reason); -for (i =0 ; i <KEY_SCAN_SIZE ; ++i) + ks =get_key_by_management_key_id (multi, mda_key_id); + +if (ks) { -struct key_state *ks =get_key_scan (multi, i); -if (ks->mda_key_id == mda_key_id) - { - ks->mda_status = auth ?ACF_SUCCEEDED :ACF_FAILED ; - ret = true; - } + ks->mda_status = auth ?ACF_SUCCEEDED :ACF_FAILED ; } +else + { +msg (D_TLS_DEBUG_LOW ,"%s: no key state found for management key id " +"%d" , __func__, mda_key_id); + } + } -return ret; +return (bool ) ks; }#endif /* ifdef ENABLE_MANAGEMENT */
Signature · 1 line
2.39.2 (Apple Git-143)
PATCH v2 2/2 Fix CR_RESPONSE mangaement message using wrong key_id
the management interface expects the management key id instead of the openvpn key id. In the past they often were the same for low ids which hid the bug quite well.
Also do not pick uninitialised keystates (management key_id is not valid in these).
Patch v2: do not add logging
--- src/openvpn/push.c | 4 ++-- src/openvpn/ssl_common.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
src/openvpn/push.c +2 -2
@@ -267,9 +267,9 @@ 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 =get_primary_key (c->c2.tls_multi)->key_id; +unsigned int mda_key_id =get_primary_key (c->c2.tls_multi)->mda_key_id; -management_notify_client_cr_response (key_id, mda, es, m); +management_notify_client_cr_response (mda_key_id, mda, es, m);#endif #if ENABLE_PLUGIN verify_crresponse_plugin (c->c2.tls_multi, m);
src/openvpn/ssl_common.h +1 -1
@@ -733,7 +733,7 @@ get_key_by_management_key_id(struct tls_multi *multi, unsigned int mda_key_id)for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) {struct key_state *ks =get_key_scan (multi, i); -if (ks->mda_key_id == mda_key_id) +if (ks->mda_key_id == mda_key_id && ks->state >S_UNDEF ) {return ks; }
Signature · 1 line
2.39.2 (Apple Git-143)
Re: [Openvpn-devel] [PATCH v2 2/2] Fix CR_RESPONSE mangaement message using wrong key_id
Quoted 8 lines · Arne Schwabe
the management interface expects the management key id instead of the openvpn key id. In the past they often were the same for low ids which hid the bug quite well.
Also do not pick uninitialised keystates (management
key_idis not valid in these).Patch v2: do not add logging
Similar patch was sent in via https://github.com/OpenVPN/openvpn/pull/359,
so kinda sorta acked?
Quoted 28 lines
Change-Id If9fa1165a0e886b570b3738546ed810a32367cbeSigned-off-by Arne Schwabe <[email protected]...>--- src/openvpn/push.c | 4 ++-- src/openvpn/ssl_common.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)src/openvpn/push.c +2
-2@@ -267,9 +267,9 @@ 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 =get_primary_key (c->c2.tls_multi)->key_id; +unsigned int mda_key_id =get_primary_key (c->c2.tls_multi)->mda_key_id; -management_notify_client_cr_response (key_id, mda, es, m); +management_notify_client_cr_response (mda_key_id, mda, es, m);#endif #if ENABLE_PLUGIN verify_crresponse_plugin (c->c2.tls_multi, m);src/openvpn/ssl_common.h +0
-0
src/openvpn/ssl_common.h get_key_by_management_key_id(struct tls_multi *multi, unsigned int mda_key_id) · line 739
Quoted 8 lines
src/openvpn/ssl_common.h +1
-1@@ -733,7 +733,7 @@ get_key_by_management_key_id(struct tls_multi *multi, unsigned int mda_key_id)for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) {struct key_state *ks =get_key_scan (multi, i); -if (ks->mda_key_id == mda_key_id) +if (ks->mda_key_id == mda_key_id && ks->state >S_UNDEF ) {return ks; }
Regards,
Signature · 1 line
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH v2 2/2] Fix CR_RESPONSE mangaement message using wrong key_id
In reply to Arne SchwabeIt seems I missed to send this ACK to the list.. Here it is.
Quoted 48 lines · Arne Schwabe
the management interface expects the management key id instead of the openvpn key id. In the past they often were the same for low ids which hid the bug quite well.
Also do not pick uninitialised keystates (management
key_idis not valid in these).Patch v2: do not add logging
Change-Id If9fa1165a0e886b570b3738546ed810a32367cbeSigned-off-by Arne Schwabe <[email protected]...>--- src/openvpn/push.c | 4 ++-- src/openvpn/ssl_common.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)src/openvpn/push.c +2
-2@@ -267,9 +267,9 @@ 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 =get_primary_key (c->c2.tls_multi)->key_id; +unsigned int mda_key_id =get_primary_key (c->c2.tls_multi)->mda_key_id; -management_notify_client_cr_response (key_id, mda, es, m); +management_notify_client_cr_response (mda_key_id, mda, es, m);#endif #if ENABLE_PLUGIN verify_crresponse_plugin(c->c2.tls_multi, m);src/openvpn/ssl_common.h +1
-1@@ -733,7 +733,7 @@ get_key_by_management_key_id(struct tls_multi *multi, unsigned int mda_key_id)for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) {struct key_state *ks =get_key_scan (multi, i); -if (ks->mda_key_id == mda_key_id) +if (ks->mda_key_id == mda_key_id && ks->state >S_UNDEF ) {return ks;}
Quoted 4 lines
--
2.39.2 (Apple Git-143)
Acked-by Selva Nair <[email protected]...>
[Openvpn-devel] [PATCH applied] Re: Fix CR_RESPONSE mangaement message using wrong key_id
In reply to Arne SchwabeThanks for reminding me that there was something pending here as well...
This is a bit complicated, though. The bugfix needs to go to 2.6 as well, so "2/2 applied", but the v2 as sent to the list has a hunk that is really a bugfix for get_key_by_management_key_id(), which is in 1/2, and that is refactoring -> master.
So I've only applied the first hunk here, and the second hunk should go together with all of 1/2 into a "master only" patch. Right?
I have run this through the basic client test, which isn't exactly excercising the management interface (not at all) but I have nothing for that - so I'm relying on Selva's ACK and Jemmy Wang's test results in GH #359 on "does it actually fix the bug".
Your patch has been applied to the master and release/2.6 branch.
commit 223baa9c9b818e4c542a9037f190f53ce6f7af5c (master) commit 0caf0389454c75ae9dfeda096a820446d2d1a926 (HEAD -> release/2.6)
Author: Arne Schwabe Date: Mon May 22 12:11:38 2023 +0200
Fix CR_RESPONSE mangaement message using wrong key_id
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Selva Nair <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg26719.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
PATCH v3 1/1 Introduce get_key_by_management_key_id helper function
In reply to Arne SchwabeThis function allows us to map from a management key id to a key structure and also allows this function to be reused.
Patch v2: add message when key is not found.
Patch v3: only consider valid keys
--- src/openvpn/ssl_common.h | 20 ++++++++++++++++++++ src/openvpn/ssl_verify.c | 23 +++++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-)
src/openvpn/ssl_common.h +20 -0
@@ -722,4 +722,24 @@ get_primary_key(const struct tls_multi *multi) return &multi->session[TM_ACTIVE].key[KS_PRIMARY]; } +#ifdef ENABLE_MANAGEMENT +/** +* Gets the \c key_state object that belong to the management key id or +* return NULL if not found. +*/ +static inline struct key_state * +get_key_by_management_key_id (struct tls_multi *multi,unsigned int mda_key_id) +{ +for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) + { +struct key_state *ks =get_key_scan (multi, i); +if (ks->mda_key_id == mda_key_id && ks->state >S_UNDEF ) + { +return ks; + } + } +return NULL ; +} +#endif +#endif /* SSL_COMMON_H_ */
src/openvpn/ssl_verify.c +13 -10
@@ -1266,22 +1266,25 @@ tls_authentication_status(struct tls_multi *multi)bool tls_authenticate_key (struct tls_multi *multi,const unsigned int mda_key_id,const bool auth,const char *client_reason) { -bool ret = false; +struct key_state *ks =NULL ;if (multi) { -int i; +auth_set_client_reason (multi, client_reason); -for (i =0 ; i <KEY_SCAN_SIZE ; ++i) + ks =get_key_by_management_key_id (multi, mda_key_id); + +if (ks) { -struct key_state *ks =get_key_scan (multi, i); -if (ks->mda_key_id == mda_key_id) - { - ks->mda_status = auth ?ACF_SUCCEEDED :ACF_FAILED ; - ret = true; - } + ks->mda_status = auth ?ACF_SUCCEEDED :ACF_FAILED ; } +else + { +msg (D_TLS_DEBUG_LOW ,"%s: no key state found for management key id " +"%d" , __func__, mda_key_id); + } + } -return ret; +return (bool ) ks; }#endif /* ifdef ENABLE_MANAGEMENT */
Signature · 1 line
2.39.2 (Apple Git-143)
PATCH v3 1/1 Introduce get_key_by_management_key_id helper function
In reply to Arne SchwabeThis function allows us to map from a management key id to a key structure and also allows this function to be reused.
Patch v2: add message when key is not found.
Patch v3: only consider valid keys
--- src/openvpn/ssl_common.h | 20 ++++++++++++++++++++ src/openvpn/ssl_verify.c | 23 +++++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-)
src/openvpn/ssl_common.h +20 -0
@@ -722,4 +722,24 @@ get_primary_key(const struct tls_multi *multi) return &multi->session[TM_ACTIVE].key[KS_PRIMARY]; } +#ifdef ENABLE_MANAGEMENT +/** +* Gets the \c key_state object that belong to the management key id or +* return NULL if not found. +*/ +static inline struct key_state * +get_key_by_management_key_id (struct tls_multi *multi,unsigned int mda_key_id) +{ +for (int i =0 ; i <KEY_SCAN_SIZE ; ++i) + { +struct key_state *ks =get_key_scan (multi, i); +if (ks->mda_key_id == mda_key_id && ks->state >S_UNDEF ) + { +return ks; + } + } +return NULL ; +} +#endif +#endif /* SSL_COMMON_H_ */
src/openvpn/ssl_verify.c +13 -10
@@ -1266,22 +1266,25 @@ tls_authentication_status(struct tls_multi *multi)bool tls_authenticate_key (struct tls_multi *multi,const unsigned int mda_key_id,const bool auth,const char *client_reason) { -bool ret = false; +struct key_state *ks =NULL ;if (multi) { -int i; +auth_set_client_reason (multi, client_reason); -for (i =0 ; i <KEY_SCAN_SIZE ; ++i) + ks =get_key_by_management_key_id (multi, mda_key_id); + +if (ks) { -struct key_state *ks =get_key_scan (multi, i); -if (ks->mda_key_id == mda_key_id) - { - ks->mda_status = auth ?ACF_SUCCEEDED :ACF_FAILED ; - ret = true; - } + ks->mda_status = auth ?ACF_SUCCEEDED :ACF_FAILED ; } +else + { +msg (D_TLS_DEBUG_LOW ,"%s: no key state found for management key id " +"%d" , __func__, mda_key_id); + } + } -return ret; +return (bool ) ks; }#endif /* ifdef ENABLE_MANAGEMENT */
Signature · 1 line
2.39.2 (Apple Git-143)