@@ -2320,6 +2320,23 @@ impl Ssl {
2320
2320
}
2321
2321
}
2322
2322
2323
+ /// Creates a new `Ssl`.
2324
+ ///
2325
+ /// This corresponds to [`SSL_new`].
2326
+ /// This function does the same as [`Self:new()`] except that it takes &[SslContextRef].
2327
+ // Both functions exist for backward compatibility (no breaking API).
2328
+ pub fn new_from_ref ( ctx : & SslContextRef ) -> Result < Ssl , ErrorStack > {
2329
+ unsafe {
2330
+ let ptr = cvt_p ( ffi:: SSL_new ( ctx. as_ptr ( ) ) ) ?;
2331
+ let mut ssl = Ssl :: from_ptr ( ptr) ;
2332
+ SSL_CTX_up_ref ( ctx. as_ptr ( ) ) ;
2333
+ let ctx_owned = SslContext :: from_ptr ( ctx. as_ptr ( ) ) ;
2334
+ ssl. set_ex_data ( * SESSION_CTX_INDEX , ctx_owned) ;
2335
+
2336
+ Ok ( ssl)
2337
+ }
2338
+ }
2339
+
2323
2340
/// Initiates a client-side TLS handshake.
2324
2341
///
2325
2342
/// This corresponds to [`SSL_connect`].
@@ -3278,6 +3295,15 @@ impl<S: Read + Write> SslStream<S> {
3278
3295
}
3279
3296
}
3280
3297
3298
+ /// Creates a new `SslStream`.
3299
+ ///
3300
+ /// This function performs no IO; the stream will not have performed any part of the handshake
3301
+ /// with the peer. The `connect` and `accept` methods can be used to
3302
+ /// explicitly perform the handshake.
3303
+ pub fn new ( ssl : Ssl , stream : S ) -> Result < Self , ErrorStack > {
3304
+ Ok ( Self :: new_base ( ssl, stream) )
3305
+ }
3306
+
3281
3307
/// Constructs an `SslStream` from a pointer to the underlying OpenSSL `SSL` struct.
3282
3308
///
3283
3309
/// This is useful if the handshake has already been completed elsewhere.
@@ -3382,6 +3408,48 @@ impl<S: Read + Write> SslStream<S> {
3382
3408
pub fn set_shutdown ( & mut self , state : ShutdownState ) {
3383
3409
unsafe { ffi:: SSL_set_shutdown ( self . ssl . as_ptr ( ) , state. bits ( ) ) }
3384
3410
}
3411
+
3412
+ /// Initiates a client-side TLS handshake.
3413
+ ///
3414
+ /// This corresponds to [`SSL_connect`].
3415
+ ///
3416
+ /// [`SSL_connect`]: https://www.openssl.org/docs/man1.1.1/man3/SSL_connect.html
3417
+ pub fn connect ( & mut self ) -> Result < ( ) , Error > {
3418
+ let ret = unsafe { ffi:: SSL_connect ( self . ssl . as_ptr ( ) ) } ;
3419
+ if ret > 0 {
3420
+ Ok ( ( ) )
3421
+ } else {
3422
+ Err ( self . make_error ( ret) )
3423
+ }
3424
+ }
3425
+
3426
+ /// Initiates a server-side TLS handshake.
3427
+ ///
3428
+ /// This corresponds to [`SSL_accept`].
3429
+ ///
3430
+ /// [`SSL_accept`]: https://www.openssl.org/docs/man1.1.1/man3/SSL_accept.html
3431
+ pub fn accept ( & mut self ) -> Result < ( ) , Error > {
3432
+ let ret = unsafe { ffi:: SSL_accept ( self . ssl . as_ptr ( ) ) } ;
3433
+ if ret > 0 {
3434
+ Ok ( ( ) )
3435
+ } else {
3436
+ Err ( self . make_error ( ret) )
3437
+ }
3438
+ }
3439
+
3440
+ /// Initiates the handshake.
3441
+ ///
3442
+ /// This corresponds to [`SSL_do_handshake`].
3443
+ ///
3444
+ /// [`SSL_do_handshake`]: https://www.openssl.org/docs/man1.1.1/man3/SSL_do_handshake.html
3445
+ pub fn do_handshake ( & mut self ) -> Result < ( ) , Error > {
3446
+ let ret = unsafe { ffi:: SSL_do_handshake ( self . ssl . as_ptr ( ) ) } ;
3447
+ if ret > 0 {
3448
+ Ok ( ( ) )
3449
+ } else {
3450
+ Err ( self . make_error ( ret) )
3451
+ }
3452
+ }
3385
3453
}
3386
3454
3387
3455
impl < S > SslStream < S > {
0 commit comments