Fix world server authentication for AzerothCore

- Use 6-byte CMSG header (uint16 size + uint32 opcode) for client-to-server
- Enable RC4 encryption immediately after sending AUTH_SESSION
- Encrypt all 6 header bytes to match AzerothCore expectations
- Add 8-byte addon info (addonCount + clientTime) for proper parsing
- Fix CharCreateResult enum to use correct WoW 3.3.5a response codes
- Add CHAR_NAME_* error codes for name validation messages
- Allow character list refresh from CHAR_LIST_RECEIVED state
This commit is contained in:
Kelsi 2026-02-05 21:03:11 -08:00
parent b2a1fbc42c
commit 82a44659b4
4 changed files with 272 additions and 76 deletions

View file

@ -91,40 +91,79 @@ void WorldSocket::send(const Packet& packet) {
const auto& data = packet.getData();
uint16_t opcode = packet.getOpcode();
uint16_t size = static_cast<uint16_t>(data.size());
uint16_t payloadLen = static_cast<uint16_t>(data.size());
// WotLK 3.3.5 CMSG header (6 bytes total):
// - size (2 bytes, big-endian) = payloadLen + 4 (opcode is 4 bytes for CMSG)
// - opcode (4 bytes, little-endian)
// Note: Client-to-server uses 4-byte opcode, server-to-client uses 2-byte
uint16_t sizeField = payloadLen + 4;
// Build header (6 bytes for outgoing): size(2) + opcode(4)
std::vector<uint8_t> sendData;
sendData.reserve(6 + size);
sendData.reserve(6 + payloadLen);
// Size (2 bytes, big-endian) - payload size only, does NOT include header
sendData.push_back((size >> 8) & 0xFF);
sendData.push_back(size & 0xFF);
// Size (2 bytes, big-endian)
uint8_t size_hi = (sizeField >> 8) & 0xFF;
uint8_t size_lo = sizeField & 0xFF;
sendData.push_back(size_hi);
sendData.push_back(size_lo);
// Opcode (4 bytes, big-endian)
sendData.push_back((opcode >> 24) & 0xFF);
sendData.push_back((opcode >> 16) & 0xFF);
sendData.push_back((opcode >> 8) & 0xFF);
// Opcode (4 bytes, little-endian)
sendData.push_back(opcode & 0xFF);
sendData.push_back((opcode >> 8) & 0xFF);
sendData.push_back(0); // High bytes are 0 for all WoW opcodes
sendData.push_back(0);
// Encrypt header if encryption is enabled
// Debug: log encryption state and header
LOG_DEBUG("SEND opcode=0x", std::hex, opcode, std::dec,
" encryptionEnabled=", encryptionEnabled,
" header=[", std::hex,
(int)sendData[0], " ", (int)sendData[1], " ",
(int)sendData[2], " ", (int)sendData[3], " ",
(int)sendData[4], " ", (int)sendData[5], std::dec, "]",
" sizeField=", sizeField, " payloadLen=", payloadLen);
// Encrypt header if encryption is enabled (all 6 bytes)
if (encryptionEnabled) {
uint8_t plainHeader[6];
memcpy(plainHeader, sendData.data(), 6);
encryptCipher.process(sendData.data(), 6);
LOG_DEBUG("Encrypted outgoing header: opcode=0x", std::hex, opcode, std::dec);
LOG_DEBUG("Encrypted header: plain=[", std::hex,
(int)plainHeader[0], " ", (int)plainHeader[1], " ",
(int)plainHeader[2], " ", (int)plainHeader[3], " ",
(int)plainHeader[4], " ", (int)plainHeader[5], "] -> enc=[",
(int)sendData[0], " ", (int)sendData[1], " ",
(int)sendData[2], " ", (int)sendData[3], " ",
(int)sendData[4], " ", (int)sendData[5], "]", std::dec);
}
// Add payload (unencrypted)
sendData.insert(sendData.end(), data.begin(), data.end());
LOG_DEBUG("Sending world packet: opcode=0x", std::hex, opcode, std::dec,
" size=", size, " bytes (", sendData.size(), " total)");
" payload=", payloadLen, " bytes (", sendData.size(), " total)");
// Debug: dump packet bytes for AUTH_SESSION
if (opcode == 0x1ED) {
std::string hexDump = "AUTH_SESSION raw bytes: ";
for (size_t i = 0; i < sendData.size(); ++i) {
char buf[4];
snprintf(buf, sizeof(buf), "%02x ", sendData[i]);
hexDump += buf;
if ((i + 1) % 32 == 0) hexDump += "\n";
}
LOG_DEBUG(hexDump);
}
// Send complete packet
ssize_t sent = net::portableSend(sockfd, sendData.data(), sendData.size());
if (sent < 0) {
LOG_ERROR("Send failed: ", net::errorString(net::lastError()));
} else if (static_cast<size_t>(sent) != sendData.size()) {
LOG_WARNING("Partial send: ", sent, " of ", sendData.size(), " bytes");
} else {
LOG_DEBUG("Actually sent ", sent, " bytes to server");
if (static_cast<size_t>(sent) != sendData.size()) {
LOG_WARNING("Partial send: ", sent, " of ", sendData.size(), " bytes");
}
}
}
@ -167,12 +206,19 @@ void WorldSocket::tryParsePackets() {
decryptCipher.process(header, 4);
}
// Parse header (big-endian)
// Parse header
// Size: 2 bytes big-endian (includes opcode, so payload = size - 2)
uint16_t size = (header[0] << 8) | header[1];
uint16_t opcode = (header[2] << 8) | header[3];
// Opcode: 2 bytes little-endian
uint16_t opcode = header[2] | (header[3] << 8);
// Total packet size: header(4) + payload(size)
size_t totalSize = 4 + size;
LOG_DEBUG("RECV encryptionEnabled=", encryptionEnabled,
" header=[", std::hex, (int)header[0], " ", (int)header[1], " ",
(int)header[2], " ", (int)header[3], std::dec, "]",
" -> size=", size, " opcode=0x", std::hex, opcode, std::dec);
// Total packet size: size field (2) + size value (which includes opcode + payload)
size_t totalSize = 2 + size;
if (receiveBuffer.size() < totalSize) {
// Not enough data yet
@ -183,12 +229,23 @@ void WorldSocket::tryParsePackets() {
// We have a complete packet!
LOG_DEBUG("Parsing world packet: opcode=0x", std::hex, opcode, std::dec,
" size=", size, " bytes");
" size=", size, " totalSize=", totalSize, " bytes");
// Extract payload (skip header)
std::vector<uint8_t> packetData(receiveBuffer.begin() + 4,
receiveBuffer.begin() + totalSize);
// Log first few bytes of payload
if (!packetData.empty()) {
std::string payloadHex;
for (size_t i = 0; i < std::min(packetData.size(), size_t(16)); i++) {
char buf[4];
snprintf(buf, sizeof(buf), "%02x ", packetData[i]);
payloadHex += buf;
}
LOG_DEBUG("Payload (first 16 bytes): ", payloadHex);
}
// Create packet with opcode and payload
Packet packet(opcode, packetData);
@ -208,13 +265,14 @@ void WorldSocket::initEncryption(const std::vector<uint8_t>& sessionKey) {
return;
}
LOG_INFO("Initializing world server header encryption");
LOG_INFO(">>> ENABLING ENCRYPTION - encryptionEnabled will become true <<<");
// Convert hardcoded keys to vectors
std::vector<uint8_t> encryptKey(ENCRYPT_KEY, ENCRYPT_KEY + 16);
std::vector<uint8_t> decryptKey(DECRYPT_KEY, DECRYPT_KEY + 16);
// Compute HMAC-SHA1(key, sessionKey) for each cipher
// Compute HMAC-SHA1(seed, sessionKey) for each cipher
// The 16-byte seed is the HMAC key, session key is the message
std::vector<uint8_t> encryptHash = auth::Crypto::hmacSHA1(encryptKey, sessionKey);
std::vector<uint8_t> decryptHash = auth::Crypto::hmacSHA1(decryptKey, sessionKey);