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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
use proto::PostQuantumRequestV1;
use std::fmt;
#[cfg(not(target_os = "ios"))]
use std::net::SocketAddr;
#[cfg(not(target_os = "ios"))]
use std::net::{IpAddr, Ipv4Addr};
use std::time::Instant;
use talpid_types::net::wireguard::{PresharedKey, PublicKey};
use tonic::transport::Channel;
#[cfg(not(target_os = "ios"))]
use tonic::transport::Endpoint;
#[cfg(not(target_os = "ios"))]
use tower::service_fn;
use zeroize::Zeroize;
mod hqc;
mod ml_kem;
#[cfg(not(target_os = "ios"))]
mod socket;
#[allow(clippy::derive_partial_eq_without_eq)]
mod proto {
tonic::include_proto!("ephemeralpeer");
}
const DAITA_VERSION: u32 = 2;
#[derive(Debug)]
pub enum Error {
GrpcConnectError(tonic::transport::Error),
// TODO: Remove box when upgrading tonic to a version with
// https://github.com/hyperium/tonic/pull/2282
GrpcError(Box<tonic::Status>),
MissingCiphertexts,
InvalidCiphertextLength {
algorithm: &'static str,
actual: usize,
expected: usize,
},
InvalidCiphertextCount {
actual: usize,
},
MissingDaitaResponse,
#[cfg(target_os = "ios")]
TcpConnectionOpen,
#[cfg(target_os = "ios")]
UnableToCreateRuntime,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Error::*;
match self {
GrpcConnectError(err) => write!(f, "Failed to connect to config service: {err:?}"),
GrpcError(status) => write!(f, "RPC failed: {status}"),
MissingCiphertexts => write!(f, "Found no ciphertexts in response"),
InvalidCiphertextLength {
algorithm,
actual,
expected,
} => write!(
f,
"Expected a {expected} bytes ciphertext for {algorithm}, got {actual} bytes"
),
InvalidCiphertextCount { actual } => {
write!(f, "Expected 2 ciphertext in the response, got {actual}")
}
MissingDaitaResponse => "Expected DAITA configuration in response".fmt(f),
#[cfg(target_os = "ios")]
TcpConnectionOpen => "Failed to open TCP connection".fmt(f),
#[cfg(target_os = "ios")]
UnableToCreateRuntime => "Unable to create iOS PQ PSK runtime".fmt(f),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::GrpcConnectError(error) => Some(error),
_ => None,
}
}
}
pub type RelayConfigService = proto::ephemeral_peer_client::EphemeralPeerClient<Channel>;
/// Port used by the tunnel config service.
pub const CONFIG_SERVICE_PORT: u16 = 1337;
pub struct EphemeralPeer {
pub psk: Option<PresharedKey>,
pub daita: Option<DaitaSettings>,
}
pub struct DaitaSettings {
pub client_machines: Vec<String>,
pub max_padding_frac: f64,
pub max_blocking_frac: f64,
}
/// Negotiate a short-lived peer with a PQ-safe PSK or with DAITA enabled.
#[cfg(not(target_os = "ios"))]
pub async fn request_ephemeral_peer(
service_address: Ipv4Addr,
parent_pubkey: PublicKey,
ephemeral_pubkey: PublicKey,
enable_post_quantum: bool,
enable_daita: bool,
) -> Result<EphemeralPeer, Error> {
log::debug!("Connecting to relay config service at {service_address}");
let client = connect_relay_config_client(service_address).await?;
log::debug!("Connected to relay config service at {service_address}");
request_ephemeral_peer_with(
client,
parent_pubkey,
ephemeral_pubkey,
enable_post_quantum,
enable_daita,
)
.await
}
pub async fn request_ephemeral_peer_with(
mut client: RelayConfigService,
parent_pubkey: PublicKey,
ephemeral_pubkey: PublicKey,
enable_quantum_resistant: bool,
enable_daita: bool,
) -> Result<EphemeralPeer, Error> {
let (pq_request, kem_keypairs) = if enable_quantum_resistant {
let start = Instant::now();
let (pq_request, kem_keypairs) = post_quantum_secrets();
log::debug!(
"Generated quantum-resistant key exchange material in {} ms",
start.elapsed().as_millis()
);
(Some(pq_request), Some(kem_keypairs))
} else {
(None, None)
};
let response = client
.register_peer_v1(proto::EphemeralPeerRequestV1 {
wg_parent_pubkey: parent_pubkey.as_bytes().to_vec(),
wg_ephemeral_peer_pubkey: ephemeral_pubkey.as_bytes().to_vec(),
post_quantum: pq_request,
daita: None,
daita_v2: enable_daita.then(|| {
let platform = get_platform();
log::trace!("DAITA v2 platform: {platform:?}");
proto::DaitaRequestV2 {
level: i32::from(proto::DaitaLevel::LevelDefault),
platform: i32::from(platform),
version: DAITA_VERSION,
}
}),
})
.await
.map_err(|status| Error::GrpcError(Box::new(status)))?;
let response = response.into_inner();
let psk = if let Some((ml_kem_keypair, hqc_keypair)) = kem_keypairs {
let ciphertexts = response
.post_quantum
.ok_or(Error::MissingCiphertexts)?
.ciphertexts;
// Unpack the ciphertexts into one per KEM without needing to access them by index.
let [ml_kem_ciphertext, hqc_ciphertext] = <&[Vec<u8>; 2]>::try_from(ciphertexts.as_slice())
.map_err(|_| Error::InvalidCiphertextCount {
actual: ciphertexts.len(),
})?;
// Store the PSK data on the heap. So it can be passed around and then zeroized on drop
// without being stored in a bunch of places on the stack.
let mut psk_data = Box::new([0u8; 32]);
// Decapsulate ML-KEM and mix into PSK
{
let mut shared_secret = ml_kem_keypair.decapsulate(ml_kem_ciphertext)?;
xor_assign(&mut psk_data, &shared_secret);
// The shared secret is sadly stored in an array on the stack. So we can't get any
// guarantees that it's not copied around on the stack. The best we can do here
// is to zero out the version we have and hope the compiler optimizes out copies.
// https://github.com/Argyle-Software/kyber/issues/59
shared_secret.zeroize();
}
// Decapsulate HQC and mix into PSK
{
let mut shared_secret = hqc_keypair.decapsulate(hqc_ciphertext)?;
xor_assign(&mut psk_data, &shared_secret);
// The shared secret is sadly stored in an array on the stack. So we can't get any
// guarantees that it's not copied around on the stack. The best we can do here
// is to zero out the version we have and hope the compiler optimizes out copies.
// https://github.com/Argyle-Software/kyber/issues/59
shared_secret.zeroize();
}
Some(PresharedKey::from(psk_data))
} else {
None
};
let daita = response.daita.map(|daita| DaitaSettings {
client_machines: daita.client_machines,
max_padding_frac: daita.max_padding_frac,
max_blocking_frac: daita.max_blocking_frac,
});
if daita.is_none() && enable_daita {
return Err(Error::MissingDaitaResponse);
}
Ok(EphemeralPeer { psk, daita })
}
const fn get_platform() -> proto::DaitaPlatform {
use proto::DaitaPlatform;
const PLATFORM: DaitaPlatform = if cfg!(target_os = "windows") {
DaitaPlatform::WindowsWgGo
} else if cfg!(target_os = "linux") {
DaitaPlatform::LinuxWgGo
} else if cfg!(target_os = "macos") {
DaitaPlatform::MacosWgGo
} else if cfg!(target_os = "android") {
DaitaPlatform::AndroidWgGo
} else if cfg!(target_os = "ios") {
DaitaPlatform::IosWgGo
} else {
panic!("This platform does not support DAITA V2")
};
PLATFORM
}
fn post_quantum_secrets() -> (PostQuantumRequestV1, (ml_kem::Keypair, hqc::Keypair)) {
let ml_kem_keypair = ml_kem::keypair();
let hqc_keypair = hqc::keypair();
(
proto::PostQuantumRequestV1 {
kem_pubkeys: vec![
proto::KemPubkeyV1 {
algorithm_name: ml_kem_keypair.algorithm_name().to_owned(),
key_data: ml_kem_keypair.encapsulation_key(),
},
proto::KemPubkeyV1 {
algorithm_name: hqc_keypair.algorithm_name().to_owned(),
key_data: hqc_keypair.encapsulation_key(),
},
],
},
(ml_kem_keypair, hqc_keypair),
)
}
/// Performs `dst = dst ^ src`.
fn xor_assign(dst: &mut [u8; 32], src: &[u8; 32]) {
for (dst_byte, src_byte) in dst.iter_mut().zip(src.iter()) {
*dst_byte ^= src_byte;
}
}
/// Create a new `RelayConfigService` connected to the given IP.
/// On non-Windows platforms the connection is made with a socket where the MSS
/// value has been speficically lowered, to avoid MTU issues. See the `socket` module.
#[cfg(not(target_os = "ios"))]
async fn connect_relay_config_client(ip: Ipv4Addr) -> Result<RelayConfigService, Error> {
use hyper_util::rt::tokio::TokioIo;
let endpoint = Endpoint::from_static("tcp://0.0.0.0:0");
let addr = SocketAddr::new(IpAddr::V4(ip), CONFIG_SERVICE_PORT);
let connection = endpoint
.connect_with_connector(service_fn(move |_| async move {
let sock = socket::TcpSocket::new()?;
let stream = sock.connect(addr).await?;
let sniffer = socket_sniffer::SocketSniffer {
s: stream,
rx_bytes: 0,
tx_bytes: 0,
start_time: std::time::Instant::now(),
};
Ok::<_, std::io::Error>(TokioIo::new(sniffer))
}))
.await
.map_err(Error::GrpcConnectError)?;
Ok(RelayConfigService::new(connection))
}
mod socket_sniffer {
pub struct SocketSniffer<S> {
pub s: S,
pub rx_bytes: usize,
pub tx_bytes: usize,
pub start_time: std::time::Instant,
}
use std::{
io,
pin::Pin,
task::{Context, Poll},
};
use tokio::io::AsyncWrite;
use tokio::io::{AsyncRead, ReadBuf};
impl<S> Drop for SocketSniffer<S> {
fn drop(&mut self) {
let duration = self.start_time.elapsed();
log::debug!(
"Tunnel config client connection ended. RX: {} bytes, TX: {} bytes, duration: {} s",
self.rx_bytes,
self.tx_bytes,
duration.as_secs()
);
}
}
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncRead for SocketSniffer<S> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let initial_data = buf.filled().len();
let bytes = std::task::ready!(Pin::new(&mut self.s).poll_read(cx, buf));
if bytes.is_ok() {
self.rx_bytes += buf.filled().len().saturating_sub(initial_data);
}
Poll::Ready(bytes)
}
}
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncWrite for SocketSniffer<S> {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
let bytes = std::task::ready!(Pin::new(&mut self.s).poll_write(cx, buf));
if let Ok(bytes) = bytes {
self.tx_bytes += bytes;
}
Poll::Ready(bytes)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.s).poll_flush(cx)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.s).poll_shutdown(cx)
}
}
}
|