Skip to content
This repository has been archived by the owner on Jan 17, 2020. It is now read-only.

ALPN support #131

Merged
merged 3 commits into from
Jul 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/gcloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() -> Result<(), io::Error> {

let mut ca = vec!();
File::open(Path::new("../../certs/roots.pem")).and_then(|mut f| f.read_to_end(&mut ca))?;
let connection_method = ConnectionMethod::Tls(ca, None);
let connection_method = ConnectionMethod::Tls { ca, cert_and_key: None, alpn: vec![] };

let mqtt_options = MqttOptions::new(client_id, "mqtt.googleapis.com", 8883)
.set_keep_alive(10)
Expand Down
2 changes: 1 addition & 1 deletion examples/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
let client_cert = include_bytes!("tlsfiles/bike1.cert.pem").to_vec();
let client_key = include_bytes!("tlsfiles/bike1.key.pem").to_vec();

let connection_method = ConnectionMethod::Tls(ca, Some((client_cert, client_key)));
let connection_method = ConnectionMethod::Tls { ca, cert_and_key: Some((client_cert, client_key)), alpn: vec![] };

let mqtt_options = MqttOptions::new(client_id, "localhost", 8883)
.set_keep_alive(10)
Expand Down
10 changes: 8 additions & 2 deletions src/client/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,14 @@ impl Connection {
let builder = NetworkStream::builder();

let builder = match connection_method {
ConnectionMethod::Tls(ca, Some((cert, key))) => builder.add_certificate_authority(&ca).add_client_auth(&cert, &key),
ConnectionMethod::Tls(ca, None) => builder.add_certificate_authority(&ca),
ConnectionMethod::Tls { ca, cert_and_key, alpn } => {
let builder = builder.add_certificate_authority(&ca).add_alpn_protocols(&alpn);
if let Some((ref cert, ref key)) = cert_and_key {
builder.add_client_auth(cert, key)
} else {
builder
}
},
ConnectionMethod::Tcp => builder,
};

Expand Down
10 changes: 10 additions & 0 deletions src/client/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use crate::client::network::{generate_httpproxy_auth, resolve};
certificate_authority: None,
client_cert: None,
client_private_key: None,
alpn_protocols: Vec::new(),
http_proxy: None,
}
}
Expand All @@ -61,6 +62,7 @@ use crate::client::network::{generate_httpproxy_auth, resolve};
certificate_authority: Option<Vec<u8>>,
client_cert: Option<Vec<u8>>,
client_private_key: Option<Vec<u8>>,
alpn_protocols: Vec<Vec<u8>>,
http_proxy: Option<HttpProxy>,
}

Expand All @@ -76,6 +78,12 @@ use crate::client::network::{generate_httpproxy_auth, resolve};
self
}

pub fn add_alpn_protocols(mut self, protocols: &[Vec<u8>]) -> NetworkStreamBuilder {
self.alpn_protocols.append(&mut protocols.to_vec());
debug!("{:?}", &self.alpn_protocols);
self
}

pub fn set_http_proxy(
mut self,
id: &str,
Expand Down Expand Up @@ -120,6 +128,8 @@ use crate::client::network::{generate_httpproxy_auth, resolve};
_ => unimplemented!(),
};

config.set_protocols(&self.alpn_protocols);

Ok(TlsConnector::from(Arc::new(config)))
}

Expand Down
11 changes: 9 additions & 2 deletions src/mqttoptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ pub enum SecurityOptions {
pub enum ConnectionMethod {
/// Plain text connection
Tcp,
/// Encrypted connection. (ca data, optional client cert and key data)
Tls(Vec<u8>, Option<(Vec<u8>, Vec<u8>)>),
/// Encrypted connection.
Tls {
/// ca data
ca: Vec<u8>,
/// optional client cert and key data
cert_and_key: Option<(Vec<u8>, Vec<u8>)>,
/// ALPN extension protocols
alpn: Vec<Vec<u8>>,
},
}

/// Mqtt through http proxy
Expand Down