forked from rust-postgres/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenssl.rs
More file actions
69 lines (59 loc) · 2.11 KB
/
Copy pathopenssl.rs
File metadata and controls
69 lines (59 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! OpenSSL support.
extern crate openssl;
extern crate openssl_verify;
use std::error::Error;
use self::openssl::ssl::{IntoSsl, SslContext, SslStream, SslMethod, SSL_VERIFY_PEER,
SSL_OP_NO_SSLV2, SSL_OP_NO_SSLV3, SSL_OP_NO_COMPRESSION};
use self::openssl::ssl::error::SslError;
use self::openssl_verify::verify_callback;
use io::{StreamWrapper, Stream, NegotiateSsl};
impl StreamWrapper for SslStream<Stream> {
fn get_ref(&self) -> &Stream {
self.get_ref()
}
fn get_mut(&mut self) -> &mut Stream {
self.get_mut()
}
}
/// A `NegotiateSsl` implementation that uses OpenSSL.
///
/// Requires the `with-openssl` feature.
#[derive(Debug)]
pub struct Negotiator(SslContext);
impl Negotiator {
/// Creates a `Negotiator` with a reasonable default configuration.
///
/// The configuration is modeled after libcurl's and is subject to change.
pub fn new() -> Result<Negotiator, SslError> {
let mut ctx = try!(SslContext::new(SslMethod::Sslv23));
try!(ctx.set_default_verify_paths());
ctx.set_options(SSL_OP_NO_SSLV2 | SSL_OP_NO_SSLV3 | SSL_OP_NO_COMPRESSION);
try!(ctx.set_cipher_list("ALL!EXPORT!EXPORT40!EXPORT56!aNULL!LOW!RC4@STRENGTH"));
Ok(ctx.into())
}
/// Returns a reference to the associated `SslContext`.
pub fn context(&self) -> &SslContext {
&self.0
}
/// Returns a mutable reference to the associated `SslContext`.
pub fn context_mut(&mut self) -> &mut SslContext {
&mut self.0
}
}
impl From<SslContext> for Negotiator {
fn from(ctx: SslContext) -> Negotiator {
Negotiator(ctx)
}
}
impl NegotiateSsl for Negotiator {
fn negotiate_ssl(&self,
domain: &str,
stream: Stream)
-> Result<Box<StreamWrapper>, Box<Error + Send + Sync>> {
let domain = domain.to_owned();
let mut ssl = try!(self.0.into_ssl());
ssl.set_verify_callback(SSL_VERIFY_PEER, move |p, x| verify_callback(&domain, p, x));
let stream = try!(SslStream::connect(ssl, stream));
Ok(Box::new(stream))
}
}