Skip to content

Commit 5aa54b8

Browse files
author
Arto Kinnunen
authored
Merge pull request #127 from ARMmbed/sync_with_mbedos
Sync with Mbed OS
2 parents e5e0c13 + 6fe7841 commit 5aa54b8

File tree

3 files changed

+93
-6
lines changed

3 files changed

+93
-6
lines changed

source/coap_security_handler.c

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
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)
7287
const 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)
7894
static 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

test/coap-service/unittest/stub/mbedtls_stub.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,38 @@ int mbedtls_ctr_drbg_random(void *p_rng,
248248
return mbedtls_stub.crt_expected_int;
249249
}
250250

251+
// from hmac_drbg.h
252+
void mbedtls_hmac_drbg_init(mbedtls_hmac_drbg_context *ctx)
253+
{
254+
255+
}
256+
257+
void mbedtls_hmac_drbg_free(mbedtls_hmac_drbg_context *ctx)
258+
{
259+
260+
}
261+
262+
int mbedtls_hmac_drbg_seed(mbedtls_hmac_drbg_context *ctx,
263+
const mbedtls_md_info_t *md_info,
264+
int (*f_entropy)(void *, unsigned char *, size_t),
265+
void *p_entropy,
266+
const unsigned char *custom,
267+
size_t len)
268+
{
269+
return mbedtls_stub.crt_expected_int;
270+
}
271+
272+
int mbedtls_hmac_drbg_random(void *p_rng, unsigned char *output, size_t out_len)
273+
{
274+
return mbedtls_stub.crt_expected_int;
275+
}
276+
277+
// from md.h
278+
const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
279+
{
280+
return 0;
281+
}
282+
251283
//From x509_crt.h
252284
void mbedtls_x509_crt_init(mbedtls_x509_crt *a)
253285
{

test/coap-service/unittest/stub/mbedtls_stub.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
#include "mbedtls/platform.h"
2525
#include "mbedtls/ssl.h"
2626
#include "mbedtls/ctr_drbg.h"
27+
#include "mbedtls/hmac_drbg.h"
2728
#include "mbedtls/x509_crt.h"
2829
#include "mbedtls/sha256.h"
2930
#include "mbedtls/entropy.h"
3031
#include "mbedtls/pk.h"
3132
#include "mbedtls/platform.h"
33+
#include "mbedtls/md.h"
3234

3335

3436
#define HANDSHAKE_FINISHED_VALUE 8888

0 commit comments

Comments
 (0)