Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new(), connect(), accept() and handshake() to SslStream #153

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions boring/src/ssl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,23 @@ impl Ssl {
}
}

/// Creates a new `Ssl`.
///
/// This corresponds to [`SSL_new`].
/// This function does the same as [`Self:new()`] except that it takes &[SslContextRef].
// Both functions exist for backward compatibility (no breaking API).
pub fn new_from_ref(ctx: &SslContextRef) -> Result<Ssl, ErrorStack> {
unsafe {
let ptr = cvt_p(ffi::SSL_new(ctx.as_ptr()))?;
let mut ssl = Ssl::from_ptr(ptr);
SSL_CTX_up_ref(ctx.as_ptr());
let ctx_owned = SslContext::from_ptr(ctx.as_ptr());
ssl.set_ex_data(*SESSION_CTX_INDEX, ctx_owned);

Ok(ssl)
}
}

/// Initiates a client-side TLS handshake.
///
/// This corresponds to [`SSL_connect`].
Expand Down Expand Up @@ -3215,6 +3232,15 @@ impl<S: Read + Write> SslStream<S> {
}
}

/// Creates a new `SslStream`.
///
/// This function performs no IO; the stream will not have performed any part of the handshake
/// with the peer. The `connect` and `accept` methods can be used to
/// explicitly perform the handshake.
pub fn new(ssl: Ssl, stream: S) -> Result<Self, ErrorStack> {
Ok(Self::new_base(ssl, stream))
}

/// Constructs an `SslStream` from a pointer to the underlying OpenSSL `SSL` struct.
///
/// This is useful if the handshake has already been completed elsewhere.
Expand Down Expand Up @@ -3319,6 +3345,48 @@ impl<S: Read + Write> SslStream<S> {
pub fn set_shutdown(&mut self, state: ShutdownState) {
unsafe { ffi::SSL_set_shutdown(self.ssl.as_ptr(), state.bits()) }
}

/// Initiates a client-side TLS handshake.
///
/// This corresponds to [`SSL_connect`].
///
/// [`SSL_connect`]: https://www.openssl.org/docs/man1.1.1/man3/SSL_connect.html
pub fn connect(&mut self) -> Result<(), Error> {
let ret = unsafe { ffi::SSL_connect(self.ssl.as_ptr()) };
if ret > 0 {
Ok(())
} else {
Err(self.make_error(ret))
}
}

/// Initiates a server-side TLS handshake.
///
/// This corresponds to [`SSL_accept`].
///
/// [`SSL_accept`]: https://www.openssl.org/docs/man1.1.1/man3/SSL_accept.html
pub fn accept(&mut self) -> Result<(), Error> {
let ret = unsafe { ffi::SSL_accept(self.ssl.as_ptr()) };
if ret > 0 {
Ok(())
} else {
Err(self.make_error(ret))
}
}

/// Initiates the handshake.
///
/// This corresponds to [`SSL_do_handshake`].
///
/// [`SSL_do_handshake`]: https://www.openssl.org/docs/man1.1.1/man3/SSL_do_handshake.html
pub fn do_handshake(&mut self) -> Result<(), Error> {
let ret = unsafe { ffi::SSL_do_handshake(self.ssl.as_ptr()) };
if ret > 0 {
Ok(())
} else {
Err(self.make_error(ret))
}
}
}

impl<S> SslStream<S> {
Expand Down