feat(server): secure chat, as vanilla 26.3's server

Chat session updates with their profile key checked against the services'
keys, signed chat and the message chain, last-seen acknowledgements, the
signature cache, signed say/me/msg, enforce-secure-profile, and vanilla's
log lines. Offline servers and players without a chat session chat
unsigned, as on vanilla. tools/test_secure_chat_parity.py compares both
servers with test keys and a mock services server.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-18 07:28:35 +02:00
parent 5b62d771bb
commit 454b3842bb
23 changed files with 2505 additions and 86 deletions
Generated
+2
View File
@@ -1539,6 +1539,7 @@ dependencies = [
"horizon-world",
"serde_json",
"sha1",
"sha2",
]
[[package]]
@@ -1569,6 +1570,7 @@ dependencies = [
name = "horizon-server-files"
version = "0.1.0"
dependencies = [
"base64",
"horizon-core",
"horizon-protocol",
"horizon-world",
+3 -1
View File
@@ -128,7 +128,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
eprintln!("[lan] {packet:?}");
}
}
HostEvent::Chat(line) | HostEvent::Warn(line) => println!("{line}"),
HostEvent::Chat { line, .. } | HostEvent::Warn(line) | HostEvent::Error(line) => {
println!("{line}")
}
HostEvent::Left(_) => {}
}
}
+1
View File
@@ -64,6 +64,7 @@ mod item_particles;
mod jukebox;
mod lan;
mod lan_actions;
mod lan_chat;
mod lan_crafting;
mod lan_entities;
mod lan_mobs;
+13 -2
View File
@@ -473,6 +473,18 @@ impl Game<'_> {
// DedicatedServer.initServer: names nobody has resolve offline only without authentication.
cache.resolve_offline_users = !props.online_mode;
*USER_CACHE.lock().unwrap() = Some(cache);
// Services.create: the services' public keys, fetched in the
// background (they check players' chat keys).
let secure_chat = Arc::new(horizon_client::lan::SecureChat::new(
props.enforce_secure_profile && props.online_mode,
|| {
let discovery = profiles::discovery_url();
profiles::fetch_profile_key_keys(&discovery).unwrap_or_else(|e| {
::log::error!("{e}");
Vec::new()
})
},
));
log("INFO", "Starting minecraft server version 26.3");
log("INFO", "Loading properties");
let game_mode = props.game_mode;
@@ -571,8 +583,7 @@ impl Game<'_> {
enable_status: props.enable_status,
hide_online_players: props.hide_online_players,
favicon,
// DedicatedServer.enforceSecureProfile needs online mode.
enforces_secure_chat: false,
secure_chat: Some(secure_chat),
accepts_transfers: props.accepts_transfers,
announce: false,
listen: true,
+24 -9
View File
@@ -855,6 +855,9 @@ impl Game<'_> {
}
for event in events {
match event {
event @ (HostEvent::Chat { .. } | HostEvent::Warn(_) | HostEvent::Error(_)) => {
self.lan_log_event(event);
}
HostEvent::Joined(conn) => {
self.lan_guest_joined(&mut host, conn, state.as_ref(), &entities, spawn);
if self.options.dedicated.is_some()
@@ -911,15 +914,6 @@ impl Game<'_> {
);
}
}
// MinecraftServer.logChatMessage of an unsigned message.
HostEvent::Chat(line) if self.options.dedicated.is_some() => {
super::dedicated::log("INFO", &format!("[Not Secure] {line}"));
}
HostEvent::Chat(line) => self.system_message(Text::literal(line)),
HostEvent::Warn(line) if self.options.dedicated.is_some() => {
super::dedicated::log("WARN", &line);
}
HostEvent::Warn(line) => log::warn!("{line}"),
HostEvent::Packet(conn, packet) => {
self.lan_guest_packet(&mut host, conn, *packet);
}
@@ -1209,3 +1203,24 @@ impl Game<'_> {
}
}
}
impl Game<'_> {
/// A hub's chat and log lines: a dedicated server logs them
/// (MinecraftServer.logChatMessage, `[Not Secure]` for unsigned or
/// expired messages); a LAN host shows the chat.
pub(super) fn lan_log_event(&mut self, event: HostEvent) {
let dedicated = self.options.dedicated.is_some();
match event {
HostEvent::Chat { line, secure } if dedicated => {
let tag = if secure { "" } else { "[Not Secure] " };
super::dedicated::log("INFO", &format!("{tag}{line}"));
}
HostEvent::Chat { line, .. } => self.system_message(Text::literal(line)),
HostEvent::Warn(line) if dedicated => super::dedicated::log("WARN", &line),
HostEvent::Warn(line) => log::warn!("{line}"),
HostEvent::Error(line) if dedicated => super::dedicated::log("ERROR", &line),
HostEvent::Error(line) => log::error!("{line}"),
_ => {}
}
}
}
+14 -8
View File
@@ -639,7 +639,8 @@ impl Game<'_> {
self.drop_guest_items(host, gi, vec![c]);
}
}
S::ChatCommand(c) => self.guest_command(host, conn, &c.command),
S::ChatCommand(c) => self.guest_chat_command(host, conn, &c.command),
S::ChatCommandSigned(p) => self.guest_signed_command(host, conn, p),
S::ChangeDifficulty(p) if host.hub.guests[gi].permission >= 2 => {
if !self.level.difficulty_locked && (0..=3).contains(&p.difficulty) {
self.ui.difficulty = if self.level.hardcore {
@@ -731,12 +732,6 @@ impl Game<'_> {
self.send_admin_feedback(host, Some(conn));
}
}
S::Unknown(8, payload) => {
// A signed command: the text comes first.
if let Ok(text) = horizon_protocol::buf::Reader::new(&payload).string(32767) {
self.guest_command(host, conn, &text);
}
}
S::Unknown(id, payload)
if id == horizon_protocol::ids::play::serverbound::COMMAND_SUGGESTION =>
{
@@ -861,7 +856,18 @@ impl Game<'_> {
}
/// Commands.performCommand for a guest with command access.
fn guest_command(&mut self, host: &mut LanHost, conn: u32, command: &str) {
/// Commands.performCommand for a guest, with the message arguments it
/// signed (none after an unsigned command packet).
pub(super) fn guest_command(
&mut self,
host: &mut LanHost,
conn: u32,
command: &str,
signed: Option<Vec<(String, horizon_client::lan::hub::chat::ChatMessage)>>,
) {
if self.guest_message_command(host, conn, command, signed.as_ref()) {
return;
}
// ListPlayersCommand needs no permission and lists every player.
let words: Vec<&str> = command.trim_start_matches('/').split(' ').collect();
if words[0] == "list" && (words.len() == 1 || words[1..] == ["uuids"]) {
+194
View File
@@ -0,0 +1,194 @@
//! A guest's commands that carry chat: the unsigned and signed command
//! packets (ServerGamePacketListenerImpl.handleChatCommand and
//! handleSignedChatCommand), and the message commands whose text they sign
//! (SayCommand, EmoteCommands, MsgCommand), relayed by the hub as chat.
use super::{Game, lan::LanHost};
use horizon_client::{
command::{Arg, ArgType, NodeKind, ParseResults, vanilla},
lan::{
data::component_nbt,
hub::{HostEvent, chat::ChatMessage},
},
text::{self, Text},
};
use horizon_protocol::packets::play::{self, Clientbound as C};
/// The signing context of a command: each message argument as the chain
/// let it through, by argument name (CommandSigningContext.SignedArguments).
type Signed = Vec<(String, ChatMessage)>;
impl Game<'_> {
/// CommandDispatcher.parse with the guest's permission level.
fn parse_as(&mut self, permission: u8, command: &str) -> ParseResults {
let level = std::mem::replace(&mut self.ui.registries.permission, permission);
let parse = vanilla::game_dispatcher().parse(command, &self.ui.registries);
self.ui.registries.permission = level;
parse
}
/// SignableCommand.of: the command's message arguments, by name, with
/// their text, not past a redirect to the root (`execute ... run`).
fn signable_arguments(&mut self, permission: u8, command: &str) -> Vec<(String, String)> {
let parse = self.parse_as(permission, command);
let nodes = &vanilla::game_dispatcher().nodes;
let root = parse.context.root;
let mut out = Vec::new();
let mut context = Some(&parse.context);
while let Some(c) = context {
for &(node, (start, end)) in &c.nodes {
if let NodeKind::Argument {
ty: ArgType::Message,
name,
} = &nodes[node].kind
&& let Some(text) = command.get(start..end)
{
out.push((name.clone(), text.to_owned()));
}
}
context = c.child.as_deref().filter(|child| child.root != root);
}
out
}
/// handleChatCommand (performUnsignedChatCommand): refused when the
/// server enforces secure chat and the command has a message to sign.
pub(super) fn guest_chat_command(&mut self, host: &mut LanHost, conn: u32, command: &str) {
let Some(i) = host.hub.guests.iter().position(|g| g.conn == conn) else {
return;
};
if !host.hub.try_handle_chat(i, command, true) {
return;
}
let permission = host.hub.guests[i].permission;
if host.hub.enforces_secure_chat()
&& !self.signable_arguments(permission, command).is_empty()
{
let name = host.hub.guests[i].name.clone();
self.lan_log_event(HostEvent::Error(format!(
"Received unsigned command packet from {name}, but the command requires signable arguments: {command}"
)));
let failure = Text::translate("chat.disabled.invalid_command_signature", &[])
.with_color(text::RED);
host.hub.guests[i].send(&C::SystemChat(play::SystemChat {
content: component_nbt(&failure.component()),
overlay: false,
}));
return;
}
self.guest_command(host, conn, command, None);
}
/// handleSignedChatCommand: the acknowledgements, then each message
/// argument through the sender's chain, then the command with them.
pub(super) fn guest_signed_command(
&mut self,
host: &mut LanHost,
conn: u32,
p: play::ChatCommandSigned,
) {
let Some(i) = host.hub.guests.iter().position(|g| g.conn == conn) else {
return;
};
let mut events = Vec::new();
let mut signed = None;
if let Some(last_seen) = host.hub.apply_last_seen(i, &p.last_seen, &mut events)
&& host.hub.try_handle_chat(i, &p.command, true)
{
let permission = host.hub.guests[i].permission;
let parsed = self.signable_arguments(permission, &p.command);
match host
.hub
.signed_arguments(i, &p, &parsed, &last_seen, &mut events)
{
Ok(arguments) => signed = Some(arguments),
Err(e) => host.hub.decode_failure(i, e, &mut events),
}
}
for event in events {
self.lan_log_event(event);
}
if let Some(signed) = signed {
self.guest_command(host, conn, &p.command, Some(signed));
}
}
/// SayCommand, EmoteCommands and MsgCommand for a guest: chat to the
/// players, signed when the guest signed it (a disguised chat after an
/// unsigned command packet). False when the command is another one, or
/// one this can't run (selectors other than `@s` and `@a`), for the
/// game's dispatcher to handle.
pub(super) fn guest_message_command(
&mut self,
host: &mut LanHost,
conn: u32,
command: &str,
signed: Option<&Signed>,
) -> bool {
let Some(i) = host.hub.guests.iter().position(|g| g.conn == conn) else {
return false;
};
let parse = self.parse_as(host.hub.guests[i].permission, command);
let Ok((context, id)) = parse.executable() else {
return false;
};
let argument = if id == "me" { "action" } else { "message" };
let (Some(Arg::Str(text)), true) =
(context.get(argument), matches!(id, "say" | "me" | "msg"))
else {
return false;
};
let message = signed.and_then(|s| {
s.iter()
.find(|(n, _)| n == argument)
.map(|(_, m)| m.clone())
});
let mut events = Vec::new();
if id == "msg" {
let Some(Arg::Entity(selector)) = context.get("targets") else {
return false;
};
let raw = context
.args
.iter()
.rev()
.find(|a| a.name == "targets")
.and_then(|a| command.get(a.range.0..a.range.1));
let guests = &host.hub.guests;
let placed = |k: &usize| guests[*k].placed;
let targets: Vec<usize> = match (raw, &selector.player_name, selector.uuid) {
(Some("@s"), ..) => vec![i],
(Some("@a"), ..) => (0..guests.len()).filter(placed).collect(),
(_, Some(name), _) => (0..guests.len())
.filter(|&k| placed(&k) && guests[k].name.eq_ignore_ascii_case(name))
.collect(),
(_, None, Some(uuid)) => (0..guests.len())
.filter(|&k| placed(&k) && guests[k].uuid == uuid)
.collect(),
_ => return false,
};
if targets.is_empty() {
let failure = Text::empty()
.append(Text::translate("argument.entity.notfound.player", &[]))
.with_color(text::RED);
host.hub.guests[i].send(&C::SystemChat(play::SystemChat {
content: component_nbt(&failure.component()),
overlay: false,
}));
return true;
}
host.hub.whisper(i, &targets, message, text);
} else {
let chat_type = if id == "say" {
"say_command"
} else {
"emote_command"
};
host.hub
.broadcast_command_chat(i, chat_type, message, text, &mut events);
}
for event in events {
self.lan_log_event(event);
}
true
}
}
+9
View File
@@ -37,6 +37,12 @@ pub struct Guest {
pub last_action_tick: u64,
/// The next ClientboundPlayerChatPacket.globalIndex for this connection.
pub chat_index: i32,
/// Secure chat (ServerGamePacketListenerImpl): the player's message
/// chain, keyed once it sends a chat session; the messages its
/// acknowledgements cover; the signatures its client caches.
pub chain: horizon_protocol::secure_chat::Chain,
pub last_seen: horizon_protocol::secure_chat::LastSeenValidator,
pub signatures: horizon_protocol::secure_chat::SignatureCache,
/// The hub tick the player was placed on: chunks go out from the tick
/// after next, behind the first doTick's packets, as vanilla's do.
pub placed_at: u64,
@@ -188,6 +194,9 @@ impl Guest {
address: j.address,
last_action_tick: 0,
chat_index: 0,
chain: Default::default(),
last_seen: Default::default(),
signatures: Default::default(),
placed_at: 0,
center_sent: None,
left_reason: String::new(),
+26 -53
View File
@@ -33,6 +33,8 @@ use horizon_world::{
use std::sync::{Arc, atomic::Ordering};
/// GameEvent types (ClientboundGameEventPacket).
pub mod chat;
pub mod game_event {
pub const START_RAINING: u8 = 1;
pub const STOP_RAINING: u8 = 2;
@@ -108,10 +110,16 @@ pub enum HostEvent {
/// A guest left (state to save, entities to drop).
Left(Box<Guest>),
Packet(u32, Box<S>),
/// A chat line to show on the host (already broadcast to the guests).
Chat(String),
/// A chat line to show on the host (already broadcast to the guests);
/// a dedicated server logs it, `[Not Secure]` unless signed and fresh.
Chat {
line: String,
secure: bool,
},
/// A server log warning (ServerGamePacketListenerImpl's LOGGER.warn).
Warn(String),
/// A server log error (LOGGER.error).
Error(String),
}
pub struct Hub {
@@ -411,56 +419,23 @@ impl Hub {
},
}));
}
/// PlayerList.broadcastChatMessage of a guest's unsigned message
/// (PlayerChatMessage.unsigned): a ClientboundPlayerChatPacket to every
/// player, each connection numbering its messages.
pub fn player_chat(&mut self, from: usize, text: &str) {
let (uuid, name) = (self.guests[from].uuid, self.guests[from].name.clone());
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_millis() as i64);
let bound = play::ChatTypeBound {
chat_type: self.chat_type,
name: display_name(uuid, &name),
target: None,
};
for g in self.guests.iter_mut().filter(|g| g.placed) {
let packet = C::PlayerChat(play::PlayerChat {
global_index: g.chat_index,
sender: uuid,
index: 0,
signature: None,
content: text.to_owned(),
timestamp,
salt: 0,
last_seen: Vec::new(),
unsigned_content: None,
filter: (0, Vec::new()),
chat_type: bound.clone(),
});
g.chat_index += 1;
g.send(&packet);
}
}
/// Disconnect a guest with a reason (ServerGamePacketListenerImpl.disconnect).
pub fn kick(&mut self, conn: u32, reason: Tag) {
if let Some(g) = self.guest(conn) {
// The log's "lost connection" line gives the reason, as vanilla's.
let text = self.english(&reason);
if let Some(g) = self.guest_mut(conn) {
g.left_reason = text;
g.sender.disconnect(reason);
}
}
fn player_info(&self, uuid: u128, name: &str, mode: i32, latency: i32) -> play::PlayerInfo {
let guest = self.guests.iter().find(|g| g.uuid == uuid);
play::PlayerInfo {
uuid,
name: Some(name.to_owned()),
properties: self
.guests
.iter()
.find(|g| g.uuid == uuid)
.map(|g| g.properties.clone())
.unwrap_or_default(),
chat_session: None,
properties: guest.map(|g| g.properties.clone()).unwrap_or_default(),
chat_session: guest.and_then(|g| g.chain.session()).map(|s| s.data()),
game_mode: Some(mode),
listed: Some(true),
latency: Some(latency),
@@ -553,8 +528,9 @@ impl Hub {
show_death_screen: true,
limited_crafting: false,
spawn: self.spawn_info(mode),
online_mode: false,
enforces_secure_chat: false,
// MinecraftServer.usesAuthentication, enforceSecureProfile.
online_mode: self.lan.shared.config.online.is_some(),
enforces_secure_chat: self.enforces_secure_chat(),
}));
g.send(&C::ChangeDifficulty(play::ChangeDifficulty {
difficulty: ctx.difficulty,
@@ -1056,7 +1032,9 @@ impl Hub {
fn remove_guest(&mut self, i: usize, reason: &str) -> Guest {
let mut g = self.guests.remove(i);
g.left_reason = reason.to_owned();
if g.left_reason.is_empty() {
g.left_reason = reason.to_owned();
}
if std::env::var_os("HORIZON_NET_DEBUG").is_some() {
log::info!("[lan] {} left: {reason}", g.name);
}
@@ -1807,14 +1785,9 @@ impl Hub {
0 => self.wake(i),
_ => {}
},
S::Chat(c) => {
let text: String = c.message.chars().filter(|c| !c.is_control()).collect();
if !text.is_empty() {
let name = self.guests[i].name.clone();
self.player_chat(i, &text);
events.push(HostEvent::Chat(format!("<{name}> {text}")));
}
}
S::Chat(c) => self.handle_chat(i, c, events),
S::ChatAck(p) => self.chat_ack(i, p.offset, events),
S::ChatSessionUpdate(p) => self.chat_session_update(i, p, events),
other => events.push(HostEvent::Packet(conn, Box::new(other))),
}
}
+500
View File
@@ -0,0 +1,500 @@
//! Chat on the server side, as ServerGamePacketListenerImpl and PlayerList
//! handle it: chat sessions validated against the services' keys, signed
//! messages checked link by link, the acknowledged window, and messages
//! relayed with each connection's numbering and signature cache.
use super::{HostEvent, Hub, display_name};
use horizon_protocol::{
packets::play::{self, Clientbound as C},
secure_chat::{self, Body, Chain, ChatSession, DecodeError, ProfileKey, Signature, Unpacked},
text::{literal, translatable},
};
use horizon_world::nbt::{JavaString, Tag};
/// Now, in epoch milliseconds (Instant.now()).
pub fn now_millis() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_millis() as i64)
}
/// StringUtil.isAllowedChatCharacter.
fn allowed(c: char) -> bool {
c != '\u{a7}' && c >= ' ' && c != '\u{7f}'
}
/// A translatable message in red.
fn red(key: &str) -> Tag {
let mut t = translatable(key, &[]);
if let Tag::Compound(map) = &mut t {
map.insert(
JavaString::from("color"),
Tag::String(JavaString::from("red")),
);
}
t
}
/// PlayerChatMessage as the server relays it.
#[derive(Clone, Debug, PartialEq)]
pub struct ChatMessage {
pub sender: u128,
/// The link's index in the sender's chain (0 when unsigned).
pub index: i32,
pub signature: Option<Signature>,
pub content: String,
/// Epoch milliseconds.
pub timestamp: i64,
pub salt: i64,
pub last_seen: Vec<Signature>,
/// What clients show instead of the signed text (withUnsignedContent).
pub unsigned_content: Option<Tag>,
}
impl ChatMessage {
/// The message a chain let through: a signed one keeps its body;
/// PlayerChatMessage.unsigned takes the time now, no salt, nothing seen.
pub fn new(sender: u128, unpacked: Unpacked, body: &Body, now: i64) -> Self {
let (index, signature, timestamp, salt, last_seen) = match unpacked {
Unpacked::Unsigned => (0, None, now, 0, Vec::new()),
Unpacked::Signed { index, signature } => (
index,
Some(signature),
body.timestamp,
body.salt,
body.last_seen.to_vec(),
),
};
Self {
sender,
index,
signature,
content: body.content.to_owned(),
timestamp,
salt,
last_seen,
unsigned_content: None,
}
}
/// hasExpiredServer.
pub fn expired(&self, now: i64) -> bool {
now > self.timestamp + secure_chat::EXPIRES_AFTER_SERVER
}
/// PlayerList.verifyChatTrusted: signed and not expired.
pub fn trusted(&self, now: i64) -> bool {
self.signature.is_some() && !self.expired(now)
}
}
impl Hub {
/// MinecraftServer.enforceSecureProfile.
pub fn enforces_secure_chat(&self) -> bool {
self.lan.shared.config.enforces_secure_chat()
}
/// Component.getString in the server's language, for the log.
pub(crate) fn english(&self, tag: &Tag) -> String {
match &self.lan.shared.config.log {
Some(log) => horizon_protocol::text::plain(tag, &log.lang),
None => horizon_protocol::text::plain(tag, &Default::default()),
}
}
/// tryHandleChat: an illegal character disconnects; with chat hidden a
/// message (not a command) is refused. Whether to go on.
pub fn try_handle_chat(&mut self, i: usize, message: &str, command: bool) -> bool {
let conn = self.guests[i].conn;
if !message.chars().all(allowed) {
self.kick(
conn,
translatable("multiplayer.disconnect.illegal_characters", &[]),
);
return false;
}
// ChatVisiblity.HIDDEN.
if !command && self.guests[i].info.chat_visibility == 2 {
self.guests[i].send(&C::SystemChat(play::SystemChat {
content: red("chat.disabled.options"),
overlay: false,
}));
return false;
}
true
}
/// unpackAndApplyLastSeen: the acknowledged messages, or a disconnect.
pub fn apply_last_seen(
&mut self,
i: usize,
update: &play::LastSeenUpdate,
events: &mut Vec<HostEvent>,
) -> Option<Vec<Signature>> {
let g = &mut self.guests[i];
match g
.last_seen
.apply_update(update.offset, update.acknowledged, update.checksum)
{
Ok(seen) => Some(seen),
// Vanilla logs the ValidationException's stack trace after the line.
Err(_) => {
events.push(HostEvent::Error(format!(
"Failed to validate message acknowledgements from {}",
g.name
)));
let conn = g.conn;
self.kick(
conn,
translatable("multiplayer.disconnect.chat_validation_failed", &[]),
);
None
}
}
}
/// handleChatAck.
pub(super) fn chat_ack(&mut self, i: usize, offset: i32, events: &mut Vec<HostEvent>) {
let g = &mut self.guests[i];
if g.last_seen.apply_offset(offset).is_err() {
events.push(HostEvent::Error(format!(
"Failed to validate message acknowledgement offset from {}",
g.name
)));
let conn = g.conn;
self.kick(
conn,
translatable("multiplayer.disconnect.chat_validation_failed", &[]),
);
}
}
/// handleMessageDecodeFailure: logged, and the reason in red for the player.
pub fn decode_failure(&mut self, i: usize, e: DecodeError, events: &mut Vec<HostEvent>) {
let reason = red(e.key());
events.push(HostEvent::Warn(format!(
"Failed to update secure chat state for {}: '{}'",
self.guests[i].name,
self.english(&reason)
)));
self.guests[i].send(&C::SystemChat(play::SystemChat {
content: reason,
overlay: false,
}));
}
/// handleChat: the acknowledgements, then the message through the
/// sender's chain, then to everyone.
pub(super) fn handle_chat(&mut self, i: usize, c: play::Chat, events: &mut Vec<HostEvent>) {
let Some(last_seen) = self.apply_last_seen(i, &c.last_seen, events) else {
return;
};
if !self.try_handle_chat(i, &c.message, false) {
return;
}
let (enforce, now) = (self.enforces_secure_chat(), now_millis());
let body = Body {
content: &c.message,
timestamp: c.timestamp,
salt: c.salt,
last_seen: &last_seen,
};
let signature = c.signature.as_deref().and_then(secure_chat::signature);
let g = &mut self.guests[i];
let (uuid, name) = (g.uuid, g.name.clone());
let message = match g
.chain
.unpack(uuid, signature.as_ref(), &body, enforce, now)
{
Ok(unpacked) => ChatMessage::new(uuid, unpacked, &body, now),
Err(e) => return self.decode_failure(i, e, events),
};
if message.signature.is_some() && message.expired(now) {
events.push(HostEvent::Warn(format!(
"Received expired chat: '{}'. Is the client/server system time unsynchronized?",
message.content
)));
}
let bound = play::ChatTypeBound {
chat_type: self.chat_type,
name: display_name(uuid, &name),
target: None,
};
let line = format!("<{name}> {}", message.content);
self.broadcast_chat(&message, &bound, line, events);
}
/// handleChatSessionUpdate: a new profile key, validated against the
/// services' keys, starts a new chain and is announced to everyone.
pub(super) fn chat_session_update(
&mut self,
i: usize,
p: play::ChatSessionUpdate,
events: &mut Vec<HostEvent>,
) {
let key = ProfileKey {
expires_at: p.expires_at,
key: p.key,
signature: p.key_signature,
};
let g = &self.guests[i];
let (conn, uuid) = (g.conn, g.uuid);
let old = g.chain.session().map(|s| s.profile_key.expires_at);
if g.chain.session().is_some_and(|s| s.profile_key == key) {
return;
}
if old.is_some_and(|old| key.expires_at < old) {
self.kick(
conn,
translatable("multiplayer.disconnect.expired_public_key", &[]),
);
return;
}
let secure = self.lan.shared.config.secure_chat.clone();
let keys = secure.as_deref().map_or(&[][..], |s| s.keys());
if keys.is_empty() {
events.push(HostEvent::Warn(format!(
"Ignoring chat session from {} due to missing Services public key",
g.name
)));
return;
}
match ChatSession::validate(p.session, key, keys, uuid) {
Ok(session) => {
let data = session.data();
self.guests[i].chain = Chain::keyed(session);
// resetPlayerChatState: INITIALIZE_CHAT to every player.
self.broadcast(&C::PlayerInfoUpdate(play::PlayerInfoUpdate {
actions: 2,
entries: vec![play::PlayerInfo {
uuid,
chat_session: Some(data),
..Default::default()
}],
}));
}
Err(reason) => {
events.push(HostEvent::Error(
"Failed to validate profile key".to_owned(),
));
self.kick(conn, translatable(reason, &[]));
}
}
}
/// PlayerList.broadcastChatMessage: logged (`[Not Secure]` unless signed
/// and fresh), then sent to every player.
pub fn broadcast_chat(
&mut self,
message: &ChatMessage,
bound: &play::ChatTypeBound,
line: String,
events: &mut Vec<HostEvent>,
) {
let secure = message.trusted(now_millis());
events.push(HostEvent::Chat { line, secure });
for k in 0..self.guests.len() {
if self.guests[k].placed {
self.send_chat(k, message, bound);
}
}
}
/// ServerPlayer.sendChatMessage (players who show chat) and
/// sendPlayerChatMessage: numbered for the connection, the last seen
/// packed against its cache; a signed message joins the cache and the
/// messages the player must acknowledge.
pub fn send_chat(&mut self, k: usize, message: &ChatMessage, bound: &play::ChatTypeBound) {
let g = &mut self.guests[k];
if g.info.chat_visibility != 0 {
return;
}
let last_seen = message
.last_seen
.iter()
.map(|s| g.signatures.pack(s))
.collect();
g.send(&C::PlayerChat(play::PlayerChat {
global_index: g.chat_index,
sender: message.sender,
index: message.index,
signature: message.signature.map(|s| s.to_vec()),
content: message.content.clone(),
timestamp: message.timestamp,
salt: message.salt,
last_seen,
unsigned_content: message.unsigned_content.clone(),
filter: (0, Vec::new()),
chat_type: bound.clone(),
}));
g.chat_index += 1;
if let Some(signature) = &message.signature {
g.signatures.push(&message.last_seen, Some(signature));
g.last_seen.add_pending(signature);
if g.last_seen.tracked_count() > 4096 {
let conn = g.conn;
self.kick(
conn,
translatable("multiplayer.disconnect.too_many_pending_chats", &[]),
);
}
}
}
/// sendChatMessage of a system message: a disguised chat, to players
/// who show chat.
fn send_disguised(&self, k: usize, message: Tag, chat_type: play::ChatTypeBound) {
let g = &self.guests[k];
if g.info.chat_visibility == 0 {
g.send(&C::DisguisedChat(play::DisguisedChat {
message,
chat_type,
}));
}
}
/// collectSignedArguments, or collectUnsignedArguments when the client
/// signed none: each of the command's message arguments (`parsed`, by
/// name) through the sender's chain.
pub fn signed_arguments(
&mut self,
i: usize,
p: &play::ChatCommandSigned,
parsed: &[(String, String)],
last_seen: &[Signature],
events: &mut Vec<HostEvent>,
) -> Result<Vec<(String, ChatMessage)>, DecodeError> {
let (enforce, now) = (self.enforces_secure_chat(), now_millis());
let g = &mut self.guests[i];
let uuid = g.uuid;
let mut out = Vec::new();
if p.arguments.is_empty() {
for (name, value) in parsed {
// SignedMessageBody.unsigned.
let body = Body {
content: value,
timestamp: now,
salt: 0,
last_seen: &[],
};
let unpacked = g.chain.unpack(uuid, None, &body, enforce, now)?;
out.push((name.clone(), ChatMessage::new(uuid, unpacked, &body, now)));
}
return Ok(out);
}
let mismatch = |events: &mut Vec<HostEvent>| {
let names = |l: &mut dyn Iterator<Item = &String>| {
l.map(String::as_str).collect::<Vec<_>>().join(", ")
};
events.push(HostEvent::Error(format!(
"Signed command mismatch between server and client ('{}'): got [{}] from client, but expected [{}]",
p.command,
names(&mut p.arguments.iter().map(|a| &a.0)),
names(&mut parsed.iter().map(|a| &a.0)),
)));
DecodeError::InvalidCommandSignature
};
for (name, signature) in &p.arguments {
let Some((_, value)) = parsed.iter().find(|(n, _)| n == name) else {
g.chain.set_broken();
return Err(mismatch(events));
};
let body = Body {
content: value,
timestamp: p.timestamp,
salt: p.salt,
last_seen,
};
let signature = secure_chat::signature(signature);
let unpacked = g
.chain
.unpack(uuid, signature.as_ref(), &body, enforce, now)?;
out.push((name.clone(), ChatMessage::new(uuid, unpacked, &body, now)));
}
if parsed.iter().any(|(n, _)| !out.iter().any(|(o, _)| o == n)) {
return Err(mismatch(events));
}
Ok(out)
}
/// A message command's chat (SayCommand, EmoteCommands): to everyone,
/// bound to `chat_type` and the sender's name.
pub fn broadcast_command_chat(
&mut self,
i: usize,
chat_type: &str,
message: Option<ChatMessage>,
text: &str,
events: &mut Vec<HostEvent>,
) {
let (uuid, name) = (self.guests[i].uuid, self.guests[i].name.clone());
let bound = play::ChatTypeBound {
chat_type: self.chat_type_id(chat_type),
name: display_name(uuid, &name),
target: None,
};
let line = match chat_type {
"say_command" => format!("[{name}] {text}"),
_ => format!("* {name} {text}"),
};
match message {
Some(message) => self.broadcast_chat(&message, &bound, line, events),
// An unsigned command packet (no signing context):
// PlayerChatMessage.system, a disguised chat.
None => {
events.push(HostEvent::Chat {
line,
secure: false,
});
for k in 0..self.guests.len() {
if self.guests[k].placed {
self.send_disguised(k, literal(text), bound.clone());
}
}
}
}
}
/// MsgCommand.sendMessage to each target: the outgoing line for the
/// sender, the incoming one for the target. Nothing is logged.
pub fn whisper(
&mut self,
i: usize,
targets: &[usize],
message: Option<ChatMessage>,
text: &str,
) {
let (uuid, name) = (self.guests[i].uuid, self.guests[i].name.clone());
let (outgoing, incoming) = (
self.chat_type_id("msg_command_outgoing"),
self.chat_type_id("msg_command_incoming"),
);
for &t in targets {
let target = display_name(self.guests[t].uuid, &self.guests[t].name);
let out = play::ChatTypeBound {
chat_type: outgoing,
name: display_name(uuid, &name),
target: Some(target),
};
let inc = play::ChatTypeBound {
chat_type: incoming,
name: display_name(uuid, &name),
target: None,
};
match &message {
Some(m) => {
self.send_chat(i, m, &out);
self.send_chat(t, m, &inc);
}
None => {
self.send_disguised(i, literal(text), out);
self.send_disguised(t, literal(text), inc);
}
}
}
}
/// A chat type's registry id.
fn chat_type_id(&self, name: &str) -> i32 {
self.sync
.id("minecraft:chat_type", &format!("minecraft:{name}"))
.unwrap_or(self.chat_type)
}
}
+48 -4
View File
@@ -122,8 +122,8 @@ pub struct NetConfig {
pub hide_online_players: bool,
/// server-icon.png (64x64 PNG bytes).
pub favicon: Option<Vec<u8>>,
/// MinecraftServer.enforceSecureProfile.
pub enforces_secure_chat: bool,
/// Secure chat (a dedicated server; none on a LAN world).
pub secure_chat: Option<Arc<SecureChat>>,
pub accepts_transfers: bool,
/// LanServerPinger: announce the world on the local network.
pub announce: bool,
@@ -138,6 +138,10 @@ pub struct NetConfig {
pub log: Option<NetLog>,
}
impl NetConfig {
/// MinecraftServer.enforceSecureProfile.
pub fn enforces_secure_chat(&self) -> bool {
self.secure_chat.as_ref().is_some_and(|s| s.enforced())
}
/// IntegratedServer.publishServer.
pub fn lan(port: u16, motd: String, max_players: usize) -> Self {
Self {
@@ -149,7 +153,7 @@ impl NetConfig {
enable_status: true,
hide_online_players: false,
favicon: None,
enforces_secure_chat: false,
secure_chat: None,
accepts_transfers: false,
announce: true,
listen: true,
@@ -161,6 +165,46 @@ impl NetConfig {
}
}
/// A dedicated server's secure chat (DedicatedServer.enforceSecureProfile,
/// Services.profileKeySignatureValidator): the services' profile-key keys,
/// fetched in the background from startup and waited for by their first
/// use, as authlib's RetryableFetch does.
pub struct SecureChat {
/// enforce-secure-profile, in online mode.
pub enforce: bool,
keys: std::sync::OnceLock<Vec<horizon_protocol::rsa::RsaPublicKey>>,
fetch: Mutex<Option<JoinHandle<Vec<horizon_protocol::rsa::RsaPublicKey>>>>,
}
impl SecureChat {
pub fn new(
enforce: bool,
fetch: impl FnOnce() -> Vec<horizon_protocol::rsa::RsaPublicKey> + Send + 'static,
) -> Self {
let handle = std::thread::Builder::new()
.name("Services keys".into())
.spawn(fetch)
.ok();
Self {
enforce,
keys: std::sync::OnceLock::new(),
fetch: Mutex::new(handle),
}
}
/// The keys players' chat keys must be signed by (none when the
/// services couldn't be reached).
pub fn keys(&self) -> &[horizon_protocol::rsa::RsaPublicKey] {
self.keys.get_or_init(|| {
let handle = self.fetch.lock().ok().and_then(|mut h| h.take());
handle.and_then(|h| h.join().ok()).unwrap_or_default()
})
}
/// enforceSecureProfile: enforced, and able to check keys.
pub fn enforced(&self) -> bool {
self.enforce && !self.keys().is_empty()
}
}
/// State shared with the connection threads.
pub struct Shared {
pub sync: Arc<data::SyncData>,
@@ -301,7 +345,7 @@ impl Shared {
Json::String(format!("data:image/png;base64,{}", base64(icon))),
));
}
if self.config.enforces_secure_chat {
if self.config.enforces_secure_chat() {
status.push(("enforcesSecureChat".to_owned(), Json::Bool(true)));
}
Json::Object(status).compact()
+2
View File
@@ -13,6 +13,8 @@ serde_json = "1.0.151"
# The shared secret and RSA padding (OS randomness) and the server hash.
getrandom = "0.3"
sha1 = "0.10"
# Secure chat: SHA256withRSA message signatures.
sha2 = "0.10"
[lints]
workspace = true
+10
View File
@@ -311,6 +311,16 @@ impl Codec<Vec<u8>> for Bytes {
w.byte_array(v)
}
}
/// ByteBufCodecs.byteArray(N): VarInt-prefixed bytes, at most N.
pub struct BytesN<const N: usize>;
impl<const N: usize> Codec<Vec<u8>> for BytesN<N> {
fn read(r: &mut Reader) -> Result<Vec<u8>> {
r.byte_array(N)
}
fn write(v: &Vec<u8>, w: &mut Writer) {
w.byte_array(v)
}
}
/// Everything left in the packet (custom payloads).
pub struct Rest;
impl Codec<Vec<u8>> for Rest {
+1
View File
@@ -13,6 +13,7 @@ pub mod item;
pub mod packets;
pub mod registry;
pub mod rsa;
pub mod secure_chat;
pub mod servers;
pub mod text;
+60 -3
View File
@@ -2,9 +2,9 @@
use crate::{
Error, Result,
buf::{
Bool, Byte, ChunkPos, Codec, Double, Float, Ident, Int, List, Long, LpVec3, Nbt, Opt,
OptVarInt, Pos, Reader, SectionPos, Short, Str, StrN, UByte, Uuid, VarInt, VarLong, Vec3d,
Writer,
Bool, Byte, BytesN, ChunkPos, Codec, Double, Float, Ident, Int, List, Long, LpVec3, Nbt,
Opt, OptVarInt, Pos, Reader, SectionPos, Short, Str, StrN, UByte, Uuid, VarInt, VarLong,
Vec3d, Writer,
},
chunk::{ChunkData, LightData},
entity_data::{EntityData, Value},
@@ -1604,6 +1604,42 @@ packet!(Chat {
last_seen: LastSeenUpdate => LastSeenUpdate,
});
packet!(ChatCommand { command: String => Str });
packet!(ChatAck { offset: i32 => VarInt });
/// ArgumentSignatures: each signed argument's name (at most 16 chars) and
/// signature, at most 8.
pub struct ArgumentSignatures;
impl Codec<Vec<(String, Vec<u8>)>> for ArgumentSignatures {
fn read(r: &mut Reader) -> Result<Vec<(String, Vec<u8>)>> {
let n = r.count(8)?;
(0..n)
.map(|_| Ok((r.string(16)?, r.bytes(256)?.to_vec())))
.collect()
}
fn write(v: &Vec<(String, Vec<u8>)>, w: &mut Writer) {
w.varint(v.len() as i32);
for (name, signature) in v {
w.string(name);
w.bytes(signature);
}
}
}
packet!(ChatCommandSigned {
command: String => Str,
timestamp: i64 => Long,
salt: i64 => Long,
arguments: Vec<(String, Vec<u8>)> => ArgumentSignatures,
last_seen: LastSeenUpdate => LastSeenUpdate,
});
packet!(
/// RemoteChatSession.Data: the session id, then ProfilePublicKey.Data
/// (expiry in epoch milliseconds, X.509 key, the services' signature).
ChatSessionUpdate {
session: u128 => Uuid,
expires_at: i64 => Long,
key: Vec<u8> => BytesN<512>,
key_signature: Vec<u8> => BytesN<4096>,
}
);
packet!(ChangeDifficultyRequest { difficulty: i32 => VarInt });
packet!(LockDifficulty { locked: bool => Bool });
packet!(SetGameRule { entries: Vec<GameRuleEntry> => List<GameRuleEntry> });
@@ -1697,7 +1733,10 @@ packet_enum!(Serverbound, crate::ids::play::serverbound {
Attack(Attack) = ATTACK,
ChangeGameMode(ChangeGameMode) = CHANGE_GAME_MODE,
Chat(Chat) = CHAT,
ChatAck(ChatAck) = CHAT_ACK,
ChatCommand(ChatCommand) = CHAT_COMMAND,
ChatCommandSigned(ChatCommandSigned) = CHAT_COMMAND_SIGNED,
ChatSessionUpdate(ChatSessionUpdate) = CHAT_SESSION_UPDATE,
PlaceRecipe(super::recipes::PlaceRecipe) = PLACE_RECIPE,
ChunkBatchReceived(ChunkBatchReceived) = CHUNK_BATCH_RECEIVED,
ClientCommand(ClientCommand) = CLIENT_COMMAND,
@@ -2184,6 +2223,24 @@ mod tests {
Serverbound::ChatCommand(ChatCommand {
command: "time set day".into(),
}),
Serverbound::ChatAck(ChatAck { offset: 3 }),
Serverbound::ChatCommandSigned(ChatCommandSigned {
command: "say hi".into(),
timestamp: 1_700_000_000_000,
salt: -5,
arguments: vec![("message".into(), vec![9; 256])],
last_seen: LastSeenUpdate {
offset: 1,
acknowledged: [1, 0, 8],
checksum: -3,
},
}),
Serverbound::ChatSessionUpdate(ChatSessionUpdate {
session: 0x1234,
expires_at: 1_800_000_000_000,
key: vec![0x30; 294],
key_signature: vec![7; 512],
}),
Serverbound::ChunkBatchReceived(ChunkBatchReceived {
chunks_per_tick: 9.5,
}),
+90 -2
View File
@@ -3,8 +3,10 @@
//! and challenge (Crypt.encryptUsingKey, Java's "RSA" cipher =
//! RSA/ECB/PKCS1Padding), and on the server's side the key pair it makes at
//! startup (Crypt.generateKeyPair: 1024 bits) and the decryption
//! (Crypt.decryptUsingKey). Modular exponentiation uses Montgomery
//! multiplication on 32-bit limbs; RSA moduli are always odd.
//! (Crypt.decryptUsingKey); and the PKCS#1 v1.5 signature checks of secure
//! chat (SHA256withRSA for messages, SHA1withRSA for the services' profile
//! key signatures). Modular exponentiation uses Montgomery multiplication on
//! 32-bit limbs; RSA moduli are always odd.
use crate::{Error, Result};
/// A non-negative integer, little-endian 32-bit limbs.
@@ -291,6 +293,63 @@ impl RsaPublicKey {
}
}
/// The digest a PKCS#1 v1.5 signature covers: Java's `SHA1withRSA` and
/// `SHA256withRSA`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SignatureHash {
Sha1,
Sha256,
}
impl SignatureHash {
/// The DER DigestInfo before the digest (RFC 8017 9.2, note 1).
fn prefix(self) -> &'static [u8] {
match self {
Self::Sha1 => &[
0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04,
0x14,
],
Self::Sha256 => &[
0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02,
0x01, 0x05, 0x00, 0x04, 0x20,
],
}
}
fn digest(self, message: &[u8]) -> Vec<u8> {
use sha1::Digest;
match self {
Self::Sha1 => sha1::Sha1::digest(message).to_vec(),
Self::Sha256 => sha2::Sha256::digest(message).to_vec(),
}
}
}
impl RsaPublicKey {
/// `Signature.verify` for RSASSA-PKCS1-v1_5 (RFC 8017 8.2.2): the
/// signature is exactly the modulus' length and below it, and opens to
/// `00 01 FF.. 00 DigestInfo`. Anything else is a failed check, as
/// SignatureValidator reports Java's exceptions.
pub fn verify(&self, hash: SignatureHash, message: &[u8], signature: &[u8]) -> bool {
let k = self.size();
// Equal lengths compare as big-endian numbers.
if signature.len() != k || signature >= self.modulus.as_slice() {
return false;
}
let (prefix, digest) = (hash.prefix(), hash.digest(message));
let t = prefix.len() + digest.len();
if k < t + 11 {
return false;
}
let mut expected = vec![0xff; k];
expected[0] = 0;
expected[1] = 1;
expected[k - t - 1] = 0;
expected[k - t..k - digest.len()].copy_from_slice(prefix);
expected[k - digest.len()..].copy_from_slice(&digest);
self.apply(signature, &self.exponent) == expected
}
}
/// Small odd primes for sieving prime candidates.
const SMALL_PRIMES: [u32; 53] = [
3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
@@ -604,5 +663,34 @@ pub(crate) mod tests {
assert!(block.ends_with(&hex(
"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
)));
let signature = hex(TEST_SIG_HELLO);
assert!(key.verify(SignatureHash::Sha256, b"hello", &signature));
assert!(!key.verify(SignatureHash::Sha256, b"hellO", &signature));
assert!(!key.verify(SignatureHash::Sha1, b"hello", &signature));
let mut flipped = signature.clone();
flipped[40] ^= 1;
assert!(!key.verify(SignatureHash::Sha256, b"hello", &flipped));
assert!(!key.verify(SignatureHash::Sha256, b"hello", &signature[1..]));
assert!(!key.verify(SignatureHash::Sha256, b"hello", &key.modulus));
}
/// Signatures made here with the private exponent (EMSA-PKCS1-v1_5 by
/// hand) check out for both digests.
#[test]
fn verifies_both_digests() {
let key = RsaPublicKey::from_der(&hex(TEST_SPKI)).unwrap();
for hash in [SignatureHash::Sha1, SignatureHash::Sha256] {
let (prefix, digest) = (hash.prefix(), hash.digest(b"payload"));
let mut block = vec![0xff; 128];
block[0] = 0;
block[1] = 1;
let t = prefix.len() + digest.len();
block[127 - t] = 0;
block[128 - t..128 - digest.len()].copy_from_slice(prefix);
block[128 - digest.len()..].copy_from_slice(&digest);
let signature = key.apply(&block, &hex(TEST_D));
assert!(key.verify(hash, b"payload", &signature));
assert!(!key.verify(hash, b"payloaD", &signature));
}
}
}
+491
View File
@@ -0,0 +1,491 @@
//! Secure chat as vanilla 26.3's server keeps it: a player's profile key,
//! validated against the services' keys (ProfilePublicKey,
//! RemoteChatSession), the message chain each player signs
//! (SignedMessageChain), what a signature covers (PlayerChatMessage,
//! SignedMessageLink, SignedMessageBody), the window of messages a client
//! acknowledges (LastSeenMessagesValidator) and the cache a connection packs
//! signatures against (MessageSignatureCache).
use crate::rsa::{RsaPublicKey, SignatureHash};
use std::collections::{HashSet, VecDeque};
/// MessageSignature.BYTES.
pub const SIGNATURE_BYTES: usize = 256;
/// A message signature (MessageSignature).
pub type Signature = [u8; SIGNATURE_BYTES];
/// LastSeenMessages.LAST_SEEN_MESSAGES_MAX_LENGTH: the window a client acknowledges.
pub const LAST_SEEN: usize = 20;
/// PlayerChatMessage.MESSAGE_EXPIRES_AFTER_SERVER, in milliseconds.
pub const EXPIRES_AFTER_SERVER: i64 = 5 * 60 * 1000;
/// A signature from its packet bytes (always 256 on the wire).
pub fn signature(bytes: &[u8]) -> Option<Signature> {
bytes.try_into().ok()
}
/// ProfilePublicKey.Data: the player's chat key (X.509 DER), when it
/// expires (epoch milliseconds), and the services' signature over both.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProfileKey {
pub expires_at: i64,
pub key: Vec<u8>,
pub signature: Vec<u8>,
}
impl ProfileKey {
/// Data.signedPayload: the profile id, the expiry and the key.
fn signed_payload(&self, profile: u128) -> Vec<u8> {
let mut out = Vec::with_capacity(24 + self.key.len());
out.extend_from_slice(&profile.to_be_bytes());
out.extend_from_slice(&self.expires_at.to_be_bytes());
out.extend_from_slice(&self.key);
out
}
/// Data.validateSignature with the services' profile-key keys
/// (SignatureValidator.from(keySet, PROFILE_KEY): any of them, SHA1withRSA).
pub fn signed_by(&self, services: &[RsaPublicKey], profile: u128) -> bool {
let payload = self.signed_payload(profile);
services
.iter()
.any(|k| k.verify(SignatureHash::Sha1, &payload, &self.signature))
}
/// Data.hasExpired.
pub fn has_expired(&self, now: i64) -> bool {
self.expires_at < now
}
}
/// A validated RemoteChatSession: its id and the player's profile key.
#[derive(Clone, Debug)]
pub struct ChatSession {
pub id: u128,
pub profile_key: ProfileKey,
key: RsaPublicKey,
}
impl ChatSession {
/// RemoteChatSession.Data.validate (ProfilePublicKey.createValidated);
/// the error is the disconnect message's translation key.
pub fn validate(
id: u128,
profile_key: ProfileKey,
services: &[RsaPublicKey],
profile: u128,
) -> Result<Self, &'static str> {
const INVALID: &str = "multiplayer.disconnect.invalid_public_key_signature";
let key = RsaPublicKey::from_der(&profile_key.key).map_err(|_| INVALID)?;
if !profile_key.signed_by(services, profile) {
return Err(INVALID);
}
Ok(Self {
id,
profile_key,
key,
})
}
/// RemoteChatSession.Data, as ClientboundPlayerInfoUpdatePacket's
/// INITIALIZE_CHAT carries it.
pub fn data(&self) -> (u128, i64, Vec<u8>, Vec<u8>) {
let k = &self.profile_key;
(self.id, k.expires_at, k.key.clone(), k.signature.clone())
}
}
/// SignedMessageBody: what a player signed besides the chain link.
#[derive(Clone, Copy, Debug)]
pub struct Body<'a> {
pub content: &'a str,
/// Epoch milliseconds.
pub timestamp: i64,
pub salt: i64,
pub last_seen: &'a [Signature],
}
/// PlayerChatMessage.updateSignature: the version, the link (sender,
/// session, index) and the body (salt, time in seconds, content, last seen).
pub fn signed_payload(sender: u128, session: u128, index: i32, body: &Body) -> Vec<u8> {
let content = body.content.as_bytes();
let mut out = Vec::with_capacity(64 + content.len() + body.last_seen.len() * SIGNATURE_BYTES);
out.extend_from_slice(&1i32.to_be_bytes());
out.extend_from_slice(&sender.to_be_bytes());
out.extend_from_slice(&session.to_be_bytes());
out.extend_from_slice(&index.to_be_bytes());
out.extend_from_slice(&body.salt.to_be_bytes());
out.extend_from_slice(&body.timestamp.div_euclid(1000).to_be_bytes());
out.extend_from_slice(&(content.len() as i32).to_be_bytes());
out.extend_from_slice(content);
out.extend_from_slice(&(body.last_seen.len() as i32).to_be_bytes());
for s in body.last_seen {
out.extend_from_slice(s);
}
out
}
/// SignedMessageChain.DecodeException's messages, and the signed command
/// mismatch (ServerGamePacketListenerImpl.INVALID_COMMAND_SIGNATURE).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DecodeError {
MissingProfileKey,
ChainBroken,
ExpiredProfileKey,
InvalidSignature,
OutOfOrderChat,
InvalidCommandSignature,
}
impl DecodeError {
pub fn key(self) -> &'static str {
match self {
Self::MissingProfileKey => "chat.disabled.missingProfileKey",
Self::ChainBroken => "chat.disabled.chain_broken",
Self::ExpiredProfileKey => "chat.disabled.expiredProfileKey",
Self::InvalidSignature => "chat.disabled.invalid_signature",
Self::OutOfOrderChat => "chat.disabled.out_of_order_chat",
Self::InvalidCommandSignature => "chat.disabled.invalid_command_signature",
}
}
}
/// A message the chain let through: unsigned (PlayerChatMessage.unsigned)
/// or signed at a link of the sender's chain.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Unpacked {
Unsigned,
Signed { index: i32, signature: Signature },
}
/// A player's SignedMessageChain.Decoder: unsigned until the player sends a
/// chat session, then keyed to it.
#[derive(Clone, Debug, Default)]
pub struct Chain {
session: Option<ChatSession>,
/// The next link's index; None once the chain is broken.
next: Option<i32>,
/// The last message's time (lastTimeStamp, from Instant.EPOCH).
last_timestamp: i64,
}
impl Chain {
/// RemoteChatSession.createMessageDecoder: a new chain from link 0.
pub fn keyed(session: ChatSession) -> Self {
Self {
session: Some(session),
next: Some(0),
last_timestamp: 0,
}
}
pub fn session(&self) -> Option<&ChatSession> {
self.session.as_ref()
}
/// Decoder.unpack for `sender`'s message. Without a session the message
/// is unsigned unless the server enforces secure chat; with one it must
/// be the next link, in order, signed by the player's key.
pub fn unpack(
&mut self,
sender: u128,
signature: Option<&Signature>,
body: &Body,
enforce: bool,
now: i64,
) -> Result<Unpacked, DecodeError> {
let Some(session) = &self.session else {
return if enforce {
Err(DecodeError::MissingProfileKey)
} else {
Ok(Unpacked::Unsigned)
};
};
let signature = signature.ok_or(DecodeError::MissingProfileKey)?;
if session.profile_key.has_expired(now) {
return Err(DecodeError::ExpiredProfileKey);
}
let index = self.next.ok_or(DecodeError::ChainBroken)?;
if body.timestamp < self.last_timestamp {
self.next = None;
return Err(DecodeError::OutOfOrderChat);
}
self.last_timestamp = body.timestamp;
let payload = signed_payload(sender, session.id, index, body);
if !session
.key
.verify(SignatureHash::Sha256, &payload, signature)
{
self.next = None;
return Err(DecodeError::InvalidSignature);
}
self.next = index.checked_add(1);
Ok(Unpacked::Signed {
index,
signature: *signature,
})
}
/// Decoder.setChainBroken (nothing for the unsigned decoder).
pub fn set_broken(&mut self) {
if self.session.is_some() {
self.next = None;
}
}
}
/// Arrays.hashCode(byte[]).
fn java_hash(bytes: &[u8]) -> i32 {
bytes.iter().fold(1i32, |h, &b| {
h.wrapping_mul(31).wrapping_add(i32::from(b as i8))
})
}
/// LastSeenMessages.computeChecksum: never 0, which means "not checked".
pub fn checksum(entries: &[Signature]) -> i8 {
let c = entries
.iter()
.fold(1i32, |c, e| c.wrapping_mul(31).wrapping_add(java_hash(e)));
match c as i8 {
0 => 1,
b => b,
}
}
/// LastSeenMessagesValidator: the messages sent to a player that its next
/// acknowledgement may name, each pending until it does.
#[derive(Clone, Debug)]
pub struct LastSeenValidator {
tracked: VecDeque<Option<(Signature, bool)>>,
last_pending: Option<Signature>,
}
impl Default for LastSeenValidator {
fn default() -> Self {
Self {
tracked: std::iter::repeat_n(None, LAST_SEEN).collect(),
last_pending: None,
}
}
}
impl LastSeenValidator {
pub fn add_pending(&mut self, signature: &Signature) {
if self.last_pending.as_ref() != Some(signature) {
self.tracked.push_back(Some((*signature, true)));
self.last_pending = Some(*signature);
}
}
pub fn tracked_count(&self) -> usize {
self.tracked.len()
}
/// applyOffset: drop the `offset` oldest messages.
pub fn apply_offset(&mut self, offset: i32) -> Result<(), String> {
let max = self.tracked.len() as i64 - LAST_SEEN as i64;
if offset >= 0 && i64::from(offset) <= max {
self.tracked.drain(..offset as usize);
Ok(())
} else {
Err(format!(
"Advanced last seen window by {offset} messages, but expected at most {max}"
))
}
}
/// applyUpdate: the offset, then the acknowledged messages of the
/// window (a 20-bit set, little-endian bytes), checked against the
/// client's checksum unless it sent 0.
pub fn apply_update(
&mut self,
offset: i32,
acknowledged: [u8; 3],
checksum: i8,
) -> Result<Vec<Signature>, String> {
self.apply_offset(offset)?;
let bit = |i: usize| acknowledged[i / 8] >> (i % 8) & 1 != 0;
let length = (0..24).rev().find(|&i| bit(i)).map_or(0, |i| i + 1);
if length > LAST_SEEN {
return Err(format!(
"Last seen update contained {length} messages, but maximum window size is {LAST_SEEN}"
));
}
let mut seen = Vec::with_capacity(LAST_SEEN);
for i in 0..LAST_SEEN {
let entry = self.tracked[i];
if bit(i) {
let Some((s, _)) = entry else {
return Err(format!(
"Last seen update acknowledged unknown or previously ignored message at index {i}"
));
};
self.tracked[i] = Some((s, false));
seen.push(s);
} else {
if let Some((_, false)) = entry {
return Err(format!(
"Last seen update ignored previously acknowledged message at index {i}"
));
}
self.tracked[i] = None;
}
}
if checksum != 0 && checksum != self::checksum(&seen) {
return Err(
"Checksum mismatch on last seen update: the client and server must have desynced"
.to_owned(),
);
}
Ok(seen)
}
}
/// MessageSignatureCache: the 128 signatures a connection's client also
/// holds, so a packed signature can name one by index.
#[derive(Clone, Debug)]
pub struct SignatureCache {
entries: Vec<Option<Signature>>,
}
impl Default for SignatureCache {
fn default() -> Self {
Self {
entries: vec![None; 128],
}
}
}
impl SignatureCache {
/// MessageSignature.pack: the cache index, or the full signature.
pub fn pack(&self, signature: &Signature) -> (i32, Option<Vec<u8>>) {
match self
.entries
.iter()
.position(|e| e.as_ref() == Some(signature))
{
Some(i) => (i as i32, None),
None => (-1, Some(signature.to_vec())),
}
}
/// push(body, signature): the message's last seen and its signature go
/// to the front, most recent first; what they displace moves back.
pub fn push(&mut self, last_seen: &[Signature], signature: Option<&Signature>) {
let mut queue: VecDeque<Signature> = last_seen.iter().chain(signature).copied().collect();
let new: HashSet<Signature> = queue.iter().copied().collect();
for slot in &mut self.entries {
let Some(next) = queue.pop_back() else { break };
if let Some(old) = slot.replace(next)
&& !new.contains(&old)
{
queue.push_front(old);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sig(b: u8) -> Signature {
[b; SIGNATURE_BYTES]
}
#[test]
fn payload_layout_matches_update_signature() {
let body = Body {
content: "hé",
timestamp: 1_700_000_123_456,
salt: -2,
last_seen: &[sig(7)],
};
let p = signed_payload(1, 2, 3, &body);
assert_eq!(&p[..4], &[0, 0, 0, 1]);
assert_eq!(&p[4..20], &1u128.to_be_bytes());
assert_eq!(&p[20..36], &2u128.to_be_bytes());
assert_eq!(&p[36..40], &3i32.to_be_bytes());
assert_eq!(&p[40..48], &(-2i64).to_be_bytes());
assert_eq!(&p[48..56], &1_700_000_123i64.to_be_bytes());
assert_eq!(&p[56..60], &3i32.to_be_bytes());
assert_eq!(&p[60..63], "hé".as_bytes());
assert_eq!(&p[63..67], &1i32.to_be_bytes());
assert_eq!(p.len(), 67 + 256);
// Instant.getEpochSecond floors.
let before = Body {
timestamp: -1,
..body
};
assert_eq!(
&signed_payload(1, 2, 3, &before)[48..56],
&(-1i64).to_be_bytes()
);
}
#[test]
fn unsigned_chain_follows_enforcement() {
let mut chain = Chain::default();
let body = Body {
content: "hi",
timestamp: 5,
salt: 0,
last_seen: &[],
};
assert_eq!(
chain.unpack(1, None, &body, false, 0),
Ok(Unpacked::Unsigned)
);
assert_eq!(
chain.unpack(1, Some(&sig(1)), &body, false, 0),
Ok(Unpacked::Unsigned)
);
assert_eq!(
chain.unpack(1, None, &body, true, 0),
Err(DecodeError::MissingProfileKey)
);
}
#[test]
fn checksums_follow_java_hash_codes() {
// Arrays.hashCode(new byte[]{-1}) = 31 + (-1) = 30.
assert_eq!(java_hash(&[0xff]), 30);
assert_eq!(checksum(&[]), 1);
let one = [sig(0)];
let expected = 31i32.wrapping_add(java_hash(&sig(0))) as i8;
assert_eq!(checksum(&one), if expected == 0 { 1 } else { expected });
}
#[test]
fn last_seen_window() {
let mut v = LastSeenValidator::default();
assert_eq!(v.tracked_count(), 20);
v.add_pending(&sig(1));
v.add_pending(&sig(1));
v.add_pending(&sig(2));
assert_eq!(v.tracked_count(), 22);
assert!(v.apply_offset(3).is_err());
// Slide by two: the two messages are the window's last entries.
let mut ack = [0u8; 3];
ack[18 / 8] |= 1 << (18 % 8);
ack[19 / 8] |= 1 << (19 % 8);
let seen = v.apply_update(2, ack, checksum(&[sig(1), sig(2)])).unwrap();
assert_eq!(seen, vec![sig(1), sig(2)]);
// Once acknowledged, a message can't be ignored.
assert!(v.apply_update(0, [0; 3], 0).is_err());
let mut v = LastSeenValidator::default();
v.add_pending(&sig(1));
assert!(v.apply_update(1, [0, 0, 0x10], 0).is_err(), "bit 20");
let mut v = LastSeenValidator::default();
assert!(v.apply_update(0, [1, 0, 0], 0).is_err(), "unknown");
let mut v = LastSeenValidator::default();
v.add_pending(&sig(1));
let mut ack = [0u8; 3];
ack[2] |= 1 << 3;
assert!(v.apply_update(1, ack, 99).is_err(), "checksum");
}
#[test]
fn cache_moves_recent_signatures_first() {
let mut c = SignatureCache::default();
assert_eq!(c.pack(&sig(1)), (-1, Some(sig(1).to_vec())));
c.push(&[], Some(&sig(1)));
assert_eq!(c.pack(&sig(1)), (0, None));
c.push(&[sig(1)], Some(&sig(2)));
assert_eq!(c.pack(&sig(2)), (0, None));
assert_eq!(c.pack(&sig(1)), (1, None));
c.push(&[], Some(&sig(3)));
assert_eq!(
[c.pack(&sig(3)).0, c.pack(&sig(2)).0, c.pack(&sig(1)).0],
[0, 1, 2]
);
}
}
+2
View File
@@ -12,6 +12,8 @@ horizon-protocol = { path = "../horizon-protocol" }
horizon-world = { path = "../horizon-world" }
log = "0.4"
# Online mode: finding accounts by name (the services' profiles endpoint).
# Secure chat: the services' public keys arrive base64-encoded.
base64 = "0.22"
ureq = { version = "2", default-features = false, features = ["tls"] }
[lints]
@@ -6,6 +6,7 @@ use crate::{
json::{self, Json},
user_lists::NameAndId,
};
use horizon_protocol::rsa::RsaPublicKey;
use std::time::Duration;
/// Mojang's services discovery document.
@@ -51,6 +52,57 @@ pub fn find_by_name(discovery: &str, name: &str) -> Option<NameAndId> {
found(&agent.get(&url).call().ok()?.into_string().ok()?, name)
}
/// MinecraftServicesKeyInfo.get: the services' public keys from the
/// discovery document's `authentication` / `getPublicKeys` endpoint. The
/// profile-key keys (`playerCertificateKeys`, ServicesKeyType.PROFILE_KEY)
/// sign the players' chat keys; secure chat checks them. The error is the
/// line vanilla logs, after which it has no keys.
pub fn fetch_profile_key_keys(discovery: &str) -> Result<Vec<RsaPublicKey>, &'static str> {
const FAILED: &str = "Failed to request Minecraft Services public key";
let agent = ureq::AgentBuilder::new()
.timeout(Duration::from_secs(15))
.user_agent(concat!("Horizon/", env!("CARGO_PKG_VERSION")))
.build();
let get = |url: &str| agent.get(url).call().ok()?.into_string().ok();
let doc = get(discovery)
.and_then(|d| json::parse(&d, true).ok())
.ok_or(FAILED)?;
let url = endpoint(&doc, "authentication", "getPublicKeys").ok_or(FAILED)?;
profile_key_keys(&get(&url).ok_or(FAILED)?)
}
/// The key set answer: `{"profilePropertyKeys": [...], "playerCertificateKeys":
/// [...]}`, each `{"publicKey": base64 X.509}`. One bad key makes the whole
/// answer malformed (parseList throws).
pub fn profile_key_keys(answer: &str) -> Result<Vec<RsaPublicKey>, &'static str> {
use base64::Engine;
const MALFORMED: &str = "Received malformed Minecraft Services public key data";
let doc = json::parse(answer, true).map_err(|_| MALFORMED)?;
let list = |name: &str| -> Result<Vec<RsaPublicKey>, &'static str> {
let Some(entries) = doc.get(name) else {
return Ok(Vec::new());
};
let Json::Array(entries) = entries else {
return Err(MALFORMED);
};
entries
.iter()
.map(|e| {
let text = e
.get("publicKey")
.and_then(Json::as_string)
.ok_or(MALFORMED)?;
let der = base64::engine::general_purpose::STANDARD
.decode(text)
.map_err(|_| MALFORMED)?;
RsaPublicKey::from_der(&der).map_err(|_| MALFORMED)
})
.collect()
};
list("profilePropertyKeys")?;
list("playerCertificateKeys")
}
/// MinecraftServicesDiscoveryService.getUrl(service, endpoint).
fn endpoint(doc: &Json, service: &str, name: &str) -> Option<String> {
doc.get("discovery")?
@@ -97,4 +149,20 @@ mod tests {
assert_eq!(found("{}", "Notch"), None);
assert_eq!(found("", "Notch"), None);
}
#[test]
fn services_key_sets() {
// The 1024-bit test key of horizon-protocol's RSA tests, base64.
let key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrAmB0+SRQqPcm84lbsRF2yo8GXzEAzUFDusT302dO3P+VtBoFJdI2WlScEG7GaFiBs5IXdCD9Mf9cFE/xG6JDEedRzSxrXRgXtq9mhqQ8ldYSQ2EwBeaUW2CTzZUiPOxeiFaCbanS7hsyM2KsHOvqs2WcvEUxmptUDdqBakeY/wIDAQAB";
let answer = format!(
r#"{{"profilePropertyKeys":[{{"publicKey":"{key}"}}],"playerCertificateKeys":[{{"publicKey":"{key}"}},{{"publicKey":"{key}"}}]}}"#
);
let keys = profile_key_keys(&answer).unwrap();
assert_eq!(keys.len(), 2);
assert_eq!(keys[0].size(), 128);
assert_eq!(profile_key_keys("{}").unwrap().len(), 0);
let bad = r#"{"profilePropertyKeys":[{"publicKey":"AAAA"}],"playerCertificateKeys":[]}"#;
assert!(profile_key_keys(bad).is_err());
assert!(profile_key_keys("not json").is_err());
}
}
+3 -2
View File
@@ -20,8 +20,9 @@ plan are in [docs/server.md](https://github.com/ludovic111/horizon/blob/main/doc
Until then Horizon players can join offline servers only (`online_mode = false`). An offline server never
checks accounts: anyone who reaches it can join under any name. Keep it on your LAN or a tailnet
(Tailscale, ZeroTier), or share it only with friends you trust.
- **No secure chat.** Chat isn't signed, and `enforce-secure-profile` has no effect. Vanilla clients show
chat from the server as not secure.
- **Secure chat** works as on vanilla's server: in online mode, vanilla clients sign their chat, and with
`enforce-secure-profile=true` (the default) unsigned chat is refused. On an offline server, or for a player
without a chat session (Horizon's own client today), chat stays unsigned and shows as not secure.
- **Nether and End:** each player travels on their own through portals, and operators with `execute in
<dimension> run tp`. The players in the level they leave stop seeing them, and the ones in the level they
arrive in start to, as on vanilla (checked against vanilla with two players, by command and through an End
+2 -2
View File
@@ -82,7 +82,8 @@ not tick cost, so it is in scope here (see section 9).
| Area | Status |
|---|---|
| `online-mode=true` | **Works** (ServerLoginPacketListenerImpl): a 1024-bit RSA key pair at start ("Generating keypair"), the encryption request, the shared secret and challenge, AES/CFB8, and the session service's `hasJoined` check (with the address when `prevent-proxy-connections` is on), with vanilla's log lines and messages. `test_online_mode_parity.py` runs the same logins against vanilla's server and Horizon's with a mock session service. Not yet: secure chat (chat is unsigned, as on a server that doesn't enforce it). |
| `online-mode=true` | **Works** (ServerLoginPacketListenerImpl): a 1024-bit RSA key pair at start ("Generating keypair"), the encryption request, the shared secret and challenge, AES/CFB8, and the session service's `hasJoined` check (with the address when `prevent-proxy-connections` is on), with vanilla's log lines and messages. `test_online_mode_parity.py` runs the same logins against vanilla's server and Horizon's with a mock session service. |
| Secure chat | **Works** (`SignedMessageChain`, `LastSeenMessagesValidator`, `MessageSignatureCache`): the chat session update and its profile key, checked against the services' `playerCertificateKeys`; signed chat, `say`, `me` and `msg`, relayed with their signatures and last-seen lists; `enforce-secure-profile` (with `online-mode`) refuses unsigned chat; and vanilla's log lines (`[Not Secure]`, the decoder's warnings, the kicks). Offline servers and players without a chat session chat unsigned, as on vanilla. `test_secure_chat_parity.py` runs the same signed and unsigned players against vanilla's server and Horizon's, with test keys and a mock services server. |
| Nether and End | **Players can't change dimension.** The engine keeps one dimension loaded (`Game::enter_dimension` moves only the local single-player player), and the hub serves one `dimension` to everyone. |
| Refused logins | Logged as vanilla does: `Disconnecting <name> (/address): <reason>`, then `<name> (/address) lost connection: <reason>`, and the session errors (`Username '<name>' tried to join with an invalid session`, `Couldn't verify username because servers are unavailable`). |
| Gameplay for remote players | The LAN guest leftovers apply: an item used on a block is used twice, the recipe book doesn't work, status effects don't apply to guests, arrows don't record their owner, team friendly fire is ignored (#484). Block desync and short freezes are reported on a same-machine server (#500). |
@@ -507,7 +508,6 @@ machine, then published in `benchmarks/`; and item 5.
**Out (next):**
- secure chat on the server (online mode itself works);
- the Nether and End for remote players;
- the headless engine and its RAM win;
- a server-only prepare without a JDK;
@@ -0,0 +1,625 @@
package dev.horizonlab.parity;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mojang.serialization.JsonOps;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import net.minecraft.SharedConstants;
import net.minecraft.commands.arguments.ArgumentSignatures;
import net.minecraft.core.UUIDUtil;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.ProtocolInfo;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.network.chat.LastSeenMessages;
import net.minecraft.network.chat.LastSeenMessagesTracker;
import net.minecraft.network.chat.LocalChatSession;
import net.minecraft.network.chat.MessageSignature;
import net.minecraft.network.chat.MessageSignatureCache;
import net.minecraft.network.chat.PlayerChatMessage;
import net.minecraft.network.chat.RemoteChatSession;
import net.minecraft.network.chat.SignedMessageBody;
import net.minecraft.network.chat.SignedMessageChain;
import net.minecraft.network.chat.SignedMessageLink;
import net.minecraft.network.chat.SignedMessageValidator;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.common.ClientboundDisconnectPacket;
import net.minecraft.network.protocol.common.ClientboundKeepAlivePacket;
import net.minecraft.network.protocol.common.ClientboundPingPacket;
import net.minecraft.network.protocol.common.ServerboundClientInformationPacket;
import net.minecraft.network.protocol.common.ServerboundKeepAlivePacket;
import net.minecraft.network.protocol.common.ServerboundPongPacket;
import net.minecraft.network.protocol.configuration.ClientboundFinishConfigurationPacket;
import net.minecraft.network.protocol.configuration.ClientboundSelectKnownPacks;
import net.minecraft.network.protocol.configuration.ConfigurationProtocols;
import net.minecraft.network.protocol.configuration.ServerboundFinishConfigurationPacket;
import net.minecraft.network.protocol.configuration.ServerboundSelectKnownPacks;
import net.minecraft.network.protocol.game.ClientboundChunkBatchFinishedPacket;
import net.minecraft.network.protocol.game.ClientboundDisguisedChatPacket;
import net.minecraft.network.protocol.game.ClientboundLoginPacket;
import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket;
import net.minecraft.network.protocol.game.ClientboundSystemChatPacket;
import net.minecraft.network.protocol.game.GameProtocols;
import net.minecraft.network.protocol.game.ServerboundAcceptTeleportationPacket;
import net.minecraft.network.protocol.game.ServerboundChatAckPacket;
import net.minecraft.network.protocol.game.ServerboundChatCommandPacket;
import net.minecraft.network.protocol.game.ServerboundChatCommandSignedPacket;
import net.minecraft.network.protocol.game.ServerboundChatPacket;
import net.minecraft.network.protocol.game.ServerboundChatSessionUpdatePacket;
import net.minecraft.network.protocol.game.ServerboundChunkBatchReceivedPacket;
import net.minecraft.network.protocol.game.ServerboundPlayerLoadedPacket;
import net.minecraft.network.protocol.handshake.ClientIntent;
import net.minecraft.network.protocol.handshake.ClientIntentionPacket;
import net.minecraft.network.protocol.handshake.HandshakeProtocols;
import net.minecraft.network.protocol.login.ClientboundHelloPacket;
import net.minecraft.network.protocol.login.ClientboundLoginCompressionPacket;
import net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket;
import net.minecraft.network.protocol.login.ClientboundLoginFinishedPacket;
import net.minecraft.network.protocol.login.LoginProtocols;
import net.minecraft.network.protocol.login.ServerboundHelloPacket;
import net.minecraft.network.protocol.login.ServerboundKeyPacket;
import net.minecraft.network.protocol.login.ServerboundLoginAcknowledgedPacket;
import net.minecraft.server.level.ClientInformation;
import net.minecraft.util.Crypt;
import net.minecraft.util.Signer;
import net.minecraft.world.entity.player.ProfileKeyPair;
import net.minecraft.world.entity.player.ProfilePublicKey;
/**
* Secure chat seen by vanilla-codec clients: several players on one server,
* signing their chat with profile keys that a mock of the Minecraft services
* signed (the harness serves its public key as the services' key set).
*
* <p>{@code keys <dir>} writes the mock services' key pair: the private key
* (PKCS#8) and {@code publickeys.json} as the services answer it. With a
* host, a port, the private key, an output file and a scenario, it plays the
* scenario. Each client signs and acknowledges exactly as
* ClientPacketListener does (LocalChatSession, SignedMessageChain's encoder,
* LastSeenMessagesTracker, MessageSignatureCache), and checks every player
* chat it receives with SignedMessageValidator against the sender's session
* from the player list. The transcript holds what each player received and
* whether its signature checked out; keys, salts, times and signatures
* differ between runs and are left out.
*/
public final class SecureChatClient {
private static final List<String> LINES = new ArrayList<>();
private static PrivateKey services;
private static String sessionUrl;
public static void main(String[] args) throws Exception {
ParityData.init();
if (args[0].equals("keys")) {
keys(Path.of(args[1]));
return;
}
String host = args[0];
int port = Integer.parseInt(args[1]);
services = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(Files.readAllBytes(Path.of(args[2]))));
Path output = Path.of(args[3]);
String scenario = args[4];
sessionUrl = args.length > 5 ? args[5] : null;
try {
if (scenario.equals("offline")) offline(host, port);
else online(host, port);
} catch (Exception e) {
StringBuilder line = new StringBuilder("error " + e);
for (Throwable c = e.getCause(); c != null; c = c.getCause()) line.append(" / caused by ").append(c);
LINES.add(line.toString());
}
try (PrintWriter w = new PrintWriter(Files.newBufferedWriter(output, StandardCharsets.UTF_8))) {
for (String line : LINES) w.println(line);
}
System.exit(0);
}
private static void keys(Path dir) throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048);
KeyPair pair = generator.generateKeyPair();
Files.write(dir.resolve("services-private.der"), pair.getPrivate().getEncoded());
JsonObject key = new JsonObject();
key.addProperty("publicKey", Base64.getEncoder().encodeToString(pair.getPublic().getEncoded()));
JsonArray list = new JsonArray();
list.add(key);
JsonObject set = new JsonObject();
set.add("profilePropertyKeys", list);
set.add("playerCertificateKeys", list);
Files.writeString(dir.resolve("publickeys.json"), set.toString());
}
// ---- scenarios ----------------------------------------------------
/** Offline mode: the server checks chat sessions but enforces nothing. */
private static void offline(String host, int port) throws Exception {
Client alice = Client.join(host, port, "Alice", true, null);
Client bob = Client.join(host, port, "Bob", true, null);
Client carol = Client.join(host, port, "Carol", false, null);
List<Client> all = List.of(alice, bob, carol);
settle(all, "joined");
alice.chat("hello, signed");
settle(all, "alice chats");
bob.chat("seen it");
settle(all, "bob chats with alice's message seen");
carol.chat("no key here");
settle(all, "carol chats unsigned");
alice.signedCommand("say signed announcement", "message", "signed announcement");
settle(all, "alice says");
alice.signedCommand("me waves", "action", "waves");
settle(all, "alice emotes");
alice.signedCommand("msg Bob just between us", "message", "just between us");
settle(all, "alice whispers to bob");
carol.signedCommand("me shrugs", null, null);
settle(all, "carol emotes, unsigned arguments");
carol.command("say plain announcement");
settle(all, "carol says through the unsigned packet");
bob.signedCommand("say mismatch", "nothing", "mismatch");
settle(all, "bob signs the wrong argument");
bob.chat("after the mismatch");
settle(all, "bob's chain after the mismatch");
alice.tamperedChat("forged");
settle(all, "alice's signature fails");
alice.chat("after the forgery");
settle(all, "alice's chain is broken");
bob.send(new ServerboundChatAckPacket(50));
settle(all, "bob acknowledges too much");
for (Client c : all) c.close();
}
/** Online mode with enforce-secure-profile: unsigned chat is refused. */
private static void online(String host, int port) throws Exception {
Client alice = Client.join(host, port, "Alice", true, null);
Client carol = Client.join(host, port, "Carol", false, null);
List<Client> all = List.of(alice, carol);
settle(all, "joined");
alice.chat("signed and enforced");
settle(all, "alice chats");
carol.chat("unsigned under enforcement");
settle(all, "carol chats without a key");
carol.command("me tries the unsigned packet");
settle(all, "carol's unsigned command");
carol.signedCommand("me tries without signatures", null, null);
settle(all, "carol's command without signatures");
// A key the services signed for another player.
Client mallory = Client.join(host, port, "Mallory", true, UUID.nameUUIDFromBytes("someone else".getBytes()));
settle(List.of(alice, carol, mallory), "mallory's borrowed key");
for (Client c : all) c.close();
mallory.close();
}
/** Lets the server answer, then records what each player received, in player order. */
private static void settle(List<Client> clients, String step) throws Exception {
Thread.sleep(1500);
LINES.add("step " + step);
for (Client c : clients) c.drain();
}
private static String json(Component c) {
return ComponentSerialization.CODEC.encodeStart(ParityData.access.createSerializationContext(JsonOps.INSTANCE), c).getOrThrow().toString();
}
/** A player: one connection, its chat session and what its client tracks. */
private static final class Client {
final String name;
UUID id;
Socket socket;
DataInputStream in;
OutputStream out;
int threshold = -1;
ProtocolInfo<?> inbound;
ProtocolInfo<?> outbound;
final BlockingQueue<Packet<?>> queue = new LinkedBlockingQueue<>();
volatile boolean closed;
LocalChatSession session;
SignedMessageChain.Encoder encoder = SignedMessageChain.Encoder.UNSIGNED;
final LastSeenMessagesTracker lastSeen = new LastSeenMessagesTracker(20);
final MessageSignatureCache cache = MessageSignatureCache.createDefault();
int nextChatIndex;
final Map<UUID, String> names = new LinkedHashMap<>();
final Map<UUID, SignedMessageValidator> validators = new HashMap<>();
boolean enforced;
boolean loaded;
net.minecraft.world.entity.PositionMoveRotation position = new net.minecraft.world.entity.PositionMoveRotation(
net.minecraft.world.phys.Vec3.ZERO, net.minecraft.world.phys.Vec3.ZERO, 0, 0);
Client(String name) {
this.name = name;
}
void record(String line) {
LINES.add(name + " " + line);
}
static Client join(String host, int port, String name, boolean signs, UUID keyOwner) throws Exception {
Client c = new Client(name);
c.connect(host, port, signs, keyOwner);
return c;
}
void connect(String host, int port, boolean signs, UUID keyOwner) throws Exception {
socket = new Socket();
socket.connect(new InetSocketAddress(host, port), 10_000);
socket.setSoTimeout(30_000);
socket.setTcpNoDelay(true);
in = new DataInputStream(socket.getInputStream());
out = socket.getOutputStream();
outbound = HandshakeProtocols.SERVERBOUND;
send(new ClientIntentionPacket(SharedConstants.getProtocolVersion(), host, port, ClientIntent.LOGIN));
outbound = LoginProtocols.SERVERBOUND;
inbound = LoginProtocols.CLIENTBOUND;
send(new ServerboundHelloPacket(name, UUIDUtil.createOfflinePlayerUUID(name)));
while (true) {
Packet<?> p = read();
if (p instanceof ClientboundHelloPacket hello) {
// ClientHandshakePacketListenerImpl.handleHello: join through the session service.
SecretKey secret = Crypt.generateSecretKey();
String hash = new BigInteger(Crypt.digestData(hello.getServerId(), hello.getPublicKey(), secret)).toString(16);
joinSession(hash);
send(new ServerboundKeyPacket(secret, hello.getPublicKey(), hello.getChallenge()));
in = new DataInputStream(new CipherInputStream(socket.getInputStream(), Crypt.getCipher(Cipher.DECRYPT_MODE, secret)));
out = new CipherOutputStream(socket.getOutputStream(), Crypt.getCipher(Cipher.ENCRYPT_MODE, secret));
} else if (p instanceof ClientboundLoginCompressionPacket compression) {
threshold = compression.getCompressionThreshold();
} else if (p instanceof ClientboundLoginFinishedPacket finished) {
id = finished.gameProfile().id();
record("logged in");
send(ServerboundLoginAcknowledgedPacket.INSTANCE);
break;
} else if (p instanceof ClientboundLoginDisconnectPacket d) {
record("refused " + json(d.reason()));
closed = true;
return;
}
}
outbound = ConfigurationProtocols.SERVERBOUND;
inbound = ConfigurationProtocols.CLIENTBOUND;
send(new ServerboundClientInformationPacket(ClientInformation.createDefault()));
while (true) {
Packet<?> p = read();
if (p instanceof ClientboundSelectKnownPacks packs) {
send(new ServerboundSelectKnownPacks(packs.knownPacks()));
} else if (p instanceof ClientboundKeepAlivePacket k) {
send(new ServerboundKeepAlivePacket(k.getId()));
} else if (p instanceof ClientboundPingPacket ping) {
send(new ServerboundPongPacket(ping.getId()));
} else if (p instanceof ClientboundFinishConfigurationPacket) {
send(ServerboundFinishConfigurationPacket.INSTANCE);
break;
} else if (p instanceof ClientboundDisconnectPacket d) {
record("disconnected " + json(d.reason()));
closed = true;
return;
}
}
GameProtocols.Context context = new GameProtocols.Context() {
@Override
public boolean hasInfiniteMaterials() {
return false;
}
@Override
public boolean canUseCommandBlocks() {
return false;
}
};
outbound = GameProtocols.SERVERBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access), context);
inbound = GameProtocols.CLIENTBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access));
// ClientPacketListener.handleLogin: the session goes out once the
// login packet is in (prepareKeyPair, setKeyPair).
while (true) {
Packet<?> p = read();
if (p instanceof ClientboundLoginPacket login) {
enforced = login.enforcesSecureChat();
record("login online " + login.onlineMode() + " enforces " + login.enforcesSecureChat());
if (signs) startSession(keyOwner == null ? id : keyOwner);
break;
}
}
Thread reader = new Thread(() -> {
try {
while (!closed) {
Packet<?> p = read();
queue.put(p);
// Nothing follows a disconnect but the closed socket.
if (p instanceof ClientboundDisconnectPacket) return;
}
} catch (Exception e) {
if (!closed) queue.add(new ClientboundDisconnectPacket(Component.literal("connection lost: " + e)));
}
}, name + " reader");
reader.setDaemon(true);
reader.start();
}
/** The session service's join, as the launcher's account would. */
void joinSession(String serverHash) throws Exception {
HttpURLConnection http = (HttpURLConnection) URI.create(sessionUrl + "/session/minecraft/join").toURL().openConnection();
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setRequestProperty("Content-Type", "application/json");
JsonObject body = new JsonObject();
body.addProperty("accessToken", "mock-token");
body.addProperty("selectedProfile", name);
body.addProperty("serverId", serverHash);
http.getOutputStream().write(body.toString().getBytes(StandardCharsets.UTF_8));
http.getResponseCode();
http.disconnect();
}
/** A profile key pair the mock services signed for {@code owner}, and its session. */
void startSession(UUID owner) throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048);
KeyPair pair = generator.generateKeyPair();
Instant expires = Instant.now().plusSeconds(3600).truncatedTo(java.time.temporal.ChronoUnit.MILLIS);
byte[] key = pair.getPublic().getEncoded();
byte[] payload = ByteBuffer.allocate(24 + key.length).putLong(owner.getMostSignificantBits())
.putLong(owner.getLeastSignificantBits()).putLong(expires.toEpochMilli()).put(key).array();
byte[] signature = Signer.from(services, "SHA1withRSA").sign(payload);
ProfilePublicKey publicKey = new ProfilePublicKey(new ProfilePublicKey.Data(expires, pair.getPublic(), signature));
session = LocalChatSession.create(new ProfileKeyPair(pair.getPrivate(), publicKey, expires));
encoder = session.createMessageEncoder(id);
send(new ServerboundChatSessionUpdatePacket(session.asRemote().asData()));
}
/** ClientPacketListener.sendChat. */
void chat(String content) throws Exception {
Instant now = Instant.now();
long salt = Crypt.SaltSupplier.getLong();
LastSeenMessagesTracker.Update update = lastSeen.generateAndApplyUpdate();
MessageSignature signature = encoder.pack(new SignedMessageBody(content, now, salt, update.lastSeen()));
send(new ServerboundChatPacket(content, now, salt, Optional.ofNullable(signature), update.update()));
}
/** A chat whose signature doesn't match its text. */
void tamperedChat(String content) throws Exception {
Instant now = Instant.now();
long salt = Crypt.SaltSupplier.getLong();
LastSeenMessagesTracker.Update update = lastSeen.generateAndApplyUpdate();
MessageSignature signature = encoder.pack(new SignedMessageBody(content + "!", now, salt, update.lastSeen()));
send(new ServerboundChatPacket(content, now, salt, Optional.ofNullable(signature), update.update()));
}
/** ClientPacketListener.sendCommand for a command with one message argument (none: no signatures). */
void signedCommand(String command, String argument, String value) throws Exception {
Instant now = Instant.now();
long salt = Crypt.SaltSupplier.getLong();
LastSeenMessagesTracker.Update update = lastSeen.generateAndApplyUpdate();
List<ArgumentSignatures.Entry> entries = new ArrayList<>();
if (argument != null) {
MessageSignature signature = encoder.pack(new SignedMessageBody(value, now, salt, update.lastSeen()));
if (signature != null) entries.add(new ArgumentSignatures.Entry(argument, signature));
}
send(new ServerboundChatCommandSignedPacket(command, now, salt, new ArgumentSignatures(entries), update.update()));
}
void command(String command) throws Exception {
send(new ServerboundChatCommandPacket(command));
}
/** Handles and records what arrived. */
void drain() throws Exception {
Packet<?> p;
while ((p = queue.poll(50, TimeUnit.MILLISECONDS)) != null) handle(p);
}
void handle(Packet<?> packet) throws Exception {
if (packet instanceof ClientboundKeepAlivePacket k) {
send(new ServerboundKeepAlivePacket(k.getId()));
} else if (packet instanceof ClientboundPingPacket ping) {
send(new ServerboundPongPacket(ping.getId()));
} else if (packet instanceof ClientboundPlayerPositionPacket pos) {
position = net.minecraft.world.entity.PositionMoveRotation.calculateAbsolute(position, pos.change(), pos.relatives());
send(new ServerboundAcceptTeleportationPacket(pos.id(), position.position().x, position.position().y,
position.position().z, position.yRot(), position.xRot()));
if (!loaded) {
send(new ServerboundPlayerLoadedPacket());
loaded = true;
}
} else if (packet instanceof ClientboundChunkBatchFinishedPacket) {
send(new ServerboundChunkBatchReceivedPacket(64.0F));
} else if (packet instanceof ClientboundPlayerInfoUpdatePacket info) {
for (ClientboundPlayerInfoUpdatePacket.Entry e : info.entries()) {
if (info.actions().contains(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER)) {
names.put(e.profileId(), e.profile().name());
}
if (info.actions().contains(ClientboundPlayerInfoUpdatePacket.Action.INITIALIZE_CHAT)) {
RemoteChatSession.Data data = e.chatSession();
String who = names.getOrDefault(e.profileId(), "?");
if (data != null) {
RemoteChatSession remote = new RemoteChatSession(data.sessionId(), new ProfilePublicKey(data.profilePublicKey()));
validators.put(e.profileId(), remote.createMessageValidator(ProfilePublicKey.EXPIRY_GRACE_PERIOD));
sessions.put(e.profileId(), data.sessionId());
record("session of " + who);
} else if (!info.actions().contains(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER)) {
record("session cleared for " + who);
}
}
}
} else if (packet instanceof ClientboundPlayerChatPacket chat) {
playerChat(chat);
} else if (packet instanceof ClientboundDisguisedChatPacket d) {
record("disguised " + d.chatType().chatType().getRegisteredName() + " " + json(d.chatType().name()) + " "
+ d.chatType().targetName().map(SecureChatClient::json) + " " + json(d.message()));
} else if (packet instanceof ClientboundSystemChatPacket s) {
record("system " + json(s.content()));
} else if (packet instanceof ClientboundDisconnectPacket d) {
record("disconnected " + json(d.reason()));
closed = true;
}
}
/** ClientPacketListener.handlePlayerChat, then markMessageAsProcessed. */
void playerChat(ClientboundPlayerChatPacket p) {
int expected = nextChatIndex++;
String sender = names.getOrDefault(p.sender(), p.sender().toString());
StringBuilder lastSeenPacked = new StringBuilder();
for (MessageSignature.Packed packed : p.body().lastSeen().entries()) {
lastSeenPacked.append(packed.fullSignature() != null ? "full" : "cached:" + packed.id()).append(',');
}
Optional<SignedMessageBody> body = p.body().unpack(cache);
if (p.globalIndex() != expected || body.isEmpty()) {
record("chat out of order or unknown signature: index " + p.globalIndex() + " expected " + expected);
return;
}
MessageSignature signature = p.signature().orElse(null);
cache.push(body.get(), signature);
SignedMessageValidator validator = validators.get(p.sender());
String verdict;
if (validator != null) {
PlayerChatMessage message = new PlayerChatMessage(
new SignedMessageLink(p.index(), p.sender(), sessionId(p.sender())), signature, body.get(),
p.unsignedContent().orElse(null), p.filterMask());
verdict = validator.updateAndValidate(message) != null ? "verified" : "rejected";
} else {
verdict = signature == null ? (enforced ? "unsigned-rejected" : "unsigned") : "no-session";
}
record("chat #" + p.globalIndex() + " from " + sender + " link " + p.index() + " " + verdict + " '" + body.get().content()
+ "' seen [" + lastSeenPacked + "] " + body.get().lastSeen().entries().size() + " unsigned "
+ p.unsignedContent().map(SecureChatClient::json) + " " + p.chatType().chatType().getRegisteredName() + " "
+ json(p.chatType().name()) + " " + p.chatType().targetName().map(SecureChatClient::json));
if (signature != null) lastSeen.addPending(signature, true);
}
private final Map<UUID, UUID> sessions = new HashMap<>();
UUID sessionId(UUID sender) {
return sessions.getOrDefault(sender, new UUID(0, 0));
}
// ---- framing ------------------------------------------------------
synchronized void send(Packet<?> packet) throws Exception {
ByteBuf body = Unpooled.buffer();
encode(body, packet);
byte[] raw = new byte[body.readableBytes()];
body.readBytes(raw);
ByteBuf frame = Unpooled.buffer();
if (threshold >= 0) {
ByteBuf inner = Unpooled.buffer();
if (raw.length >= threshold) {
writeVarInt(inner, raw.length);
Deflater d = new Deflater();
d.setInput(raw);
d.finish();
byte[] tmp = new byte[raw.length + 64];
while (!d.finished()) inner.writeBytes(tmp, 0, d.deflate(tmp));
d.end();
} else {
writeVarInt(inner, 0);
inner.writeBytes(raw);
}
writeVarInt(frame, inner.readableBytes());
frame.writeBytes(inner);
} else {
writeVarInt(frame, raw.length);
frame.writeBytes(raw);
}
byte[] bytes = new byte[frame.readableBytes()];
frame.readBytes(bytes);
// A server that disconnected the player may close before this
// write: what it sent last is the disconnect, which the reader
// records, not the broken pipe.
if (broken) return;
try {
out.write(bytes);
out.flush();
} catch (java.io.IOException e) {
broken = true;
}
}
private boolean broken;
@SuppressWarnings({"unchecked", "rawtypes"})
private void encode(ByteBuf body, Packet<?> packet) {
((ProtocolInfo) outbound).codec().encode(body, packet);
}
Packet<?> read() throws Exception {
int length = readVarInt(in);
byte[] frame = new byte[length];
in.readFully(frame);
byte[] raw = frame;
if (threshold >= 0) {
DataInputStream f = new DataInputStream(new ByteArrayInputStream(frame));
int dataLength = readVarInt(f);
byte[] rest = f.readAllBytes();
if (dataLength == 0) {
raw = rest;
} else {
Inflater inflater = new Inflater();
inflater.setInput(rest);
raw = new byte[dataLength];
inflater.inflate(raw);
inflater.end();
}
}
return inbound.codec().decode(Unpooled.wrappedBuffer(raw));
}
void close() throws Exception {
closed = true;
socket.close();
}
}
private static int readVarInt(InputStream in) throws Exception {
int value = 0;
for (int i = 0; i < 5; i++) {
int b = in.read();
if (b < 0) throw new EOFException();
value |= (b & 0x7F) << (7 * i);
if ((b & 0x80) == 0) return value;
}
throw new IllegalStateException("VarInt too big");
}
private static void writeVarInt(ByteBuf buf, int value) {
while ((value & ~0x7F) != 0) {
buf.writeByte((value & 0x7F) | 0x80);
value >>>= 7;
}
buf.writeByte(value);
}
}
+317
View File
@@ -0,0 +1,317 @@
#!/usr/bin/env python3
"""Secure chat on vanilla's dedicated server and Horizon's, with no real account.
A mock of the Minecraft services stands in for Mojang's: its key set (the
`authentication` / `getPublicKeys` endpoint) holds a key pair the harness
makes, so the profile keys it signs are as good as Mojang's to both servers.
Vanilla finds the mock through authlib's `-Dminecraft.api.discovery.host`,
Horizon through `HORIZON_API_DISCOVERY`. SecureChatClient (vanilla's own
codecs and chat classes) plays several players at once: each signs its chat
with a profile key the mock signed, acknowledges what it saw, and checks the
signature of every chat it receives against the sender's session from the
player list, as vanilla's client does.
Two servers, each run on vanilla then on Horizon:
- offline mode (enforce-secure-profile is then off): signed chat with the
last-seen window and the signature cache, unsigned chat beside it, /say,
/me and /msg with signed and unsigned arguments, a signed argument under
the wrong name, a forged signature and the broken chain after it, and an
acknowledgement past the window;
- online mode with enforce-secure-profile: the status and the login packet
say so, unsigned chat and unsigned commands with a message are refused, and
a key signed for another player is refused.
Compared: every player's transcript, the status' `enforcesSecureChat`, and
the servers' log lines about chat.
"""
import hashlib
import json
import os
from pathlib import Path
import platform
import re
import shutil
import socket
import subprocess
import sys
import time
import threading
import urllib.parse
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
try:
from . import lab, oracle_harness as oh, test_rust_parity as base
from .test_dedicated_server_parity import Server, free_port
except ImportError:
import lab
import oracle_harness as oh
import test_rust_parity as base
from test_dedicated_server_parity import Server, free_port
NAME = 'secure-chat-parity'
SCENARIOS = {
'offline': {'online-mode': 'false'},
'online': {'online-mode': 'true', 'enforce-secure-profile': 'true'},
}
PROPERTIES = {
'white-list': 'false',
'level-seed': '12345',
'view-distance': '3',
'simulation-distance': '3',
'spawn-protection': '0',
'max-players': '8',
'pause-when-empty-seconds': '-1',
}
def account(name):
"""The mock services' account id for a name."""
return hashlib.md5(f'account:{name}'.encode()).hexdigest()
class Services:
"""Discovery, the key set, the session service's join and hasJoined, profiles by name."""
def __init__(self, key_set):
self.joined = {}
self.lock = threading.Lock()
services = self
class Handler(BaseHTTPRequestHandler):
def log_message(self, *args):
pass
def reply(self, status, body=None):
data = b'' if body is None else json.dumps(body).encode()
self.send_response(status)
if body is not None:
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Length', str(len(data)))
self.end_headers()
self.wfile.write(data)
def do_GET(self):
url = urllib.parse.urlparse(self.path)
query = dict(urllib.parse.parse_qsl(url.query))
if url.path == '/minecraft/client':
return self.reply(200, services.discovery())
if url.path == '/publickeys':
return self.reply(200, key_set)
if url.path.startswith('/users/profiles/minecraft/'):
name = urllib.parse.unquote(url.path.rsplit('/', 1)[1])
return self.reply(200, {'id': account(name), 'name': name})
if url.path == '/session/minecraft/hasJoined':
name = query.get('username')
with services.lock:
ok = services.joined.get(name) == query.get('serverId')
if ok:
return self.reply(200, {'id': account(name), 'name': name, 'properties': []})
return self.reply(204)
self.reply(404, {'error': 'NotFound'})
def do_POST(self):
body = json.loads(self.rfile.read(int(self.headers.get('Content-Length', 0))) or b'{}')
if self.path != '/session/minecraft/join':
return self.reply(404, {'error': 'NotFound'})
# The test client names its account instead of its id.
with services.lock:
services.joined[body.get('selectedProfile')] = body.get('serverId')
self.reply(204)
self.server = ThreadingHTTPServer(('127.0.0.1', 0), Handler)
self.url = f'http://127.0.0.1:{self.server.server_address[1]}'
threading.Thread(target=self.server.serve_forever, daemon=True).start()
def discovery(self):
def endpoints(**names):
return {'endpoints': {k: {'uri': self.url + v} for k, v in names.items()}}
return {'environment': 'mock', 'product': 'minecraft', 'discovery': {
'authentication': endpoints(getPublicKeys='/publickeys'),
'session': endpoints(join='/session/minecraft/join', verify='/session/minecraft/hasJoined',
getProfileById='/session/minecraft/profile/{profileId}'),
'player': {'endpoints': {}},
'profiles': {'endpoints': {'getTexture': {'validUris': [self.url + '/texture/{textureId}']},
'getByName': {'uri': self.url + '/users/profiles/minecraft/{name}'}}},
'telemetry': {'endpoints': {}},
}}
def stop(self):
self.server.shutdown()
def player_id(name, scenario):
"""The player's id: the account's online, UUIDUtil.createOfflinePlayerUUID offline."""
if scenario == 'online':
return account(name)
digest = bytearray(hashlib.md5(f'OfflinePlayer:{name}'.encode()).digest())
digest[6] = digest[6] & 0x0F | 0x30
digest[8] = digest[8] & 0x3F | 0x80
return digest.hex()
def dashed(u):
return f'{u[:8]}-{u[8:12]}-{u[12:16]}-{u[16:20]}-{u[20:]}'
def varint(n):
out = b''
while True:
b = n & 0x7F
n >>= 7
out += bytes([b | (0x80 if n else 0)])
if not n:
return out
def status(port):
"""The status response's JSON (ServerStatus)."""
def packet(body):
return varint(len(body)) + body
def read_varint(s):
value = 0
for i in range(5):
b = s.recv(1)[0]
value |= (b & 0x7F) << (7 * i)
if not b & 0x80:
return value
with socket.create_connection(('127.0.0.1', port), timeout=10) as s:
host = b'127.0.0.1'
s.sendall(packet(b'\x00' + varint(777) + varint(len(host)) + host + port.to_bytes(2, 'big') + b'\x01'))
s.sendall(packet(b'\x00'))
length = read_varint(s)
data = b''
while len(data) < length:
data += s.recv(length - len(data))
text = data[1:]
n, shift, i = 0, 0, 0
while True:
b = text[i]
n |= (b & 0x7F) << shift
shift += 7
i += 1
if not b & 0x80:
break
return json.loads(text[i:i + n])
# The servers' lines about chat, with their level. The thread is left out:
# vanilla handles acknowledgements on its network threads, named per platform.
CHAT_LINE = re.compile(r'^\[\d\d:\d\d:\d\d\] \[([^\]]+)\]: (.*(?:<\w+>|\[Not Secure\]|\[\w+\] |\* \w+ |secure chat|'
r'signable|Signed command|acknowledgement|profile key|chat session|expired chat|lost connection|'
r'Disconnecting).*)$')
def chat_lines(lines):
out = []
for line in lines:
if m := CHAT_LINE.match(line):
text = re.sub(r'/127\.0\.0\.1:\d+', '/127.0.0.1:_', m.group(2))
out.append(f'[{m.group(1).rsplit("/", 1)[-1]}] {text}')
return out
def run_side(label, scenario, command, directory, client):
port = free_port()
shutil.rmtree(directory, ignore_errors=True)
directory.mkdir(parents=True)
props = dict(PROPERTIES, **SCENARIOS[scenario], **{'server-port': str(port)})
(directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items()))
(directory / 'eula.txt').write_text('eula=true\n')
# Alice and Carol are operators (/say needs it), by their ids.
ops = [{'uuid': dashed(player_id(name, scenario)), 'name': name, 'level': 4, 'bypassesPlayerLimit': False}
for name in ('Alice', 'Carol')]
(directory / 'ops.json').write_text(json.dumps(ops, indent=2))
server = Server(command, directory, f'{label}-{scenario}')
try:
server.wait_for(r'Done \(', timeout=600)
# A server can drop a ping in its first moments (the network thread still starting): try again.
for attempt in range(5):
try:
enforces = status(port).get('enforcesSecureChat', False)
break
except (OSError, IndexError):
if attempt == 4:
raise
time.sleep(2)
out = directory.parent / f'{label}-{scenario}.txt'
client(port, scenario, out)
transcript = [f'status enforcesSecureChat {enforces}'] + out.read_text(encoding='utf-8').splitlines()
finally:
server.stop()
return transcript, chat_lines(server.lines)
def main():
lock, jars, libraries = base.verify_artifacts()
build = lab.LOCAL / NAME
build.mkdir(parents=True, exist_ok=True)
report = build / 'report.json'
report.unlink(missing_ok=True)
classes = build / 'classes'
shutil.rmtree(classes, ignore_errors=True)
classes.mkdir()
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/SecureChatClient.java'
oh.run_logged(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-nowarn', '-sourcepath',
lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle],
build, 'compile-java')
oh.run_logged(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], build,
'compile-rust')
metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'],
cwd=lab.ROOT, text=True))
binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client')
java = ['java', '-Xmx1g', '-Duser.timezone=UTC', '-cp', str(classes) + os.pathsep + dependencies,
'dev.horizonlab.parity.SecureChatClient']
oh.run_logged([*java, 'keys', build], build, 'keys')
services = Services(json.loads((build / 'publickeys.json').read_text()))
discovery = services.url + '/minecraft/client'
os.environ.update(HORIZON_NO_AUDIO='1', HORIZON_API_DISCOVERY=discovery)
servers = {
'vanilla': ['java', '-Xmx2g', '-Duser.timezone=UTC', f'-Dminecraft.api.discovery.host={discovery}',
'-cp', dependencies, 'net.minecraft.server.Main', 'nogui'],
'horizon': [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'],
}
def client(port, scenario, out):
oh.run_logged([*java, '127.0.0.1', port, build / 'services-private.der', out, scenario, services.url],
build, out.stem, timeout=300)
differences = []
try:
for scenario in SCENARIOS:
results = {}
for label, command in servers.items():
results[label] = run_side(label, scenario, command, build / f'{label}-{scenario}', client)
transcript, log = results[label]
(build / f'{label}-{scenario}-all.txt').write_text('\n'.join(transcript + ['--- log'] + log) + '\n',
encoding='utf-8')
import difflib
for part, index in (('transcript', 0), ('log', 1)):
diff = [l for l in difflib.unified_diff(results['vanilla'][index], results['horizon'][index],
'vanilla', 'horizon', n=0, lineterm='')
if l[:1] in '+-' and l[:3] not in ('+++', '---')]
if diff:
differences.append(f'{scenario} {part}:\n ' + '\n '.join(diff[:60]))
errors = [l for l in results['vanilla'][0] if l.startswith('error')]
if errors:
differences.append(f'{scenario}: the vanilla run failed: {errors}')
finally:
services.stop()
if differences:
raise AssertionError('\n'.join(differences) + f'\n(transcripts in {build})')
lab.write_json(report, dict(
schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(),
java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0],
scope='Secure chat against vanilla\'s dedicated server: chat sessions validated against mock services keys, '
'signed chat with last seen and the signature cache, unsigned chat, /say /me /msg with signed and '
'unsigned arguments, mismatched and forged signatures, the broken chain, a bad acknowledgement; '
'enforce-secure-profile in online mode (status, login, refusals, a borrowed key); the chat log lines.'))
print('secure chat: transcripts and chat log lines identical')
if __name__ == '__main__':
try:
main()
except AssertionError as error:
sys.exit(f'{NAME}: {error}')