@@ -2306,34 +2306,52 @@ impl Ssl {
23062306 }
23072307 }
23082308
2309- /// Initiates a client-side TLS handshake.
2309+ /// Initiates a client-side TLS handshake, returning a [`MidHandshakeSslStream`] .
23102310 ///
2311- /// This corresponds to [`SSL_connect`].
2311+ /// This method is guaranteed to return without calling any callback defined
2312+ /// in the internal [`Ssl`] or [`SslContext`].
2313+ ///
2314+ /// See [`SslStreamBuilder::setup_connect`] for more details.
23122315 ///
23132316 /// # Warning
23142317 ///
2315- /// OpenSSL's default configuration is insecure. It is highly recommended to use
2316- /// `SslConnector` rather than `Ssl` directly, as it manages that configuration.
2318+ /// BoringSSL's default configuration is insecure. It is highly recommended to use
2319+ /// [`SslConnector`] rather than [`Ssl`] directly, as it manages that configuration.
2320+ pub fn setup_connect < S > ( self , stream : S ) -> MidHandshakeSslStream < S >
2321+ where
2322+ S : Read + Write ,
2323+ {
2324+ SslStreamBuilder :: new ( self , stream) . setup_connect ( )
2325+ }
2326+
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
23172333 ///
2318- /// [`SSL_connect`]: https://www.openssl.org/docs/manmaster/man3/SSL_connect.html
2334+ /// OpenSSL's default configuration is insecure. It is highly recommended to use
2335+ /// [`SslConnector`] rather than `Ssl` directly, as it manages that configuration.
23192336 pub fn connect < S > ( self , stream : S ) -> Result < SslStream < S > , HandshakeError < S > >
23202337 where
23212338 S : Read + Write ,
23222339 {
2323- SslStreamBuilder :: new ( self , stream) . connect ( )
2340+ self . setup_connect ( stream) . handshake ( )
23242341 }
23252342
23262343 /// Initiates a server-side TLS handshake.
23272344 ///
2328- /// This corresponds to [`SSL_accept`].
2345+ /// This method is guaranteed to return without calling any callback defined
2346+ /// in the internal [`Ssl`] or [`SslContext`].
23292347 ///
2330- /// # Warning
2348+ /// See [`SslStreamBuilder::setup_accept`] for more details.
23312349 ///
2332- /// OpenSSL's default configuration is insecure. It is highly recommended to use
2333- /// `SslAcceptor` rather than `Ssl` directly, as it manages that configuration.
2350+ /// # Warning
23342351 ///
2335- /// [`SSL_accept`]: https://www.openssl.org/docs/manmaster/man3/SSL_accept.html
2336- pub fn accept < S > ( self , stream : S ) -> Result < SslStream < S > , HandshakeError < S > >
2352+ /// BoringSSL's default configuration is insecure. It is highly recommended to use
2353+ /// [`SslAcceptor`] rather than [`Ssl`] directly, as it manages that configuration.
2354+ pub fn setup_accept < S > ( self , stream : S ) -> MidHandshakeSslStream < S >
23372355 where
23382356 S : Read + Write ,
23392357 {
@@ -2352,7 +2370,25 @@ impl Ssl {
23522370 }
23532371 }
23542372
2355- SslStreamBuilder :: new ( self , stream) . accept ( )
2373+ SslStreamBuilder :: new ( self , stream) . setup_accept ( )
2374+ }
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 ( )
23562392 }
23572393}
23582394
@@ -3461,46 +3497,60 @@ where
34613497 unsafe { ffi:: SSL_set_accept_state ( self . inner . ssl . as_ptr ( ) ) }
34623498 }
34633499
3464- /// See `Ssl::connect`
3500+ /// Initiates a client-side TLS handshake, returning a [`MidHandshakeSslStream`].
3501+ ///
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`].
3505+ pub fn setup_connect ( mut self ) -> MidHandshakeSslStream < S > {
3506+ self . set_connect_state ( ) ;
3507+
3508+ MidHandshakeSslStream {
3509+ stream : self . inner ,
3510+ error : Error {
3511+ code : ErrorCode :: WANT_WRITE ,
3512+ cause : Some ( InnerError :: Io ( io:: Error :: new (
3513+ io:: ErrorKind :: WouldBlock ,
3514+ "connect handshake has not started yet" ,
3515+ ) ) ) ,
3516+ } ,
3517+ }
3518+ }
3519+
3520+ /// Attempts a client-side TLS handshake.
3521+ ///
3522+ /// This is a convenience method which combines [`Self::setup_connect`] and
3523+ /// [`MidHandshakeSslStream::handshake`].
34653524 pub fn connect ( self ) -> Result < SslStream < S > , HandshakeError < S > > {
3466- let mut stream = self . inner ;
3467- let ret = unsafe { ffi:: SSL_connect ( stream. ssl . as_ptr ( ) ) } ;
3468- if ret > 0 {
3469- Ok ( stream)
3470- } else {
3471- let error = stream. make_error ( ret) ;
3472- match error. would_block ( ) {
3473- true => Err ( HandshakeError :: WouldBlock ( MidHandshakeSslStream {
3474- stream,
3475- error,
3476- } ) ) ,
3477- false => Err ( HandshakeError :: Failure ( MidHandshakeSslStream {
3478- stream,
3479- error,
3480- } ) ) ,
3481- }
3525+ self . setup_connect ( ) . handshake ( )
3526+ }
3527+
3528+ /// Initiates a server-side TLS handshake, returning a [`MidHandshakeSslStream`].
3529+ ///
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`].
3533+ pub fn setup_accept ( mut self ) -> MidHandshakeSslStream < S > {
3534+ self . set_accept_state ( ) ;
3535+
3536+ MidHandshakeSslStream {
3537+ stream : self . inner ,
3538+ error : Error {
3539+ code : ErrorCode :: WANT_READ ,
3540+ cause : Some ( InnerError :: Io ( io:: Error :: new (
3541+ io:: ErrorKind :: WouldBlock ,
3542+ "accept handshake has not started yet" ,
3543+ ) ) ) ,
3544+ } ,
34823545 }
34833546 }
34843547
3485- /// See `Ssl::accept`
3548+ /// Attempts a server-side TLS handshake.
3549+ ///
3550+ /// This is a convenience method which combines [`Self::setup_accept`] and
3551+ /// [`MidHandshakeSslStream::handshake`].
34863552 pub fn accept ( self ) -> Result < SslStream < S > , HandshakeError < S > > {
3487- let mut stream = self . inner ;
3488- let ret = unsafe { ffi:: SSL_accept ( stream. ssl . as_ptr ( ) ) } ;
3489- if ret > 0 {
3490- Ok ( stream)
3491- } else {
3492- let error = stream. make_error ( ret) ;
3493- match error. would_block ( ) {
3494- true => Err ( HandshakeError :: WouldBlock ( MidHandshakeSslStream {
3495- stream,
3496- error,
3497- } ) ) ,
3498- false => Err ( HandshakeError :: Failure ( MidHandshakeSslStream {
3499- stream,
3500- error,
3501- } ) ) ,
3502- }
3503- }
3553+ self . setup_accept ( ) . handshake ( )
35043554 }
35053555
35063556 /// Initiates the handshake.
0 commit comments