Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tls: consume Identity when building TLS config #218

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions wtransport/examples/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> Result<()> {
let identity = Identity::self_signed(["localhost", "127.0.0.1", "::1"]).unwrap();
let cert_digest = identity.certificate_chain().as_slice()[0].hash();

let webtransport_server = WebTransportServer::new(&identity)?;
let webtransport_server = WebTransportServer::new(identity)?;
let http_server = HttpServer::new(&cert_digest, webtransport_server.local_port()).await?;

info!(
Expand Down Expand Up @@ -50,7 +50,7 @@ mod webtransport {
}

impl WebTransportServer {
pub fn new(identity: &Identity) -> Result<Self> {
pub fn new(identity: Identity) -> Result<Self> {
let config = ServerConfig::builder()
.with_bind_default(0)
.with_identity(identity)
Expand Down
2 changes: 1 addition & 1 deletion wtransport/examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> Result<()> {

let config = ServerConfig::builder()
.with_bind_default(4433)
.with_identity(&Identity::self_signed(["localhost"]).unwrap())
.with_identity(Identity::self_signed(["localhost"]).unwrap())
.keep_alive_interval(Some(Duration::from_secs(3)))
.build();

Expand Down
16 changes: 8 additions & 8 deletions wtransport/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//!
//! let server_config = ServerConfig::builder()
//! .with_bind_default(443)
//! .with_identity(&Identity::load_pemfiles("cert.pem", "key.pem").await?)
//! .with_identity(Identity::load_pemfiles("cert.pem", "key.pem").await?)
//! .build();
//!
//! # Ok(())
Expand Down Expand Up @@ -194,7 +194,7 @@ pub struct InvalidIdleTimeout;
/// # async fn run() -> Result<()> {
/// ServerConfig::builder()
/// .with_bind_default(443)
/// .with_identity(&Identity::load_pemfiles("cert.pem", "key.pem").await?);
/// .with_identity(Identity::load_pemfiles("cert.pem", "key.pem").await?);
/// # Ok(())
/// # }
/// ```
Expand All @@ -221,7 +221,7 @@ pub struct InvalidIdleTimeout;
/// # async fn run() -> Result<()> {
/// let server_config = ServerConfig::builder()
/// .with_bind_default(443)
/// .with_identity(&Identity::load_pemfiles("cert.pem", "key.pem").await?)
/// .with_identity(Identity::load_pemfiles("cert.pem", "key.pem").await?)
/// .keep_alive_interval(Some(Duration::from_secs(3)))
/// .build();
/// # Ok(())
Expand Down Expand Up @@ -269,7 +269,7 @@ impl ServerConfig {
/// # use wtransport::ServerConfig;
/// let config = ServerConfig::builder()
/// .with_bind_default(4433)
/// .with_identity(&Identity::load_pemfiles("cert.pem", "key.pem").await?);
/// .with_identity(Identity::load_pemfiles("cert.pem", "key.pem").await?);
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -348,14 +348,14 @@ impl ServerConfigBuilder<states::WantsIdentity> {
///
/// let server_config = ServerConfig::builder()
/// .with_bind_default(4433)
/// .with_identity(&identity)
/// .with_identity(identity)
/// .build();
/// # Ok(())
/// # }
/// ```
pub fn with_identity(
self,
identity: &Identity,
identity: Identity,
) -> ServerConfigBuilder<states::WantsTransportConfigServer> {
use crate::tls::server::build_default_tls_config;

Expand Down Expand Up @@ -440,14 +440,14 @@ impl ServerConfigBuilder<states::WantsIdentity> {
/// // Create a ServerConfigBuilder with the custom transport configuration and default TLS settings
/// let server_config = ServerConfig::builder()
/// .with_bind_default(4433)
/// .with_custom_transport(&identity, custom_transport_config)
/// .with_custom_transport(identity, custom_transport_config)
/// .build();
/// ```
#[cfg(feature = "quinn")]
#[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
pub fn with_custom_transport(
self,
identity: &Identity,
identity: Identity,
quic_transport_config: QuicTransportConfig,
) -> ServerConfigBuilder<states::WantsTransportConfigServer> {
use crate::tls::server::build_default_tls_config;
Expand Down
2 changes: 1 addition & 1 deletion wtransport/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub mod endpoint_side {
/// # async fn run() -> Result<()> {
/// # let config = ServerConfig::builder()
/// # .with_bind_default(4433)
/// # .with_identity(&Identity::self_signed(["doc"]).unwrap())
/// # .with_identity(Identity::self_signed(["doc"]).unwrap())
/// # .build();
/// let server = Endpoint::server(config)?;
/// loop {
Expand Down
2 changes: 1 addition & 1 deletion wtransport/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
//! async fn main() -> Result<()> {
//! let config = ServerConfig::builder()
//! .with_bind_default(4433)
//! .with_identity(&Identity::load_pemfiles("cert.pem", "key.pem").await?)
//! .with_identity(Identity::load_pemfiles("cert.pem", "key.pem").await?)
//! .build();
//!
//! let server = Endpoint::server(config)?;
Expand Down
15 changes: 7 additions & 8 deletions wtransport/src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,20 +603,19 @@ pub mod server {
/// # Arguments
///
/// - `identity`: A reference to the identity containing the certificate chain and private key.
pub fn build_default_tls_config(identity: &Identity) -> TlsServerConfig {
pub fn build_default_tls_config(identity: Identity) -> TlsServerConfig {
let certificates = identity
.certificate_chain()
.as_slice()
.iter()
.map(|cert| rustls_pki_types::CertificateDer::from(cert.der().to_vec()))
.certificate_chain
.0
.into_iter()
.map(|cert| cert.0)
.collect();

let private_key = identity.private_key();
let private_key = identity.private_key.0;

// TODO: clone key is inefficient; perpaps the key was borrowed from static data?
let mut tls_config = TlsServerConfig::builder()
.with_no_client_auth()
.with_single_cert(certificates, private_key.0.clone_key())
.with_single_cert(certificates, private_key)
.expect("Certificate and private key should be already validated");

tls_config.alpn_protocols = [WEBTRANSPORT_ALPN.to_vec()].to_vec();
Expand Down
Loading