1515//! let connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
1616//!
1717//! let stream = TcpStream::connect("google.com:443").unwrap();
18- //! let mut stream = connector.connect("google.com", stream).unwrap();
18+ //! let mut stream = connector
19+ //! .setup_connect("google.com", stream)
20+ //! .unwrap()
21+ //! .handshake()
22+ //! .unwrap();
1923//!
2024//! stream.write_all(b"GET / HTTP/1.0\r\n\r\n").unwrap();
2125//! let mut res = vec![];
4953//! Ok(stream) => {
5054//! let acceptor = acceptor.clone();
5155//! thread::spawn(move || {
52- //! let stream = acceptor.accept(stream).unwrap();
56+ //! let stream = acceptor
57+ //! .setup_accept(stream)
58+ //! .unwrap()
59+ //! .handshake()
60+ //! .unwrap();
61+ //!
5362//! handle_client(stream);
5463//! });
5564//! }
@@ -107,7 +116,7 @@ pub use self::connector::{
107116 ConnectConfiguration , SslAcceptor , SslAcceptorBuilder , SslConnector , SslConnectorBuilder ,
108117} ;
109118pub use self :: ech:: { SslEchKeys , SslEchKeysRef } ;
110- pub use self :: error:: { Error , ErrorCode , HandshakeError } ;
119+ pub use self :: error:: { Error , ErrorCode } ;
111120
112121mod async_callbacks;
113122mod bio;
@@ -2742,22 +2751,6 @@ impl Ssl {
27422751 SslStreamBuilder :: new ( self , stream) . setup_connect ( )
27432752 }
27442753
2745- /// Attempts a client-side TLS handshake.
2746- ///
2747- /// This is a convenience method which combines [`Self::setup_connect`] and
2748- /// [`MidHandshakeSslStream::handshake`].
2749- ///
2750- /// # Warning
2751- ///
2752- /// OpenSSL's default configuration is insecure. It is highly recommended to use
2753- /// [`SslConnector`] rather than `Ssl` directly, as it manages that configuration.
2754- pub fn connect < S > ( self , stream : S ) -> Result < SslStream < S > , HandshakeError < S > >
2755- where
2756- S : Read + Write ,
2757- {
2758- self . setup_connect ( stream) . handshake ( )
2759- }
2760-
27612754 /// Initiates a server-side TLS handshake.
27622755 ///
27632756 /// This method is guaranteed to return without calling any callback defined
@@ -2790,24 +2783,6 @@ impl Ssl {
27902783
27912784 SslStreamBuilder :: new ( self , stream) . setup_accept ( )
27922785 }
2793-
2794- /// Attempts a server-side TLS handshake.
2795- ///
2796- /// This is a convenience method which combines [`Self::setup_accept`] and
2797- /// [`MidHandshakeSslStream::handshake`].
2798- ///
2799- /// # Warning
2800- ///
2801- /// OpenSSL's default configuration is insecure. It is highly recommended to use
2802- /// `SslAcceptor` rather than `Ssl` directly, as it manages that configuration.
2803- ///
2804- /// [`SSL_accept`]: https://www.openssl.org/docs/manmaster/man3/SSL_accept.html
2805- pub fn accept < S > ( self , stream : S ) -> Result < SslStream < S > , HandshakeError < S > >
2806- where
2807- S : Read + Write ,
2808- {
2809- self . setup_accept ( stream) . handshake ( )
2810- }
28112786}
28122787
28132788impl fmt:: Debug for SslRef {
@@ -3903,18 +3878,43 @@ impl<S> MidHandshakeSslStream<S> {
39033878
39043879 /// Restarts the handshake process.
39053880 #[ corresponds( SSL_do_handshake ) ]
3906- pub fn handshake ( mut self ) -> Result < SslStream < S > , HandshakeError < S > > {
3881+ pub fn handshake ( mut self ) -> Result < SslStream < S > , Self > {
39073882 let ret = unsafe { ffi:: SSL_do_handshake ( self . stream . ssl . as_ptr ( ) ) } ;
39083883 if ret > 0 {
39093884 Ok ( self . stream )
39103885 } else {
39113886 self . error = self . stream . make_error ( ret) ;
3912- Err ( if self . error . would_block ( ) {
3913- HandshakeError :: WouldBlock ( self )
3914- } else {
3915- HandshakeError :: Failure ( self )
3916- } )
3887+
3888+ Err ( self )
3889+ }
3890+ }
3891+
3892+ /// An `impl Display` suitable to represent the current error.
3893+ pub fn display_error < ' a > ( & ' a self ) -> impl fmt:: Display + ' a {
3894+ struct Display < ' a , S > ( & ' a MidHandshakeSslStream < S > ) ;
3895+
3896+ impl < S > fmt:: Display for Display < ' _ , S > {
3897+ fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
3898+ fmt. write_str ( "TLS handshake failed" ) ?;
3899+
3900+ #[ cfg( feature = "rpk" ) ]
3901+ if self . 0 . ssl ( ) . ssl_context ( ) . is_rpk ( ) {
3902+ return self . 0 . error ( ) . fmt ( fmt) ;
3903+ }
3904+
3905+ match self . 0 . ssl ( ) . verify_result ( ) {
3906+ // INVALID_CALL is returned if no verification took place,
3907+ // such as before a cert is sent.
3908+ Ok ( ( ) ) | Err ( X509VerifyError :: INVALID_CALL ) => { }
3909+ Err ( verify) => write ! ( fmt, ": cert verification failed - {verify}" ) ?,
3910+ }
3911+
3912+ fmt. write_str ( " " ) ?;
3913+ self . 0 . error ( ) . fmt ( fmt)
3914+ }
39173915 }
3916+
3917+ Display ( self )
39183918 }
39193919}
39203920
@@ -4249,22 +4249,20 @@ where
42494249
42504250 /// Configure as an outgoing stream from a client.
42514251 #[ corresponds( SSL_set_connect_state ) ]
4252- pub fn set_connect_state ( & mut self ) {
4252+ fn set_connect_state ( & mut self ) {
42534253 unsafe { ffi:: SSL_set_connect_state ( self . inner . ssl . as_ptr ( ) ) }
42544254 }
42554255
42564256 /// Configure as an incoming stream to a server.
42574257 #[ corresponds( SSL_set_accept_state ) ]
4258- pub fn set_accept_state ( & mut self ) {
4258+ fn set_accept_state ( & mut self ) {
42594259 unsafe { ffi:: SSL_set_accept_state ( self . inner . ssl . as_ptr ( ) ) }
42604260 }
42614261
42624262 /// Initiates a client-side TLS handshake, returning a [`MidHandshakeSslStream`].
42634263 ///
4264- /// This method calls [`Self::set_connect_state`] and returns without actually
4265- /// initiating the handshake. The caller is then free to call
4266- /// [`MidHandshakeSslStream`] and loop on [`HandshakeError::WouldBlock`].
4267- #[ must_use]
4264+ /// The caller is then free to call [`MidHandshakeSslStream::handshake`] and retry
4265+ /// on blocking errors.
42684266 pub fn setup_connect ( mut self ) -> MidHandshakeSslStream < S > {
42694267 self . set_connect_state ( ) ;
42704268
@@ -4280,20 +4278,10 @@ where
42804278 }
42814279 }
42824280
4283- /// Attempts a client-side TLS handshake.
4284- ///
4285- /// This is a convenience method which combines [`Self::setup_connect`] and
4286- /// [`MidHandshakeSslStream::handshake`].
4287- pub fn connect ( self ) -> Result < SslStream < S > , HandshakeError < S > > {
4288- self . setup_connect ( ) . handshake ( )
4289- }
4290-
42914281 /// Initiates a server-side TLS handshake, returning a [`MidHandshakeSslStream`].
42924282 ///
4293- /// This method calls [`Self::set_accept_state`] and returns without actually
4294- /// initiating the handshake. The caller is then free to call
4295- /// [`MidHandshakeSslStream`] and loop on [`HandshakeError::WouldBlock`].
4296- #[ must_use]
4283+ /// The caller is then free to call [`MidHandshakeSslStream::handshake`] and retry
4284+ /// on blocking errors.
42974285 pub fn setup_accept ( mut self ) -> MidHandshakeSslStream < S > {
42984286 self . set_accept_state ( ) ;
42994287
@@ -4308,33 +4296,6 @@ where
43084296 } ,
43094297 }
43104298 }
4311-
4312- /// Attempts a server-side TLS handshake.
4313- ///
4314- /// This is a convenience method which combines [`Self::setup_accept`] and
4315- /// [`MidHandshakeSslStream::handshake`].
4316- pub fn accept ( self ) -> Result < SslStream < S > , HandshakeError < S > > {
4317- self . setup_accept ( ) . handshake ( )
4318- }
4319-
4320- /// Initiates the handshake.
4321- ///
4322- /// This will fail if `set_accept_state` or `set_connect_state` was not called first.
4323- #[ corresponds( SSL_do_handshake ) ]
4324- pub fn handshake ( self ) -> Result < SslStream < S > , HandshakeError < S > > {
4325- let mut stream = self . inner ;
4326- let ret = unsafe { ffi:: SSL_do_handshake ( stream. ssl . as_ptr ( ) ) } ;
4327- if ret > 0 {
4328- Ok ( stream)
4329- } else {
4330- let error = stream. make_error ( ret) ;
4331- Err ( if error. would_block ( ) {
4332- HandshakeError :: WouldBlock ( MidHandshakeSslStream { stream, error } )
4333- } else {
4334- HandshakeError :: Failure ( MidHandshakeSslStream { stream, error } )
4335- } )
4336- }
4337- }
43384299}
43394300
43404301impl < S > SslStreamBuilder < S > {
0 commit comments