Skip to content

Commit 0169b4a

Browse files
rein.vanbaarencron2
authored andcommitted
Fix MBEDTLS_DEPRECATED_REMOVED build errors
This commit allows compiling OpenVPN with recent versions of mbed TLS if MBEDTLS_DEPRECATED_REMOVED is defined. Cherry-pick note: - Adapt to still support TLS 1.0 and 1.1 which were removed in master. Change-Id: If96c2ebd2af16b18ed34820e8c0531547e2076d9 Signed-off-by: Max Fillinger <maximilian.fillinger@foxcrypto.com> Acked-by: Arne Schwabe <arne-openvpn@rfc2549.org> Message-Id: <20240618120127.4564-1-gert@greenie.muc.de> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg28771.html Signed-off-by: Gert Doering <gert@greenie.muc.de> Acked-by: Gert Doering <gert@greenie.muc.de> (cherry picked from commit 8eb397d) Message-Id: <20250429154923.20921-1-gert@greenie.muc.de> URL: https://www.mail-archive.com/search?l=mid&q=20250429154923.20921-1-gert@greenie.muc.de Signed-off-by: Gert Doering <gert@greenie.muc.de>
1 parent 6ca0fb4 commit 0169b4a

File tree

3 files changed

+79
-35
lines changed

3 files changed

+79
-35
lines changed

src/openvpn/mbedtls_compat.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <mbedtls/cipher.h>
4141
#include <mbedtls/ctr_drbg.h>
4242
#include <mbedtls/dhm.h>
43+
#include <mbedtls/ecp.h>
4344
#include <mbedtls/md.h>
4445
#include <mbedtls/pem.h>
4546
#include <mbedtls/pk.h>
@@ -51,6 +52,12 @@
5152
#include <psa/crypto.h>
5253
#endif
5354

55+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
56+
typedef uint16_t mbedtls_compat_group_id;
57+
#else
58+
typedef mbedtls_ecp_group_id mbedtls_compat_group_id;
59+
#endif
60+
5461
static inline void
5562
mbedtls_compat_psa_crypto_init(void)
5663
{
@@ -64,6 +71,16 @@ mbedtls_compat_psa_crypto_init(void)
6471
#endif /* HAVE_MBEDTLS_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C) */
6572
}
6673

