Skip to content

Commit bf48203

Browse files
committed
Make openssl an optional dependency
1 parent 3c83d46 commit bf48203

5 files changed

Lines changed: 45 additions & 33 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ phf_codegen = "0.7"
2525

2626
[dependencies]
2727
phf = "0.7"
28-
openssl = "0.6"
2928
log = "0.3"
3029
rustc-serialize = "0.3"
3130
byteorder = "0.3"
3231
debug-builders = "0.1"
3332
bufstream = "0.1"
33+
rust-crypto = "0.2"
34+
openssl = { version = "0.6", optional = true }
3435
uuid = { version = "0.1", optional = true }
3536
unix_socket = { version = "0.3", optional = true }
3637
time = { version = "0.1.14", optional = true }

src/io.rs renamed to src/io/mod.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! Types and traits for SSL adaptors.
2-
32
pub use priv_io::Stream;
43

5-
use openssl::ssl::{SslStream, SslContext};
64
use std::error::Error;
75
use std::io::prelude::*;
86

7+
#[cfg(feature = "openssl")]
8+
mod openssl;
9+
910
/// A trait implemented by SSL adaptors.
1011
pub trait StreamWrapper: Read+Write+Send {
1112
/// Returns a reference to the underlying `Stream`.
@@ -15,16 +16,6 @@ pub trait StreamWrapper: Read+Write+Send {
1516
fn get_mut(&mut self) -> &mut Stream;
1617
}
1718

18-
impl StreamWrapper for SslStream<Stream> {
19-
fn get_ref(&self) -> &Stream {
20-
self.get_ref()
21-
}
22-
23-
fn get_mut(&mut self) -> &mut Stream {
24-
self.get_mut()
25-
}
26-
}
27-
2819
/// A trait implemented by types that can negotiate SSL over a Postgres stream.
2920
pub trait NegotiateSsl {
3021
/// Negotiates an SSL session, returning a wrapper around the provided
@@ -36,14 +27,6 @@ pub trait NegotiateSsl {
3627
-> Result<Box<StreamWrapper>, Box<Error>>;
3728
}
3829

39-
impl NegotiateSsl for SslContext {
40-
fn negotiate_ssl(&mut self, _: &str, stream: Stream)
41-
-> Result<Box<StreamWrapper>, Box<Error>> {
42-
let stream = try!(SslStream::new(self, stream));
43-
Ok(Box::new(stream))
44-
}
45-
}
46-
4730
/// An uninhabited type implementing `NegotiateSsl`.
4831
///
4932
/// `NoSsl` cannot be instantiated, so the only `SslMode<NoSslMode>` value that

src/io/openssl.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
extern crate openssl;
2+
3+
use std::error::Error;
4+
5+
use self::openssl::ssl::{SslContext, SslStream};
6+
use io::{StreamWrapper, Stream, NegotiateSsl};
7+
8+
impl StreamWrapper for SslStream<Stream> {
9+
fn get_ref(&self) -> &Stream {
10+
self.get_ref()
11+
}
12+
13+
fn get_mut(&mut self) -> &mut Stream {
14+
self.get_mut()
15+
}
16+
}
17+
18+
impl NegotiateSsl for SslContext {
19+
fn negotiate_ssl(&mut self, _: &str, stream: Stream)
20+
-> Result<Box<StreamWrapper>, Box<Error>> {
21+
let stream = try!(SslStream::new(self, stream));
22+
Ok(Box::new(stream))
23+
}
24+
}

src/lib.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@
4747

4848
extern crate bufstream;
4949
extern crate byteorder;
50+
extern crate crypto;
5051
#[macro_use]
5152
extern crate log;
52-
extern crate openssl;
5353
extern crate phf;
5454
extern crate rustc_serialize as serialize;
5555
#[cfg(feature = "unix_socket")]
5656
extern crate unix_socket;
5757
extern crate debug_builders;
5858

5959
use bufstream::BufStream;
60+
use crypto::digest::Digest;
61+
use crypto::md5::Md5;
6062
use debug_builders::DebugStruct;
61-
use openssl::crypto::hash::{self, Hasher};
62-
use serialize::hex::ToHex;
6363
use std::ascii::AsciiExt;
6464
use std::borrow::{ToOwned, Cow};
6565
use std::cell::{Cell, RefCell};
@@ -640,13 +640,14 @@ impl InnerConnection {
640640
}
641641
AuthenticationMD5Password { salt } => {
642642
let pass = try!(user.password.ok_or(ConnectError::MissingPassword));
643-
let mut hasher = Hasher::new(hash::Type::MD5);
644-
let _ = hasher.write_all(pass.as_bytes());
645-
let _ = hasher.write_all(user.user.as_bytes());
646-
let output = hasher.finish().to_hex();
647-
let _ = hasher.write_all(output.as_bytes());
648-
let _ = hasher.write_all(&salt);
649-
let output = format!("md5{}", hasher.finish().to_hex());
643+
let mut hasher = Md5::new();
644+
let _ = hasher.input(pass.as_bytes());
645+
let _ = hasher.input(user.user.as_bytes());
646+
let output = hasher.result_str();
647+
hasher.reset();
648+
let _ = hasher.input(output.as_bytes());
649+
let _ = hasher.input(&salt);
650+
let output = format!("md5{}", hasher.result_str());
650651
try!(self.write_messages(&[PasswordMessage {
651652
password: &output
652653
}]));

tests/test.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
extern crate postgres;
22
extern crate rustc_serialize as serialize;
33
extern crate url;
4+
#[cfg(feature = "openssl")]
45
extern crate openssl;
56

6-
use openssl::ssl::SslContext;
7-
use openssl::ssl::SslMethod;
7+
#[cfg(feature = "openssl")]
8+
use openssl::ssl::{SslContext, SslMethod};
89
use std::thread;
910

1011
use postgres::{HandleNotice,
@@ -670,6 +671,7 @@ fn test_cancel_query() {
670671
}
671672

672673
#[test]
674+
#[cfg(feature = "openssl")]
673675
fn test_require_ssl_conn() {
674676
let ctx = SslContext::new(SslMethod::Sslv23).unwrap();
675677
let conn = or_panic!(Connection::connect("postgres://postgres@localhost",
@@ -678,6 +680,7 @@ fn test_require_ssl_conn() {
678680
}
679681

680682
#[test]
683+
#[cfg(feature = "openssl")]
681684
fn test_prefer_ssl_conn() {
682685
let ctx = SslContext::new(SslMethod::Sslv23).unwrap();
683686
let conn = or_panic!(Connection::connect("postgres://postgres@localhost",

0 commit comments

Comments
 (0)