[edge] Tests first: a Domi device cert is accepted for mTLS, a self-signed one with the same subject, an unlisted one and none are not (red)
This commit is contained in:
@@ -265,3 +265,49 @@ pub async fn websocket(
|
||||
.await
|
||||
.map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "websocket timed out"))?
|
||||
}
|
||||
|
||||
/// A client identity for mTLS: a PEM chain (leaf first) and its PEM private key.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ClientCert {
|
||||
pub chain_pem: String,
|
||||
pub key_pem: String,
|
||||
/// Offer only TLS 1.2 (client certificates take a different path there).
|
||||
pub tls12_only: bool,
|
||||
}
|
||||
|
||||
/// One request over a fresh TLS connection that presents `cert` if the server asks for one.
|
||||
pub async fn https_with_cert(
|
||||
addr: SocketAddr,
|
||||
sni: &str,
|
||||
roots: Arc<RootCertStore>,
|
||||
cert: &ClientCert,
|
||||
req: &Req,
|
||||
) -> io::Result<Resp> {
|
||||
use rustls_pki_types::pem::PemObject;
|
||||
use rustls_pki_types::{CertificateDer, PrivateKeyDer};
|
||||
crate::tls::install_crypto();
|
||||
let chain: Vec<CertificateDer<'static>> =
|
||||
CertificateDer::pem_slice_iter(cert.chain_pem.as_bytes())
|
||||
.collect::<Result<_, _>>()
|
||||
.map_err(other)?;
|
||||
let key = PrivateKeyDer::from_pem_slice(cert.key_pem.as_bytes()).map_err(other)?;
|
||||
let versions: &[&rustls::SupportedProtocolVersion] = if cert.tls12_only {
|
||||
&[&rustls::version::TLS12]
|
||||
} else {
|
||||
rustls::DEFAULT_VERSIONS
|
||||
};
|
||||
let mut cfg = rustls::ClientConfig::builder_with_protocol_versions(versions)
|
||||
.with_root_certificates(roots)
|
||||
.with_client_auth_cert(chain, key)
|
||||
.map_err(other)?;
|
||||
cfg.alpn_protocols = vec![b"http/1.1".to_vec()];
|
||||
let connector = TlsConnector::from(Arc::new(cfg));
|
||||
tokio::time::timeout(TIMEOUT, async {
|
||||
let tcp = TcpStream::connect(addr).await?;
|
||||
let name = ServerName::try_from(sni.to_owned()).map_err(other)?;
|
||||
let s = connector.connect(name, tcp).await?;
|
||||
send(s, req).await
|
||||
})
|
||||
.await
|
||||
.map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "request timed out"))?
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user