rust: bssl-tls: Expose bssl_sys::SSL handle from `TlsConnection`

Upon user request, out of necessity to interact with other FFI libraries
we could expose a convenient wrapper around transmutes because we do not
want to commit type layouts to a public API contract yet.

Signed-off-by: Xiangfei Ding <xfding@google.com>
Change-Id: Ie9d80a5d6e8b50213648cc1aa075a99d6a6a6964
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/94887
Reviewed-by: Rudolf Polzer <rpolzer@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 e7a26ae..62137b1 100644
--- a/rust/bssl-tls/src/connection.rs
+++ b/rust/bssl-tls/src/connection.rs
@@ -178,6 +178,30 @@
         }
         res
     }
+
+    /// Get a handle of the connection object.
+    ///
+    /// # Safety
+    /// - `self` must outlive all uses of the returned handle;
+    /// - this handle must be used with functions from the BoringSSL library this crate is linked
+    ///   to; otherwise, it is **undefined behaviour**.
+    pub unsafe fn as_mut_ptr(&self) -> *mut bssl_sys::SSL {
+        self.ptr.as_ptr()
+    }
+
+    /// Reconstitute a borrow of the connection object.
+    ///
+    /// # Safety
+    /// - the handle `ptr` must be sourced originally from [`TlsConnection::as_mut_ptr`]
+    ///   of this crate.
+    /// - the handle `*ptr` must not be aliased and accessed across threads until the borrow is
+    ///   expired; otherwise, it is **undefined behaviour**.
+    pub unsafe fn from_mut_ptr(ptr: &mut *mut bssl_sys::SSL) -> &mut Self {
+        unsafe {
+            // Safety: `TlsConnection` is a thin wrapper around `*mut bssl_sys::SSL`
+            core::mem::transmute(ptr)
+        }
+    }
 }
 
 // TODO(@xfding): there seems to be some type inference regression, drop the turbofish when it is