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

[WIP] Support ALPN on Security Framework #100

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ lazy_static = "1.0"
libc = "0.2"
tempfile = "3.0"

[features]
alpn = ["security-framework/OSX_10_13"]

[target.'cfg(target_os = "windows")'.dependencies]
schannel = "0.1.13"

Expand Down
10 changes: 10 additions & 0 deletions src/imp/security_framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ pub struct TlsConnector {
use_sni: bool,
danger_accept_invalid_hostnames: bool,
danger_accept_invalid_certs: bool,
alpn: Vec<String>,
}

impl TlsConnector {
Expand All @@ -280,6 +281,7 @@ impl TlsConnector {
use_sni: builder.use_sni,
danger_accept_invalid_hostnames: builder.accept_invalid_hostnames,
danger_accept_invalid_certs: builder.accept_invalid_certs,
alpn: builder.alpn.clone(),
})
}

Expand All @@ -302,6 +304,10 @@ impl TlsConnector {
builder.danger_accept_invalid_hostnames(self.danger_accept_invalid_hostnames);
builder.danger_accept_invalid_certs(self.danger_accept_invalid_certs);

if self.alpn.len() > 0 {
builder.alpn_protocols(&self.alpn.iter().map(AsRef::as_ref).collect::<Vec<_>>());
}

match builder.handshake(domain, stream) {
Ok(stream) => Ok(TlsStream { stream, cert: None }),
Err(e) => Err(e.into()),
Expand Down Expand Up @@ -385,6 +391,10 @@ impl<S: io::Read + io::Write> TlsStream<S> {
Ok(trust.certificate_at_index(0).map(Certificate))
}

pub fn negotiated_protocols(&self) -> Result<Vec<String>, Error> {
self.stream.context().alpn_protocols().map_err(|e| Error::from(e))
}

#[cfg(target_os = "ios")]
pub fn tls_server_end_point(&self) -> Result<Option<Vec<u8>>, Error> {
Ok(None)
Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ pub struct TlsConnectorBuilder {
accept_invalid_certs: bool,
accept_invalid_hostnames: bool,
use_sni: bool,
alpn: Vec<String>,
}

impl TlsConnectorBuilder {
Expand Down Expand Up @@ -405,6 +406,17 @@ impl TlsConnectorBuilder {
self
}

/// Request specific protocols through ALPN (Application-Layer Protocol Negotiation).
///
/// Defaults to none
pub fn request_protocols(
&mut self,
protocols: &[&str],
) -> &mut TlsConnectorBuilder {
self.alpn = protocols.iter().map(|s| s.to_string()).collect();
self
}

/// Creates a new `TlsConnector`.
pub fn build(&self) -> Result<TlsConnector> {
let connector = imp::TlsConnector::new(self)?;
Expand Down Expand Up @@ -450,6 +462,7 @@ impl TlsConnector {
use_sni: true,
accept_invalid_certs: false,
accept_invalid_hostnames: false,
alpn: vec![],
}
}

Expand Down Expand Up @@ -628,6 +641,11 @@ impl<S: io::Read + io::Write> TlsStream<S> {
Ok(self.0.tls_server_end_point()?)
}

/// Returns the negotiated protocols
pub fn negotiated_protocols(&self) -> Result<Vec<String>> {
Copy link
Owner

Choose a reason for hiding this comment

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

ALPN only allows a single protocol to be negotiated, so this should return a Result<Option<String>>.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks. Will update the method.

Ok(self.0.negotiated_protocols()?)
}

/// Shuts down the TLS session.
pub fn shutdown(&mut self) -> io::Result<()> {
self.0.shutdown()?;
Expand Down