#include "Void/TeamSpeak/Protocol/License.hpp" #include "Void/TeamSpeak/Protocol/Crypto.hpp" #include #include #include #include namespace Void::TeamSpeak::Protocol { namespace { [[noreturn]] void fail(const char* what) { throw std::runtime_error(std::string("License: ") + what); } void ensureSodium() { static std::once_flag once; static bool ok = false; std::call_once(once, [] { ok = sodium_init() >= 0; }); if (!ok) { fail("libsodium init failed"); } } // Ed25519 point compressed root key. constexpr std::array LICENSE_ROOT_KEY = { 0xcd, 0x0d, 0xe2, 0xae, 0xd4, 0x63, 0x45, 0x50, 0x9a, 0x7e, 0x3c, 0xfd, 0x8f, 0x68, 0xb3, 0xdc, 0x75, 0x55, 0xb2, 0x9d, 0xcc, 0xec, 0x73, 0xcd, 0x18, 0x75, 0x0f, 0x99, 0x38, 0x12, 0x40, 0x8a}; size_t findNullTerminator(ByteView data, size_t start) { for (size_t i = start; i < data.size(); ++i) { if (data[i] == 0) { return i - start; } } fail("non-null-terminated string in license"); } } // namespace std::vector parseLicense(ByteView data) { if (data.size() < 1) { fail("license too short"); } if (data[0] != 1) { fail("unsupported license version"); } std::vector blocks; size_t pos = 1; while (pos < data.size()) { if (data.size() - pos < 42) { fail("license block too short"); } if (data[pos] != 0) { fail("wrong key kind"); } LicenseBlock block; std::memcpy(block.key.data(), data.data() + pos + 1, 32); const uint8_t blockType = data[pos + 33]; block.type = blockType; size_t extraLen = 0; switch (blockType) { case 0: // Intermediate extraLen = findNullTerminator(data, pos + 46) + 5; break; case 2: // Server extraLen = findNullTerminator(data, pos + 47) + 6; break; case 8: { // TS5 Server size_t p = pos + 44; const uint8_t propCount = data[pos + 43]; for (uint8_t i = 0; i < propCount; ++i) { const uint8_t propLen = data[p]; p += 1 + propLen; } extraLen = p - (pos + 42); break; } case 32: // Ephemeral extraLen = 0; break; default: fail("invalid license block type"); } const size_t blockLen = 42 + extraLen; if (pos + blockLen > data.size()) { fail("license block overruns buffer"); } auto content = data.subspan(pos + 1, blockLen - 1); auto h = Crypto::sha512(content); std::memcpy(block.hash.data(), h.data(), 32); blocks.push_back(block); pos += blockLen; } return blocks; } std::array deriveLicenseKey(const std::vector& blocks) { ensureSodium(); std::array parentKey = LICENSE_ROOT_KEY; for (const auto& block : blocks) { // Clamp the block hash into an Ed25519 scalar. std::array scalar = block.hash; scalar[0] &= 0xf8; scalar[31] &= 0x3f; scalar[31] |= 0x40; std::array mul{}; if (crypto_scalarmult_ed25519_noclamp(mul.data(), scalar.data(), block.key.data()) != 0) { fail("ed25519 scalarmult failed"); } std::array sum{}; if (crypto_core_ed25519_add(sum.data(), mul.data(), parentKey.data()) != 0) { fail("ed25519 point add failed"); } parentKey = sum; } return parentKey; } TempKey generateTemporaryKey() { ensureSodium(); TempKey out; randombytes_buf(out.privateKey.data(), out.privateKey.size()); out.privateKey[0] &= 248; out.privateKey[31] &= 127; out.privateKey[31] |= 64; if (crypto_scalarmult_ed25519_base_noclamp(out.publicKey.data(), out.privateKey.data()) != 0) { fail("ed25519 base mult failed"); } return out; } std::array getSharedSecret2(ByteView serverDerivedKey, ByteView tempPrivateKey) { ensureSodium(); if (serverDerivedKey.size() != 32 || tempPrivateKey.size() != 32) { fail("bad key length for shared secret"); } std::array priv{}; std::memcpy(priv.data(), tempPrivateKey.data(), 32); priv[31] &= 0x7f; std::array mul{}; if (crypto_scalarmult_ed25519_noclamp(mul.data(), priv.data(), serverDerivedKey.data()) != 0) { fail("ed25519 scalarmult failed"); } return Crypto::sha512(ByteView(mul.data(), mul.size())); } } // namespace Void::TeamSpeak::Protocol