web3_async_native_tls/lib.rs
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
#![warn(
missing_debug_implementations,
missing_docs,
rust_2018_idioms,
unreachable_pub
)]
//! Async TLS streams
//!
//! # Examples
//!
//! To connect as a client to a remote server:
//!
//! ```rust
//! # #[cfg(feature = "runtime-async-std")]
//! # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { async_std::task::block_on(async {
//! #
//! use async_std::prelude::*;
//! use async_std::net::TcpStream;
//!
//! let stream = TcpStream::connect("google.com:443").await?;
//! let mut stream = async_native_tls::connect("google.com", stream).await?;
//! stream.write_all(b"GET / HTTP/1.0\r\n\r\n").await?;
//!
//! let mut res = Vec::new();
//! stream.read_to_end(&mut res).await?;
//! println!("{}", String::from_utf8_lossy(&res));
//! #
//! # Ok(()) }) }
//! # #[cfg(feature = "runtime-tokio")]
//! # fn main() {}
//! ```
#[cfg(not(any(feature = "runtime-tokio", feature = "runtime-async-std")))]
compile_error!("one of 'runtime-async-std' or 'runtime-tokio' features must be enabled");
#[cfg(all(feature = "runtime-tokio", feature = "runtime-async-std"))]
compile_error!("only one of 'runtime-async-std' or 'runtime-tokio' features must be enabled");
mod acceptor;
mod connector;
mod handshake;
mod runtime;
mod std_adapter;
mod tls_stream;
pub use accept::accept;
pub use acceptor::{Error as AcceptError, TlsAcceptor};
pub use connect::{connect, TlsConnector};
pub use host::Host;
pub use tls_stream::TlsStream;
#[doc(inline)]
pub use native_tls::{Certificate, Error, Identity, Protocol, Result};
mod accept {
use crate::runtime::{AsyncRead, AsyncWrite};
use crate::TlsStream;
/// One of accept of an incoming connection.
///
/// # Example
///
/// ```no_run
/// # #[cfg(feature = "runtime-async-std")]
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*;
/// use async_std::net::TcpListener;
/// use async_std::fs::File;
///
/// let listener = TcpListener::bind("0.0.0.0:8443").await?;
/// let (stream, _addr) = listener.accept().await?;
///
/// let key = File::open("identity.pfx").await?;
/// let stream = async_native_tls::accept(key, "<password>", stream).await?;
/// // handle stream here
/// #
/// # Ok(()) }) }
/// # #[cfg(feature = "runtime-tokio")]
/// # fn main() {}
/// ```
pub async fn accept<R, S, T>(
file: R,
password: S,
stream: T,
) -> Result<TlsStream<T>, crate::AcceptError>
where
R: AsyncRead + Unpin,
S: AsRef<str>,
T: AsyncRead + AsyncWrite + Unpin,
{
let acceptor = crate::TlsAcceptor::new(file, password).await?;
let stream = acceptor.accept(stream).await?;
Ok(stream)
}
}
mod host {
use url::Url;
/// The host part of a domain (without scheme, port and path).
///
/// This is the argument to the [`connect`](crate::connect::connect) function. Strings and string slices are
/// converted into Hosts automatically, as is [Url](url::Url) with the `host-from-url` feature (enabled by default).
#[derive(Debug)]
pub struct Host(String);
impl Host {
/// The host as string. Consumes self.
#[allow(clippy::wrong_self_convention)]
pub fn as_string(self) -> String {
self.0
}
}
impl From<&str> for Host {
fn from(host: &str) -> Self {
Self(host.into())
}
}
impl From<String> for Host {
fn from(host: String) -> Self {
Self(host)
}
}
impl From<&String> for Host {
fn from(host: &String) -> Self {
Self(host.into())
}
}
impl From<Url> for Host {
fn from(url: Url) -> Self {
Self(
url.host_str()
.expect("URL has to include a host part.")
.into(),
)
}
}
impl From<&Url> for Host {
fn from(url: &Url) -> Self {
Self(
url.host_str()
.expect("URL has to include a host part.")
.into(),
)
}
}
}
mod connect {
use std::fmt::{self, Debug};
use crate::host::Host;
use crate::runtime::{AsyncRead, AsyncWrite};
use crate::TlsStream;
use crate::{Certificate, Identity, Protocol};
/// Connect a client to a remote server.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "runtime-async-std")]
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*;
/// use async_std::net::TcpStream;
///
/// let stream = TcpStream::connect("google.com:443").await?;
/// let mut stream = async_native_tls::connect("google.com", stream).await?;
/// stream.write_all(b"GET / HTTP/1.0\r\n\r\n").await?;
///
/// let mut res = Vec::new();
/// stream.read_to_end(&mut res).await?;
/// println!("{}", String::from_utf8_lossy(&res));
/// #
/// # Ok(()) }) }
/// # #[cfg(feature = "runtime-tokio")]
/// # fn main() {}
/// ```
pub async fn connect<S>(host: impl Into<Host>, stream: S) -> native_tls::Result<TlsStream<S>>
where
S: AsyncRead + AsyncWrite + Unpin,
{
let stream = TlsConnector::new().connect(host, stream).await?;
Ok(stream)
}
/// Connect a client to a remote server.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "runtime-async-std")]
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*;
/// use async_std::net::TcpStream;
/// use async_native_tls::TlsConnector;
///
/// let stream = TcpStream::connect("google.com:443").await?;
/// let mut stream = TlsConnector::new()
/// .use_sni(true)
/// .connect("google.com", stream)
/// .await?;
/// stream.write_all(b"GET / HTTP/1.0\r\n\r\n").await?;
///
/// let mut res = Vec::new();
/// stream.read_to_end(&mut res).await?;
/// println!("{}", String::from_utf8_lossy(&res));
/// #
/// # Ok(()) }) }
/// # #[cfg(feature = "runtime-tokio")]
/// # fn main() {}
/// ```
pub struct TlsConnector {
builder: native_tls::TlsConnectorBuilder,
}
impl Default for TlsConnector {
fn default() -> Self {
TlsConnector::new()
}
}
impl TlsConnector {
/// Create a new instance.
pub fn new() -> Self {
Self {
builder: native_tls::TlsConnector::builder(),
}
}
/// Sets the identity to be used for client certificate authentication.
pub fn identity(mut self, identity: Identity) -> Self {
self.builder.identity(identity);
self
}
/// Sets the minimum supported protocol version.
///
/// A value of `None` enables support for the oldest protocols supported by the
/// implementation. Defaults to `Some(Protocol::Tlsv10)`.
pub fn min_protocol_version(mut self, protocol: Option<Protocol>) -> Self {
self.builder.min_protocol_version(protocol);
self
}
/// Sets the maximum supported protocol version.
///
/// A value of `None` enables support for the newest protocols supported by the
/// implementation. Defaults to `None`.
pub fn max_protocol_version(mut self, protocol: Option<Protocol>) -> Self {
self.builder.max_protocol_version(protocol);
self
}
/// Adds a certificate to the set of roots that the connector will trust.
///
/// The connector will use the system's trust root by default. This method can be used to
/// add to that set when communicating with servers not trusted by the system. Defaults to
/// an empty set.
pub fn add_root_certificate(mut self, cert: Certificate) -> Self {
self.builder.add_root_certificate(cert);
self
}
/// Controls the use of certificate validation.
///
/// Defaults to false.
///
/// # Warning
///
/// You should think very carefully before using this method. If invalid certificates are
/// trusted, any certificate for any site will be trusted for use. This includes expired
/// certificates. This introduces significant vulnerabilities, and should only be used as a
/// last resort.
pub fn danger_accept_invalid_certs(mut self, accept_invalid_certs: bool) -> Self {
self.builder
.danger_accept_invalid_certs(accept_invalid_certs);
self
}
/// Controls the use of Server Name Indication (SNI).
///
/// Defaults to `true`.
pub fn use_sni(mut self, use_sni: bool) -> Self {
self.builder.use_sni(use_sni);
self
}
/// Controls the use of hostname verification.
///
/// Defaults to `false`.
///
/// # Warning
///
/// You should think very carefully before using this method. If invalid hostnames are
/// trusted, any valid certificate for any site will be trusted for use. This introduces
/// significant vulnerabilities, and should only be used as a last resort.
pub fn danger_accept_invalid_hostnames(mut self, accept_invalid_hostnames: bool) -> Self {
self.builder
.danger_accept_invalid_hostnames(accept_invalid_hostnames);
self
}
/// Connect to a remote server.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "runtime-async-std")]
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*;
/// use async_std::net::TcpStream;
/// use async_native_tls::TlsConnector;
///
/// let stream = TcpStream::connect("google.com:443").await?;
/// let mut stream = TlsConnector::new()
/// .use_sni(true)
/// .connect("google.com", stream)
/// .await?;
/// stream.write_all(b"GET / HTTP/1.0\r\n\r\n").await?;
///
/// let mut res = Vec::new();
/// stream.read_to_end(&mut res).await?;
/// println!("{}", String::from_utf8_lossy(&res));
/// #
/// # Ok(()) }) }
/// # #[cfg(feature = "runtime-tokio")]
/// # fn main() {}
/// ```
pub async fn connect<S>(
&self,
host: impl Into<Host>,
stream: S,
) -> native_tls::Result<TlsStream<S>>
where
S: AsyncRead + AsyncWrite + Unpin,
{
let host: Host = host.into();
let domain = host.as_string();
let connector = self.builder.build()?;
let connector = crate::connector::TlsConnector::from(connector);
let stream = connector.connect(&domain, stream).await?;
Ok(stream)
}
}
impl Debug for TlsConnector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TlsConnector").finish()
}
}
impl From<native_tls::TlsConnectorBuilder> for TlsConnector {
fn from(builder: native_tls::TlsConnectorBuilder) -> Self {
Self { builder }
}
}
}