Skip to content

Commit bebb315

Browse files
committed
Sync tls crates up to 0.15 API
1 parent 9e313fa commit bebb315

File tree

4 files changed

+69
-32
lines changed

4 files changed

+69
-32
lines changed

postgres-native-tls/src/lib.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
1+
//! Native TLS support for the `postgres` crate.
12
pub extern crate native_tls;
23
extern crate postgres;
34

45
use native_tls::TlsConnector;
56
use postgres::tls::{Stream, TlsHandshake, TlsStream};
67
use std::error::Error;
7-
use std::fmt::{self, Debug};
8+
use std::fmt;
89
use std::io::{self, Read, Write};
910

1011
#[cfg(test)]
1112
mod test;
1213

13-
pub struct NativeTls {
14-
connector: TlsConnector,
15-
}
14+
/// A `TlsHandshake` implementation that uses the native-tls crate.
15+
///
16+
/// Requires the `with-native-tls` feature.
17+
pub struct NativeTls(TlsConnector);
1618

17-
impl Debug for NativeTls {
19+
impl fmt::Debug for NativeTls {
1820
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1921
fmt.debug_struct("NativeTls").finish()
2022
}
2123
}
2224

2325
impl NativeTls {
26+
/// Creates a new `NativeTls` with its default configuration.
2427
pub fn new() -> Result<NativeTls, native_tls::Error> {
2528
let connector = TlsConnector::builder().build()?;
26-
Ok(NativeTls::with_connector(connector))
29+
Ok(NativeTls(connector))
30+
}
31+
32+
/// Returns a reference to the inner `TlsConnector`.
33+
pub fn connector(&self) -> &TlsConnector {
34+
&self.0
2735
}
2836

29-
pub fn with_connector(connector: TlsConnector) -> NativeTls {
30-
NativeTls { connector }
37+
/// Returns a mutable reference to the inner `TlsConnector`.
38+
pub fn connector_mut(&mut self) -> &mut TlsConnector {
39+
&mut self.0
40+
}
41+
}
42+
43+
impl From<TlsConnector> for NativeTls {
44+
fn from(connector: TlsConnector) -> NativeTls {
45+
NativeTls(connector)
3146
}
3247
}
3348

@@ -36,8 +51,8 @@ impl TlsHandshake for NativeTls {
3651
&self,
3752
domain: &str,
3853
stream: Stream,
39-
) -> Result<Box<TlsStream>, Box<Error + Sync + Send>> {
40-
let stream = self.connector.connect(domain, stream)?;
54+
) -> Result<Box<TlsStream>, Box<Error + Send + Sync>> {
55+
let stream = self.0.connect(domain, stream)?;
4156
Ok(Box::new(NativeTlsStream(stream)))
4257
}
4358
}

