Skip to content

Commit a3cdf87

Browse files
eaufavornox
authored andcommitted
Add new(), connect(), accept() and handshake() to SslStream
These APIs allow more SslStream to be used more flexibly
1 parent 6057ab7 commit a3cdf87

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

boring/src/ssl/mod.rs

+68
Original file line numberDiff line numberDiff line change
@@ -2320,6 +2320,23 @@ impl Ssl {
23202320
}
23212321
}
23222322

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+
23232340
/// Initiates a client-side TLS handshake.
23242341
///
23252342
/// This corresponds to [`SSL_connect`].
@@ -3278,6 +3295,15 @@ impl<S: Read + Write> SslStream<S> {
32783295
}
32793296
}
32803297

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+
32813307
/// Constructs an `SslStream` from a pointer to the underlying OpenSSL `SSL` struct.
32823308
///
32833309
/// This is useful if the handshake has already been completed elsewhere.
@@ -3382,6 +3408,48 @@ impl<S: Read + Write> SslStream<S> {
33823408
pub fn set_shutdown(&mut self, state: ShutdownState) {
33833409
unsafe { ffi::SSL_set_shutdown(self.ssl.as_ptr(), state.bits()) }
33843410
}
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+
}
33853453
}
33863454

33873455
impl<S> SslStream<S> {

0 commit comments

Comments
 (0)