1
+ //! OpenSSL support for the `postgres` crate.
1
2
pub extern crate openssl;
2
3
extern crate postgres;
3
4
4
- use openssl:: error:: ErrorStack ;
5
- use openssl:: ssl:: { ConnectConfiguration , SslConnector , SslMethod , SslStream } ;
6
- use postgres:: tls:: { Stream , TlsHandshake , TlsStream } ;
7
5
use std:: error:: Error ;
8
- use std:: fmt;
9
6
use std:: io:: { self , Read , Write } ;
7
+ use std:: fmt;
8
+ use openssl:: error:: ErrorStack ;
9
+ use openssl:: ssl:: { SslMethod , SslConnector , SslStream } ;
10
+ use postgres:: tls:: { TlsStream , Stream , TlsHandshake } ;
10
11
11
12
#[ cfg( test) ]
12
13
mod test;
13
14
15
+ /// A `TlsHandshake` implementation that uses OpenSSL.
16
+ ///
17
+ /// Requires the `with-openssl` feature.
14
18
pub struct OpenSsl {
15
19
connector : SslConnector ,
16
- config : Box < Fn ( & mut ConnectConfiguration ) -> Result < ( ) , ErrorStack > + Sync + Send > ,
20
+ disable_verification : bool ,
17
21
}
18
22
19
23
impl fmt:: Debug for OpenSsl {
@@ -23,23 +27,39 @@ impl fmt::Debug for OpenSsl {
23
27
}
24
28
25
29
impl OpenSsl {
30
+ /// Creates a `OpenSsl` with `SslConnector`'s default configuration.
26
31
pub fn new ( ) -> Result < OpenSsl , ErrorStack > {
27
32
let connector = SslConnector :: builder ( SslMethod :: tls ( ) ) ?. build ( ) ;
28
- Ok ( OpenSsl :: with_connector ( connector) )
33
+ Ok ( OpenSsl :: from ( connector) )
29
34
}
30
35
31
- pub fn with_connector ( connector : SslConnector ) -> OpenSsl {
32
- OpenSsl {
33
- connector,
34
- config : Box :: new ( |_| Ok ( ( ) ) ) ,
35
- }
36
+ /// Returns a reference to the inner `SslConnector`.
37
+ pub fn connector ( & self ) -> & SslConnector {
38
+ & self . connector
39
+ }
40
+
41
+ /// Returns a mutable reference to the inner `SslConnector`.
42
+ pub fn connector_mut ( & mut self ) -> & mut SslConnector {
43
+ & mut self . connector
36
44
}
37
45
38
- pub fn callback < F > ( & mut self , f : F )
39
- where
40
- F : Fn ( & mut ConnectConfiguration ) -> Result < ( ) , ErrorStack > + ' static + Sync + Send ,
41
- {
42
- self . config = Box :: new ( f) ;
46
+ /// If set, the
47
+ /// `SslConnector::danger_connect_without_providing_domain_for_certificate_verification_and_server_name_indication`
48
+ /// method will be used to connect.
49
+ ///
50
+ /// If certificate verification has been disabled in the `SslConnector`, verification must be
51
+ /// additionally disabled here for that setting to take effect.
52
+ pub fn danger_disable_hostname_verification ( & mut self , disable_verification : bool ) {
53
+ self . disable_verification = disable_verification;
54
+ }
55
+ }
56
+
57
+ impl From < SslConnector > for OpenSsl {
58
+ fn from ( connector : SslConnector ) -> OpenSsl {
59
+ OpenSsl {
60
+ connector : connector,
61
+ disable_verification : false ,
62
+ }
43
63
}
44
64
}
45
65
@@ -48,11 +68,13 @@ impl TlsHandshake for OpenSsl {
48
68
& self ,
49
69
domain : & str ,
50
70
stream : Stream ,
51
- ) -> Result < Box < TlsStream > , Box < Error + Sync + Send > > {
71
+ ) -> Result < Box < TlsStream > , Box < Error + Send + Sync > > {
52
72
let mut ssl = self . connector . configure ( ) ?;
53
- ( self . config ) ( & mut ssl) ?;
73
+ if self . disable_verification {
74
+ ssl. set_use_server_name_indication ( false ) ;
75
+ ssl. set_verify_hostname ( false ) ;
76
+ }
54
77
let stream = ssl. connect ( domain, stream) ?;
55
-
56
78
Ok ( Box :: new ( OpenSslStream ( stream) ) )
57
79
}
58
80
}
0 commit comments