2023-12-30 22:08:37 -05:00
|
|
|
|
//! `starttls::ServerConfig` provides a `ServerConnector` for starttls connections
|
|
|
|
|
|
|
2024-08-05 15:52:27 +02:00
|
|
|
|
#[cfg(feature = "tls-native")]
|
|
|
|
|
|
use native_tls::Error as TlsError;
|
|
|
|
|
|
use std::error::Error as StdError;
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
|
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
|
|
|
|
|
use tokio_rustls::rustls::pki_types::InvalidDnsNameError;
|
|
|
|
|
|
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
|
|
|
|
|
use tokio_rustls::rustls::Error as TlsError;
|
|
|
|
|
|
|
2023-12-30 22:08:37 -05:00
|
|
|
|
use futures::{sink::SinkExt, stream::StreamExt};
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
|
|
|
|
|
use {
|
|
|
|
|
|
std::sync::Arc,
|
|
|
|
|
|
tokio_rustls::{
|
|
|
|
|
|
client::TlsStream,
|
2024-07-25 20:51:20 +02:00
|
|
|
|
rustls::pki_types::ServerName,
|
|
|
|
|
|
rustls::{ClientConfig, RootCertStore},
|
2023-12-30 22:08:37 -05:00
|
|
|
|
TlsConnector,
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "tls-native")]
|
|
|
|
|
|
use {
|
|
|
|
|
|
native_tls::TlsConnector as NativeTlsConnector,
|
|
|
|
|
|
tokio_native_tls::{TlsConnector, TlsStream},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-07-24 20:39:27 +02:00
|
|
|
|
use minidom::Element;
|
2023-12-30 22:08:37 -05:00
|
|
|
|
use sasl::common::ChannelBinding;
|
|
|
|
|
|
use tokio::{
|
|
|
|
|
|
io::{AsyncRead, AsyncWrite},
|
|
|
|
|
|
net::TcpStream,
|
|
|
|
|
|
};
|
2024-07-24 20:39:27 +02:00
|
|
|
|
use xmpp_parsers::{jid::Jid, ns};
|
2023-12-30 22:08:37 -05:00
|
|
|
|
|
2024-08-05 15:09:59 +02:00
|
|
|
|
use crate::{
|
|
|
|
|
|
connect::{ServerConnector, ServerConnectorError, Tcp},
|
|
|
|
|
|
error::{Error, ProtocolError},
|
2024-08-06 17:00:53 +02:00
|
|
|
|
proto::{Packet, XmppStream},
|
2024-08-05 15:33:23 +02:00
|
|
|
|
AsyncClient,
|
2024-08-05 15:09:59 +02:00
|
|
|
|
};
|
2023-12-30 22:08:37 -05:00
|
|
|
|
|
2024-01-01 01:13:51 -05:00
|
|
|
|
/// AsyncClient that connects over StartTls
|
|
|
|
|
|
pub type StartTlsAsyncClient = AsyncClient<ServerConfig>;
|
|
|
|
|
|
|
2023-12-30 22:08:37 -05:00
|
|
|
|
/// StartTLS XMPP server connection configuration
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
|
pub enum ServerConfig {
|
|
|
|
|
|
/// Use SRV record to find server host
|
|
|
|
|
|
UseSrv,
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
|
|
/// Manually define server host and port
|
|
|
|
|
|
Manual {
|
|
|
|
|
|
/// Server host name
|
|
|
|
|
|
host: String,
|
|
|
|
|
|
/// Server port
|
|
|
|
|
|
port: u16,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ServerConnector for ServerConfig {
|
|
|
|
|
|
type Stream = TlsStream<TcpStream>;
|
2024-08-06 17:00:53 +02:00
|
|
|
|
async fn connect(&self, jid: &Jid, ns: &str) -> Result<XmppStream<Self::Stream>, Error> {
|
2023-12-30 22:08:37 -05:00
|
|
|
|
// TCP connection
|
|
|
|
|
|
let tcp_stream = match self {
|
|
|
|
|
|
ServerConfig::UseSrv => {
|
2024-08-05 15:09:59 +02:00
|
|
|
|
Tcp::resolve_with_srv(jid.domain().as_str(), "_xmpp-client._tcp", 5222).await?
|
2023-12-30 22:08:37 -05:00
|
|
|
|
}
|
2024-08-05 15:09:59 +02:00
|
|
|
|
ServerConfig::Manual { host, port } => Tcp::resolve(host.as_str(), *port).await?,
|
2023-12-30 22:08:37 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
2024-08-06 17:00:53 +02:00
|
|
|
|
// Unencryped XmppStream
|
|
|
|
|
|
let xmpp_stream = XmppStream::start(tcp_stream, jid.clone(), ns.to_owned()).await?;
|
2023-12-30 22:08:37 -05:00
|
|
|
|
|
|
|
|
|
|
if xmpp_stream.stream_features.can_starttls() {
|
|
|
|
|
|
// TlsStream
|
|
|
|
|
|
let tls_stream = starttls(xmpp_stream).await?;
|
2024-08-06 17:00:53 +02:00
|
|
|
|
// Encrypted XmppStream
|
|
|
|
|
|
Ok(XmppStream::start(tls_stream, jid.clone(), ns.to_owned()).await?)
|
2023-12-30 22:08:37 -05:00
|
|
|
|
} else {
|
2024-08-04 17:32:12 +02:00
|
|
|
|
return Err(crate::Error::Protocol(ProtocolError::NoTls).into());
|
2023-12-30 22:08:37 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn channel_binding(
|
|
|
|
|
|
#[allow(unused_variables)] stream: &Self::Stream,
|
|
|
|
|
|
) -> Result<sasl::common::ChannelBinding, Error> {
|
|
|
|
|
|
#[cfg(feature = "tls-native")]
|
|
|
|
|
|
{
|
|
|
|
|
|
log::warn!("tls-native doesn’t support channel binding, please use tls-rust if you want this feature!");
|
|
|
|
|
|
Ok(ChannelBinding::None)
|
|
|
|
|
|
}
|
|
|
|
|
|
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
|
|
|
|
|
{
|
|
|
|
|
|
let (_, connection) = stream.get_ref();
|
|
|
|
|
|
Ok(match connection.protocol_version() {
|
|
|
|
|
|
// TODO: Add support for TLS 1.2 and earlier.
|
|
|
|
|
|
Some(tokio_rustls::rustls::ProtocolVersion::TLSv1_3) => {
|
|
|
|
|
|
let data = vec![0u8; 32];
|
2024-08-04 17:32:12 +02:00
|
|
|
|
let data = connection
|
|
|
|
|
|
.export_keying_material(data, b"EXPORTER-Channel-Binding", None)
|
|
|
|
|
|
.map_err(|e| StartTlsError::Tls(e))?;
|
2023-12-30 22:08:37 -05:00
|
|
|
|
ChannelBinding::TlsExporter(data)
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => ChannelBinding::None,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "tls-native")]
|
|
|
|
|
|
async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
|
2024-08-06 17:00:53 +02:00
|
|
|
|
xmpp_stream: XmppStream<S>,
|
2023-12-30 22:08:37 -05:00
|
|
|
|
) -> Result<TlsStream<S>, Error> {
|
2024-04-20 21:23:12 +02:00
|
|
|
|
let domain = xmpp_stream.jid.domain().to_owned();
|
2023-12-30 22:08:37 -05:00
|
|
|
|
let stream = xmpp_stream.into_inner();
|
|
|
|
|
|
let tls_stream = TlsConnector::from(NativeTlsConnector::builder().build().unwrap())
|
|
|
|
|
|
.connect(&domain, stream)
|
2024-08-04 17:32:12 +02:00
|
|
|
|
.await
|
|
|
|
|
|
.map_err(|e| StartTlsError::Tls(e))?;
|
2023-12-30 22:08:37 -05:00
|
|
|
|
Ok(tls_stream)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
|
|
|
|
|
async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
|
2024-08-06 17:00:53 +02:00
|
|
|
|
xmpp_stream: XmppStream<S>,
|
2023-12-30 22:08:37 -05:00
|
|
|
|
) -> Result<TlsStream<S>, Error> {
|
2024-03-03 16:15:04 +01:00
|
|
|
|
let domain = xmpp_stream.jid.domain().to_string();
|
2024-08-04 17:32:12 +02:00
|
|
|
|
let domain = ServerName::try_from(domain).map_err(|e| StartTlsError::DnsNameError(e))?;
|
2023-12-30 22:08:37 -05:00
|
|
|
|
let stream = xmpp_stream.into_inner();
|
2024-07-25 20:51:20 +02:00
|
|
|
|
let root_store = RootCertStore {
|
|
|
|
|
|
roots: webpki_roots::TLS_SERVER_ROOTS.into(),
|
|
|
|
|
|
};
|
2023-12-30 22:08:37 -05:00
|
|
|
|
let config = ClientConfig::builder()
|
|
|
|
|
|
.with_root_certificates(root_store)
|
|
|
|
|
|
.with_no_client_auth();
|
|
|
|
|
|
let tls_stream = TlsConnector::from(Arc::new(config))
|
|
|
|
|
|
.connect(domain, stream)
|
|
|
|
|
|
.await
|
|
|
|
|
|
.map_err(|e| Error::from(crate::Error::Io(e)))?;
|
|
|
|
|
|
Ok(tls_stream)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-06 17:00:53 +02:00
|
|
|
|
/// Performs `<starttls/>` on an XmppStream and returns a binary
|
2023-12-30 22:08:37 -05:00
|
|
|
|
/// TlsStream.
|
|
|
|
|
|
pub async fn starttls<S: AsyncRead + AsyncWrite + Unpin>(
|
2024-08-06 17:00:53 +02:00
|
|
|
|
mut xmpp_stream: XmppStream<S>,
|
2023-12-30 22:08:37 -05:00
|
|
|
|
) -> Result<TlsStream<S>, Error> {
|
|
|
|
|
|
let nonza = Element::builder("starttls", ns::TLS).build();
|
|
|
|
|
|
let packet = Packet::Stanza(nonza);
|
|
|
|
|
|
xmpp_stream.send(packet).await?;
|
|
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
|
match xmpp_stream.next().await {
|
|
|
|
|
|
Some(Ok(Packet::Stanza(ref stanza))) if stanza.name() == "proceed" => break,
|
|
|
|
|
|
Some(Ok(Packet::Text(_))) => {}
|
|
|
|
|
|
Some(Err(e)) => return Err(e.into()),
|
|
|
|
|
|
_ => {
|
2024-08-04 17:32:12 +02:00
|
|
|
|
return Err(crate::Error::Protocol(ProtocolError::NoTls).into());
|
2023-12-30 22:08:37 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
get_tls_stream(xmpp_stream).await
|
|
|
|
|
|
}
|
2024-08-05 15:52:27 +02:00
|
|
|
|
|
|
|
|
|
|
/// StartTLS ServerConnector Error
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
pub enum StartTlsError {
|
|
|
|
|
|
/// TLS error
|
|
|
|
|
|
Tls(TlsError),
|
|
|
|
|
|
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
|
|
|
|
|
/// DNS name parsing error
|
|
|
|
|
|
DnsNameError(InvalidDnsNameError),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ServerConnectorError for StartTlsError {}
|
|
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for StartTlsError {
|
|
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
|
match self {
|
|
|
|
|
|
Self::Tls(e) => write!(fmt, "TLS error: {}", e),
|
|
|
|
|
|
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
|
|
|
|
|
Self::DnsNameError(e) => write!(fmt, "DNS name error: {}", e),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl StdError for StartTlsError {}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<TlsError> for StartTlsError {
|
|
|
|
|
|
fn from(e: TlsError) -> Self {
|
|
|
|
|
|
Self::Tls(e)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
|
|
|
|
|
impl From<InvalidDnsNameError> for StartTlsError {
|
|
|
|
|
|
fn from(e: InvalidDnsNameError) -> Self {
|
|
|
|
|
|
Self::DnsNameError(e)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|