From patchwork Sun Nov 17 07:12:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 925 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director9.mail.ord1d.rsapps.net ([172.31.255.6]) by backend30.mail.ord1d.rsapps.net with LMTP id 8IdnJfyN0V3JXAAAIUCqbw for ; Sun, 17 Nov 2019 13:14:20 -0500 Received: from proxy20.mail.iad3b.rsapps.net ([172.31.255.6]) by director9.mail.ord1d.rsapps.net with LMTP id OAfLIvyN0V0bZgAAalYnBA ; Sun, 17 Nov 2019 13:14:20 -0500 Received: from smtp35.gate.iad3b ([172.31.255.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy20.mail.iad3b.rsapps.net with LMTP id OLGpHPyN0V3uYAAAcDxLoQ ; Sun, 17 Nov 2019 13:14:20 -0500 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp35.gate.iad3b.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=none (p=nil; dis=none) header.from=rfc2549.org X-Suspicious-Flag: YES X-Classification-ID: 12ca527a-0966-11ea-a5af-525400503131-1-1 Received: from [216.105.38.7] ([216.105.38.7:51916] helo=lists.sourceforge.net) by smtp35.gate.iad3b.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 6A/BC-16448-CFD81DD5; Sun, 17 Nov 2019 13:14:20 -0500 Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1iWP2w-0005SS-Fe; Sun, 17 Nov 2019 18:13:14 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1iWP2n-0005Qn-JJ for openvpn-devel@lists.sourceforge.net; Sun, 17 Nov 2019 18:13:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=References:In-Reply-To:Message-Id:Date:Subject:To: From:Sender:Reply-To:Cc:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=0g4xcHE2rKvSSWRRZPmXJQr3xSXRlboObVD4RZ+kR7c=; b=X2xrg8QVqsrpPTgKjyQyhHnXyE vyF5ioVMR1BrfoM88BNwW29L7skChbucvv07E8zxFhG8OewGSy+PAYgSqwnhE+m/5I4x6oGI+ax2B sNQ33hOhiJC8OZ7HutgD+O7CE3awglj1NBxvTYtIdldBHThG3pT1/IH6/C6rvS8NjZ4o=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=References:In-Reply-To:Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc :MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=0g4xcHE2rKvSSWRRZPmXJQr3xSXRlboObVD4RZ+kR7c=; b=Qaxox92oP8IcsNM753Aa6O8My7 JegjWs8P69AsliP2qEFOoe/GOnRmXh2vVkAs4o/mK6TdAD8BAL0jpn/14PWIUIXH/F0gd0YsLdFt/ 5CUMeSbOgmGWll8SwPj4E3Wp/sz+/hAxcPU1I1WS5esl1Ovx2GvvEtIVOQSh8eYcs61Q=; Received: from [192.26.174.232] (helo=mail.blinkt.de) by sfi-mx-3.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1iWP2j-003csv-3H for openvpn-devel@lists.sourceforge.net; Sun, 17 Nov 2019 18:13:05 +0000 Received: from kamera.blinkt.de ([2001:638:502:390:20c:29ff:fec8:535c]) by mail.blinkt.de with smtp (Exim 4.92.3 (FreeBSD)) (envelope-from ) id 1iWP2S-000Aet-0y for openvpn-devel@lists.sourceforge.net; Sun, 17 Nov 2019 19:12:44 +0100 Received: (nullmailer pid 28967 invoked by uid 10006); Sun, 17 Nov 2019 18:12:43 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Sun, 17 Nov 2019 19:12:41 +0100 Message-Id: <20191117181243.28919-2-arne@rfc2549.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191117181243.28919-1-arne@rfc2549.org> References: <20191117181243.28919-1-arne@rfc2549.org> X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 URIBL_BLOCKED ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [URIs: rfc2549.org] 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 1.0 RDNS_NONE Delivered to internal network by a host with no rDNS -0.1 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1iWP2j-003csv-3H Subject: [Openvpn-devel] [PATCH 2/4] Implement dynamic NCP negotiation X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox 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. Signed-off-by: Arne Schwabe --- doc/openvpn.8 | 10 +++- src/openvpn/init.c | 1 - src/openvpn/options.c | 1 + src/openvpn/push.c | 32 +++++++---- src/openvpn/ssl.c | 111 +++++++++++++++++++++++++++++++++++++-- src/openvpn/ssl.h | 34 ++++++++++++ src/openvpn/ssl_common.h | 1 - 7 files changed, 172 insertions(+), 18 deletions(-) diff --git a/doc/openvpn.8 b/doc/openvpn.8 index ae24b6c0..4f0df786 100644 --- a/doc/openvpn.8 +++ b/doc/openvpn.8 @@ -3111,6 +3111,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= \-\- the client pushes the list of configured +ciphers with the +\.B -\-\ncp\-ciphers +option to the server. + IV_GUI_VER= \-\- the UI version of a UI if one is running, for example "de.blinkt.openvpn 0.5.47" for the Android app. @@ -4368,7 +4373,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 @@ -4392,7 +4398,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 diff --git a/src/openvpn/init.c b/src/openvpn/init.c index 8f142311..5058b300 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -2828,7 +2828,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 = c->c1.ncp_ciphers; to.ncp_enabled = options->ncp_enabled; to.transition_window = options->transition_window; diff --git a/src/openvpn/options.c b/src/openvpn/options.c index c282b582..9be5a826 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -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" diff --git a/src/openvpn/push.c b/src/openvpn/push.c index 368b6920..1f1e1f0d 100644 --- a/src/openvpn/push.c +++ b/src/openvpn/push.c @@ -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,31 @@ 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 + { + char *peer_ciphers = tls_peer_ncp_list(peer_info); + 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); + free(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; } diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index 6ca5c79a..61b29330 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -1934,6 +1934,81 @@ tls_item_in_cipher_list(const char *item, const char *list) return token != NULL; } +char * +tls_peer_ncp_list(const char *peer_info) +{ + /* Check if the peer sends the IV_CIPHERS list */ + const char *ncp_ciphers_start; + if (peer_info && (ncp_ciphers_start = strstr(peer_info, "IV_CIPHERS="))) + { + ncp_ciphers_start += strlen("IV_CIPHERS="); + const char *ncp_ciphers_end = strstr(ncp_ciphers_start, "\n"); + if (!ncp_ciphers_end) + { + /* IV_CIPHERS is at end of the peer_info list and no '\n' + * follows */ + ncp_ciphers_end = ncp_ciphers_start + strlen(ncp_ciphers_start); + } + + char *ncp_ciphers_peer = string_alloc(ncp_ciphers_start, NULL); + /* NULL terminate the copy at the right position */ + ncp_ciphers_peer[ncp_ciphers_end - ncp_ciphers_start] = '\0'; + return ncp_ciphers_peer; + + } + else if (tls_peer_info_ncp_ver(peer_info)>=2) + { + /* If the peer announces IV_NCP=2 then it supports the AES GCM + * ciphers */ + return strdup("AES-256-GCM:AES-128-GCM"); + } + else + { + return strdup(""); + } +} + +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) +{ + char *peer_ncp_list = tls_peer_ncp_list(peer_info); + + char *tmp_ciphers = string_alloc(server_list, NULL); + char *tmp_ciphers_orig = tmp_ciphers; + + const char *token = strsep(&tmp_ciphers, ":"); + while (token) + { + if (tls_item_in_cipher_list(token, peer_ncp_list) + || streq(token, remote_cipher)) + { + break; + } + token = strsep(&tmp_ciphers, ":"); + } + /* We have not found a common cipher, as a last resort check if the + * server cipher can be used + */ + if (token == NULL + && (tls_item_in_cipher_list(server_cipher, peer_ncp_list) + || streq(server_cipher, remote_cipher))) + { + token = server_cipher; + } + + char *ret = NULL; + if (token != NULL) + { + ret = string_alloc(token, gc); + } + + free(tmp_ciphers_orig); + free(peer_ncp_list); + return ret; +} + void tls_poor_mans_ncp(struct options *o, const char *remote_ciphername) { @@ -2322,11 +2397,15 @@ 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) - && 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)) + && (session->opt->mode == MODE_SERVER || session->opt->pull)) { - buf_printf(&out, "IV_NCP=2\n"); + if (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"); + } + buf_printf(&out, "IV_CIPHERS=%s\n", session->opt->config_ncp_ciphers); } /* push compression status */ @@ -2561,6 +2640,28 @@ error: return false; } +/** + * Returns whether the client supports NCP either by + * announcing IV_NCP>=2 or the IV_CIPHERS list + */ +static bool +tls_peer_supports_ncp(const char *peer_info) +{ + if (!peer_info) + { + return false; + } + else if (tls_peer_info_ncp_ver(peer_info) >= 2 + || strstr(peer_info, "IV_CIPHERS=")) + { + return true; + } + else + { + return false; + } +} + static bool key_method_2_read(struct buffer *buf, struct tls_multi *multi, struct tls_session *session) { @@ -2633,7 +2734,7 @@ key_method_2_read(struct buffer *buf, struct tls_multi *multi, struct tls_sessio multi->remote_ciphername = options_string_extract_option(options, "cipher", NULL); - if (tls_peer_info_ncp_ver(multi->peer_info) < 2) + if (!tls_peer_supports_ncp(multi->peer_info)) { /* Peer does not support NCP, but leave NCP enabled if the local and * remote cipher do not match to attempt 'poor-man's NCP'. diff --git a/src/openvpn/ssl.h b/src/openvpn/ssl.h index a944ca3a..ad3b46d7 100644 --- a/src/openvpn/ssl.h +++ b/src/openvpn/ssl.h @@ -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 needs to be + * free()ed after use. If no information is available an empty string + * ("") is returned. + */ +char * +tls_peer_ncp_list(const char *peer_info); + /** * Check whether the ciphers in the supplied list are supported. * diff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h index fb82f610..998ea3c4 100644 --- a/src/openvpn/ssl_common.h +++ b/src/openvpn/ssl_common.h @@ -289,7 +289,6 @@ struct tls_options bool tcp_mode; const char *config_ciphername; - const char *config_authname; const char *config_ncp_ciphers; bool ncp_enabled;