use super::{Connection, Error, Identity};
use crate::udp;
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub enum ClientAuth {
#[default]
None,
Optional(Vec<std::path::PathBuf>),
Required(Vec<std::path::PathBuf>),
}
impl ClientAuth {
pub(crate) fn roots(&self) -> Option<(&[std::path::PathBuf], bool)> {
match self {
Self::None => None,
Self::Optional(roots) => Some((roots, false)),
Self::Required(roots) => Some((roots, true)),
}
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Config {
pub identity: Identity,
pub alpn: Vec<String>,
pub client_auth: ClientAuth,
pub transport: super::Transport,
}
impl Config {
pub fn new(identity: Identity) -> Self {
Self {
identity,
alpn: Vec::new(),
client_auth: ClientAuth::default(),
transport: super::Transport::default(),
}
}
pub(crate) fn check(&self) -> Result<(), Error> {
if self.client_auth.roots().is_some_and(|(roots, _)| roots.is_empty()) {
return Err(Error::Tls(
"client authentication needs at least one root certificate".to_string(),
));
}
Ok(())
}
}
pub async fn accept(socket: udp::Socket, config: &Config) -> Result<Connection, Error> {
let endpoint = super::Endpoint::new(socket, super::endpoint::Config::default().with_server(config.clone()))?;
endpoint.accept().await
}