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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright (c) 2019 Reyk Floeter <contact@reykfloeter.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

//! Async [`Tls`] bindings for [`libtls`].
//!
//! See also [`libtls`] for more information.
//!
//! > Note, the API for this crate is neither finished nor documented yet.
//!
//! # Example
//!
//! ```rust
//! # use std::io;
//! # use tokio::io::{AsyncReadExt, AsyncWriteExt};
//! # use tokio_libtls::prelude::*;
//! #
//! async fn async_https_connect(servername: String) -> io::Result<()> {
//!     let request = format!(
//!         "GET / HTTP/1.1\r\n\
//!          Host: {}\r\n\
//!          Connection: close\r\n\r\n",
//!         servername
//!     );
//!
//!     let config = TlsConfigBuilder::new().build()?;
//!     let mut tls = AsyncTls::connect(&(servername + ":443"), &config, None).await?;
//!     tls.write_all(request.as_bytes()).await?;
//!
//!     let mut buf = vec![0u8; 1024];
//!     tls.read_exact(&mut buf).await?;
//!
//!     let ok = b"HTTP/1.1 200 OK\r\n";
//!     assert_eq!(&buf[..ok.len()], ok);
//!
//!     Ok(())
//! }
//! # #[tokio::main]
//! # async fn main() {
//! #    async_https_connect("www.example.com".to_owned()).await.unwrap();
//! # }
//! ```
//!
//! [`Tls`]: https://reyk.github.io/rust-libtls/libtls/tls/struct.Tls.html
//! [`libtls`]: https://reyk.github.io/rust-libtls/libtls

#![doc(
    html_logo_url = "https://www.libressl.org/images/libressl.jpg",
    html_favicon_url = "https://www.libressl.org/favicon.ico"
)]
#![warn(missing_docs)]

/// Error handling.
pub mod error;

/// A "prelude" for crates using the `tokio-libtls` crate.
pub mod prelude;

use error::AsyncTlsError;
use libtls::{config::TlsConfig, error::TlsError, tls::Tls};
use mio::{event::Evented, unix::EventedFd, PollOpt, Ready, Token};
use prelude::*;
use std::{
    io::{self, Read, Write},
    net::ToSocketAddrs,
    ops::{Deref, DerefMut},
    os::unix::io::{AsRawFd, RawFd},
    pin::Pin,
    task::{Context, Poll},
    time::Duration,
};
use tokio::{io::PollEvented, net::TcpStream, time::timeout};

macro_rules! try_async_tls {
    ($call: expr) => {
        match $call {
            Ok(size) => Poll::Ready(Ok(size)),
            Err(err) => {
                let err: io::Error = err.into();
                if err.kind() == io::ErrorKind::WouldBlock {
                    Poll::Pending
                } else {
                    Poll::Ready(Err(err))
                }
            }
        }
    };
}

/// Wrapper for async I/O operations with `Tls`.
#[derive(Debug)]
pub struct TlsStream {
    tls: Tls,
    tcp: TcpStream,
}

impl TlsStream {
    /// Create new `TlsStream` from `Tls` object and `TcpStream`.
    pub fn new(tls: Tls, tcp: TcpStream) -> Self {
        Self { tls, tcp }
    }
}

impl Deref for TlsStream {
    type Target = Tls;

    fn deref(&self) -> &Self::Target {
        &self.tls
    }
}

impl DerefMut for TlsStream {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.tls
    }
}

impl AsRawFd for TlsStream {
    fn as_raw_fd(&self) -> RawFd {
        self.tcp.as_raw_fd()
    }
}

impl io::Read for TlsStream {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.tls.read(buf)
    }
}

impl io::Write for TlsStream {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.tls.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.tls.flush()
    }
}

impl AsyncRead for TlsStream {
    fn poll_read(
        mut self: Pin<&mut Self>,
        _cx: &mut Context,
        buf: &mut [u8],
    ) -> Poll<Result<usize, io::Error>> {
        try_async_tls!(self.tls.read(buf))
    }
}

impl AsyncWrite for TlsStream {
    fn poll_write(
        mut self: Pin<&mut Self>,
        _cx: &mut Context,
        buf: &[u8],
    ) -> Poll<Result<usize, io::Error>> {
        try_async_tls!(self.tls.write(buf))
    }

