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
use crate::certificate;
use crate::certificate::P2pCertificate;
use futures::future::BoxFuture;
use futures::AsyncWrite;
use futures::{AsyncRead, FutureExt};
use futures_rustls::TlsStream;
use libp2p_core::{identity, InboundUpgrade, OutboundUpgrade, PeerId, UpgradeInfo};
use rustls::{CommonState, ServerName};
use std::net::{IpAddr, Ipv4Addr};
use std::sync::Arc;
#[derive(thiserror::Error, Debug)]
pub enum UpgradeError {
#[error("Failed to generate certificate")]
CertificateGeneration(#[from] certificate::GenError),
#[error("Failed to upgrade server connection")]
ServerUpgrade(std::io::Error),
#[error("Failed to upgrade client connection")]
ClientUpgrade(std::io::Error),
#[error("Failed to parse certificate")]
BadCertificate(#[from] certificate::ParseError),
}
#[derive(Clone)]
pub struct Config {
server: rustls::ServerConfig,
client: rustls::ClientConfig,
}
impl Config {
pub fn new(identity: &identity::Keypair) -> Result<Self, certificate::GenError> {
Ok(Self {
server: crate::make_server_config(identity)?,
client: crate::make_client_config(identity, None)?,
})
}
}
impl UpgradeInfo for Config {
type Info = &'static [u8];
type InfoIter = std::iter::Once<Self::Info>;
fn protocol_info(&self) -> Self::InfoIter {
std::iter::once(b"/tls/1.0.0")
}
}
impl<C> InboundUpgrade<C> for Config
where
C: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
type Output = (PeerId, TlsStream<C>);
type Error = UpgradeError;
type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;
fn upgrade_inbound(self, socket: C, _: Self::Info) -> Self::Future {
async move {
let stream = futures_rustls::TlsAcceptor::from(Arc::new(self.server))
.accept(socket)
.await
.map_err(UpgradeError::ServerUpgrade)?;
let peer_id = extract_single_certificate(stream.get_ref().1)?.peer_id();
Ok((peer_id, stream.into()))
}
.boxed()
}
}
impl<C> OutboundUpgrade<C> for Config
where
C: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
type Output = (PeerId, TlsStream<C>);
type Error = UpgradeError;
type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;
fn upgrade_outbound(self, socket: C, _: Self::Info) -> Self::Future {
async move {
let name = ServerName::IpAddress(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
let stream = futures_rustls::TlsConnector::from(Arc::new(self.client))
.connect(name, socket)
.await
.map_err(UpgradeError::ClientUpgrade)?;
let peer_id = extract_single_certificate(stream.get_ref().1)?.peer_id();
Ok((peer_id, stream.into()))
}
.boxed()
}
}
fn extract_single_certificate(
state: &CommonState,
) -> Result<P2pCertificate<'_>, certificate::ParseError> {
let cert = match state
.peer_certificates()
.expect("config enforces presence of certificates")
{
[single] => single,
_ => panic!("config enforces exactly one certificate"),
};
certificate::parse(cert)
}