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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
extern crate openssl;
use std::io;
use std::fmt;
use std::error;
use self::openssl::pkcs12;
use self::openssl::error::ErrorStack;
use self::openssl::ssl::{self, SslMethod, SslConnectorBuilder, SslConnector, SslAcceptorBuilder,
SslAcceptor, MidHandshakeSslStream};
pub struct Error(ssl::Error);
impl error::Error for Error {
fn description(&self) -> &str {
error::Error::description(&self.0)
}
fn cause(&self) -> Option<&error::Error> {
error::Error::cause(&self.0)
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, fmt)
}
}
impl fmt::Debug for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.0, fmt)
}
}
impl From<ssl::Error> for Error {
fn from(err: ssl::Error) -> Error {
Error(err)
}
}
impl From<ErrorStack> for Error {
fn from(err: ErrorStack) -> Error {
ssl::Error::Ssl(err).into()
}
}
pub struct Pkcs12(pkcs12::ParsedPkcs12);
impl Pkcs12 {
pub fn from_der(buf: &[u8], pass: &str) -> Result<Pkcs12, Error> {
let pkcs12 = try!(pkcs12::Pkcs12::from_der(buf));
let parsed = try!(pkcs12.parse(pass));
Ok(Pkcs12(parsed))
}
}
pub struct MidHandshakeTlsStream<S>(MidHandshakeSslStream<S>);
impl<S> fmt::Debug for MidHandshakeTlsStream<S>
where S: fmt::Debug
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.0, fmt)
}
}
impl<S> MidHandshakeTlsStream<S> {
pub fn get_ref(&self) -> &S {
self.0.get_ref()
}
pub fn get_mut(&mut self) -> &mut S {
self.0.get_mut()
}
}
impl<S> MidHandshakeTlsStream<S>
where S: io::Read + io::Write
{
pub fn handshake(self) -> Result<TlsStream<S>, HandshakeError<S>> {
match self.0.handshake() {
Ok(s) => Ok(TlsStream(s)),
Err(e) => Err(e.into()),
}
}
}
pub enum HandshakeError<S> {
Failure(Error),
Interrupted(MidHandshakeTlsStream<S>),
}
impl<S> From<ssl::HandshakeError<S>> for HandshakeError<S> {
fn from(e: ssl::HandshakeError<S>) -> HandshakeError<S> {
match e {
ssl::HandshakeError::SetupFailure(e) => {
HandshakeError::Failure(Error(ssl::Error::Ssl(e)))
}
ssl::HandshakeError::Failure(e) => HandshakeError::Failure(Error(e.into_error())),
ssl::HandshakeError::Interrupted(s) => {
HandshakeError::Interrupted(MidHandshakeTlsStream(s))
}
}
}
}
impl<S> From<ErrorStack> for HandshakeError<S> {
fn from(e: ErrorStack) -> HandshakeError<S> {
HandshakeError::Failure(e.into())
}
}
pub struct TlsConnectorBuilder(SslConnectorBuilder);
impl TlsConnectorBuilder {
pub fn identity(&mut self, pkcs12: Pkcs12) -> Result<(), Error> {
let ctx = self.0.builder_mut();
try!(ctx.set_certificate(&pkcs12.0.cert));
try!(ctx.set_private_key(&pkcs12.0.pkey));
try!(ctx.check_private_key());
for cert in pkcs12.0.chain {
try!(ctx.add_extra_chain_cert(cert));
}
Ok(())
}
pub fn build(self) -> Result<TlsConnector, Error> {
Ok(TlsConnector(self.0.build()))
}
}
pub struct TlsConnector(SslConnector);
impl TlsConnector {
pub fn builder() -> Result<TlsConnectorBuilder, Error> {
let builder = try!(SslConnectorBuilder::new(SslMethod::tls()));
Ok(TlsConnectorBuilder(builder))
}
pub fn connect<S>(&self, domain: &str, stream: S) -> Result<TlsStream<S>, HandshakeError<S>>
where S: io::Read + io::Write
{
let s = try!(self.0.connect(domain, stream));
Ok(TlsStream(s))
}
}
pub trait TlsConnectorBuilderExt {
fn builder(&self) -> &SslConnectorBuilder;
fn builder_mut(&mut self) -> &mut SslConnectorBuilder;
}
impl TlsConnectorBuilderExt for ::TlsConnectorBuilder {
fn builder(&self) -> &SslConnectorBuilder {
&(self.0).0
}
fn builder_mut(&mut self) -> &mut SslConnectorBuilder {
&mut (self.0).0
}
}
pub struct TlsAcceptorBuilder(SslAcceptorBuilder);
impl TlsAcceptorBuilder {
pub fn build(self) -> Result<TlsAcceptor, Error> {
Ok(TlsAcceptor(self.0.build()))
}
}
pub struct TlsAcceptor(SslAcceptor);
impl TlsAcceptor {
pub fn builder(pkcs12: Pkcs12) -> Result<TlsAcceptorBuilder, Error> {
let builder = try!(SslAcceptorBuilder::mozilla_intermediate(
SslMethod::tls(), &pkcs12.0.pkey, &pkcs12.0.cert, &pkcs12.0.chain));
Ok(TlsAcceptorBuilder(builder))
}
pub fn accept<S>(&self, stream: S) -> Result<TlsStream<S>, HandshakeError<S>>
where S: io::Read + io::Write
{
let s = try!(self.0.accept(stream));
Ok(TlsStream(s))
}
}
pub trait TlsAcceptorBuilderExt {
fn builder(&self) -> &SslAcceptorBuilder;
fn builder_mut(&mut self) -> &mut SslAcceptorBuilder;
}
impl TlsAcceptorBuilderExt for ::TlsAcceptorBuilder {
fn builder(&self) -> &SslAcceptorBuilder {
&(self.0).0
}
fn builder_mut(&mut self) -> &mut SslAcceptorBuilder {
&mut (self.0).0
}
}
pub struct TlsStream<S>(ssl::SslStream<S>);
impl<S: fmt::Debug> fmt::Debug for TlsStream<S> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.0, fmt)
}
}
impl<S: io::Read + io::Write> TlsStream<S> {
pub fn buffered_read_size(&self) -> Result<usize, Error> {
Ok(self.0.ssl().pending())
}
pub fn get_ref(&self) -> &S {
self.0.get_ref()
}
pub fn get_mut(&mut self) -> &mut S {
self.0.get_mut()
}
}
impl<S: io::Read + io::Write> io::Read for TlsStream<S> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
}
impl<S: io::Read + io::Write> io::Write for TlsStream<S> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}
pub trait TlsStreamExt<S> {
fn raw_stream(&self) -> &ssl::SslStream<S>;
fn raw_stream_mut(&mut self) -> &mut ssl::SslStream<S>;
}
impl<S> TlsStreamExt<S> for ::TlsStream<S> {
fn raw_stream(&self) -> &ssl::SslStream<S> {
&(self.0).0
}
fn raw_stream_mut(&mut self) -> &mut ssl::SslStream<S> {
&mut (self.0).0
}
}