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
use async_tls::{TlsConnector, TlsAcceptor};
use std::{fmt, io, sync::Arc};
#[derive(Clone)]
pub struct Config {
pub(crate) client: TlsConnector,
pub(crate) server: Option<TlsAcceptor>
}
impl fmt::Debug for Config {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Config")
}
}
#[derive(Clone)]
pub struct PrivateKey(rustls::PrivateKey);
impl PrivateKey {
pub fn new(bytes: Vec<u8>) -> Self {
PrivateKey(rustls::PrivateKey(bytes))
}
}
#[derive(Debug, Clone)]
pub struct Certificate(rustls::Certificate);
impl Certificate {
pub fn new(bytes: Vec<u8>) -> Self {
Certificate(rustls::Certificate(bytes))
}
}
impl Config {
pub fn new<I>(key: PrivateKey, certs: I) -> Result<Self, Error>
where
I: IntoIterator<Item = Certificate>
{
let mut builder = Config::builder();
builder.server(key, certs)?;
Ok(builder.finish())
}
pub fn client() -> Self {
Config {
client: Arc::new(client_config()).into(),
server: None
}
}
pub fn builder() -> Builder {
Builder { client: client_config(), server: None }
}
}
fn client_config() -> rustls::ClientConfig {
let mut client = rustls::ClientConfig::new();
client.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
client
}
pub struct Builder {
client: rustls::ClientConfig,
server: Option<rustls::ServerConfig>
}
impl Builder {
pub fn server<I>(&mut self, key: PrivateKey, certs: I) -> Result<&mut Self, Error>
where
I: IntoIterator<Item = Certificate>
{
let mut server = rustls::ServerConfig::new(rustls::NoClientAuth::new());
let certs = certs.into_iter().map(|c| c.0).collect();
server.set_single_cert(certs, key.0).map_err(|e| Error::Tls(Box::new(e)))?;
self.server = Some(server);
Ok(self)
}
pub fn add_trust(&mut self, cert: &Certificate) -> Result<&mut Self, Error> {
self.client.root_store.add(&cert.0).map_err(|e| Error::Tls(Box::new(e)))?;
Ok(self)
}
pub fn finish(self) -> Config {
Config {
client: Arc::new(self.client).into(),
server: self.server.map(|s| Arc::new(s).into())
}
}
}
pub(crate) fn dns_name_ref(name: &str) -> Result<webpki::DNSNameRef<'_>, Error> {
webpki::DNSNameRef::try_from_ascii_str(name).map_err(|_| Error::InvalidDnsName(name.into()))
}
#[derive(Debug)]
pub enum Error {
Io(io::Error),
Tls(Box<dyn std::error::Error + Send + Sync>),
InvalidDnsName(String),
#[doc(hidden)]
__Nonexhaustive
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "i/o error: {}", e),
Error::Tls(e) => write!(f, "tls error: {}", e),
Error::InvalidDnsName(n) => write!(f, "invalid DNS name: {}", n),
Error::__Nonexhaustive => f.write_str("__Nonexhaustive")
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
Error::Tls(e) => Some(&**e),
Error::InvalidDnsName(_) | Error::__Nonexhaustive => None
}
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}