Skip to content

Commit ac8ddaf

Browse files
committed
Restructure connection and security
Make connection handler deal with all addressing, and hide the internals of security handler. Will allow security handler code to be stubbed out if mbed TLS is not available.
1 parent 9dfc6f9 commit ac8ddaf

File tree

8 files changed

+114
-95
lines changed

8 files changed

+114
-95
lines changed

source/coap_connection_handler.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ typedef struct secure_session {
6464
coap_security_t *sec_handler; //owned
6565
internal_socket_t *parent; //not owned
6666

67+
uint8_t remote_address[16];
68+
uint16_t remote_port;
69+
6770
secure_timer_t timer;
6871

6972
session_state_t session_state;
@@ -72,7 +75,7 @@ typedef struct secure_session {
7275
} secure_session_t;
7376

7477
static NS_LIST_DEFINE(secure_session_list, secure_session_t, link);
75-
static int send_to_socket(int8_t socket_id, const uint8_t *address_ptr, uint16_t port, const uint8_t source_addr[static 16], const void *buf, size_t len);
78+
static int send_to_socket(int8_t socket_id, void *handle, const void *buf, size_t len);
7679
static int receive_from_socket(int8_t socket_id, unsigned char *buf, size_t len);
7780
static void start_timer(int8_t timer_id, uint32_t int_ms, uint32_t fin_ms);
7881
static int timer_status(int8_t timer_id);
@@ -146,8 +149,10 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const
146149
timer_id++;
147150
}
148151
this->timer.id = timer_id;
152+
memcpy(this->remote_address, address_ptr, 16);
153+
this->remote_port = port;
149154

