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
use std::fmt;
use std::marker;
use crate::assert_send;
use crate::assert_sync;
use crate::AsyncSocket;
use crate::AsyncSocketBox;
use crate::BoxFuture;
use crate::ImplInfo;
use crate::TlsAcceptor;
use crate::TlsAcceptorBuilder;
use crate::TlsStream;
pub trait TlsAcceptorType: fmt::Debug + fmt::Display + Sync + 'static {
fn implemented(&self) -> bool;
fn supports_alpn(&self) -> bool;
fn supports_der_keys(&self) -> bool;
fn supports_pkcs12_keys(&self) -> bool;
fn info(&self) -> ImplInfo;
fn builder_from_der_key(
&self,
cert: &[u8],
key: &[u8],
) -> anyhow::Result<TlsAcceptorBuilderBox>;
fn builder_from_pkcs12(
&self,
pkcs12: &[u8],
passphrase: &str,
) -> anyhow::Result<TlsAcceptorBuilderBox>;
}
pub(crate) struct TlsAcceptorTypeImpl<A: TlsAcceptor>(pub marker::PhantomData<A>);
impl<A: TlsAcceptor> fmt::Debug for TlsAcceptorTypeImpl<A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&A::info(), f)
}
}
impl<A: TlsAcceptor> fmt::Display for TlsAcceptorTypeImpl<A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&A::info(), f)
}
}
impl<A: TlsAcceptor> TlsAcceptorType for TlsAcceptorTypeImpl<A> {
fn implemented(&self) -> bool {
A::IMPLEMENTED
}
fn supports_alpn(&self) -> bool {
A::SUPPORTS_ALPN
}
fn supports_der_keys(&self) -> bool {
A::SUPPORTS_DER_KEYS
}
fn supports_pkcs12_keys(&self) -> bool {
A::SUPPORTS_PKCS12_KEYS
}
fn info(&self) -> ImplInfo {
A::info()
}
fn builder_from_der_key(
&self,
cert: &[u8],
key: &[u8],
) -> anyhow::Result<TlsAcceptorBuilderBox> {
let builder = A::builder_from_der_key(cert, key)?;
Ok(TlsAcceptorBuilderBox(Box::new(builder)))
}
fn builder_from_pkcs12(
&self,
pkcs12: &[u8],
passphrase: &str,
) -> anyhow::Result<TlsAcceptorBuilderBox> {
let builder = A::builder_from_pkcs12(pkcs12, passphrase)?;
Ok(TlsAcceptorBuilderBox(Box::new(builder)))
}
}
trait TlsAcceptorBuilderDyn: Send + 'static {
fn type_dyn(&self) -> &'static dyn TlsAcceptorType;
fn set_alpn_protocols(&mut self, protocols: &[&[u8]]) -> anyhow::Result<()>;
fn build(self: Box<Self>) -> anyhow::Result<TlsAcceptorBox>;
}
impl<A: TlsAcceptorBuilder> TlsAcceptorBuilderDyn for A {
fn type_dyn(&self) -> &'static dyn TlsAcceptorType {
<A::Acceptor as TlsAcceptor>::TYPE_DYN
}
fn set_alpn_protocols(&mut self, protocols: &[&[u8]]) -> anyhow::Result<()> {
(*self).set_alpn_protocols(protocols)
}
fn build(self: Box<Self>) -> anyhow::Result<TlsAcceptorBox> {
Ok(TlsAcceptorBox(Box::new((*self).build()?)))
}
}
pub struct TlsAcceptorBuilderBox(Box<dyn TlsAcceptorBuilderDyn>);
impl TlsAcceptorBuilderBox {
pub fn type_dyn(&self) -> &'static dyn TlsAcceptorType {
self.0.type_dyn()
}
pub fn set_alpn_protocols(&mut self, protocols: &[&[u8]]) -> anyhow::Result<()> {
self.0.set_alpn_protocols(protocols)
}
pub fn build(self) -> anyhow::Result<TlsAcceptorBox> {
self.0.build()
}
}
trait TlsAcceptorDyn: Send + Sync + 'static {
fn type_dyn(&self) -> &'static dyn TlsAcceptorType;
fn accept<'a>(&'a self, socket: AsyncSocketBox) -> BoxFuture<'a, anyhow::Result<TlsStream>>;
}
impl<A: TlsAcceptor> TlsAcceptorDyn for A {
fn type_dyn(&self) -> &'static dyn TlsAcceptorType {
A::TYPE_DYN
}
fn accept<'a>(&'a self, socket: AsyncSocketBox) -> BoxFuture<'a, anyhow::Result<TlsStream>> {
self.accept(socket)
}
}
pub struct TlsAcceptorBox(Box<dyn TlsAcceptorDyn>);
impl TlsAcceptorBox {
pub(crate) fn new<A: TlsAcceptor>(acceptor: A) -> TlsAcceptorBox {
TlsAcceptorBox(Box::new(acceptor))
}
pub fn type_dyn(&self) -> &'static dyn TlsAcceptorType {
self.0.type_dyn()
}
pub fn accept<'a, S: AsyncSocket>(
&'a self,
socket: S,
) -> BoxFuture<'a, anyhow::Result<TlsStream>> {
self.0.accept(AsyncSocketBox::new(socket))
}
}
fn _assert_kinds() {
assert_send::<TlsAcceptorBuilderBox>();
assert_send::<TlsAcceptorBox>();
assert_sync::<TlsAcceptorBox>();
}