Skip to content

Commit 667b730

Browse files
committed
Add native-tls support
1 parent d99843b commit 667b730

File tree

5 files changed

+79
-9
lines changed

5 files changed

+79
-9
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ with-bit-vec = ["bit-vec"]
2525
with-chrono = ["chrono"]
2626
with-eui48 = ["eui48"]
2727
with-openssl = ["openssl"]
28+
with-native-tls = ["native-tls"]
2829
with-rustc-serialize = ["rustc-serialize"]
2930
with-security-framework = ["security-framework"]
3031
with-serde_json = ["serde_json"]
@@ -44,6 +45,7 @@ bit-vec = { version = "0.4", optional = true }
4445
chrono = { version = "0.2.14", optional = true }
4546
eui48 = { version = "0.1", optional = true }
4647
openssl = { version = "0.9", optional = true }
48+
native-tls = { version = "0.1", optional = true }
4749
rustc-serialize = { version = "0.3", optional = true }
4850
security-framework = { version = "0.1.2", optional = true }
4951
serde_json = { version = ">= 0.6, < 0.9", optional = true }

src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,25 @@
4444
//! This crate supports TLS secured connections. The `TlsMode` enum is passed to connection methods
4545
//! and indicates if the connection will not, may, or must be secured by TLS. The TLS implementation
4646
//! is pluggable through the `TlsHandshake` trait. Implementations for OpenSSL and OSX's Secure
47-
//! Transport are provided behind the `with-openssl` and `with-security-framework` feature flags
48-
//! respectively.
47+
//! Transport are provided behind the `with-openssl`, `with-security-framework`, and
48+
//! `with-native-tls` feature flags respectively.
4949
//!
5050
//! ## Examples
5151
//!
52-
//! Connecting using OpenSSL:
52+
//! Connecting using native-tls:
5353
//!
5454
//! ```no_run
5555
//! extern crate postgres;
5656
//!
5757
//! use postgres::{Connection, TlsMode};
58-
//! # #[cfg(feature = "with-openssl")]
59-
//! use postgres::tls::openssl::OpenSsl;
58+
//! # #[cfg(feature = "with-native-tls")]
59+
//! use postgres::tls::native_tls::NativeTls;
6060
//!
61-
//! # #[cfg(not(feature = "with-openssl"))] fn main() {}
62-
//! # #[cfg(feature = "with-openssl")]
61+
//! # #[cfg(not(feature = "with-native-tls"))] fn main() {}
62+
//! # #[cfg(feature = "with-native-tls")]
6363
//! fn main() {
64-
//! let openssl = OpenSsl::new().unwrap();
65-
//! let conn = Connection::connect("postgres://postgres@localhost", TlsMode::Require(&openssl))
64+
//! let negotiator = NativeTls::new().unwrap();
65+
//! let conn = Connection::connect("postgres://postgres@localhost", TlsMode::Require(&negotiator))
6666
//! .unwrap();
6767
//! }
6868
//! ```

src/tls/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use std::fmt;
99
pub mod openssl;
1010
#[cfg(feature = "with-security-framework")]
1111
pub mod security_framework;
12+
#[cfg(feature = "with-native-tls")]
13+
pub mod native_tls;
1214

1315
/// A trait implemented by TLS streams.
1416
pub trait TlsStream: fmt::Debug + Read + Write + Send {

src/tls/native_tls.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//! Native TLS support.
2+
extern crate native_tls;
3+
4+
use std::error::Error;
5+
use std::fmt;
6+
7+
use self::native_tls::TlsConnector;
8+
use tls::{TlsStream, Stream, TlsHandshake};
9+
10+
impl TlsStream for native_tls::TlsStream<Stream> {
11+
fn get_ref(&self) -> &Stream {
12+
self.get_ref()
13+
}
14+
15+
fn get_mut(&mut self) -> &mut Stream {
16+
self.get_mut()
17+
}
18+
}
19+
20+
/// A `TlsHandshake` implementation that uses the native-tls crate.
21+
///
22+
/// Requires the `with-native-tls` feature.
23+
pub struct NativeTls(TlsConnector);
24+
25+
impl fmt::Debug for NativeTls {
26+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
27+
fmt.debug_struct("NativeTls").finish()
28+
}
29+
}
30+
31+
impl NativeTls {
32+
/// Creates a new `NativeTls` with its default configuration.
33+
pub fn new() -> Result<NativeTls, native_tls::Error> {
34+
let connector = try!(TlsConnector::builder());
35+
let connector = try!(connector.build());
36+
Ok(NativeTls(connector))
37+
}
38+
39+
/// Returns a reference to the inner `TlsConnector`.
40+
pub fn connector(&self) -> &TlsConnector {
41+
&self.0
42+
}
43+
44+
/// Returns a mutable reference to the inner `TlsConnector`.
45+
pub fn connector_mut(&mut self) -> &mut TlsConnector {
46+
&mut self.0
47+
}
48+
}
49+
50+
impl From<TlsConnector> for NativeTls {
51+
fn from(connector: TlsConnector) -> NativeTls {
52+
NativeTls(connector)
53+
}
54+
}
55+
56+
impl TlsHandshake for NativeTls {
57+
fn tls_handshake(&self,
58+
domain: &str,
59+
stream: Stream)
60+
-> Result<Box<TlsStream>, Box<Error + Send + Sync>> {
61+
let stream = try!(self.0.connect(domain, stream));
62+
Ok(Box::new(stream))
63+
}
64+
}

tests/test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ extern crate url;
66
extern crate openssl;
77
#[cfg(feature = "with-security-framework")]
88
extern crate security_framework;
9+
#[cfg(feature = "native-tls")]
10+
extern crate native_tls;
911

1012
use fallible_iterator::FallibleIterator;
1113
use postgres::{HandleNotice, Connection, GenericConnection, TlsMode};

0 commit comments

Comments
 (0)