summaryrefslogtreecommitdiffstats
path: root/dns-transport/src/tls_stream.rs
blob: be6443acc963ff71752a8cd2fa77398d4828201e (plain)
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
use std::net::TcpStream;
use super::Error;
use super::HttpsTransport;
use super::TlsTransport;

#[cfg(any(feature = "with_nativetls", feature = "with_nativetls_vendored"))]
fn stream_nativetls(domain: &str, port: u16) -> Result<native_tls::TlsStream<TcpStream>, Error> {
    let connector = native_tls::TlsConnector::new()?;
    let stream = TcpStream::connect((domain, port))?;
    Ok(connector.connect(domain, stream)?)
}

#[cfg(feature = "with_rustls")]
fn stream_rustls(domain: &str, port: u16) -> Result<rustls::StreamOwned<rustls::ClientSession,TcpStream>, Error> {
    use std::sync::Arc;

    let mut config = rustls::ClientConfig::new();

    config.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);

    let dns_name = webpki::DNSNameRef::try_from_ascii_str(domain)?;

    let conn = rustls::ClientSession::new(&Arc::new(config), dns_name);

    let sock = TcpStream::connect((domain, port))?;
    let tls = rustls::StreamOwned::new(conn, sock);

    Ok(tls)
}

pub trait TlsStream<S: std::io::Read + std::io::Write> {
    fn stream(domain: &str, port: u16) -> Result<S, Error>;
}

#[cfg(any(feature = "with_tls", feature = "with_https"))]
cfg_if::cfg_if! {
    if #[cfg(any(feature = "with_nativetls", feature = "with_nativetls_vendored"))] {

        impl TlsStream<native_tls::TlsStream<TcpStream>> for HttpsTransport {
            fn stream(domain: &str, port: u16) -> Result<native_tls::TlsStream<TcpStream>, Error> {
                stream_nativetls(domain, port)
            }
        }

        impl TlsStream<native_tls::TlsStream<TcpStream>> for TlsTransport {
            fn stream(domain: &str, port: u16) -> Result<native_tls::TlsStream<TcpStream>, Error> {
                stream_nativetls(domain, port)
            }
        }

    } else if #[cfg(feature = "with_rustls")] {

        impl TlsStream<rustls::StreamOwned<rustls::ClientSession,TcpStream>> for HttpsTransport {
            fn stream(domain: &str, port: u16) -> Result<rustls::StreamOwned<rustls::ClientSession,TcpStream>, Error> {
                stream_rustls(domain, port)
            }
        }

        impl TlsStream<rustls::StreamOwned<rustls::ClientSession,TcpStream>> for TlsTransport {
            fn stream(domain: &str, port: u16) -> Result<rustls::StreamOwned<rustls::ClientSession,TcpStream>, Error> {
                stream_rustls(domain, port)
            }
        }

    } else {
        unreachable!("tls/https enabled but no tls implementation provided")
    }
}