tokio-rustls/README.md

78 lines
2.5 KiB
Markdown
Raw Normal View History

2023-05-31 07:17:08 +00:00
# tokio-rustls
2023-05-31 07:22:02 +00:00
[![github actions](https://github.com/rustls/tokio-rustls/workflows/CI/badge.svg)](https://github.com/rustls/tokio-rustls/actions)
2023-05-31 07:17:08 +00:00
[![crates](https://img.shields.io/crates/v/tokio-rustls.svg)](https://crates.io/crates/tokio-rustls)
2023-05-31 07:22:02 +00:00
[![license](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/rustls/tokio-rustls/blob/main/LICENSE-MIT)
[![license](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/rustls/tokio-rustls/blob/main/LICENSE-APACHE)
2023-05-31 07:17:08 +00:00
[![docs.rs](https://docs.rs/tokio-rustls/badge.svg)](https://docs.rs/tokio-rustls)
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
Asynchronous TLS/SSL streams for [Tokio](https://tokio.rs/) using
[Rustls](https://github.com/rustls/rustls).
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
### Basic Structure of a Client
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
```rust
use std::sync::Arc;
use tokio::net::TcpStream;
use tokio_rustls::rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore, ServerName};
use tokio_rustls::TlsConnector;
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
// ...
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
let mut root_cert_store = RootCertStore::empty();
root_cert_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
}));
let config = ClientConfig::builder()
.with_safe_defaults()
.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();
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
let stream = TcpStream::connect(&addr).await?;
let mut stream = connector.connect(dnsname, stream).await?;
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
// ...
```
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
### Client Example Program
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
See [examples/client](examples/client/src/main.rs). You can run it with:
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
```sh
cd examples/client
cargo run -- hsts.badssl.com
```
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
### Server Example Program
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
See [examples/server](examples/server/src/main.rs). You can run it with:
2020-01-09 23:37:48 +00:00
2023-05-31 07:17:08 +00:00
```sh
cd examples/server
cargo run -- 127.0.0.1:8000 --cert mycert.der --key mykey.der
```
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
### License & Origin
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
This project is licensed under either of
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
* 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)
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
at your option.
2020-01-09 23:36:35 +00:00
2023-05-31 07:17:08 +00:00
This started as a fork of [tokio-tls](https://github.com/tokio-rs/tokio-tls).
2020-01-09 23:36:35 +00:00
### Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
2023-05-31 07:17:08 +00:00
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.