Dedicated server: online mode, and refused logins in the log

online-mode=true now authenticates players as vanilla's
ServerLoginPacketListenerImpl does. The server makes a 1024-bit RSA key
pair at startup ("Generating keypair"), sends it with a random challenge,
checks the challenge and decrypts the shared secret from the client's key
packet, encrypts the connection with AES/CFB8, and asks the session
service's verify endpoint (hasJoined, through the services discovery
document) whether the player joined with the server's hash, from the
"User Authenticator #N" thread. A verified profile keeps its UUID and
properties, so skins show in the player list; an unverified one is
refused with "Failed to verify username!", and an unreachable service
with authservers_down. The offline-mode warnings are only printed in
offline mode.

Refused logins are logged as vanilla logs them: "Disconnecting
<name> (/<address>): <reason>" and "<name> (/<address>) lost
connection: <reason>", with log-ips respected.

horizon-protocol gains RSA key generation (Miller-Rabin primes,
e = 65537), the X.509 encoding of the public key and PKCS#1 v1.5
decryption. test_online_mode_parity.py runs its logins against Horizon's
server too, plus a verified player the whitelist refuses, and compares
the two servers' log lines about the player.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-16 22:59:36 +02:00
parent f535d1b48c
commit e7003e7d49
10 changed files with 713 additions and 64 deletions
+95
View File
@@ -232,6 +232,87 @@ pub fn join_server(session: &AuthSession, server_hash: &str) -> Result<(), JoinE
}
}
/// What the session service says about a player joining this server
/// (MinecraftServicesSessionService.hasJoinedServer).
#[derive(Clone, Debug, PartialEq)]
pub struct JoinedProfile {
pub uuid: u128,
pub properties: Vec<horizon_protocol::packets::login::Property>,
}
/// HttpDiscoveryService.buildQuery: `URLEncoder.encode(value, UTF-8)`.
fn url_encode(text: &str) -> String {
let mut out = String::new();
for b in text.bytes() {
match b {
b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'.' | b'-' | b'*' | b'_' => {
out.push(char::from(b));
}
b' ' => out.push('+'),
_ => out.push_str(&format!("%{b:02X}")),
}
}
out
}
/// The session service's verify endpoint, asked by an online-mode server
/// whether `name` joined with `server_hash` (and from `ip`, when the server
/// prevents proxy connections). `Ok(None)`: it didn't, or the service said
/// no; `Err(Unavailable)`: no answer.
pub fn has_joined(
name: &str,
server_hash: &str,
ip: Option<std::net::IpAddr>,
) -> Result<Option<JoinedProfile>, JoinError> {
let doc = discovery()?;
let url = endpoint(&doc, "session", "verify").ok_or(JoinError::Unavailable)?;
let mut query = format!(
"username={}&serverId={}",
url_encode(name),
url_encode(server_hash)
);
if let Some(ip) = ip {
query.push_str(&format!("&ip={}", url_encode(&ip.to_string())));
}
let separator = if url.contains('?') { '&' } else { '?' };
let body = match agent().get(&format!("{url}{separator}{query}")).call() {
Ok(response) => response.into_string().unwrap_or_default(),
Err(ureq::Error::Status(status, response)) => {
let error =
JoinError::from_response(status, &response.into_string().unwrap_or_default());
return match error {
JoinError::Unavailable => Err(error),
_ => Ok(None),
};
}
Err(ureq::Error::Transport(_)) => return Err(JoinError::Unavailable),
};
Ok(parse_joined(&body))
}
/// HasJoinedMinecraftServerResponse: `id` and `properties`; no id is no profile.
pub fn parse_joined(body: &str) -> Option<JoinedProfile> {
let v: serde_json::Value = serde_json::from_str(body).ok()?;
let uuid = parse_uuid(v.get("id")?.as_str()?)?;
let properties = v
.get("properties")
.and_then(|p| p.as_array())
.into_iter()
.flatten()
.filter_map(|p| {
Some(horizon_protocol::packets::login::Property {
name: p.get("name")?.as_str()?.to_owned(),
value: p.get("value")?.as_str()?.to_owned(),
signature: p
.get("signature")
.and_then(|s| s.as_str())
.map(str::to_owned),
})
})
.collect();
Some(JoinedProfile { uuid, properties })
}
#[cfg(test)]
mod tests {
use super::*;
@@ -250,6 +331,20 @@ mod tests {
path
}
#[test]
fn verify_answers() {
assert_eq!(url_encode("Tester_1"), "Tester_1");
assert_eq!(url_encode("-5a f"), "-5a+f");
assert_eq!(url_encode("::1"), "%3A%3A1");
let body = r#"{"id":"069a79f444e94726a5befca90e38aaf5","name":"Notch","properties":[{"name":"textures","value":"e30=","signature":"c2ln"}]}"#;
let p = parse_joined(body).unwrap();
assert_eq!(p.uuid, UUID);
assert_eq!(p.properties.len(), 1);
assert_eq!(p.properties[0].signature.as_deref(), Some("c2ln"));
assert_eq!(parse_joined(""), None);
assert_eq!(parse_joined(r#"{"name":"Notch"}"#), None);
}
#[test]
fn session_files_are_read_once_and_must_be_private() {
let dir = std::env::temp_dir().join(format!("horizon-auth-test-{}", std::process::id()));
+37 -19
View File
@@ -9,7 +9,7 @@
use super::{Game, Options, lan::LanSettings};
use horizon_client::{
Result,
lan::{LoginGate, NetConfig, hub::HubConfig},
lan::{LoginGate, NetConfig, NetLog, OnlineMode, hub::HubConfig},
server::{
eula,
java_properties::now_millis,
@@ -60,6 +60,11 @@ static SETUP: OnceLock<Mutex<Option<Setup>>> = OnceLock::new();
/// Log4j's console pattern: `[HH:mm:ss] [thread/LEVEL]: message`.
pub(super) fn log(level: &str, message: &str) {
log_from("Server thread", level, message);
}
/// A line from another of vanilla's threads (the login's User Authenticator).
fn log_from(thread: &str, level: &str, message: &str) {
let file_level = match level {
"WARN" => ::log::Level::Warn,
"ERROR" => ::log::Level::Error,
@@ -73,7 +78,7 @@ pub(super) fn log(level: &str, message: &str) {
});
let secs = now_millis().div_euclid(1000).rem_euclid(86_400);
println!(
"[{:02}:{:02}:{:02}] [Server thread/{level}]: {message}",
"[{:02}:{:02}:{:02}] [{thread}/{level}]: {message}",
secs / 3600,
secs / 60 % 60,
secs % 60
@@ -354,6 +359,19 @@ impl Game<'_> {
),
);
let port = port.unwrap_or(props.server_port as u16);
// MinecraftServer.initializeKeyPair (only online mode uses it).
log("INFO", "Generating keypair");
let online = if props.online_mode {
let key = horizon_protocol::rsa::RsaPrivateKey::generate(1024)
.map_err(|e| format!("Failed to generate key pair: {e}"))?;
Some(Arc::new(OnlineMode {
public_der: key.public_der(),
key,
prevent_proxy: props.prevent_proxy_connections,
}))
} else {
None
};
log(
"INFO",
&format!(
@@ -365,27 +383,21 @@ impl Game<'_> {
}
),
);
if props.online_mode {
// Authentication needs Mojang's session servers (an HTTPS client
// and RSA), which Horizon does not have yet: offline mode.
if online.is_none() {
log("WARN", "**** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!");
log(
"WARN",
"online-mode=true is not supported by Horizon yet: running in offline mode",
"The server will make no attempt to authenticate usernames. Beware.",
);
log(
"WARN",
"While this makes the game possible to play without internet access, it also opens up the ability for hackers to connect with any username they choose.",
);
log(
"WARN",
"To change this, set \"online-mode\" to \"true\" in the server.properties file.",
);
}
log("WARN", "**** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!");
log(
"WARN",
"The server will make no attempt to authenticate usernames. Beware.",
);
log(
"WARN",
"While this makes the game possible to play without internet access, it also opens up the ability for hackers to connect with any username they choose.",
);
log(
"WARN",
"To change this, set \"online-mode\" to \"true\" in the server.properties file.",
);
let mut lists = Lists::load();
lists.use_whitelist = props.white_list;
lists.enforce_whitelist = props.enforce_whitelist;
@@ -451,6 +463,12 @@ impl Game<'_> {
.any(|e| e.user.id == uuid && e.bypasses_player_limit)
})),
gate: Some(gate),
online,
log: Some(NetLog {
write: Arc::new(log_from),
lang: Arc::new(horizon_client::net::load_language(&self.options.resources)),
log_ips: props.log_ips,
}),
};
let config = HubConfig {
default_game_mode: game_mode,
@@ -735,6 +735,7 @@ mod tab_list_tests {
conn: uuid as u32,
uuid,
name: name.into(),
properties: Vec::new(),
info: ClientInformation::new(8),
brand: "test".into(),
sender: PlayerSender {
+178 -9
View File
@@ -1,4 +1,5 @@
//! One accepted connection: handshake, status ping, offline-mode login and the
//! One accepted connection: handshake, status ping, login (offline, or online
//! with encryption and the session service's verdict) and the
//! configuration phase (ServerStatusPacketListenerImpl,
//! ServerLoginPacketListenerImpl, ServerConfigurationPacketListenerImpl). A
//! player that completes configuration continues in the play state: the
@@ -12,7 +13,7 @@ use super::{
use horizon_protocol::{
PROTOCOL_VERSION,
connection::{ConnReader, ConnWriter, Connection},
crypto::offline_uuid,
crypto::{offline_uuid, server_hash, uuid_string},
packets::{
Empty,
configuration::{self as cfg, ClientInformation, KnownPack},
@@ -24,7 +25,7 @@ use std::{
net::TcpStream,
sync::{
Arc,
atomic::{AtomicI32, AtomicI64, AtomicUsize, Ordering},
atomic::{AtomicI32, AtomicI64, AtomicU32, AtomicUsize, Ordering},
mpsc::{Receiver, RecvTimeoutError, Sender, channel},
},
time::{Duration, Instant},
@@ -305,6 +306,145 @@ fn serve_status(c: &mut Connection, shared: &Shared) -> Result<(), String> {
}
}
/// ServerLoginPacketListenerImpl.getUserName: the name and the address
/// (Connection.getLoggableAddress, `/ip:port` as InetSocketAddress prints it).
fn user_name(name: &str, address: std::net::SocketAddr, log: &super::NetLog) -> String {
if log.log_ips {
format!("{name} (/{address})")
} else {
format!("{name} (<ip address withheld>)")
}
}
/// ServerLoginPacketListenerImpl.disconnect: log the reason's text and send
/// it. Returns the text, for the "lost connection" line that follows.
fn refuse(
c: &mut Connection,
shared: &Shared,
thread: &str,
user: &str,
reason: &horizon_world::nbt::Tag,
) -> String {
let text = shared
.config
.log
.as_ref()
.map(|log| horizon_protocol::text::plain(reason, &log.lang))
.unwrap_or_default();
if let Some(log) = &shared.config.log {
(log.write)(thread, "INFO", &format!("Disconnecting {user}: {text}"));
}
login_disconnect(c, reason);
text
}
/// ServerLoginPacketListenerImpl.onDisconnect, once the server thread sees
/// the connection closed: by a refusal, or by the client ("Disconnected").
fn lost(shared: &Shared, user: &str, text: &str) {
if let Some(log) = &shared.config.log {
(log.write)(
"Server thread",
"INFO",
&format!("{user} lost connection: {text}"),
);
}
}
/// UNIQUE_THREAD_ID: the User Authenticator threads are numbered from 1.
static AUTHENTICATORS: AtomicU32 = AtomicU32::new(0);
/// ServerLoginPacketListenerImpl.handleKey and the User Authenticator
/// thread: check the client's answer to the encryption request, encrypt the
/// connection, and ask the session service whether the player joined with
/// this server's hash. The profile, or the text of the refusal.
fn authenticate(
c: &mut Connection,
shared: &Shared,
online: &super::OnlineMode,
name: &str,
address: std::net::SocketAddr,
user: &str,
) -> Result<Result<(u128, Vec<login::Property>), String>, String> {
let e = |e: horizon_protocol::Error| e.to_string();
let mut challenge = [0u8; 4];
horizon_protocol::crypto::random_bytes(&mut challenge).map_err(e)?;
c.send(
&login::Clientbound::Hello(login::Hello {
server_id: String::new(),
public_key: online.public_der.clone(),
challenge: challenge.to_vec(),
should_authenticate: true,
})
.encode(),
)
.map_err(e)?;
let (id, payload) = c.recv().map_err(e)?;
let key = match login::Serverbound::decode(id, &payload).map_err(e)? {
login::Serverbound::Key(k) => k,
_ => return Err("Unexpected packet during login".into()),
};
// ServerboundKeyPacket.isChallengeValid and getSecretKey.
let protocol_error = || "Protocol error".to_owned();
if online.key.decrypt(&key.challenge).ok().as_deref() != Some(&challenge[..]) {
return Err(protocol_error());
}
let secret: [u8; 16] = online
.key
.decrypt(&key.key)
.ok()
.and_then(|s| s.try_into().ok())
.ok_or_else(protocol_error)?;
let digest = server_hash("", &secret, &online.public_der);
c.enable_encryption(&secret);
let thread = format!(
"User Authenticator #{}",
AUTHENTICATORS.fetch_add(1, Ordering::Relaxed) + 1
);
let log = |level: &str, line: &str| {
if let Some(log) = &shared.config.log {
(log.write)(&thread, level, line);
}
};
let ip = online.prevent_proxy.then(|| address.ip());
match crate::auth::has_joined(name, &digest, ip) {
Ok(Some(profile)) => {
log(
"INFO",
&format!("UUID of player {name} is {}", uuid_string(profile.uuid)),
);
Ok(Ok((profile.uuid, profile.properties)))
}
Ok(None) => {
let text = refuse(
c,
shared,
&thread,
user,
&translatable("multiplayer.disconnect.unverified_username", &[]),
);
log(
"ERROR",
&format!("Username '{name}' tried to join with an invalid session"),
);
Ok(Err(text))
}
Err(_) => {
let text = refuse(
c,
shared,
&thread,
user,
&translatable("multiplayer.disconnect.authservers_down", &[]),
);
log(
"ERROR",
"Couldn't verify username because servers are unavailable",
);
Ok(Err(text))
}
}
}
fn serve_login(
mut c: Connection,
address: std::net::SocketAddr,
@@ -323,14 +463,36 @@ fn serve_login(
);
return Ok(());
}
// Offline mode: the profile is derived from the name (PlayerList.canPlayerLogin
// then rejects duplicates and a full server).
let uuid = offline_uuid(&hello.name);
let user = shared
.config
.log
.as_ref()
.map(|log| user_name(&hello.name, address, log))
.unwrap_or_default();
// handleHello: online mode authenticates first; offline mode derives the
// profile from the name.
let (uuid, properties) = match &shared.config.online {
Some(online) => match authenticate(&mut c, shared, online, &hello.name, address, &user) {
Ok(Ok(profile)) => profile,
Ok(Err(refused)) => {
lost(shared, &user, &refused);
return Ok(());
}
Err(err) => {
lost(shared, &user, "Disconnected");
return Err(err);
}
},
None => (offline_uuid(&hello.name), Vec::new()),
};
// verifyLoginAndFinishConnectionSetup: PlayerList.canPlayerLogin, then
// duplicates and a full server.
if let Some(reason) = shared.try_admit(address, uuid, &hello.name) {
login_disconnect(&mut c, &reason);
let text = refuse(&mut c, shared, "Server thread", &user, &reason);
lost(shared, &user, &text);
return Ok(());
}
let result = login_and_configure(&mut c, shared, uuid, &hello.name);
let result = login_and_configure(&mut c, shared, uuid, &hello.name, properties);
match result {
Ok(Some(parts)) => {
play(c, shared, parts, address);
@@ -342,6 +504,9 @@ fn serve_login(
}
Err(err) => {
shared.release(uuid);
// ServerConfigurationPacketListenerImpl.onDisconnect.
let profile = format!("{} ({})", hello.name, uuid_string(uuid));
lost(shared, &profile, "Disconnected");
Err(err)
}
}
@@ -350,6 +515,7 @@ fn serve_login(
struct Parts {
uuid: u128,
name: String,
properties: Vec<login::Property>,
info: ClientInformation,
brand: String,
}
@@ -359,6 +525,7 @@ fn login_and_configure(
shared: &Arc<Shared>,
uuid: u128,
name: &str,
properties: Vec<login::Property>,
) -> Result<Option<Parts>, String> {
let e = |e: horizon_protocol::Error| e.to_string();
let threshold = shared.config.compression_threshold;
@@ -372,7 +539,7 @@ fn login_and_configure(
profile: login::GameProfile {
uuid,
name: name.to_owned(),
properties: Vec::new(),
properties: properties.clone(),
},
session: uuid,
})
@@ -451,6 +618,7 @@ fn login_and_configure(
Ok(Some(Parts {
uuid,
name: name.to_owned(),
properties,
info,
brand,
}))
@@ -480,6 +648,7 @@ fn play(c: Connection, shared: &Arc<Shared>, parts: Parts, address: std::net::So
conn: conn_id,
uuid: parts.uuid,
name: parts.name,
properties: parts.properties,
info: parts.info,
brand: parts.brand,
sender,
+3
View File
@@ -36,6 +36,8 @@ pub struct Guest {
pub left_reason: String,
pub uuid: u128,
pub name: String,
/// The profile's properties, for the player list (skins).
pub properties: Vec<horizon_protocol::packets::login::Property>,
pub entity_id: i32,
pub sender: PlayerSender,
pub latency: Arc<AtomicI32>,
@@ -130,6 +132,7 @@ impl Guest {
left_reason: String::new(),
uuid: j.uuid,
name: j.name,
properties: j.properties,
entity_id,
sender: j.sender,
latency: j.latency,
+6 -1
View File
@@ -389,7 +389,12 @@ impl Hub {
play::PlayerInfo {
uuid,
name: Some(name.to_owned()),
properties: Vec::new(),
properties: self
.guests
.iter()
.find(|g| g.uuid == uuid)
.map(|g| g.properties.clone())
.unwrap_or_default(),
chat_session: None,
game_mode: Some(mode),
listed: Some(true),
+28
View File
@@ -58,6 +58,8 @@ pub struct Joined {
pub conn: u32,
pub uuid: u128,
pub name: String,
/// The profile's properties (skin textures, in online mode).
pub properties: Vec<horizon_protocol::packets::login::Property>,
pub info: ClientInformation,
pub brand: String,
pub sender: PlayerSender,
@@ -79,6 +81,27 @@ pub enum Out {
pub type LoginGate =
Arc<dyn Fn(SocketAddr, u128, &str) -> Option<horizon_world::nbt::Tag> + Send + Sync>;
/// online-mode=true: the key pair the server made at startup
/// (MinecraftServer.initializeKeyPair) and how players are verified.
pub struct OnlineMode {
pub key: horizon_protocol::rsa::RsaPrivateKey,
/// The public key as X.509 (PublicKey.getEncoded), sent in the hello.
pub public_der: Vec<u8>,
/// prevent-proxy-connections: the session service also checks the address.
pub prevent_proxy: bool,
}
/// The dedicated server's log for the login phase: (thread, level, line)
/// written as vanilla's Log4j would, and the language that turns a
/// disconnect reason into its text (Component.getString).
#[derive(Clone)]
pub struct NetLog {
pub write: Arc<dyn Fn(&str, &str, &str) + Send + Sync>,
pub lang: Arc<horizon_protocol::text::Language>,
/// log-ips: addresses in the log, or "<ip address withheld>".
pub log_ips: bool,
}
/// How the server side of the network behaves: an integrated server's LAN
/// uses the defaults, a dedicated server takes them from server.properties.
#[derive(Clone)]
@@ -107,6 +130,9 @@ pub struct NetConfig {
/// Ops bypass a full server (ServerOpList.canBypassPlayerLimit).
pub bypasses_limit: Option<Arc<dyn Fn(u128) -> bool + Send + Sync>>,
pub gate: Option<LoginGate>,
/// Online mode: players are authenticated (none on a LAN world).
pub online: Option<Arc<OnlineMode>>,
pub log: Option<NetLog>,
}
impl NetConfig {
/// IntegratedServer.publishServer.
@@ -126,6 +152,8 @@ impl NetConfig {
listen: true,
bypasses_limit: None,
gate: None,
online: None,
log: None,
}
}
}
+279 -1
View File
@@ -1,7 +1,9 @@
//! RSA for the online-mode login: the server's X.509 public key
//! (Crypt.byteToPublicKey) and PKCS#1 v1.5 encryption of the shared secret
//! and challenge (Crypt.encryptUsingKey, Java's "RSA" cipher =
//! RSA/ECB/PKCS1Padding). Modular exponentiation uses Montgomery
//! 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.
use crate::{Error, Result};
@@ -43,6 +45,46 @@ impl Big {
}
true
}
/// self · other, as many limbs as both together.
fn mul(&self, other: &Self) -> Self {
let mut out = vec![0u32; self.0.len() + other.0.len()];
for (i, &a) in self.0.iter().enumerate() {
let mut carry = 0u64;
for (j, &b) in other.0.iter().enumerate() {
let t = u64::from(out[i + j]) + u64::from(a) * u64::from(b) + carry;
out[i + j] = t as u32;
carry = t >> 32;
}
out[i + other.0.len()] = carry as u32;
}
Self(out)
}
/// self · m + a, one limb longer.
fn mul_small_add(&self, m: u32, a: u32) -> Self {
let mut out = Vec::with_capacity(self.0.len() + 1);
let mut carry = u64::from(a);
for &l in &self.0 {
let t = u64::from(l) * u64::from(m) + carry;
out.push(t as u32);
carry = t >> 32;
}
out.push(carry as u32);
Self(out)
}
/// (self / d, self mod d).
fn div_small(&self, d: u32) -> (Self, u32) {
let mut out = vec![0u32; self.0.len()];
let mut rem = 0u64;
for i in (0..self.0.len()).rev() {
let t = rem << 32 | u64::from(self.0[i]);
out[i] = (t / u64::from(d)) as u32;
rem = t % u64::from(d);
}
(Self(out), rem as u32)
}
fn is_one(&self) -> bool {
self.0.first() == Some(&1) && self.0[1..].iter().all(|&l| l == 0)
}
fn sub_assign(&mut self, other: &Self) {
let mut borrow = 0i64;
for (a, b) in self.0.iter_mut().zip(&other.0) {
@@ -249,6 +291,198 @@ impl RsaPublicKey {
}
}
/// 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,
101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193,
197, 199, 211, 223, 227, 229, 233, 239, 241, 251,
];
/// Miller-Rabin rounds: a composite passes all of them with odds below 2⁻⁸⁰.
const ROUNDS: usize = 40;
const E: u32 = 65537;
/// A random odd number of `limbs` limbs with its two top bits set, so two of
/// them multiply to exactly twice the bits.
fn candidate(limbs: usize) -> Result<Big> {
let mut bytes = vec![0u8; limbs * 4];
crate::crypto::random_bytes(&mut bytes)?;
bytes[0] |= 0xC0;
bytes[limbs * 4 - 1] |= 1;
Ok(Big::from_be_bytes(&bytes, limbs))
}
/// Miller-Rabin with random bases.
fn probably_prime(p: &Big) -> Result<bool> {
if SMALL_PRIMES.iter().any(|&q| p.div_small(q).1 == 0) {
return Ok(false);
}
let k = p.0.len();
let mont = Mont::new(p.clone());
let mut p_minus_one = p.clone();
p_minus_one.0[0] -= 1; // p is odd
let s = (0..k * 32).find(|&i| p_minus_one.bit(i)).unwrap_or(0);
// d = (p - 1) >> s
let mut d = Big(vec![0; k]);
for i in 0..k * 32 - s {
if p_minus_one.bit(i + s) {
d.0[i / 32] |= 1 << (i % 32);
}
}
let mut two = Big(vec![0; k]);
two.0[0] = 2;
'rounds: for _ in 0..ROUNDS {
// A random base in [2, p - 2].
let mut a = candidate(k)?;
a.0[k - 1] %= p.0[k - 1];
if !a.ge(&two) || a.ge(&p_minus_one) {
continue;
}
let mut x = mont.pow(&a, &d);
if x.is_one() || x == p_minus_one {
continue;
}
for _ in 1..s {
x = mont.pow(&x, &two);
if x == p_minus_one {
continue 'rounds;
}
if x.is_one() {
return Ok(false);
}
}
return Ok(false);
}
Ok(true)
}
/// A prime of `limbs` limbs whose p - 1 is prime to E.
fn prime(limbs: usize) -> Result<Big> {
loop {
let p = candidate(limbs)?;
if p.div_small(E).1 != 1 && probably_prime(&p)? {
return Ok(p);
}
}
}
/// DER: a tag and its length, then the contents.
fn der_element(tag: u8, contents: &[u8]) -> Vec<u8> {
let mut out = vec![tag];
let len = contents.len();
if len < 0x80 {
out.push(len as u8);
} else {
let bytes: Vec<u8> = len
.to_be_bytes()
.into_iter()
.skip_while(|&b| b == 0)
.collect();
out.push(0x80 | bytes.len() as u8);
out.extend(bytes);
}
out.extend_from_slice(contents);
out
}
/// A DER INTEGER from big-endian magnitude bytes (a leading zero keeps it positive).
fn der_integer(magnitude: &[u8]) -> Vec<u8> {
let mut v = strip(magnitude);
if v.first().is_none_or(|b| b & 0x80 != 0) {
v.insert(0, 0);
}
der_element(0x02, &v)
}
/// The server's key pair (Crypt.generateKeyPair).
pub struct RsaPrivateKey {
pub public: RsaPublicKey,
/// The private exponent, big-endian.
exponent: Vec<u8>,
}
impl std::fmt::Debug for RsaPrivateKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RsaPrivateKey")
.field("public", &self.public)
.finish_non_exhaustive()
}
}
impl RsaPrivateKey {
/// A new key pair with a `bits`-bit modulus (a multiple of 64) and
/// public exponent 65537, as Java's RSA KeyPairGenerator makes.
pub fn generate(bits: usize) -> Result<Self> {
let limbs = bits / 64;
let p = prime(limbs)?;
let q = loop {
let q = prime(limbs)?;
if q != p {
break q;
}
};
let n = p.mul(&q);
// d = (1 + k·φ) / E with k making it whole: k ≡ -φ⁻¹ (mod E).
let (mut p1, mut q1) = (p, q);
p1.0[0] -= 1;
q1.0[0] -= 1;
let phi = p1.mul(&q1);
let r = u64::from(phi.div_small(E).1);
let inverse = (1..u64::from(E))
.find(|k| k * r % u64::from(E) == 1)
.ok_or_else(|| Error::new("RSA key generation failed"))?;
let k = (u64::from(E) - inverse) as u32;
let (d, rem) = phi.mul_small_add(k, 1).div_small(E);
if rem != 0 {
return Err(Error::new("RSA key generation failed"));
}
let size = bits / 8;
Ok(Self {
public: RsaPublicKey {
modulus: strip(&n.to_be_bytes(size)),
exponent: strip(&E.to_be_bytes()),
},
exponent: d.to_be_bytes(size),
})
}
/// PublicKey.getEncoded: X.509 SubjectPublicKeyInfo.
pub fn public_der(&self) -> Vec<u8> {
let key = der_element(
0x30,
&[
der_integer(&self.public.modulus),
der_integer(&self.public.exponent),
]
.concat(),
);
let algorithm = der_element(
0x30,
&[der_element(0x06, RSA_ENCRYPTION), vec![0x05, 0x00]].concat(),
);
let bits = der_element(0x03, &[&[0u8][..], &key].concat());
der_element(0x30, &[algorithm, bits].concat())
}
/// Crypt.decryptUsingKey: RSAES-PKCS1-v1_5 decryption (RFC 8017 7.2.2).
pub fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>> {
let bad = || Error::new("Decryption error");
let k = self.public.size();
// The modulus has no leading zero, so equal lengths compare as numbers.
if ciphertext.len() != k || ciphertext >= self.public.modulus.as_slice() {
return Err(bad());
}
let block = self.public.apply(ciphertext, &self.exponent);
if block[0] != 0 || block[1] != 2 {
return Err(bad());
}
let zero = block[2..].iter().position(|&b| b == 0).ok_or_else(bad)? + 2;
if zero < 10 {
return Err(bad());
}
Ok(block[zero + 1..].to_vec())
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
@@ -316,6 +550,50 @@ pub(crate) mod tests {
assert!(key.encrypt(&[0; 118]).is_err());
}
/// A key made here: Java-style DER that parses back, and what its public
/// half encrypts its private half decrypts.
#[test]
fn generated_keys_round_trip() {
let key = RsaPrivateKey::generate(1024).unwrap();
assert_eq!(key.public.size(), 128);
assert_eq!(key.public.exponent, vec![1, 0, 1]);
let der = key.public_der();
assert_eq!(
&der[..3],
&[0x30, 0x81, 0x9f],
"a 1024-bit SubjectPublicKeyInfo"
);
assert_eq!(RsaPublicKey::from_der(&der).unwrap(), key.public);
for message in [&[0x5au8; 16][..], &[1, 2, 3, 4], &[]] {
let c = key.public.encrypt(message).unwrap();
assert_eq!(key.decrypt(&c).unwrap(), message);
}
assert!(key.decrypt(&[0u8; 128]).is_err());
assert!(key.decrypt(&[1u8; 64]).is_err());
}
#[test]
fn decrypts_with_a_known_key() {
let key = RsaPrivateKey {
public: RsaPublicKey::from_der(&hex(TEST_SPKI)).unwrap(),
exponent: hex(TEST_D),
};
let c = key.public.encrypt(b"secret").unwrap();
assert_eq!(key.decrypt(&c).unwrap(), b"secret");
}
#[test]
fn finds_primes() {
let p = Big(vec![0xFFFF_FFFB]); // 2^32 - 5 is prime
assert!(probably_prime(&p).unwrap());
assert!(!probably_prime(&Big(vec![0xFFFF_FFFD])).unwrap()); // 3 · 1431655765
// 2^61 - 1 (prime) and a Carmichael-free composite 2^61 + 1.
let m61 = (1u64 << 61) - 1;
assert!(probably_prime(&Big(vec![m61 as u32, (m61 >> 32) as u32])).unwrap());
let c = (1u64 << 61) + 1;
assert!(!probably_prime(&Big(vec![c as u32, (c >> 32) as u32])).unwrap());
}
/// An OpenSSL signature checked with the public exponent: the block ends
/// with SHA-256("hello"), so the modular arithmetic agrees with OpenSSL's.
#[test]
+6 -6
View File
@@ -76,9 +76,9 @@ not tick cost, so it is in scope here (see section 9).
| Area | Status |
|---|---|
| `online-mode=true` | **Not supported.** The server logs a warning and runs offline: no session `hasJoined` check, no encryption, no secure chat. (The client side of online mode works and is tested by `test_online_mode_parity.py` against vanilla's server.) A public server must not run like this. This is the top engine item. |
| `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). |
| 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 | Nothing is logged. Vanilla logs `Disconnecting <name>: <reason>` (`ServerLoginPacketListenerImpl.disconnect`) and `<name> lost connection: …`. |
| 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). |
| Defaults that surprise owners | `white-list=true` is vanilla 26.3's default, so a fresh server refuses everyone until they are added. That matches vanilla, so the first-run flow (section 3) asks about the whitelist up front. |
| Management | Only stdin. There is no service, no backups, no status beyond `list`, no restart after a crash, and a server can't be managed while it runs in the background. |
@@ -239,7 +239,7 @@ motd = "A Minecraft Server"
port = 25565
bind = "" # listen on every address, or only on this one (server-ip)
max_players = 20
online_mode = true # not supported by Horizon yet: the server runs offline and says so
online_mode = true # check players' Minecraft accounts (false: anyone joins under any name)
whitelist = true # only whitelisted players and operators can join
enforce_whitelist = false # kick online players taken off the whitelist
view_distance = 10
@@ -487,7 +487,7 @@ machine, then published in `benchmarks/`; and item 5.
**Out (next):**
- online mode on the server side (session `hasJoined`, encryption, secure chat);
- 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;
@@ -497,8 +497,8 @@ machine, then published in `benchmarks/`; and item 5.
- the website's "host a server" page;
- retiring `tools/server.py` and `docs/server-quickstart.txt` (once the benchmark replaces them).
Until online mode works, the docs say to run Horizon's server only on a LAN, a tailnet, or behind a
whitelist.
Online mode works, so a public server can check accounts. An offline server should still run only on a
LAN, a tailnet, or behind a whitelist.
## 11. Pull requests
+80 -28
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
"""Horizon's client joins vanilla's online-mode server, with no real account.
"""Horizon's client joins vanilla's online-mode server and Horizon's, with no real account.
A small mock of the Minecraft services (dev only: the discovery document and
the session service's join and hasJoined endpoints) stands in for Mojang's.
@@ -20,7 +20,13 @@ in the world with its chunks, chats, runs /list, leaves).
Also checked, with vanilla's messages:
- a token the session service rejects ("Failed to log in: Invalid session");
- a join the session service forgets (the server: "Failed to verify username!");
- no account at all (the client refuses before joining).
- no account at all (the client refuses before joining);
- a verified player the whitelist refuses.
Horizon's dedicated server then runs the same steps in online mode
(`HORIZON_API_DISCOVERY` points it at the mock too): it must give the client
the same answers and log the same lines about the player as vanilla's
("UUID of player ...", "Disconnecting ...", "... lost connection: ...").
Chat is unsigned here: the mock serves no Mojang public keys, so the server
can't validate profile keys and doesn't enforce secure chat.
@@ -31,9 +37,11 @@ import os
from pathlib import Path
import platform
import shutil
import re
import subprocess
import sys
import threading
import time
import urllib.parse
try:
@@ -167,43 +175,42 @@ def run_client(binary, build, port, label, token, discovery):
got['session file left'] = (directory / 'session.json').exists()
return got
# The server's lines about the player, as both servers print them.
PLAYER_LINE = re.compile(r'^\[\d\d:\d\d:\d\d\] \[([^\]]+)\]: (.*(?:Tester|Username|Disconnecting|verify username).*)$')
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)
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')
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
services = Services()
discovery = services.url + '/minecraft/client'
def player_lines(lines):
out = []
for line in lines:
if m := PLAYER_LINE.match(line):
text = re.sub(r'/127\.0\.0\.1:\d+', '/127.0.0.1:_', m.group(2))
text = re.sub(r'entity id \d+ at \(.*\)', 'entity id _ at _', text)
out.append(f'[{m.group(1)}] {text}')
return out
def run_side(label, command, binary, build, services, discovery):
"""The five logins against one server; its checks and its log about the player."""
port = free_port()
directory = build / 'server'
directory = build / label
shutil.rmtree(directory, ignore_errors=True)
directory.mkdir()
props = dict(PROPERTIES, **{'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')
command = ['java', '-Xmx2g', '-Duser.timezone=UTC', f'-Dminecraft.api.discovery.host={discovery}',
'-cp', dependencies, 'net.minecraft.server.Main', 'nogui']
server = Server(command, directory, 'vanilla-online')
os.environ.update(HORIZON_NO_AUDIO='1', HORIZON_API_DISCOVERY=discovery)
server = Server(command, directory, f'{label}-online')
checks = []
def check(label, actual, expected):
checks.append(f'{label}: ' + ('ok' if actual == expected else f'expected {expected!r}, got {actual!r}'))
def check(name, actual, expected):
checks.append(f'{name}: ' + ('ok' if actual == expected else f'expected {expected!r}, got {actual!r}'))
try:
server.wait_for(r'Done \(', timeout=600)
services.take_log()
# 1. Signed in: the full encrypted handshake, then play.
got = run_client(binary, build, port, 'signed-in', GOOD, discovery)
got = run_client(binary, build, port, f'{label}-signed-in', GOOD, discovery)
server.wait_for(r'Tester left the game', timeout=30)
log = services.take_log()
joins = [e for e in log if e[0] == 'join']
@@ -227,27 +234,71 @@ def main():
# 2. A token the session service rejects: vanilla's message, no key sent.
mark = len(server.lines)
got = run_client(binary, build, port, 'rejected', BAD, discovery)
got = run_client(binary, build, port, f'{label}-rejected', BAD, discovery)
check('rejected token: client message', got['error'],
'disconnected: Failed to log in: Invalid session (Try restarting your game and the launcher)')
check('rejected token: no verification', [e[0] for e in services.take_log()], ['join'])
# 3. The session service forgets the join: the server refuses the player.
got = run_client(binary, build, port, 'forgotten', FORGET, discovery)
got = run_client(binary, build, port, f'{label}-forgotten', FORGET, discovery)
server.wait_for(r'Failed to verify username', timeout=30)
check('forgotten join: client message', got['error'], 'disconnected: Failed to verify username!')
check('forgotten join: server verified and refused',
[(e[0], e[3]) for e in services.take_log()], [('join', True), ('verify', False)])
# 4. No account: refused by the client before anything is sent to the services.
got = run_client(binary, build, port, 'no-account', None, discovery)
got = run_client(binary, build, port, f'{label}-no-account', None, discovery)
check('no account: client message', got['error'] or '', 'disconnected: This server is in online mode. '
'Sign in with your Microsoft account in the Horizon launcher to join it.')
check('no account: no service calls', services.take_log(), [])
check('no player joined after the first', 'joined the game' in '\n'.join(server.lines[mark:]), False)
# 5. Verified, but not on the whitelist.
server.console('whitelist on')
server.wait_for(r'Whitelist is now turned on', timeout=30)
got = run_client(binary, build, port, f'{label}-not-whitelisted', GOOD, discovery)
server.wait_for(r'Disconnecting Tester', timeout=30)
check('not whitelisted: client message', got['error'], 'disconnected: You are not white-listed on this server!')
check('not whitelisted: verified first', [(e[0], e[3]) for e in services.take_log()],
[('join', True), ('verify', True)])
time.sleep(1)
finally:
server.stop()
return checks, player_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)
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')
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
services = Services()
discovery = services.url + '/minecraft/client'
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']
checks, transcripts = [], {}
try:
for label, command in [('vanilla', vanilla), ('horizon', horizon)]:
side_checks, transcripts[label] = run_side(label, command, binary, build, services, discovery)
checks += [f'{label}: {c}' for c in side_checks]
finally:
services.stop()
for label in transcripts:
(build / f'{label}.txt').write_text('\n'.join(transcripts[label]) + '\n', encoding='utf-8')
if 'horizon' in transcripts:
import difflib
diff = [l for l in difflib.unified_diff(transcripts['vanilla'], transcripts['horizon'], 'vanilla', 'horizon',
n=0, lineterm='') if l[:1] in '+-' and l[:3] not in ('+++', '---')]
checks.append('server log about the player: ' + ('ok' if not diff else 'differs:\n ' + '\n '.join(diff)))
for line in checks:
print(line)
@@ -257,8 +308,9 @@ def main():
lab.write_json(report, dict(
schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(),
java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], checks=len(checks),
scope='Online-mode login against vanilla\'s dedicated server and a mock session service: server hash, '
'RSA key packet, AES/CFB8, session join and verify, play; rejected token, forgotten join, no account.'))
scope='Online-mode login against vanilla\'s dedicated server and Horizon\'s, with a mock session service: '
'server hash, RSA key packet, AES/CFB8, session join and verify, play; rejected token, forgotten '
'join, no account, whitelist; the servers\' log lines about the player.'))
print(f'online mode: {len(checks)} checks ok')