Skip to content

Commit 6c6e672

Browse files
author
Tero Heinonen
committed
MbedTLS config file fixing,
small refactoring.
1 parent 518fb25 commit 6c6e672

File tree

5 files changed

+32
-119
lines changed

5 files changed

+32
-119
lines changed

source/coap_connection_handler.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ typedef struct secure_timer_s {
4949
} secure_timer_t;
5050

5151
typedef struct secure_session {
52-
thread_security_t *sec_handler; //owned
52+
coap_security_t *sec_handler; //owned
5353
internal_socket_t *parent; //not owned
5454

5555
secure_timer_t timer;
@@ -652,12 +652,17 @@ int coap_connection_handler_send_data(thread_conn_handler_t *handler, ns_address
652652
handler->socket->dest_addr.identifier = dest_addr->identifier;
653653
handler->socket->dest_addr.type = dest_addr->type;
654654
uint8_t *pw = (uint8_t *)ns_dyn_mem_alloc(64);
655+
if(!pw){
656+
//todo: free secure session?
657+
return -1;
658+
}
655659
uint8_t pw_len;
656660
if( handler->_get_password_cb && 0 == handler->_get_password_cb(handler->socket->listen_socket, dest_addr->address, dest_addr->identifier, pw, &pw_len)){
657661
coap_security_handler_connect(session->sec_handler, false, pw, pw_len);
658662
ns_dyn_mem_free(pw);
659663
return -2;
660664
}else{
665+
//free secure session?
661666
ns_dyn_mem_free(pw);
662667
return -1;
663668
}

source/coap_security_handler.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int entropy_poll( void *data, unsigned char *output, size_t len, size_t *olen );
3333
int f_send( void *ctx, const unsigned char *buf, size_t len );
3434
int f_recv(void *ctx, unsigned char *buf, size_t len);
3535

36-
static int coap_security_handler_init(thread_security_t *sec){
36+
static int coap_security_handler_init(coap_security_t *sec){
3737
const char *pers = "dtls_client";
3838
mbedtls_ssl_init( &sec->_ssl );
3939
mbedtls_ssl_config_init( &sec->_conf );
@@ -61,15 +61,15 @@ static int coap_security_handler_init(thread_security_t *sec){
6161
return 0;
6262
}
6363

64-
static void coap_security_handler_reset(thread_security_t *sec){
64+
static void coap_security_handler_reset(coap_security_t *sec){
6565
mbedtls_entropy_free( &sec->_entropy );
6666
mbedtls_ctr_drbg_free( &sec->_ctr_drbg );
6767
mbedtls_ssl_config_free(&sec->_conf);
6868
mbedtls_ssl_free(&sec->_ssl);
6969
}
7070

7171

72-
thread_security_t *thread_security_create(int8_t socket_id, int8_t timer_id, uint8_t *address_ptr, uint16_t port,
72+
coap_security_t *thread_security_create(int8_t socket_id, int8_t timer_id, uint8_t *address_ptr, uint16_t port,
7373
send_cb *send_cb,
7474
receive_cb *receive_cb,
7575
start_timer_cb *start_timer_cb,
@@ -78,7 +78,7 @@ thread_security_t *thread_security_create(int8_t socket_id, int8_t timer_id, uin
7878
if( !address_ptr || send_cb == NULL || receive_cb == NULL || start_timer_cb == NULL || timer_status_cb == NULL){
7979
return NULL;
8080
}
81-
thread_security_t *this = ns_dyn_mem_alloc(sizeof(thread_security_t));
81+
coap_security_t *this = ns_dyn_mem_alloc(sizeof(coap_security_t));
8282
if( !this ){
8383
return NULL;
8484
}
@@ -88,6 +88,8 @@ thread_security_t *thread_security_create(int8_t socket_id, int8_t timer_id, uin
8888
}
8989
this->_remote_port = port;
9090
memcpy(this->_remote_address, address_ptr, 16);
91+
memset(this->_pw, 0, 64);
92+
this->_pw_len = 0;
9193
this->_socket_id = socket_id;
9294
this->_timer_id = timer_id;
9395
this->_send_cb = send_cb;
@@ -98,7 +100,7 @@ thread_security_t *thread_security_create(int8_t socket_id, int8_t timer_id, uin
98100
return this;
99101
}
100102

101-
void thread_security_destroy(thread_security_t *sec){
103+
void thread_security_destroy(coap_security_t *sec){
102104
if( sec ){
103105
coap_security_handler_reset(sec);
104106
ns_dyn_mem_free(sec);
@@ -210,7 +212,7 @@ static int export_key_block(void *ctx,
210212
*/
211213
static void set_timer(void *sec_obj, uint32_t int_ms, uint32_t fin_ms)
212214
{
213-
thread_security_t *sec = (thread_security_t *)sec_obj;
215+
coap_security_t *sec = (coap_security_t *)sec_obj;
214216
if( sec->_start_timer_cb ){
215217
sec->_start_timer_cb( sec->_timer_id, int_ms, fin_ms);
216218
}
@@ -225,14 +227,14 @@ static void set_timer(void *sec_obj, uint32_t int_ms, uint32_t fin_ms)
225227
*/
226228
static int get_timer(void *sec_obj)
227229
{
228-
thread_security_t *sec = (thread_security_t *)sec_obj;
230+
coap_security_t *sec = (coap_security_t *)sec_obj;
229231
if( sec->_timer_status_cb ){
230232
return sec->_timer_status_cb(sec->_timer_id);
231233
}
232234
return -1;
233235
}
234236

235-
int coap_security_handler_connect(thread_security_t *sec, bool is_server, const unsigned char *pw, uint8_t len){
237+
int coap_security_handler_connect(coap_security_t *sec, bool is_server, const unsigned char *pw, uint8_t len){
236238

237239
if( !sec ){
238240
return -1;
@@ -303,7 +305,7 @@ int coap_security_handler_connect(thread_security_t *sec, bool is_server, const
303305
return ret;
304306
}
305307

306-
int coap_security_handler_continue_connecting(thread_security_t *sec){
308+
int coap_security_handler_continue_connecting(coap_security_t *sec){
307309
int ret=-1;
308310
while( ret != MBEDTLS_ERR_SSL_WANT_READ ){
309311
ret = mbedtls_ssl_handshake_step( &sec->_ssl );
@@ -333,7 +335,7 @@ int coap_security_handler_continue_connecting(thread_security_t *sec){
333335
}
334336

335337

336-
int coap_security_handler_send_message(thread_security_t *sec, unsigned char *message, size_t len){
338+
int coap_security_handler_send_message(coap_security_t *sec, unsigned char *message, size_t len){
337339
int ret=-1;
338340

339341
if( sec ){
@@ -345,7 +347,7 @@ int coap_security_handler_send_message(thread_security_t *sec, unsigned char *me
345347
return ret; //bytes written
346348
}
347349

348-
int thread_security_send_close_alert(thread_security_t *sec)
350+
int thread_security_send_close_alert(coap_security_t *sec)
349351
{
350352
if( !sec ){
351353
return -1;
@@ -356,7 +358,7 @@ int thread_security_send_close_alert(thread_security_t *sec)
356358
coap_security_handler_init(sec);
357359
}
358360

359-
int coap_security_handler_read(thread_security_t *sec, unsigned char* buffer, size_t len){
361+
int coap_security_handler_read(coap_security_t *sec, unsigned char* buffer, size_t len){
360362
int ret=-1;
361363

362364
if( sec && buffer ){
@@ -370,12 +372,12 @@ int coap_security_handler_read(thread_security_t *sec, unsigned char* buffer, si
370372
}
371373

372374
int f_send( void *ctx, const unsigned char *buf, size_t len){
373-
thread_security_t *sec = (thread_security_t *)ctx;
375+
coap_security_t *sec = (coap_security_t *)ctx;
374376
return sec->_send_cb(sec->_socket_id, sec->_remote_address, sec->_remote_port, buf, len);
375377
}
376378

377379
int f_recv(void *ctx, unsigned char *buf, size_t len){
378-
thread_security_t *sec = (thread_security_t *)ctx;
380+
coap_security_t *sec = (coap_security_t *)ctx;
379381
return sec->_receive_cb(sec->_socket_id, buf, len);
380382
}
381383

source/include/coap_security_handler.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ typedef void start_timer_cb(int8_t timer_id, uint32_t min, uint32_t fin);
2929
typedef int timer_status_cb(int8_t timer_id);
3030

3131
typedef struct thread_security_s {
32-
3332
mbedtls_ssl_config _conf;
3433
mbedtls_ssl_context _ssl;
34+
3535
mbedtls_ctr_drbg_context _ctr_drbg;
3636
mbedtls_entropy_context _entropy;
3737
bool _is_started;
@@ -50,24 +50,25 @@ typedef struct thread_security_s {
5050
receive_cb *_receive_cb;
5151
start_timer_cb *_start_timer_cb;
5252
timer_status_cb *_timer_status_cb;
53-
} thread_security_t;
5453

55-
thread_security_t *thread_security_create(int8_t socket_id, int8_t timer_id, uint8_t *address_ptr, uint16_t port,
54+
} coap_security_t;
55+
56+
coap_security_t *thread_security_create(int8_t socket_id, int8_t timer_id, uint8_t *address_ptr, uint16_t port,
5657
send_cb *send_cb,
5758
receive_cb *receive_cb,
5859
start_timer_cb *start_timer_cb,
5960
timer_status_cb *timer_status_cb);
6061

61-
void thread_security_destroy(thread_security_t *sec);
62+
void thread_security_destroy(coap_security_t *sec);
6263

63-
int coap_security_handler_connect(thread_security_t *sec, bool is_server, const unsigned char *pw, uint8_t len);
64+
int coap_security_handler_connect(coap_security_t *sec, bool is_server, const unsigned char *pw, uint8_t len);
6465

65-
int coap_security_handler_continue_connecting(thread_security_t *sec);
66+
int coap_security_handler_continue_connecting(coap_security_t *sec);
6667

67-
int coap_security_handler_send_message(thread_security_t *sec, unsigned char *message, size_t len);
68+
int coap_security_handler_send_message(coap_security_t *sec, unsigned char *message, size_t len);
6869

69-
int thread_security_send_close_alert(thread_security_t *sec);
70+
int thread_security_send_close_alert(coap_security_t *sec);
7071

71-
int coap_security_handler_read(thread_security_t *sec, unsigned char* buffer, size_t len);
72+
int coap_security_handler_read(coap_security_t *sec, unsigned char* buffer, size_t len);
7273

7374
#endif

source/include/config-thread.h

Lines changed: 0 additions & 94 deletions
This file was deleted.

source/override_flags.cmake

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)