    fn poll_flush(mut self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Result<(), io::Error>> {
        try_async_tls!(self.tls.close()).map(|_| Ok(()))
    }

    fn poll_close(mut self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Result<(), io::Error>> {
        try_async_tls!(self.tls.close()).map(|_| Ok(()))
    }
}

impl Evented for TlsStream {
    fn register(
        &self,
        poll: &mio::Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        match EventedFd(&self.as_raw_fd()).register(poll, token, interest, opts) {
            Err(ref err) if err.kind() == io::ErrorKind::AlreadyExists => {
                self.reregister(poll, token, interest, opts)
            }
            Err(err) => Err(err),
            Ok(_) => Ok(()),
        }
    }

    fn reregister(
        &self,
        poll: &mio::Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        EventedFd(&self.as_raw_fd()).reregister(poll, token, interest, opts)
    }

    fn deregister(&self, poll: &mio::Poll) -> io::Result<()> {
        EventedFd(&self.as_raw_fd()).deregister(poll)
    }
}

/// Pollable wrapper for async I/O operations with `Tls`.
pub type AsyncTlsStream = PollEvented<TlsStream>;

/// Async `Tls` struct.
pub struct AsyncTls {
    inner: Option<Result<AsyncTlsStream, AsyncTlsError>>,
}

impl AsyncTls {
    /// Accept a new async `Tls` connection.
    pub async fn accept_stream(
        tcp: TcpStream,
        config: &TlsConfig,
        options: Option<AsyncTlsOptions>,
    ) -> io::Result<AsyncTlsStream> {
        let options = options.unwrap_or_else(|| AsyncTlsOptions::new());

        let mut tls = Tls::server()?;
        tls.configure(config)?;
        tls.accept_raw_fd(&tcp)?;

        let async_tls = TlsStream::new(tls, tcp);
        let stream = PollEvented::new(async_tls)?;
        let fut = Self {
            inner: Some(Err(AsyncTlsError::Readable(stream))),
        };

        // Accept with an optional timeout for the TLS handshake.
        let tls = match options.timeout {
            Some(tm) => match timeout(tm, fut).await {
                Ok(res) => res,
                Err(err) => Err(err.into()),
            },
            None => fut.await,
        }?;

        Ok(tls)
    }

    /// Upgrade a TCP stream to a new async `Tls` connection.
    pub async fn connect_stream(
        tcp: TcpStream,
        config: &TlsConfig,
        options: Option<AsyncTlsOptions>,
    ) -> io::Result<AsyncTlsStream> {
        let options = options.unwrap_or_else(|| AsyncTlsOptions::new());
        let servername = match options.servername {
            Some(name) => name,
            None => tcp.peer_addr()?.to_string(),
        };

        let mut tls = Tls::client()?;

        tls.configure(config)?;
        tls.connect_raw_fd(&tcp, &servername)?;

        let async_tls = TlsStream::new(tls, tcp);
        let stream = PollEvented::new(async_tls)?;
        let fut = Self {
            inner: Some(Err(AsyncTlsError::Readable(stream))),
        };

        // Connect with an optional timeout for the TLS handshake.
        let tls = match options.timeout {
            Some(tm) => match timeout(tm, fut).await {
                Ok(res) => res,
                Err(err) => Err(err.into()),
            },
            None => fut.await,
        }?;

        Ok(tls)
    }

    /// Connect a new async `Tls` connection.
    pub async fn connect(
        host: &str,
        config: &TlsConfig,
        options: Option<AsyncTlsOptions>,
    ) -> io::Result<AsyncTlsStream> {
        let mut options = options.unwrap_or_else(|| AsyncTlsOptions::new());

        // Remove _last_ colon (to satisfy the IPv6 form, e.g. [::1]::443).
        if let None = options.servername {
            match host.rfind(':') {
                None => return Err(io::ErrorKind::InvalidInput.into()),
                Some(index) => options.servername(&host[0..index]),
            };
        };

        let mut last_error = io::ErrorKind::ConnectionRefused.into();

        for addr in host.to_socket_addrs()? {
            // Connect with an optional timeout.
            let res = match options.timeout {
                Some(tm) => match timeout(tm, TcpStream::connect(&addr)).await {
                    Ok(res) => res,
                    Err(err) => Err(err.into()),
                },
                None => TcpStream::connect(&addr).await,
            };

            // Return the first TCP successful connection, store the last error.
            match res {
                Ok(tcp) => {
                    return Self::connect_stream(tcp, config, Some(options)).await;
                }
                Err(err) => last_error = err,
            }
        }

        Err(last_error)
    }
}

impl Future for AsyncTls {
    type Output = Result<AsyncTlsStream, io::Error>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        let inner = self
            .inner
            .take()
            .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "cannot take inner"))?;
        match inner {
            Ok(tls) => {
                cx.waker().wake_by_ref();
                Poll::Ready(Ok(tls))
            }
            Err(AsyncTlsError::Readable(stream)) => {
                self.inner = match stream.poll_read_ready(cx, Ready::readable()) {
                    Poll::Ready(_) => Some(Err(AsyncTlsError::Handshake(stream))),
                    _ => Some(Err(AsyncTlsError::Handshake(stream))),
                };
                cx.waker().wake_by_ref();
                Poll::Pending
            }
            Err(AsyncTlsError::Writeable(stream)) => {
                self.inner = match stream.poll_write_ready(cx) {
                    Poll::Ready(_) => Some(Err(AsyncTlsError::Handshake(stream))),
                    _ => Some(Err(AsyncTlsError::Writeable(stream))),
                };
                cx.waker().wake_by_ref();
                Poll::Pending
            }
            Err(AsyncTlsError::Handshake(mut stream)) => {
                let tls = &mut *stream.get_mut();
                let res = match tls.tls_handshake() {
                    Ok(res) => {
                        if res == libtls::TLS_WANT_POLLIN as isize {
                            Err(AsyncTlsError::Readable(stream))
                        } else if res == libtls::TLS_WANT_POLLOUT as isize {
                            Err(AsyncTlsError::Writeable(stream))
                        } else {
                            Ok(stream)
                        }
                    }
                    Err(err) => Err(err.into()),
                };
                self.inner = Some(res);
                cx.waker().wake_by_ref();
                Poll::Pending
            }
            Err(AsyncTlsError::Error(TlsError::IoError(err))) => Poll::Ready(Err(err)),
            Err(AsyncTlsError::Error(err)) => {
                Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, err.to_string())))
            }
        }
    }
}

