1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//! A trait to represent a stream

use crate::transport::smtp::{client::mock::MockStream, error::Error};
#[cfg(feature = "native-tls")]
use native_tls::{TlsConnector, TlsStream};
#[cfg(feature = "rustls-tls")]
use rustls::{ClientConfig, ClientSession};
#[cfg(feature = "native-tls")]
use std::io::ErrorKind;
#[cfg(feature = "rustls-tls")]
use std::sync::Arc;
use std::{
    io::{self, Read, Write},
    net::{Ipv4Addr, Shutdown, SocketAddr, SocketAddrV4, TcpStream, ToSocketAddrs},
    time::Duration,
};

/// Parameters to use for secure clients
#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub struct TlsParameters {
    /// A connector from `native-tls`
    #[cfg(feature = "native-tls")]
    connector: TlsConnector,
    /// A client from `rustls`
    #[cfg(feature = "rustls-tls")]
    // TODO use the same in all transports of the client
    connector: Box<ClientConfig>,
    /// The domain name which is expected in the TLS certificate from the server
    domain: String,
}

impl TlsParameters {
    /// Creates a `TlsParameters`
    #[cfg(feature = "native-tls")]
    pub fn new(domain: String, connector: TlsConnector) -> Self {
        Self { connector, domain }
    }

    /// Creates a `TlsParameters`
    #[cfg(feature = "rustls-tls")]
    pub fn new(domain: String, connector: ClientConfig) -> Self {
        Self {
            connector: Box::new(connector),
            domain,
        }
    }
}

/// Represents the different types of underlying network streams
pub enum NetworkStream {
    /// Plain TCP stream
    Tcp(TcpStream),
    /// Encrypted TCP stream
    #[cfg(feature = "native-tls")]
    Tls(Box<TlsStream<TcpStream>>),
    #[cfg(feature = "rustls-tls")]
    Tls(Box<rustls::StreamOwned<ClientSession, TcpStream>>),
    /// Mock stream
    Mock(MockStream),
}

