@@ -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 }
0 commit comments