Skip to content

Commit

Permalink
reuse tlsconfig as transport.quic + do not import quic unless feature…
Browse files Browse the repository at this point in the history
… enabled

also chnage openssl version to * to track native-tls
  • Loading branch information
emillynge committed Jan 13, 2022
1 parent 867d243 commit e28727c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ atty = "0.2"
quinn = { version = "0.8.0", optional = true}
rustls = { version = "*", default-features = false, features = ["quic"], optional = true }
rustls-pemfile = { version = "*", optional = true }
openssl = { version = "0.10.38", optional = true }
openssl = { version = "*", optional = true }
futures-util = { version = "*", optional = true}

[build-dependencies]
Expand Down
39 changes: 25 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub struct TransportConfig {
pub transport_type: TransportType,
pub tls: Option<TlsConfig>,
pub noise: Option<NoiseConfig>,
pub quic: Option<TlsConfig>, // reuse TLSconfig since QUIC uses TLS1.3
}

fn default_transport() -> TransportConfig {
Expand Down Expand Up @@ -188,27 +189,37 @@ impl Config {
Ok(())
}

fn validate_tls_config(tls_config: &TlsConfig, is_server: bool) -> Result<()>{
if is_server {
tls_config
.pkcs12
.as_ref()
.and(tls_config.pkcs12_password.as_ref())
.ok_or(anyhow!("Missing `pkcs12` or `pkcs12_password`"))?;
} else {
tls_config
.trusted_root
.as_ref()
.ok_or(anyhow!("Missing `trusted_root`"))?;
}
Ok(())
}
fn validate_transport_config(config: &TransportConfig, is_server: bool) -> Result<()> {
match config.transport_type {
TransportType::Tcp => Ok(()),
TransportType::Quic | TransportType::Tls => {
TransportType::Tls => {
let tls_config = config
.tls
.as_ref()
.ok_or(anyhow!("Missing TLS configuration"))?;
if is_server {
tls_config
.pkcs12
.as_ref()
.and(tls_config.pkcs12_password.as_ref())
.ok_or(anyhow!("Missing `pkcs12` or `pkcs12_password`"))?;
} else {
tls_config
.trusted_root
.as_ref()
.ok_or(anyhow!("Missing `trusted_root`"))?;
}
Ok(())
Config::validate_tls_config(tls_config, is_server)
}
TransportType::Quic => {
let tls_config = config
.quic
.as_ref()
.ok_or(anyhow!("Missing QUIC configuration"))?;
Config::validate_tls_config(tls_config, is_server)
}
TransportType::Noise => {
// The check is done in transport
Expand Down
4 changes: 3 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::protocol::{
self, read_auth, read_hello, Ack, ControlChannelCmd, DataChannelCmd, Hello, UdpTraffic,
HASH_WIDTH_IN_BYTES,
};
use crate::transport::{QuicTransport, TcpTransport, Transport};
use crate::transport::{TcpTransport, Transport};
use anyhow::{anyhow, bail, Context, Result};
use backoff::backoff::Backoff;
use backoff::ExponentialBackoff;
Expand All @@ -26,6 +26,8 @@ use tracing::{debug, error, info, info_span, instrument, warn, Instrument, Span}
use crate::transport::NoiseTransport;
#[cfg(feature = "tls")]
use crate::transport::TlsTransport;
#[cfg(feature = "quic")]
use crate::transport::QuicTransport;

type ServiceDigest = protocol::Digest; // SHA256 of a service name
type Nonce = protocol::Digest; // Also called `session_key`
Expand Down
5 changes: 3 additions & 2 deletions src/transport/quic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ impl Transport for QuicTransport {
type Stream = QuicBiStream;

async fn new(config: &TransportConfig) -> Result<Self> {
let config = match &config.tls {
let config = match &config.quic {
Some(v) => v,
None => {
return Err(anyhow!("Missing tls config"));
return Err(anyhow!("Missing tls config: {:?}", config));
}
};

Expand Down Expand Up @@ -169,6 +169,7 @@ impl Transport for QuicTransport {
let pkcs12 =
Pkcs12::from_der(buf.as_slice()).with_context(|| "Failed to open `tls.pkcs12`")?;


let parsed = pkcs12
.parse(self.config.pkcs12_password.as_ref().unwrap())
.with_context(|| "Could not decrypt `tls.pkcs12` using `tls.pkcs12_password`")?;
Expand Down
4 changes: 2 additions & 2 deletions tests/for_tcp/quic_transport.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ default_token = "default_token_if_not_specify"

[client.transport]
type = "quic"
[client.transport.tls]
[client.transport.quic]
trusted_root = "examples/tls/test_ca.pem"
hostname = "testserver"

Expand All @@ -19,7 +19,7 @@ default_token = "default_token_if_not_specify"

[server.transport]
type = "quic"
[server.transport.tls]
[server.transport.quic]
pkcs12 = "examples/tls/test_server.pfx"
pkcs12_password = "1234"

Expand Down
4 changes: 2 additions & 2 deletions tests/for_udp/quic_transport.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ default_token = "default_token_if_not_specify"

[client.transport]
type = "quic"
[client.transport.tls]
[client.transport.quic]
trusted_root = "examples/tls/test_ca.pem"
hostname = "testserver"

Expand All @@ -21,7 +21,7 @@ default_token = "default_token_if_not_specify"

[server.transport]
type = "quic"
[server.transport.tls]
[server.transport.quic]
pkcs12 = "examples/tls/test_server.pfx"
pkcs12_password = "1234"

Expand Down

0 comments on commit e28727c

Please sign in to comment.