Skip to content

Commit 01160ab

Browse files
committed
feat(ssl): enable hostname verification by default for OpenSSL
Additionally disables SSLv2 and SSLv3, as those are universally considered unsafe. Closes #472
1 parent eab289b commit 01160ab

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ default-features = false
3232
version = "0.7"
3333
optional = true
3434

35+
[dependencies.openssl-verify]
36+
version = "0.1"
37+
optional = true
38+
3539
[dependencies.security-framework]
3640
version = "0.1.4"
3741
optional = true
@@ -49,6 +53,6 @@ env_logger = "0.3"
4953

5054
[features]
5155
default = ["ssl"]
52-
ssl = ["openssl", "cookie/secure"]
56+
ssl = ["openssl", "openssl-verify", "cookie/secure"]
5357
serde-serialization = ["serde", "mime/serde"]
5458
nightly = []

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ extern crate time;
133133
#[macro_use] extern crate url;
134134
#[cfg(feature = "openssl")]
135135
extern crate openssl;
136+
#[cfg(feature = "openssl-verify")]
137+
extern crate openssl_verify;
136138
#[cfg(feature = "security-framework")]
137139
extern crate security_framework;
138140
#[cfg(feature = "serde-serialization")]

src/net.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ mod openssl {
619619
use std::sync::Arc;
620620
use std::time::Duration;
621621

622-
use openssl::ssl::{Ssl, SslContext, SslStream, SslMethod, SSL_VERIFY_NONE};
622+
use openssl::ssl::{Ssl, SslContext, SslStream, SslMethod, SSL_VERIFY_NONE, SSL_VERIFY_PEER, SSL_OP_NO_SSLV2, SSL_OP_NO_SSLV3};
623623
use openssl::ssl::error::StreamError as SslIoError;
624624
use openssl::ssl::error::SslError;
625625
use openssl::x509::X509FileType;
@@ -651,11 +651,10 @@ mod openssl {
651651

652652
impl Default for OpensslClient {
653653
fn default() -> OpensslClient {
654-
OpensslClient(SslContext::new(SslMethod::Sslv23).unwrap_or_else(|e| {
655-
// if we cannot create a SslContext, that's because of a
656-
// serious problem. just crash.
657-
panic!("{}", e)
658-
}))
654+
let mut ctx = SslContext::new(SslMethod::Sslv23).unwrap();
655+
ctx.set_default_verify_paths().unwrap();
656+
ctx.set_options(SSL_OP_NO_SSLV2 | SSL_OP_NO_SSLV3);
657+
OpensslClient(ctx)
659658
}
660659
}
661660

@@ -664,8 +663,10 @@ mod openssl {
664663
type Stream = SslStream<T>;
665664

666665
fn wrap_client(&self, stream: T, host: &str) -> ::Result<Self::Stream> {
667-
let ssl = try!(Ssl::new(&self.0));
666+
let mut ssl = try!(Ssl::new(&self.0));
668667
try!(ssl.set_hostname(host));
668+
let host = host.to_owned();
669+
ssl.set_verify_callback(SSL_VERIFY_PEER, move |p, x| ::openssl_verify::verify_callback(&host, p, x));
669670
SslStream::connect(ssl, stream).map_err(From::from)
670671
}
671672
}

0 commit comments

Comments
 (0)