From: "MaxF (Code Review)" Date: Thu, 13 Jun 2024 09:35:22 +0000 References: Reply-To: max@...2606..., arne-openvpn@...1227..., rein.vanbaaren@...1435..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <4d799bdd61343c9df823d01e8cb58048d0fd8384-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="Tvfeuv8FwoY="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Fix MBEDTLS_DEPRECATED_REMOVED build errors List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel , comododragon --Tvfeuv8FwoY= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello plaisthos, flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/681?usp=3Demail to review the following change. Change subject: Fix MBEDTLS_DEPRECATED_REMOVED build errors ...................................................................... Fix MBEDTLS_DEPRECATED_REMOVED build errors This commit allows compiling OpenVPN with recent versions of mbed TLS if MBEDTLS_DEPRECATED_REMOVED is defined. Also disables TLS 1.0 and 1.1 because these are dropped in recent versions of mbed TLS. Change-Id: If96c2ebd2af16b18ed34820e8c0531547e2076d9 Signed-off-by: Max Fillinger --- M src/openvpn/mbedtls_compat.h M src/openvpn/ssl_mbedtls.c M src/openvpn/ssl_mbedtls.h 3 files changed, 72 insertions(+), 42 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/81/681/1 diff --git a/src/openvpn/mbedtls_compat.h b/src/openvpn/mbedtls_compat.h index d742b54..74dca22 100644 --- a/src/openvpn/mbedtls_compat.h +++ b/src/openvpn/mbedtls_compat.h @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -51,6 +52,13 @@ #include #endif +#if MBEDTLS_VERSION_NUMBER >=3D 0x03000000 +typedef uint16_t mbedtls_compat_group_id; +#else +#define MBEDTLS_SSL_IANA_TLS_GROUP_NONE MBEDTLS_ECP_DP_NONE +typedef mbedtls_ecp_group_id mbedtls_compat_group_id; +#endif + static inline void mbedtls_compat_psa_crypto_init(void) { @@ -64,6 +72,16 @@ #endif /* HAVE_MBEDTLS_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C) */ } +static inline mbedtls_compat_group_id +mbedtls_compat_get_group_id(const mbedtls_ecp_curve_info *curve_info) +{ +#if MBEDTLS_VERSION_NUMBER >=3D 0x03000000 + return curve_info->tls_id; +#else + return curve_info->grp_id; +#endif +} + /* * In older versions of mbedtls, mbedtls_ctr_drbg_update() did not return = an * error code, and it was deprecated in favor of mbedtls_ctr_drbg_update_r= et() @@ -124,6 +142,34 @@ } #if MBEDTLS_VERSION_NUMBER < 0x03020100 +typedef enum { + MBEDTLS_SSL_VERSION_UNKNOWN, /*!< Context not in use or version not ye= t negotiated. */ + MBEDTLS_SSL_VERSION_TLS1_2 =3D 0x0303, /*!< (D)TLS 1.2 */ + MBEDTLS_SSL_VERSION_TLS1_3 =3D 0x0304, /*!< (D)TLS 1.3 */ +} mbedtls_ssl_protocol_version; + +static inline void +mbedtls_ssl_conf_min_tls_version(mbedtls_ssl_config *conf, mbedtls_ssl_pro= tocol_version tls_version) +{ + int major =3D (tls_version >> 8) & 0xff; + int minor =3D tls_version & 0xff; + mbedtls_ssl_conf_min_version(conf, major, minor); +} + +static inline void +mbedtls_ssl_conf_max_tls_version(mbedtls_ssl_config *conf, mbedtls_ssl_pro= tocol_version tls_version) +{ + int major =3D (tls_version >> 8) & 0xff; + int minor =3D tls_version & 0xff; + mbedtls_ssl_conf_max_version(conf, major, minor); +} + +static inline void +mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf, mbedtls_compat_group_id = *groups) +{ + mbedtls_ssl_conf_curves(conf, groups); +} + static inline size_t mbedtls_cipher_info_get_block_size(const mbedtls_cipher_info_t *cipher) { diff --git a/src/openvpn/ssl_mbedtls.c b/src/openvpn/ssl_mbedtls.c index a68588e..545db0f 100644 --- a/src/openvpn/ssl_mbedtls.c +++ b/src/openvpn/ssl_mbedtls.c @@ -402,7 +402,7 @@ /* Get number of groups and allocate an array in ctx */ int groups_count =3D get_num_elements(groups, ':'); - ALLOC_ARRAY_CLEAR(ctx->groups, mbedtls_ecp_group_id, groups_count + 1) + ALLOC_ARRAY_CLEAR(ctx->groups, mbedtls_compat_group_id, groups_count += 1) /* Parse allowed ciphers, getting IDs */ int i =3D 0; @@ -419,11 +419,11 @@ } else { - ctx->groups[i] =3D ci->grp_id; + ctx->groups[i] =3D mbedtls_compat_get_group_id(ci); i++; } } - ctx->groups[i] =3D MBEDTLS_ECP_DP_NONE; + ctx->groups[i] =3D MBEDTLS_SSL_IANA_TLS_GROUP_NONE; =20 gc_free(&gc); } @@ -1050,47 +1050,31 @@ } /** - * Convert an OpenVPN tls-version variable to mbed TLS format (i.e. a majo= r and - * minor ssl version number). + * Convert an OpenVPN tls-version variable to mbed TLS format * * @param tls_ver The tls-version variable to convert. - * @param major Returns the TLS major version in mbed TLS format. - * Must be a valid pointer. - * @param minor Returns the TLS minor version in mbed TLS format. - * Must be a valid pointer. + * + * @return Translated mbedTLS SSL version from OpenVPN TLS version. */ -static void -tls_version_to_major_minor(int tls_ver, int *major, int *minor) +mbedtls_ssl_protocol_version +tls_version_to_ssl_version(int tls_ver) { - ASSERT(major); - ASSERT(minor); switch (tls_ver) { -#if defined(MBEDTLS_SSL_PROTO_TLS1) - case TLS_VER_1_0: - *major =3D MBEDTLS_SSL_MAJOR_VERSION_3; - *minor =3D MBEDTLS_SSL_MINOR_VERSION_1; - break; -#endif - -#if defined(MBEDTLS_SSL_PROTO_TLS1_1) - case TLS_VER_1_1: - *major =3D MBEDTLS_SSL_MAJOR_VERSION_3; - *minor =3D MBEDTLS_SSL_MINOR_VERSION_2; - break; -#endif - #if defined(MBEDTLS_SSL_PROTO_TLS1_2) case TLS_VER_1_2: - *major =3D MBEDTLS_SSL_MAJOR_VERSION_3; - *minor =3D MBEDTLS_SSL_MINOR_VERSION_3; - break; + return MBEDTLS_SSL_VERSION_TLS1_2; +#endif + +#if defined(MBEDTLS_SSL_PROTO_TLS1_3) + case TLS_VER_1_3: + return MBEDTLS_SSL_VERSION_TLS1_3; #endif default: msg(M_FATAL, "%s: invalid or unsupported TLS version %d", __fu= nc__, tls_ver); - break; + return MBEDTLS_SSL_VERSION_UNKNOWN; } } @@ -1171,7 +1155,7 @@ if (ssl_ctx->groups) { - mbedtls_ssl_conf_curves(ks_ssl->ssl_config, ssl_ctx->groups); + mbedtls_ssl_conf_groups(ks_ssl->ssl_config, ssl_ctx->groups); } /* Disable TLS renegotiations if the mbedtls library supports that fea= ture. @@ -1221,15 +1205,14 @@ &SSLF_TLS_VERSION_MIN_MASK; /* default to TLS 1.2 */ - int major =3D MBEDTLS_SSL_MAJOR_VERSION_3; - int minor =3D MBEDTLS_SSL_MINOR_VERSION_3; + mbedtls_ssl_protocol_version version =3D MBEDTLS_SSL_VERSION_TLS1_= 2; if (configured_tls_version_min > TLS_VER_UNSPEC) { - tls_version_to_major_minor(configured_tls_version_min, &major,= &minor); + version =3D tls_version_to_ssl_version(configured_tls_version_= min); } - mbedtls_ssl_conf_min_version(ks_ssl->ssl_config, major, minor); + mbedtls_ssl_conf_min_tls_version(ks_ssl->ssl_config, version); } /* Initialize maximum TLS version */ @@ -1238,20 +1221,19 @@ (session->opt->ssl_flags >> SSLF_TLS_VERSION_MAX_SHIFT) &SSLF_TLS_VERSION_MAX_MASK; - int major =3D 0; - int minor =3D 0; + mbedtls_ssl_protocol_version version =3D MBEDTLS_SSL_VERSION_UNKNO= WN; if (configured_tls_version_max > TLS_VER_UNSPEC) { - tls_version_to_major_minor(configured_tls_version_max, &major,= &minor); + version =3D tls_version_to_ssl_version(configured_tls_version_= max); } else { /* Default to tls_version_max(). */ - tls_version_to_major_minor(tls_version_max(), &major, &minor); + version =3D tls_version_to_ssl_version(tls_version_max()); } - mbedtls_ssl_conf_max_version(ks_ssl->ssl_config, major, minor); + mbedtls_ssl_conf_max_tls_version(ks_ssl->ssl_config, version); } #if HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB diff --git a/src/openvpn/ssl_mbedtls.h b/src/openvpn/ssl_mbedtls.h index 1fd0ce8..34b4f02 100644 --- a/src/openvpn/ssl_mbedtls.h +++ b/src/openvpn/ssl_mbedtls.h @@ -39,6 +39,8 @@ #include #endif +#include "mbedtls_compat.h" + typedef struct _buffer_entry buffer_entry; struct _buffer_entry { @@ -118,7 +120,7 @@ #endif struct external_context external_key; /**< External key context */ int *allowed_ciphers; /**< List of allowed ciphers for this conn= ection */ - mbedtls_ecp_group_id *groups; /**< List of allowed groups for this= connection */ + mbedtls_compat_group_id *groups; /**< List of allowed groups for t= his connection */ mbedtls_x509_crt_profile cert_profile; /**< Allowed certificate types = */ }; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/681?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: If96c2ebd2af16b18ed34820e8c0531547e2076d9 Gerrit-Change-Number: 681 Gerrit-PatchSet: 1 Gerrit-Owner: MaxF Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: comododragon Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --Tvfeuv8FwoY= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

