1 parent
38e86d4a07
commit
6dddf513b3
6 files changed
+600
-3
No files matched your search
+250
-1
@@ -4,13 +4,20 @@
|
||||
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/param_build.h>
|
||||
#include <openssl/pem.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <regex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
namespace Echoion::Protocol
|
||||
{
|
||||
@@ -28,6 +35,151 @@ namespace Echoion::Protocol
|
||||
return static_cast<EVP_PKEY*>(p.get());
|
||||
}
|
||||
|
||||
struct ParsedPrivateKey {
|
||||
BIGNUM* privateKey = nullptr;
|
||||
BIGNUM* publicX = nullptr;
|
||||
BIGNUM* publicY = nullptr;
|
||||
|
||||
~ParsedPrivateKey()
|
||||
{
|
||||
BN_free(privateKey);
|
||||
BN_free(publicX);
|
||||
BN_free(publicY);
|
||||
}
|
||||
ParsedPrivateKey() = default;
|
||||
ParsedPrivateKey(const ParsedPrivateKey&) = delete;
|
||||
ParsedPrivateKey& operator=(const ParsedPrivateKey&) = delete;
|
||||
ParsedPrivateKey(ParsedPrivateKey&& other) noexcept
|
||||
: privateKey(std::exchange(other.privateKey, nullptr)),
|
||||
publicX(std::exchange(other.publicX, nullptr)),
|
||||
publicY(std::exchange(other.publicY, nullptr))
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
size_t readDerLength(Crypto::ByteView der, size_t& position)
|
||||
{
|
||||
if (position >= der.size()) {
|
||||
fail("truncated DER length");
|
||||
}
|
||||
const auto first = der[position++];
|
||||
if (first < 0x80) {
|
||||
return first;
|
||||
}
|
||||
const auto bytes = static_cast<size_t>(first & 0x7f);
|
||||
if (bytes == 0 || bytes > sizeof(size_t) ||
|
||||
position + bytes > der.size()) {
|
||||
fail("invalid DER length");
|
||||
}
|
||||
size_t length = 0;
|
||||
for (size_t i = 0; i < bytes; ++i) {
|
||||
length = (length << 8) | der[position++];
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
ParsedPrivateKey parsePrivateKeyDer(Crypto::ByteView der)
|
||||
{
|
||||
if (der.empty() || der.front() != 0x30) {
|
||||
fail("imported key is not a DER sequence");
|
||||
}
|
||||
size_t position = 1;
|
||||
const auto sequenceLength = readDerLength(der, position);
|
||||
if (position + sequenceLength != der.size()) {
|
||||
fail("invalid imported key length");
|
||||
}
|
||||
|
||||
uint8_t keyKind = 0;
|
||||
std::vector<BIGNUM*> integers;
|
||||
while (position < der.size()) {
|
||||
const auto tag = der[position++];
|
||||
const auto length = readDerLength(der, position);
|
||||
if (position + length > der.size()) {
|
||||
for (auto* value : integers) {
|
||||
BN_free(value);
|
||||
}
|
||||
fail("truncated imported key");
|
||||
}
|
||||
if (tag == 0x03) {
|
||||
if (length < 2) {
|
||||
fail("invalid imported key flags");
|
||||
}
|
||||
keyKind = der[position + 1];
|
||||
} else if (tag == 0x02) {
|
||||
integers.push_back(BN_bin2bn(
|
||||
der.data() + position, static_cast<int>(length), nullptr));
|
||||
}
|
||||
position += length;
|
||||
}
|
||||
|
||||
ParsedPrivateKey parsed;
|
||||
// INTEGER lists exclude the bit string. Public+private is
|
||||
// [32, x, y, d]; private-only is [32, d].
|
||||
if (keyKind == 0x80 && integers.size() == 4) {
|
||||
parsed.publicX = integers[1];
|
||||
parsed.publicY = integers[2];
|
||||
parsed.privateKey = integers[3];
|
||||
BN_free(integers[0]);
|
||||
} else if (keyKind == 0xc0 && integers.size() == 2) {
|
||||
parsed.privateKey = integers[1];
|
||||
BN_free(integers[0]);
|
||||
} else {
|
||||
for (auto* value : integers) {
|
||||
BN_free(value);
|
||||
}
|
||||
fail("unsupported imported identity key");
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
EVP_PKEY* makePrivateKey(ParsedPrivateKey& parsed)
|
||||
{
|
||||
std::array<uint8_t, 65> publicPoint{};
|
||||
publicPoint[0] = 0x04;
|
||||
if (!parsed.publicX || !parsed.publicY) {
|
||||
EC_GROUP* group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
|
||||
EC_POINT* point = group ? EC_POINT_new(group) : nullptr;
|
||||
if (!group || !point ||
|
||||
EC_POINT_mul(group, point, parsed.privateKey, nullptr, nullptr,
|
||||
nullptr) != 1 ||
|
||||
EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
|
||||
publicPoint.data(), publicPoint.size(), nullptr) !=
|
||||
publicPoint.size()) {
|
||||
EC_POINT_free(point);
|
||||
EC_GROUP_free(group);
|
||||
fail("cannot derive imported public key");
|
||||
}
|
||||
EC_POINT_free(point);
|
||||
EC_GROUP_free(group);
|
||||
} else {
|
||||
if (BN_bn2binpad(parsed.publicX, publicPoint.data() + 1, 32) != 32 ||
|
||||
BN_bn2binpad(parsed.publicY, publicPoint.data() + 33, 32) != 32) {
|
||||
fail("invalid imported public key");
|
||||
}
|
||||
}
|
||||
|
||||
OSSL_PARAM_BLD* builder = OSSL_PARAM_BLD_new();
|
||||
OSSL_PARAM_BLD_push_utf8_string(builder, OSSL_PKEY_PARAM_GROUP_NAME,
|
||||
"prime256v1", 0);
|
||||
OSSL_PARAM_BLD_push_octet_string(builder, OSSL_PKEY_PARAM_PUB_KEY,
|
||||
publicPoint.data(), publicPoint.size());
|
||||
OSSL_PARAM_BLD_push_BN(builder, OSSL_PKEY_PARAM_PRIV_KEY,
|
||||
parsed.privateKey);
|
||||
OSSL_PARAM* params = OSSL_PARAM_BLD_to_param(builder);
|
||||
EVP_PKEY_CTX* context =
|
||||
EVP_PKEY_CTX_new_from_name(nullptr, "EC", nullptr);
|
||||
EVP_PKEY* key = nullptr;
|
||||
const bool ok = context && EVP_PKEY_fromdata_init(context) == 1 &&
|
||||
EVP_PKEY_fromdata(context, &key, EVP_PKEY_KEYPAIR, params) == 1;
|
||||
EVP_PKEY_CTX_free(context);
|
||||
OSSL_PARAM_free(params);
|
||||
OSSL_PARAM_BLD_free(builder);
|
||||
if (!ok) {
|
||||
fail("cannot construct imported private key");
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
// Minimal big-endian encoding of a BIGNUM (no leading zeros).
|
||||
Bytes bnBytes(const BIGNUM* bn)
|
||||
{
|
||||
@@ -199,6 +351,85 @@ namespace Echoion::Protocol
|
||||
EVP_PKEY_free(static_cast<EVP_PKEY*>(p));
|
||||
}
|
||||
|
||||
Identity Identity::importTeamSpeakIni(const std::string& path)
|
||||
{
|
||||
std::ifstream input(path, std::ios::binary);
|
||||
if (!input) {
|
||||
fail("cannot open TeamSpeak identity export");
|
||||
}
|
||||
const std::string contents((std::istreambuf_iterator<char>(input)),
|
||||
std::istreambuf_iterator<char>());
|
||||
// TeamSpeak's exported value is <valid-key-offset>V<base64>. It may
|
||||
// appear quoted or as the value of an INI property.
|
||||
const std::regex pattern{R"((\d+)V([A-Za-z0-9+/]+={0,2}))"};
|
||||
std::smatch match;
|
||||
if (!std::regex_search(contents, match, pattern)) {
|
||||
fail("TeamSpeak export does not contain an identity");
|
||||
}
|
||||
|
||||
uint64_t offset = 0;
|
||||
try {
|
||||
offset = std::stoull(match[1].str());
|
||||
} catch (const std::exception&) {
|
||||
fail("invalid TeamSpeak key offset");
|
||||
}
|
||||
auto obfuscated = Util::base64Decode(match[2].str());
|
||||
if (obfuscated.size() < 20) {
|
||||
fail("TeamSpeak identity is too short");
|
||||
}
|
||||
|
||||
const auto payloadBegin = obfuscated.begin() + 20;
|
||||
const auto nullPosition =
|
||||
std::find(payloadBegin, obfuscated.end(), uint8_t{0});
|
||||
const auto hash = Crypto::sha1(Crypto::ByteView(
|
||||
&*payloadBegin, static_cast<size_t>(nullPosition - payloadBegin)));
|
||||
for (size_t i = 0; i < hash.size(); ++i) {
|
||||
obfuscated[i] ^= hash[i];
|
||||
}
|
||||
constexpr std::string_view obfuscationKey =
|
||||
"b9dfaa7bee6ac57ac7b65f1094a1c155e747327bc2fe5d51c512023fe54a2802"
|
||||
"01004e90ad1daaae1075d53b7d571c30e063b5a62a4a017bb394833aa0983e6e";
|
||||
const auto xorLength = std::min(obfuscated.size(), size_t{100});
|
||||
for (size_t i = 0; i < xorLength; ++i) {
|
||||
obfuscated[i] ^= static_cast<uint8_t>(obfuscationKey[i]);
|
||||
}
|
||||
|
||||
std::string encoded(obfuscated.begin(), obfuscated.end());
|
||||
if (const auto nul = encoded.find('\0'); nul != std::string::npos) {
|
||||
encoded.resize(nul);
|
||||
}
|
||||
const auto der = Util::base64Decode(encoded);
|
||||
auto parsed = parsePrivateKeyDer(der);
|
||||
EVP_PKEY* key = makePrivateKey(parsed);
|
||||
EVP_PKEY_CTX* checkContext = EVP_PKEY_CTX_new(key, nullptr);
|
||||
const bool valid = checkContext &&
|
||||
EVP_PKEY_private_check(checkContext) == 1 &&
|
||||
EVP_PKEY_pairwise_check(checkContext) == 1;
|
||||
EVP_PKEY_CTX_free(checkContext);
|
||||
if (!valid) {
|
||||
EVP_PKEY_free(key);
|
||||
fail("TeamSpeak identity key pair is inconsistent");
|
||||
}
|
||||
|
||||
Identity identity;
|
||||
identity.pkey_ = std::shared_ptr<void>(key, PKeyDeleter{});
|
||||
const auto point = pubPoint(key);
|
||||
if (point.size() != 65 || point.front() != 0x04) {
|
||||
fail("unexpected imported public key format");
|
||||
}
|
||||
BIGNUM* x = BN_bin2bn(point.data() + 1, 32, nullptr);
|
||||
BIGNUM* y = BN_bin2bn(point.data() + 33, 32, nullptr);
|
||||
identity.publicKeyString_ = buildPublicKeyString(x, y);
|
||||
BN_free(x);
|
||||
BN_free(y);
|
||||
identity.keyOffset_ = offset;
|
||||
const auto uidHash = Crypto::sha1(Crypto::ByteView(
|
||||
reinterpret_cast<const uint8_t*>(identity.publicKeyString_.data()),
|
||||
identity.publicKeyString_.size()));
|
||||
identity.uid_ = Util::base64Encode(uidHash);
|
||||
return identity;
|
||||
}
|
||||
|
||||
Identity Identity::generate(int securityLevel)
|
||||
{
|
||||
EVP_PKEY* pkey = nullptr;
|
||||
@@ -375,7 +606,9 @@ namespace Echoion::Protocol
|
||||
std::string pem(data, static_cast<size_t>(n));
|
||||
BIO_free(mem);
|
||||
|
||||
std::ofstream out(path, std::ios::trunc);
|
||||
const std::filesystem::path destination{path};
|
||||
const auto temporary = destination.string() + ".tmp";
|
||||
std::ofstream out(temporary, std::ios::binary | std::ios::trunc);
|
||||
if (!out) {
|
||||
fail("cannot open identity file for writing");
|
||||
}
|
||||
@@ -384,6 +617,22 @@ namespace Echoion::Protocol
|
||||
out << publicKeyString_ << "\n";
|
||||
out << "-----PRIVATE-----\n";
|
||||
out << pem;
|
||||
out.close();
|
||||
if (!out) {
|
||||
std::filesystem::remove(temporary);
|
||||
fail("cannot finish writing identity file");
|
||||
}
|
||||
std::filesystem::permissions(
|
||||
temporary,
|
||||
std::filesystem::perms::owner_read |
|
||||
std::filesystem::perms::owner_write,
|
||||
std::filesystem::perm_options::replace);
|
||||
std::error_code renameError;
|
||||
std::filesystem::rename(temporary, destination, renameError);
|
||||
if (renameError) {
|
||||
std::filesystem::remove(temporary);
|
||||
fail("cannot install identity file");
|
||||
}
|
||||
}
|
||||
|
||||
Identity Identity::load(const std::string& path)
|
||||
|
||||
Reference in new issue
Block a user