@@ -141,7 +141,7 @@ static int8_t virtual_socket_id_allocate()
141
141
return new_virtual_socket_id ;
142
142
}
143
143
144
- static secure_session_t * secure_session_create (internal_socket_t * parent , const uint8_t * address_ptr , uint16_t port )
144
+ static secure_session_t * secure_session_create (internal_socket_t * parent , const uint8_t * address_ptr , uint16_t port , SecureConnectionMode secure_mode )
145
145
{
146
146
if (!address_ptr ){
147
147
return NULL ;
@@ -184,7 +184,7 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const
184
184
memcpy (this -> remote_host .address , address_ptr , 16 );
185
185
this -> remote_host .identifier = port ;
186
186
187
- this -> sec_handler = coap_security_create (parent -> socket , this -> timer .id , this , ECJPAKE ,
187
+ this -> sec_handler = coap_security_create (parent -> socket , this -> timer .id , this , secure_mode ,
188
188
& secure_session_sendto , & secure_session_recvfrom , & start_timer , & timer_status );
189
189
if ( !this -> sec_handler ){
190
190
ns_dyn_mem_free (this );
@@ -595,31 +595,34 @@ static void secure_recv_sckt_msg(void *cb_res)
595
595
596
596
// Create session
597
597
if (!session ) {
598
- session = secure_session_create (sock , src_address .address , src_address .identifier );
599
- }
600
- if (!session ) {
601
- tr_err ("secure_recv_sckt_msg session creation failed - OOM" );
602
- return ;
603
- }
604
- // Record the destination. We are not strict on local address - all
605
- // session_find calls match only on remote address and port. But we
606
- // record the last-used destination address to use it as the source of
607
- // outgoing packets.
608
- memcpy (session -> local_address , dst_address , 16 );
609
- session -> last_contact_time = coap_service_get_internal_timer_ticks ();
610
- // Start handshake
611
- if (!coap_security_handler_is_started (session -> sec_handler ) ){
612
- uint8_t * pw = ns_dyn_mem_alloc (64 );
613
- uint8_t pw_len ;
614
- if ( sock -> parent -> _get_password_cb && 0 == sock -> parent -> _get_password_cb (sock -> socket , src_address .address , src_address .identifier , pw , & pw_len )){
615
- //TODO: get_password_cb should support certs and PSK also
616
- coap_security_keys_t keys ;
617
- keys ._priv = pw ;
618
- keys ._priv_len = pw_len ;
619
- coap_security_handler_connect_non_blocking (session -> sec_handler , true, DTLS , keys , sock -> timeout_min , sock -> timeout_max );
598
+ coap_security_keys_t keys ;
599
+ memset (& keys , 0 , sizeof (coap_security_keys_t ));
600
+
601
+ if (sock -> parent -> _get_password_cb && 0 == sock -> parent -> _get_password_cb (sock -> socket , src_address .address , src_address .identifier , & keys )) {
602
+ session = secure_session_create (sock , src_address .address , src_address .identifier , keys .mode );
603
+ if (!session ) {
604
+ tr_err ("secure_recv_sckt_msg session creation failed - OOM" );
605
+ ns_dyn_mem_free (keys ._key );
606
+ return ;
607
+ }
620
608
//TODO: error handling
609
+ } else {
610
+ return ;
611
+ }
612
+
613
+ // Record the destination. We are not strict on local address - all
614
+ // session_find calls match only on remote address and port. But we
615
+ // record the last-used destination address to use it as the source of
616
+ // outgoing packets.
617
+ memcpy (session -> local_address , dst_address , 16 );
618
+
619
+ session -> last_contact_time = coap_service_get_internal_timer_ticks ();
620
+ // Start handshake
621
+ if (!coap_security_handler_is_started (session -> sec_handler )) {
622
+ coap_security_handler_connect_non_blocking (session -> sec_handler , true, DTLS , keys , sock -> timeout_min , sock -> timeout_max );
623
+ ns_dyn_mem_free (keys ._key );
624
+
621
625
}
622
- ns_dyn_mem_free (pw );
623
626
} else {
624
627
//Continue handshake
625
628
if (session -> session_state == SECURE_SESSION_HANDSHAKE_ONGOING ) {
@@ -703,34 +706,29 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
703
706
}
704
707
705
708
if (handler -> socket -> is_secure ) {
709
+ coap_security_keys_t keys ;
710
+ memset (& keys , 0 , sizeof (coap_security_keys_t ));
711
+
706
712
secure_session_t * session = secure_session_find (sock , address , port );
707
713
if (!session ) {
708
- session = secure_session_create (sock , address , port );
709
- }
710
- if (!session ) {
711
- tr_err ("coap_connection_handler_virtual_recv session creation failed - OOM" );
712
- return -1 ;
713
- }
714
-
715
- session -> last_contact_time = coap_service_get_internal_timer_ticks ();
716
-
717
- if (!coap_security_handler_is_started (session -> sec_handler )) {
718
- uint8_t * pw = ns_dyn_mem_alloc (64 );
719
- uint8_t pw_len ;
720
- if (sock -> parent -> _get_password_cb && 0 == sock -> parent -> _get_password_cb (sock -> socket , address , port , pw , & pw_len )) {
721
- //TODO: get_password_cb should support certs and PSK also
722
- coap_security_keys_t keys ;
723
- keys ._priv = pw ;
724
- keys ._priv_len = pw_len ;
714
+ if (sock -> parent -> _get_password_cb && 0 == sock -> parent -> _get_password_cb (sock -> socket , address , port , & keys )) {
715
+ session = secure_session_create (sock , address , port , keys .mode );
716
+ if (!session ) {
717
+ tr_err ("coap_connection_handler_virtual_recv session creation failed - OOM" );
718
+ ns_dyn_mem_free (keys ._key );
719
+ return -1 ;
720
+ }
725
721
coap_security_handler_connect_non_blocking (session -> sec_handler , true, DTLS , keys , handler -> socket -> timeout_min , handler -> socket -> timeout_max );
726
- //TODO: error handling
727
- ns_dyn_mem_free (pw );
722
+ ns_dyn_mem_free (keys ._key );
728
723
return 0 ;
729
724
} else {
730
- ns_dyn_mem_free (pw );
731
725
return -1 ;
732
726
}
733
- } else {
727
+ }
728
+
729
+ session -> last_contact_time = coap_service_get_internal_timer_ticks ();
730
+
731
+ if (coap_security_handler_is_started (session -> sec_handler )) {
734
732
if (session -> session_state == SECURE_SESSION_HANDSHAKE_ONGOING ) {
735
733
int ret = coap_security_handler_continue_connecting (session -> sec_handler );
736
734
if (ret == 0 ){
@@ -813,6 +811,9 @@ void connection_handler_destroy(coap_conn_handler_t *handler, bool multicast_gro
813
811
if (multicast_group_leave ) {
814
812
coap_multicast_group_join_or_leave (handler -> socket -> socket , SOCKET_IPV6_LEAVE_GROUP , handler -> socket_interface_selection );
815
813
}
814
+ if (handler -> security_keys ) {
815
+ ns_dyn_mem_free (handler -> security_keys );
816
+ }
816
817
int_socket_delete (handler -> socket );
817
818
ns_dyn_mem_free (handler );
818
819
}
@@ -873,30 +874,24 @@ int coap_connection_handler_send_data(coap_conn_handler_t *handler, const ns_add
873
874
handler -> socket -> bypass_link_sec = bypass_link_sec ;
874
875
secure_session_t * session = secure_session_find (handler -> socket , dest_addr -> address , dest_addr -> identifier );
875
876
if (!session ) {
876
- session = secure_session_create (handler -> socket , dest_addr -> address , dest_addr -> identifier );
877
- if (!session ) {
878
- return -1 ;
879
- }
880
- session -> last_contact_time = coap_service_get_internal_timer_ticks ();
881
- uint8_t * pw = ns_dyn_mem_alloc (64 );
882
- if (!pw ) {
883
- //todo: free secure session?
877
+ coap_security_keys_t security_material ;
878
+ memset (& security_material , 0 , sizeof (coap_security_keys_t ));
879
+
880
+ if (!handler -> _get_password_cb || 0 != handler -> _get_password_cb (handler -> socket -> socket , (uint8_t * )dest_addr -> address , dest_addr -> identifier , & security_material )) {
884
881
return -1 ;
885
882
}
886
- uint8_t pw_len ;
887
- if (handler -> _get_password_cb && 0 == handler -> _get_password_cb (handler -> socket -> socket , (uint8_t * )dest_addr -> address , dest_addr -> identifier , pw , & pw_len )) {
888
- //TODO: get_password_cb should support certs and PSK also
889
- coap_security_keys_t keys ;
890
- keys ._priv = pw ;
891
- keys ._priv_len = pw_len ;
892
- coap_security_handler_connect_non_blocking (session -> sec_handler , false, DTLS , keys , handler -> socket -> timeout_min , handler -> socket -> timeout_max );
893
- ns_dyn_mem_free (pw );
894
- return -2 ;
895
- } else {
896
- //free secure session?
897
- ns_dyn_mem_free (pw );
883
+
884
+ session = secure_session_create (handler -> socket , dest_addr -> address , dest_addr -> identifier , security_material .mode );
885
+ if (!session ) {
886
+ ns_dyn_mem_free (security_material ._key );
898
887
return -1 ;
899
888
}
889
+ session -> last_contact_time = coap_service_get_internal_timer_ticks ();
890
+
891
+ coap_security_handler_connect_non_blocking (session -> sec_handler , false, DTLS , security_material , handler -> socket -> timeout_min , handler -> socket -> timeout_max );
892
+ ns_dyn_mem_free (security_material ._key );
893
+ return -2 ;
894
+
900
895
} else if (session -> session_state == SECURE_SESSION_OK ) {
901
896
if (coap_security_handler_send_message (session -> sec_handler , data_ptr , data_len ) > 0 ) {
902
897
session -> last_contact_time = coap_service_get_internal_timer_ticks ();
0 commit comments