11#if defined(USE_MBEDTLS) // USE_MBEDTLS used with NO_SSL
22
3- #include " mbedtls/ctr_drbg.h"
43#include " mbedtls/debug.h"
4+ #if MBEDTLS_VERSION_NUMBER < 0x04000000
55#include " mbedtls/entropy.h"
6+ #include " mbedtls/ctr_drbg.h"
7+ #endif
68#include " mbedtls/error.h"
79
810#if MBEDTLS_VERSION_NUMBER >= 0x03000000
@@ -26,8 +28,10 @@ typedef mbedtls_ssl_context SSL;
2628typedef struct {
2729 mbedtls_ssl_config conf; /* SSL configuration */
2830 mbedtls_x509_crt cert; /* Certificate */
31+ #if MBEDTLS_VERSION_NUMBER < 0x04000000
2932 mbedtls_ctr_drbg_context ctr; /* Counter random generator state */
3033 mbedtls_entropy_context entropy; /* Entropy context */
34+ #endif
3135 mbedtls_pk_context pkey; /* Private key */
3236} SSL_CTX;
3337
@@ -125,7 +129,9 @@ mbed_sslctx_init(SSL_CTX *ctx, const char *crt, const char *cipherlist)
125129 }
126130
127131 DEBUG_TRACE (" %s" , " Initializing MbedTLS SSL" );
132+ #if MBEDTLS_VERSION_NUMBER < 0x04000000
128133 mbedtls_entropy_init (&ctx->entropy );
134+ #endif
129135
130136 conf = &ctx->conf ;
131137 mbedtls_ssl_config_init (conf);
@@ -152,7 +158,9 @@ mbed_sslctx_init(SSL_CTX *ctx, const char *crt, const char *cipherlist)
152158
153159 /* Initialize TLS key and cert */
154160 mbedtls_pk_init (&ctx->pkey );
161+ #if MBEDTLS_VERSION_NUMBER < 0x04000000
155162 mbedtls_ctr_drbg_init (&ctx->ctr );
163+ #endif
156164 mbedtls_x509_crt_init (&ctx->cert );
157165
158166#ifdef MBEDTLS_PSA_CRYPTO_C
@@ -168,6 +176,7 @@ mbed_sslctx_init(SSL_CTX *ctx, const char *crt, const char *cipherlist)
168176 }
169177#endif
170178
179+ #if MBEDTLS_VERSION_NUMBER < 0x04000000
171180 rc = mbedtls_ctr_drbg_seed (&ctx->ctr ,
172181 mbedtls_entropy_func,
173182 &ctx->entropy ,
@@ -177,8 +186,9 @@ mbed_sslctx_init(SSL_CTX *ctx, const char *crt, const char *cipherlist)
177186 DEBUG_TRACE (" TLS random seed failed (%i)" , rc);
178187 return -1 ;
179188 }
189+ #endif
180190
181- #if MBEDTLS_VERSION_NUMBER >= 0x03000000
191+ #if MBEDTLS_VERSION_NUMBER >= 0x03000000 && MBEDTLS_VERSION_NUMBER < 0x04000000
182192 // mbedtls_pk_parse_keyfile() has changed in mbedTLS 3.0. You now need
183193 // to pass a properly seeded, cryptographically secure RNG when calling
184194 // these functions. It is used for blinding, a countermeasure against
@@ -209,7 +219,9 @@ mbed_sslctx_init(SSL_CTX *ctx, const char *crt, const char *cipherlist)
209219 return -1 ;
210220 }
211221
222+ #if MBEDTLS_VERSION_NUMBER >= 0x03000000 && MBEDTLS_VERSION_NUMBER < 0x04000000
212223 mbedtls_ssl_conf_rng (conf, mbedtls_ctr_drbg_random, &ctx->ctr );
224+ #endif
213225
214226 /* Set auth mode if peer cert should be verified */
215227 mbedtls_ssl_conf_authmode (conf, MBEDTLS_SSL_VERIFY_NONE);
@@ -237,10 +249,12 @@ mbed_sslctx_init(SSL_CTX *ctx, const char *crt, const char *cipherlist)
237249void
238250mbed_sslctx_uninit (SSL_CTX *ctx)
239251{
252+ #if MBEDTLS_VERSION_NUMBER < 0x04000000
240253 mbedtls_ctr_drbg_free (&ctx->ctr );
254+ mbedtls_entropy_free (&ctx->entropy );
255+ #endif
241256 mbedtls_pk_free (&ctx->pkey );
242257 mbedtls_x509_crt_free (&ctx->cert );
243- mbedtls_entropy_free (&ctx->entropy );
244258 mbedtls_ssl_config_free (&ctx->conf );
245259}
246260
0 commit comments