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
use std::net::SocketAddr;
use std::io;
use futures::Future;
use openssl::x509::X509 as OpensslX509;
use tokio_core::reactor::Core;
use error::*;
use client::{ClientConnection, ClientStreamHandle};
use tls::{TlsClientStream, TlsClientStreamBuilder};
pub struct TlsClientConnection {
io_loop: Core,
tls_client_stream: Box<Future<Item = TlsClientStream, Error = io::Error>>,
client_stream_handle: Box<ClientStreamHandle>,
}
impl TlsClientConnection {
pub fn builder() -> TlsClientConnectionBuilder {
TlsClientConnectionBuilder(TlsClientStream::builder())
}
}
impl ClientConnection for TlsClientConnection {
type MessageStream = TlsClientStream;
fn unwrap
(self)
-> (Core,
Box<Future<Item = Self::MessageStream, Error = io::Error>>,
Box<ClientStreamHandle>) {
(self.io_loop, self.tls_client_stream, self.client_stream_handle)
}
}
pub struct TlsClientConnectionBuilder(TlsClientStreamBuilder);
impl TlsClientConnectionBuilder {
pub fn add_ca(&mut self, ca: OpensslX509) {
self.0.add_ca(ca);
}
#[cfg(feature = "mtls")]
pub fn identity(&mut self, pkcs12: Pkcs12) {
self.0.identity(pkcs12);
}
pub fn build(self,
name_server: SocketAddr,
subject_name: String)
-> ClientResult<TlsClientConnection> {
let io_loop = try!(Core::new());
let (tls_client_stream, handle) = self.0.build(name_server, subject_name, io_loop.handle());
Ok(TlsClientConnection {
io_loop: io_loop,
tls_client_stream: tls_client_stream,
client_stream_handle: handle,
})
}
}