1+ //! NegotiateSsl support for OpenSSL.
12extern crate openssl;
23extern crate openssl_verify;
34
45use std:: error:: Error ;
56
6- use self :: openssl:: ssl:: { IntoSsl , SslContext , SslStream , SSL_VERIFY_PEER } ;
7+ use self :: openssl:: ssl:: { IntoSsl , SslContext , SslStream , SslMethod , SSL_VERIFY_PEER ,
8+ SSL_OP_NO_SSLV2 , SSL_OP_NO_SSLV3 , SSL_OP_NO_COMPRESSION } ;
9+ use self :: openssl:: ssl:: error:: SslError ;
710use self :: openssl_verify:: verify_callback;
811use io:: { StreamWrapper , Stream , NegotiateSsl } ;
912
@@ -17,13 +20,46 @@ impl StreamWrapper for SslStream<Stream> {
1720 }
1821}
1922
20- impl NegotiateSsl for SslContext {
23+ /// A `NegotiateSsl` implementation that uses OpenSSL.
24+ #[ derive( Debug ) ]
25+ pub struct Negotiator ( SslContext ) ;
26+
27+ impl Negotiator {
28+ /// Creates a `Negotiator` with a reasonable default configuration.
29+ ///
30+ /// The configuration is modeled after libcurl's and is subject to change.
31+ pub fn new ( ) -> Result < Negotiator , SslError > {
32+ let mut ctx = try!( SslContext :: new ( SslMethod :: Sslv23 ) ) ;
33+ try!( ctx. set_default_verify_paths ( ) ) ;
34+ ctx. set_options ( SSL_OP_NO_SSLV2 | SSL_OP_NO_SSLV3 | SSL_OP_NO_COMPRESSION ) ;
35+ try!( ctx. set_cipher_list ( "ALL!EXPORT!EXPORT40!EXPORT56!aNULL!LOW!RC4@STRENGTH" ) ) ;
36+ Ok ( ctx. into ( ) )
37+ }
38+
39+ /// Returns a reference to the associated `SslContext`.
40+ pub fn context ( & self ) -> & SslContext {
41+ & self . 0
42+ }
43+
44+ /// Returns a mutable reference to the associated `SslContext`.
45+ pub fn context_mut ( & mut self ) -> & mut SslContext {
46+ & mut self . 0
47+ }
48+ }
49+
50+ impl From < SslContext > for Negotiator {
51+ fn from ( ctx : SslContext ) -> Negotiator {
52+ Negotiator ( ctx)
53+ }
54+ }
55+
56+ impl NegotiateSsl for Negotiator {
2157 fn negotiate_ssl ( & self ,
2258 domain : & str ,
2359 stream : Stream )
2460 -> Result < Box < StreamWrapper > , Box < Error + Send + Sync > > {
2561 let domain = domain. to_owned ( ) ;
26- let mut ssl = try!( self . into_ssl ( ) ) ;
62+ let mut ssl = try!( self . 0 . into_ssl ( ) ) ;
2763 ssl. set_verify_callback ( SSL_VERIFY_PEER , move |p, x| verify_callback ( & domain, p, x) ) ;
2864 let stream = try!( SslStream :: connect ( ssl, stream) ) ;
2965 Ok ( Box :: new ( stream) )
0 commit comments