tls_api_openssl/
stream.rs

1use std::fmt;
2use std::io;
3use std::io::Read;
4use std::io::Write;
5use std::marker::PhantomData;
6
7use openssl::ssl::SslRef;
8use openssl::ssl::SslStream;
9use tls_api::async_as_sync::AsyncIoAsSyncIo;
10use tls_api::async_as_sync::AsyncWrapperOps;
11use tls_api::async_as_sync::TlsStreamOverSyncIo;
12use tls_api::async_as_sync::WriteShutdown;
13use tls_api::spi_async_socket_impl_delegate;
14use tls_api::spi_tls_stream_over_sync_io_wrapper;
15use tls_api::AsyncSocket;
16use tls_api::ImplInfo;
17
18spi_tls_stream_over_sync_io_wrapper!(TlsStream, OpenSSLStream);
19
20impl<A: AsyncSocket> TlsStream<A> {
21    /// Get the [`SslRef`] object for the stream.
22    pub fn get_ssl_ref(&self) -> &SslRef {
23        self.0.stream.0.ssl()
24    }
25}
26
27#[derive(Debug)]
28pub(crate) struct AsyncWrapperOpsImpl<S, A>(PhantomData<(S, A)>)
29where
30    S: fmt::Debug + Unpin + Send + 'static,
31    A: AsyncSocket;
32
33impl<S, A> AsyncWrapperOps<A> for AsyncWrapperOpsImpl<S, A>
34where
35    S: fmt::Debug + Unpin + Send + 'static,
36    A: AsyncSocket,
37{
38    type SyncWrapper = OpenSSLStream<AsyncIoAsSyncIo<A>>;
39
40    fn debug(w: &Self::SyncWrapper) -> &dyn fmt::Debug {
41        &w.0
42    }
43
44    fn get_mut(w: &mut Self::SyncWrapper) -> &mut AsyncIoAsSyncIo<A> {
45        w.0.get_mut()
46    }
47
48    fn get_ref(w: &Self::SyncWrapper) -> &AsyncIoAsSyncIo<A> {
49        w.0.get_ref()
50    }
51
52    fn get_alpn_protocol(w: &Self::SyncWrapper) -> anyhow::Result<Option<Vec<u8>>> {
53        Ok(w.0.ssl().selected_alpn_protocol().map(Vec::from))
54    }
55
56    fn impl_info() -> ImplInfo {
57        crate::into()
58    }
59}
60
61pub(crate) struct OpenSSLStream<A: Read + Write>(pub(crate) SslStream<A>);
62
63impl<A: Read + Write> Write for OpenSSLStream<A> {
64    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
65        self.0.write(buf)
66    }
67
68    fn flush(&mut self) -> io::Result<()> {
69        self.0.flush()
70    }
71
72    fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
73        self.0.write_vectored(bufs)
74    }
75
76    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
77        self.0.write_all(buf)
78    }
79
80    fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
81        self.0.write_fmt(fmt)
82    }
83}
84
85impl<A: Read + Write> WriteShutdown for OpenSSLStream<A> {
86    fn shutdown(&mut self) -> Result<(), io::Error> {
87        self.flush()?;
88        self.0.shutdown().map_err(|e| {
89            e.into_io_error()
90                .unwrap_or_else(|e| io::Error::new(io::ErrorKind::Other, e))
91        })?;
92        Ok(())
93    }
94}
95
96impl<A: Read + Write> Read for OpenSSLStream<A> {
97    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
98        self.0.read(buf)
99    }
100
101    fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
102        self.0.read_vectored(bufs)
103    }
104
105    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
106        self.0.read_to_end(buf)
107    }
108
109    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
110        self.0.read_to_string(buf)
111    }
112
113    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
114        self.0.read_exact(buf)
115    }
116}