From: "MaxF (Code Review)" Date: Tue, 17 Oct 2023 17:21:03 +0000 In-Reply-To: References: Reply-To: max@...2606..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <4bcd60f40e9054666b2f19776291c2c35e1ce7c2-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="n85yBNW0d4Q="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: Add compatibility functions for mbedtls 2.X.Y List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --n85yBNW0d4Q= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/371?usp=3Demail to look at the new patch set (#2). Change subject: Add compatibility functions for mbedtls 2.X.Y ...................................................................... Add compatibility functions for mbedtls 2.X.Y Change-Id: I143ca14c27c0c378b5e6b4184e80890d2c0e225e Signed-off-by: Max Fillinger --- M configure.ac M src/openvpn/crypto_mbedtls.c A src/openvpn/mbedtls_compat.h M src/openvpn/ssl_mbedtls.c M src/openvpn/ssl_verify_mbedtls.c 5 files changed, 220 insertions(+), 37 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/71/371/2 diff --git a/configure.ac b/configure.ac index 2072e8c..6658485 100644 --- a/configure.ac +++ b/configure.ac @@ -1025,6 +1025,12 @@ [AC_MSG_ERROR([mbed TLS version >=3D 2.16.12 or >=3D 3.2.1 required])] ) + AC_CHECK_HEADER( + psa/crypto.h, + [AC_DEFINE([MBEDTLS_HAVE_PSA_CRYPTO_H], [1], [yes])], + [AC_DEFINE([MBEDTLS_HAVE_PSA_CRYPTO_H], [0], [no])] + ) + AC_CHECK_FUNCS( [ \ mbedtls_cipher_write_tag \ diff --git a/src/openvpn/crypto_mbedtls.c b/src/openvpn/crypto_mbedtls.c index e85e4de..f244863 100644 --- a/src/openvpn/crypto_mbedtls.c +++ b/src/openvpn/crypto_mbedtls.c @@ -41,6 +41,7 @@ #include "integer.h" #include "crypto_backend.h" #include "otime.h" +#include "mbedtls_compat.h" #include "misc.h" #include diff --git a/src/openvpn/mbedtls_compat.h b/src/openvpn/mbedtls_compat.h new file mode 100644 index 0000000..07dfe99 --- /dev/null +++ b/src/openvpn/mbedtls_compat.h @@ -0,0 +1,162 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single TCP/UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2023 Fox Crypto B.V. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/** + * @file mbedtls compatibility stub + * + * This file provide compatibility stubs for the mbedtls libraries + * prior to version 3. This version made most fields in structs private + * and requires accessor functions to be used. For earlier versions, we + * implement the accessor functions here. + */ + +#ifndef MBEDTLS_COMPAT_H_ +#define MBEDTLS_COMPAT_H_ + +#include "errlevel.h" + +#include +#include +#include +#include +#include +#include +#include + +#if MBEDTLS_HAVE_PSA_CRYPTO_H + #include +#endif + +static inline void +mbedtls_compat_psa_crypto_init(void) +{ +#if MBEDTLS_HAVE_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C) + if (psa_crypto_init() !=3D PSA_SUCCESS) + { + msg(M_FATAL, "mbedtls: psa_crypto_init() failed"); + } +#else + return; +#endif /* MBEDTLS_HAVE_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C) */ +} + +static inline int +mbedtls_compat_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_= pk_context *prv, + int (*f_rng)(void *, unsigned char *, size_t)= , void *p_rng) +{ +#if MBEDTLS_VERSION_NUMBER < 0x03020100 + return mbedtls_pk_check_pair(pub, prv); +#else + return mbedtls_pk_check_pair(pub, prv, f_rng, p_rng); +#endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */ +} + +static inline int +mbedtls_compat_pk_parse_key(mbedtls_pk_context *ctx, + const unsigned char *key, size_t keylen, + const unsigned char *pwd, size_t pwdlen, + int (*f_rng)(void *, unsigned char *, size_t),= void *p_rng) +{ +#if MBEDTLS_VERSION_NUMBER < 0x03020100 + return mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen); +#else + return mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen, f_rng, p_rn= g); +#endif +} + +static inline int +mbedtls_compat_pk_parse_keyfile(mbedtls_pk_context *ctx, + const char *path, const char *password, + int (*f_rng)(void *, unsigned char *, size= _t), void *p_rng) +{ +#if MBEDTLS_VERSION_NUMBER < 0x03020100 + return mbedtls_pk_parse_keyfile(ctx, path, password); +#else + return mbedtls_pk_parse_keyfile(ctx, path, password, f_rng, p_rng); +#endif +} + +#if MBEDTLS_VERSION_NUMBER < 0x03020100 +static inline unsigned int +mbedtls_cipher_info_get_block_size(const mbedtls_cipher_info_t *cipher) +{ + return cipher->block_size; +} + +static inline unsigned int +mbedtls_cipher_info_get_iv_size(const mbedtls_cipher_info_t *cipher) +{ + return cipher->iv_size; +} + +static inline unsigned int +mbedtls_cipher_info_get_key_bitlen(const mbedtls_cipher_info_t *cipher) +{ + return cipher->key_bitlen; +} + +static inline mbedtls_cipher_mode_t +mbedtls_cipher_info_get_mode(const mbedtls_cipher_info_t *cipher) +{ + return cipher->mode; +} + +static inline const char * +mbedtls_cipher_info_get_name(const mbedtls_cipher_info_t *cipher) +{ + return cipher->name; +} + +static inline mbedtls_cipher_type_t +mbedtls_cipher_info_get_type(const mbedtls_cipher_info_t *cipher) +{ + return cipher->type; +} + +static inline size_t +mbedtls_dhm_get_bitlen(const mbedtls_dhm_context *ctx) +{ + return 8 * ctx->len; +} + +static inline const mbedtls_md_info_t * +mbedtls_md_info_from_ctx(const mbedtls_md_context_t *ctx) +{ + return ctx->md_info; +} + +static inline const unsigned char * +mbedtls_pem_get_buffer(const mbedtls_pem_context *ctx, size_t *buf_size) +{ + *buf_size =3D ctx->buflen; + return ctx->buf; +} + +static inline int +mbedtls_x509_crt_has_ext_type(const mbedtls_x509_crt *ctx, int ext_type) +{ + return ctx->ext_types & ext_type; +} +#endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */ + +#endif /* MBEDTLS_COMPAT_H_ */ diff --git a/src/openvpn/ssl_mbedtls.c b/src/openvpn/ssl_mbedtls.c index a4ed722..0a84682 100644 --- a/src/openvpn/ssl_mbedtls.c +++ b/src/openvpn/ssl_mbedtls.c @@ -41,6 +41,7 @@ #include "buffer.h" #include "misc.h" #include "manage.h" +#include "mbedtls_compat.h" #include "pkcs11_backend.h" #include "ssl_common.h" @@ -54,8 +55,6 @@ #include #include -#include - /** * Compatibility: mbedtls_ctr_drbg_update was deprecated in mbedtls 2.16 a= nd * replaced with mbedtls_ctr_drbg_update_ret, which returns an error code. @@ -106,7 +105,7 @@ void tls_init_lib(void) { - (void)psa_crypto_init(); + mbedtls_compat_psa_crypto_init(); } void @@ -503,40 +502,40 @@ if (priv_key_inline) { - status =3D mbedtls_pk_parse_key(ctx->priv_key, - (const unsigned char *) priv_key_fil= e, - strlen(priv_key_file) + 1, NULL, 0, - mbedtls_ctr_drbg_random, - rand_ctx_get()); + status =3D mbedtls_compat_pk_parse_key(ctx->priv_key, + (const unsigned char *) priv_= key_file, + strlen(priv_key_file) + 1, NU= LL, 0, + mbedtls_ctr_drbg_random, + rand_ctx_get()); if (MBEDTLS_ERR_PK_PASSWORD_REQUIRED =3D=3D status) { char passbuf[512] =3D {0}; pem_password_callback(passbuf, 512, 0, NULL); - status =3D mbedtls_pk_parse_key(ctx->priv_key, - (const unsigned char *) priv_key= _file, - strlen(priv_key_file) + 1, - (unsigned char *) passbuf, - strlen(passbuf), - mbedtls_ctr_drbg_random, - rand_ctx_get()); + status =3D mbedtls_compat_pk_parse_key(ctx->priv_key, + (const unsigned char *) p= riv_key_file, + strlen(priv_key_file) + 1, + (unsigned char *) passbuf, + strlen(passbuf), + mbedtls_ctr_drbg_random, + rand_ctx_get()); } } else { - status =3D mbedtls_pk_parse_keyfile(ctx->priv_key, - priv_key_file, - NULL, - mbedtls_ctr_drbg_random, - rand_ctx_get()); + status =3D mbedtls_compat_pk_parse_keyfile(ctx->priv_key, + priv_key_file, + NULL, + mbedtls_ctr_drbg_random, + rand_ctx_get()); if (MBEDTLS_ERR_PK_PASSWORD_REQUIRED =3D=3D status) { char passbuf[512] =3D {0}; pem_password_callback(passbuf, 512, 0, NULL); - status =3D mbedtls_pk_parse_keyfile(ctx->priv_key, - priv_key_file, passbuf, - mbedtls_ctr_drbg_random, - rand_ctx_get()); + status =3D mbedtls_compat_pk_parse_keyfile(ctx->priv_key, + priv_key_file, passbu= f, + mbedtls_ctr_drbg_rand= om, + rand_ctx_get()); } } if (!mbed_ok(status)) @@ -552,10 +551,10 @@ return 1; } - if (!mbed_ok(mbedtls_pk_check_pair(&ctx->crt_chain->pk, - ctx->priv_key, - mbedtls_ctr_drbg_random, - rand_ctx_get()))) + if (!mbed_ok(mbedtls_compat_pk_check_pair(&ctx->crt_chain->pk, + ctx->priv_key, + mbedtls_ctr_drbg_random, + rand_ctx_get()))) { msg(M_WARN, "Private key does not match the certificate"); return 1; @@ -585,6 +584,9 @@ static inline int external_pkcs1_sign( void *ctx_voidptr, int (*f_rng)(void *, unsigned char *, size_t), void *= p_rng, +#if MBEDTLS_VERSION_NUMBER < 0x03020100 + int mode, +#endif mbedtls_md_type_t md_alg, unsigned int hashlen, const= unsigned char *hash, unsigned char *sig ) { @@ -599,6 +601,13 @@ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; } +#if MBEDTLS_VERSION_NUMBER < 0x03020100 + if (MBEDTLS_RSA_PRIVATE !=3D mode) + { + return MBEDTLS_RSA_BAD_INPUT_DATA; + } +#endif + /* * Support a wide range of hashes. TLSv1.1 and before only need SIG_RS= A_RAW, * but TLSv1.2 needs the full suite of hashes. @@ -1173,7 +1182,7 @@ /* Initialize minimum TLS version */ { - const int tls_version_min =3D + const int configured_tls_version_min =3D (session->opt->ssl_flags >> SSLF_TLS_VERSION_MIN_SHIFT) &SSLF_TLS_VERSION_MIN_MASK; @@ -1181,9 +1190,9 @@ int major =3D MBEDTLS_SSL_MAJOR_VERSION_3; int minor =3D MBEDTLS_SSL_MINOR_VERSION_3; - if (tls_version_min > TLS_VER_UNSPEC) + if (configured_tls_version_min > TLS_VER_UNSPEC) { - tls_version_to_major_minor(tls_version_min, &major, &minor); + tls_version_to_major_minor(configured_tls_version_min, &major,= &minor); } mbedtls_ssl_conf_min_version(ks_ssl->ssl_config, major, minor); @@ -1191,17 +1200,21 @@ /* Initialize maximum TLS version */ { - const int tls_version_max =3D + const int configured_tls_version_max =3D (session->opt->ssl_flags >> SSLF_TLS_VERSION_MAX_SHIFT) &SSLF_TLS_VERSION_MAX_MASK; - /* default to TLS 1.3 */ - int major =3D MBEDTLS_SSL_MAJOR_VERSION_3; - int minor =3D MBEDTLS_SSL_MINOR_VERSION_4; + int major =3D 0; + int minor =3D 0; - if (tls_version_max > TLS_VER_UNSPEC) + if (configured_tls_version_max > TLS_VER_UNSPEC) { - tls_version_to_major_minor(tls_version_max, &major, &minor); + tls_version_to_major_minor(configured_tls_version_max, &major,= &minor); + } + else + { + /* Default to tls_version_max(). */ + tls_version_to_major_minor(tls_version_max(), &major, &minor); } mbedtls_ssl_conf_max_version(ks_ssl->ssl_config, major, minor); diff --git a/src/openvpn/ssl_verify_mbedtls.c b/src/openvpn/ssl_verify_mbed= tls.c index 33c3769..ce21324 100644 --- a/src/openvpn/ssl_verify_mbedtls.c +++ b/src/openvpn/ssl_verify_mbedtls.c @@ -35,6 +35,7 @@ #if defined(ENABLE_CRYPTO_MBEDTLS) #include "crypto_mbedtls.h" +#include "mbedtls_compat.h" #include "ssl_verify.h" #include #include -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/371?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: I143ca14c27c0c378b5e6b4184e80890d2c0e225e Gerrit-Change-Number: 371 Gerrit-PatchSet: 2 Gerrit-Owner: MaxF Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --n85yBNW0d4Q= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

MaxF uploaded patch set #2 to this change.

View Change=

Add compatibility functions for mbedtls 2.X.Y

Change-Id: I143ca14c2= 7c0c378b5e6b4184e80890d2c0e225e
Signed-off-by: Max Fillinger <max@...493...= 2606...>
---
M configure.ac
M src/openvpn/crypto_mbedtls.c
A= src/openvpn/mbedtls_compat.h
M src/openvpn/ssl_mbedtls.c
M src/openv= pn/ssl_verify_mbedtls.c
5 files changed, 220 insertions(+), 37 deletions= (-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/=
71/371/2
diff --git a/configure.ac b/configure.ac
i= ndex 2072e8c..6658485 100644
--- a/configure.ac
<= span>+++ b/configure.ac
@@ -1025,6 +1025,12 @@
[AC_MSG_ERROR([mbed TLS version >=3D 2.16.12 or >=3D 3.2.1 req= uired])]
)

+ AC_CHECK_HEADER(
+ psa/crypto.h,
+ [AC_DEFINE([MBEDTLS_HAVE_PSA_CRYPTO_H= ], [1], [yes])],
+ = [AC_DEFINE([MBEDTLS_HAVE_PSA_CRYPTO_H], [0], [no])]
+ )
+
AC_CHECK_FUNCS(
= [ \
mbedtls_cipher_write_tag \
diff -= -git a/src/openvpn/crypto_mbedtls.c b/src/openvpn/crypto_mbedtls.cindex e85e4de..f244863 100644
--- a/src/openvpn/cry= pto_mbedtls.c
+++ b/src/openvpn/crypto_mbedtls.c
= @@ -41,6 +41,7 @@
#include "integer.h"
#include "crypto_backend.h"
#inclu= de "otime.h"
+/*
+ * OpenVPN -- An application to securely tunnel IP = networks
+ * = over a single TCP/UDP port, with support for SSL/TLS-based
+ * session authentic= ation and key exchange,

+ * packet encryption, packet authentication, and
= + * packet compress= ion.
+ *
+ * Copyright (C) 2023 Fox Crypt= o B.V. <openvpn@...2507...>
+ *
+ * = This program is free software; you can redistribute it and/or modify
+ * it under the terms of= the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the h= ope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of<= /span>
+ * MERCHANTABILITY = or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.<= /span>
+ *
+ * You should have received a copy o= f the GNU General Public License along
+ * with this program; if not, write to the Free Softwar= e Foundation, Inc.,
+= * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
<= span style=3D"color: hsl(120, 100%, 40%);">+ */

+
+/**
+ * @fil= e mbedtls compatibility stub
+ *
+ * This = file provide compatibility stubs for the mbedtls libraries
+ * prior to version 3. This version = made most fields in structs private
+ * and requires accessor functions to be used. For earlier = versions, we
+ * impl= ement the accessor functions here.
+ */
+<= /span>
+#ifndef MBEDTLS_COMP= AT_H_
+#define MBEDTL= S_COMPAT_H_
+<= br>+#include "errlevel.h&q= uot;
+
+#include <mbedtls/cipher.h><= /span>
+#include <mbedtls= /dhm.h>
+#include = <mbedtls/md.h>
= +#include <mbedtls/pem.h>
+#include <mbedtls/pk.h>
+#include <mbedtls/version.h>
+#include <mbedtls/x509_crt.h>

+
+#if MBEDTLS_HAVE_PSA_CRYPTO_H
<= span style=3D"color: hsl(120, 100%, 40%);">+ #include <psa/crypto.h&g= t;
+#endif
= +
+static inline void
+mbedtls_compat_psa_crypto_init(void)
+{
+#if MBEDTLS_HAVE_PSA_CRYPTO_H && defined(MB= EDTLS_PSA_CRYPTO_C)
+= if (psa_crypto_init() !=3D PSA_SUCCESS)
+ {
+ msg(M_FATAL, "mbedtls: psa_crypto_init() failed"= ;);
+ }
= +#else
+ return;
+#endif /* MBEDTLS_HAVE_PSA_CRYPTO_H && defin= ed(MBEDTLS_PSA_CRYPTO_C) */
+}
++static inline int
<= span style=3D"color: hsl(120, 100%, 40%);">+mbedtls_compat_pk_check_pair(co= nst mbedtls_pk_context *pub, const mbedtls_pk_context *prv,
+ int (*= f_rng)(void *, unsigned char *, size_t), void *p_rng)
+{
+#if MBEDTLS_VERSION_NUMBER < 0x03020100
+ return mbedtls_pk_check_pair(pub= , prv);
+#else=
+ return mbedtls_pk_chec= k_pair(pub, prv, f_rng, p_rng);
+#endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */
<= span style=3D"color: hsl(120, 100%, 40%);">+}
+
+static inline int
+mbedtls_compat_pk_parse_key(mbedtls_pk_context *ctx,
+ const uns= igned char *key, size_t keylen,
+ const unsigned char *pwd, size_t pw= dlen,
+ = int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
+{
+#if MBEDTLS_VERSION_NUMBER < 0x03020= 100
+ return mbedt= ls_pk_parse_key(ctx, key, keylen, pwd, pwdlen);
+#else
+ return mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen= , f_rng, p_rng);
+#en= dif
+}
+

+static inline int
+mbedtls_compat_pk_parse_keyfile(mbedtls_pk_context *ctx= ,
+ = const char *path, const char *password,
+ int (*f_r= ng)(void *, unsigned char *, size_t), void *p_rng)
+{
+#if MBEDTLS_VERSION_NUMBER < 0x03020100
+ return mbedtls_pk_parse_keyfile(ctx= , path, password);
+#= else
+ return mbed= tls_pk_parse_keyfile(ctx, path, password, f_rng, p_rng);
+#endif
+}
+
+#if MBEDTLS_V= ERSION_NUMBER < 0x03020100
+static inline unsigned int
+mbedtls_cipher_info_get_block_size(const mbedtls_cipher_in= fo_t *cipher)
+{
+ return cipher->bl= ock_size;
+}+
+static inline unsigned int
+mbedtls_cipher_info_get_iv_size(const mb= edtls_cipher_info_t *cipher)
+{
+ retur= n cipher->iv_size;
+}
+
+static inline unsigned int
+mbedtls_cipher_info_get_key_= bitlen(const mbedtls_cipher_info_t *cipher)
+{
+ return cipher->key_bitlen;
+}
+
+static inline mbe= dtls_cipher_mode_t
+m= bedtls_cipher_info_get_mode(const mbedtls_cipher_info_t *cipher)
= +{
+ return cipher->mode;
+}
+
+st= atic inline const char *
+mbedtls_cipher_info_get_name(const mbedtls_cipher_info_t *cipher)
+{
+ return cipher->name;
+}

+
+static inline mbedtls_cipher_type_t
+mbedtls_cipher_info_get_type(const mbedtls_cipher_info= _t *cipher)
+{=
+ return cipher->type= ;
+}
+
+static inline size_t
+mbedtls_dhm_get_bitlen(const mbedtls_dhm_context *ctx)=
+{
+ return 8 * ctx->len;+}
+
+static inline const mbedtls_md_info_t *
+mbedtls_md_info_from_ctx(const mbedtls_md_cont= ext_t *ctx)
+{=
+ return ctx->md_info= ;
+}
+
+static inline const unsigned char *
+mbedtls_pem_get_buffer(const mbedtls_pe= m_context *ctx, size_t *buf_size)
+{
+ = *buf_size =3D ctx->buflen;
+ return ctx->buf;
+}
+

+static inline int
+mbedtls_x509_crt_has_ext_= type(const mbedtls_x509_crt *ctx, int ext_type)
+{
+ return ctx->ext_types & ext_type;
+}
+#endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */=
+
+#endif /* MBEDTLS_COMPAT_H_ */
diff --git a/src/openvpn/ssl_mbedtls.c b/src/openvpn/ssl_mbedtls.c
=
index a4ed722..0a84682 100644
--- a/src/openvpn/s= sl_mbedtls.c
+++ b/src/openvpn/ssl_mbedtls.c
@@ -41,6 +41,7 @@
#include "buffer.h" #include "misc.h"
#include "manage= .h"
+#include &q= uot;mbedtls_compat.h"
#include "pkcs11_backend.h= "
#include "ssl_common.h"
=
@@ -54,8 +55,6 @@
#include <mbedtls/oi= d.h>
#include <mbedtls/pem.h>

-#include <psa/crypto= .h>
-
/**

* Compatibility: mbedtls_ctr_drbg_update was depre= cated in mbedtls 2.16 and
* replaced with mbedtls_ctr_drb= g_update_ret, which returns an error code.
@@ -106,7 +105,7= @@
void
tls_init_lib(void)
{
- (void)psa_cr= ypto_init();
+ mbe= dtls_compat_psa_crypto_init();
}
<= br> void
@@ -503,40 +502,40 @@

if (priv_key_inline)
{
- status =3D mbedtls_pk_parse_= key(ctx->priv_key,
-= (const unsigned char *) priv_key_file= ,
- = strlen(priv_key_file) + 1, NULL, 0,
- mbedt= ls_ctr_drbg_random,
- = rand_ctx_get());
+ status =3D mbedtls_compat_pk_pars= e_key(ctx->priv_key,
+ (const unsigned char *) pr= iv_key_file,
+ = strlen(priv_key_file) + 1, NULL, 0,
+ = mbedtls_ctr_drbg_random,
+ r= and_ctx_get());

if (MBEDTLS_ERR_= PK_PASSWORD_REQUIRED =3D=3D status)
{
char passbuf[512] =3D {0};

pe= m_password_callback(passbuf, 512, 0, NULL);
- status =3D mbedtls_pk_parse_key(ctx->p= riv_key,
- = (const unsigned char *) priv_key_file,<= br>- = strlen(priv_key_file) + 1,
- (unsigned char = *) passbuf,
- = strlen(passbuf),
- mbedtl= s_ctr_drbg_random,
- = rand_ctx_get());
+ status =3D mbedtls_compat_p= k_parse_key(ctx->priv_key,
+ (const unsigned = char *) priv_key_file,
+ = (unsigned char *) passbuf,
+ = strlen(passbuf),
+ mbedtls_ctr_drb= g_random,
+ = rand_ctx_get());
= }
}
else
= {
- status = =3D mbedtls_pk_parse_keyfile(ctx->priv_key,
- priv_key= _file,
- = NULL,
- mbedtls_ctr_drbg_rando= m,
- = rand_ctx_get());
+ status =3D mbedtls_compat_pk_parse_keyfile(ctx-= >priv_key,
+ = priv_key_file,
+ = NULL,
+ = mbedtls_ctr_drbg_random,
+ = rand_ctx_get());
if (MBE= DTLS_ERR_PK_PASSWORD_REQUIRED =3D=3D status)
{
char passbuf[512] =3D {0};
= pem_password_callback(passbuf, 512, 0, NULL);
- status =3D mbedtls_pk_parse_key= file(ctx->priv_key,
= - priv_key_file, passbuf,

- = mbedtls_ctr_drbg_random,
- rand_c= tx_get());
+ = status =3D mbedtls_compat_pk_parse_keyfile(ctx->priv_key,
<= span style=3D"color: hsl(120, 100%, 40%);">+ = priv_key_file, passbuf,
+ = mbedtls_ctr_drbg_random,
+ rand_ctx_get(= ));
}
}
= if (!mbed_ok(status))
@@ -552,10 +551,10 @@
return 1;
}

- if (!mbed_ok(mbedtls_pk_check_pa= ir(&ctx->crt_chain->pk,
- ctx->priv_key,=
- = mbedtls_ctr_drbg_random,
- rand_ctx_get())))

+ if (!mbed_ok(mbedtls= _compat_pk_check_pair(&ctx->crt_chain->pk,
+ = ctx->priv_key,
+ = rand_ctx_get())))
{ msg(M_WARN, "Private key does not match the certificat= e");
return 1;
@@ -585,6 +584= ,9 @@
static inline int
external_pkcs1_si= gn( void *ctx_voidptr,
int (*f_rng)(v= oid *, unsigned char *, size_t), void *p_rng,
+#if MBEDTLS_VERSION_NUMBER < 0x03020100=
+ int m= ode,
+#endif mbedtls_md_type_t md_alg, unsigned int hashle= n, const unsigned char *hash,
unsigne= d char *sig )
{
@@ -599,6 +601,13 @@

return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
= }

+#if MBEDTLS_VERSION_NUMBER < 0x03020100
+ if (MBEDTLS_RSA_PRIVATE !=3D mode)+ {
+ return MBEDTLS_RSA_BAD_INPUT_DATA= ;
+ }
+#endif
+
/*
= * Support a wide range of hashes. TLSv1.1 and before only need SIG_RSA_RA= W,
* but TLSv1.2 needs the full suite of hashes.
@@ -1173,7 +1182,7 @@

/*= Initialize minimum TLS version */
{
- const int tls_version_min =3D
+ const int con= figured_tls_version_min =3D
(session->opt-&= gt;ssl_flags >> SSLF_TLS_VERSION_MIN_SHIFT)
= &SSLF_TLS_VERSION_MIN_MASK;

@@ -1= 181,9 +1190,9 @@
int major =3D MBEDTLS_SSL_MAJOR_V= ERSION_3;
int minor =3D MBEDTLS_SSL_MINOR_VERSION_= 3;

- = if (tls_version_min > TLS_VER_UNSPEC)
+ if (configured_tls_version_min > TLS= _VER_UNSPEC)
{
- tls_version_to_major_minor(tls_version_min, &= amp;major, &minor);
+ tls_version_to_major_minor(configured_tls_version_min, &= ;major, &minor);
}
mbedtls_ssl_conf_min_version(ks_ssl->ssl_config, major, = minor);
@@ -1191,17 +1200,21 @@
/* Initialize maximum TLS version */
{
- const int tls_ver= sion_max =3D
+ = const int configured_tls_version_max =3D
(ses= sion->opt->ssl_flags >> SSLF_TLS_VERSION_MAX_SHIFT)
<= span> &SSLF_TLS_VERSION_MAX_MASK;

<= br>- /* default to TLS 1.3= */
- int major = =3D MBEDTLS_SSL_MAJOR_VERSION_3;
- int minor =3D MBEDTLS_SSL_MINOR_VERSION_4;
+ int major =3D 0;
+ int minor =3D 0;

- = if (tls_version_max > TLS_VER_UNSPEC)
+ if (configured_tls_version_max > TLS_VER_U= NSPEC)
{
- tls_version_to_major_minor(tls_version_max, &ma= jor, &minor);
+ = tls_version_to_major_minor(configured_tls_version_max, &major= , &minor);
+ = }
+ else

+ {
+ /* Default to tls_ve= rsion_max(). */
+ = tls_version_to_major_minor(tls_version_max(), &major, &mino= r);
}

m= bedtls_ssl_conf_max_version(ks_ssl->ssl_config, major, minor);diff --git a/src/openvpn/ssl_verify_mbedtls.c b/src/openvpn/ssl_veri= fy_mbedtls.c
index 33c3769..ce21324 100644
= --- a/src/openvpn/ssl_verify_mbedtls.c
+++ b/src/openvpn/ss= l_verify_mbedtls.c
@@ -35,6 +35,7 @@
#if d= efined(ENABLE_CRYPTO_MBEDTLS)

#include &= quot;crypto_mbedtls.h"
+#include "mbedtls_compat.h"
#include &qu= ot;ssl_verify.h"
#include <mbedtls/asn1.h>
#include <mbedtls/error.h>

To view, visit change 371. To unsubscribe, or for help writing mail filte= rs, visit settings.

<= div itemscope itemtype=3D"http://schema.org/EmailMessage">
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I143ca14c27c0c378b5e6b4184e8= 0890d2c0e225e
Gerrit-Change-Number: 371
Gerrit-PatchSet: 2
Gerrit-Owner: MaxF <max@...2606...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
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: newpatchset
--n85yBNW0d4Q=--