Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-11 16:30:05 +03:00
1 parent 38e86d4a07
commit 6dddf513b3
6 files changed
+600 -3

No files matched your search

+73
View File
@@ -4,6 +4,8 @@
#include "echoion/protocol/Util.hpp"
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>
using namespace Echoion::Protocol;
@@ -50,3 +52,74 @@ TEST_CASE(identity_save_load_roundtrip)
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/echoion_ts_identity.ini";
const std::string nativePath = "/tmp/echoion_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/echoion_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());
}