MaxF would like plaisthos and flichtenheld to review th= is change.

Vi= ew Change

Fix MBEDTLS_DEPRECATED_REMOVED build errors

This commi= t allows compiling OpenVPN with recent versions of mbed TLS
if MBEDTLS_D= EPRECATED_REMOVED is defined.

Also disables TLS 1.0 and 1.1 because = these are dropped in recent
versions of mbed TLS.

Change-Id: If96= c2ebd2af16b18ed34820e8c0531547e2076d9
Signed-off-by: Max Fillinger <m= aximilian.fillinger@...2507...>
---
M src/openvpn/mbedtls_compat.h=
M src/openvpn/ssl_mbedtls.c
M src/openvpn/ssl_mbedtls.h
3 files c= hanged, 72 insertions(+), 42 deletions(-)

git pull ssh://gerrit.o=
penvpn.net:29418/openvpn refs/changes/81/681/1
diff --git a/src/ope=
nvpn/mbedtls_compat.h b/src/openvpn/mbedtls_compat.h
index = d742b54..74dca22 100644
--- a/src/openvpn/mbedtls_compat.h<= /span>
+++ b/src/openvpn/mbedtls_compat.h
@@ -40,6= +40,7 @@
#include <mbedtls/cipher.h>
#include <mbedtls/ctr_drbg.h>

