3030#include "mbedtls/entropy.h"
3131#include "mbedtls/entropy_poll.h"
3232#include "mbedtls/ctr_drbg.h"
33+ #include "mbedtls/hmac_drbg.h"
3334#include "mbedtls/ssl_ciphersuites.h"
3435
3536#include "ns_trace.h"
@@ -41,7 +42,20 @@ struct coap_security_s {
4142 mbedtls_ssl_config _conf ;
4243 mbedtls_ssl_context _ssl ;
4344
44- mbedtls_ctr_drbg_context _ctr_drbg ;
45+ #if defined(MBEDTLS_CTR_DRBG_C )
46+ mbedtls_ctr_drbg_context _drbg ;
47+ #define DRBG_INIT mbedtls_ctr_drbg_init
48+ #define DRBG_RANDOM mbedtls_ctr_drbg_random
49+ #define DRBG_FREE mbedtls_ctr_drbg_free
50+ #elif defined(MBEDTLS_HMAC_DRBG_C )
51+ mbedtls_hmac_drbg_context _drbg ;
52+ #define DRBG_INIT mbedtls_hmac_drbg_init
53+ #define DRBG_RANDOM mbedtls_hmac_drbg_random
54+ #define DRBG_FREE mbedtls_hmac_drbg_free
55+ #else
56+ #error "CTR or HMAC must be defined for coap_security_handler!"
57+ #endif
58+
4559 mbedtls_entropy_context _entropy ;
4660 bool _is_started ;
4761 simple_cookie_t _cookie ;
@@ -68,19 +82,23 @@ struct coap_security_s {
6882
6983};
7084
85+ #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE )
7186#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED )
7287const int ECJPAKE_SUITES [] = {
7388 MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8 ,
7489 0
7590};
7691#endif
7792
93+ #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED )
7894static const int PSK_SUITES [] = {
7995 MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256 ,
8096 MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8 ,
8197 MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8 ,
8298 0
8399};
100+ #endif /* defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) */
101+ #endif /* !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) */
84102
85103#define TRACE_GROUP "CsSh"
86104
@@ -110,7 +128,7 @@ static int coap_security_handler_init(coap_security_t *sec)
110128
111129 mbedtls_ssl_init (& sec -> _ssl );
112130 mbedtls_ssl_config_init (& sec -> _conf );
113- mbedtls_ctr_drbg_init (& sec -> _ctr_drbg );
131+ DRBG_INIT (& sec -> _drbg );
114132 mbedtls_entropy_init (& sec -> _entropy );
115133
116134#if defined(MBEDTLS_X509_CRT_PARSE_C )
@@ -128,12 +146,22 @@ static int coap_security_handler_init(coap_security_t *sec)
128146 128 , entropy_source_type ) < 0 ) {
129147 return -1 ;
130148 }
131-
132- if ((mbedtls_ctr_drbg_seed (& sec -> _ctr_drbg , mbedtls_entropy_func , & sec -> _entropy ,
149+ #if defined( MBEDTLS_CTR_DRBG_C )
150+ if ((mbedtls_ctr_drbg_seed (& sec -> _drbg , mbedtls_entropy_func , & sec -> _entropy ,
133151 (const unsigned char * ) pers ,
134152 strlen (pers ))) != 0 ) {
135153 return -1 ;
136154 }
155+ #elif defined(MBEDTLS_HMAC_DRBG_C )
156+ if ((mbedtls_hmac_drbg_seed (& sec -> _drbg , mbedtls_md_info_from_type (MBEDTLS_MD_SHA256 ),
157+ mbedtls_entropy_func , & sec -> _entropy ,
158+ (const unsigned char * ) pers ,
159+ strlen (pers ))) != 0 ) {
160+ return -1 ;
161+ }
162+ #else
163+ #error "CTR or HMAC must be defined for coap_security_handler!"
164+ #endif
137165 return 0 ;
138166}
139167
@@ -156,7 +184,9 @@ static void coap_security_handler_reset(coap_security_t *sec)
156184#endif
157185
158186 mbedtls_entropy_free (& sec -> _entropy );
159- mbedtls_ctr_drbg_free (& sec -> _ctr_drbg );
187+
188+ DRBG_FREE (& sec -> _drbg );
189+
160190 mbedtls_ssl_config_free (& sec -> _conf );
161191 mbedtls_ssl_free (& sec -> _ssl );
162192#if defined(MBEDTLS_PLATFORM_C )
@@ -332,7 +362,9 @@ static int coap_security_handler_configure_keys(coap_security_t *sec, coap_secur
332362 if (0 != mbedtls_ssl_conf_psk (& sec -> _conf , keys ._priv_key , keys ._priv_key_len , keys ._cert , keys ._cert_len )) {
333363 break ;
334364 }
365+ #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE )
335366 mbedtls_ssl_conf_ciphersuites (& sec -> _conf , PSK_SUITES );
367+ #endif /* !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) */
336368 ret = 0 ;
337369#endif
338370 break ;
@@ -342,7 +374,9 @@ static int coap_security_handler_configure_keys(coap_security_t *sec, coap_secur
342374 if (mbedtls_ssl_set_hs_ecjpake_password (& sec -> _ssl , keys ._key , keys ._key_len ) != 0 ) {
343375 return -1 ;
344376 }
377+ #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE )
345378 mbedtls_ssl_conf_ciphersuites (& sec -> _conf , ECJPAKE_SUITES );
379+ #endif /* !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) */
346380
347381 //NOTE: If thread starts supporting PSK in other modes, then this will be needed!
348382 mbedtls_ssl_conf_export_keys_cb (& sec -> _conf ,
@@ -388,17 +422,31 @@ int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_ser
388422 mbedtls_ssl_conf_handshake_timeout (& sec -> _conf , timeout_min , timeout_max );
389423 }
390424
391- mbedtls_ssl_conf_rng (& sec -> _conf , mbedtls_ctr_drbg_random , & sec -> _ctr_drbg );
425+ #if !defined(MBEDTLS_SSL_CONF_RNG )
426+ mbedtls_ssl_conf_rng (& sec -> _conf , DRBG_RANDOM , & sec -> _drbg );
427+ #endif
392428
393429 if ((mbedtls_ssl_setup (& sec -> _ssl , & sec -> _conf )) != 0 ) {
394430 return -1 ;
395431 }
396432
433+ // Defines MBEDTLS_SSL_CONF_RECV/SEND/RECV_TIMEOUT define global functions which should be the same for all
434+ // callers of mbedtls_ssl_set_bio_ctx and there should be only one ssl context. If these rules don't apply,
435+ // these defines can't be used.
436+ #if !defined(MBEDTLS_SSL_CONF_RECV ) && !defined(MBEDTLS_SSL_CONF_SEND ) && !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT )
397437 mbedtls_ssl_set_bio (& sec -> _ssl , sec ,
398438 f_send , f_recv , NULL );
439+ #else
440+ mbedtls_ssl_set_bio_ctx (& sec -> _ssl , sec );
441+ #endif /* !defined(MBEDTLS_SSL_CONF_RECV) && !defined(MBEDTLS_SSL_CONF_SEND) && !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT) */
399442
443+ // Defines MBEDTLS_SSL_CONF_SET_TIMER/GET_TIMER define global functions which should be the same for all
444+ // callers of mbedtls_ssl_set_timer_cb and there should be only one ssl context. If these rules don't apply,
445+ // these defines can't be used.
446+ #if !defined(MBEDTLS_SSL_CONF_SET_TIMER ) && !defined(MBEDTLS_SSL_CONF_GET_TIMER )
400447 mbedtls_ssl_set_timer_cb (& sec -> _ssl , sec , set_timer ,
401448 get_timer );
449+ #endif /* !defined(MBEDTLS_SSL_CONF_SET_TIMER) && !defined(MBEDTLS_SSL_CONF_GET_TIMER) */
402450
403451#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED )
404452 //TODO: Figure out better way!!!
@@ -420,8 +468,13 @@ int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_ser
420468 & sec -> _cookie );
421469#endif
422470
471+ #if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER ) || !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER )
423472 mbedtls_ssl_conf_min_version (& sec -> _conf , MBEDTLS_SSL_MAJOR_VERSION_3 , MBEDTLS_SSL_MAJOR_VERSION_3 );
473+ #endif /* !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) */
474+
475+ #if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER ) || !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER )
424476 mbedtls_ssl_conf_max_version (& sec -> _conf , MBEDTLS_SSL_MAJOR_VERSION_3 , MBEDTLS_SSL_MAJOR_VERSION_3 );
477+ #endif /* !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER) */
425478
426479 sec -> _is_started = true;
427480
0 commit comments