30
30
#include "mbedtls/entropy.h"
31
31
#include "mbedtls/entropy_poll.h"
32
32
#include "mbedtls/ctr_drbg.h"
33
+ #include "mbedtls/hmac_drbg.h"
33
34
#include "mbedtls/ssl_ciphersuites.h"
34
35
35
36
#include "ns_trace.h"
@@ -41,7 +42,20 @@ struct coap_security_s {
41
42
mbedtls_ssl_config _conf ;
42
43
mbedtls_ssl_context _ssl ;
43
44
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
+
45
59
mbedtls_entropy_context _entropy ;
46
60
bool _is_started ;
47
61
simple_cookie_t _cookie ;
@@ -68,19 +82,23 @@ struct coap_security_s {
68
82
69
83
};
70
84
85
+ #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE )
71
86
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED )
72
87
const int ECJPAKE_SUITES [] = {
73
88
MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8 ,
74
89
0
75
90
};
76
91
#endif
77
92
93
+ #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED )
78
94
static const int PSK_SUITES [] = {
79
95
MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256 ,
80
96
MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8 ,
81
97
MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8 ,
82
98
0
83
99
};
100
+ #endif /* defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) */
101
+ #endif /* !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) */
84
102
85
103
#define TRACE_GROUP "CsSh"
86
104
@@ -110,7 +128,7 @@ static int coap_security_handler_init(coap_security_t *sec)
110
128
111
129
mbedtls_ssl_init (& sec -> _ssl );
112
130
mbedtls_ssl_config_init (& sec -> _conf );
113
- mbedtls_ctr_drbg_init (& sec -> _ctr_drbg );
131
+ DRBG_INIT (& sec -> _drbg );
114
132
mbedtls_entropy_init (& sec -> _entropy );
115
133
116
134
#if defined(MBEDTLS_X509_CRT_PARSE_C )
@@ -128,12 +146,22 @@ static int coap_security_handler_init(coap_security_t *sec)
128
146
128 , entropy_source_type ) < 0 ) {
129
147
return -1 ;
130
148
}
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 ,
133
151
(const unsigned char * ) pers ,
134
152
strlen (pers ))) != 0 ) {
135
153
return -1 ;
136
154
}
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
137
165
return 0 ;
138
166
}
139
167
@@ -156,7 +184,9 @@ static void coap_security_handler_reset(coap_security_t *sec)
156
184
#endif
157
185
158
186
mbedtls_entropy_free (& sec -> _entropy );
159
- mbedtls_ctr_drbg_free (& sec -> _ctr_drbg );
187
+
188
+ DRBG_FREE (& sec -> _drbg );
189
+
160
190
mbedtls_ssl_config_free (& sec -> _conf );
161
191
mbedtls_ssl_free (& sec -> _ssl );
162
192
#if defined(MBEDTLS_PLATFORM_C )
@@ -332,7 +362,9 @@ static int coap_security_handler_configure_keys(coap_security_t *sec, coap_secur
332
362
if (0 != mbedtls_ssl_conf_psk (& sec -> _conf , keys ._priv_key , keys ._priv_key_len , keys ._cert , keys ._cert_len )) {
333
363
break ;
334
364
}
365
+ #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE )
335
366
mbedtls_ssl_conf_ciphersuites (& sec -> _conf , PSK_SUITES );
367
+ #endif /* !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) */
336
368
ret = 0 ;
337
369
#endif
338
370
break ;
@@ -342,7 +374,9 @@ static int coap_security_handler_configure_keys(coap_security_t *sec, coap_secur
342
374
if (mbedtls_ssl_set_hs_ecjpake_password (& sec -> _ssl , keys ._key , keys ._key_len ) != 0 ) {
343
375
return -1 ;
344
376
}
377
+ #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE )
345
378
mbedtls_ssl_conf_ciphersuites (& sec -> _conf , ECJPAKE_SUITES );
379
+ #endif /* !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) */
346
380
347
381
//NOTE: If thread starts supporting PSK in other modes, then this will be needed!
348
382
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
388
422
mbedtls_ssl_conf_handshake_timeout (& sec -> _conf , timeout_min , timeout_max );
389
423
}
390
424
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
392
428
393
429
if ((mbedtls_ssl_setup (& sec -> _ssl , & sec -> _conf )) != 0 ) {
394
430
return -1 ;
395
431
}
396
432
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 )
397
437
mbedtls_ssl_set_bio (& sec -> _ssl , sec ,
398
438
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) */
399
442
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 )
400
447
mbedtls_ssl_set_timer_cb (& sec -> _ssl , sec , set_timer ,
401
448
get_timer );
449
+ #endif /* !defined(MBEDTLS_SSL_CONF_SET_TIMER) && !defined(MBEDTLS_SSL_CONF_GET_TIMER) */
402
450
403
451
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED )
404
452
//TODO: Figure out better way!!!
@@ -420,8 +468,13 @@ int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_ser
420
468
& sec -> _cookie );
421
469
#endif
422
470
471
+ #if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER ) || !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER )
423
472
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 )
424
476
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) */
425
478
426
479
sec -> _is_started = true;
427
480
0 commit comments