postgres-native-tls/src/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn connect() {
1212
builder.add_root_certificate(cert);
1313
let connector = builder.build().unwrap();
1414

15-
let handshake = NativeTls::with_connector(connector);
15+
let handshake = NativeTls::from(connector);
1616
let conn = Connection::connect(
1717
"postgres://ssl_user@localhost:5433/postgres",
1818
TlsMode::Require(&handshake),

postgres-openssl/src/lib.rs

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1+
//! OpenSSL support for the `postgres` crate.
12
pub extern crate openssl;
23
extern crate postgres;
34

4-
use openssl::error::ErrorStack;
5-
use openssl::ssl::{ConnectConfiguration, SslConnector, SslMethod, SslStream};
6-
use postgres::tls::{Stream, TlsHandshake, TlsStream};
75
use std::error::Error;
8-
use std::fmt;
96
use std::io::{self, Read, Write};
7+
use std::fmt;
8+
use openssl::error::ErrorStack;
9+
use openssl::ssl::{SslMethod, SslConnector, SslStream};
10+
use postgres::tls::{TlsStream, Stream, TlsHandshake};
1011

1112
#[cfg(test)]
1213
mod test;
1314

15+
/// A `TlsHandshake` implementation that uses OpenSSL.
16+
///
17+
/// Requires the `with-openssl` feature.
1418
pub struct OpenSsl {
1519
connector: SslConnector,
16-
config: Box<Fn(&mut ConnectConfiguration) -> Result<(), ErrorStack> + Sync + Send>,
20+
disable_verification: bool,
1721
}
1822

1923
impl fmt::Debug for OpenSsl {
@@ -23,23 +27,39 @@ impl fmt::Debug for OpenSsl {
2327
}
2428

2529
impl OpenSsl {
30+
/// Creates a `OpenSsl` with `SslConnector`'s default configuration.
2631
pub fn new() -> Result<OpenSsl, ErrorStack> {
2732
let connector = SslConnector::builder(SslMethod::tls())?.build();
28-
Ok(OpenSsl::with_connector(connector))
33+
Ok(OpenSsl::from(connector))
2934
}
3035

31-
pub fn with_connector(connector: SslConnector) -> OpenSsl {
32-
OpenSsl {
33-
connector,
34-
config: Box::new(|_| Ok(())),
35-
}
36+
/// Returns a reference to the inner `SslConnector`.
37+
pub fn connector(&self) -> &SslConnector {
38+
&self.connector
39+
}
40+
41+
/// Returns a mutable reference to the inner `SslConnector`.
42+
pub fn connector_mut(&mut self) -> &mut SslConnector {
43+
&mut self.connector
3644
}
3745

38-
pub fn callback<F>(&mut self, f: F)
39-
where
40-
F: Fn(&mut ConnectConfiguration) -> Result<(), ErrorStack> + 'static + Sync + Send,
41-
{
42-
self.config = Box::new(f);
46+
/// If set, the
47+
/// `SslConnector::danger_connect_without_providing_domain_for_certificate_verification_and_server_name_indication`
48+
/// method will be used to connect.
49+
///
50+
/// If certificate verification has been disabled in the `SslConnector`, verification must be
51+
/// additionally disabled here for that setting to take effect.
52+
pub fn danger_disable_hostname_verification(&mut self, disable_verification: bool) {
53+
self.disable_verification = disable_verification;
54+
}
55+
}
56+
57+
impl From<SslConnector> for OpenSsl {
58+
fn from(connector: SslConnector) -> OpenSsl {
59+
OpenSsl {
60+
connector: connector,
61+
disable_verification: false,
62+
}
4363
}
4464
}
4565

@@ -48,11 +68,13 @@ impl TlsHandshake for OpenSsl {
4868
&self,
4969
domain: &str,
5070
stream: Stream,
51-
) -> Result<Box<TlsStream>, Box<Error + Sync + Send>> {
71+
) -> Result<Box<TlsStream>, Box<Error + Send + Sync>> {
5272
let mut ssl = self.connector.configure()?;
53-
(self.config)(&mut ssl)?;
73+
if self.disable_verification {
74+
ssl.set_use_server_name_indication(false);
75+
ssl.set_verify_hostname(false);
76+
}
5477
let stream = ssl.connect(domain, stream)?;
55-
5678
Ok(Box::new(OpenSslStream(stream)))
5779
}
5880
}

postgres-openssl/src/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use OpenSsl;
77
fn require() {
88
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
99
builder.set_ca_file("../test/server.crt").unwrap();
10-
let negotiator = OpenSsl::with_connector(builder.build());
10+
let negotiator = OpenSsl::from(builder.build());
1111
let conn = Connection::connect(
1212
"postgres://ssl_user@localhost:5433/postgres",
1313
TlsMode::Require(&negotiator),
@@ -19,7 +19,7 @@ fn require() {
1919
fn prefer() {
2020
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
2121
builder.set_ca_file("../test/server.crt").unwrap();
22-
let negotiator = OpenSsl::with_connector(builder.build());
22+
let negotiator = OpenSsl::from(builder.build());
2323
let conn = Connection::connect(
2424
"postgres://ssl_user@localhost:5433/postgres",
2525
TlsMode::Require(&negotiator),

0 commit comments

Comments
 (0)