#include <mbedtl= s/dhm.h>
+#include= <mbedtls/ecp.h>
#include <mbedtls/md.h>
#include <mbedtls/pem.h>
#include <mb= edtls/pk.h>
@@ -51,6 +52,13 @@
#inc= lude <psa/crypto.h>
#endif
<= br>+#if MBEDTLS_VERSION_NUMBER = >=3D 0x03000000
+t= ypedef uint16_t mbedtls_compat_group_id;
+#else
+#define MBEDTLS_SSL_IANA_TLS_GROUP_NONE MBEDTLS_ECP_DP_NONE+typedef mbedtls_ecp_group_id= mbedtls_compat_group_id;
+#endif
+=
static inline void
mbedtls_compat_psa_crypto_in= it(void)
{
@@ -64,6 +72,16 @@
#endif /* HAVE_MBEDTLS_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRY= PTO_C) */

}

+static inline mbedtls_compat_group_id
= +mbedtls_compat_get_group_id(co= nst mbedtls_ecp_curve_info *curve_info)
+{
+#if MBEDTLS_VERSION_NUMBER >=3D 0x03000000
+ return curve_info->tls_id;
+#else
+ return curve_info->grp_id;
+#endif
+}
+
/*
* In older versions of mb= edtls, mbedtls_ctr_drbg_update() did not return an
* erro= r code, and it was deprecated in favor of mbedtls_ctr_drbg_update_ret()

@@ -124,6 +142,34 @@
}

#if MBEDTLS_VERSION_NUMBER < 0x03020100
+typedef enum {
+ MBEDTLS_SSL_VERSION_UNKNOWN, /*!< Con= text not in use or version not yet negotiated. */
+ MBEDTLS_SSL_VERSION_TLS1_2 =3D 0x0303, /*= !< (D)TLS 1.2 */
+= MBEDTLS_SSL_VERSION_TLS1_3 =3D 0x0304, /*!< (D)TLS 1.3 */
= +} mbedtls_ssl_protocol_version= ;
+
+static inline void
+mbedtls_ssl_conf_min_tls_version(mbedt= ls_ssl_config *conf, mbedtls_ssl_protocol_version tls_version)
+{

+ int major =3D (tls_version >> 8) & 0= xff;
+ int minor = =3D tls_version & 0xff;
+ mbedtls_ssl_conf_min_version(conf, major, minor);
+}
+
+static inline void
+mbedtls_ssl_conf_max_tls_version(mbedtls_ssl_config *conf, mbedtls_ssl_= protocol_version tls_version)
+{
+ int = major =3D (tls_version >> 8) & 0xff;
+ int minor =3D tls_version & 0xff;=
+ mbedtls_ssl_conf_max_v= ersion(conf, major, minor);
+}
++static inline void
= +mbedtls_ssl_conf_groups(mbedtl= s_ssl_config *conf, mbedtls_compat_group_id *groups)
+{
+ mbedtls_ssl_conf_curves(conf, groups);
+}
+
static inline size_t
mbedtls_cipher_info_get_block_size(const mbedtls_cipher_info_t *cipher)
{
diff --git a/src/openvpn/ssl_mbedtls.c b/= src/openvpn/ssl_mbedtls.c
index a68588e..545db0f 100644

--- a/src/openvpn/ssl_mbedtls.c
+++ b/src/open= vpn/ssl_mbedtls.c
@@ -402,7 +402,7 @@

/* Get number of groups and allocate an array in ctx */
int groups_count =3D get_num_elements(groups, ':= 9;);
- ALLOC_ARRAY_C= LEAR(ctx->groups, mbedtls_ecp_group_id, groups_count + 1)
+ ALLOC_ARRAY_CLEAR(ctx->grou= ps, mbedtls_compat_group_id, groups_count + 1)


= /* Parse allowed ciphers, getting IDs */
in= t i =3D 0;
@@ -419,11 +419,11 @@
}=
else
{
- ctx->groups[i] =3D ci-&g= t;grp_id;
+ = ctx->groups[i] =3D mbedtls_compat_get_group_id(ci);
= i++;
}
}- ctx->groups[i] =3D MBEDT= LS_ECP_DP_NONE;
+ = ctx->groups[i] =3D MBEDTLS_SSL_IANA_TLS_GROUP_NONE;

