Skip to content

Commit

Permalink
Make Tunn::set_static_private infallible
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Oct 16, 2023
1 parent 623ea19 commit 7ac5273
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 30 deletions.
27 changes: 5 additions & 22 deletions boringtun/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,6 @@ impl Device {
}

fn set_key(&mut self, private_key: x25519::StaticSecret) {
let mut bad_peers = vec![];

let public_key = x25519::PublicKey::from(&private_key);
let key_pair = Some((private_key.clone(), public_key));

Expand All @@ -466,30 +464,15 @@ impl Device {
let rate_limiter = Arc::new(RateLimiter::new(&public_key, HANDSHAKE_RATE_LIMIT));

for peer in self.peers.values_mut() {
let mut peer_mut = peer.lock();

if peer_mut
.tunnel
.set_static_private(
private_key.clone(),
public_key,
Some(Arc::clone(&rate_limiter)),
)
.is_err()
{
// In case we encounter an error, we will remove that peer
// An error will be a result of bad public key/secret key combination
bad_peers.push(Arc::clone(peer));
}
peer.lock().tunnel.set_static_private(
private_key.clone(),
public_key,
Some(Arc::clone(&rate_limiter)),
)
}

self.key_pair = key_pair;
self.rate_limiter = Some(rate_limiter);

// Remove all the bad peers
for _ in bad_peers {
unimplemented!();
}
}

#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
Expand Down
5 changes: 2 additions & 3 deletions boringtun/src/noise/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ impl NoiseParams {
&mut self,
static_private: x25519::StaticSecret,
static_public: x25519::PublicKey,
) -> Result<(), WireGuardError> {
) {
// Check that the public key indeed matches the private key
let check_key = x25519::PublicKey::from(&static_private);
assert_eq!(check_key.as_bytes(), static_public.as_bytes());
Expand All @@ -404,7 +404,6 @@ impl NoiseParams {
self.static_public = static_public;

self.static_shared = self.static_private.diffie_hellman(&self.peer_static_public);
Ok(())
}
}

Expand Down Expand Up @@ -475,7 +474,7 @@ impl Handshake {
&mut self,
private_key: x25519::StaticSecret,
public_key: x25519::PublicKey,
) -> Result<(), WireGuardError> {
) {
self.params.set_static_private(private_key, public_key)
}

Expand Down
8 changes: 3 additions & 5 deletions boringtun/src/noise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,16 @@ impl Tunn {
static_private: x25519::StaticSecret,
static_public: x25519::PublicKey,
rate_limiter: Option<Arc<RateLimiter>>,
) -> Result<(), WireGuardError> {
) {
self.timers.should_reset_rr = rate_limiter.is_none();
self.rate_limiter = rate_limiter.unwrap_or_else(|| {
Arc::new(RateLimiter::new(&static_public, PEER_HANDSHAKE_RATE_LIMIT))
});
self.handshake
.set_static_private(static_private, static_public)?;
.set_static_private(static_private, static_public);
for s in &mut self.sessions {
*s = None;
}
Ok(())
}

/// Encapsulate a single packet from the tunnel interface.
Expand Down Expand Up @@ -605,8 +604,7 @@ mod tests {

let my_tun = Tunn::new(my_secret_key, their_public_key, None, None, my_idx, None);

let their_tun =
Tunn::new(their_secret_key, my_public_key, None, None, their_idx, None);
let their_tun = Tunn::new(their_secret_key, my_public_key, None, None, their_idx, None);

(my_tun, their_tun)
}
Expand Down

0 comments on commit 7ac5273

Please sign in to comment.