Skip to content

Commit

Permalink
feat(net): Allow more generic SSL verification (fixes #244)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Jan 14, 2015
1 parent 8f66de4 commit af57785
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ use std::iter::Extend;
use url::UrlParser;
use url::ParseError as UrlError;

use openssl::ssl::VerifyCallback;

use header::{Headers, Header, HeaderFormat};
use header::common::{ContentLength, Location};
use method::Method;
use net::{NetworkConnector, HttpConnector};
use net::{NetworkConnector, HttpConnector, ContextVerifier};
use status::StatusClass::Redirection;
use {Url, Port, HttpResult};
use HttpError::HttpUriError;
Expand All @@ -57,7 +55,7 @@ impl Client<HttpConnector> {
}

/// Set the SSL verifier callback for use with OpenSSL.
pub fn set_ssl_verifier(&mut self, verifier: VerifyCallback) {
pub fn set_ssl_verifier(&mut self, verifier: ContextVerifier) {
self.connector = HttpConnector(Some(verifier));
}

Expand Down
13 changes: 9 additions & 4 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use std::raw::{self, TraitObject};
use std::sync::Arc;

use uany::UnsafeAnyExt;
use openssl::ssl::{Ssl, SslStream, SslContext, VerifyCallback};
use openssl::ssl::SslVerifyMode::{SslVerifyPeer, SslVerifyNone};
use openssl::ssl::{Ssl, SslStream, SslContext};
use openssl::ssl::SslVerifyMode::SslVerifyNone;
use openssl::ssl::SslMethod::Sslv23;
use openssl::ssl::error::{SslError, StreamError, OpenSslErrors, SslSessionClosed};
use openssl::x509::X509FileType;
Expand Down Expand Up @@ -309,7 +309,10 @@ impl NetworkStream for HttpStream {

/// A connector that will produce HttpStreams.
#[allow(missing_copy_implementations)]
pub struct HttpConnector(pub Option<VerifyCallback>);
pub struct HttpConnector(pub Option<ContextVerifier>);

/// A method that can set verification methods on an SSL context
pub type ContextVerifier = for <'a> fn(&'a mut SslContext) -> ();

impl NetworkConnector for HttpConnector {
type Stream = HttpStream;
Expand All @@ -325,7 +328,9 @@ impl NetworkConnector for HttpConnector {
debug!("https scheme");
let stream = try!(TcpStream::connect(addr));
let mut context = try!(SslContext::new(Sslv23).map_err(lift_ssl_error));
self.0.as_ref().map(|cb| context.set_verify(SslVerifyPeer, Some(*cb)));
if let Some(ref v) = self.0 {
v(&mut context);
}
let ssl = try!(Ssl::new(&context).map_err(lift_ssl_error));
try!(ssl.set_hostname(host).map_err(lift_ssl_error));
let stream = try!(SslStream::new(&context, stream).map_err(lift_ssl_error));
Expand Down

0 comments on commit af57785

Please sign in to comment.