Only announce IV_NCP=2 when we are willing to support these ciphers
PATCH v3 1/5 Only announce IV_NCP=2 when we are willing to support these ciphers
We currently always announce IV_NCP=2 when we support these ciphers even when we do not accept them. This lead to a server pushing a AES-GCM-128 cipher to clients and the client then rejecting it.
Patch V2: Remove unecessary restoring of ncp_ciphers Patch V3: Do not add ncp_ciphers in context
--- doc/openvpn.8 | 2 ++ src/openvpn/init.c | 1 + src/openvpn/ssl.c | 4 +++- src/openvpn/ssl_common.h | 1 + 4 files changed, 7 insertions(+), 1 deletion(-)
doc/openvpn.8 +2 -0
@@ -4398,6 +4398,8 @@ NCP server (v2.4+) with "\-\-cipher BF\-CBC" and "\-\-ncp\-ciphers AES\-256\-GCM:AES\-256\-CBC" set can either specify "\-\-cipher BF\-CBC" or "\-\-cipher AES\-256\-CBC" and both will work. +Note, for using NCP with a OpenVPN 2.4 server this list must include +the AES\-256\-GCM and AES\-128\-GCM ciphers. .\"********************************************************* .TP .B \-\-ncp\-disable
src/openvpn/init.c +1 -0
@@ -2827,6 +2827,7 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) to.tcp_mode =link_socket_proto_connection_oriented (options->ce.proto); to.config_ciphername = c->c1.ciphername; to.config_authname = c->c1.authname; + to.config_ncp_ciphers = options->ncp_ciphers; to.ncp_enabled = options->ncp_enabled; to.transition_window = options->transition_window; to.handshake_window = options->handshake_window;
src/openvpn/ssl.c +3 -1
@@ -2322,7 +2322,9 @@ push_peer_info(struct buffer *buf, struct tls_session *session)/* support for Negotiable Crypto Parameters */ if (session->opt->ncp_enabled - && (session->opt->mode ==MODE_SERVER || session->opt->pull)) + && (session->opt->mode ==MODE_SERVER || session->opt->pull) + &&tls_item_in_cipher_list ("AES-128-GCM" , session->opt->config_ncp_ciphers) + &&tls_item_in_cipher_list ("AES-256-GCM" , session->opt->config_ncp_ciphers)) {buf_printf (&out,"IV_NCP=2\n" ); }
src/openvpn/ssl_common.h +1 -0
@@ -290,6 +290,7 @@ struct tls_optionsconst char *config_ciphername;const char *config_authname; +const char *config_ncp_ciphers;bool ncp_enabled;bool tls_crypt_v2;
Signature · 1 line
2.25.0
PATCH v3 2/5 Implement dynamic NCP negotiation
Our current NCP version is flawed in the way that it can only indicate support for AES-256-GCM and AES-128-GCM. While configuring client and server with different ncp-cipher configuration directive works, the server will blindly push the first cipher of that list to the client if the client sends IV_NCP=2.
This patches introduces IV_CIPHER sent from the client to the server that contains the full list of cipers that the client is willing to support (*). The server will then pick the first ciphr of its own ncp-cipher list that the client indicates support for.
We choose a textual representation of the ciphers instead of a binary sinc a binary would mean that we would need to have a central place to maintain a mapping between binary and the actual cipher name. Also the normal ncp-cipher list is quite short, so this should not be problem. It also provides the freedom to negioate new ciphers from SSL libraries without the need to upgrade OpenVPN/its binary cipher table.
* the client/server will also accpt the cipher specifid in --cipher but eventually we want to get rid of --ciper. So this patch keeps a reasonable backwards compatbility (especially poor man's NCP) but does not encourage to use --cipher for negotiation in documentation or warning messages.
Patch V2: Remove #include "ssl_ncp.h" Note to compile on windows the patch
"Add strsep compat function" should be applied first
Patch V3: Use string_alloc with gc instead strdup()
--- doc/openvpn.8 | 10 +++- src/openvpn/init.c | 1 - src/openvpn/push.c | 31 +++++++---- src/openvpn/ssl.c | 110 +++++++++++++++++++++++++++++++++++++-- src/openvpn/ssl.h | 34 ++++++++++++ src/openvpn/ssl_common.h | 1 - 6 files changed, 169 insertions(+), 18 deletions(-)
doc/openvpn.8 +8 -2
@@ -3117,6 +3117,11 @@ IV_NCP=2 \-\- negotiable ciphers, client supports pushed by the server, a value of 2 or greater indicates client supports AES\-GCM\-128 and AES\-GCM\-256. +IV_CIPHERS=<ncp\-ciphers> \-\- the client pushes the list of configured +ciphers with the +\.B -\-\ncp\-ciphers +option to the server. + IV_GUI_VER=<gui_id> <version> \-\- the UI version of a UI if one is running, for example "de.blinkt.openvpn 0.5.47" for the Android app.
@@ -4374,7 +4379,8 @@ is a colon\-separated list of ciphers, and defaults to For servers, the first cipher from .B cipher_list -will be pushed to clients that support cipher negotiation. +that is also supported by the client will be pushed to clients +that support cipher negotiation. Cipher negotiation is enabled in client\-server mode only. I.e. if .B \-\-mode
@@ -4398,7 +4404,7 @@ NCP server (v2.4+) with "\-\-cipher BF\-CBC" and "\-\-ncp\-ciphers AES\-256\-GCM:AES\-256\-CBC" set can either specify "\-\-cipher BF\-CBC" or "\-\-cipher AES\-256\-CBC" and both will work. -Note, for using NCP with a OpenVPN 2.4 server this list must include +Note, for using NCP with a OpenVPN 2.4 peer this list must include the AES\-256\-GCM and AES\-128\-GCM ciphers. .\"********************************************************* .TP
src/openvpn/init.c +0 -1
@@ -2826,7 +2826,6 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) to.replay_time = options->replay_time; to.tcp_mode =link_socket_proto_connection_oriented (options->ce.proto); to.config_ciphername = c->c1.ciphername; - to.config_authname = c->c1.authname; to.config_ncp_ciphers = options->ncp_ciphers; to.ncp_enabled = options->ncp_enabled; to.transition_window = options->transition_window;
src/openvpn/push.c +22 -9
@@ -429,7 +429,7 @@ prepare_push_reply(struct context *c, struct gc_arena *gc,prepare_auth_token_push_reply (tls_multi, gc, push_list);/* Push cipher if client supports Negotiable Crypto Parameters */ -if (tls_peer_info_ncp_ver (peer_info) >=2 && o->ncp_enabled) +if (o->ncp_enabled) {/* if we have already created our key, we cannot *change* our own * cipher -> so log the fact and push the "what we have now" cipher
@@ -445,17 +445,30 @@ prepare_push_reply(struct context *c, struct gc_arena *gc, }else { -/* Push the first cipher from --ncp-ciphers to the client. -* TODO: actual negotiation, instead of server dictatorship. */ -char *push_cipher =string_alloc (o->ncp_ciphers, &o->gc); - o->ciphername =strtok (push_cipher,":" ); +/* +* Push the first cipher from --ncp-ciphers to the client that +* the client announces to be supporting. +*/ +char *push_cipher =ncp_get_best_cipher (o->ncp_ciphers, o->ciphername, + peer_info, + tls_multi->remote_ciphername, + &o->gc); + +if (push_cipher) + { + o->ciphername = push_cipher; + } +else + { +const char *peer_ciphers =tls_peer_ncp_list (peer_info, gc); +msg (M_INFO ,"PUSH: No common cipher between server and client." +"Expect this connection not to work. " +"Server ncp-ciphers: '%s', client supported ciphers '%s'" , + o->ncp_ciphers, peer_ciphers); + } }push_option_fmt (gc, push_list,M_USAGE ,"cipher %s" , o->ciphername); } -else if (o->ncp_enabled) - { -tls_poor_mans_ncp (o, tls_multi->remote_ciphername); - }return true; }
src/openvpn/ssl.c +105 -5
135 lines not on this page · raw message
src/openvpn/ssl.h +34 -0
@@ -512,6 +512,40 @@ tls_get_peer_info(const struct tls_multi *multi)*/ int tls_peer_info_ncp_ver (const char *peer_info); +/** +* Iterates through the ciphers in server_list and return the first +* cipher that is also supported by the peer according to the IV_NCP +* and IV_CIPHER values in peer_info. +* +* We also accept a cipher that is the remote cipher of the client for +* "Poor man's NCP": Use peer cipher if it is an allowed (NCP) cipher. +* Allows non-NCP peers to upgrade their cipher individually. +* +* Make sure to call tls_session_update_crypto_params() after calling this +* function. +* +* @param gc gc arena that is ONLY used to allocate the returned string +* +* @returns NULL if no common cipher is available, otherwise the best common +* cipher +*/ +char * +ncp_get_best_cipher (const char *server_list,const char *server_cipher, +const char *peer_info,const char *remote_cipher, +struct gc_arena *gc); + + +/** +* Returns the support cipher list from the peer according to the IV_NCP +* and IV_CIPHER values in peer_info. +* +* @returns Either a string containing the ncp list that is either static +* or allocated via gc. If no information is available an empty string +* ("") is returned. +*/ +const char * +tls_peer_ncp_list (const char *peer_info,struct gc_arena *gc); +/** * Check whether the ciphers in the supplied list are supported. *
src/openvpn/ssl_common.h +0 -1
@@ -289,7 +289,6 @@ struct tls_optionsbool tcp_mode;const char *config_ciphername; -const char *config_authname;const char *config_ncp_ciphers;bool ncp_enabled;
Signature · 1 line
2.25.0
Re: [Openvpn-devel] [PATCH v3 2/5] Implement dynamic NCP negotiation
Hi,
Tested with Windows server and Linux client, negotiation works.
A few nit-picks, which could be fixed with follow-up patch:
Quoted 1 line
+ * @param gc gc arena that is ONLY used to allocate the returned string
This is not true, since it is also used inside tls_peer_ncp_list() to generate temp string containing client pushed ciphers.
src/openvpn/ssl.c tls_item_in_cipher_list(const char *item, const char *list) · line 1976
Quoted 5 lines
src/openvpn/ssl.c +5
-0+ncp_get_best_cipher(const char *server_list, const char *server_cipher, + const char *peer_info, const char *remote_cipher, + struct gc_arena *gc) +{ + const char *peer_ncp_list = tls_peer_ncp_list(peer_info, gc);
That gc has lifetime of VPN tunnel and is used here to allocate a temp string which is needed only in this function.
Quoted 1 line
- const char
*config_authname;
This doesn't seem to be related to NCP negotiation.
Re: [Openvpn-devel] [PATCH v3 2/5] Implement dynamic NCP negotiation
Am 17.02.20 um 14:42 schrieb Lev Stipakov:
Quoted 11 lines
Hi,
Tested with Windows server and Linux client, negotiation works.
A few nit-picks, which could be fixed with follow-up patch:
Quoted 1 line
+ * @param gc gc arena that is ONLY used to allocate the returned string
This is not true, since it is also used inside
tls_peer_ncp_list()to generate temp string containing client pushed ciphers.
Quoted 8 lines
src/openvpn/ssl.c tls_item_in_cipher_list(const char *item, const char *list) · line 1976
Quoted 5 lines
src/openvpn/ssl.c +5
-0+ncp_get_best_cipher(const char *server_list, const char *server_cipher, + const char *peer_info, const char *remote_cipher, + struct gc_arena *gc) +{ + const char *peer_ncp_list = tls_peer_ncp_list(peer_info, gc);That gc has lifetime of VPN tunnel and is used here to allocate a temp string which is needed only in this function.
I sent a patch to fix those as 6/5.
Quoted 4 lines
Quoted 2 lines
- const char
*config_authname;This doesn't seem to be related to NCP negotiation.
The first version of NCP was intended to later also allow to negiate authname aswell as cipher, so saving the config_authname would have been required but this was never implemented and Steffan and I discussed this during the last Hackathon and we concluded that we don't want dynamic --auth as part of NCPv2 as well, so NCPv2 removes this leftover code.
Arne
PATCH v3 3/5 Move NCP related function into a seperate file and add unit tests
In reply to Arne SchwabeThis allows unit test the NCP functions. The ssl.c file has too many dependencies to make unit testing of it viable.
Patch V2: Removing the include "ssl_ncp.h" from options.c for V2 of
implement dynamic NCP forces a new version of this patch to add the #include in this patch. Merge VS studio file changes for ssl_ncp.[ch] into this patch
Patch V3: Regenerate for changes in earlier patches, apply Lev's changes
to Visual Studio project file
--- src/openvpn/Makefile.am | 1 + src/openvpn/init.c | 1 + src/openvpn/multi.c | 1 + src/openvpn/openvpn.vcxproj | 2 + src/openvpn/openvpn.vcxproj.filters | 8 +- src/openvpn/options.c | 1 + src/openvpn/push.c | 1 + src/openvpn/ssl.c | 171 +------------------- src/openvpn/ssl.h | 65 -------- src/openvpn/ssl_ncp.c | 224 +++++++++++++++++++++++++++ src/openvpn/ssl_ncp.h | 101 ++++++++++++ tests/unit_tests/openvpn/Makefile.am | 18 ++- tests/unit_tests/openvpn/test_ncp.c | 182 ++++++++++++++++++++++ 13 files changed, 540 insertions(+), 236 deletions(-) create mode 100644 src/openvpn/ssl_ncp.c create mode 100644 src/openvpn/ssl_ncp.h create mode 100644 tests/unit_tests/openvpn/test_ncp.c
src/openvpn/Makefile.am +1 -0
@@ -116,6 +116,7 @@ openvpn_SOURCES = \ ssl.c ssl.h ssl_backend.h \ ssl_openssl.c ssl_openssl.h \ ssl_mbedtls.c ssl_mbedtls.h \ + ssl_ncp.c ssl_ncp.h \ ssl_common.h \ ssl_verify.c ssl_verify.h ssl_verify_backend.h \ ssl_verify_openssl.c ssl_verify_openssl.h \
src/openvpn/init.c +1 -0
@@ -49,6 +49,7 @@#include "ping.h" #include "mstats.h" #include "ssl_verify.h" +#include "ssl_ncp.h" #include "tls_crypt.h" #include "forward.h" #include "auth_token.h"
src/openvpn/multi.c +1 -0
@@ -45,6 +45,7 @@#include "gremlin.h" #include "mstats.h" #include "ssl_verify.h" +#include "ssl_ncp.h" #include "vlan.h" #include <inttypes.h>
src/openvpn/openvpn.vcxproj +2 -0
@@ -192,6 +192,7 @@ <ClCompile Include="socks.c" /> <ClCompile Include="ssl.c" /> <ClCompile Include="ssl_openssl.c" /> + <ClCompile Include="ssl_ncp.c" /> <ClCompile Include="ssl_verify.c" /> <ClCompile Include="ssl_verify_openssl.c" /> <ClCompile Include="status.c" />
@@ -278,6 +279,7 @@ <ClInclude Include="ssl.h" /> <ClInclude Include="ssl_backend.h" /> <ClInclude Include="ssl_common.h" /> + <ClInclude Include="ssl_ncp.h" /> <ClInclude Include="ssl_openssl.h" /> <ClInclude Include="ssl_verify.h" /> <ClInclude Include="ssl_verify_backend.h" />
src/openvpn/openvpn.vcxproj.filters +7 -1
@@ -243,6 +243,9 @@ <ClCompile Include="ring_buffer.c"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="ssl_ncp.c"> + <Filter>Source Files</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="base64.h">
@@ -506,10 +509,13 @@ <ClInclude Include="ring_buffer.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="ssl_ncp.h"> + <Filter>Header Files</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ResourceCompile Include="openvpn_win32_resources.rc"> <Filter>Resource Files</Filter> </ResourceCompile> </ItemGroup> -</Project> +</Project>
src/openvpn/options.c +1 -0
@@ -45,6 +45,7 @@#include "shaper.h" #include "crypto.h" #include "ssl.h" +#include "ssl_ncp.h" #include "options.h" #include "misc.h" #include "socket.h"
src/openvpn/push.c +1 -0
@@ -33,6 +33,7 @@#include "options.h" #include "ssl.h" #include "ssl_verify.h" +#include "ssl_ncp.h" #include "manage.h" #include "memdbg.h"
src/openvpn/ssl.c +2 -169
201 lines not on this page · raw message
src/openvpn/ssl.h +0 -65
77 lines not on this page · raw message
src/openvpn/ssl_ncp.c new file +224 -0
224 lines not on this page · raw message
src/openvpn/ssl_ncp.h new file +101 -0
101 lines not on this page · raw message
tests/unit_tests/openvpn/Makefile.am +17 -1
@@ -6,7 +6,7 @@ if HAVE_LD_WRAP_SUPPORT test_binaries += argv_testdriver buffer_testdriver endif -test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver +test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver ncp_testdriver if HAVE_LD_WRAP_SUPPORT test_binaries += tls_crypt_testdriver endif
@@ -110,3 +110,19 @@ auth_token_testdriver_SOURCES = test_auth_token.c mock_msg.c \ $(openvpn_srcdir)/packet_id.c \ $(openvpn_srcdir)/platform.c \ $(openvpn_srcdir)/base64.c + + +ncp_testdriver_CFLAGS = @TEST_CFLAGS@ \ + -I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir) \ + $(OPTIONAL_CRYPTO_CFLAGS) +ncp_testdriver_LDFLAGS = @TEST_LDFLAGS@ \ + $(OPTIONAL_CRYPTO_LIBS) + +ncp_testdriver_SOURCES = test_ncp.c mock_msg.c \ + $(openvpn_srcdir)/buffer.c \ + $(openvpn_srcdir)/crypto.c \ + $(openvpn_srcdir)/crypto_mbedtls.c \ + $(openvpn_srcdir)/crypto_openssl.c \ + $(openvpn_srcdir)/otime.c \ + $(openvpn_srcdir)/packet_id.c \ + $(openvpn_srcdir)/platform.c
tests/unit_tests/openvpn/test_ncp.c new file +182 -0
182 lines not on this page · raw message
Signature · 1 line
2.25.0
PATCH v3 4/5 Normalise ncp-ciphers option and restrict it to 127 bytes
In reply to Arne SchwabeIn scenarios of mbed TLS vs OpenSSL we already normalise the ciphers that are send via the wire protocol via OCC to not have a mismatch warning between server and client. This is done by translate_cipher_name_from_openvpn. The same applies also to the ncp-ciphers list. Specifying non normalised names in ncp-ciphers will cause negotation not to succeed if ciphers are not in the same form. Therefore we will normalise the ciphers in options_postmutate.
The alternative and a lot less user friendly alternative would be to bail if on of the ciphers in ncp-ciphers is not in its normalised form.
Also restrict the ncp-ciphers list to 127. This is somewhat arbitrary but should prevent too large IV_CIPHER messages and problems sending those. The server will accept also large IV_CIPHER values from clients.
Patch V2: Correct comment about normalising ciphers Patch V3: Correct #ifdef statement
--- doc/openvpn.8 | 3 ++ src/openvpn/options.c | 14 ++++--- src/openvpn/ssl_ncp.c | 57 +++++++++++++++++++++++++---- src/openvpn/ssl_ncp.h | 19 +++++++++- tests/unit_tests/openvpn/test_ncp.c | 44 ++++++++++++++++++---- 5 files changed, 115 insertions(+), 22 deletions(-)
doc/openvpn.8 +3 -0
@@ -4406,6 +4406,9 @@ AES\-256\-GCM:AES\-256\-CBC" set can either specify "\-\-cipher BF\-CBC" or Note, for using NCP with a OpenVPN 2.4 peer this list must include the AES\-256\-GCM and AES\-128\-GCM ciphers. + +This list is restricted to be 127 chars long after conversion to OpenVPN +ciphers. .\"********************************************************* .TP .B \-\-ncp\-disable
src/openvpn/options.c +9 -5
@@ -2511,11 +2511,6 @@ options_postprocess_verify_ce(const struct options *options, const struct connec }#endif /* P2MP_SERVER */ -if (options->ncp_enabled && !tls_check_ncp_cipher_list (options->ncp_ciphers)) - { -msg (M_USAGE ,"NCP cipher list contains unsupported ciphers." ); - } -if (options->keysize) {msg (M_WARN ,"WARNING: --keysize is DEPRECATED and will be removed in OpenVPN 2.6" );
@@ -3093,6 +3088,15 @@ options_postprocess_mutate(struct options *o)options_postprocess_mutate_invariant (o); +if (o->ncp_enabled) + { + o->ncp_ciphers =mutate_ncp_cipher_list (o->ncp_ciphers, &o->gc); +if (o->ncp_ciphers ==NULL ) + { +msg (M_USAGE ,"NCP cipher list contains unsupported ciphers or is too long." ); + } + } +if (o->remote_list && !o->connection_list) {/*
src/openvpn/ssl_ncp.c +50 -7
77 lines not on this page · raw message
src/openvpn/ssl_ncp.h +17 -2
@@ -87,10 +87,17 @@ tls_peer_ncp_list(const char *peer_info, struct gc_arena *gc);* Check whether the ciphers in the supplied list are supported. * * @param list Colon-separated list of ciphers +* @parms gc gc_arena to allocate the returned string * -* @returns true iff all ciphers in list are supported. +* @returns colon separated string of normalised (via +* translate_cipher_name_from_openvpn) and +* zero terminated string iff all ciphers +* in list are supported and the total length +* is short than MAX_NCP_CIPHERS_LENGTH. NULL +* otherwise. */ -bool tls_check_ncp_cipher_list (const char *list); +char * +mutate_ncp_cipher_list (const char *list,struct gc_arena *gc);/** * Return true iff item is present in the colon-separated zero-terminated
@@ -98,4 +105,12 @@ bool tls_check_ncp_cipher_list(const char *list);*/ bool tls_item_in_cipher_list (const char *item,const char *list); +/** +* The maximum length of a ncp-cipher string that is accepted. +* +* Since this list needs to be pushed as IV_CIPHERS, we are conservative +* about its length. +*/ +#define MAX_NCP_CIPHERS_LENGTH 127 +#endif /* ifndef OPENVPN_SSL_NCP_H */
tests/unit_tests/openvpn/test_ncp.c +36 -8
74 lines not on this page · raw message
Signature · 1 line
2.25.0
Tested with MSVC, checked that if --ncp-ciphers options doesn't include AES-256-GCM,
IV_NCP=2 is indeed not pushed.
PATCH 6/6 Use gc_arena in ncp_get_best_cipher
In reply to Arne SchwabeThis avoids using the session specific gc arena to hold the temporary string returned by tls_peer_ncp_list for the whole session.
--- src/openvpn/ssl_ncp.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
src/openvpn/ssl_ncp.c +6 -4
@@ -216,10 +216,12 @@ ncp_get_best_cipher(const char *server_list, const char *server_cipher, const char *peer_info, const char *remote_cipher, struct gc_arena *gc) { - const char *peer_ncp_list = tls_peer_ncp_list(peer_info, gc); -char *tmp_ciphers =string_alloc (server_list,NULL ); -char *tmp_ciphers_orig = tmp_ciphers; + struct gc_arena gc_tmp = gc_new(); + +const char *peer_ncp_list =tls_peer_ncp_list (peer_info, &gc_tmp); + +char *tmp_ciphers =string_alloc (server_list, &gc_tmp);const char *token =strsep (&tmp_ciphers,":" );while (token)
@@ -247,7 +249,7 @@ ncp_get_best_cipher(const char *server_list, const char *server_cipher, ret =string_alloc (token, gc); } -free (tmp_ciphers_orig); +gc_free (&gc_tmp);return ret; }
Signature · 1 line
2.25.0