@@ -64,6 +64,9 @@ typedef struct secure_session {
64
64
coap_security_t * sec_handler ; //owned
65
65
internal_socket_t * parent ; //not owned
66
66
67
+ uint8_t remote_address [16 ];
68
+ uint16_t remote_port ;
69
+
67
70
secure_timer_t timer ;
68
71
69
72
session_state_t session_state ;
@@ -72,7 +75,7 @@ typedef struct secure_session {
72
75
} secure_session_t ;
73
76
74
77
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 );
76
79
static int receive_from_socket (int8_t socket_id , unsigned char * buf , size_t len );
77
80
static void start_timer (int8_t timer_id , uint32_t int_ms , uint32_t fin_ms );
78
81
static int timer_status (int8_t timer_id );
@@ -146,8 +149,10 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const
146
149
timer_id ++ ;
147
150
}
148
151
this -> timer .id = timer_id ;
152
+ memcpy (this -> remote_address , address_ptr , 16 );
153
+ this -> remote_port = port ;
149
154
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 ,
151
156
& send_to_socket , & receive_from_socket , & start_timer , & timer_status );
152
157
if ( !this -> sec_handler ){
153
158
ns_dyn_mem_free (this );
@@ -178,8 +183,8 @@ static secure_session_t *secure_session_find(internal_socket_t *parent, const ui
178
183
secure_session_t * this = NULL ;
179
184
ns_list_foreach (secure_session_t , cur_ptr , & secure_session_list ) {
180
185
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 ) {
183
188
this = cur_ptr ;
184
189
// hack_save_remote_address(address_ptr, port);
185
190
break ;
@@ -219,7 +224,11 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem
219
224
if ( !is_secure ){
220
225
this -> listen_socket = socket_open (SOCKET_UDP , listen_port , recv_sckt_msg );
221
226
}else {
227
+ #ifdef COAP_SECURITY_AVAILABLE
222
228
this -> listen_socket = socket_open (SOCKET_UDP , listen_port , secure_recv_sckt_msg );
229
+ #else
230
+ tr_err ("Secure CoAP unavailable - SSL library not configured, possibly due to lack of entropy source" );
231
+ #endif
223
232
}
224
233
// Socket create failed
225
234
if (this -> listen_socket < 0 ){
@@ -329,15 +338,16 @@ static int8_t send_to_real_socket(int8_t socket_id, const ns_address_t *address,
329
338
return socket_sendmsg (socket_id , & msghdr , 0 );
330
339
}
331
340
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 )
341
+ static int send_to_socket (int8_t socket_id , void * handle , const void * buf , size_t len )
333
342
{
343
+ secure_session_t * session = handle ;
334
344
internal_socket_t * sock = int_socket_find_by_socket_id (socket_id );
335
345
if (!sock ){
336
346
return -1 ;
337
347
}
338
348
if (!sock -> real_socket ){
339
349
// Send to virtual socket cb
340
- int ret = sock -> parent -> _send_cb (sock -> listen_socket , address_ptr , port , buf , len );
350
+ int ret = sock -> parent -> _send_cb (sock -> listen_socket , session -> remote_address , session -> remote_port , buf , len );
341
351
if ( ret < 0 )
342
352
return ret ;
343
353
return len ;
@@ -353,7 +363,7 @@ static int send_to_socket(int8_t socket_id, const uint8_t *address_ptr, uint16_t
353
363
//For some reason socket_sendto returns 0 in success, while other socket impls return number of bytes sent!!!
354
364
//TODO: check if address_ptr is valid and use that instead if it is
355
365
356
- int8_t ret = send_to_real_socket (sock -> listen_socket , & sock -> dest_addr , source_addr , buf , len );
366
+ int8_t ret = send_to_real_socket (sock -> listen_socket , & sock -> dest_addr , session -> remote_address , buf , len );
357
367
if (ret < 0 ) {
358
368
return ret ;
359
369
}
@@ -536,8 +546,8 @@ static void secure_recv_sckt_msg(void *cb_res)
536
546
}
537
547
session -> last_contact_time = coap_service_get_internal_timer_ticks ();
538
548
// Start handshake
539
- if (!session -> sec_handler -> _is_started ) {
540
- uint8_t * pw = ( uint8_t * ) ns_dyn_mem_alloc (64 );
549
+ if (!coap_security_handler_is_started ( session -> sec_handler ) ) {
550
+ uint8_t * pw = ns_dyn_mem_alloc (64 );
541
551
uint8_t pw_len ;
542
552
if ( sock -> parent -> _get_password_cb && 0 == sock -> parent -> _get_password_cb (sock -> listen_socket , src_address .address , src_address .identifier , pw , & pw_len )){
543
553
//TODO: get_password_cb should support certs and PSK also
@@ -560,7 +570,7 @@ static void secure_recv_sckt_msg(void *cb_res)
560
570
if ( sock -> parent -> _security_done_cb ){
561
571
sock -> parent -> _security_done_cb (sock -> listen_socket , src_address .address ,
562
572
src_address .identifier ,
563
- session -> sec_handler -> _keyblk . value );
573
+ ( void * ) coap_security_handler_keyblock ( session -> sec_handler ) );
564
574
}
565
575
} else if (ret < 0 ){
566
576
// error handling
@@ -641,8 +651,8 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
641
651
642
652
session -> last_contact_time = coap_service_get_internal_timer_ticks ();
643
653
644
- if (!session -> sec_handler -> _is_started ) {
645
- uint8_t * pw = ( uint8_t * ) ns_dyn_mem_alloc (64 );
654
+ if (!coap_security_handler_is_started ( session -> sec_handler ) ) {
655
+ uint8_t * pw = ns_dyn_mem_alloc (64 );
646
656
uint8_t pw_len ;
647
657
if (sock -> parent -> _get_password_cb && 0 == sock -> parent -> _get_password_cb (sock -> listen_socket , address , port , pw , & pw_len )) {
648
658
//TODO: get_password_cb should support certs and PSK also
@@ -665,7 +675,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
665
675
if ( handler -> _security_done_cb ){
666
676
handler -> _security_done_cb (sock -> listen_socket ,
667
677
address , port ,
668
- session -> sec_handler -> _keyblk . value );
678
+ ( void * ) coap_security_handler_keyblock ( session -> sec_handler ) );
669
679
}
670
680
return 0 ;
671
681
}
@@ -807,7 +817,7 @@ int coap_connection_handler_send_data(coap_conn_handler_t *handler, const ns_add
807
817
memcpy ( handler -> socket -> dest_addr .address , dest_addr -> address , 16 );
808
818
handler -> socket -> dest_addr .identifier = dest_addr -> identifier ;
809
819
handler -> socket -> dest_addr .type = dest_addr -> type ;
810
- uint8_t * pw = ( uint8_t * ) ns_dyn_mem_alloc (64 );
820
+ uint8_t * pw = ns_dyn_mem_alloc (64 );
811
821
if (!pw ) {
812
822
//todo: free secure session?
813
823
return -1 ;
0 commit comments