Skip to content
Draft
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 src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl NatsClient {
if tls_required {
match Url::parse(&cluster_uri) {
Ok(url) => match url.host_str() {
Some(host) => future::ok(Either::B(connect_tls(host.to_string(), cluster_sa))),
Some(host) => future::ok(Either::B(connect_tls(host.to_string(), cluster_sa, None))),
None => future::err(NatsError::TlsHostMissingError),
},
Err(e) => future::err(e.into()),
Expand Down
12 changes: 10 additions & 2 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use std::sync::Arc;
pub(crate) mod connection;
mod connection_inner;

mod tls;
pub use self::tls::*;

use error::NatsError;

use self::connection::NatsConnectionState;
Expand All @@ -28,13 +31,18 @@ pub(crate) fn connect(addr: SocketAddr) -> impl Future<Item = NatsConnection, Er
}

/// Connect to a TLS over TCP socket. Upgrade is performed automatically
pub(crate) fn connect_tls(host: String, addr: SocketAddr) -> impl Future<Item = NatsConnection, Error = NatsError> {
pub(crate) fn connect_tls(
host: String,
addr: SocketAddr,
config: Option<NatsClientTlsConfig>,
) -> impl Future<Item = NatsConnection, Error = NatsError> {
let inner_host = host.clone();
NatsConnectionInner::connect_tcp(&addr)
.and_then(move |socket| {
debug!(target: "nitox", "Connected through TCP, upgrading to TLS");
NatsConnectionInner::upgrade_tcp_to_tls(&host, socket)
}).map(move |socket| {
})
.map(move |socket| {
debug!(target: "nitox", "Connected through TCP over TLS");
NatsConnection {
is_tls: true,
Expand Down
58 changes: 58 additions & 0 deletions src/net/tls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use error::NatsError;
use std::sync::Arc;
// Written by @wafflespeanut from @Naamio
use native_tls::{Certificate, Identity};

/// TLS configuration for the client.
#[derive(Clone, Default)]
pub struct NatsClientTlsConfig {
pub(crate) identity: Option<Arc<(Vec<u8>, String)>>,
pub(crate) root_cert: Option<Arc<Vec<u8>>>,
}

impl NatsClientTlsConfig {
/// Set the identity from a DER-formatted PKCS #12 archive using the the given password to decrypt the key.
pub fn pkcs12_identity<B>(mut self, der_bytes: B, password: &str) -> Result<Self, NatsError>
where
B: AsRef<[u8]>,
{
self.identity = Some(Arc::new((der_bytes.as_ref().into(), password.into())));
self.identity()?;
Ok(self)
}

/// Set the root certificate in DER-format.
pub fn root_cert_der<B>(mut self, der_bytes: B) -> Result<Self, NatsError>
where
B: AsRef<[u8]>,
{
self.root_cert = Some(Arc::new(der_bytes.as_ref().into()));
self.root_cert()?;
Ok(self)
}

pub(crate) fn identity(&self) -> Result<Option<Identity>, NatsError> {
if let Some((b, p)) = self.identity.as_ref().map(|s| &**s) {
Ok(Some(Identity::from_pkcs12(b, p)?))
} else {
Ok(None)
}
}

pub(crate) fn root_cert(&self) -> Result<Option<Certificate>, NatsError> {
if let Some(b) = self.root_cert.as_ref() {
Ok(Some(Certificate::from_der(b)?))
} else {
Ok(None)
}
}
}

impl ::std::fmt::Debug for NatsClientTlsConfig {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
f.debug_struct("NatsClientTlsConfig")
.field("identity_exists", &self.identity.is_some())
.field("root_cert_exists", &self.root_cert.is_some())
.finish()
}
}