rust: bssl-tls: Remove TlsConnectionRef Since we are no longer carrying certificate cache in the connection object, `TlsConnection` can be made into a "thin wrapper" again. In the future design, we will park any additional data to an ex_data slot. The thin wrapper design will stay. Signed-off-by: Xiangfei Ding <xfding@google.com> Change-Id: I2da226e367f37f11f3a17ad2e91508646a6a6964 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/94667 Reviewed-by: Adam Langley <agl@google.com> Presubmit-BoringSSL-Verified: boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/rust/bssl-tls/src/connection.rs b/rust/bssl-tls/src/connection.rs index ce066c7..0dbb7c9 100644 --- a/rust/bssl-tls/src/connection.rs +++ b/rust/bssl-tls/src/connection.rs
@@ -18,14 +18,7 @@ use core::{ ffi::c_int, marker::PhantomData, - mem::{ - forget, - transmute, // - }, - ops::{ - Deref, - DerefMut, // - }, + mem::forget, ptr::NonNull, task::Waker, // }; @@ -140,6 +133,7 @@ /// `Mode` is expected to be either [`TlsMode`] or [`QuicMode`]. /// These generics will govern the capabilities that respective TLS connection role can access, // NOTE: any method that involves I/O must require exclusive access, enforced by requiring `&mut`. +#[repr(transparent)] pub struct TlsConnection<Role, Mode = TlsMode> { ptr: NonNull<bssl_sys::SSL>, _p: PhantomData<fn() -> (Role, Mode)>, @@ -158,35 +152,7 @@ } } -/// Tls Connection Reference -// NOTE: Do not expose access to this type by-value! -#[repr(transparent)] -pub struct TlsConnectionRef<R, M = TlsMode>(NonNull<bssl_sys::SSL>, PhantomData<fn() -> (R, M)>); - -impl<R, M> Deref for TlsConnection<R, M> { - type Target = TlsConnectionRef<R, M>; - fn deref(&self) -> &Self::Target { - unsafe { - // Safety: `TlsConnectionRef` is a transparent wrapper around `NonNull<bssl_sys::SSL>`. - transmute(&self.ptr) - } - } -} - -impl<R, M> DerefMut for TlsConnection<R, M> { - fn deref_mut(&mut self) -> &mut Self::Target { - unsafe { - // Safety: `TlsConnectionRef` is a transparent wrapper around `NonNull<bssl_sys::SSL>`. - transmute(&mut self.ptr) - } - } -} - -/// Safety: all internal states of `bssl_sys::SSL` that this connection are either exclusively owned -/// or reference-counted but immutable, including `SSL_SESSION`s and `SSL_CTX`s. -unsafe impl<R, M> Send for TlsConnectionRef<R, M> {} - -impl<R, M> TlsConnectionRef<R, M> { +impl<R, M> TlsConnection<R, M> { /// Call this method whenever I/O is performed on the connection. pub(crate) fn categorise_error_for_io(&self, rc: c_int) -> Result<IoStatus, Error> { let reason = unsafe { @@ -208,7 +174,7 @@ // TODO(@xfding): there seems to be some type inference regression, drop the turbofish when it is // resolved. -impl<R, M> TlsConnectionRef<R, M> +impl<R, M> TlsConnection<R, M> where M: methods::HasTlsConnectionMethod, { @@ -226,7 +192,7 @@ } } -impl<R, M> TlsConnectionRef<R, M> +impl<R, M> TlsConnection<R, M> where M: methods::HasTlsConnectionMethod, { @@ -236,7 +202,7 @@ // Safety: // - `self.ptr()` is a valid `SSL` handle created from `from_ssl` in this crate. // - The data was previously initialized as `Box<Option<Waker>>`. - waker_data_ref_from_ssl(self.0) + waker_data_ref_from_ssl(self.ptr) }; if let Some(other_waker) = waker_data && waker.will_wake(other_waker) @@ -288,9 +254,9 @@ } } -impl<R, M> TlsConnectionRef<R, M> { +impl<R, M> TlsConnection<R, M> { pub(crate) fn ptr(&self) -> *mut bssl_sys::SSL { - self.0.as_ptr() + self.ptr.as_ptr() } /// Check if the connection is DTLS.
diff --git a/rust/bssl-tls/src/connection/io.rs b/rust/bssl-tls/src/connection/io.rs index b81135f..b156cab 100644 --- a/rust/bssl-tls/src/connection/io.rs +++ b/rust/bssl-tls/src/connection/io.rs
@@ -28,6 +28,7 @@ use crate::{ ReceiveBuffer, connection::{ + TlsConnection, lifecycle::ShutdownStatus, methods::HasTlsConnectionMethod, // }, @@ -44,9 +45,7 @@ io::IoStatus, // }; -use super::TlsConnectionRef; - -impl<R, M> TlsConnectionRef<R, M> +impl<R, M> TlsConnection<R, M> where M: HasTlsConnectionMethod, { @@ -185,7 +184,7 @@ } /// Async I/O -impl<R, M> TlsConnectionRef<R, M> +impl<R, M> TlsConnection<R, M> where M: HasTlsConnectionMethod, { @@ -200,14 +199,14 @@ } } -impl<R, M> TlsConnectionRef<R, M> +impl<R, M> TlsConnection<R, M> where M: HasTlsConnectionMethod + HasBasicIo, { fn do_async_io( mut self: Pin<&mut Self>, cx: &mut Context<'_>, - sync_op: impl FnOnce(&mut TlsConnectionRef<R, M>) -> Result<IoStatus, Error>, + sync_op: impl FnOnce(&mut TlsConnection<R, M>) -> Result<IoStatus, Error>, ) -> Result<Option<IoStatus>, Error> { self.set_waker(cx.waker());
diff --git a/rust/bssl-tls/src/connection/io/stdio.rs b/rust/bssl-tls/src/connection/io/stdio.rs index 2a67962..95cde2d 100644 --- a/rust/bssl-tls/src/connection/io/stdio.rs +++ b/rust/bssl-tls/src/connection/io/stdio.rs
@@ -16,11 +16,11 @@ use super::{ Error, - TlsConnectionRef, TlsMode, // }; use crate::{ ReceiveBuffer, + connection::TlsConnection, context::DtlsMode, errors::{ IoError, @@ -62,7 +62,7 @@ } } -impl<R> io::Read for TlsConnectionRef<R, TlsMode> { +impl<R> io::Read for TlsConnection<R, TlsMode> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { let mut buf = ReceiveBuffer::new(buf); let res = self.sync_read(&mut buf); @@ -70,7 +70,7 @@ } } -impl<R> io::Write for TlsConnectionRef<R, TlsMode> { +impl<R> io::Write for TlsConnection<R, TlsMode> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { translate_res_for_stdio(self.sync_write(buf)) } @@ -95,7 +95,7 @@ } } -impl<R> DatagramSocket for TlsConnectionRef<R, DtlsMode> { +impl<R> DatagramSocket for TlsConnection<R, DtlsMode> { fn send(&mut self, datagram: &[u8]) -> AbstractSocketResult { translate_result_for_datagram(self.sync_write(datagram)) }
diff --git a/rust/bssl-tls/src/connection/lifecycle.rs b/rust/bssl-tls/src/connection/lifecycle.rs index 3b072f8..71e9e48 100644 --- a/rust/bssl-tls/src/connection/lifecycle.rs +++ b/rust/bssl-tls/src/connection/lifecycle.rs
@@ -33,7 +33,7 @@ connection::{ Client, Server, - TlsConnectionRef, + TlsConnection, methods::HasTlsConnectionMethod, // }, context::{ @@ -50,7 +50,7 @@ }; /// # Connection shutdown -impl<R, M> TlsConnectionRef<R, M> { +impl<R, M> TlsConnection<R, M> { /// Set whether shutting down this connection sends out a `close_notify` alert. pub fn set_quiet_shutdown(&mut self, quiet: bool) -> &mut Self { unsafe { @@ -77,7 +77,7 @@ /// /// Please refer to [`EstablishedTlsConnection`] and [`TlsConnectionInHandshake`] for allowed /// operations. -impl<R, M> TlsConnectionRef<R, M> { +impl<R, M> TlsConnection<R, M> { /// Access handshake-related options if the connection is in handshake mode. pub fn in_handshake<'a>(&'a mut self) -> Option<TlsConnectionInHandshake<'a, R, M>> { if self.is_in_handshake() { @@ -113,9 +113,9 @@ /// # Connection state /// -/// When operations on [`TlsConnectionRef`] return with pending status, +/// When operations on [`TlsConnection`] return with pending status, /// there will be reasons why the operations should be retried. -impl<R, M> TlsConnectionRef<R, M> { +impl<R, M> TlsConnection<R, M> { /// Check the connection if it needs additional data. pub fn wants_data(&self) -> Option<TlsPendingData> { let code = unsafe { @@ -128,7 +128,7 @@ } /// # Alerts -impl<R, M> TlsConnectionRef<R, M> +impl<R, M> TlsConnection<R, M> where M: HasTlsConnectionMethod, { @@ -162,7 +162,7 @@ } } -impl<R> TlsConnectionRef<R, TlsMode> { +impl<R> TlsConnection<R, TlsMode> { /// Inspect if the connection is suspended for which reason, after invocation of I/O methods. pub fn take_pending_reason(&mut self) -> Option<TlsRetryReason> { let methods = self.get_connection_methods(); @@ -172,10 +172,10 @@ /// A handle to the connection that is valid only during handshake. #[repr(transparent)] -pub struct TlsConnectionInHandshake<'a, R, M>(pub(crate) &'a mut TlsConnectionRef<R, M>); +pub struct TlsConnectionInHandshake<'a, R, M>(pub(crate) &'a mut TlsConnection<R, M>); impl<R, M> Deref for TlsConnectionInHandshake<'_, R, M> { - type Target = TlsConnectionRef<R, M>; + type Target = TlsConnection<R, M>; fn deref(&self) -> &Self::Target { &*self.0 } @@ -237,10 +237,10 @@ /// A handle to the connection that is valid only after initialization, or in other words after /// handshake. #[repr(transparent)] -pub struct EstablishedTlsConnection<'a, R, M = TlsMode>(&'a mut TlsConnectionRef<R, M>); +pub struct EstablishedTlsConnection<'a, R, M = TlsMode>(&'a mut TlsConnection<R, M>); impl<R, M> Deref for EstablishedTlsConnection<'_, R, M> { - type Target = TlsConnectionRef<R, M>; + type Target = TlsConnection<R, M>; fn deref(&self) -> &Self::Target { &*self.0 }
diff --git a/rust/bssl-tls/src/connection/transport.rs b/rust/bssl-tls/src/connection/transport.rs index 35826c8..71abcb5 100644 --- a/rust/bssl-tls/src/connection/transport.rs +++ b/rust/bssl-tls/src/connection/transport.rs
@@ -22,8 +22,8 @@ check_tls_error, config::ConfigurationError, connection::{ + TlsConnection, TlsConnectionBuilder, - TlsConnectionRef, methods::HasTlsConnectionMethod, // }, context::HasBasicIo, @@ -39,7 +39,7 @@ /// # Transport configurations /// /// These are the methods to configure the underlying IO drivers and transport configurations. -impl<R, M> TlsConnectionRef<R, M> +impl<R, M> TlsConnection<R, M> where M: HasBasicIo + HasTlsConnectionMethod, {