Skip to content

Commit 2ffc9ba

Browse files
committed
ts_tunnel: use the handshake primitives from ts_noise
Signed-off-by: David Anderson <danderson@tailscale.com> Change-Id: Id81c21d18d19205b249d89d477a3d9a96a6a6964
1 parent 6bd7d6e commit 2ffc9ba

7 files changed

Lines changed: 128 additions & 348 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ts_tunnel/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@ rust-version.workspace = true
1515
ts_keys.workspace = true
1616
ts_packet.workspace = true
1717
ts_time.workspace = true
18+
ts_noise.workspace = true
1819

1920
# Unconditionally required dependencies.
2021
aead.workspace = true
2122
blake2.workspace = true
2223
chacha20poly1305.workspace = true
23-
hkdf.workspace = true
2424
itertools.workspace = true
2525
rand.workspace = true
2626
tracing.workspace = true
27-
x25519-dalek.workspace = true
2827
zerocopy.workspace = true
2928

3029
[dev-dependencies]

ts_tunnel/src/config.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,11 @@
1-
use rand::{
2-
Rng, RngExt,
3-
distr::{Distribution, StandardUniform},
4-
};
51
use ts_keys::NodePublicKey;
62

73
/// A handle for a wireguard peer.
84
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
95
pub struct PeerId(pub u32);
106

117
/// A wireguard symmetric pre-shared key.
12-
#[derive(Copy, Clone)]
13-
pub struct Psk([u8; 32]);
14-
15-
impl From<[u8; 32]> for Psk {
16-
fn from(bytes: [u8; 32]) -> Self {
17-
Psk(bytes)
18-
}
19-
}
20-
21-
impl AsRef<[u8]> for Psk {
22-
fn as_ref(&self) -> &[u8] {
23-
&self.0
24-
}
25-
}
26-
27-
impl AsMut<[u8]> for Psk {
28-
fn as_mut(&mut self) -> &mut [u8] {
29-
&mut self.0
30-
}
31-
}
32-
33-
impl Distribution<Psk> for StandardUniform {
34-
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Psk {
35-
Psk(rng.random())
36-
}
37-
}
8+
pub type Psk = ts_noise::core::Psk;
389

3910
/// The cryptographic configuration for a wireguard peer.
4011
pub struct PeerConfig {

ts_tunnel/src/endpoint.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
config::{PeerConfig, PeerId},
1616
handshake::{Handshake, ReceivedHandshake, SessionPair, initiate_handshake},
1717
macs::{MACReceiver, MACSender},
18-
messages::{CookieReply, HandshakeResponse, Message, SessionId},
18+
messages::{CookieReply, HandshakeResponse, Message, MessageMut, SessionId},
1919
session::{ReceiveSession, TransmitSession},
2020
time::{TAI64N, TAI64NClock},
2121
};
@@ -347,21 +347,21 @@ impl Peer {
347347
) {
348348
let pre_len = packets.len();
349349

350-
packets.retain_mut(|packet| match Message::try_from(packet.as_ref()) {
350+
packets.retain_mut(|packet| match MessageMut::try_from(packet.as_mut()) {
351351
Err(()) => {
352352
tracing::trace!("dropping invalid packet");
353353
false
354354
}
355-
Ok(Message::TransportDataHeader(_)) => true,
356-
Ok(Message::HandshakeResponse(resp)) => {
355+
Ok(MessageMut::TransportDataHeader(_)) => true,
356+
Ok(MessageMut::HandshakeResponse(resp)) => {
357357
self.recv_handshake_response(endpoint, resp, out);
358358
false
359359
}
360-
Ok(Message::CookieReply(resp)) => {
360+
Ok(MessageMut::CookieReply(resp)) => {
361361
self.recv_cookie_reply(resp);
362362
false
363363
}
364-
Ok(Message::HandshakeInitiation(_)) => {
364+
Ok(MessageMut::HandshakeInitiation(_)) => {
365365
debug_assert!(
366366
false,
367367
"handshake initiations should have been filtered out prior to calling recv"
@@ -390,11 +390,12 @@ impl Peer {
390390
fn recv_handshake_response(
391391
&mut self,
392392
endpoint: &mut EndpointState,
393-
packet: &HandshakeResponse,
393+
packet: &mut HandshakeResponse,
394394
out: &mut RecvResult,
395395
) {
396396
let Some(session) = self.handshake.finish(
397397
packet,
398+
&endpoint.my_key.private,
398399
&self.config.psk,
399400
&endpoint.my_cookie,
400401
Instant::now(),
@@ -657,8 +658,9 @@ impl Endpoint {
657658
ret
658659
}
659660

660-
fn process_one_handshake(&mut self, packet: PacketMut, out: &mut RecvResult) {
661-
let Ok(Message::HandshakeInitiation(init)) = Message::try_from(packet.as_ref()) else {
661+
fn process_one_handshake(&mut self, mut packet: PacketMut, out: &mut RecvResult) {
662+
let Ok(MessageMut::HandshakeInitiation(init)) = MessageMut::try_from(packet.as_mut())
663+
else {
662664
tracing::error!("message parsing failed");
663665
return;
664666
};

0 commit comments

Comments
 (0)