Files
VoidTeamspeak/tests/test_identity.cpp
T
faumaray c1a11ffe29 Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-12 02:34:34 +03:00

126 lines
4.1 KiB
C++

#include "check.hpp"
#include "Void/TeamSpeak/Protocol/Crypto.hpp"
#include "Void/TeamSpeak/Protocol/Identity.hpp"
#include "Void/TeamSpeak/Protocol/Util.hpp"
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>
using namespace Void::TeamSpeak::Protocol;
TEST_CASE(identity_generate_uid_and_level)
{
auto id = Identity::generate(8);
// uid must equal base64(sha1(ascii(publicKeyString))).
auto h = Crypto::sha1(Crypto::ByteView(
reinterpret_cast<const uint8_t*>(id.publicKeyString().data()),
id.publicKeyString().size()));
CHECK_EQ(id.uid(),
Util::base64Encode(Crypto::ByteView(h.data(), h.size())));
// publicKeyString is valid base64 of a DER SEQUENCE (starts with 0x30).
auto der = Util::base64Decode(id.publicKeyString());
CHECK(!der.empty());
CHECK_EQ(der[0], uint8_t(0x30));
// Security level actually reached the requested threshold.
CHECK(id.securityLevel() >= 8);
}
TEST_CASE(identity_sign_verifiable_length)
{
auto id = Identity::generate(0);
std::string msg = "hello";
auto sig = id.sign(Crypto::ByteView(
reinterpret_cast<const uint8_t*>(msg.data()), msg.size()));
// DER ECDSA signature over P-256 is ~70-72 bytes; never empty.
CHECK(!sig.empty());
CHECK(sig[0] == 0x30); // DER SEQUENCE
}
TEST_CASE(identity_save_load_roundtrip)
{
auto id = Identity::generate(0);
std::string path = "/tmp/void_teamspeak_identity_test.txt";
id.save(path);
auto loaded = Identity::load(path);
CHECK_EQ(loaded.publicKeyString(), id.publicKeyString());
CHECK_EQ(loaded.uid(), id.uid());
CHECK_EQ(loaded.keyOffset(), id.keyOffset());
std::remove(path.c_str());
}
namespace
{
std::string teamSpeakExportForScalarOne(uint64_t offset)
{
// libtomcrypt private-only key: flags 0xc0, 256 bits, scalar d=1.
const std::vector<uint8_t> der{0x30, 0x0a, 0x03, 0x02, 0x06, 0xc0,
0x02, 0x01, 0x20, 0x02, 0x01, 0x01};
const auto decoded = Util::base64Encode(der);
std::vector<uint8_t> obfuscated(decoded.begin(), decoded.end());
constexpr std::string_view key =
"b9dfaa7bee6ac57ac7b65f1094a1c155e747327bc2fe5d51c512023fe54a2802"
"01004e90ad1daaae1075d53b7d571c30e063b5a62a4a017bb394833aa0983e6e";
// Real exported identities are longer than 20 bytes. Pad the base64
// text with whitespace, which the decoder accepts, to exercise the
// exact TeamSpeak obfuscation algorithm.
obfuscated.resize(32, static_cast<uint8_t>(' '));
for (size_t i = 20; i < obfuscated.size(); ++i) {
obfuscated[i] ^= static_cast<uint8_t>(key[i]);
}
const auto hash = Crypto::sha1(Crypto::ByteView(
obfuscated.data() + 20, obfuscated.size() - 20));
for (size_t i = 0; i < 20; ++i) {
obfuscated[i] ^=
static_cast<uint8_t>(key[i]) ^ hash[i];
}
return std::to_string(offset) + "V" + Util::base64Encode(obfuscated);
}
}
TEST_CASE(identity_import_teamspeak_ini)
{
const std::string exportPath = "/tmp/void_teamspeak_identity.ini";
const std::string nativePath = "/tmp/void_teamspeak_imported_identity.txt";
{
std::ofstream output(exportPath);
output << "[Identity]\nidentity=\"" << teamSpeakExportForScalarOne(42)
<< "\"\n";
}
const auto imported = Identity::importTeamSpeakIni(exportPath);
CHECK_EQ(imported.keyOffset(), uint64_t{42});
CHECK(!imported.uid().empty());
CHECK(!imported.sign({reinterpret_cast<const uint8_t*>("x"), 1}).empty());
imported.save(nativePath);
const auto loaded = Identity::load(nativePath);
CHECK_EQ(loaded.uid(), imported.uid());
const auto permissions = std::filesystem::status(nativePath).permissions();
CHECK((permissions & std::filesystem::perms::group_all) ==
std::filesystem::perms::none);
CHECK((permissions & std::filesystem::perms::others_all) ==
std::filesystem::perms::none);
std::remove(exportPath.c_str());
std::remove(nativePath.c_str());
}
TEST_CASE(identity_import_rejects_malformed_export)
{
const std::string path = "/tmp/void_teamspeak_bad_identity.ini";
{
std::ofstream output(path);
output << "[Identity]\nidentity=not-a-private-key\n";
}
bool rejected = false;
try {
(void)Identity::importTeamSpeakIni(path);
} catch (const std::exception&) {
rejected = true;
}
CHECK(rejected);
std::remove(path.c_str());
}