Skip to content

Commit be9c398

Browse files
committed
Reenable certificate files in client for testing
1 parent 7bf1d74 commit be9c398

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,17 @@ See [examples/server](examples/server/src/main.rs). You can run it with:
3636

3737
```sh
3838
cd examples/server
39-
cargo run -- 127.0.0.1:8000 --cert mycert.der --key mykey.der
39+
cargo run -- 127.0.0.1:8080 --cert ../../tests/end.cert --key ../../tests/end.rsa
4040
```
4141

42+
and point the client at it with:
43+
44+
```sh
45+
cd examples/client
46+
cargo run -- 127.0.0.1 --port 8080 --domain localhost --cafile ../../tests/end.chain```
47+
48+
**NOTE**: Don't ever use those certificate files anywhere but for testing!
49+
4250
## Safety
4351
4452
This crate uses ``#![deny(unsafe_code)]`` to ensure everything is implemented in

examples/client/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ["quininer <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8-
async-std = { path = "../../../async-std" }
98
structopt = "0.2"
9+
rustls = "0.16"
10+
async-std = "0.99"
1011
async-tls = { path = "../.." }

examples/client/src/main.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ use async_std::io::Write;
55
use async_std::net::TcpStream;
66
use async_std::task;
77
use async_tls::TlsConnector;
8+
use rustls::ClientConfig;
9+
use std::sync::Arc;
10+
use std::fs::File;
11+
use std::io::BufReader;
812
use std::net::ToSocketAddrs;
13+
use std::path::{Path, PathBuf};
914
use structopt::StructOpt;
1015

1116
#[derive(StructOpt)]
@@ -20,6 +25,11 @@ struct Options {
2025
/// The domain to connect to. This may be different from the host!
2126
#[structopt(short = "d", long = "domain")]
2227
domain: Option<String>,
28+
29+
/// A file with a certificate authority chain, allows to connect
30+
/// to certificate authories not included in the default set
31+
#[structopt(short = "c", long = "cafile", parse(from_os_str))]
32+
cafile: Option<PathBuf>,
2333
}
2434

2535
fn main() -> io::Result<()> {
@@ -40,7 +50,11 @@ fn main() -> io::Result<()> {
4050

4151
// Create default connector comes preconfigured with all you need to safely connect
4252
// to remote servers!
43-
let connector = TlsConnector::default();
53+
let connector = if let Some(cafile) = &options.cafile {
54+
connector_for_ca_file(cafile)?
55+
} else {
56+
TlsConnector::default()
57+
};
4458

4559
task::block_on(async {
4660
// Open a normal TCP connection, just as you are used to
@@ -66,3 +80,13 @@ fn main() -> io::Result<()> {
6680
Ok(())
6781
})
6882
}
83+
84+
fn connector_for_ca_file(cafile: &Path) -> io::Result<TlsConnector> {
85+
let mut config = ClientConfig::new();
86+
let mut pem = BufReader::new(File::open(cafile)?);
87+
config
88+
.root_store
89+
.add_pem_file(&mut pem)
90+
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid cert"))?;
91+
Ok(TlsConnector::from(Arc::new(config)))
92+
}

examples/server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2018"
66

77
[dependencies]
88
structopt = "0.2"
9+
async-std = "0.99"
910
async-tls = { path = "../.." }
10-
async-std = { path = "../../../async-std" }
1111
rustls = "0.16"
1212
webpki = "0.21"

0 commit comments

Comments
 (0)