74+
static inline mbedtls_compat_group_id
75+
mbedtls_compat_get_group_id(const mbedtls_ecp_curve_info *curve_info)
76+
{
77+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
78+
return curve_info->tls_id;
79+
#else
80+
return curve_info->grp_id;
81+
#endif
82+
}
83+
6784
/*
6885
* In older versions of mbedtls, mbedtls_ctr_drbg_update() did not return an
6986
* error code, and it was deprecated in favor of mbedtls_ctr_drbg_update_ret()
@@ -124,6 +141,36 @@ mbedtls_compat_pk_parse_keyfile(mbedtls_pk_context *ctx,
124141
}
125142

126143
#if MBEDTLS_VERSION_NUMBER < 0x03020100
144+
typedef enum {
145+
MBEDTLS_SSL_VERSION_UNKNOWN, /*!< Context not in use or version not yet negotiated. */
146+
MBEDTLS_SSL_VERSION_TLS1_0 = 0x0301, /*!< (D)TLS 1.0 */
147+
MBEDTLS_SSL_VERSION_TLS1_1 = 0x0302, /*!< (D)TLS 1.1 */
148+
MBEDTLS_SSL_VERSION_TLS1_2 = 0x0303, /*!< (D)TLS 1.2 */
149+
MBEDTLS_SSL_VERSION_TLS1_3 = 0x0304, /*!< (D)TLS 1.3 */
150+
} mbedtls_ssl_protocol_version;
151+
152+
static inline void
153+
mbedtls_ssl_conf_min_tls_version(mbedtls_ssl_config *conf, mbedtls_ssl_protocol_version tls_version)
154+
{
155+
int major = (tls_version >> 8) & 0xff;
156+
int minor = tls_version & 0xff;
157+
mbedtls_ssl_conf_min_version(conf, major, minor);
158+
}
159+
160+
static inline void
161+
mbedtls_ssl_conf_max_tls_version(mbedtls_ssl_config *conf, mbedtls_ssl_protocol_version tls_version)
162+
{
163+
int major = (tls_version >> 8) & 0xff;
164+
int minor = tls_version & 0xff;
165+
mbedtls_ssl_conf_max_version(conf, major, minor);
166+
}
167+
168+
static inline void
169+
mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf, mbedtls_compat_group_id *groups)
170+
{
171+
mbedtls_ssl_conf_curves(conf, groups);
172+
}
173+
127174
static inline size_t
128175
mbedtls_cipher_info_get_block_size(const mbedtls_cipher_info_t *cipher)
129176
{

src/openvpn/ssl_mbedtls.c

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ tls_ctx_set_tls_groups(struct tls_root_ctx *ctx, const char *groups)
401401

402402
/* Get number of groups and allocate an array in ctx */
403403
int groups_count = get_num_elements(groups, ':');
404-
ALLOC_ARRAY_CLEAR(ctx->groups, mbedtls_ecp_group_id, groups_count + 1)
404+
ALLOC_ARRAY_CLEAR(ctx->groups, mbedtls_compat_group_id, groups_count + 1)
405405

406406
/* Parse allowed ciphers, getting IDs */
407407
int i = 0;
@@ -418,11 +418,15 @@ tls_ctx_set_tls_groups(struct tls_root_ctx *ctx, const char *groups)
418418
}
419419
else
420420
{
421-
ctx->groups[i] = ci->grp_id;
421+
ctx->groups[i] = mbedtls_compat_get_group_id(ci);
422422
i++;
423423
}
424424
}
425-
ctx->groups[i] = MBEDTLS_ECP_DP_NONE;
425+
426+
/* Recent mbedtls versions state that the list of groups must be terminated
427+
* with 0. Older versions state that it must be terminated with MBEDTLS_ECP_DP_NONE
428+
* which is also 0, so this works either way. */
429+
ctx->groups[i] = 0;
426430

427431
gc_free(&gc);
428432
}
@@ -1049,47 +1053,40 @@ tls_version_max(void)
10491053
}
10501054

10511055
/**
1052-
* Convert an OpenVPN tls-version variable to mbed TLS format (i.e. a major and
1053-
* minor ssl version number).
1056+
* Convert an OpenVPN tls-version variable to mbed TLS format
10541057
*
10551058
* @param tls_ver The tls-version variable to convert.
1056-
* @param major Returns the TLS major version in mbed TLS format.
1057-
* Must be a valid pointer.
1058-
* @param minor Returns the TLS minor version in mbed TLS format.
1059-
* Must be a valid pointer.
1059+
*
1060+
* @return Translated mbedTLS SSL version from OpenVPN TLS version.
10601061
*/
1061-
static void
1062-
tls_version_to_major_minor(int tls_ver, int *major, int *minor)
1062+
static mbedtls_ssl_protocol_version
1063+
tls_version_to_ssl_version(int tls_ver)
10631064
{
1064-
ASSERT(major);
1065-
ASSERT(minor);
1066-
10671065
switch (tls_ver)
10681066
{
10691067
#if defined(MBEDTLS_SSL_PROTO_TLS1)
10701068
case TLS_VER_1_0:
1071-
*major = MBEDTLS_SSL_MAJOR_VERSION_3;
1072-
*minor = MBEDTLS_SSL_MINOR_VERSION_1;
1073-
break;
1069+
return MBEDTLS_SSL_VERSION_TLS1_0;
10741070
#endif
10751071

10761072
#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
10771073
case TLS_VER_1_1:
1078-
*major = MBEDTLS_SSL_MAJOR_VERSION_3;
1079-
*minor = MBEDTLS_SSL_MINOR_VERSION_2;
1080-
break;
1074+
return MBEDTLS_SSL_VERSION_TLS1_1;
10811075
#endif
10821076

10831077
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10841078
case TLS_VER_1_2:
1085-
*major = MBEDTLS_SSL_MAJOR_VERSION_3;
1086-
*minor = MBEDTLS_SSL_MINOR_VERSION_3;
1087-
break;
1079+
return MBEDTLS_SSL_VERSION_TLS1_2;
1080+
#endif
1081+
1082+
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1083+
case TLS_VER_1_3:
1084+
return MBEDTLS_SSL_VERSION_TLS1_3;
10881085
#endif
10891086

10901087
default:
10911088
msg(M_FATAL, "%s: invalid or unsupported TLS version %d", __func__, tls_ver);
1092-
break;
1089+
return MBEDTLS_SSL_VERSION_UNKNOWN;
10931090
}
10941091
}
10951092

