From patchwork Thu Apr 22 05:17:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 1765 X-Patchwork-Delegate: a@unstable.cc Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director10.mail.ord1d.rsapps.net ([172.27.255.53]) by backend30.mail.ord1d.rsapps.net with LMTP id sEObDNCTgWBLJgAAIUCqbw (envelope-from ) for ; Thu, 22 Apr 2021 11:18:40 -0400 Received: from proxy8.mail.iad3a.rsapps.net ([172.27.255.53]) by director10.mail.ord1d.rsapps.net with LMTP id WI91DNCTgWBqfQAApN4f7A (envelope-from ) for ; Thu, 22 Apr 2021 11:18:40 -0400 Received: from smtp36.gate.iad3a ([172.27.255.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy8.mail.iad3a.rsapps.net with LMTPS id EBzJBdCTgWDKKQAAsBr/qg (envelope-from ) for ; Thu, 22 Apr 2021 11:18:40 -0400 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: smtp36.gate.iad3a.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: 03823aa4-a37e-11eb-9dc4-525400575b2b-1-1 Received: from [216.105.38.7] ([216.105.38.7:40626] helo=lists.sourceforge.net) by smtp36.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 8B/DD-10582-FC391806; Thu, 22 Apr 2021 11:18:39 -0400 Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1lZb5J-0005cw-HJ; Thu, 22 Apr 2021 15:17:41 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lZb5I-0005cH-4i for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 15:17:40 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: 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=9oW2mACl9EvPvpnYM0ZSCqfKZySeBRBwdcg7PagDdQ4=; b=ln/rBQbH/7n2Njy+g5+JG/TXEf 8jrK97pavwpPrk3GF9jtgKFOA3UGj5qy855WV60DePVLINrhQbyWdc9QVKAEX1CWAKOPbHLNcn++0 l5Ue1KwA/8btpnHSg+toDmZH8fDCJzo8B0HCuF3qJWlfam/qwzX55+T/1JbFBYPD4PEs=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: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=9oW2mACl9EvPvpnYM0ZSCqfKZySeBRBwdcg7PagDdQ4=; b=O4O/0glyeraQDAJPYABNMcq2Zq Fk2/IOyzqPwjNkV4FuQHzZydL4lUnjfuGMqNg0dQiECYgVURdvImcsR+Mim/QBzNa4CfVZNWLkeHr YDsGH1YEKVjHobSoobdY92WK51oePulUTvB37Gscv44Pv4DZvUXk1gKf1YZ3+i+fUH3w=; Received: from mail.blinkt.de ([192.26.174.232]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.3) id 1lZb5E-0007cV-AH for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 15:17:40 +0000 Received: from kamera.blinkt.de ([2001:638:502:390:20c:29ff:fec8:535c]) by mail.blinkt.de with smtp (Exim 4.94 (FreeBSD)) (envelope-from ) id 1lZb52-000B5b-J5 for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 17:17:24 +0200 Received: (nullmailer pid 2132634 invoked by uid 10006); Thu, 22 Apr 2021 15:17:25 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Thu, 22 Apr 2021 17:17:23 +0200 Message-Id: <20210422151724.2132573-6-arne@rfc2549.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210422151724.2132573-1-arne@rfc2549.org> References: <20210422151724.2132573-1-arne@rfc2549.org> MIME-Version: 1.0 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 X-Headers-End: 1lZb5E-0007cV-AH Subject: [Openvpn-devel] [PATCH 6/7] Introduce S_GENERATED_KEYS state and generate keys only when authenticated 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: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox Since generating data channel does not happen when we have reach the S_ACTIVE/S_GOT_KEY state anymore like it used to be before NCP, the state that data channel keys deserves its own state in the state machine. The changes done by this commit are rather intrusive since they move the key generation to a completely different place and also rely on the state machine to decide if keys should be generated rather than on the complicated conditions that were implemented in the key_method_2_write/read methods. A (intended) side effect of this change is that sessions that are still in deferred state (ks->authenticated == KS_DEFERRED) will not have data channel keys generated. This avoids corner cases where not fully authenticated sessions might leak data. Signed-off-by: Arne Schwabe --- src/openvpn/forward.h | 2 +- src/openvpn/init.c | 1 + src/openvpn/ssl.c | 88 +++++++++++++++++----------------------- src/openvpn/ssl.h | 10 +++++ src/openvpn/ssl_common.h | 9 +++- 5 files changed, 57 insertions(+), 53 deletions(-) diff --git a/src/openvpn/forward.h b/src/openvpn/forward.h index 3461e6422..b8760099e 100644 --- a/src/openvpn/forward.h +++ b/src/openvpn/forward.h @@ -450,7 +450,7 @@ connection_established(struct context *c) { if (c->c2.tls_multi) { - return c->c2.tls_multi->multi_state >= CAS_CONNECT_DONE; + return c->c2.tls_multi->multi_state >= CAS_WAITING_OPTIONS_IMPORT; } else { diff --git a/src/openvpn/init.c b/src/openvpn/init.c index 8a5c7bc55..d7e71a497 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -2202,6 +2202,7 @@ do_up(struct context *c, bool pulled_options, unsigned int option_types_found) } c->c2.do_up_ran = true; + c->c2.tls_multi->multi_state = CAS_CONNECT_DONE; } return true; } diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index 4bb395039..a7e0c9148 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -788,6 +788,9 @@ state_name(int state) case S_ERROR: return "S_ERROR"; + case S_GENERATED_KEYS: + return "S_GENERATED_KEYS"; + default: return "S_???"; } @@ -1840,13 +1843,13 @@ key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len) * This erases the source material used to generate the data channel keys, and * can thus be called only once per session. */ -static bool +bool tls_session_generate_data_channel_keys(struct tls_session *session) { bool ret = false; struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */ - if (ks->authenticated == KS_AUTH_FALSE) + if (ks->authenticated <= KS_AUTH_FALSE) { msg(D_TLS_ERRORS, "TLS Error: key_state not authenticated"); goto cleanup; @@ -1862,6 +1865,9 @@ tls_session_generate_data_channel_keys(struct tls_session *session) tls_limit_reneg_bytes(session->opt->key_type.cipher, &session->opt->renegotiate_bytes); + /* set the state of the keys for the session to generated */ + ks->state = S_GENERATED_KEYS; + ret = true; cleanup: secure_memzero(ks->key_src, sizeof(*ks->key_src)); @@ -2375,30 +2381,6 @@ key_method_2_write(struct buffer *buf, struct tls_multi *multi, struct tls_sessi goto error; } - /* - * 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_CONNECT_DONE status is identical to - * NCP options are processed and do not wait for NCP being finished. - */ - if (ks->authenticated > KS_AUTH_FALSE && session->opt->mode == MODE_SERVER - && (multi->multi_state >= CAS_CONNECT_DONE || ks->key_id > 0)) - { - /* if key_id >= 1, is a renegotiation, so we use the already established - * parameters and do not need to delay anything. */ - - /* key-id == 0 and multi_state >= CAS_CONNECT_DONE is a special case of - * the server reusing the session of a reconnecting client. */ - if (!tls_session_generate_data_channel_keys(session)) - { - msg(D_TLS_ERRORS, "TLS Error: server generate_key_expansion failed"); - goto error; - } - } - return true; error: @@ -2598,21 +2580,7 @@ key_method_2_read(struct buffer *buf, struct tls_multi *multi, struct tls_sessio setenv_del(session->opt->es, "exported_keying_material"); } - - /* - * Generate tunnel keys if we're a client. - * If --pull is enabled, the first key generation is postponed until after the - * pull/push, so we can process pushed cipher directives. - */ - if (!session->opt->server && (!session->opt->pull || ks->key_id > 0)) - { - if (!tls_session_generate_data_channel_keys(session)) - { - msg(D_TLS_ERRORS, "TLS Error: client generate_key_expansion failed"); - goto error; - } - } - + gc_free(&gc); return true; @@ -2814,7 +2782,7 @@ tls_process(struct tls_multi *multi, else { /* Skip the connect script related states */ - multi->multi_state = CAS_CONNECT_DONE; + multi->multi_state = CAS_WAITING_OPTIONS_IMPORT; } } @@ -3137,6 +3105,27 @@ tls_multi_process(struct tls_multi *multi, /* If we have successfully authenticated and are still waiting for the authentication to finish * move the state machine for the multi context forward */ + + if (multi->multi_state >= CAS_CONNECT_DONE) + { + for (int i = 0; i < TM_SIZE; ++i) + { + struct tls_session *session = &multi->session[i]; + struct key_state *ks = &session->key[KS_PRIMARY]; + + if (ks->state == S_ACTIVE && ks->authenticated == KS_AUTH_TRUE) + { + /* This will ks->state from S_ACTIVE to S_GENERATED_KEYS */ + if (!tls_session_generate_data_channel_keys(session)) + { + msg(D_TLS_ERRORS, "TLS Error: generate_key_expansion failed"); + ks->authenticated = KS_AUTH_FALSE; + ks->state = S_ERROR; + } + } + } + } + if (multi->multi_state == CAS_WAITING_AUTH && tas == TLS_AUTHENTICATION_SUCCEEDED) { multi->multi_state = CAS_PENDING; @@ -3245,11 +3234,10 @@ handle_data_channel_packet(struct tls_multi *multi, * passive side is the server which only listens for the connections, the * active side is the client which initiates connections). */ - if (TLS_AUTHENTICATED(multi, ks) - && key_id == ks->key_id - && (ks->authenticated == KS_AUTH_TRUE) + if (ks->state >= S_GENERATED_KEYS && key_id == ks->key_id && (floated || link_socket_actual_match(from, &ks->remote_addr))) { + ASSERT(ks->authenticated == KS_AUTH_TRUE); if (!ks->crypto_options.key_ctx_bi.initialized) { msg(D_MULTI_DROPPED, @@ -3571,8 +3559,7 @@ tls_pre_decrypt(struct tls_multi *multi, /* * Remote is requesting a key renegotiation */ - if (op == P_CONTROL_SOFT_RESET_V1 - && TLS_AUTHENTICATED(multi, ks)) + if (op == P_CONTROL_SOFT_RESET_V1 && TLS_AUTHENTICATED(multi, ks)) { if (!read_control_auth(buf, &session->tls_wrap, from, session->opt)) @@ -3833,10 +3820,11 @@ struct key_state *tls_select_encryption_key(struct tls_multi *multi) for (int i = 0; i < KEY_SCAN_SIZE; ++i) { struct key_state *ks = get_key_scan(multi, i); - if (ks->state >= S_ACTIVE - && ks->authenticated == KS_AUTH_TRUE - && ks->crypto_options.key_ctx_bi.initialized) + if (ks->state >= S_GENERATED_KEYS) { + ASSERT(ks->authenticated == KS_AUTH_TRUE); + ASSERT(ks->crypto_options.key_ctx_bi.initialized); + if (!ks_select) { ks_select = ks; diff --git a/src/openvpn/ssl.h b/src/openvpn/ssl.h index 2791143f6..18a11bbdf 100644 --- a/src/openvpn/ssl.h +++ b/src/openvpn/ssl.h @@ -615,4 +615,14 @@ show_available_tls_ciphers(const char *cipher_list, const char *cipher_list_tls13, const char *tls_cert_profile); + +/** + * Generate data channel keys for the supplied TLS session. + * + * This erases the source material used to generate the data channel keys, and + * can thus be called only once per session. + */ +bool +tls_session_generate_data_channel_keys(struct tls_session *session); + #endif /* ifndef OPENVPN_SSL_H */ diff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h index 01f00950e..89312cc88 100644 --- a/src/openvpn/ssl_common.h +++ b/src/openvpn/ssl_common.h @@ -64,7 +64,8 @@ * material. * -# \c S_GOT_KEY, have received remote part of \c key_source2 random * material. - * -# \c S_ACTIVE, normal operation + * -# \c S_ACTIVE, control channel successfully established + * -# \c S_GENERATED_KEYS, the * * Servers follow the same order, except for \c S_SENT_KEY and \c * S_GOT_KEY being reversed, because the server first receives the @@ -92,7 +93,10 @@ #define S_ACTIVE 6 /**< Operational \c key_state state * immediately after negotiation has * completed while still within the - * handshake window. */ + * handshake window, deferred auth, client + * connect and can still + * be pending. */ +#define S_GENERATED_KEYS 7 /**< The data channel keys have been generated */ /* Note that earlier versions also had a S_OP_NORMAL state that was * virtually identical with S_ACTIVE and the code still assumes everything * >= S_ACTIVE to be fully operational */ @@ -517,6 +521,7 @@ enum multi_status { CAS_PENDING_DEFERRED, CAS_PENDING_DEFERRED_PARTIAL, /**< at least handler succeeded, no result yet*/ CAS_FAILED, + CAS_WAITING_OPTIONS_IMPORT, /**< client with pull or p2p waiting for first time options import */ CAS_CONNECT_DONE, };