From patchwork Mon Aug 24 21:36:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 1401 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director12.mail.ord1d.rsapps.net ([172.28.255.1]) by backend30.mail.ord1d.rsapps.net with LMTP id gHatLsG/RF8MBwAAIUCqbw for ; Tue, 25 Aug 2020 03:37:37 -0400 Received: from proxy9.mail.ord1c.rsapps.net ([172.28.255.1]) by director12.mail.ord1d.rsapps.net with LMTP id 8IiZLsG/RF9SXgAAIasKDg (envelope-from ) for ; Tue, 25 Aug 2020 03:37:37 -0400 Received: from smtp20.gate.ord1c ([172.28.255.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy9.mail.ord1c.rsapps.net with LMTPS id SHhNLsG/RF/2VAAAgxtkuw (envelope-from ) for ; Tue, 25 Aug 2020 03:37:37 -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: smtp20.gate.ord1c.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: d8d9778a-e6a5-11ea-a2c3-bc305bf03180-1-1 Received: from [216.105.38.7] ([216.105.38.7:50982] helo=lists.sourceforge.net) by smtp20.gate.ord1c.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 0C/75-14820-1CFB44F5; Tue, 25 Aug 2020 03:37:37 -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 1kATVu-0001pw-T7; Tue, 25 Aug 2020 07:37:02 +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 1kATVt-0001pI-Dg for openvpn-devel@lists.sourceforge.net; Tue, 25 Aug 2020 07:37:01 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=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:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=nmTcTKvAoMLTl5BhEFnGoEuRMfgVD+X+8i+TIw0boH0=; b=U7r7aYO3IpbCuCG+CypA2sVCqy jwqjGOugI8hU+xkv6ALKbladYWght9TnaJX0sr/xZHIcZw2Sbxy3W2npJJAkVe4sEJlnBJ3Y2pYko ggPixAhrvz79aIFriCtH1RWS8O6G8KJFmSIho3fdMImq9Bjg0zZKn8PCSlE/e8yx/b5c=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=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: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=nmTcTKvAoMLTl5BhEFnGoEuRMfgVD+X+8i+TIw0boH0=; b=bJS1GFhd1QC5iqc1ma5pjGgOI0 u7JsPhGdgqSxje1HZ29+XT8phGoGi4iWq8pM3OuSwESLT4vw354VcYDKhzVTalImyAe9Q32hAUAWm m3tCySKRXfDCIbM5Qwe6YFXRLfObuX1wWSYoYZsy6tvPajJjNxwXSv+1h4sEo9Smv4hA=; Received: from [192.26.174.232] (helo=mail.blinkt.de) by sfi-mx-4.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1kATVr-005rqD-S0 for openvpn-devel@lists.sourceforge.net; Tue, 25 Aug 2020 07:37:01 +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 1kATVb-000Pib-IW for openvpn-devel@lists.sourceforge.net; Tue, 25 Aug 2020 09:36:43 +0200 Received: (nullmailer pid 15965 invoked by uid 10006); Tue, 25 Aug 2020 07:36:43 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Tue, 25 Aug 2020 09:36:42 +0200 Message-Id: <20200825073643.15920-1-arne@rfc2549.org> X-Mailer: git-send-email 2.17.1 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.5 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1kATVr-005rqD-S0 Subject: [Openvpn-devel] [PATCH v4 1/2] Move openvpn specific key expansion 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: , MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox This moves the OpenVPN specific PRF into its own function also simplifies the code a bit by passing tls_session directly instead of 5 of its fields. Patch v2: Rebase Patch v4: rewrite/fix comments, fix potential not initialised before goto issue Signed-off-by: Arne Schwabe Acked-by: Steffan Karger --- src/openvpn/ssl.c | 119 ++++++++++++++++++++++++++++++---------------- 1 file changed, 79 insertions(+), 40 deletions(-) diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index 3fcaa25f..b4d94b8a 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -1765,27 +1765,37 @@ openvpn_PRF(const uint8_t *secret, VALGRIND_MAKE_READABLE((void *)output, output_len); } -/* - * Using source entropy from local and remote hosts, mix into - * master key. - */ -static bool -generate_key_expansion(struct key_ctx_bi *key, - const struct key_type *key_type, - const struct key_source2 *key_src, - const struct session_id *client_sid, - const struct session_id *server_sid, - bool server) +static void +init_key_contexts(struct key_ctx_bi *key, + const struct key_type *key_type, + bool server, + struct key2 *key2) +{ + /* Initialize key contexts */ + int key_direction = server ? KEY_DIRECTION_INVERSE : KEY_DIRECTION_NORMAL; + init_key_ctx_bi(key, key2, key_direction, key_type, "Data Channel"); + + /* Initialize implicit IVs */ + key_ctx_update_implicit_iv(&key->encrypt, (*key2).keys[(int)server].hmac, + MAX_HMAC_KEY_LENGTH); + key_ctx_update_implicit_iv(&key->decrypt, (*key2).keys[1-(int)server].hmac, + MAX_HMAC_KEY_LENGTH); + +} + + +static struct key2 +generate_key_expansion_openvpn_prf(const struct tls_session *session) { uint8_t master[48] = { 0 }; - struct key2 key2 = { 0 }; - bool ret = false; - if (key->initialized) - { - msg(D_TLS_ERRORS, "TLS Error: key already initialized"); - goto exit; - } + const struct key_state *ks = &session->key[KS_PRIMARY]; + const struct key_source2 *key_src = ks->key_src; + + const struct session_id *client_sid = session->opt->server ? + &ks->session_id_remote : &session->session_id; + const struct session_id *server_sid = !session->opt->server ? + &ks->session_id_remote : &session->session_id; /* debugging print of source key material */ key_source2_print(key_src); @@ -1803,6 +1813,7 @@ generate_key_expansion(struct key_ctx_bi *key, master, sizeof(master)); + struct key2 key2; /* compute key expansion */ openvpn_PRF(master, sizeof(master), @@ -1815,41 +1826,73 @@ generate_key_expansion(struct key_ctx_bi *key, server_sid, (uint8_t *)key2.keys, sizeof(key2.keys)); + secure_memzero(&master, sizeof(master)); + /* + * fixup_key only correctly sets DES parity bits if the cipher is a + * DES variant. + * + * The newer OpenSSL and mbed TLS libraries (those that support EKM) + * ignore these bits. + * + * We keep the DES fixup here as compatibility. + * OpenVPN3 never did this fixup anyway. So this code is *probably* not + * required but we keep it for compatibility until we remove DES support + * since it does not hurt either. + */ + for (int i = 0; i < 2; ++i) + { + fixup_key(&key2.keys[i], &session->opt->key_type); + } key2.n = 2; - key2_print(&key2, key_type, "Master Encrypt", "Master Decrypt"); + return key2; +} + +/* + * Using source entropy from local and remote hosts, mix into + * master key. + */ +static bool +generate_key_expansion(struct key_ctx_bi *key, + const struct tls_session *session) +{ + bool ret = false; + struct key2 key2; + + if (key->initialized) + { + msg(D_TLS_ERRORS, "TLS Error: key already initialized"); + goto exit; + } + + + bool server = session->opt->server; + + key2 = generate_key_expansion_openvpn_prf(session); + + key2_print(&key2, &session->opt->key_type, + "Master Encrypt", "Master Decrypt"); /* check for weak keys */ for (int i = 0; i < 2; ++i) { - fixup_key(&key2.keys[i], key_type); - if (!check_key(&key2.keys[i], key_type)) + if (!check_key(&key2.keys[i], &session->opt->key_type)) { msg(D_TLS_ERRORS, "TLS Error: Bad dynamic key generated"); goto exit; } } - - /* Initialize OpenSSL key contexts */ - int key_direction = server ? KEY_DIRECTION_INVERSE : KEY_DIRECTION_NORMAL; - init_key_ctx_bi(key, &key2, key_direction, key_type, "Data Channel"); - - /* Initialize implicit IVs */ - key_ctx_update_implicit_iv(&key->encrypt, key2.keys[(int)server].hmac, - MAX_HMAC_KEY_LENGTH); - key_ctx_update_implicit_iv(&key->decrypt, key2.keys[1-(int)server].hmac, - MAX_HMAC_KEY_LENGTH); - + init_key_contexts(key, &session->opt->key_type, server, &key2); ret = true; exit: - secure_memzero(&master, sizeof(master)); secure_memzero(&key2, sizeof(key2)); return ret; } + static void key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len) { @@ -1879,10 +1922,7 @@ tls_session_generate_data_channel_keys(struct tls_session *session) { bool ret = false; struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */ - const struct session_id *client_sid = session->opt->server ? - &ks->session_id_remote : &session->session_id; - const struct session_id *server_sid = !session->opt->server ? - &ks->session_id_remote : &session->session_id; + if (ks->authenticated == KS_AUTH_FALSE) { @@ -1891,9 +1931,8 @@ tls_session_generate_data_channel_keys(struct tls_session *session) } ks->crypto_options.flags = session->opt->crypto_flags; - if (!generate_key_expansion(&ks->crypto_options.key_ctx_bi, - &session->opt->key_type, ks->key_src, client_sid, server_sid, - session->opt->server)) + + if (!generate_key_expansion(&ks->crypto_options.key_ctx_bi, session)) { msg(D_TLS_ERRORS, "TLS Error: generate_key_expansion failed"); goto cleanup;