use bytes::BytesMut;
use embedded_io::ErrorType;
use super::handshake::{build_host_banner, do_auth, open, recv_handshake_pkt, Verdict};
use super::{Connection, ConnectionConfig};
use crate::base::auth::Authenticator;
use crate::base::error::{AuthError, Error, ProtocolError};
use crate::base::protocol::command::{self, Command};
use crate::base::protocol::constant::STLS_VERSION;
use crate::base::protocol::features::Feature;
use crate::base::protocol::{Checksum, Packet};
use crate::base::wire::{send_pkt, DesyncFlag};
use crate::tls::TlsClientConfig;
use crate::transport::tls::StartTls;
impl<T, const MAX_CHANNELS: usize, const MAX_PROPERTIES: usize, const MAX_FEATURES: usize>
Connection<T, MAX_CHANNELS, MAX_PROPERTIES, MAX_FEATURES>
where
T: StartTls,
{
pub async fn connect_tls<A: Authenticator>(
transport: T,
auth: A,
features: &[Feature],
tls: &TlsClientConfig,
) -> Result<Self, Error<<T as ErrorType>::Error>> {
Self::connect_tls_with_config(transport, auth, features, tls, ConnectionConfig::new()).await
}
pub async fn connect_tls_with_config<A: Authenticator>(
transport: T,
auth: A,
features: &[Feature],
tls: &TlsClientConfig,
config: ConnectionConfig,
) -> Result<Self, Error<<T as ErrorType>::Error>> {
let banner = build_host_banner(features);
Self::connect_tls_with_raw_banner_and_config(
transport,
auth,
banner.as_slice(),
tls,
config,
)
.await
}
pub async fn connect_tls_with_raw_banner_and_config<A: Authenticator>(
mut transport: T,
mut auth: A,
banner: &[u8],
tls: &TlsClientConfig,
config: ConnectionConfig,
) -> Result<Self, Error<<T as ErrorType>::Error>> {
let desync = DesyncFlag::new();
let mut recv_buf = BytesMut::new();
let verdict = open(
&mut transport,
&mut auth,
banner,
&config,
&desync,
&mut recv_buf,
)
.await?;
let cnxn = match verdict {
Verdict::Connected(cnxn) => cnxn,
Verdict::StartTls(offer) => {
Self::upgrade(
&mut transport,
&desync,
&mut auth,
&mut recv_buf,
offer,
tls,
&config,
)
.await?
}
};
Self::assemble(transport, desync, recv_buf, banner, config, cnxn)
}
async fn upgrade<A: Authenticator>(
transport: &mut T,
desync: &DesyncFlag,
auth: &mut A,
recv_buf: &mut BytesMut,
offer: Packet,
tls: &TlsClientConfig,
config: &ConnectionConfig,
) -> Result<Packet, Error<<T as ErrorType>::Error>> {
if offer.arg0 != STLS_VERSION {
log::warn!(
"device offers STLS version {:#010x}, we speak {:#010x}",
offer.arg0,
STLS_VERSION
);
}
if !recv_buf.is_empty() {
return Err(ProtocolError::DataAfterStls.into());
}
let reply = Packet::new(Command::StartTls, STLS_VERSION, 0, alloc::vec::Vec::new());
send_pkt(transport, desync, &reply, Checksum::Compute).await?;
transport.start_tls(tls).await.map_err(Error::Io)?;
let pkt = recv_handshake_pkt(transport, recv_buf, config.max_payload())
.await
.map_err(|e| Self::verdict_on(e, recv_buf))?;
match pkt.command {
Command::Connect => Ok(pkt),
Command::Auth if pkt.arg0 == command::AUTH_TOKEN => {
let verdict = do_auth(transport, desync, auth, recv_buf, pkt.data, config)
.await
.map_err(|e| Self::verdict_on(e, recv_buf))?;
match verdict {
Verdict::Connected(cnxn) => Ok(cnxn),
Verdict::StartTls(_) => {
Err(ProtocolError::UnexpectedCommand(Command::StartTls).into())
}
}
}
other => Err(ProtocolError::UnexpectedCommand(other).into()),
}
}
fn verdict(error: <T as ErrorType>::Error) -> Error<<T as ErrorType>::Error> {
if T::is_key_rejected(&error) {
log::debug!("device closed the TLS session over our key: {error:?}");
AuthError::TlsKeyNotTrusted.into()
} else {
Error::Io(error)
}
}
fn verdict_on(
error: Error<<T as ErrorType>::Error>,
recv_buf: &BytesMut,
) -> Error<<T as ErrorType>::Error> {
match error {
Error::UnexpectedEof if recv_buf.is_empty() => {
log::debug!("device closed the TLS session before its CNXN");
AuthError::TlsClosedBeforeConnect.into()
}
Error::Io(e) => Self::verdict(e),
other => other,
}
}
}