79 lines
2.5 KiB
Markdown
79 lines
2.5 KiB
Markdown
# tokio-rustls
|
|
|
|
[](https://github.com/rustls/tokio-rustls/actions)
|
|
[](https://crates.io/crates/tokio-rustls)
|
|
[](https://github.com/rustls/tokio-rustls/blob/main/LICENSE-MIT)
|
|
[](https://github.com/rustls/tokio-rustls/blob/main/LICENSE-APACHE)
|
|
[](https://docs.rs/tokio-rustls)
|
|
|
|
Asynchronous TLS/SSL streams for [Tokio](https://tokio.rs/) using
|
|
[Rustls](https://github.com/rustls/rustls).
|
|
|
|
### Basic Structure of a Client
|
|
|
|
```rust
|
|
use rustls_pki_types::ServerName;
|
|
use std::sync::Arc;
|
|
use tokio::net::TcpStream;
|
|
use tokio_rustls::rustls::{ClientConfig, RootCertStore};
|
|
use tokio_rustls::TlsConnector;
|
|
|
|
// ...
|
|
|
|
let mut root_cert_store = RootCertStore::empty();
|
|
root_cert_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
|
|
let config = ClientConfig::builder()
|
|
.with_root_certificates(root_cert_store)
|
|
.with_no_client_auth();
|
|
let connector = TlsConnector::from(Arc::new(config));
|
|
let dnsname = ServerName::try_from("www.rust-lang.org").unwrap();
|
|
|
|
let stream = TcpStream::connect(&addr).await?;
|
|
let mut stream = connector.connect(dnsname, stream).await?;
|
|
|
|
// ...
|
|
```
|
|
|
|
### Client Example Program
|
|
|
|
See [examples/client.rs](examples/client.rs). You can run it with:
|
|
|
|
```sh
|
|
cargo run --example client -- hsts.badssl.com
|
|
```
|
|
|
|
### Server Example Program
|
|
|
|
See [examples/server.rs](examples/server.rs). You can run it with:
|
|
|
|
```sh
|
|
cargo run --example server -- 127.0.0.1:8000 --cert certs/cert.pem --key certs/cert.key.pem
|
|
```
|
|
|
|
If you don't have a certificate and key, you can generate a random key and
|
|
self-signed certificate for testing with:
|
|
|
|
```sh
|
|
cargo install --locked rustls-cert-gen
|
|
rustls-cert-gen --output certs/ --san localhost
|
|
```
|
|
|
|
### License & Origin
|
|
|
|
This project is licensed under either of
|
|
|
|
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
https://www.apache.org/licenses/LICENSE-2.0)
|
|
- MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
https://opensource.org/licenses/MIT)
|
|
|
|
at your option.
|
|
|
|
This started as a fork of [tokio-tls](https://github.com/tokio-rs/tokio-tls).
|
|
|
|
### Contribution
|
|
|
|
Unless you explicitly state otherwise, any contribution intentionally submitted
|
|
for inclusion in tokio-rustls by you, as defined in the Apache-2.0 license, shall be
|
|
dual licensed as above, without any additional terms or conditions.
|