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:
@@ -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()));
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user