1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
// hbak_common is the main hbak library implementing the protocol shared logic.
// Copyright (C) 2024 Himbeer <himbeerserverde@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::config::RemoteNodeAuth;
use crate::message::*;
use crate::proto::Snapshot;
use crate::stream::CHUNKSIZE;
use crate::system;
use crate::{NetworkError, RemoteError};
use std::io::{self, BufRead, BufReader, BufWriter, Write};
use std::marker::PhantomData;
use std::net::{SocketAddr, TcpStream};
use std::ops::DerefMut;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use chacha20poly1305::aead::generic_array::GenericArray;
use chacha20poly1305::aead::stream::{DecryptorBE32, EncryptorBE32};
use chacha20poly1305::{Key, XChaCha20Poly1305};
use subtle::ConstantTimeEq;
/// Default TCP server port. Not officially reserved.
/// 406 is the sum of the ASCII codes for `hbak` and an offset to the 20000 port range.
pub const DEFAULT_PORT: u16 = 20406;
/// TCP connect timeout. Connection attempt is aborted if remote doesn't respond.
pub const CONNECT_TIMEOUT: Duration = Duration::from_secs(30);
/// TCP read timeout. Used for cancellation of [`StreamConn::data_sync`] receive thread
/// and `hbakd` TCP accept loop.
pub const READ_TIMEOUT: Duration = Duration::from_millis(200);
mod private {
pub trait Sealed {}
}
/// A valid phase of a [`StreamConn`].
pub trait Phase: private::Sealed {}
impl private::Sealed for Idle {}
impl private::Sealed for Active {}
impl Phase for Idle {}
impl Phase for Active {}
/// The `Idle` phase of a [`StreamConn`].
///
/// No stream setup or timestamp synchronization has occured and transmissions are not allowed.
pub struct Idle;
/// The `Active` phase of a [`StreamConn`].
///
/// Timestamp synchronization has succeeded and transmissions are allowed and possibly in progress.
pub struct Active;
/// An `AuthConn` attempts mutual authentication between the local node
/// and a remote [`AuthServ`], transforming into a [`StreamConn`] on success.
pub struct AuthConn {
stream: TcpStream,
}
impl AuthConn {
/// Shorthand for `AuthConn::from(TcpStream::connect_timeout(addr, CONNECT_TIMEOUT)?)`.
///
/// This is a low-level constructor that should not be used for dual stack connectivity.
/// Use [`AuthConn::new_first_success`] unless its behavior is unsuitable.
pub fn new(addr: &SocketAddr) -> Result<Self, NetworkError> {
Ok(TcpStream::connect_timeout(addr, CONNECT_TIMEOUT)?.into())
}
/// Iterates over the passed addresses until a connection succeeds
/// or there are no more addresses left to try.
///
/// This is useful for dual stack connectivity and should replace the low-level
/// [`AuthConn::new`] constructor in most cases.
pub fn new_first_success<A>(addrs: A) -> Result<Self, NetworkError>
where
A: Iterator<Item = SocketAddr> + ExactSizeIterator + Clone,
{
for addr in addrs.clone() {
match Self::new(&addr) {
Ok(conn) => return Ok(conn),
// Stable version of [`ExactSizeIterator::is_empty`] (tracking issue: #35428).
Err(e) if addrs.len() == 0 => return Err(e),
_ => {}
}
}
Err(NetworkError::NoAddrs)
}
/// Performs mutual authentication and encryption of the connection
/// using the provided node name and passphrase,
/// returning a [`StreamConn`] on success.
pub fn secure_stream<P: AsRef<[u8]>>(
self,
node_name: String,
remote_node_name: String,
passphrase: P,
) -> Result<StreamConn<Idle>, NetworkError> {
// Consuming the `AuthConn` guarantees that this function can never be called again.
let challenge = system::random_bytes(32);
let nonce = system::random_bytes(19);
let key;
self.send_message(&CryptoMessage::Hello(Hello {
node_name,
challenge: challenge.clone(),
nonce: nonce.clone(),
}))?;
match self.recv_message()? {
CryptoMessage::ServerAuth(server_auth) => {
let server_auth = server_auth?;
key = system::derive_key(&server_auth.verifier, &passphrase)?;
let server_proof = system::hash_hmac(&key, &challenge);
if server_auth.proof.ct_eq(&server_proof).into() {
let proof = system::hash_hmac(&key, &server_auth.challenge);
self.send_message(&CryptoMessage::ClientAuth(Ok(ClientAuth { proof })))?;
} else {
self.send_message(&CryptoMessage::ClientAuth(Err(RemoteError::AccessDenied)))?;
return Err(RemoteError::Unauthorized.into());
}
}
_ => {
self.send_message(&CryptoMessage::ClientAuth(Err(
RemoteError::IllegalTransition,
)))?;
return Err(NetworkError::IllegalTransition);
}
}
match self.recv_message()? {
CryptoMessage::Encrypt(encrypt) => {
encrypt?;
Ok(StreamConn::try_from_conn(
self.stream,
key,
nonce,
remote_node_name,
)?)
}
_ => {
self.send_message(&CryptoMessage::Error(RemoteError::IllegalTransition))?;
Err(NetworkError::IllegalTransition)
}
}
}
fn send_message(&self, message: &CryptoMessage) -> Result<(), NetworkError> {
let buf = bincode::serialize(message)?;
(&self.stream).write_all(&buf)?;
Ok(())
}
fn recv_message(&self) -> Result<CryptoMessage, NetworkError> {
Ok(bincode::deserialize_from(&self.stream)?)
}
}
impl From<TcpStream> for AuthConn {
fn from(stream: TcpStream) -> Self {
Self { stream }
}
}
/// An `AuthServ` attempts mutual authentication between the local node
/// and a remote [`AuthConn`], transforming into a [`StreamConn`] on success.
pub struct AuthServ {
stream: TcpStream,
}
impl AuthServ {
/// Performs mutual authentication and encryption of the connection
/// using the provided authentication storage,
/// returning a [`StreamConn`] on success.
pub fn secure_stream(
self,
auth_storage: impl IntoIterator<Item = RemoteNodeAuth>,
) -> Result<(StreamConn<Idle>, RemoteNodeAuth), NetworkError> {
// Consuming the `AuthServ` guarantees that this function can never be called again.
let challenge = system::random_bytes(32);
let nonce;
let key;
let remote_node_auth;
let remote_node_name;
let client_proof;
match self.recv_message()? {
CryptoMessage::Hello(hello) => {
let auth = auth_storage
.into_iter()
.find(|rna| rna.node_name == hello.node_name);
if let Some(auth) = auth {
nonce = hello.nonce;
key = auth.key.clone();
remote_node_auth = auth;
remote_node_name = hello.node_name;
client_proof = system::hash_hmac(&key, &challenge);
let proof = system::hash_hmac(&key, &hello.challenge);
self.send_message(&CryptoMessage::ServerAuth(Ok(ServerAuth {
verifier: remote_node_auth.verifier.clone(),
challenge,
proof,
})))?;
} else {
self.send_message(&CryptoMessage::ServerAuth(Err(RemoteError::AccessDenied)))?;
return Err(RemoteError::Unauthorized.into());
}
}
_ => {
self.send_message(&CryptoMessage::ServerAuth(Err(
RemoteError::IllegalTransition,
)))?;
return Err(NetworkError::IllegalTransition);
}
}
match self.recv_message()? {
CryptoMessage::ClientAuth(client_auth) => {
let client_auth = client_auth?;
if client_auth.proof.ct_eq(&client_proof).into() {
self.send_message(&CryptoMessage::Encrypt(Ok(())))?;
Ok((
StreamConn::try_from_conn(self.stream, key, nonce, remote_node_name)?,
remote_node_auth,
))
} else {
self.send_message(&CryptoMessage::Encrypt(Err(RemoteError::AccessDenied)))?;
Err(RemoteError::Unauthorized.into())
}
}
_ => {
self.send_message(&CryptoMessage::Encrypt(Err(RemoteError::IllegalTransition)))?;
Err(NetworkError::IllegalTransition)
}
}
}
fn send_message(&self, message: &CryptoMessage) -> Result<(), NetworkError> {
let buf = bincode::serialize(message)?;
(&self.stream).write_all(&buf)?;
Ok(())
}
fn recv_message(&self) -> Result<CryptoMessage, NetworkError> {
Ok(bincode::deserialize_from(&self.stream)?)
}
}
impl From<TcpStream> for AuthServ {
fn from(stream: TcpStream) -> Self {
Self { stream }
}
}
/// A `StreamConn` can be used to exchange synchronization information (timestamps)
/// and provides circuit-switched access to snapshot storage.
/// It is the result of successful authentication and encryption
/// using an [`AuthConn`] or an [`AuthServ`].
pub struct StreamConn<P: Phase> {
stream_read: Mutex<BufReader<TcpStream>>,
stream_write: Mutex<BufWriter<TcpStream>>,
encryptor: Mutex<EncryptorBE32<XChaCha20Poly1305>>,
decryptor: Mutex<DecryptorBE32<XChaCha20Poly1305>>,
remote_node_name: String,
_phase: PhantomData<P>,
}
impl<P: Phase> StreamConn<P> {
/// Returns the name of the remote node.
pub fn remote_node_name(&self) -> &str {
&self.remote_node_name
}
fn send_message(&self, message: &StreamMessage) -> Result<(), NetworkError> {
let plaintext = bincode::serialize(message)?;
let ciphertext = self
.encryptor
.lock()
.unwrap()
.encrypt_next(plaintext.as_slice())?;
let mut w = self.stream_write.lock().unwrap();
bincode::serialize_into(w.deref_mut(), &RawMessage(ciphertext))?;
w.flush()?;
Ok(())
}
fn recv_message(&self) -> Result<StreamMessage, NetworkError> {
let ciphertext: RawMessage =
bincode::deserialize_from(self.stream_read.lock().unwrap().deref_mut())?;
let plaintext = self
.decryptor
.lock()
.unwrap()
.decrypt_next(ciphertext.0.as_slice())?;
Ok(bincode::deserialize(&plaintext)?)
}
}
impl StreamConn<Idle> {
/// Constructs a new `StreamConn` from a [`std::net::TcpStream`],
/// encryption key and nonce.
pub(crate) fn try_from_conn(
stream: TcpStream,
key: Vec<u8>,
nonce: Vec<u8>,
remote_node_name: String,
) -> io::Result<Self> {
stream.set_read_timeout(Some(READ_TIMEOUT))?;
let key = Key::from_slice(&key);
let nonce = GenericArray::from_slice(&nonce);
Ok(Self {
stream_read: Mutex::new(BufReader::with_capacity(2 * CHUNKSIZE, stream.try_clone()?)),
stream_write: Mutex::new(BufWriter::with_capacity(2 * CHUNKSIZE, stream)),
encryptor: Mutex::new(EncryptorBE32::new(key, nonce)),
decryptor: Mutex::new(DecryptorBE32::new(key, nonce)),
remote_node_name,
_phase: PhantomData,
})
}
/// Exchanges synchronization information (timestamps), returning an `Active` `StreamConn`
/// that can send and receive data.
pub fn meta_sync(
self,
sync_info: SyncInfo,
) -> Result<(StreamConn<Active>, SyncInfo), NetworkError> {
self.send_message(&StreamMessage::SyncInfo(sync_info))?;
match self.recv_message()? {
StreamMessage::SyncInfo(sync_info) => Ok((
StreamConn::<Active> {
stream_read: self.stream_read,
stream_write: self.stream_write,
encryptor: self.encryptor,
decryptor: self.decryptor,
remote_node_name: self.remote_node_name,
_phase: PhantomData,
},
sync_info,
)),
_ => {
self.send_message(&StreamMessage::Error(RemoteError::IllegalTransition))?;
Err(NetworkError::IllegalTransition)
}
}
}
}
impl StreamConn<Active> {
/// Transmits the passed [`std::io::Read`]s using their associated metadata.
/// Receives remote transmissions using the provided stream setup closure.
pub fn data_sync<B, W, I, S, F>(
self,
tx: I,
rx_setup: S,
rx_finish: F,
) -> Result<(), NetworkError>
where
B: BufRead,
W: Write + Send,
I: IntoIterator<Item = (B, Snapshot)> + Send,
S: Fn(&Snapshot) -> Result<W, RemoteError> + Sync,
F: Fn(Snapshot) -> Result<(), RemoteError> + Sync,
{
let mut stream = None;
let start_streaming = Arc::new(Mutex::new(false));
let mut handle = |message| -> Result<bool, NetworkError> {
match message {
StreamMessage::Stream(stream) => {
*start_streaming.lock().unwrap() = true;
stream?;
}
StreamMessage::Replicate(replicate) => {
if stream.is_none() {
match rx_setup(&replicate.snapshot) {
Ok(w) => {
stream = Some((w, replicate.snapshot));
self.send_message(&StreamMessage::Stream(Ok(())))?;
}
Err(e) => {
self.send_message(&StreamMessage::Stream(Err(e.clone())))?;
return Err(e.into());
}
}
} else {
self.send_message(&StreamMessage::Stream(Err(
RemoteError::AlreadyStreaming,
)))?;
}
}
StreamMessage::Chunk(chunk) => {
if let Some(stream) = &mut stream {
match stream.0.write_all(&chunk) {
Ok(_) => {}
Err(e) => {
self.send_message(&StreamMessage::Error(RemoteError::RxError))?;
return Err(e.into());
}
}
} else {
self.send_message(&StreamMessage::Error(RemoteError::NotStreaming))?;
}
}
StreamMessage::End(end) => {
end?;
if let Some(current_stream) = stream.take() {
drop(current_stream.0);
if let Err(e) = rx_finish(current_stream.1) {
self.send_message(&StreamMessage::Error(e.clone()))?;
return Err(e.into());
}
} else {
self.send_message(&StreamMessage::Error(RemoteError::NotStreaming))?;
}
}
StreamMessage::Done => return Ok(true),
StreamMessage::Error(e) => return Err(e.into()),
_ => {
self.send_message(&StreamMessage::Error(RemoteError::IllegalTransition))?;
return Err(NetworkError::IllegalTransition);
}
}
Ok(false)
};
let send_chunk = |r: &mut B| -> Result<bool, NetworkError> {
let mut chunk = vec![0; 16 + CHUNKSIZE];
let n = r.read(&mut chunk)?;
chunk.truncate(n);
if !chunk.is_empty() {
self.send_message(&StreamMessage::Chunk(chunk))?;
Ok(true)
} else {
self.send_message(&StreamMessage::End(Ok(())))?;
Ok(false)
}
};
let local_done = Mutex::new(false);
thread::scope(|s| {
let mut tx = Some(s.spawn(|| -> Result<(), NetworkError> {
for (mut r, snapshot) in tx.into_iter() {
self.send_message(&StreamMessage::Replicate(snapshot.into()))?;
while !*start_streaming.lock().unwrap() {
thread::sleep(READ_TIMEOUT);
}
*start_streaming.lock().unwrap() = false;
while send_chunk(&mut r)? {}
}
Ok(())
}));
let mut rx = Some(s.spawn(|| -> Result<(), NetworkError> {
let mut remote_done = false;
while !*local_done.lock().unwrap() || !remote_done {
let message = match self.recv_message() {
Ok(message) => message,
Err(NetworkError::Bincode(bincode_err)) => match *bincode_err {
bincode::ErrorKind::Io(io_err)
if io_err.kind() == io::ErrorKind::WouldBlock
|| io_err.kind() == io::ErrorKind::TimedOut =>
{
continue
}
bincode::ErrorKind::Io(io_err)
if io_err.kind() == io::ErrorKind::UnexpectedEof
&& *local_done.lock().unwrap()
&& remote_done =>
{
return Ok(())
}
_ => return Err(bincode_err.into()),
},
Err(e) => return Err(e),
};
if handle(message)? {
remote_done = true;
}
}
Ok(())
}));
let mut remote_done = false;
loop {
// Unlock the local_done mutex before sleeping to prevent receive thread deadlock.
{
let mut local_done = local_done.lock().unwrap();
if tx.as_ref().map(|tx| tx.is_finished()).unwrap_or(false) && !*local_done {
tx.take()
.expect("tx thread already joined")
.join()
.unwrap()?;
*local_done = true;
self.send_message(&StreamMessage::Done)?;
}
if rx.as_ref().map(|rx| rx.is_finished()).unwrap_or(false) && !remote_done {
rx.take()
.expect("rx thread already joined")
.join()
.unwrap()?;
remote_done = true;
}
if *local_done && remote_done {
break;
}
}
thread::sleep(READ_TIMEOUT);
}
Ok(())
})
}
}
|