@@ -48,16 +48,18 @@ use openssl::hash::MessageDigest;
4848use openssl:: nid:: Nid ;
4949#[ cfg( feature = "runtime" ) ]
5050use 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 } ;
5355use std:: future:: Future ;
5456use std:: io;
5557use std:: pin:: Pin ;
5658#[ cfg( feature = "runtime" ) ]
5759use std:: sync:: Arc ;
5860use std:: task:: { Context , Poll } ;
5961use tokio:: io:: { AsyncRead , AsyncWrite , ReadBuf } ;
60- use tokio_openssl:: { HandshakeError , SslStream } ;
62+ use tokio_openssl:: SslStream ;
6163use tokio_postgres:: tls;
6264#[ cfg( feature = "runtime" ) ]
6365use tokio_postgres:: tls:: MakeTlsConnect ;
@@ -131,23 +133,55 @@ impl TlsConnector {
131133
132134impl < S > TlsConnect < S > for TlsConnector
133135where
134- S : AsyncRead + AsyncWrite + Unpin + Debug + ' static + Sync + Send ,
136+ S : AsyncRead + AsyncWrite + Unpin + Send + ' static ,
135137{
136138 type Stream = TlsStream < S > ;
137- type Error = HandshakeError < S > ;
139+ type Error = Box < dyn Error + Send + Sync > ;
138140 #[ 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 > > ;
140142
141143 fn connect ( self , stream : S ) -> Self :: Future {
142144 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+ }
145154 } ;
146155
147156 Box :: pin ( future)
148157 }
149158}
150159
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+
151185/// The stream returned by `TlsConnector`.
152186pub struct TlsStream < S > ( SslStream < S > ) ;
153187
0 commit comments