Skip to content

Commit fe0e1ad

Browse files
committed
Update to openssl 0.9
1 parent 7d96c13 commit fe0e1ad

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ path = "tests/test.rs"
2424
with-bit-vec = ["bit-vec"]
2525
with-chrono = ["chrono"]
2626
with-eui48 = ["eui48"]
27-
with-openssl = ["openssl", "openssl-verify"]
27+
with-openssl = ["openssl"]
2828
with-rustc-serialize = ["rustc-serialize"]
2929
with-security-framework = ["security-framework"]
3030
with-serde_json = ["serde_json"]
@@ -41,8 +41,7 @@ postgres-protocol = "0.1"
4141
bit-vec = { version = "0.4", optional = true }
4242
chrono = { version = "0.2.14", optional = true }
4343
eui48 = { version = "0.1", optional = true }
44-
openssl-verify = { version = "0.2", optional = true }
45-
openssl = { version = "0.8", optional = true }
44+
openssl = { version = "0.9", optional = true }
4645
rustc-serialize = { version = "0.3", optional = true }
4746
security-framework = { version = "0.1.2", optional = true }
4847
serde_json = { version = ">= 0.6, < 0.9", optional = true }

src/tls/openssl.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
//! OpenSSL support.
22
extern crate openssl;
3-
extern crate openssl_verify;
43

54
use std::error::Error;
5+
use std::fmt;
66

77
use self::openssl::error::ErrorStack;
8-
use self::openssl::ssl::{IntoSsl, SslContext, SslStream, SslMethod, SSL_VERIFY_PEER,
9-
SSL_OP_NO_SSLV2, SSL_OP_NO_SSLV3, SSL_OP_NO_COMPRESSION};
10-
use self::openssl_verify::verify_callback;
8+
use self::openssl::ssl::{SslMethod, SslConnector, SslConnectorBuilder, SslStream};
119
use tls::{TlsStream, Stream, TlsHandshake};
1210

1311
impl TlsStream for SslStream<Stream> {
@@ -23,35 +21,35 @@ impl TlsStream for SslStream<Stream> {
2321
/// A `TlsHandshake` implementation that uses OpenSSL.
2422
///
2523
/// Requires the `with-openssl` feature.
26-
#[derive(Debug)]
27-
pub struct OpenSsl(SslContext);
24+
pub struct OpenSsl(SslConnector);
25+
26+
impl fmt::Debug for OpenSsl {
27+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
28+
fmt.debug_struct("OpenSsl").finish()
29+
}
30+
}
2831

2932
impl OpenSsl {
30-
/// Creates a `OpenSsl` with a reasonable default configuration.
31-
///
32-
/// The configuration is modeled after libcurl's and is subject to change.
33+
/// Creates a `OpenSsl` with `SslConnector`'s default configuration.
3334
pub fn new() -> Result<OpenSsl, ErrorStack> {
34-
let mut ctx = try!(SslContext::new(SslMethod::Sslv23));
35-
try!(ctx.set_default_verify_paths());
36-
ctx.set_options(SSL_OP_NO_SSLV2 | SSL_OP_NO_SSLV3 | SSL_OP_NO_COMPRESSION);
37-
try!(ctx.set_cipher_list("ALL!EXPORT!EXPORT40!EXPORT56!aNULL!LOW!RC4@STRENGTH"));
38-
Ok(ctx.into())
35+
let connector = try!(SslConnectorBuilder::new(SslMethod::tls())).build();
36+
Ok(OpenSsl(connector))
3937
}
4038

41-
/// Returns a reference to the associated `SslContext`.
42-
pub fn context(&self) -> &SslContext {
39+
/// Returns a reference to the inner `SslConnector`.
40+
pub fn connector(&self) -> &SslConnector {
4341
&self.0
4442
}
4543

46-
/// Returns a mutable reference to the associated `SslContext`.
47-
pub fn context_mut(&mut self) -> &mut SslContext {
44+
/// Returns a mutable reference to the inner `SslConnector`.
45+
pub fn connector_mut(&mut self) -> &mut SslConnector {
4846
&mut self.0
4947
}
5048
}
5149

52-
impl From<SslContext> for OpenSsl {
53-
fn from(ctx: SslContext) -> OpenSsl {
54-
OpenSsl(ctx)
50+
impl From<SslConnector> for OpenSsl {
51+
fn from(connector: SslConnector) -> OpenSsl {
52+
OpenSsl(connector)
5553
}
5654
}
5755

@@ -60,10 +58,7 @@ impl TlsHandshake for OpenSsl {
6058
domain: &str,
6159
stream: Stream)
6260
-> Result<Box<TlsStream>, Box<Error + Send + Sync>> {
63-
let domain = domain.to_owned();
64-
let mut ssl = try!(self.0.into_ssl());
65-
ssl.set_verify_callback(SSL_VERIFY_PEER, move |p, x| verify_callback(&domain, p, x));
66-
let stream = try!(SslStream::connect(ssl, stream));
61+
let stream = try!(self.0.connect(domain, stream));
6762
Ok(Box::new(stream))
6863
}
6964
}

tests/test.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,12 @@ fn test_cancel_query() {
664664
#[test]
665665
#[cfg(feature = "with-openssl")]
666666
fn test_require_ssl_conn() {
667+
use openssl::ssl::{SslMethod, SslConnectorBuilder};
667668
use postgres::tls::openssl::OpenSsl;
668669

669-
let mut negotiator = OpenSsl::new().unwrap();
670-
negotiator.context_mut().set_CA_file(".travis/server.crt").unwrap();
670+
let mut builder = SslConnectorBuilder::new(SslMethod::tls()).unwrap();
671+
builder.builder_mut().set_ca_file(".travis/server.crt").unwrap();
672+
let negotiator = OpenSsl::from(builder.build());
671673
let conn = or_panic!(Connection::connect("postgres://postgres@localhost",
672674
TlsMode::Require(&negotiator)));
673675
or_panic!(conn.execute("SELECT 1::VARCHAR", &[]));
@@ -676,10 +678,12 @@ fn test_require_ssl_conn() {
676678
#[test]
677679
#[cfg(feature = "with-openssl")]
678680
fn test_prefer_ssl_conn() {
681+
use openssl::ssl::{SslMethod, SslConnectorBuilder};
679682
use postgres::tls::openssl::OpenSsl;
680683

681-
let mut negotiator = OpenSsl::new().unwrap();
682-
negotiator.context_mut().set_CA_file(".travis/server.crt").unwrap();
684+
let mut builder = SslConnectorBuilder::new(SslMethod::tls()).unwrap();
685+
builder.builder_mut().set_ca_file(".travis/server.crt").unwrap();
686+
let negotiator = OpenSsl::from(builder.build());
683687
let conn = or_panic!(Connection::connect("postgres://postgres@localhost",
684688
TlsMode::Require(&negotiator)));
685689
or_panic!(conn.execute("SELECT 1::VARCHAR", &[]));

0 commit comments

Comments
 (0)