impl NetworkStream {
    /// Returns peer's address
    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
        match *self {
            NetworkStream::Tcp(ref s) => s.peer_addr(),
            #[cfg(feature = "native-tls")]
            NetworkStream::Tls(ref s) => s.get_ref().peer_addr(),
            #[cfg(feature = "rustls-tls")]
            NetworkStream::Tls(ref s) => s.get_ref().peer_addr(),
            NetworkStream::Mock(_) => Ok(SocketAddr::V4(SocketAddrV4::new(
                Ipv4Addr::new(127, 0, 0, 1),
                80,
            ))),
        }
    }

    /// Shutdowns the connection
    pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
        match *self {
            NetworkStream::Tcp(ref s) => s.shutdown(how),
            #[cfg(feature = "native-tls")]
            NetworkStream::Tls(ref s) => s.get_ref().shutdown(how),
            #[cfg(feature = "rustls-tls")]
            NetworkStream::Tls(ref s) => s.get_ref().shutdown(how),
            NetworkStream::Mock(_) => Ok(()),
        }
    }

    pub fn connect<T: ToSocketAddrs>(
        server: T,
        timeout: Option<Duration>,
        tls_parameters: Option<&TlsParameters>,
    ) -> Result<NetworkStream, Error> {
        fn try_connect_timeout<T: ToSocketAddrs>(
            server: T,
            timeout: Duration,
        ) -> Result<TcpStream, Error> {
            let addrs = server.to_socket_addrs()?;
            for addr in addrs {
                let result = TcpStream::connect_timeout(&addr, timeout);
                if result.is_ok() {
                    return result.map_err(|e| e.into());
                }
            }
            Err(Error::Client("Could not connect"))
        }

        let tcp_stream = match timeout {
            Some(t) => try_connect_timeout(server, t)?,
            None => TcpStream::connect(server)?,
        };

        match tls_parameters {
            #[cfg(feature = "native-tls")]
            Some(context) => context
                .connector
                .connect(context.domain.as_ref(), tcp_stream)
                .map(|tls| NetworkStream::Tls(Box::new(tls)))
                .map_err(|e| Error::Io(io::Error::new(ErrorKind::Other, e))),
            #[cfg(feature = "rustls-tls")]
            Some(context) => {
                let domain = webpki::DNSNameRef::try_from_ascii_str(&context.domain)?;

                Ok(NetworkStream::Tls(Box::new(rustls::StreamOwned::new(
                    ClientSession::new(&Arc::new(*context.connector.clone()), domain),
                    tcp_stream,
                ))))
            }
            #[cfg(not(any(feature = "native-tls", feature = "rustls-tls")))]
            Some(_) => panic!("TLS configuration without support"),
            None => Ok(NetworkStream::Tcp(tcp_stream)),
        }
    }

    #[allow(unused_variables, unreachable_code)]
    pub fn upgrade_tls(&mut self, tls_parameters: &TlsParameters) -> Result<(), Error> {
        *self = match *self {
            #[cfg(feature = "native-tls")]
            NetworkStream::Tcp(ref mut stream) => match tls_parameters
                .connector
                .connect(tls_parameters.domain.as_ref(), stream.try_clone().unwrap())
            {
                Ok(tls_stream) => NetworkStream::Tls(Box::new(tls_stream)),
                Err(err) => return Err(Error::Io(io::Error::new(ErrorKind::Other, err))),
            },
            #[cfg(feature = "rustls-tls")]
            NetworkStream::Tcp(ref mut stream) => {
                let domain = webpki::DNSNameRef::try_from_ascii_str(&tls_parameters.domain)?;

                NetworkStream::Tls(Box::new(rustls::StreamOwned::new(
                    ClientSession::new(&Arc::new(*tls_parameters.connector.clone()), domain),
                    stream.try_clone().unwrap(),
                )))
            }
            #[cfg(not(any(feature = "native-tls", feature = "rustls-tls")))]
            NetworkStream::Tcp(_) => panic!("STARTTLS without TLS support"),
            #[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
            NetworkStream::Tls(_) => return Ok(()),
            NetworkStream::Mock(_) => return Ok(()),
        };

        Ok(())
    }

    pub fn is_encrypted(&self) -> bool {
        match *self {
            NetworkStream::Tcp(_) | NetworkStream::Mock(_) => false,
            #[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
            NetworkStream::Tls(_) => true,
        }
    }

    pub fn set_read_timeout(&mut self, duration: Option<Duration>) -> io::Result<()> {
        match *self {
            NetworkStream::Tcp(ref mut stream) => stream.set_read_timeout(duration),
            #[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
            NetworkStream::Tls(ref mut stream) => stream.get_ref().set_read_timeout(duration),
            NetworkStream::Mock(_) => Ok(()),
        }
    }

    /// Set write timeout for IO calls
    pub fn set_write_timeout(&mut self, duration: Option<Duration>) -> io::Result<()> {
        match *self {
            NetworkStream::Tcp(ref mut stream) => stream.set_write_timeout(duration),
            #[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
            NetworkStream::Tls(ref mut stream) => stream.get_ref().set_write_timeout(duration),
            NetworkStream::Mock(_) => Ok(()),
        }
    }
}

impl Read for NetworkStream {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        match *self {
            NetworkStream::Tcp(ref mut s) => s.read(buf),
            #[cfg(feature = "native-tls")]
            NetworkStream::Tls(ref mut s) => s.read(buf),
            #[cfg(feature = "rustls-tls")]
            NetworkStream::Tls(ref mut s) => s.read(buf),
            NetworkStream::Mock(ref mut s) => s.read(buf),
        }
    }
}

impl Write for NetworkStream {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        match *self {
            NetworkStream::Tcp(ref mut s) => s.write(buf),
            #[cfg(feature = "native-tls")]
            NetworkStream::Tls(ref mut s) => s.write(buf),
            #[cfg(feature = "rustls-tls")]
            NetworkStream::Tls(ref mut s) => s.write(buf),
            NetworkStream::Mock(ref mut s) => s.write(buf),
        }
    }

    fn flush(&mut self) -> io::Result<()> {
        match *self {
            NetworkStream::Tcp(ref mut s) => s.flush(),
            #[cfg(feature = "native-tls")]
            NetworkStream::Tls(ref mut s) => s.flush(),
            #[cfg(feature = "rustls-tls")]
            NetworkStream::Tls(ref mut s) => s.flush(),
            NetworkStream::Mock(ref mut s) => s.flush(),
        }
    }
}