fix: session keys are 16 bytes sent as 32 hex characters

The key was a 32 bit number, sent to the client as the MD5 hash of its
decimal text. The client only stores the key and sends it back, so it
is now 16 bytes from the system entropy source written as 32 lowercase
hex characters, which fills the same 33 character field of the login
response.

The auth, master and world servers pass the key itself between them as
a 33 character string in place of the number: SET_SESSION_KEY,
SESSION_KEY_RESPONSE and NEW_SESSION_ALERT keep their IDs and carry the
string where the number was. The master server keeps one key for each
username, and a world server compares the key a client sends with the
one from the master server in constant time. A key stays valid until
the next login of its account replaces it, as before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-10 15:37:08 -06:00
parent 66e6137a9f
commit 26b188852f
6 changed files with 145 additions and 36 deletions

View File

@@ -873,19 +873,16 @@ void HandlePacketChat(Packet* packet) {
namespace {
void OnSessionKeyResponse(const MasterPackets::SessionKeyResponse& response, const SystemAddress& masterAddr) {
//Read our session key and to which user it belongs:
const uint32_t sessionKey = response.sessionKey;
const LUString& sessionKey = response.sessionKey;
const LUWString& username = response.username;
//Find them:
auto it = g_PendingUsers.find(username.GetAsString());
if (it == g_PendingUsers.end()) return;
//Convert our key:
std::string userHash = std::to_string(sessionKey);
userHash = md5(userHash);
//Verify it:
if (userHash != it->second.hash) {
//Verify it, the key the client sent has to be the one master holds:
const std::string& userHash = sessionKey.string;
if (userHash.empty() || !GeneralUtils::EqualsInConstantTime(userHash, it->second.hash)) {
LOG("Session key mismatch for user %s", username.GetAsString().c_str());
Game::server->Disconnect(it->second.sysAddr, eServerDisconnectIdentifiers::INVALID_SESSION_KEY);
return;
@@ -958,7 +955,7 @@ namespace {
}
void OnNewSessionAlert(const MasterPackets::NewSessionAlert& alert, const SystemAddress& sysAddr) {
const uint32_t sessionKey = alert.sessionKey;
const LUString& sessionKey = alert.sessionKey;
const LUString& username = alert.username;
LOG("Got new session alert for user %s", username.string.c_str());
//Find them:
@@ -969,7 +966,7 @@ namespace {
}
//Check the key:
if (sessionKey != std::atoi(user->GetSessionKey().c_str())) {
if (!GeneralUtils::EqualsInConstantTime(sessionKey.string, user->GetSessionKey())) {
LOG("But the session key is invalid!", username.string.c_str());
Game::server->Disconnect(user->GetSystemAddress(), eServerDisconnectIdentifiers::INVALID_SESSION_KEY);
return;