Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ssl): enable hostname verification by default for OpenSSL #784

Merged
merged 1 commit into from
May 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ default-features = false
version = "0.7"
optional = true

[dependencies.openssl-verify]
version = "0.1"
optional = true

[dependencies.security-framework]
version = "0.1.4"
optional = true
Expand All @@ -49,6 +53,6 @@ env_logger = "0.3"

[features]
default = ["ssl"]
ssl = ["openssl", "cookie/secure"]
ssl = ["openssl", "openssl-verify", "cookie/secure"]
serde-serialization = ["serde", "mime/serde"]
nightly = []
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ extern crate time;
#[macro_use] extern crate url;
#[cfg(feature = "openssl")]
extern crate openssl;
#[cfg(feature = "openssl-verify")]
extern crate openssl_verify;
#[cfg(feature = "security-framework")]
extern crate security_framework;
#[cfg(feature = "serde-serialization")]
Expand Down
15 changes: 8 additions & 7 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ mod openssl {
use std::sync::Arc;
use std::time::Duration;

use openssl::ssl::{Ssl, SslContext, SslStream, SslMethod, SSL_VERIFY_NONE};
use openssl::ssl::{Ssl, SslContext, SslStream, SslMethod, SSL_VERIFY_NONE, SSL_VERIFY_PEER, SSL_OP_NO_SSLV2, SSL_OP_NO_SSLV3};
use openssl::ssl::error::StreamError as SslIoError;
use openssl::ssl::error::SslError;
use openssl::x509::X509FileType;
Expand Down Expand Up @@ -651,11 +651,10 @@ mod openssl {

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

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

fn wrap_client(&self, stream: T, host: &str) -> ::Result<Self::Stream> {
let ssl = try!(Ssl::new(&self.0));
let mut ssl = try!(Ssl::new(&self.0));
try!(ssl.set_hostname(host));
let host = host.to_owned();
ssl.set_verify_callback(SSL_VERIFY_PEER, move |p, x| ::openssl_verify::verify_callback(&host, p, x));
SslStream::connect(ssl, stream).map_err(From::from)
}
}
Expand Down