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 1.3 version support #278

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ libc = "0.2"
tempfile = "3.1.0"

[target.'cfg(target_os = "windows")'.dependencies]
schannel = "0.1.17"
schannel = "0.1.20"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required as TLSv1.3 came with the switch from winapi to windows-rs in 0.1.20


[target.'cfg(not(any(target_os = "windows", target_os = "macos", target_os = "ios")))'.dependencies]
log = "0.4.5"
Expand Down
10 changes: 10 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ fn main() {
if version >= 0x1_01_00_00_0 {
println!("cargo:rustc-cfg=have_min_max_version");
}

// TLS 1.3 requires openssl 1.1.1
if version >= 0x1_01_01_00_0 {
println!("cargo:rustc-cfg=have_tls13_version");
}
}

if let Ok(version) = env::var("DEP_OPENSSL_LIBRESSL_VERSION_NUMBER") {
Expand All @@ -16,5 +21,10 @@ fn main() {
if version >= 0x2_06_01_00_0 {
println!("cargo:rustc-cfg=have_min_max_version");
}

// TLS 1.3 requires libressl 3.4.0
if version >= 0x3_04_00_00_0 {
println!("cargo:rustc-cfg=have_tls13_version");
}
}
}
23 changes: 15 additions & 8 deletions src/imp/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ fn supported_protocols(
min: Option<Protocol>,
max: Option<Protocol>,
ctx: &mut SslContextBuilder,
) -> Result<(), ErrorStack> {
) -> Result<(), Error> {
use self::openssl::ssl::SslVersion;

fn cvt(p: Protocol) -> SslVersion {
fn cvt(p: Protocol) -> Result<SslVersion, Error> {
match p {
Protocol::Sslv3 => SslVersion::SSL3,
Protocol::Tlsv10 => SslVersion::TLS1,
Protocol::Tlsv11 => SslVersion::TLS1_1,
Protocol::Tlsv12 => SslVersion::TLS1_2,
Protocol::Sslv3 => Ok(SslVersion::SSL3),
Protocol::Tlsv10 => Ok(SslVersion::TLS1),
Protocol::Tlsv11 => Ok(SslVersion::TLS1_1),
Protocol::Tlsv12 => Ok(SslVersion::TLS1_2),
#[cfg(have_tls13_version)]
Protocol::Tlsv13 => Ok(SslVersion::TLS1_3),
#[cfg(not(have_tls13_version))]
Protocol::Tlsv13 => Err(Error::UnsupportedTls13)
}
}

ctx.set_min_proto_version(min.map(cvt))?;
ctx.set_max_proto_version(max.map(cvt))?;
ctx.set_min_proto_version(min.map(cvt).transpose()?)?;
ctx.set_max_proto_version(max.map(cvt).transpose()?)?;

Ok(())
}
Expand Down Expand Up @@ -115,6 +119,7 @@ pub enum Error {
Ssl(ssl::Error, X509VerifyResult),
EmptyChain,
NotPkcs8,
UnsupportedTls13,
}

impl error::Error for Error {
Expand All @@ -124,6 +129,7 @@ impl error::Error for Error {
Error::Ssl(ref e, _) => error::Error::source(e),
Error::EmptyChain => None,
Error::NotPkcs8 => None,
Error::UnsupportedTls13 => None,
}
}
}
Expand All @@ -139,6 +145,7 @@ impl fmt::Display for Error {
"at least one certificate must be provided to create an identity"
),
Error::NotPkcs8 => write!(fmt, "expected PKCS#8 PEM"),
Error::UnsupportedTls13 => write!(fmt, "TLS version 1.3 not supported"),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/imp/schannel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static PROTOCOLS: &'static [Protocol] = &[
Protocol::Tls10,
Protocol::Tls11,
Protocol::Tls12,
Protocol::Tls13,
];

fn convert_protocols(min: Option<::Protocol>, max: Option<::Protocol>) -> &'static [Protocol] {
Expand Down
12 changes: 7 additions & 5 deletions src/imp/security_framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ static SET_AT_EXIT: Once = Once::new();
#[cfg(not(target_os = "ios"))]
static TEMP_KEYCHAIN: Lazy<Mutex<Option<(SecKeychain, TempDir)>>> = Lazy::new(|| Mutex::new(None));

fn convert_protocol(protocol: Protocol) -> SslProtocol {
fn convert_protocol(protocol: Protocol) -> Result<SslProtocol, Error> {
match protocol {
Protocol::Sslv3 => SslProtocol::SSL3,
Protocol::Tlsv10 => SslProtocol::TLS1,
Protocol::Tlsv11 => SslProtocol::TLS11,
Protocol::Tlsv12 => SslProtocol::TLS12,
Protocol::Sslv3 => Ok(SslProtocol::SSL3),
Protocol::Tlsv10 => Ok(SslProtocol::TLS1),
Protocol::Tlsv11 => Ok(SslProtocol::TLS11),
Protocol::Tlsv12 => Ok(SslProtocol::TLS12),
// Not supported in SecureTransport API used in security_framework
Protocol::Tlsv13 => Err(Error(base::Error::from("TLS 1.3 is not supported")))
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ pub enum Protocol {
Tlsv11,
/// The TLS 1.2 protocol.
Tlsv12,
/// The TLS 1.3 protocol.
///
/// Only works on Windows, or with openssl >= 1.1.1 or libressl >= 3.4.0.
/// It will fail at runtime when used in other situations.
Tlsv13,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to expose a uniform interface rather than having this be conditionally defined based on the backend version.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched to runtime errors when unsupported.

}

/// A builder for `TlsConnector`s.
Expand Down
20 changes: 20 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ macro_rules! p {
};
}

#[cfg(any(target_os = "windows", have_tls13_version))]
#[test]
fn connect_google_tls13() {
let builder = p!(
TlsConnector::builder()
.min_protocol_version(Some(Protocol::Tlsv13))
.max_protocol_version(Some(Protocol::Tlsv13))
.build());
let s = p!(TcpStream::connect("google.com:443"));
let mut socket = p!(builder.connect("google.com", s));

p!(socket.write_all(b"GET / HTTP/1.0\r\n\r\n"));
let mut result = vec![];
p!(socket.read_to_end(&mut result));

println!("{}", String::from_utf8_lossy(&result));
assert!(result.starts_with(b"HTTP/1.0"));
assert!(result.ends_with(b"</HTML>\r\n") || result.ends_with(b"</html>"));
}

#[test]
fn connect_google() {
let builder = p!(TlsConnector::new());
Expand Down