From patchwork Thu Apr 22 05:17:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 1763 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director14.mail.ord1d.rsapps.net ([172.27.255.58]) by backend30.mail.ord1d.rsapps.net with LMTP id OLltCseTgWBKKwAAIUCqbw (envelope-from ) for ; Thu, 22 Apr 2021 11:18:31 -0400 Received: from proxy3.mail.iad3a.rsapps.net ([172.27.255.58]) by director14.mail.ord1d.rsapps.net with LMTP id uLg5CseTgWBKcQAAeJ7fFg (envelope-from ) for ; Thu, 22 Apr 2021 11:18:31 -0400 Received: from smtp34.gate.iad3a ([172.27.255.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy3.mail.iad3a.rsapps.net with LMTPS id ODj3AceTgWCXMQAAYaqY3Q (envelope-from ) for ; Thu, 22 Apr 2021 11:18:31 -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: smtp34.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: fd262eea-a37d-11eb-95ec-525400865cc7-1-1 Received: from [216.105.38.7] ([216.105.38.7:40270] helo=lists.sourceforge.net) by smtp34.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 18/58-25307-4C391806; Thu, 22 Apr 2021 11:18:29 -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-0005ck-A1; 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-0005cG-4E 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: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:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=S0IX+At3kQP2xPhFkNwfixyNITDcsgwJsG7hKyXaaR4=; b=GkGiihxqjFP4VIJ1ZjRozWn2cC r5hJoMiDvWtlj7bslTZ7l3hurCFJJHUyLmtpGjJZvtaYHagS8WTsJccWm/XYbkBdXYnW2mYCu/Apo ozmvsi9FMJ1jX2/a1UXPM9089/Y/Lc1aZ3V7QA9coMPE18DPipyrZnB9rlI76a/6JhcE=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version: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:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post: List-Owner:List-Archive; bh=S0IX+At3kQP2xPhFkNwfixyNITDcsgwJsG7hKyXaaR4=; b=I zeS/8p/bgw9f3arZLIt87cmgVemVnzxhlwiAc+CK2FhQ3L+h1ac1OzhqrU31n6n54ZN3OY7JB0uTM 0ZQWPt/eBiwaQEDuxORASnPkVaiEeOi0Rmz1IHyrjuC0rAxSInAY8p/7YfmMoHVltztFzxkl2NUnM lvJYUgBbYeg2ZZjI=; Received: from mail.blinkt.de ([192.26.174.232]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1lZb5E-00DGRB-Fq 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-000B5M-6w for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 17:17:24 +0200 Received: (nullmailer pid 2132619 invoked by uid 10006); Thu, 22 Apr 2021 15:17:24 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Thu, 22 Apr 2021 17:17:18 +0200 Message-Id: <20210422151724.2132573-1-arne@rfc2549.org> X-Mailer: git-send-email 2.25.1 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-00DGRB-Fq Subject: [Openvpn-devel] [PATCH 1/7] Move tls_select_primary_key into its own function 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 tls_pre_encrypt mainly performs the task of selecting the primary encryption key but also performs other minor tasks. To allow only querying for the key that should be used for encryption extract this part of the function into its own function. Signed-off-by: Arne Schwabe Acked-by: Antonio Quartulli --- src/openvpn/ssl.c | 36 +++++++++++++++++++++--------------- src/openvpn/ssl.h | 10 ++++++++++ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index 3921b3ba9..3bc84e02c 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -3820,27 +3820,15 @@ error: return false; } -/* Choose the key with which to encrypt a data packet */ -void -tls_pre_encrypt(struct tls_multi *multi, - struct buffer *buf, struct crypto_options **opt) +struct key_state *tls_select_encryption_key(struct tls_multi *multi) { - multi->save_ks = NULL; - if (buf->len <= 0) - { - buf->len = 0; - *opt = NULL; - return; - } - struct key_state *ks_select = NULL; 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 - ) + && ks->authenticated == KS_AUTH_TRUE + && ks->crypto_options.key_ctx_bi.initialized) { if (!ks_select) { @@ -3853,6 +3841,24 @@ tls_pre_encrypt(struct tls_multi *multi, } } } + return ks_select; +} + + +/* Choose the key with which to encrypt a data packet */ +void +tls_pre_encrypt(struct tls_multi *multi, + struct buffer *buf, struct crypto_options **opt) +{ + multi->save_ks = NULL; + if (buf->len <= 0) + { + buf->len = 0; + *opt = NULL; + return; + } + + struct key_state *ks_select = tls_select_encryption_key(multi); if (ks_select) { diff --git a/src/openvpn/ssl.h b/src/openvpn/ssl.h index 6369e8bf6..135c60732 100644 --- a/src/openvpn/ssl.h +++ b/src/openvpn/ssl.h @@ -372,6 +372,16 @@ bool tls_pre_decrypt_lite(const struct tls_auth_standalone *tas, void tls_pre_encrypt(struct tls_multi *multi, struct buffer *buf, struct crypto_options **opt); +/** + * Selects the primary encryption that should be used to encrypt data of an + * outgoing packet. + * @ingroup data_crypto + * + * If no key is found NULL is returned instead. + * + * @param multi - The TLS state for this packet's destination VPN tunnel. + */ +struct key_state *tls_select_encryption_key(struct tls_multi *multi); /** * Prepend a one-byte OpenVPN data channel P_DATA_V1 opcode to the packet. From patchwork Thu Apr 22 05:17:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 1764 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director8.mail.ord1d.rsapps.net ([172.27.255.9]) by backend30.mail.ord1d.rsapps.net with LMTP id yE9DNs+TgWDCEwAAIUCqbw (envelope-from ) for ; Thu, 22 Apr 2021 11:18:39 -0400 Received: from proxy10.mail.iad3a.rsapps.net ([172.27.255.9]) by director8.mail.ord1d.rsapps.net with LMTP id KK4HNs+TgWApSQAAfY0hYg (envelope-from ) for ; Thu, 22 Apr 2021 11:18:39 -0400 Received: from smtp10.gate.iad3a ([172.27.255.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy10.mail.iad3a.rsapps.net with LMTPS id 2NbmLc+TgWBiLQAAnQ/bqA (envelope-from ) for ; Thu, 22 Apr 2021 11:18:39 -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: smtp10.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: 0386ddac-a37e-11eb-a6ce-525400a8203f-1-1 Received: from [216.105.38.7] ([216.105.38.7:47128] helo=lists.sourceforge.net) by smtp10.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 32/70-01866-FC391806; Thu, 22 Apr 2021 11:18:39 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.92.3) (envelope-from ) id 1lZb5O-0005v4-1y; Thu, 22 Apr 2021 15:17:46 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.3) (envelope-from ) id 1lZb5L-0005uW-58 for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 15:17:43 +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=pALeFcfN+fgedo50ZRYgwXFYvUew+dmQCLZieEa6RIY=; b=ZVgcjNr5HIbrGffsTg7uxsp7Wv xYWRx3OOF9aDzZiYk8pC1xPKQh/b8iXJlymHB/vYKLn6kCBuZRTOGIumhoVyvCIQU35Ka/iAmnWD3 zZ2qGWhs5YS0EeKN1p/Y2/uRVmDrvrT0iQMi4lGoL47hacYmLOk+LFb2PiUdfW94gaII=; 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=pALeFcfN+fgedo50ZRYgwXFYvUew+dmQCLZieEa6RIY=; b=OMoRa+CTlLyWABBUCC43oZ3idz xHAogZ0pdJ8zMGjw5aRAUfT1pXQniyULs8typTH+TH2NGc6mEe2ukXjpn+fLF2Z7cTEeLbgp3foiL XPM84QktKB7BPAQ6K6U+hCvKjGf0KMG7HNp7CziuuxgVv1GYu1rQYvW2MJ0nG/xCUP4U=; 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-0007cS-Eu for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 15:17:43 +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-000B5O-9G for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 17:17:24 +0200 Received: (nullmailer pid 2132622 invoked by uid 10006); Thu, 22 Apr 2021 15:17:24 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Thu, 22 Apr 2021 17:17:19 +0200 Message-Id: <20210422151724.2132573-2-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-0007cS-Eu Subject: [Openvpn-devel] [PATCH 2/7] Use functions to access key_state instead direct member access 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 This uses get_key_scan and get_primary key instead the directly accessing the members of the struct to improve readiability of the code. Signed-off-by: Arne Schwabe Acked-by: Antonio Quartulli --- src/openvpn/multi.c | 3 +-- src/openvpn/push.c | 9 ++++----- src/openvpn/ssl.c | 11 +++-------- src/openvpn/ssl.h | 2 +- src/openvpn/ssl_common.h | 9 +++++++++ 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index d51316de2..666456da9 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -1800,8 +1800,7 @@ multi_client_set_protocol_options(struct context *c) * cipher -> so log the fact and push the "what we have now" cipher * (so the client is always told what we expect it to use) */ - const struct tls_session *session = &tls_multi->session[TM_ACTIVE]; - if (session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized) + if (get_primary_key(tls_multi)->crypto_options.key_ctx_bi.initialized) { msg(M_INFO, "PUSH: client wants to negotiate cipher (NCP), but " "server has already generated data channel keys, " diff --git a/src/openvpn/push.c b/src/openvpn/push.c index bba555fa1..fcafc5003 100644 --- a/src/openvpn/push.c +++ b/src/openvpn/push.c @@ -222,7 +222,7 @@ 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 = session->key[KS_PRIMARY].key_id; + int key_id = get_primary_key(c->c2.tls_multi)->key_id; management_notify_client_cr_response(key_id, mda, es, m); @@ -304,7 +304,7 @@ receive_auth_pending(struct context *c, const struct buffer *buffer) "to %us", c->options.handshake_window, min_uint(max_timeout, server_timeout)); - struct key_state *ks = &c->c2.tls_multi->session[TM_ACTIVE].key[KS_PRIMARY]; + const struct key_state *ks = get_primary_key(c->c2.tls_multi); c->c2.push_request_timeout = ks->established + min_uint(max_timeout, server_timeout); } @@ -369,7 +369,7 @@ bool send_auth_pending_messages(struct tls_multi *tls_multi, const char *extra, unsigned int timeout) { - struct key_state *ks = &tls_multi->session[TM_ACTIVE].key[KS_PRIMARY]; + struct key_state *ks = get_key_scan(tls_multi, 0); static const char info_pre[] = "INFO_PRE,"; @@ -476,8 +476,7 @@ cleanup: bool send_push_request(struct context *c) { - struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE]; - struct key_state *ks = &session->key[KS_PRIMARY]; + const struct key_state *ks = get_primary_key(c->c2.tls_multi); /* We timeout here under two conditions: * a) we reached the hard limit of push_request_timeout diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index 3bc84e02c..7d66cf565 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -3448,7 +3448,7 @@ tls_pre_decrypt(struct tls_multi *multi, if (i == TM_SIZE && is_hard_reset_method2(op)) { struct tls_session *session = &multi->session[TM_ACTIVE]; - struct key_state *ks = &session->key[KS_PRIMARY]; + const struct key_state *ks = get_primary_key(multi); /* * If we have no session currently in progress, the initial packet will @@ -3933,7 +3933,6 @@ tls_send_payload(struct tls_multi *multi, const uint8_t *data, int size) { - struct tls_session *session; struct key_state *ks; bool ret = false; @@ -3941,8 +3940,7 @@ tls_send_payload(struct tls_multi *multi, ASSERT(multi); - session = &multi->session[TM_ACTIVE]; - ks = &session->key[KS_PRIMARY]; + ks = get_key_scan(multi, 0); if (ks->state >= S_ACTIVE) { @@ -3971,16 +3969,13 @@ bool tls_rec_payload(struct tls_multi *multi, struct buffer *buf) { - struct tls_session *session; - struct key_state *ks; bool ret = false; tls_clear_error(); ASSERT(multi); - session = &multi->session[TM_ACTIVE]; - ks = &session->key[KS_PRIMARY]; + struct key_state *ks = get_key_scan(multi, 0); if (ks->state >= S_ACTIVE && BLEN(&ks->plaintext_read_buf)) { diff --git a/src/openvpn/ssl.h b/src/openvpn/ssl.h index 135c60732..2791143f6 100644 --- a/src/openvpn/ssl.h +++ b/src/openvpn/ssl.h @@ -547,7 +547,7 @@ tls_test_payload_len(const struct tls_multi *multi) { if (multi) { - const struct key_state *ks = &multi->session[TM_ACTIVE].key[KS_PRIMARY]; + const struct key_state *ks = get_primary_key(multi); if (ks->state >= S_ACTIVE) { return BLEN(&ks->plaintext_read_buf); diff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h index 514cdd964..9c923f2a6 100644 --- a/src/openvpn/ssl_common.h +++ b/src/openvpn/ssl_common.h @@ -631,4 +631,13 @@ get_key_scan(struct tls_multi *multi, int index) } } +/** gets an item of \c key_state objects in the + * order they should be scanned by data + * channel modules. */ +static inline const struct key_state * +get_primary_key(const struct tls_multi *multi) +{ + return &multi->session[TM_ACTIVE].key[KS_PRIMARY]; +} + #endif /* SSL_COMMON_H_ */ From patchwork Thu Apr 22 05:17:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 1762 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director15.mail.ord1d.rsapps.net ([172.27.255.7]) by backend30.mail.ord1d.rsapps.net with LMTP id gN1rCcWTgWDCEwAAIUCqbw (envelope-from ) for ; Thu, 22 Apr 2021 11:18:29 -0400 Received: from proxy4.mail.iad3a.rsapps.net ([172.27.255.7]) by director15.mail.ord1d.rsapps.net with LMTP id UE0xCcWTgWCrTAAAIcMcQg (envelope-from ) for ; Thu, 22 Apr 2021 11:18:29 -0400 Received: from smtp22.gate.iad3a ([172.27.255.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy4.mail.iad3a.rsapps.net with LMTPS id sA3sAMWTgWAjDwAA8Zvu4w (envelope-from ) for ; Thu, 22 Apr 2021 11:18:29 -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: smtp22.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: fc997e1e-a37d-11eb-9fae-5254005ae9fe-1-1 Received: from [216.105.38.7] ([216.105.38.7:47046] helo=lists.sourceforge.net) by smtp22.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 9A/7F-29978-3C391806; Thu, 22 Apr 2021 11:18:27 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.92.3) (envelope-from ) id 1lZb5N-0005uo-EF; Thu, 22 Apr 2021 15:17:45 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.3) (envelope-from ) id 1lZb5L-0005uV-3X for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 15:17:43 +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=PeToVXJ01U2XUsG8TWsw/ODk68HnEJptBfgwvDIWDPg=; b=AuosMHhmX8To8OOCWu/8lNIUSk +swqTlPae2pOIj0TIl4Q2C34T8mgKp60ZokRzyIav+427OAUJCFJ7GTFX9foTL59Ff1pp/8zj0dhd /0CWJkIfcZdR0WZsjgnPYNORQtZY28hlMcrsSxWUFsTfy7Ukr+nwp7hdS0AWhAKVWIgQ=; 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=PeToVXJ01U2XUsG8TWsw/ODk68HnEJptBfgwvDIWDPg=; b=gMdCaXXiaGv6+phxVPgCCUyjTk 3FLOE1FkKyfnKup9veS/IbLqTerqmaXqQpviqFKkkZ3dLmJiDmkIUf4G82okPFouc3EwFcgnwqDjQ XK2VnCocxL0cqzEb5IcK6j2eHen/eooD8uhqNvAeUbGVHTVGeuQLJ/SmCpcB6tgYutKg=; Received: from mail.blinkt.de ([192.26.174.232]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1lZb5E-00DGRC-Fr for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 15:17:43 +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-000B5S-Bw for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 17:17:24 +0200 Received: (nullmailer pid 2132625 invoked by uid 10006); Thu, 22 Apr 2021 15:17:24 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Thu, 22 Apr 2021 17:17:20 +0200 Message-Id: <20210422151724.2132573-3-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-00DGRC-Fr Subject: [Openvpn-devel] [PATCH 3/7] Return cached result in tls_authentication_status 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 tls_authentication_status does caching to avoid file I/O more than every TLS_MULTI_AUTH_STATUS_INTERVAL (10s) per connection. But counter-intuitively it does not return the cached result but rather TLS_AUTHENTICATION_UNDEFINED if the cache is not refreshed by the call. This is workarounded by forcing a refresh in some areas of the code (latency = 0). This patch changes the behaviour by always returning the last known status and only updating the file status when the i/o timeout for the caches is reached. The patch also changes the DEFINE enum into a real enum. Signed-off-by: Arne Schwabe --- src/openvpn/multi.c | 2 +- src/openvpn/push.c | 3 ++- src/openvpn/ssl_common.h | 11 +++++++++- src/openvpn/ssl_verify.c | 46 ++++++++++++++++++++-------------------- src/openvpn/ssl_verify.h | 3 +-- 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 666456da9..ab2270a58 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -2596,7 +2596,7 @@ static const multi_client_connect_handler client_connect_handlers[] = { static void multi_connection_established(struct multi_context *m, struct multi_instance *mi) { - if (tls_authentication_status(mi->context.c2.tls_multi, 0) + if (tls_authentication_status(mi->context.c2.tls_multi, TLS_MULTI_AUTH_STATUS_INTERVAL) != TLS_AUTHENTICATION_SUCCEEDED) { return; diff --git a/src/openvpn/push.c b/src/openvpn/push.c index fcafc5003..428efb68e 100644 --- a/src/openvpn/push.c +++ b/src/openvpn/push.c @@ -855,7 +855,8 @@ process_incoming_push_request(struct context *c) { int ret = PUSH_MSG_ERROR; - if (tls_authentication_status(c->c2.tls_multi, 0) == TLS_AUTHENTICATION_FAILED + + if (tls_authentication_status(c->c2.tls_multi, TLS_MULTI_AUTH_STATUS_INTERVAL) == TLS_AUTHENTICATION_FAILED || c->c2.tls_multi->multi_state == CAS_FAILED) { const char *client_reason = tls_client_reason(c->c2.tls_multi); diff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h index 9c923f2a6..026da3578 100644 --- a/src/openvpn/ssl_common.h +++ b/src/openvpn/ssl_common.h @@ -152,6 +152,15 @@ struct auth_deferred_status unsigned int auth_control_status; }; +/* key_state_test_auth_control_file return values, these specify the + * current status of a deferred authentication */ +enum auth_deferred_result { + ACF_PENDING, /**< deferred auth still pending */ + ACF_SUCCEEDED, /**< deferred auth has suceeded */ + ACF_DISABLED, /**< deferred auth is not used */ + ACF_FAILED /**< deferred auth has failed */ +}; + /** * Security parameter state of one TLS and data channel %key session. * @ingroup control_processor @@ -219,7 +228,7 @@ struct key_state #ifdef ENABLE_MANAGEMENT unsigned int mda_key_id; - unsigned int mda_status; + enum auth_deferred_result mda_status; #endif time_t acf_last_mod; diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c index fffcd83c6..e000f75f7 100644 --- a/src/openvpn/ssl_verify.c +++ b/src/openvpn/ssl_verify.c @@ -845,13 +845,6 @@ cleanup: * user/password authentication. *************************************************************************** */ -/* key_state_test_auth_control_file return values, - * NOTE: acf_merge indexing depends on these values */ -#define ACF_UNDEFINED 0 -#define ACF_SUCCEEDED 1 -#define ACF_DISABLED 2 -#define ACF_FAILED 3 - void auth_set_client_reason(struct tls_multi *multi, const char *client_reason) { @@ -866,7 +859,7 @@ auth_set_client_reason(struct tls_multi *multi, const char *client_reason) #ifdef ENABLE_MANAGEMENT -static inline unsigned int +static inline enum auth_deferred_result man_def_auth_test(const struct key_state *ks) { if (management_enable_def_auth(management)) @@ -1041,13 +1034,23 @@ key_state_gen_auth_control_files(struct auth_deferred_status *ads, return (acf && apf); } -static unsigned int -key_state_test_auth_control_file(struct auth_deferred_status *ads) +/** + * Checks the control status from a file. The function will try to read + * and update the cached status if the status is still pending and the paramter + * cached is false. The function returns the + * + * + * @param ads deferred status control structure + * @param cached Return only cached status + * @return + */ +static enum auth_deferred_result +key_state_test_auth_control_file(struct auth_deferred_status *ads, bool cached) { if (ads->auth_control_file) { unsigned int ret = ads->auth_control_status; - if (ret == ACF_UNDEFINED) + if (ret == ACF_PENDING && !cached) { FILE *fp = fopen(ads->auth_control_file, "r"); if (fp) @@ -1084,10 +1087,7 @@ tls_authentication_status(struct tls_multi *multi, const int latency) /* at least one key already failed authentication */ bool failed_auth = false; - if (latency && multi->tas_last + latency >= now) - { - return TLS_AUTHENTICATION_UNDEFINED; - } + bool cached = multi->tas_last + latency >= now; multi->tas_last = now; for (int i = 0; i < KEY_SCAN_SIZE; ++i) @@ -1102,11 +1102,11 @@ tls_authentication_status(struct tls_multi *multi, const int latency) } else { - unsigned int auth_plugin = ACF_DISABLED; - unsigned int auth_script = ACF_DISABLED; - unsigned int auth_man = ACF_DISABLED; - auth_plugin = key_state_test_auth_control_file(&ks->plugin_auth); - auth_script = key_state_test_auth_control_file(&ks->script_auth); + enum auth_deferred_result auth_plugin = ACF_DISABLED; + enum auth_deferred_result auth_script = ACF_DISABLED; + enum auth_deferred_result auth_man = ACF_DISABLED; + auth_plugin = key_state_test_auth_control_file(&ks->plugin_auth, cached); + auth_script = key_state_test_auth_control_file(&ks->script_auth, cached); #ifdef ENABLE_MANAGEMENT auth_man = man_def_auth_test(ks); #endif @@ -1118,9 +1118,9 @@ tls_authentication_status(struct tls_multi *multi, const int latency) ks->authenticated = KS_AUTH_FALSE; failed_auth = true; } - else if (auth_plugin == ACF_UNDEFINED - || auth_script == ACF_UNDEFINED - || auth_man == ACF_UNDEFINED) + else if (auth_plugin == ACF_PENDING + || auth_script == ACF_PENDING + || auth_man == ACF_PENDING) { if (now < ks->auth_deferred_expire) { diff --git a/src/openvpn/ssl_verify.h b/src/openvpn/ssl_verify.h index 8358fb986..06b88b568 100644 --- a/src/openvpn/ssl_verify.h +++ b/src/openvpn/ssl_verify.h @@ -69,8 +69,7 @@ enum tls_auth_status { TLS_AUTHENTICATION_SUCCEEDED=0, TLS_AUTHENTICATION_FAILED=1, - TLS_AUTHENTICATION_DEFERRED=2, - TLS_AUTHENTICATION_UNDEFINED=3 + TLS_AUTHENTICATION_DEFERRED=2 }; /** From patchwork Thu Apr 22 05:17:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 1760 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.7]) by backend30.mail.ord1d.rsapps.net with LMTP id sPkzO8CTgWCGPQAAIUCqbw (envelope-from ) for ; Thu, 22 Apr 2021 11:18:24 -0400 Received: from proxy10.mail.iad3a.rsapps.net ([172.27.255.7]) by director10.mail.ord1d.rsapps.net with LMTP id +HnzOsCTgWBhfQAApN4f7A (envelope-from ) for ; Thu, 22 Apr 2021 11:18:24 -0400 Received: from smtp30.gate.iad3a ([172.27.255.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy10.mail.iad3a.rsapps.net with LMTPS id mL4pNMCTgWBxLQAAnQ/bqA (envelope-from ) for ; Thu, 22 Apr 2021 11:18:24 -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: smtp30.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: fa64388c-a37d-11eb-b40f-525400089674-1-1 Received: from [216.105.38.7] ([216.105.38.7:40132] helo=lists.sourceforge.net) by smtp30.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 77/6E-20230-FB391806; Thu, 22 Apr 2021 11:18:24 -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 1lZb5L-0005e7-Tp; Thu, 22 Apr 2021 15:17:43 +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 1lZb5L-0005dt-6a for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 15:17:43 +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=KeR4iec6m0kmY2mZJWVUaE4svvjE2gDMLvoAtCVx724=; b=lp4r2FCCsmbBfKWpcSpODU9VVy EiZGUT/f83AO3+OaCnn4zOcsfORnjCYclGo//JuozATHpKoj27JvDRRNBZzY0M8TcMOdRhISqcJQx vlalbbzYTxfdgD/roBfSzWZuiuR4h1F8jPOUT31sGBM/8b9kdgQIrvO2+we7i7CYLExw=; 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=KeR4iec6m0kmY2mZJWVUaE4svvjE2gDMLvoAtCVx724=; b=VHKHDHhuMbiXxD3BLE1oAdGpNr M3Dy/LfNg3bGG7j1Be6vGWtASOjuGOlZjC2b5ARBRfx4Pn2mIzBr0qjzpW1hCGauYV1++a/OL4HaO 7TYh+1nz4d31+9+x2Q6xpUWN/qg7RpXuHXPLCkCRBbfd5X5W3B+CXsyiksOM1UPRv5Ws=; 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-0007cU-AV for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 15:17:43 +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-000B5U-Dm for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 17:17:24 +0200 Received: (nullmailer pid 2132628 invoked by uid 10006); Thu, 22 Apr 2021 15:17:24 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Thu, 22 Apr 2021 17:17:21 +0200 Message-Id: <20210422151724.2132573-4-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-0007cU-AV Subject: [Openvpn-devel] [PATCH 4/7] Make waiting on auth an explicit state in the context state machine 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 Previously we relied on checking tls_authentication_status to check wether to determine if the context auth state is actually valid or not. This patch eliminates that check by introducing waiting on the authentication as extra state in the context auth, state machine. Signed-off-by: Arne Schwabe --- src/openvpn/multi.c | 6 ------ src/openvpn/ssl.c | 9 ++++++++- src/openvpn/ssl_common.h | 1 + 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index ab2270a58..7cb9e86aa 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -2596,12 +2596,6 @@ static const multi_client_connect_handler client_connect_handlers[] = { static void multi_connection_established(struct multi_context *m, struct multi_instance *mi) { - if (tls_authentication_status(mi->context.c2.tls_multi, TLS_MULTI_AUTH_STATUS_INTERVAL) - != TLS_AUTHENTICATION_SUCCEEDED) - { - return; - } - /* We are only called for the CAS_PENDING_x states, so we * can ignore other states here */ bool from_deferred = (mi->context.c2.tls_multi->multi_state != CAS_PENDING); diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index 7d66cf565..4bb395039 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -2809,7 +2809,7 @@ tls_process(struct tls_multi *multi, if (session->opt->mode == MODE_SERVER) { /* On a server we continue with running connect scripts next */ - multi->multi_state = CAS_PENDING; + multi->multi_state = CAS_WAITING_AUTH; } else { @@ -3135,6 +3135,13 @@ tls_multi_process(struct tls_multi *multi, enum tls_auth_status tas = tls_authentication_status(multi, TLS_MULTI_AUTH_STATUS_INTERVAL); + /* 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_WAITING_AUTH && tas == TLS_AUTHENTICATION_SUCCEEDED) + { + multi->multi_state = CAS_PENDING; + } + /* * If lame duck session expires, kill it. */ diff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h index 026da3578..01f00950e 100644 --- a/src/openvpn/ssl_common.h +++ b/src/openvpn/ssl_common.h @@ -512,6 +512,7 @@ struct tls_session * connect scripts/plugins */ enum multi_status { CAS_NOT_CONNECTED, + CAS_WAITING_AUTH, /**< TLS connection established but deferred auth not finished */ CAS_PENDING, CAS_PENDING_DEFERRED, CAS_PENDING_DEFERRED_PARTIAL, /**< at least handler succeeded, no result yet*/ From patchwork Thu Apr 22 05:17:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 1761 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director9.mail.ord1d.rsapps.net ([172.27.255.58]) by backend30.mail.ord1d.rsapps.net with LMTP id qFAFEcOTgWCGPQAAIUCqbw (envelope-from ) for ; Thu, 22 Apr 2021 11:18:27 -0400 Received: from proxy2.mail.iad3a.rsapps.net ([172.27.255.58]) by director9.mail.ord1d.rsapps.net with LMTP id kLXgEMOTgWBfYgAAalYnBA (envelope-from ) for ; Thu, 22 Apr 2021 11:18:27 -0400 Received: from smtp17.gate.iad3a ([172.27.255.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy2.mail.iad3a.rsapps.net with LMTPS id CFJBCsOTgWAGSwAABcWvHw (envelope-from ) for ; Thu, 22 Apr 2021 11:18:27 -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: smtp17.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: fbe97afa-a37d-11eb-b208-525400723ca9-1-1 Received: from [216.105.38.7] ([216.105.38.7:32954] helo=lists.sourceforge.net) by smtp17.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 44/BA-15159-2C391806; Thu, 22 Apr 2021 11:18:26 -0400 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 1lZb5O-0001CF-6J; Thu, 22 Apr 2021 15:17:46 +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 1lZb5K-0001Bx-Er for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 15:17:42 +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=Pl8tTEGlG18l0q+NzThie2Oifi4X7Vcilnmw0/0BSjc=; b=hd4JsCVeSd135n125diLbTGQqz sEAKZEtFFoYmQkBHYOKXmejjAWPgRsyIMpMe7ZOExW6oFBVJvg0PCLSGDjvnOfysBMKv5mBVaDWTN bzvwo/DrxN9KxsocxfmdZ64PIpc3OvehSPcT2/n9NXnIxMqBuvOXiJ6dkUNIIRvTLsDE=; 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=Pl8tTEGlG18l0q+NzThie2Oifi4X7Vcilnmw0/0BSjc=; b=LonnCQb3xIkd0CoO0C5f6rMHuh IzKZ1GjcR2560OWvVOr20woXv2nEXGbkoxqpPedYbPP6B4xs2F+mQhwPRB05KwsE3d8HZYqiXTwWi CSpOA1AHWZYXvfjMT5Ba9A99WDks5zP0LafuUIKnh92EOvVwp5E2SQ5BJ3r5kuTkdpD4=; Received: from mail.blinkt.de ([192.26.174.232]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1lZb5E-00DGRD-J8 for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 15:17:43 +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-000B5Y-GR for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 17:17:24 +0200 Received: (nullmailer pid 2132631 invoked by uid 10006); Thu, 22 Apr 2021 15:17:24 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Thu, 22 Apr 2021 17:17:22 +0200 Message-Id: <20210422151724.2132573-5-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-00DGRD-J8 Subject: [Openvpn-devel] [PATCH 5/7] Extracting key_state deferred auth status update into function 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 This extract the update of a deferred key status into into own function. Signed-off-by: Arne Schwabe --- src/openvpn/ssl_verify.c | 90 ++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 32 deletions(-) diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c index e000f75f7..912012eb7 100644 --- a/src/openvpn/ssl_verify.c +++ b/src/openvpn/ssl_verify.c @@ -1073,6 +1073,56 @@ key_state_test_auth_control_file(struct auth_deferred_status *ads, bool cached) return ACF_DISABLED; } +/** + * This method takes a key_state and if updates the state + * of the key if it is deferred. + * @param cached If auth control files should be tried to be opened or th + * cached results should be used + * @param ks The key_state to update + */ +static void +update_key_auth_status(bool cached, struct key_state *ks) +{ + if (ks->authenticated == KS_AUTH_FALSE) + { + return; + } + else + { + enum auth_deferred_result auth_plugin = ACF_DISABLED; + enum auth_deferred_result auth_script = ACF_DISABLED; + enum auth_deferred_result auth_man = ACF_DISABLED; + auth_plugin = key_state_test_auth_control_file(&ks->plugin_auth, cached); + auth_script = key_state_test_auth_control_file(&ks->script_auth, cached); +#ifdef ENABLE_MANAGEMENT + auth_man = man_def_auth_test(ks); +#endif + ASSERT(auth_plugin < 4 && auth_script < 4 && auth_man < 4); + + if (auth_plugin == ACF_FAILED || auth_script == ACF_FAILED + || auth_man == ACF_FAILED) + { + ks->authenticated = KS_AUTH_FALSE; + return; + } + else if (auth_plugin == ACF_PENDING + || auth_script == ACF_PENDING + || auth_man == ACF_PENDING) + { + if (now < ks->auth_deferred_expire) + { + /* key state is remains in KS_DEFERRED */ + } + } + else + { + /* auth_plugin and auth_man are either ACF_DISABLED or ACF_SUCCEDED */ + ks->authenticated = KS_AUTH_TRUE; + } + } +} + + enum tls_auth_status tls_authentication_status(struct tls_multi *multi, const int latency) { @@ -1096,43 +1146,19 @@ tls_authentication_status(struct tls_multi *multi, const int latency) if (TLS_AUTHENTICATED(multi, ks)) { active++; + update_key_auth_status(cached, ks); + if (ks->authenticated == KS_AUTH_FALSE) { failed_auth = true; } - else + else if (ks->authenticated == KS_AUTH_DEFERRED) { - enum auth_deferred_result auth_plugin = ACF_DISABLED; - enum auth_deferred_result auth_script = ACF_DISABLED; - enum auth_deferred_result auth_man = ACF_DISABLED; - auth_plugin = key_state_test_auth_control_file(&ks->plugin_auth, cached); - auth_script = key_state_test_auth_control_file(&ks->script_auth, cached); -#ifdef ENABLE_MANAGEMENT - auth_man = man_def_auth_test(ks); -#endif - ASSERT(auth_plugin < 4 && auth_script < 4 && auth_man < 4); - - if (auth_plugin == ACF_FAILED || auth_script == ACF_FAILED - || auth_man == ACF_FAILED) - { - ks->authenticated = KS_AUTH_FALSE; - failed_auth = true; - } - else if (auth_plugin == ACF_PENDING - || auth_script == ACF_PENDING - || auth_man == ACF_PENDING) - { - if (now < ks->auth_deferred_expire) - { - deferred = true; - } - } - else - { - /* auth_plugin and auth_man are either ACF_DISABLED or ACF_SUCCEDED */ - success = true; - ks->authenticated = KS_AUTH_TRUE; - } + deferred = true; + } + else if (ks->authenticated == KS_AUTH_TRUE) + { + success = true; } } } 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, }; From patchwork Thu Apr 22 05:17:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 1759 X-Patchwork-Delegate: a@unstable.cc Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director15.mail.ord1d.rsapps.net ([172.27.255.57]) by backend30.mail.ord1d.rsapps.net with LMTP id yMcdOcCTgWBXAwAAIUCqbw (envelope-from ) for ; Thu, 22 Apr 2021 11:18:24 -0400 Received: from proxy16.mail.iad3a.rsapps.net ([172.27.255.57]) by director15.mail.ord1d.rsapps.net with LMTP id YPPhOMCTgWBATAAAIcMcQg (envelope-from ) for ; Thu, 22 Apr 2021 11:18:24 -0400 Received: from smtp16.gate.iad3a ([172.27.255.57]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy16.mail.iad3a.rsapps.net with LMTPS id WB2kMcCTgWDXAwAADc5QwQ (envelope-from ) for ; Thu, 22 Apr 2021 11:18:24 -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: smtp16.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: fa62aa26-a37d-11eb-93c2-5254004ee196-1-1 Received: from [216.105.38.7] ([216.105.38.7:40130] helo=lists.sourceforge.net) by smtp16.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 0E/CD-10307-FB391806; Thu, 22 Apr 2021 11:18:24 -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 1lZb5K-0005dc-PS; Thu, 22 Apr 2021 15:17:42 +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 1lZb5J-0005cq-Em for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 15:17:41 +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=sZ0PctjY1DzEbjSBqcDkwnkHVenqQmrbRFycRjMlcL0=; b=Q8Bxdpjvn+22VTzKJp+mL9hw+x toVSSAvITPphpYfkscLyHLEJTsdd5leyneQIT7etUPuTktZ3zO3ejWApoQ4eVDG/P2Ylt4laxbdwG M/AJlEwcjICrWfkFtfaHmvkYJaaPmUD505vLs4nCngFA306DhV1ADay/LVl+3LAGe9Wk=; 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=sZ0PctjY1DzEbjSBqcDkwnkHVenqQmrbRFycRjMlcL0=; b=lZSkOhl3qNxauPVr5OlwlDfgpW uHOSo1XNYVTJhxJ+u6VE8kPNOtcdYgXDQyB01OARI+28+RI9YgeKlQ3+lH5KZsoTV8avj51IGp5Fz /+zR+FvPVl0bwHGbWCIHhn1bl2Q5dlBE51YNymNte45vBrR1KEgWwa/0mBSx/Zxw1v5s=; Received: from mail.blinkt.de ([192.26.174.232]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1lZb5E-00DGRE-Fr for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 15:17:41 +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-000B5e-Lv for openvpn-devel@lists.sourceforge.net; Thu, 22 Apr 2021 17:17:24 +0200 Received: (nullmailer pid 2132637 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:24 +0200 Message-Id: <20210422151724.2132573-7-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-00DGRE-Fr Subject: [Openvpn-devel] [PATCH 7/7] Move auth_token_state_flags to tls_session and cleanup initial_token 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 The usage of the auth_token_state_flags is tied to the authentication. The other authentication related flags and status are in the tls_session struct instead of the tls_multi struct. Move auth_token_state_flags to the right place. This also changes that auth_token_initial is set when the token is initially generated instead when pushing the token. Even I don't know anymore why I did it in this way in the first place. Also use multi->auth_token_initial as source for the sesssion ID since it should now always be available. Signed-off-by: Arne Schwabe --- src/openvpn/auth_token.c | 22 ++++++++++++++++++---- src/openvpn/push.c | 8 -------- src/openvpn/ssl_verify.c | 5 ++++- tests/unit_tests/openvpn/test_auth_token.c | 14 ++++++++++---- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/openvpn/auth_token.c b/src/openvpn/auth_token.c index 0ea6d1832..d0cdc5f7f 100644 --- a/src/openvpn/auth_token.c +++ b/src/openvpn/auth_token.c @@ -109,11 +109,11 @@ add_session_token_env(struct tls_session *session, struct tls_multi *multi, /* * No session before, generate a new session token for the new session */ - if (!multi->auth_token) + if (!multi->auth_token_initial) { generate_auth_token(up, multi); } - session_id_source = multi->auth_token; + session_id_source = multi->auth_token_initial; } /* * In the auth-token the auth token is already base64 encoded @@ -184,7 +184,7 @@ generate_auth_token(const struct user_pass *up, struct tls_multi *multi) uint8_t sessid[AUTH_TOKEN_SESSION_ID_LEN]; - if (multi->auth_token) + if (multi->auth_token_initial) { /* Just enough space to fit 8 bytes+ 1 extra to decode a non padded * base64 string (multiple of 3 bytes). 9 bytes => 12 bytes base64 @@ -192,13 +192,18 @@ generate_auth_token(const struct user_pass *up, struct tls_multi *multi) */ char old_tstamp_decode[9]; + /* Make a copy of the string to not modify multi->auth_token_initial */ + char* initial_token_copy = string_alloc(multi->auth_token_initial, &gc); + /* * reuse the same session id and timestamp and null terminate it at * for base64 decode it only decodes the session id part of it */ - char *old_sessid = multi->auth_token + strlen(SESSION_ID_PREFIX); + char *old_sessid =initial_token_copy + strlen(SESSION_ID_PREFIX); char *old_tsamp_initial = old_sessid + AUTH_TOKEN_SESSION_ID_LEN*8/6; + + old_tsamp_initial[12] = '\0'; ASSERT(openvpn_base64_decode(old_tsamp_initial, old_tstamp_decode, 9) == 9); @@ -277,6 +282,15 @@ generate_auth_token(const struct user_pass *up, struct tls_multi *multi) dmsg(D_SHOW_KEYS, "Generated token for client: %s (%s)", multi->auth_token, up->username); + if (!multi->auth_token_initial) + { + /* + * Save the initial auth token for clients that ignore + * the updates to the token + */ + multi->auth_token_initial = strdup(multi->auth_token); + } + gc_free(&gc); } diff --git a/src/openvpn/push.c b/src/openvpn/push.c index 428efb68e..c42b5f2e6 100644 --- a/src/openvpn/push.c +++ b/src/openvpn/push.c @@ -523,14 +523,6 @@ prepare_auth_token_push_reply(struct tls_multi *tls_multi, struct gc_arena *gc, push_option_fmt(gc, push_list, M_USAGE, "auth-token %s", tls_multi->auth_token); - if (!tls_multi->auth_token_initial) - { - /* - * Save the initial auth token for clients that ignore - * the updates to the token - */ - tls_multi->auth_token_initial = strdup(tls_multi->auth_token); - } } } diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c index 912012eb7..ff983551f 100644 --- a/src/openvpn/ssl_verify.c +++ b/src/openvpn/ssl_verify.c @@ -1544,6 +1544,7 @@ verify_user_pass(struct user_pass *up, struct tls_multi *multi, */ if (session->opt->auth_token_generate && is_auth_token(up->password)) { + ks->auth_token_state_flags = verify_auth_token(up, multi, session); if (session->opt->auth_token_call_auth) { @@ -1672,7 +1673,9 @@ verify_user_pass(struct user_pass *up, struct tls_multi *multi, * Otherwise the auth-token get pushed out as part of the "normal" * push-reply */ - if (multi->auth_token_initial) + bool initial_connect = session->key[KS_PRIMARY].key_id == 0; + + if (multi->auth_token_initial && !initial_connect) { /* * We do not explicitly schedule the sending of the diff --git a/tests/unit_tests/openvpn/test_auth_token.c b/tests/unit_tests/openvpn/test_auth_token.c index 69fc1f8c9..922bd9b13 100644 --- a/tests/unit_tests/openvpn/test_auth_token.c +++ b/tests/unit_tests/openvpn/test_auth_token.c @@ -174,7 +174,9 @@ auth_token_test_timeout(void **state) now = 100000; generate_auth_token(&ctx->up, &ctx->multi); + strcpy(ctx->up.password, ctx->multi.auth_token); + ctx->multi.auth_token_initial = NULL; /* No time has passed */ assert_int_equal(verify_auth_token(&ctx->up, &ctx->multi, ctx->session), @@ -244,10 +246,10 @@ auth_token_test_known_keys(void **state) now = 0; /* Preload the session id so the same session id is used here */ - ctx->multi.auth_token = strdup(now0key0); + ctx->multi.auth_token_initial = strdup(now0key0); /* Zero the hmac part to ensure we have a newly generated token */ - zerohmac(ctx->multi.auth_token); + zerohmac(ctx->multi.auth_token_initial); generate_auth_token(&ctx->up, &ctx->multi); @@ -305,13 +307,16 @@ auth_token_test_env(void **state) { struct test_context *ctx = (struct test_context *) *state; + struct key_state *ks = &ctx->multi.session[TM_ACTIVE].key[KS_PRIMARY]; ks->auth_token_state_flags = 0; + ctx->multi.auth_token = NULL; add_session_token_env(ctx->session, &ctx->multi, &ctx->up); assert_string_equal(lastsesion_statevalue, "Initial"); + ks->auth_token_state_flags = 0; strcpy(ctx->up.password, now0key0); add_session_token_env(ctx->session, &ctx->multi, &ctx->up); @@ -331,6 +336,7 @@ auth_token_test_env(void **state) ks->auth_token_state_flags = AUTH_TOKEN_HMAC_OK|AUTH_TOKEN_EXPIRED|AUTH_TOKEN_VALID_EMPTYUSER; add_session_token_env(ctx->session, &ctx->multi, &ctx->up); + assert_string_equal(lastsesion_statevalue, "ExpiredEmptyUser"); } @@ -341,13 +347,13 @@ auth_token_test_random_keys(void **state) now = 0x5c331e9c; /* Preload the session id so the same session id is used here */ - ctx->multi.auth_token = strdup(random_token); + ctx->multi.auth_token_initial = strdup(random_token); free_key_ctx(&ctx->multi.opt.auth_token_key); auth_token_init_secret(&ctx->multi.opt.auth_token_key, random_key, true); /* Zero the hmac part to ensure we have a newly generated token */ - zerohmac(ctx->multi.auth_token); + zerohmac(ctx->multi.auth_token_initial); generate_auth_token(&ctx->up, &ctx->multi);