Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lightway-core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,8 @@ impl<AppState: Send> Connection<AppState> {
ref mut pending_session_id,
..
} => {
let new_session_id = self.rng.lock().unwrap().random();
// Use OS RNG directly for cryptographic security
let new_session_id = wire::secure_random_session_id();

self.session.io_cb_mut().set_session_id(new_session_id);

Expand Down
2 changes: 1 addition & 1 deletion lightway-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub use utils::{
tcp_adjust_packet_checksum, udp_adjust_packet_checksum,
};
pub use version::Version;
pub use wire::{AuthMethod, Header, SessionId};
pub use wire::{secure_random_session_id, AuthMethod, Header, SessionId};

/// Default MTU size for a packet on the outside path (on the wire)
pub const MAX_OUTSIDE_MTU: usize = 1500;
Expand Down
19 changes: 19 additions & 0 deletions lightway-core/src/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use crate::borrowed_bytesmut::BorrowedBytesMut;
use bytes::{Buf, BufMut, BytesMut};
use more_asserts::*;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use rand::rngs::OsRng;
use rand::Rng;

// A module for each frame type with a payload
Expand Down Expand Up @@ -160,6 +161,24 @@ impl rand::distr::Distribution<SessionId> for rand::distr::StandardUniform {
}
}

/// Generate a cryptographically secure session ID using OS RNG.
///
/// This function uses the operating system's secure random number generator
/// (typically /dev/urandom on Unix or CryptGenRandom on Windows) which is
/// suitable for cryptographic purposes.
pub fn secure_random_session_id() -> SessionId {
let mut bytes = [0u8; 8];
OsRng.fill_bytes(&mut bytes);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been removed from the recent version
https://rust-random.github.io/book/update-0.10.html#sysrng

We moved to rand::make_rng() as per the official recommendation

let session_id = SessionId(bytes);

// Ensure we don't return a reserved value
if session_id.is_reserved() {
secure_random_session_id()
} else {
session_id
}
}

impl std::fmt::Debug for SessionId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let n = u64::from_be_bytes(self.0);
Expand Down