/// Configuration options for `AsyncTls`.
///
/// # See also
///
/// [`AsyncTls`]
///
/// [`AsyncTls`]: ./struct.AsyncTls.html
#[derive(Clone, Default, Debug, PartialEq)]
pub struct AsyncTlsOptions {
    timeout: Option<Duration>,
    servername: Option<String>,
}

impl AsyncTlsOptions {
    /// Return new empty `AsyncTlsOptions` struct.
    pub fn new() -> Self {
        Self {
            ..Default::default()
        }
    }

    /// Set the optional TCP connection and TLS handshake timeout.
    pub fn timeout(&'_ mut self, timeout: Duration) -> &'_ mut Self {
        self.timeout = Some(timeout);
        self
    }

    /// Set the optional TLS servername.
    ///
    /// If not specified, the address is derived from the host or address.
    pub fn servername(&'_ mut self, servername: &str) -> &'_ mut Self {
        self.servername = Some(servername.to_owned());
        self
    }

    /// Return as `Some(AsyncTlsOptions)` or `None` if the options are empty.
    pub fn build(&'_ mut self) -> Option<Self> {
        if self == &mut Self::new() {
            None
        } else {
            Some(self.clone())
        }
    }
}

#[cfg(test)]
mod test {
    use crate::prelude::*;
    use std::{io, time::Duration};
    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    async fn async_https_connect(servername: String) -> io::Result<()> {
        let request = format!(
            "GET / HTTP/1.1\r\n\
             Host: {}\r\n\
             Connection: close\r\n\r\n",
            servername
        );

        let config = TlsConfigBuilder::new().build()?;
        let options = AsyncTlsOptions::new()
            .servername(&servername)
            .timeout(Duration::from_secs(60))
            .build();
        let mut tls = AsyncTls::connect(&(servername + ":443"), &config, options).await?;
        tls.write_all(request.as_bytes()).await?;

        let mut buf = vec![0u8; 1024];
        tls.read_exact(&mut buf).await?;

        let ok = b"HTTP/1.1 200 OK\r\n";
        assert_eq!(&buf[..ok.len()], ok);

        Ok(())
    }

    #[tokio::test]
    async fn test_async_https_connect() {
        async_https_connect("www.example.com".to_owned())
            .await
            .unwrap();
    }
}