feat(client): TLS 1.3 control connection with SPKI pinning and LNEx v3
Replace the custom X25519/HKDF/ChaCha20 TCP layer with rustls (TLS 1.3 only, ALPN vnox-lnex/3, no plaintext or fallback). Add a trust store (pin, trust on first use, public CA), block changed fingerprints and identities, bind the Ed25519 auth signature to the TLS exporter, add RU/EN trust dialog and update the interop test and e2e voice tool. Claude-Session: https://claude.ai/code/session_01GDjxLvBcnJqniFQGUGb1V4
This commit is contained in:
parent
5f594046a6
commit
9a20ace6e6
31 changed files with 2085 additions and 567 deletions
|
|
@ -11,6 +11,7 @@ use crate::net::wire;
|
|||
use crate::net::{
|
||||
framing, handshake, internal,
|
||||
payloads::*,
|
||||
trust::TrustStore,
|
||||
types::{NetCommand, NetEvent},
|
||||
};
|
||||
|
||||
|
|
@ -20,6 +21,7 @@ use super::{VoiceState, handle_cmd};
|
|||
pub async fn session_loop(
|
||||
identity: &Identity,
|
||||
address: &str,
|
||||
trust: &TrustStore,
|
||||
channels: &mut Vec<String>,
|
||||
restored: bool,
|
||||
cmd_rx: &mut mpsc::Receiver<NetCommand>,
|
||||
|
|
@ -27,7 +29,7 @@ pub async fn session_loop(
|
|||
) -> Result<()> {
|
||||
let connection = tokio::time::timeout(
|
||||
Duration::from_secs(10),
|
||||
crate::net::transport::connect_with_peer(address),
|
||||
crate::net::transport::connect_with_peer(address, trust),
|
||||
)
|
||||
.await
|
||||
.context("gateway connection timed out")??;
|
||||
|
|
@ -35,12 +37,29 @@ pub async fn session_loop(
|
|||
let mut stream = connection.stream;
|
||||
let mut seq = 0u32;
|
||||
|
||||
let (session_id, node_name, private_mode, crypto) = tokio::time::timeout(
|
||||
let outcome = tokio::time::timeout(
|
||||
Duration::from_secs(10),
|
||||
handshake::run(&mut stream, identity, &mut seq),
|
||||
handshake::run(
|
||||
&mut stream,
|
||||
identity,
|
||||
&mut seq,
|
||||
&connection.binding,
|
||||
&connection.trust_key,
|
||||
connection
|
||||
.record
|
||||
.as_ref()
|
||||
.and_then(|record| record.identity.as_deref()),
|
||||
),
|
||||
)
|
||||
.await
|
||||
.context("gateway handshake timed out")??;
|
||||
// The identity is learned inside an already trusted channel and then pinned.
|
||||
if let Err(error) = trust.set_identity(&connection.trust_key, &outcome.server_identity) {
|
||||
tracing::warn!(%error, "could not store the server identity");
|
||||
}
|
||||
let session_id = outcome.session_id;
|
||||
let node_name = outcome.node_name;
|
||||
let private_mode = outcome.private_mode;
|
||||
let _ = event_tx
|
||||
.send(NetEvent::Connected {
|
||||
session_id: session_id.clone(),
|
||||
|
|
@ -51,18 +70,17 @@ pub async fn session_loop(
|
|||
.await;
|
||||
|
||||
for channel_id in channels.clone() {
|
||||
framing::io::write_encrypted(
|
||||
framing::write(
|
||||
&mut stream,
|
||||
PID_JOIN_CHANNEL,
|
||||
&mut seq,
|
||||
&wire::encode(&JoinChannelPayload { channel_id })?,
|
||||
&crypto,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
let (reader, mut stream) = tokio::io::split(stream);
|
||||
let mut incoming = framing::io::Reader::new(reader, crypto.clone());
|
||||
let mut incoming = framing::io::Reader::new(reader);
|
||||
let mut voice_state: Option<VoiceState> = None;
|
||||
let mut used_voice_routes = std::collections::HashSet::new();
|
||||
let mut voice_endpoint: Option<std::net::SocketAddr> = None;
|
||||
|
|
@ -77,7 +95,7 @@ pub async fn session_loop(
|
|||
_ = ping_tick.tick() => {
|
||||
let ts = now_ms();
|
||||
let payload = wire::encode(&PingPayload { timestamp: ts })?;
|
||||
framing::io::write_encrypted(&mut stream, PID_PING, &mut seq, &payload, &crypto).await?;
|
||||
framing::write(&mut stream, PID_PING, &mut seq, &payload).await?;
|
||||
}
|
||||
result = incoming.next() => {
|
||||
let pkt = result?;
|
||||
|
|
@ -113,8 +131,8 @@ pub async fn session_loop(
|
|||
cmd = cmd_rx.recv() => {
|
||||
match cmd {
|
||||
None | Some(NetCommand::Disconnect) => {
|
||||
let _ = framing::io::write_encrypted(&mut stream, PID_DISCONNECT, &mut seq,
|
||||
&wire::encode(&Disconnect {reason: "bye".into()})?, &crypto).await;
|
||||
let _ = framing::write(&mut stream, PID_DISCONNECT, &mut seq,
|
||||
&wire::encode(&Disconnect {reason: "bye".into()})?).await;
|
||||
return Ok(());
|
||||
}
|
||||
Some(NetCommand::Connect { address, auto_reconnect }) => {
|
||||
|
|
@ -129,14 +147,14 @@ pub async fn session_loop(
|
|||
}
|
||||
Some(NetCommand::JoinChannel { channel_id }) => {
|
||||
track_channel(channels, &channel_id);
|
||||
framing::io::write_encrypted(&mut stream, PID_JOIN_CHANNEL, &mut seq,
|
||||
&wire::encode(&JoinChannelPayload { channel_id })?, &crypto).await?;
|
||||
framing::write(&mut stream, PID_JOIN_CHANNEL, &mut seq,
|
||||
&wire::encode(&JoinChannelPayload { channel_id })?).await?;
|
||||
}
|
||||
Some(NetCommand::LeaveChannel { channel_id }) => {
|
||||
channels.retain(|c| c != &channel_id);
|
||||
if voice_state.as_ref().and_then(|value|value.channel_id())==Some(channel_id.as_str()) {voice_state=None;voice_endpoint=None;}
|
||||
framing::io::write_encrypted(&mut stream, PID_LEAVE_CHANNEL, &mut seq,
|
||||
&wire::encode(&LeaveChannelPayload { channel_id })?, &crypto).await?;
|
||||
framing::write(&mut stream, PID_LEAVE_CHANNEL, &mut seq,
|
||||
&wire::encode(&LeaveChannelPayload { channel_id })?).await?;
|
||||
}
|
||||
Some(NetCommand::ChannelCreate { channel_id, channel_name, kind, guild_id }) => {
|
||||
let payload = wire::encode(&ChannelCreatePayload {
|
||||
|
|
@ -145,14 +163,14 @@ pub async fn session_loop(
|
|||
kind,
|
||||
guild_id,
|
||||
})?;
|
||||
framing::io::write_encrypted(&mut stream, PID_CHANNEL_CREATE, &mut seq, &payload, &crypto).await?;
|
||||
framing::write(&mut stream, PID_CHANNEL_CREATE, &mut seq, &payload).await?;
|
||||
}
|
||||
Some(NetCommand::ChannelDelete { channel_id }) => {
|
||||
let payload = wire::encode(&ChannelDeletePayload { channel_id })?;
|
||||
framing::io::write_encrypted(&mut stream, PID_CHANNEL_DELETE, &mut seq, &payload, &crypto).await?;
|
||||
framing::write(&mut stream, PID_CHANNEL_DELETE, &mut seq, &payload).await?;
|
||||
}
|
||||
Some(NetCommand::ChannelList) => {
|
||||
framing::io::write_encrypted(&mut stream, PID_CHANNEL_LIST, &mut seq, b"", &crypto).await?;
|
||||
framing::write(&mut stream, PID_CHANNEL_LIST, &mut seq, b"").await?;
|
||||
}
|
||||
Some(NetCommand::SendChat { channel_id, content, reply_to }) => {
|
||||
let msg = ChatMessagePayload {
|
||||
|
|
@ -162,16 +180,16 @@ pub async fn session_loop(
|
|||
edited: false,
|
||||
reply_to,
|
||||
};
|
||||
framing::io::write_encrypted(&mut stream, PID_CHAT_MESSAGE, &mut seq, &wire::encode(&msg)?, &crypto).await?;
|
||||
framing::write(&mut stream, PID_CHAT_MESSAGE, &mut seq, &wire::encode(&msg)?).await?;
|
||||
}
|
||||
Some(NetCommand::SendVoice { channel_id, voice_seq: voice_pkt_seq, timestamp, opus_data }) => {
|
||||
if let Some(voice)=voice_state.as_mut(){voice.send(Some(channel_id), voice_pkt_seq, timestamp, &opus_data).await?;}
|
||||
}
|
||||
Some(NetCommand::ReadReceipt { channel_id, last_read_message_id }) => {
|
||||
let payload = wire::encode(&ReadReceipt {channel_id,last_read_message_id,user_id: identity.pubkey_hex.clone()})?;
|
||||
framing::io::write_encrypted(&mut stream, PID_READ_RECEIPT, &mut seq, &payload, &crypto).await?;
|
||||
framing::write(&mut stream, PID_READ_RECEIPT, &mut seq, &payload).await?;
|
||||
}
|
||||
Some(other) => handle_cmd(other, &mut stream, &mut seq, &crypto, identity).await?,
|
||||
Some(other) => handle_cmd(other, &mut stream, &mut seq, identity).await?,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue