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//! }
@@ -98,7 +107,7 @@ use crate::{cvt, cvt_0i, cvt_n, cvt_p, init};
98107pub use crate :: ssl:: connector:: {
99108 ConnectConfiguration , SslAcceptor , SslAcceptorBuilder , SslConnector , SslConnectorBuilder ,
100109} ;
101- pub use crate :: ssl:: error:: { Error , ErrorCode , HandshakeError } ;
110+ pub use crate :: ssl:: error:: { Error , ErrorCode } ;
102111
103112mod bio;
104113mod callbacks;
@@ -2324,22 +2333,6 @@ impl Ssl {
23242333 SslStreamBuilder :: new ( self , stream) . setup_connect ( )
23252334 }
23262335
2327- /// Attempts a client-side TLS handshake.
2328- ///
2329- /// This is a convenience method which combines [`Self::setup_connect`] and
2330- /// [`MidHandshakeSslStream::handshake`].
2331- ///
2332- /// # Warning
2333- ///
2334- /// OpenSSL's default configuration is insecure. It is highly recommended to use
2335- /// [`SslConnector`] rather than `Ssl` directly, as it manages that configuration.
2336- pub fn connect < S > ( self , stream : S ) -> Result < SslStream < S > , HandshakeError < S > >
2337- where
2338- S : Read + Write ,
2339- {
2340- self . setup_connect ( stream) . handshake ( )
2341- }
2342-
23432336 /// Initiates a server-side TLS handshake.
23442337 ///
23452338 /// This method is guaranteed to return without calling any callback defined
@@ -2372,24 +2365,6 @@ impl Ssl {
23722365
23732366 SslStreamBuilder :: new ( self , stream) . setup_accept ( )
23742367 }
2375-
2376- /// Attempts a server-side TLS handshake.
2377- ///
2378- /// This is a convenience method which combines [`Self::setup_accept`] and
2379- /// [`MidHandshakeSslStream::handshake`].
2380- ///
2381- /// # Warning
2382- ///
2383- /// OpenSSL's default configuration is insecure. It is highly recommended to use
2384- /// `SslAcceptor` rather than `Ssl` directly, as it manages that configuration.
2385- ///
2386- /// [`SSL_accept`]: https://www.openssl.org/docs/manmaster/man3/SSL_accept.html
2387- pub fn accept < S > ( self , stream : S ) -> Result < SslStream < S > , HandshakeError < S > >
2388- where
2389- S : Read + Write ,
2390- {
2391- self . setup_accept ( stream) . handshake ( )
2392- }
23932368}
23942369
23952370impl fmt:: Debug for SslRef {
@@ -3192,16 +3167,14 @@ impl<S> MidHandshakeSslStream<S> {
31923167 /// This corresponds to [`SSL_do_handshake`].
31933168 ///
31943169 /// [`SSL_do_handshake`]: https://www.openssl.org/docs/manmaster/man3/SSL_do_handshake.html
3195- pub fn handshake ( mut self ) -> Result < SslStream < S > , HandshakeError < S > > {
3170+ pub fn handshake ( mut self ) -> Result < SslStream < S > , MidHandshakeSslStream < S > > {
31963171 let ret = unsafe { ffi:: SSL_do_handshake ( self . stream . ssl . as_ptr ( ) ) } ;
31973172 if ret > 0 {
31983173 Ok ( self . stream )
31993174 } else {
32003175 self . error = self . stream . make_error ( ret) ;
3201- match self . error . would_block ( ) {
3202- true => Err ( HandshakeError :: WouldBlock ( self ) ) ,
3203- false => Err ( HandshakeError :: Failure ( self ) ) ,
3204- }
3176+
3177+ Err ( self )
32053178 }
32063179 }
32073180}
@@ -3484,7 +3457,7 @@ where
34843457 /// This corresponds to [`SSL_set_connect_state`].
34853458 ///
34863459 /// [`SSL_set_connect_state`]: https://www.openssl.org/docs/manmaster/man3/SSL_set_connect_state.html
3487- pub fn set_connect_state ( & mut self ) {
3460+ fn set_connect_state ( & mut self ) {
34883461 unsafe { ffi:: SSL_set_connect_state ( self . inner . ssl . as_ptr ( ) ) }
34893462 }
34903463
@@ -3493,15 +3466,14 @@ where
34933466 /// This corresponds to [`SSL_set_accept_state`].
34943467 ///
34953468 /// [`SSL_set_accept_state`]: https://www.openssl.org/docs/manmaster/man3/SSL_set_accept_state.html
3496- pub fn set_accept_state ( & mut self ) {
3469+ fn set_accept_state ( & mut self ) {
34973470 unsafe { ffi:: SSL_set_accept_state ( self . inner . ssl . as_ptr ( ) ) }
34983471 }
34993472
35003473 /// Initiates a client-side TLS handshake, returning a [`MidHandshakeSslStream`].
35013474 ///
3502- /// This method calls [`Self::set_connect_state`] and returns without actually
3503- /// initiating the handshake. The caller is then free to call
3504- /// [`MidHandshakeSslStream`] and loop on [`HandshakeError::WouldBlock`].
3475+ /// The caller is then free to call [`MidHandshakeSslStream::handshake`] and retry
3476+ /// on blocking errors.
35053477 pub fn setup_connect ( mut self ) -> MidHandshakeSslStream < S > {
35063478 self . set_connect_state ( ) ;
35073479
@@ -3517,19 +3489,10 @@ where
35173489 }
35183490 }
35193491
3520- /// Attempts a client-side TLS handshake.
3521- ///
3522- /// This is a convenience method which combines [`Self::setup_connect`] and
3523- /// [`MidHandshakeSslStream::handshake`].
3524- pub fn connect ( self ) -> Result < SslStream < S > , HandshakeError < S > > {
3525- self . setup_connect ( ) . handshake ( )
3526- }
3527-
35283492 /// Initiates a server-side TLS handshake, returning a [`MidHandshakeSslStream`].
35293493 ///
3530- /// This method calls [`Self::set_accept_state`] and returns without actually
3531- /// initiating the handshake. The caller is then free to call
3532- /// [`MidHandshakeSslStream`] and loop on [`HandshakeError::WouldBlock`].
3494+ /// The caller is then free to call [`MidHandshakeSslStream::handshake`] and retry
3495+ /// on blocking errors.
35333496 pub fn setup_accept ( mut self ) -> MidHandshakeSslStream < S > {
35343497 self . set_accept_state ( ) ;
35353498
@@ -3544,41 +3507,6 @@ where
35443507 } ,
35453508 }
35463509 }
3547-
3548- /// Attempts a server-side TLS handshake.
3549- ///
3550- /// This is a convenience method which combines [`Self::setup_accept`] and
3551- /// [`MidHandshakeSslStream::handshake`].
3552- pub fn accept ( self ) -> Result < SslStream < S > , HandshakeError < S > > {
3553- self . setup_accept ( ) . handshake ( )
3554- }
3555-
3556- /// Initiates the handshake.
3557- ///
3558- /// This will fail if `set_accept_state` or `set_connect_state` was not called first.
3559- ///
3560- /// This corresponds to [`SSL_do_handshake`].
3561- ///
3562- /// [`SSL_do_handshake`]: https://www.openssl.org/docs/manmaster/man3/SSL_do_handshake.html
3563- pub fn handshake ( self ) -> Result < SslStream < S > , HandshakeError < S > > {
3564- let mut stream = self . inner ;
3565- let ret = unsafe { ffi:: SSL_do_handshake ( stream. ssl . as_ptr ( ) ) } ;
3566- if ret > 0 {
3567- Ok ( stream)
3568- } else {
3569- let error = stream. make_error ( ret) ;
3570- match error. would_block ( ) {
3571- true => Err ( HandshakeError :: WouldBlock ( MidHandshakeSslStream {
3572- stream,
3573- error,
3574- } ) ) ,
3575- false => Err ( HandshakeError :: Failure ( MidHandshakeSslStream {
3576- stream,
3577- error,
3578- } ) ) ,
3579- }
3580- }
3581- }
35823510}
35833511
35843512impl < S > SslStreamBuilder < S > {
0 commit comments