@@ -221,11 +221,17 @@ pub(crate) async fn connect<'d>(
221221 Ok ( ws_stream)
222222}
223223
224+ pub ( crate ) struct TunnelNoiseState {
225+ pub transport_state : TransportState ,
226+ #[ allow( dead_code) ]
227+ pub handshake_hash : Vec < u8 > ,
228+ }
229+
224230pub ( crate ) async fn do_handshake (
225231 ws_stream : & mut WebSocketStream < MaybeTlsStream < TcpStream > > ,
226232 psk : [ u8 ; 32 ] ,
227233 connection_type : & CableTunnelConnectionType ,
228- ) -> Result < TransportState , Error > {
234+ ) -> Result < TunnelNoiseState , Error > {
229235 let noise_handshake = match connection_type {
230236 CableTunnelConnectionType :: QrCode { private_key, .. } => {
231237 let local_private_key = private_key. to_owned ( ) . to_bytes ( ) ;
@@ -327,11 +333,14 @@ pub(crate) async fn do_handshake(
327333 return Err ( Error :: Transport ( TransportError :: ConnectionFailed ) ) ;
328334 }
329335
330- Ok ( noise_handshake. into_transport_mode ( ) ?)
336+ Ok ( TunnelNoiseState {
337+ handshake_hash : noise_handshake. get_handshake_hash ( ) . to_vec ( ) ,
338+ transport_state : noise_handshake. into_transport_mode ( ) ?,
339+ } )
331340}
332341
333342pub ( crate ) async fn channel (
334- noise_state : TransportState ,
343+ noise_state : TunnelNoiseState ,
335344 tunnel_domain : & str ,
336345 maybe_known_device_store : & Option < Arc < dyn CableKnownDeviceInfoStore > > ,
337346 ws_stream : WebSocketStream < MaybeTlsStream < TcpStream > > ,
@@ -369,7 +378,7 @@ async fn connection(
369378 tunnel_domain : & str ,
370379 known_device_store : & Option < Arc < dyn CableKnownDeviceInfoStore > > ,
371380 mut ws_stream : WebSocketStream < MaybeTlsStream < TcpStream > > ,
372- mut noise_state : TransportState ,
381+ mut noise_state : TunnelNoiseState ,
373382 mut cbor_tx_recv : Receiver < CborRequest > ,
374383 cbor_rx_send : Sender < CborResponse > ,
375384) {
@@ -435,7 +444,7 @@ async fn connection(
435444async fn connection_send (
436445 request : CborRequest ,
437446 ws_stream : & mut WebSocketStream < MaybeTlsStream < TcpStream > > ,
438- noise_state : & mut TransportState ,
447+ noise_state : & mut TunnelNoiseState ,
439448) -> Result < ( ) , Error > {
440449 debug ! ( "Sending CBOR request" ) ;
441450 trace ! ( ?request) ;
@@ -462,7 +471,10 @@ async fn connection_send(
462471 trace ! ( ?frame_serialized) ;
463472
464473 let mut encrypted_frame = vec ! [ 0u8 ; MAX_CBOR_SIZE + 1 ] ;
465- match noise_state. write_message ( & frame_serialized, & mut encrypted_frame) {
474+ match noise_state
475+ . transport_state
476+ . write_message ( & frame_serialized, & mut encrypted_frame)
477+ {
466478 Ok ( size) => {
467479 encrypted_frame. resize ( size, 0u8 ) ;
468480 }
@@ -510,10 +522,13 @@ async fn connection_recv_binary_frame(message: Message) -> Result<Option<Vec<u8>
510522
511523async fn decrypt_frame (
512524 encrypted_frame : Vec < u8 > ,
513- noise_state : & mut TransportState ,
525+ noise_state : & mut TunnelNoiseState ,
514526) -> Result < Vec < u8 > , Error > {
515527 let mut decrypted_frame = vec ! [ 0u8 ; MAX_CBOR_SIZE ] ;
516- match noise_state. read_message ( & encrypted_frame, & mut decrypted_frame) {
528+ match noise_state
529+ . transport_state
530+ . read_message ( & encrypted_frame, & mut decrypted_frame)
531+ {
517532 Ok ( size) => {
518533 debug ! ( decrypted_frame_len = size, "Decrypted CBOR response" ) ;
519534 decrypted_frame. resize ( size, 0u8 ) ;
@@ -538,7 +553,7 @@ async fn decrypt_frame(
538553
539554async fn connection_recv_initial (
540555 message : Message ,
541- noise_state : & mut TransportState ,
556+ noise_state : & mut TunnelNoiseState ,
542557) -> Result < Vec < u8 > , Error > {
543558 let Some ( encrypted_frame) = connection_recv_binary_frame ( message) . await ? else {
544559 return Err ( Error :: Transport ( TransportError :: ConnectionFailed ) ) ;
@@ -633,7 +648,7 @@ async fn connection_recv(
633648 known_device_store : & Option < Arc < dyn CableKnownDeviceInfoStore > > ,
634649 message : Message ,
635650 cbor_rx_send : & Sender < CborResponse > ,
636- noise_state : & mut TransportState ,
651+ noise_state : & mut TunnelNoiseState ,
637652) -> Result < ( ) , Error > {
638653 let Some ( encrypted_frame) = connection_recv_binary_frame ( message) . await ? else {
639654 return Ok ( ( ) ) ;
@@ -685,21 +700,24 @@ async fn connection_recv(
685700
686701 let device_id: CableKnownDeviceId = ( & linking_info) . into ( ) ;
687702 match known_device_store {
688- Some ( store) => match parse_known_device ( tunnel_domain, & device_id, & linking_info) {
689- Ok ( known_device) => {
690- debug ! ( ?device_id, "Updating known device" ) ;
691- trace ! ( ?known_device) ;
692- store. put_known_device ( & device_id, & known_device) . await ;
693- }
694- Err ( e) => {
695- error ! (
696- ?e,
697- "Invalid update message from authenticator, forgetting device"
698- ) ;
699- store. delete_known_device ( & device_id) . await ;
700- return Err ( Error :: Transport ( TransportError :: TransportUnavailable ) ) ;
703+ Some ( store) => {
704+ match parse_known_device ( tunnel_domain, & device_id, & linking_info, & noise_state)
705+ {
706+ Ok ( known_device) => {
707+ debug ! ( ?device_id, "Updating known device" ) ;
708+ trace ! ( ?known_device) ;
709+ store. put_known_device ( & device_id, & known_device) . await ;
710+ }
711+ Err ( e) => {
712+ error ! (
713+ ?e,
714+ "Invalid update message from authenticator, forgetting device"
715+ ) ;
716+ store. delete_known_device ( & device_id) . await ;
717+ return Err ( Error :: Transport ( TransportError :: TransportUnavailable ) ) ;
718+ }
701719 }
702- } ,
720+ }
703721 None => {
704722 warn ! ( "Ignoring update message without a device store" ) ;
705723 }
@@ -714,6 +732,7 @@ fn parse_known_device(
714732 tunnel_domain : & str ,
715733 _device_id : & CableKnownDeviceId ,
716734 linking_info : & CableLinkingInfo ,
735+ _noise_state : & TunnelNoiseState ,
717736) -> Result < CableKnownDeviceInfo , Error > {
718737 let known_device = CableKnownDeviceInfo :: new ( tunnel_domain, linking_info) ?;
719738
0 commit comments