gc_free(&gc);
}
@@ = -1050,47 +1050,31 @@
}

= /**
- * Convert an Open= VPN tls-version variable to mbed TLS format (i.e. a major and
- * minor ssl version number).
+ * Convert an OpenVPN tls= -version variable to mbed TLS format
*
= * @param tls_ver The tls-version variable to convert.
- * @param major Returns the T= LS major version in mbed TLS format.

- * Must be a valid pointer.
<= span style=3D"color: hsl(0, 100%, 40%);">- * @param minor Returns t= he TLS minor version in mbed TLS format.
- * Must be a valid pointer.<= br>+ *
+ * @return Translated mbedTLS SSL version fr= om OpenVPN TLS version.
*/
-static void
-tls_version_to_major_minor(int tls_ver, int *major, int *minor= )
+mbedtls_ssl_protoc= ol_version
+tls_versi= on_to_ssl_version(int tls_ver)
{
- ASSERT(major);
- ASSERT(minor);

switch (tls_ver)
{
-#if defined(MBEDTLS_SSL_PROTO_TLS1)
- case TLS_VER_1_0:- *major =3D MBEDTLS_= SSL_MAJOR_VERSION_3;
- = *minor =3D MBEDTLS_SSL_MINOR_VERSION_1;
- break;
-#endif
-
-#if define= d(MBEDTLS_SSL_PROTO_TLS1_1)
- case TLS_VER_1_1:
- *major =3D MBEDTLS_SSL_MAJOR_VERSION_3;
- *minor =3D MBEDTLS_SSL_= MINOR_VERSION_2;
- = break;
-#endif
-
#if d= efined(MBEDTLS_SSL_PROTO_TLS1_2)
case TLS_VER_1_2:=
- *major = =3D MBEDTLS_SSL_MAJOR_VERSION_3;
- *minor =3D MBEDTLS_SSL_MINOR_VERSION_3;
- break;

+ return MBEDTLS_SSL_VERS= ION_TLS1_2;
+#endif
+
+#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
+ case TLS_VER_1= _3:
+ retu= rn MBEDTLS_SSL_VERSION_TLS1_3;
#endif

default:
msg(M_FATAL, &= quot;%s: invalid or unsupported TLS version %d", __func__, tls_ver);
- break;
+ return MBEDT= LS_SSL_VERSION_UNKNOWN;
}
}
@@ -1171,7 +1155,7 @@

= if (ssl_ctx->groups)
{
- mbedtls_ssl_conf_curves(ks_ssl-= >ssl_config, ssl_ctx->groups);
+ mbedtls_ssl_conf_groups(ks_ssl->ssl_config, ssl= _ctx->groups);
}

= /* Disable TLS renegotiations if the mbedtls library supports that fea= ture.
@@ -1221,15 +1205,14 @@
= &SSLF_TLS_VERSION_MIN_MASK;

= /* default to TLS 1.2 */
- int minor =3D MBEDTLS_SSL_MINOR_VER= SION_3;
+ mbed= tls_ssl_protocol_version version =3D MBEDTLS_SSL_VERSION_TLS1_2;
=
if (configured_tls_version_min > TLS_VE= R_UNSPEC)
{
- tls_version_to_major_minor(configured_tls_versio= n_min, &major, &minor);
+ version =3D tls_version_to_ssl_version(configured_t= ls_version_min);
}

- mbedtls_ssl_conf_min_versio= n(ks_ssl->ssl_config, major, minor);

+ mbedtls_ssl_conf_min_tls_version(ks_ssl->ssl= _config, version);
}

