1
1
// Copyright 2025 - Nym Technologies SA <[email protected] >
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
- use crate :: config:: { NoiseConfig , NoisePattern } ;
4
+ use crate :: config:: NoiseConfig ;
5
5
use crate :: connection:: Connection ;
6
6
use crate :: error:: NoiseError ;
7
7
use crate :: stream:: NoiseStream ;
@@ -19,12 +19,11 @@ pub mod stream;
19
19
20
20
const NOISE_PSK_PREFIX : & [ u8 ] = b"NYMTECH_NOISE_dQw4w9WgXcQ" ;
21
21
22
- pub const NOISE_VERSION : NoiseVersion = NoiseVersion :: V1 ;
22
+ pub const LATEST_NOISE_VERSION : NoiseVersion = NoiseVersion :: V1 ;
23
23
24
24
async fn upgrade_noise_initiator_v1 (
25
25
conn : TcpStream ,
26
- pattern : NoisePattern ,
27
- local_private_key : & x25519:: PrivateKey ,
26
+ config : & NoiseConfig ,
28
27
remote_pub_key : & x25519:: PublicKey ,
29
28
) -> Result < Connection , NoiseError > {
30
29
trace ! ( "Perform Noise Handshake, initiator side" ) ;
@@ -36,10 +35,10 @@ async fn upgrade_noise_initiator_v1(
36
35
. concat ( ) ;
37
36
let secret_hash = Sha256 :: digest ( secret) ;
38
37
39
- let handshake = Builder :: new ( pattern. as_str ( ) . parse ( ) ?)
40
- . local_private_key ( & local_private_key . to_bytes ( ) )
38
+ let handshake = Builder :: new ( config . pattern . as_str ( ) . parse ( ) ?)
39
+ . local_private_key ( & config . local_key . private_key ( ) . to_bytes ( ) )
41
40
. remote_public_key ( & remote_pub_key. to_bytes ( ) )
42
- . psk ( pattern. psk_position ( ) , & secret_hash)
41
+ . psk ( config . pattern . psk_position ( ) , & secret_hash)
43
42
. build_initiator ( ) ?;
44
43
45
44
let noise_stream = NoiseStream :: new ( conn, handshake) ;
@@ -64,51 +63,39 @@ pub async fn upgrade_noise_initiator(
64
63
65
64
match config. get_noise_key ( & responder_addr) {
66
65
Some ( key) => match key. version {
67
- NoiseVersion :: V1 => {
68
- upgrade_noise_initiator_v1 (
69
- conn,
70
- config. pattern ,
71
- config. local_key . private_key ( ) ,
72
- & key. x25519_pubkey ,
73
- )
74
- . await
75
- }
66
+ NoiseVersion :: V1 => upgrade_noise_initiator_v1 ( conn, config, & key. x25519_pubkey ) . await ,
67
+ // We're talking to a more recent node, but we can't adapt. Let's try to do our best and if it fails, it fails.
68
+ // If that node sees we're older, it will try to adapt too.
76
69
NoiseVersion :: Unknown => {
77
- error ! (
78
- "{:?} is announcing an unknown version of Noise" ,
79
- responder_addr
80
- ) ;
81
- Err ( NoiseError :: UnknownVersion )
70
+ warn ! ( "{responder_addr} is announcing an unknown version of Noise, we will still attempt our latest known version" ) ;
71
+ upgrade_noise_initiator_v1 ( conn, config, & key. x25519_pubkey )
72
+ . await
73
+ . or ( Err ( NoiseError :: UnknownVersion ) )
82
74
}
83
75
} ,
84
76
None => {
85
- warn ! (
86
- "{:?} can't speak Noise yet, falling back to TCP" ,
87
- responder_addr
88
- ) ;
77
+ warn ! ( "{responder_addr} can't speak Noise yet, falling back to TCP" ) ;
89
78
Ok ( Connection :: Tcp ( conn) )
90
79
}
91
80
}
92
81
}
93
82
94
83
async fn upgrade_noise_responder_v1 (
95
84
conn : TcpStream ,
96
- pattern : NoisePattern ,
97
- local_public_key : & x25519:: PublicKey ,
98
- local_private_key : & x25519:: PrivateKey ,
85
+ config : & NoiseConfig ,
99
86
) -> Result < Connection , NoiseError > {
100
87
trace ! ( "Perform Noise Handshake, responder side" ) ;
101
88
102
89
let secret = [
103
90
NOISE_PSK_PREFIX . to_vec ( ) ,
104
- local_public_key . to_bytes ( ) . to_vec ( ) ,
91
+ config . local_key . public_key ( ) . to_bytes ( ) . to_vec ( ) ,
105
92
]
106
93
. concat ( ) ;
107
94
let secret_hash = Sha256 :: digest ( secret) ;
108
95
109
- let handshake = Builder :: new ( pattern. as_str ( ) . parse ( ) ?)
110
- . local_private_key ( & local_private_key . to_bytes ( ) )
111
- . psk ( pattern. psk_position ( ) , & secret_hash)
96
+ let handshake = Builder :: new ( config . pattern . as_str ( ) . parse ( ) ?)
97
+ . local_private_key ( & config . local_key . private_key ( ) . to_bytes ( ) )
98
+ . psk ( config . pattern . psk_position ( ) , & secret_hash)
112
99
. build_responder ( ) ?;
113
100
114
101
let noise_stream = NoiseStream :: new ( conn, handshake) ;
@@ -140,16 +127,16 @@ pub async fn upgrade_noise_responder(
140
127
warn ! ( "{initiator_addr} can't speak Noise yet, falling back to TCP" , ) ;
141
128
Ok ( Connection :: Tcp ( conn) )
142
129
}
143
- //responder's info on version is shaky, so initiator has to adapt. This behavior can change in the future
144
- Some ( _ ) => {
145
- //Existing node supporting Noise
146
- upgrade_noise_responder_v1 (
147
- conn ,
148
- config. pattern ,
149
- config . local_key . public_key ( ) ,
150
- config . local_key . private_key ( ) ,
151
- )
152
- . await
153
- }
130
+ // responder's info on version is shaky, so ideally, initiator has to adapt.
131
+ // if we are newer, it won't ba able to, so let's try to meet him on his ground.
132
+ Some ( LATEST_NOISE_VERSION ) | Some ( NoiseVersion :: Unknown ) => {
133
+ // Node is announcing the same version as us, great or
134
+ // Node is announcing a newer version than us, it should adapt to us though
135
+ upgrade_noise_responder_v1 ( conn , config) . await
136
+ } //SW sample of code to allow backwards compatibility when we introduce new versions
137
+ // Some(IntermediateNoiseVersion) => {
138
+ // Node is announcing an older version, let's try to adapt
139
+ // upgrade_noise_responder_Vwhatever
140
+ // }
154
141
}
155
142
}
0 commit comments