150-
this->sec_handler = coap_security_create(parent->listen_socket, this->timer.id, address_ptr, port, ECJPAKE,
155+
this->sec_handler = coap_security_create(parent->listen_socket, this->timer.id, this, ECJPAKE,
151156
&send_to_socket, &receive_from_socket, &start_timer, &timer_status);
152157
if( !this->sec_handler ){
153158
ns_dyn_mem_free(this);
@@ -178,8 +183,8 @@ static secure_session_t *secure_session_find(internal_socket_t *parent, const ui
178183
secure_session_t *this = NULL;
179184
ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) {
180185
if( cur_ptr->sec_handler ){
181-
if (cur_ptr->parent == parent && cur_ptr->sec_handler->_remote_port == port &&
182-
memcmp(cur_ptr->sec_handler->_remote_address, address_ptr, 16) == 0) {
186+
if (cur_ptr->parent == parent && cur_ptr->remote_port == port &&
187+
memcmp(cur_ptr->remote_address, address_ptr, 16) == 0) {
183188
this = cur_ptr;
184189
// hack_save_remote_address(address_ptr, port);
185190
break;
@@ -329,15 +334,16 @@ static int8_t send_to_real_socket(int8_t socket_id, const ns_address_t *address,
329334
return socket_sendmsg(socket_id, &msghdr, 0);
330335
}
331336

332-
static int send_to_socket(int8_t socket_id, const uint8_t *address_ptr, uint16_t port, const uint8_t source_addr[static 16], const void *buf, size_t len)
337+
static int send_to_socket(int8_t socket_id, void *handle, const void *buf, size_t len)
333338
{
339+
secure_session_t *session = handle;
334340
internal_socket_t *sock = int_socket_find_by_socket_id(socket_id);
335341
if(!sock){
336342
return -1;
337343
}
338344
if(!sock->real_socket){
339345
// Send to virtual socket cb
340-
int ret = sock->parent->_send_cb(sock->listen_socket, address_ptr, port, buf, len);
346+
int ret = sock->parent->_send_cb(sock->listen_socket, session->remote_address, session->remote_port, buf, len);
341347
if( ret < 0 )
342348
return ret;
343349
return len;
@@ -353,7 +359,7 @@ static int send_to_socket(int8_t socket_id, const uint8_t *address_ptr, uint16_t
353359
//For some reason socket_sendto returns 0 in success, while other socket impls return number of bytes sent!!!
354360
//TODO: check if address_ptr is valid and use that instead if it is
355361

356-
int8_t ret = send_to_real_socket(sock->listen_socket, &sock->dest_addr, source_addr, buf, len);
362+
int8_t ret = send_to_real_socket(sock->listen_socket, &sock->dest_addr, session->remote_address, buf, len);
357363
if (ret < 0) {
358364
return ret;
359365
}
@@ -536,8 +542,8 @@ static void secure_recv_sckt_msg(void *cb_res)
536542
}
537543
session->last_contact_time = coap_service_get_internal_timer_ticks();
538544
// Start handshake
539-
if (!session->sec_handler->_is_started) {
540-
uint8_t *pw = (uint8_t *)ns_dyn_mem_alloc(64);
545+
if (!coap_security_handler_is_started(session->sec_handler) ){
546+
uint8_t *pw = ns_dyn_mem_alloc(64);
541547
uint8_t pw_len;
542548
if( sock->parent->_get_password_cb && 0 == sock->parent->_get_password_cb(sock->listen_socket, src_address.address, src_address.identifier, pw, &pw_len)){
543549
//TODO: get_password_cb should support certs and PSK also
@@ -560,7 +566,7 @@ static void secure_recv_sckt_msg(void *cb_res)
560566
if( sock->parent->_security_done_cb ){
561567
sock->parent->_security_done_cb(sock->listen_socket, src_address.address,
562568
src_address.identifier,
563-
session->sec_handler->_keyblk.value);
569+
(void *)coap_security_handler_keyblock(session->sec_handler));
564570
}
565571
} else if (ret < 0){
566572
// error handling
@@ -641,8 +647,8 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
641647

642648
session->last_contact_time = coap_service_get_internal_timer_ticks();
643649

644-
if (!session->sec_handler->_is_started) {
645-
uint8_t *pw = (uint8_t *)ns_dyn_mem_alloc(64);
650+
if (!coap_security_handler_is_started(session->sec_handler)) {
651+
uint8_t *pw = ns_dyn_mem_alloc(64);
646652
uint8_t pw_len;
647653
if (sock->parent->_get_password_cb && 0 == sock->parent->_get_password_cb(sock->listen_socket, address, port, pw, &pw_len)) {
648654
//TODO: get_password_cb should support certs and PSK also
@@ -665,7 +671,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
665671
if( handler->_security_done_cb ){
666672
handler->_security_done_cb(sock->listen_socket,
667673
address, port,
668-
session->sec_handler->_keyblk.value);
674+
(void *)coap_security_handler_keyblock(session->sec_handler));
669675
}
670676
return 0;
671677
}
@@ -807,7 +813,7 @@ int coap_connection_handler_send_data(coap_conn_handler_t *handler, const ns_add
807813
memcpy( handler->socket->dest_addr.address, dest_addr->address, 16 );
808814
handler->socket->dest_addr.identifier = dest_addr->identifier;
809815
handler->socket->dest_addr.type = dest_addr->type;
810-
uint8_t *pw = (uint8_t *)ns_dyn_mem_alloc(64);
816+
uint8_t *pw = ns_dyn_mem_alloc(64);
811817
if (!pw) {
812818
//todo: free secure session?
813819
return -1;

source/coap_security_handler.c

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,37 @@
2020
#include "mbedtls/ssl_ciphersuites.h"
2121
#include "socket_api.h"
2222

23+
struct coap_security_s {
24+
mbedtls_ssl_config _conf;
25+
mbedtls_ssl_context _ssl;
26+
27+
mbedtls_ctr_drbg_context _ctr_drbg;
28+
mbedtls_entropy_context _entropy;
29+
bool _is_started;
30+
simple_cookie_t _cookie;
31+
key_block_t _keyblk;
32+
33+
SecureConnectionMode _conn_mode;
34+
#if defined(MBEDTLS_X509_CRT_PARSE_C)
35+
mbedtls_x509_crt _cacert;
36+
mbedtls_x509_crt _owncert;
37+
#endif
38+
mbedtls_pk_context _pkey;
39+
40+
uint8_t _pw[64];
41+
uint8_t _pw_len;
42+
43+
bool _is_blocking;
44+
int8_t _socket_id;
45+
int8_t _timer_id;
46+
void *_handle;
47+
send_cb *_send_cb;
48+
receive_cb *_receive_cb;
49+
start_timer_cb *_start_timer_cb;
50+
timer_status_cb *_timer_status_cb;
51+
52+
};
53+
2354
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2455
const int ECJPAKE_SUITES[] = {
2556
MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8,
@@ -78,6 +109,16 @@ static int coap_security_handler_init(coap_security_t *sec){
78109
return 0;
79110
}
80111

112+
bool coap_security_handler_is_started(const coap_security_t *sec)
113+
{
114+
return sec->_is_started;
115+
}
116+
117+
const void *coap_security_handler_keyblock(const coap_security_t *sec)
118+
{
119+
return sec->_keyblk.value;
120+
}
121+
81122
static void coap_security_handler_reset(coap_security_t *sec){
82123
#if defined(MBEDTLS_X509_CRT_PARSE_C)
83124
mbedtls_x509_crt_free(&sec->_cacert);
@@ -93,13 +134,13 @@ static void coap_security_handler_reset(coap_security_t *sec){
93134
}
94135

95136

96-
coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, const uint8_t *address_ptr, uint16_t port, SecureConnectionMode mode,
137+
coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, void *handle, SecureConnectionMode mode,
97138
send_cb *socket_cb,
98139
receive_cb *receive_data_cb,
99140
start_timer_cb *timer_start_cb,
100141
timer_status_cb *timer_stat_cb)
101142
{
102-
if (!address_ptr || socket_cb == NULL || receive_data_cb == NULL || timer_start_cb == NULL || timer_stat_cb == NULL) {
143+
if (socket_cb == NULL || receive_data_cb == NULL || timer_start_cb == NULL || timer_stat_cb == NULL) {
103144
return NULL;
104145
}
105146
coap_security_t *this = ns_dyn_mem_alloc(sizeof(coap_security_t));
@@ -111,8 +152,7 @@ coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, const u
111152
ns_dyn_mem_free(this);
112153
return NULL;
113154
}
114-
this->_remote_port = port;
115-
memcpy(this->_remote_address, address_ptr, 16);
155+
this->_handle = handle;
116156
this->_conn_mode = mode;
117157
memset(this->_pw, 0, 64);
118158
this->_pw_len = 0;
@@ -552,7 +592,7 @@ static int get_timer(void *sec_obj)
552592

553593
int f_send( void *ctx, const unsigned char *buf, size_t len){
554594
coap_security_t *sec = (coap_security_t *)ctx;
555-
return sec->_send_cb(sec->_socket_id, sec->_remote_address, sec->_remote_port, ns_in6addr_any, buf, len);
595+
return sec->_send_cb(sec->_socket_id, sec->_handle, buf, len);
556596
}
557597

558598
int f_recv(void *ctx, unsigned char *buf, size_t len){

source/include/coap_security_handler.h

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ typedef struct key_block {
3838
unsigned char value[KEY_BLOCK_LEN];
3939
} key_block_t;
4040

41-
typedef int send_cb(int8_t socket_id, const uint8_t *address_ptr, uint16_t port, const uint8_t source_addr[static 16], const void *, size_t);
41+
typedef int send_cb(int8_t socket_id, void *handle, const void *buf, size_t);
4242
typedef int receive_cb(int8_t socket_id, unsigned char *, size_t);
4343
typedef void start_timer_cb(int8_t timer_id, uint32_t min, uint32_t fin);
4444
typedef int timer_status_cb(int8_t timer_id);
@@ -66,40 +66,9 @@ typedef struct {
6666
uint8_t _priv_len;
6767
} coap_security_keys_t;
6868

69-
typedef struct coap_security_s {
70-
mbedtls_ssl_config _conf;
71-
mbedtls_ssl_context _ssl;
69+
typedef struct coap_security_s coap_security_t;
7270

73-
mbedtls_ctr_drbg_context _ctr_drbg;
74-
mbedtls_entropy_context _entropy;
75-
bool _is_started;
76-
simple_cookie_t _cookie;
77-
key_block_t _keyblk;
78-
79-
SecureConnectionMode _conn_mode;
80-
#if defined(MBEDTLS_X509_CRT_PARSE_C)
81-
mbedtls_x509_crt _cacert;
82-
mbedtls_x509_crt _owncert;
83-
#endif
84-
mbedtls_pk_context _pkey;
85-
86-
uint8_t _remote_address[16];
87-
uint16_t _remote_port;
88-
89-
uint8_t _pw[64];
90-
uint8_t _pw_len;
91-
92-
bool _is_blocking;
93-
int8_t _socket_id;
94-
int8_t _timer_id;
95-
send_cb *_send_cb;
96-
receive_cb *_receive_cb;
97-
start_timer_cb *_start_timer_cb;
98-
timer_status_cb *_timer_status_cb;
99-
100-
} coap_security_t;
101-
102-
coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, const uint8_t *address_ptr, uint16_t port,
71+
coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, void *handle,
10372
SecureConnectionMode mode,
10473
send_cb *send_cb,
10574
receive_cb *receive_cb,
@@ -120,4 +89,8 @@ int coap_security_send_close_alert(coap_security_t *sec);
12089

12190
int coap_security_handler_read(coap_security_t *sec, unsigned char* buffer, size_t len);
12291

92+
bool coap_security_handler_is_started(const coap_security_t *sec);
93+
94+
const void *coap_security_handler_keyblock(const coap_security_t *sec);
95+
12396
#endif

test/coap-service/unittest/coap_connection_handler/test_coap_connection_handler.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,7 @@ bool test_coap_connection_handler_send_data()
123123

124124
connection_handler_destroy(handler);
125125

126-
coap_security_handler_stub.sec_obj = (coap_security_t *)malloc(sizeof(coap_security_t));
127-
memset(coap_security_handler_stub.sec_obj, 0, sizeof(coap_security_t));
128-
coap_security_handler_stub.sec_obj->_remote_port = 22;
129-
memset(coap_security_handler_stub.sec_obj->_remote_address, 1, 16 );
126+
coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc();
130127

131128
nsdynmemlib_stub.returnCounter = 1;
132129
handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
@@ -200,10 +197,7 @@ bool test_coap_connection_handler_virtual_recv()
200197
return false;
201198

202199
//handler->socket->data still in memory
203-
coap_security_handler_stub.sec_obj = (coap_security_t *)malloc(sizeof(coap_security_t));
204-
memset(coap_security_handler_stub.sec_obj, 0, sizeof(coap_security_t));
205-
coap_security_handler_stub.sec_obj->_remote_port = 55;
206-
memset(coap_security_handler_stub.sec_obj->_remote_address, 4, 16 );
200+
coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc();
207201

208202
ns_timer_stub.int8_value = -1;
209203
nsdynmemlib_stub.returnCounter = 3;
@@ -229,8 +223,6 @@ bool test_coap_connection_handler_virtual_recv()
229223

230224
nsdynmemlib_stub.returnCounter = 1;
231225
coap_security_handler_stub.int_value = 0;
232-
coap_security_handler_stub.sec_obj->_remote_port = 12;
233-
memset(coap_security_handler_stub.sec_obj->_remote_address, 1, 16 );
234226
if( 0 != coap_connection_handler_virtual_recv(handler2,buf, 12, &buf, 1) )
235227
return false;
236228

@@ -300,11 +292,7 @@ bool test_timer_callbacks()
300292
return false;
301293

302294
//handler->socket->data still in memory
303-
coap_security_handler_stub.sec_obj = (coap_security_t *)malloc(sizeof(coap_security_t));
304-
memset(coap_security_handler_stub.sec_obj, 0, sizeof(coap_security_t));
305-
coap_security_handler_stub.sec_obj->_remote_port = 55;
306-
memset(coap_security_handler_stub.sec_obj->_remote_address, 4, 16 );
307-
coap_security_handler_stub.sec_obj->_timer_id = 5;
295+
coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc();
308296

309297
ns_timer_stub.int8_value = 0;
310298
nsdynmemlib_stub.returnCounter = 3;
@@ -353,8 +341,7 @@ bool test_socket_api_callbacks()
353341
socket_callback_t *sckt_data = (socket_callback_t *)malloc(sizeof(socket_callback_t));
354342
memset(sckt_data, 0, sizeof(socket_callback_t));
355343

356-
coap_security_handler_stub.sec_obj = (coap_security_t *)malloc(sizeof(coap_security_t));
357-
memset(coap_security_handler_stub.sec_obj, 0, sizeof(coap_security_t));
344+
coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc();
358345

359346
socket_api_stub.int8_value = 0;
360347
nsdynmemlib_stub.returnCounter = 1;
@@ -433,8 +420,7 @@ bool test_security_callbacks()
433420
socket_callback_t *sckt_data = (socket_callback_t *)malloc(sizeof(socket_callback_t));
434421
memset(sckt_data, 0, sizeof(socket_callback_t));
435422

436-
coap_security_handler_stub.sec_obj = (coap_security_t *)malloc(sizeof(coap_security_t));
437-
memset(coap_security_handler_stub.sec_obj, 0, sizeof(coap_security_t));
423+
coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc();
438424

439425
nsdynmemlib_stub.returnCounter = 1;
440426
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);

0 commit comments

Comments
 (0)