@@ -48,16 +48,18 @@ use openssl::hash::MessageDigest;
48
48
use openssl:: nid:: Nid ;
49
49
#[ cfg( feature = "runtime" ) ]
50
50
use openssl:: ssl:: SslConnector ;
51
- use openssl:: ssl:: { ConnectConfiguration , SslRef } ;
52
- use std:: fmt:: Debug ;
51
+ use openssl:: ssl:: { self , ConnectConfiguration , SslRef } ;
52
+ use openssl:: x509:: X509VerifyResult ;
53
+ use std:: error:: Error ;
54
+ use std:: fmt:: { self , Debug } ;
53
55
use std:: future:: Future ;
54
56
use std:: io;
55
57
use std:: pin:: Pin ;
56
58
#[ cfg( feature = "runtime" ) ]
57
59
use std:: sync:: Arc ;
58
60
use std:: task:: { Context , Poll } ;
59
61
use tokio:: io:: { AsyncRead , AsyncWrite , ReadBuf } ;
60
- use tokio_openssl:: { HandshakeError , SslStream } ;
62
+ use tokio_openssl:: SslStream ;
61
63
use tokio_postgres:: tls;
62
64
#[ cfg( feature = "runtime" ) ]
63
65
use tokio_postgres:: tls:: MakeTlsConnect ;
@@ -131,23 +133,55 @@ impl TlsConnector {
131
133
132
134
impl < S > TlsConnect < S > for TlsConnector
133
135
where
134
- S : AsyncRead + AsyncWrite + Unpin + Debug + ' static + Sync + Send ,
136
+ S : AsyncRead + AsyncWrite + Unpin + Send + ' static ,
135
137
{
136
138
type Stream = TlsStream < S > ;
137
- type Error = HandshakeError < S > ;
139
+ type Error = Box < dyn Error + Send + Sync > ;
138
140
#[ allow( clippy:: type_complexity) ]
139
- type Future = Pin < Box < dyn Future < Output = Result < TlsStream < S > , HandshakeError < S > > > + Send > > ;
141
+ type Future = Pin < Box < dyn Future < Output = Result < TlsStream < S > , Self :: Error > > + Send > > ;
140
142
141
143
fn connect ( self , stream : S ) -> Self :: Future {
142
144
let future = async move {
143
- let stream = tokio_openssl:: connect ( self . ssl , & self . domain , stream) . await ?;
144
- Ok ( TlsStream ( stream) )
145
+ let ssl = self . ssl . into_ssl ( & self . domain ) ?;
146
+ let mut stream = SslStream :: new ( ssl, stream) ?;
147
+ match Pin :: new ( & mut stream) . connect ( ) . await {
148
+ Ok ( ( ) ) => Ok ( TlsStream ( stream) ) ,
149
+ Err ( error) => Err ( Box :: new ( ConnectError {
150
+ error,
151
+ verify_result : stream. ssl ( ) . verify_result ( ) ,
152
+ } ) as _ ) ,
153
+ }
145
154
} ;
146
155
147
156
Box :: pin ( future)
148
157
}
149
158
}
150
159
160
+ #[ derive( Debug ) ]
161
+ struct ConnectError {
162
+ error : ssl:: Error ,
163
+ verify_result : X509VerifyResult ,
164
+ }
165
+
166
+ impl fmt:: Display for ConnectError {
167
+ fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
168
+ fmt:: Display :: fmt ( & self . error , fmt) ?;
169
+
170
+ if self . verify_result != X509VerifyResult :: OK {
171
+ fmt. write_str ( ": " ) ?;
172
+ fmt:: Display :: fmt ( & self . verify_result , fmt) ?;
173
+ }
174
+
175
+ Ok ( ( ) )
176
+ }
177
+ }
178
+
179
+ impl Error for ConnectError {
180
+ fn source ( & self ) -> Option < & ( dyn Error + ' static ) > {
181
+ Some ( & self . error )
182
+ }
183
+ }
184
+
151
185
/// The stream returned by `TlsConnector`.
152
186
pub struct TlsStream < S > ( SslStream < S > ) ;
153
187
0 commit comments