use std::io::{self, Read, Write};
use std::mem::MaybeUninit;
use std::sync::{Arc, Mutex};
use rustls::client::WebPkiServerVerifier;
use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
use rustls::crypto::CryptoProvider;
use rustls::pki_types::pem::PemObject;
use rustls::pki_types::{CertificateDer, PrivateKeyDer, ServerName, UnixTime};
use rustls::server::WebPkiClientVerifier;
use rustls::{
CertificateError, ClientConfig, ClientConnection, DigitallySignedStruct, RootCertStore,
ServerConfig, ServerConnection, SignatureScheme,
};
use rustorb_giop::OutMessage;
use tokio::net::TcpStream;
use crate::{ClientAuth, TlsOptions};
const PLAIN_CHUNK: usize = 64 * 1024;
fn provider() -> Arc<CryptoProvider> {
Arc::new(rustls::crypto::ring::default_provider())
}
fn certs(path: &std::path::Path) -> Result<Vec<CertificateDer<'static>>, String> {
let certs = CertificateDer::pem_file_iter(path)
.and_then(|it| it.collect::<Result<Vec<_>, _>>())
.map_err(|e| format!("{}: {e}", path.display()))?;
if certs.is_empty() {
return Err(format!("{}: no certificate", path.display()));
}
Ok(certs)
}
fn roots(options: &TlsOptions) -> Result<RootCertStore, String> {
let path = options
.ca_file
.as_deref()
.ok_or("no CA file (-ORBsslCAFile)")?;
let mut roots = RootCertStore::empty();
for cert in certs(path)? {
roots
.add(cert)
.map_err(|e| format!("{}: {e}", path.display()))?;
}
Ok(roots)
}
#[allow(clippy::type_complexity)]
fn identity(
options: &TlsOptions,
) -> Result<Option<(Vec<CertificateDer<'static>>, PrivateKeyDer<'static>)>, String> {
let Some(key_file) = options.key_file.as_deref() else {
return match options.cert_file {
Some(_) => Err("a certificate file without a key file (-ORBsslKeyFile)".into()),
None => Ok(None),
};
};
let chain = certs(options.cert_file.as_deref().unwrap_or(key_file))?;
let key = PrivateKeyDer::from_pem_file(key_file)
.map_err(|e| format!("{}: {e}", key_file.display()))?;
Ok(Some((chain, key)))
}
pub(crate) fn client_config(options: &TlsOptions) -> Result<Arc<ClientConfig>, String> {
let roots = Arc::new(roots(options)?);
let builder = ClientConfig::builder_with_provider(provider())
.with_safe_default_protocol_versions()
.map_err(|e| e.to_string())?;
let builder = if options.verify_host {
builder.with_root_certificates(roots)
} else {
let inner = WebPkiServerVerifier::builder_with_provider(roots, provider())
.build()
.map_err(|e| e.to_string())?;
builder
.dangerous()
.with_custom_certificate_verifier(Arc::new(AnyName(inner)))
};
let mut config = match identity(options)? {
Some((chain, key)) => builder
.with_client_auth_cert(chain, key)
.map_err(|e| e.to_string())?,
None => builder.with_no_client_auth(),
};
config.enable_sni = false;
Ok(Arc::new(config))
}
pub(crate) fn server_config(options: &TlsOptions) -> Result<Arc<ServerConfig>, String> {
let (chain, key) =
identity(options)?.ok_or("a TLS endpoint without a key file (-ORBsslKeyFile)")?;
let builder = ServerConfig::builder_with_provider(provider())
.with_safe_default_protocol_versions()
.map_err(|e| e.to_string())?;
let builder = match options.client_auth {
ClientAuth::None => builder.with_no_client_auth(),
auth => {
let roots = Arc::new(roots(options)?);
let verifier = WebPkiClientVerifier::builder_with_provider(roots, provider());
let verifier = if auth == ClientAuth::Optional {
verifier.allow_unauthenticated()
} else {
verifier
};
builder.with_client_cert_verifier(verifier.build().map_err(|e| e.to_string())?)
}
};
let config = builder
.with_single_cert(chain, key)
.map_err(|e| e.to_string())?;
Ok(Arc::new(config))
}
#[derive(Debug)]
struct AnyName(Arc<WebPkiServerVerifier>);
impl ServerCertVerifier for AnyName {
fn verify_server_cert(
&self,
end_entity: &CertificateDer<'_>,
intermediates: &[CertificateDer<'_>],
server_name: &ServerName<'_>,
ocsp_response: &[u8],
now: UnixTime,
) -> Result<ServerCertVerified, rustls::Error> {
match self
.0
.verify_server_cert(end_entity, intermediates, server_name, ocsp_response, now)
{
Err(rustls::Error::InvalidCertificate(
CertificateError::NotValidForName | CertificateError::NotValidForNameContext { .. },
)) => Ok(ServerCertVerified::assertion()),
other => other,
}
}
fn verify_tls12_signature(
&self,
message: &[u8],
cert: &CertificateDer<'_>,
dss: &DigitallySignedStruct,
) -> Result<HandshakeSignatureValid, rustls::Error> {
self.0.verify_tls12_signature(message, cert, dss)
}
fn verify_tls13_signature(
&self,
message: &[u8],
cert: &CertificateDer<'_>,
dss: &DigitallySignedStruct,
) -> Result<HandshakeSignatureValid, rustls::Error> {
self.0.verify_tls13_signature(message, cert, dss)
}
fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
self.0.supported_verify_schemes()
}
}
struct Sock<'a>(&'a TcpStream);
impl Read for Sock<'_> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.try_read(buf)
}
}
impl Write for Sock<'_> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.try_write(buf)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
fn invalid(e: rustls::Error) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, e)
}
async fn handshake(stream: &TcpStream, mut conn: rustls::Connection) -> io::Result<Arc<Tls>> {
conn.set_buffer_limit(None);
loop {
while conn.wants_write() {
match conn.write_tls(&mut Sock(stream)) {
Ok(_) => {}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => stream.writable().await?,
Err(e) => return Err(e),
}
}
if !conn.is_handshaking() {
return Ok(Arc::new(Tls {
conn: Mutex::new(conn),
}));
}
match conn.read_tls(&mut Sock(stream)) {
Ok(0) => return Err(io::ErrorKind::UnexpectedEof.into()),
Ok(_) => {
if let Err(e) = conn.process_new_packets() {
let _ = conn.write_tls(&mut Sock(stream));
return Err(invalid(e));
}
}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => stream.readable().await?,
Err(e) => return Err(e),
}
}
}
pub(crate) async fn connect(
stream: &TcpStream,
config: Arc<ClientConfig>,
host: &str,
) -> io::Result<Arc<Tls>> {
let host = host.trim_start_matches('[').trim_end_matches(']');
let name = ServerName::try_from(host.to_owned())
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
let conn = ClientConnection::new(config, name).map_err(invalid)?;
handshake(stream, conn.into()).await
}
pub(crate) async fn accept(stream: &TcpStream, config: Arc<ServerConfig>) -> io::Result<Arc<Tls>> {
let conn = ServerConnection::new(config).map_err(invalid)?;
handshake(stream, conn.into()).await
}
pub(crate) struct Tls {
conn: Mutex<rustls::Connection>,
}
impl std::fmt::Debug for Tls {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Tls").finish_non_exhaustive()
}
}
pub(crate) type RawRead<'a> = dyn FnMut(&mut [MaybeUninit<u8>]) -> io::Result<usize> + 'a;
struct Recv<'a, 'b>(&'a mut RawRead<'b>);
impl Read for Recv<'_, '_> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let buf = unsafe { &mut *(buf as *mut [u8] as *mut [MaybeUninit<u8>]) };
(self.0)(buf)
}
}
struct Send<'a, F>(&'a mut F);
impl<F: FnMut(&[u8]) -> io::Result<usize>> Write for Send<'_, F> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
(self.0)(buf)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl Tls {
pub(crate) fn read(
&self,
buf: &mut [MaybeUninit<u8>],
recv: &mut RawRead<'_>,
) -> io::Result<usize> {
let n = buf.len().min(PLAIN_CHUNK);
let buf = &mut buf[..n];
buf.fill(MaybeUninit::new(0));
let buf = unsafe { &mut *(buf as *mut [MaybeUninit<u8>] as *mut [u8]) };
let mut conn = self.conn.lock().unwrap();
loop {
match conn.reader().read(buf) {
Ok(n) => return Ok(n),
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {}
Err(e) => return Err(e),
}
if conn.read_tls(&mut Recv(recv))? > 0 {
conn.process_new_packets().map_err(invalid)?;
}
}
}
pub(crate) fn encrypt(&self, msg: &OutMessage, from: usize) {
let mut conn = self.conn.lock().unwrap();
let mut skip = from;
for piece in msg.pieces() {
if skip >= piece.len() {
skip -= piece.len();
continue;
}
let _ = conn.writer().write_all(&piece[skip..]);
skip = 0;
}
}
pub(crate) fn close_notify(&self) {
self.conn.lock().unwrap().send_close_notify();
}
pub(crate) fn flush(
&self,
mut write: impl FnMut(&[u8]) -> io::Result<usize>,
) -> io::Result<bool> {
let mut conn = self.conn.lock().unwrap();
while conn.wants_write() {
match conn.write_tls(&mut Send(&mut write)) {
Ok(0) => return Err(io::ErrorKind::WriteZero.into()),
Ok(_) => {}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => return Ok(false),
Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => return Err(e),
}
}
Ok(true)
}
}