feat(security): 完善信令认证与媒体加密
- 增加信令访问令牌、HTTPS 校验、限流及跨网段中继连接修复 - 同步三端媒体 v2 协议,实现 AES-GCM、防重放和乱序分片重组,升级 Rust ABI 6 - 修复文件鉴权、权限协商及剪贴板越权,完善上传校验和串流超时处理 - 增加跨端回归测试及部署文档,在 AGENTS.md 记录真机与真实跨网段待验收项目
This commit is contained in:
1 parent
4c9d006d11
commit
f6b7730684
58 files changed
+2167
-218
No files matched your search
@@ -4,7 +4,7 @@
|
||||
//! 1. 被控端反连 TCP 41001,先发送 65 字节 ECDH P-256 公钥。
|
||||
//! 2. 主控端回送 65 字节自己的公钥;双方用 HKDF 派生 control/video/audio 三把 32 字节子密钥。
|
||||
//! 3. 后续控制帧使用 `SecureControlChannel`(8 字节明头 + 12 字节 nonce + 密文 + 16 字节 tag)。
|
||||
//! 4. 视频/音频通过 UDP 41000/41002 发送,使用 `MediaPackets` 的 13 字节头 + 4 字节 HMAC。
|
||||
//! 4. 视频/音频通过 UDP 41000/41002 发送,使用媒体 v2 AES-GCM、认证分片和防重放窗口。
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::io::{Read, Write};
|
||||
@@ -23,7 +23,7 @@ use crate::control::{ControlEvent, ControlEventKind};
|
||||
|
||||
use crate::control::SecureControlChannel;
|
||||
use crate::crypto::{derive_subkey, EphemeralKeyPair};
|
||||
use crate::media::decode_media_packet;
|
||||
use crate::media::receiver::MediaReceiver;
|
||||
use crate::session::relay::{self, RelayChannel, RelayMessage};
|
||||
use crate::session::tls::PinnedTlsStream;
|
||||
|
||||
@@ -186,6 +186,8 @@ struct Shared {
|
||||
control_key: Mutex<Option<[u8; 32]>>,
|
||||
video_key: Mutex<Option<[u8; 32]>>,
|
||||
audio_key: Mutex<Option<[u8; 32]>>,
|
||||
video_receiver: Mutex<MediaReceiver>,
|
||||
audio_receiver: Mutex<MediaReceiver>,
|
||||
sender: Mutex<Option<ControlSender>>,
|
||||
events: Mutex<Vec<String>>,
|
||||
inbound: Mutex<VecDeque<InboundEvent>>,
|
||||
@@ -204,6 +206,8 @@ impl Shared {
|
||||
control_key: Mutex::new(None),
|
||||
video_key: Mutex::new(None),
|
||||
audio_key: Mutex::new(None),
|
||||
video_receiver: Mutex::new(MediaReceiver::default()),
|
||||
audio_receiver: Mutex::new(MediaReceiver::default()),
|
||||
sender: Mutex::new(None),
|
||||
events: Mutex::new(Vec::new()),
|
||||
inbound: Mutex::new(VecDeque::new()),
|
||||
@@ -1334,15 +1338,27 @@ fn consume_media_packet(
|
||||
} else {
|
||||
&shared.audio_key
|
||||
};
|
||||
let key = match key_slot.lock().ok().and_then(|guard| *guard) {
|
||||
let key_guard = match key_slot.lock() { Ok(guard) => guard, Err(_) => return };
|
||||
let key = match key_guard.as_ref() {
|
||||
Some(key) => key,
|
||||
None => {
|
||||
shared.push_event(format!("error:{}:no-key", kind));
|
||||
return;
|
||||
}
|
||||
};
|
||||
match decode_media_packet(data, &key) {
|
||||
Ok(Some((packet, _))) => {
|
||||
let receiver_slot = if kind == "video" { &shared.video_receiver } else { &shared.audio_receiver };
|
||||
let result = match receiver_slot.lock() {
|
||||
Ok(mut receiver) => receiver.receive(key, data),
|
||||
Err(_) => return,
|
||||
};
|
||||
// Keep the key slot locked through queue publication so an old session's
|
||||
// final packet cannot be enqueued after a concurrent rekey clears queues.
|
||||
match result {
|
||||
Ok(Some(packet)) => {
|
||||
if (kind == "audio") != (packet.flags & crate::media::FLAG_AUDIO != 0) {
|
||||
shared.push_event(format!("error:{}:wrong-stream", kind));
|
||||
return;
|
||||
}
|
||||
if kind == "video" {
|
||||
if let Some((pts, flags, payload)) =
|
||||
reassembler.push(packet.pts, packet.flags, &packet.payload)
|
||||
@@ -1376,12 +1392,12 @@ fn consume_media_packet(
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(None) => shared.push_event(format!("error:{}:incomplete", kind)),
|
||||
Ok(None) => {}, // authenticated, awaiting the remaining fragments
|
||||
Err(error) => shared.push_event(format!("error:{}:{}", kind, error)),
|
||||
}
|
||||
}
|
||||
|
||||
/// 中继媒体分片在直连格式(头+负载+MAC)外又套了一层 AES-256-GCM:
|
||||
/// 中继媒体分片在 v2 已加密媒体包外再套一层 AES-256-GCM:
|
||||
/// `[nonce:12][密文][tag:16]`,AAD 固定为 `relay-media`。
|
||||
fn decrypt_relay_media(key: &[u8; 32], data: &[u8]) -> Option<Vec<u8>> {
|
||||
if data.len() < crate::crypto::NONCE_LEN + crate::crypto::TAG_LEN {
|
||||
|
||||
Reference in new issue
Block a user