@@ -1170,7 +1167,7 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,
11701167

11711168
if (ssl_ctx->groups)
11721169
{
1173-
mbedtls_ssl_conf_curves(ks_ssl->ssl_config, ssl_ctx->groups);
1170+
mbedtls_ssl_conf_groups(ks_ssl->ssl_config, ssl_ctx->groups);
11741171
}
11751172

11761173
/* Disable TLS renegotiations if the mbedtls library supports that feature.
@@ -1220,15 +1217,14 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,
12201217
&SSLF_TLS_VERSION_MIN_MASK;
12211218

12221219
/* default to TLS 1.2 */
1223-
int major = MBEDTLS_SSL_MAJOR_VERSION_3;
1224-
int minor = MBEDTLS_SSL_MINOR_VERSION_3;
1220+
mbedtls_ssl_protocol_version version = MBEDTLS_SSL_VERSION_TLS1_2;
12251221

12261222
if (configured_tls_version_min > TLS_VER_UNSPEC)
12271223
{
1228-
tls_version_to_major_minor(configured_tls_version_min, &major, &minor);
1224+
version = tls_version_to_ssl_version(configured_tls_version_min);
12291225
}
12301226

1231-
mbedtls_ssl_conf_min_version(ks_ssl->ssl_config, major, minor);
1227+
mbedtls_ssl_conf_min_tls_version(ks_ssl->ssl_config, version);
12321228
}
12331229

12341230
/* Initialize maximum TLS version */
@@ -1237,20 +1233,19 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,
12371233
(session->opt->ssl_flags >> SSLF_TLS_VERSION_MAX_SHIFT)
12381234
&SSLF_TLS_VERSION_MAX_MASK;
12391235

1240-
int major = 0;
1241-
int minor = 0;
1236+
mbedtls_ssl_protocol_version version = MBEDTLS_SSL_VERSION_UNKNOWN;
12421237

12431238
if (configured_tls_version_max > TLS_VER_UNSPEC)
12441239
{
1245-
tls_version_to_major_minor(configured_tls_version_max, &major, &minor);
1240+
version = tls_version_to_ssl_version(configured_tls_version_max);
12461241
}
12471242
else
12481243
{
12491244
/* Default to tls_version_max(). */
1250-
tls_version_to_major_minor(tls_version_max(), &major, &minor);
1245+
version = tls_version_to_ssl_version(tls_version_max());
12511246
}
12521247

1253-
mbedtls_ssl_conf_max_version(ks_ssl->ssl_config, major, minor);
1248+
mbedtls_ssl_conf_max_tls_version(ks_ssl->ssl_config, version);
12541249
}
12551250

12561251
#if HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB

src/openvpn/ssl_mbedtls.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
4040
#endif
4141

42+
#include "mbedtls_compat.h"
43+
4244
typedef struct _buffer_entry buffer_entry;
4345

4446
struct _buffer_entry {
@@ -118,7 +120,7 @@ struct tls_root_ctx {
118120
#endif
119121
struct external_context external_key; /**< External key context */
120122
int *allowed_ciphers; /**< List of allowed ciphers for this connection */
121-
mbedtls_ecp_group_id *groups; /**< List of allowed groups for this connection */
123+
mbedtls_compat_group_id *groups; /**< List of allowed groups for this connection */
122124
mbedtls_x509_crt_profile cert_profile; /**< Allowed certificate types */
123125
};
124126

0 commit comments

Comments
 (0)