/* Initialize maximum TLS version */

@@ -1238,20 +122= 1,19 @@
(session->opt->ssl_flags >>= ; SSLF_TLS_VERSION_MAX_SHIFT)
&SSLF_TLS_VE= RSION_MAX_MASK;

- int major =3D 0;
- int minor =3D 0;
+ mbedtls_ssl_protocol_version version =3D MBEDTL= S_SSL_VERSION_UNKNOWN;

if (confi= gured_tls_version_max > TLS_VER_UNSPEC)
{

- tls_version_to_= major_minor(configured_tls_version_max, &major, &minor);
= + version =3D tls_ve= rsion_to_ssl_version(configured_tls_version_max);
= }
else
{
= /* Default to tls_version_max(). */
- tls_version_to_major_minor(tls_versi= on_max(), &major, &minor);
+ version =3D tls_version_to_ssl_version(tls_versi= on_max());
}

- mbedtls_ssl_conf_max_version(ks_s= sl->ssl_config, major, minor);
+ mbedtls_ssl_conf_max_tls_version(ks_ssl->ssl_confi= g, version);
}

#if = HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB
diff --git a/src/o= penvpn/ssl_mbedtls.h b/src/openvpn/ssl_mbedtls.h
index 1fd0= ce8..34b4f02 100644
--- a/src/openvpn/ssl_mbedtls.h<= br>+++ b/src/openvpn/ssl_mbedtls.h
@@ -39,6 +39,8 @@<= /span>
#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
#endif

+#include "mbedtls_compat.h"
+
typedef struct _bu= ffer_entry buffer_entry;

struct _buffer_= entry {
@@ -118,7 +120,7 @@
#endif<= br> struct external_context external_key; /**< External key co= ntext */
int *allowed_ciphers; /**< List of a= llowed ciphers for this connection */
- mbedtls_ecp_group_id *groups; /**< List of allow= ed groups for this connection */
+ mbedtls_compat_group_id *groups; /**< List of allow= ed groups for this connection */
mbedtls_x509_crt_prof= ile cert_profile; /**< Allowed certificate types */
};<= /span>


To view, visit change 681.= To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If96c2ebd2af16b18ed34820e8c0= 531547e2076d9
Gerrit-Change-Number: 681
Gerrit-PatchSet: 1
Gerrit-Owner: MaxF <max@...2606...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: comododragon <rein.vanbaaren@...493...= 1435...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newchange
--Tvfeuv8FwoY=--