From patchwork Tue Oct 19 07:23:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 2016 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director14.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net with LMTP id GDwXK30Nb2GKMAAAIUCqbw (envelope-from ) for ; Tue, 19 Oct 2021 14:25:01 -0400 Received: from proxy4.mail.ord1d.rsapps.net ([172.30.191.6]) by director14.mail.ord1d.rsapps.net with LMTP id APzOKn0Nb2HZfwAAeJ7fFg (envelope-from ) for ; Tue, 19 Oct 2021 14:25:01 -0400 Received: from smtp18.gate.ord1d ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy4.mail.ord1d.rsapps.net with LMTPS id GNrTKn0Nb2G5TAAAiYrejw (envelope-from ) for ; Tue, 19 Oct 2021 14:25:01 -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: smtp18.gate.ord1d.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: de3fe20e-3109-11ec-b64b-5254005167a7-1-1 Received: from [216.105.38.7] ([216.105.38.7:53776] helo=lists.sourceforge.net) by smtp18.gate.ord1d.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 70/37-02336-B7D0F616; Tue, 19 Oct 2021 14:25:00 -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 1mctmf-0005g3-Mo; Tue, 19 Oct 2021 18:24:21 +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 1mctmD-0005Y1-Vh for openvpn-devel@lists.sourceforge.net; Tue, 19 Oct 2021 18:23:53 +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=6GtGijVdrGA1EZiLQGWqQP6hiU3q2PZDp8KJSu1gAW4=; b=gFT58Re8UCWgiw2flhAttjkPuE 6WWm3X1wUaWzdOz0M2vGbMA8vOCzl6Ao/409vAsEuGRN+fONe2JXg2VmsDKeSU6sNWyDvmxucY01V B08Z/+ckYpDNQ50oH6CahNmvuMuMJnmNT3CSbC6lBSlAhJS+cFn4shPjkmh1lMymCdrQ=; 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=6GtGijVdrGA1EZiLQGWqQP6hiU3q2PZDp8KJSu1gAW4=; b=DDB3/CCHDpewgQGNJxL6gbtDGc 2HG2ZKU7QNYA0dvLZi2vUzteLQTRu1qfGM7HaMx37Yk+eWIhHbpfjJxR8pYS6mXQTp6kuFaJhOssu yITVPSqh0waphxE0+rt8E+/X7lfcTWt3pGqhAWm/hjAso7ielggGosAgIPy+8fV3Zhsg=; 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.3) id 1mctm4-006Teh-Bl for openvpn-devel@lists.sourceforge.net; Tue, 19 Oct 2021 18:23:53 +0000 Received: from kamera.blinkt.de ([2001:638:502:390:20c:29ff:fec8:535c]) by mail.blinkt.de with smtp (Exim 4.94.2 (FreeBSD)) (envelope-from ) id 1mctls-0008aQ-HE for openvpn-devel@lists.sourceforge.net; Tue, 19 Oct 2021 20:23:32 +0200 Received: (nullmailer pid 613208 invoked by uid 10006); Tue, 19 Oct 2021 18:23:32 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Tue, 19 Oct 2021 20:23:06 +0200 Message-Id: <20211019182332.613155-2-arne@rfc2549.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211019182332.613155-1-arne@rfc2549.org> References: <20211019182332.613155-1-arne@rfc2549.org> MIME-Version: 1.0 X-Spam-Report: Spam detection software, running on the system "util-spamd-1.v13.lw.sourceforge.com", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: The old API is deprecated in OpenSSL 3.0 and the new API does not yet exist in OpenSSL 1.1. Emulating the new API would be more complex than just having two implementations. So this switches to a new [...] Content analysis details: (0.3 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 SPF_NONE SPF: sender does not publish an SPF Record X-Headers-End: 1mctm4-006Teh-Bl Subject: [Openvpn-devel] [PATCH v2 01/20] [OSSL 3.0] Use new EVP_MAC API for HMAC implementation 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 old API is deprecated in OpenSSL 3.0 and the new API does not yet exist in OpenSSL 1.1. Emulating the new API would be more complex than just having two implementations. So this switches to a new hmac implementation for OpenSSL 3.0. Unfortunately the new API does not have an easy to reset an HMAC, so we need to keep the key around to emulate a reset functionality. Signed-off-by: Arne Schwabe --- src/openvpn/crypto_backend.h | 2 +- src/openvpn/crypto_mbedtls.c | 2 +- src/openvpn/crypto_openssl.c | 96 +++++++++++++++++++++++++++++++++++- src/openvpn/crypto_openssl.h | 8 +++ 4 files changed, 104 insertions(+), 4 deletions(-) diff --git a/src/openvpn/crypto_backend.h b/src/openvpn/crypto_backend.h index e9447f82f..e0bfdf585 100644 --- a/src/openvpn/crypto_backend.h +++ b/src/openvpn/crypto_backend.h @@ -643,7 +643,7 @@ void hmac_ctx_cleanup(hmac_ctx_t *ctx); * * @return Size of the HMAC, or \0 if ctx is NULL. */ -int hmac_ctx_size(const hmac_ctx_t *ctx); +int hmac_ctx_size(hmac_ctx_t *ctx); /* * Resets the given HMAC context, preserving the associated key information diff --git a/src/openvpn/crypto_mbedtls.c b/src/openvpn/crypto_mbedtls.c index c632849db..e2f5f4012 100644 --- a/src/openvpn/crypto_mbedtls.c +++ b/src/openvpn/crypto_mbedtls.c @@ -939,7 +939,7 @@ hmac_ctx_cleanup(mbedtls_md_context_t *ctx) } int -hmac_ctx_size(const mbedtls_md_context_t *ctx) +hmac_ctx_size(mbedtls_md_context_t *ctx) { if (NULL == ctx) { diff --git a/src/openvpn/crypto_openssl.c b/src/openvpn/crypto_openssl.c index 419265a51..1c800df7f 100644 --- a/src/openvpn/crypto_openssl.c +++ b/src/openvpn/crypto_openssl.c @@ -1001,7 +1001,7 @@ md_ctx_final(EVP_MD_CTX *ctx, uint8_t *dst) * Generic HMAC functions * */ - +#if OPENSSL_VERSION_NUMBER < 0x30000000L HMAC_CTX * hmac_ctx_new(void) { @@ -1039,7 +1039,7 @@ hmac_ctx_cleanup(HMAC_CTX *ctx) } int -hmac_ctx_size(const HMAC_CTX *ctx) +hmac_ctx_size(HMAC_CTX *ctx) { return HMAC_size(ctx); } @@ -1066,6 +1066,98 @@ hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst) HMAC_Final(ctx, dst, &in_hmac_len); } +#else +hmac_ctx_t * +hmac_ctx_new(void) +{ + hmac_ctx_t *ctx; + ALLOC_OBJ_CLEAR(ctx, hmac_ctx_t); + EVP_MAC *hmac = EVP_MAC_fetch(NULL, "HMAC", NULL); + ctx->ctx = EVP_MAC_CTX_new(hmac); + check_malloc_return(ctx->ctx); + return ctx; +} + +void +hmac_ctx_free(hmac_ctx_t *ctx) +{ + EVP_MAC_CTX_free(ctx->ctx); + secure_memzero(ctx, sizeof(hmac_ctx_t)); + free(ctx); +} + +void +hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, int key_len, + const EVP_MD *kt) +{ + ASSERT(NULL != kt && NULL != ctx && ctx->ctx != NULL); + ASSERT(key_len <= EVP_MAX_KEY_LENGTH); + + /* We need to make a copy of the key since the OSSL parameters + * only reference it */ + memcpy(ctx->key, key, key_len); + + /* Lookup/setting of parameters in OpenSSL 3.0 are string based + * + * The OSSL_PARAM_construct_utf8_string needs a non const str but this + * only used for lookup so we cast (as OpenSSL also does internally) + * the constness away here. + */ + ctx->params[0] = OSSL_PARAM_construct_utf8_string("digest", + (char *) EVP_MD_get0_name(kt), 0); + ctx->params[1] = OSSL_PARAM_construct_octet_string("key", + ctx->key, key_len); + ctx->params[2] = OSSL_PARAM_construct_end(); + + if (!EVP_MAC_init(ctx->ctx, NULL, 0, ctx->params)) + { + crypto_msg(M_FATAL, "EVP_MAC_init failed"); + } + + /* make sure we used a big enough key */ + ASSERT(EVP_MAC_CTX_get_mac_size(ctx->ctx) <= key_len); +} + +void +hmac_ctx_cleanup(hmac_ctx_t *ctx) +{ + EVP_MAC_init(ctx->ctx, NULL, 0, NULL); +} + +int +hmac_ctx_size(hmac_ctx_t *ctx) +{ + return (int)EVP_MAC_CTX_get_mac_size(ctx->ctx); +} + +void +hmac_ctx_reset(hmac_ctx_t *ctx) +{ + /* The OpenSSL MAC API lacks a reset method and passing NULL as params + * does not reset it either, so use the params array to reinitialise it the + * same way as before */ + if (!EVP_MAC_init(ctx->ctx, NULL, 0, ctx->params)) + { + crypto_msg(M_FATAL, "EVP_MAC_init failed"); + } +} + +void +hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len) +{ + EVP_MAC_update(ctx->ctx, src, src_len); +} + +void +hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst) +{ + /* The calling code always gives us a buffer that has the size of our + * algorithm */ + size_t in_hmac_len = EVP_MAC_CTX_get_mac_size(ctx->ctx); + + EVP_MAC_final(ctx->ctx, dst, &in_hmac_len, in_hmac_len); +} +#endif int memcmp_constant_time(const void *a, const void *b, size_t size) diff --git a/src/openvpn/crypto_openssl.h b/src/openvpn/crypto_openssl.h index 59a31aacf..e540a76b9 100644 --- a/src/openvpn/crypto_openssl.h +++ b/src/openvpn/crypto_openssl.h @@ -47,7 +47,15 @@ typedef EVP_CIPHER_CTX cipher_ctx_t; typedef EVP_MD_CTX md_ctx_t; /** Generic HMAC %context. */ +#if OPENSSL_VERSION_NUMBER < 0x30000000L typedef HMAC_CTX hmac_ctx_t; +#else +typedef struct { + OSSL_PARAM params[3]; + uint8_t key[EVP_MAX_KEY_LENGTH]; + EVP_MAC_CTX *ctx; +} hmac_ctx_t; +#endif /** Maximum length of an IV */ #define OPENVPN_MAX_IV_LENGTH EVP_MAX_IV_LENGTH