use std::io::{self, Read, Write};
use std::net::TcpStream;
#[cfg(feature = "replication-tls")]
use std::sync::{Arc, Mutex};
use std::time::Duration;
#[cfg(feature = "replication-tls")]
use std::time::Instant;
#[cfg(feature = "replication-tls")]
use super::message::timed_out;
use super::node::State;
#[cfg(feature = "replication-tls")]
use crate::sync::lock;
use crate::{Error, Result};
#[cfg(feature = "replication-tls")]
const HANDSHAKE: Duration = Duration::from_secs(10);
#[derive(Clone)]
enum Session {
Plain,
#[cfg(feature = "replication-tls")]
Tls(Arc<Mutex<rustls::Connection>>),
}
pub(crate) struct Reading {
tcp: TcpStream,
#[cfg_attr(not(feature = "replication-tls"), expect(dead_code, reason = "plain TCP answers nothing by itself"))]
answers: TcpStream,
session: Session,
#[cfg_attr(not(feature = "replication-tls"), expect(dead_code, reason = "plain TCP keeps nothing back"))]
unread: Vec<u8>,
timeout: Option<Duration>,
}
pub(crate) struct Writing {
tcp: TcpStream,
session: Session,
}
pub(crate) fn accept(state: &State, tcp: TcpStream) -> Result<(Reading, Writing)> {
tcp.set_nodelay(true).map_err(|error| Error::io("a link", error))?;
#[cfg(feature = "replication-tls")]
if let Some(config) = &state.tls.server {
let connection = rustls::ServerConnection::new(Arc::clone(config)).map_err(tls_error)?;
return ends(tcp, Some(rustls::Connection::Server(connection)));
}
let _ = state;
ends(tcp, None)
}
pub(crate) fn connect(state: &State, address: &str, tcp: TcpStream) -> Result<(Reading, Writing)> {
tcp.set_nodelay(true).map_err(|error| Error::io("a link", error))?;
#[cfg(feature = "replication-tls")]
if let Some(config) = &state.tls.client {
let host = address.rsplit_once(':').map_or(address, |(host, _)| host).trim_matches(['[', ']']);
let name = rustls::pki_types::ServerName::try_from(host.to_owned())
.map_err(|_| Error::invalid(format!("{address}: no name TLS checks a certificate against")))?;
let connection = rustls::ClientConnection::new(Arc::clone(config), name).map_err(tls_error)?;
return ends(tcp, Some(rustls::Connection::Client(connection)));
}
refuse_plain_beyond_this_machine(state, &tcp, address)?;
ends(tcp, None)
}
fn refuse_plain_beyond_this_machine(state: &State, tcp: &TcpStream, address: &str) -> Result<()> {
let peer = tcp.peer_addr().map_err(|error| Error::io(format!("a link to {address}"), error))?;
if peer.ip().is_loopback() || state.options.insecure {
return Ok(());
}
Err(Error::invalid(format!("a link to {address}: {}", plain_refused())))
}
pub(crate) fn plain_refused() -> &'static str {
if cfg!(feature = "replication-tls") {
"a link beyond this machine takes TLS, whose roots and certificate a node names, or a node that says \
insecure for plain TCP that anyone on the way reads"
} else {
"a link beyond this machine takes TLS, which this build leaves out: build with the replication-tls feature, \
or say insecure for plain TCP that anyone on the way reads"
}
}
#[cfg(feature = "replication-tls")]
fn ends(tcp: TcpStream, tls: Option<rustls::Connection>) -> Result<(Reading, Writing)> {
let session = match tls {
Some(mut connection) => {
handshake(&mut connection, &tcp)?;
Session::Tls(Arc::new(Mutex::new(connection)))
}
None => Session::Plain,
};
split(tcp, session)
}
#[cfg(feature = "replication-tls")]
fn handshake(connection: &mut rustls::Connection, tcp: &TcpStream) -> Result<()> {
let deadline = Instant::now() + HANDSHAKE;
let too_long = || Error::new(crate::ErrorKind::Unavailable, "a link's TLS handshake took too long");
let failed =
|error: io::Error| if timed_out(&error) { too_long() } else { Error::io("a link's TLS handshake", error) };
let mut socket = tcp;
loop {
let left = deadline.saturating_duration_since(Instant::now());
if left.is_zero() {
return Err(too_long());
}
socket.set_read_timeout(Some(left)).and_then(|()| socket.set_write_timeout(Some(left))).map_err(failed)?;
while connection.wants_write() {
connection.write_tls(&mut socket).map_err(failed)?;
}
if !connection.is_handshaking() {
return Ok(());
}
match connection.read_tls(&mut socket) {
Ok(0) => return Err(Error::new(crate::ErrorKind::Unavailable, "a link ended in its TLS handshake")),
Ok(_) => {}
Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
Err(error) => return Err(failed(error)),
}
if let Err(error) = connection.process_new_packets() {
let _ = connection.write_tls(&mut socket);
return Err(tls_error(error));
}
}
}
#[cfg(not(feature = "replication-tls"))]
fn ends(tcp: TcpStream, tls: Option<()>) -> Result<(Reading, Writing)> {
let _ = tls;
split(tcp, Session::Plain)
}
fn split(tcp: TcpStream, session: Session) -> Result<(Reading, Writing)> {
let clone = || tcp.try_clone().map_err(|error| Error::io("a link", error));
let reading =
Reading { tcp: clone()?, answers: clone()?, session: session.clone(), unread: Vec::new(), timeout: None };
Ok((reading, Writing { tcp, session }))
}
impl Reading {
pub(crate) fn set_timeout(&mut self, timeout: Duration) -> Result<()> {
self.timeout = Some(timeout);
self.tcp.set_read_timeout(Some(timeout)).map_err(|error| Error::io("a link", error))
}
}
impl Read for Reading {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match &self.session {
Session::Plain => self.tcp.read(buf),
#[cfg(feature = "replication-tls")]
Session::Tls(tls) => {
let tls = Arc::clone(tls);
self.read_records(&tls, buf)
}
}
}
}
#[cfg(feature = "replication-tls")]
impl Reading {
fn read_records(&mut self, tls: &Mutex<rustls::Connection>, buf: &mut [u8]) -> io::Result<usize> {
let began = Instant::now();
loop {
match lock(tls).reader().read(buf) {
Ok(read) => return Ok(read),
Err(error) if error.kind() == io::ErrorKind::WouldBlock => {}
Err(error) => return Err(error),
}
if self.unread.is_empty() {
if self.timeout.is_some_and(|timeout| began.elapsed() >= timeout) {
return Err(io::Error::new(io::ErrorKind::TimedOut, "no TLS record came whole in time"));
}
let mut raw = [0; 16 << 10];
let read = self.tcp.read(&mut raw)?;
if read == 0 {
return Ok(0);
}
self.unread.extend_from_slice(&raw[..read]);
}
let mut connection = lock(tls);
let mut rest = &self.unread[..];
let mut closed = false;
while !rest.is_empty() {
closed = connection.read_tls(&mut rest)? == 0;
let ready = connection.process_new_packets().map_err(io::Error::other)?.plaintext_bytes_to_read();
if closed || ready > 0 {
break;
}
}
let fed = if closed { self.unread.len() } else { self.unread.len() - rest.len() };
self.unread.drain(..fed);
while connection.wants_write() {
connection.write_tls(&mut self.answers)?;
}
}
}
}
impl Writing {
pub(crate) fn set_timeout(&self, timeout: Duration) -> Result<()> {
self.tcp.set_write_timeout(Some(timeout)).map_err(|error| Error::io("a link", error))
}
pub(crate) fn write_all(&mut self, bytes: &[u8]) -> io::Result<()> {
match &self.session {
Session::Plain => self.tcp.write_all(bytes),
#[cfg(feature = "replication-tls")]
Session::Tls(tls) => {
let mut rest = bytes;
while !rest.is_empty() {
let mut connection = lock(tls);
let taken = connection.writer().write(rest)?;
while connection.wants_write() {
connection.write_tls(&mut self.tcp)?;
}
if taken == 0 {
return Err(io::Error::new(io::ErrorKind::WriteZero, "a link's TLS took none of a message"));
}
rest = &rest[taken..];
}
Ok(())
}
}
}
}
#[derive(Default)]
pub(crate) struct Tls {
#[cfg(feature = "replication-tls")]
pub(crate) server: Option<Arc<rustls::ServerConfig>>,
#[cfg(feature = "replication-tls")]
pub(crate) client: Option<Arc<rustls::ClientConfig>>,
}
impl Tls {
pub(crate) fn serves(&self) -> bool {
#[cfg(feature = "replication-tls")]
return self.server.is_some();
#[cfg(not(feature = "replication-tls"))]
false
}
pub(crate) fn of(node: &super::Node) -> Result<Tls> {
let Some(asked) = &node.tls else {
return Ok(Tls::default());
};
#[cfg(not(feature = "replication-tls"))]
{
let _ = asked;
Err(Error::invalid("a node's TLS, and the library was built without the replication-tls feature"))
}
#[cfg(feature = "replication-tls")]
{
let server = match (&asked.cert, &asked.key) {
(Some(cert), Some(key)) => Some(Arc::new(server_config(cert, key)?)),
(None, None) => None,
_ => return Err(Error::invalid("a node's TLS serves with a certificate and its key, both")),
};
let client = asked.roots.as_deref().map(client_config).transpose()?.map(Arc::new);
Ok(Tls { server, client })
}
}
}
#[cfg(feature = "replication-tls")]
fn server_config(cert: &std::path::Path, key: &std::path::Path) -> Result<rustls::ServerConfig> {
use rustls::pki_types::pem::PemObject;
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
let chain: Vec<CertificateDer<'static>> = CertificateDer::pem_file_iter(cert)
.and_then(Iterator::collect)
.map_err(|error| Error::invalid(format!("{}: {error}", cert.display())))?;
if chain.is_empty() {
return Err(Error::invalid(format!("{}: no certificate", cert.display())));
}
let key =
PrivateKeyDer::from_pem_file(key).map_err(|error| Error::invalid(format!("{}: {error}", key.display())))?;
rustls::ServerConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider()))
.with_safe_default_protocol_versions()
.and_then(|builder| builder.with_no_client_auth().with_single_cert(chain, key))
.map_err(tls_error)
}
#[cfg(feature = "replication-tls")]
pub(super) fn client_config(roots: &std::path::Path) -> Result<rustls::ClientConfig> {
use rustls::pki_types::CertificateDer;
use rustls::pki_types::pem::PemObject;
let mut store = rustls::RootCertStore::empty();
for root in
CertificateDer::pem_file_iter(roots).map_err(|error| Error::invalid(format!("{}: {error}", roots.display())))?
{
let root = root.map_err(|error| Error::invalid(format!("{}: {error}", roots.display())))?;
store.add(root).map_err(tls_error)?;
}
if store.is_empty() {
return Err(Error::invalid(format!("{}: no certificate to trust", roots.display())));
}
Ok(rustls::ClientConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider()))
.with_safe_default_protocol_versions()
.map_err(tls_error)?
.with_root_certificates(store)
.with_no_client_auth())
}
#[cfg(feature = "replication-tls")]
fn tls_error(error: rustls::Error) -> Error {
Error::invalid(format!("a link's TLS: {error}"))
}
#[cfg(all(test, feature = "replication-tls"))]
mod tests {
use std::io::Read;
use std::net::{TcpListener, TcpStream};
use std::sync::Arc;
use std::time::Duration;
use super::{client_config, ends, server_config};
#[test]
fn a_tls_link_reads_messages_whose_records_end_anywhere_in_a_read() {
let dir = tempfile::tempdir().unwrap();
let certified = rcgen::generate_simple_self_signed(vec!["localhost".to_owned()]).unwrap();
let (cert, key) = (dir.path().join("cert.pem"), dir.path().join("key.pem"));
std::fs::write(&cert, certified.cert.pem()).unwrap();
std::fs::write(&key, certified.signing_key.serialize_pem()).unwrap();
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap();
let message: Vec<u8> = (0..(16 << 10) + 100).map(|i| (i % 251) as u8).collect();
let (sent, count) = (message.clone(), 64);
let server = std::thread::spawn(move || {
let (tcp, _) = listener.accept().unwrap();
let connection = rustls::ServerConnection::new(Arc::new(server_config(&cert, &key).unwrap())).unwrap();
let (_reading, mut writing) = ends(tcp, Some(rustls::Connection::Server(connection))).unwrap();
for _ in 0..count {
writing.write_all(&sent).unwrap();
}
});
let name = rustls::pki_types::ServerName::try_from("localhost").unwrap();
let roots = Arc::new(client_config(&dir.path().join("cert.pem")).unwrap());
let connection = rustls::ClientConnection::new(roots, name).unwrap();
let (mut reading, _writing) =
ends(TcpStream::connect(address).unwrap(), Some(rustls::Connection::Client(connection))).unwrap();
let mut got = Vec::new();
let mut buffer = vec![0; 64 << 10];
while got.len() < message.len() * count {
std::thread::sleep(Duration::from_millis(2));
let read = reading.read(&mut buffer).unwrap();
assert!(read > 0, "the link ended after {} bytes", got.len());
got.extend_from_slice(&buffer[..read]);
}
server.join().unwrap();
assert!(got.chunks(message.len()).all(|one| one == message), "each message came whole");
}
}