Skip to content

Commit 998cf95

Browse files
bsergeanclaude
andauthored
Fix mbedTLS 4.x compatibility: guard removed APIs behind version checks (#579)
mbedTLS 4.0 removed ctr_drbg/entropy headers and APIs in favour of the PSA Crypto RNG, and dropped the rng parameters from mbedtls_pk_parse_keyfile and mbedtls_ssl_conf_rng. Guard all affected includes and call-sites with MBEDTLS_VERSION_MAJOR < 4 preprocessor checks, and replace runtime version if-checks with compile-time #if for psa_crypto_init / mbedtls_psa_crypto_free. Fixes #577 Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a1f85b1 commit 998cf95

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

ixwebsocket/IXSocketMbedTLS.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ namespace ix
4242

4343
mbedtls_ssl_init(&_ssl);
4444
mbedtls_ssl_config_init(&_conf);
45+
#if MBEDTLS_VERSION_MAJOR < 4
4546
mbedtls_ctr_drbg_init(&_ctr_drbg);
4647
mbedtls_entropy_init(&_entropy);
48+
#endif
4749
mbedtls_x509_crt_init(&_cacert);
4850
mbedtls_x509_crt_init(&_cert);
4951
mbedtls_pk_init(&_pkey);
50-
// Initialize the PSA Crypto API if required by the version of Mbed TLS (3.6.0).
51-
// This allows the X.509/TLS libraries to use PSA for crypto operations.
52+
// Initialize the PSA Crypto API for mbedTLS 3.6+ and all 4.x releases.
5253
// See: https://github.com/Mbed-TLS/mbedtls/blob/development/docs/use-psa-crypto.md
53-
if (MBEDTLS_VERSION_MAJOR >= 3 && MBEDTLS_VERSION_MINOR >= 6 && MBEDTLS_VERSION_PATCH >= 0)
54-
{
55-
psa_crypto_init();
56-
}
54+
#if MBEDTLS_VERSION_MAJOR >= 4 || (MBEDTLS_VERSION_MAJOR == 3 && MBEDTLS_VERSION_MINOR >= 6)
55+
psa_crypto_init();
56+
#endif
5757
}
5858

5959
bool SocketMbedTLS::loadSystemCertificates(std::string& errorMsg)
@@ -112,6 +112,7 @@ namespace ix
112112

113113
const char* pers = "IXSocketMbedTLS";
114114

115+
#if MBEDTLS_VERSION_MAJOR < 4
115116
if (mbedtls_ctr_drbg_seed(&_ctr_drbg,
116117
mbedtls_entropy_func,
117118
&_entropy,
@@ -121,6 +122,7 @@ namespace ix
121122
errMsg = "Setting entropy seed failed";
122123
return false;
123124
}
125+
#endif
124126

125127
if (mbedtls_ssl_config_defaults(&_conf,
126128
(isClient) ? MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
@@ -131,7 +133,9 @@ namespace ix
131133
return false;
132134
}
133135

136+
#if MBEDTLS_VERSION_MAJOR < 4
134137
mbedtls_ssl_conf_rng(&_conf, mbedtls_ctr_drbg_random, &_ctr_drbg);
138+
#endif
135139

136140
if (_tlsOptions.hasCertAndKey())
137141
{
@@ -140,7 +144,7 @@ namespace ix
140144
errMsg = "Cannot parse cert file '" + _tlsOptions.certFile + "'";
141145
return false;
142146
}
143-
#ifdef IXWEBSOCKET_USE_MBED_TLS_MIN_VERSION_3
147+
#if MBEDTLS_VERSION_MAJOR == 3
144148
if (mbedtls_pk_parse_keyfile(&_pkey, _tlsOptions.keyFile.c_str(), "", mbedtls_ctr_drbg_random, &_ctr_drbg) < 0)
145149
#else
146150
if (mbedtls_pk_parse_keyfile(&_pkey, _tlsOptions.keyFile.c_str(), "") < 0)
@@ -317,15 +321,16 @@ namespace ix
317321

318322
mbedtls_ssl_free(&_ssl);
319323
mbedtls_ssl_config_free(&_conf);
324+
#if MBEDTLS_VERSION_MAJOR < 4
320325
mbedtls_ctr_drbg_free(&_ctr_drbg);
321326
mbedtls_entropy_free(&_entropy);
327+
#endif
322328
mbedtls_x509_crt_free(&_cacert);
323329
mbedtls_x509_crt_free(&_cert);
324330
mbedtls_pk_free(&_pkey);
325-
if (MBEDTLS_VERSION_MAJOR >= 3 && MBEDTLS_VERSION_MINOR >= 6 && MBEDTLS_VERSION_PATCH >= 0)
326-
{
327-
mbedtls_psa_crypto_free();
328-
}
331+
#if MBEDTLS_VERSION_MAJOR >= 4 || (MBEDTLS_VERSION_MAJOR == 3 && MBEDTLS_VERSION_MINOR >= 6)
332+
mbedtls_psa_crypto_free();
333+
#endif
329334

330335
Socket::close();
331336
}

ixwebsocket/IXSocketMbedTLS.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99

1010
#include "IXSocket.h"
1111
#include "IXSocketTLSOptions.h"
12+
#include <mbedtls/version.h>
13+
#if MBEDTLS_VERSION_MAJOR < 4
1214
#include <mbedtls/ctr_drbg.h>
13-
#include <mbedtls/debug.h>
1415
#include <mbedtls/entropy.h>
16+
#endif
17+
#include <mbedtls/debug.h>
1518
#include <mbedtls/error.h>
1619
#include <mbedtls/net_sockets.h>
1720
#include <mbedtls/platform.h>
@@ -41,8 +44,10 @@ namespace ix
4144
private:
4245
mbedtls_ssl_context _ssl;
4346
mbedtls_ssl_config _conf;
47+
#if MBEDTLS_VERSION_MAJOR < 4
4448
mbedtls_entropy_context _entropy;
4549
mbedtls_ctr_drbg_context _ctr_drbg;
50+
#endif
4651
mbedtls_x509_crt _cacert;
4752
mbedtls_x509_crt _cert;
4853
mbedtls_pk_context _pkey;

0 commit comments

Comments
 (0)