Teaspeak-Server/client/src/protocol/HandshakeNew.cpp

154 lines
6.0 KiB
C++

#include <ed25519/ed25519.h>
#include <ed25519/sha512.h>
#include <misc/base64.h>
#include <misc/digest.h>
#include "Connection.h"
#include "License.h"
#include <log/LogUtils.h>
using namespace std;
using namespace std::chrono;
using namespace ts;
using namespace license::teamspeak;
using namespace ts::connection;
using namespace ts::protocol;
int __ed_sha512_init(sha512_context* ctx) {
//assert(!ctx->context);
ctx->context = new hash_state{};
return sha512_init((hash_state*) ctx->context) == CRYPT_OK;
}
int __ed_sha512_final(sha512_context* ctx, unsigned char *out) {
assert(ctx->context);
auto result = sha512_done((hash_state*) ctx->context, out) == CRYPT_OK;
delete (hash_state*) ctx->context;
return result;
}
int __ed_sha512_update(sha512_context* ctx, const unsigned char *msg, size_t len) {
assert(ctx->context);
return sha512_process((hash_state*) ctx->context, msg, len) == CRYPT_OK;
}
static sha512_functions __ed_sha512_functions {
__ed_sha512_init,
__ed_sha512_final,
__ed_sha512_update
};
bool ServerConnection::handshakeNew(Command &initivexpand2, const std::string& alpha, std::string &errorMessage) {
if(&__ed_sha512_functions != &_ed_sha512_functions)
_ed_sha512_functions = __ed_sha512_functions;
cout << initivexpand2.build() << endl;
u_char seed[32 * 2];
u_char clientPrivateKey[32];
u_char clientPublicKey[32];
ed25519_create_keypair(clientPublicKey, clientPrivateKey, seed);
cout << "Client key: " << base64::encode((char*) clientPrivateKey, 32) << endl;
cout << "Privet key:" << endl;
hexDump(clientPrivateKey, 32);
cout << "Public key:" << endl;
hexDump(clientPublicKey, 32);
auto license = base64::decode(initivexpand2["l"]);
auto licensestream = stringstream(license);
auto chain = LicenseChain::parse(licensestream, errorMessage);
if(!chain) return false;
chain->print();
unique_ptr<ecc_key> serverPublic(new ecc_key{});
auto omega = base64::decode(initivexpand2["omega"]);
ecc_import((u_char*) omega.data(), omega.length(), serverPublic.get());
//7B 1E AC 02 CE 77 35 0E EF C4 5C 1C F7 54 04 87 A9 A7 64 A7 8F 04 F7 53 58 64 84 D7 0A 97 F2 63
//[0x7b, 0x1e, 0xac, 0x2, 0xce, 0x77, 0x35, 0xe, 0xef, 0xc4, 0x5c, 0x1c, 0xf7, 0x54, 0x4, 0x87, 0xa9, 0xa7, 0x64, 0xa7, 0x8f, 0x4, 0xf7, 0x53, 0x58, 0x64, 0x84, 0xd7, 0xa, 0x97, 0xf2, 0xe3]
//License signed from server :)
auto licenseHash = digest::sha256(license);
auto licenseSign = base64::decode(initivexpand2["proof"]);
int state;
assert(ecc_verify_hash((u_char*) licenseSign.c_str(), licenseSign.length(), (u_char*) licenseHash.c_str(), licenseHash.length(), &state, serverPublic.get()) == CRYPT_OK);
cout << "State: " << state << endl;
assert(state == 1);
//EK!
this->idManager.nextPacketId(PacketTypeInfo::Command);
cout << this->idManager.currentPacketId(PacketTypeInfo::Command) << endl;
Command clientek("clientek");
clientek["ek"] = base64::encode((char*) clientPublicKey, 32);
auto rawProof = string((char*) clientPublicKey, 32) + base64::decode(initivexpand2["beta"]);
cout << " -> " << rawProof.length() << endl;
size_t signBufferLength = 120;
char signBuffer[signBufferLength];
prng_state prngState{};
memset(&prngState, 0, sizeof(prngState));
cout << "Data: " << base64::encode(rawProof) << endl;
cout << "KEY: " << this->clientIdentity->privateKey() << endl;
rawProof = digest::sha256(rawProof);
assert(ecc_sign_hash((u_char*) rawProof.data(), rawProof.length(), (u_char*) signBuffer, &signBufferLength, &prngState, find_prng("sprng"), this->clientIdentity->getKeyPair()) == CRYPT_OK);
cout << "ecc_sign_hash() -> " << base64::encode(signBuffer, signBufferLength) << endl;
clientek["proof"] = base64::encode(signBuffer, signBufferLength);
this->sendCommand(clientek, false);
//TODO magic stuff
shared_ptr<ServerPacket> response;
response = readNextPacket();
if (!response) {
errorMessage = "could not get a valid response!";
return false;
}
cout << "Type: " << response->type().name() << endl;
cout << "ID: " << (int) response->data()[0] << " " << (int) response->data()[1] << endl;
LicensePublicKey serverroot;
memcpy(serverroot, public_root, 32);
if(initivexpand2[0].has("root")) {
cout << "Cot costume server root!" << endl;
auto root = base64::decode(initivexpand2["root"]);
memcpy(serverroot, root.data(), 32);
}
cout << "Public root key: " << endl;
for(const auto& e : serverroot)
cout << hex << "0x" << (int) (uint8_t) e << " " << endl;
string sharedData;
this->cryptionHandler->setupSharedSecretNew(alpha, base64::decode(initivexpand2["beta"]), (char*) clientPrivateKey, (char*) chain->generatePublicKey(serverroot).data());
//this->cryptionHandler->setupSharedSecretNew(alpha, base64::decode(initivexpand2["beta"]), (char*) clientPrivateKey, (char*) public_tea_root);
threads::self::sleep_for(milliseconds(250));
Command clientinit("clientinit");
//94ec66de-5940-4e38-b002-970df0cf6c94,62444179-0d99-42ba-a45c-c6b1557d079a,d95f9901-c42d-4bac-8849-7164fd9e2310
//clientinit["client_badges"] = "badges=450f81c1-ab41-4211-a338-222fa94ed157,c9e97536-5a2d-4c8e-a135-af404587a472,94ec66de-5940-4e38-b002-970df0cf6c94"; //,62444179-0d99-42ba-a45c-c6b1557d079a
clientinit["client_nickname"] = "Wolf C++ XX";
clientinit["client_version"] = "3.1.8 [Build: 1516614607]";
clientinit["client_platform"] = "Windows";
clientinit["client_version_sign"] = "gDEgQf/BiOQZdAheKccM1XWcMUj2OUQqt75oFuvF2c0MQMXyv88cZQdUuckKbcBRp7RpmLInto4PIgd7mPO7BQ==";
clientinit["client_input_hardware"] = true;
clientinit["client_output_hardware"] = true;
clientinit["client_default_channel"] = "";
clientinit["client_default_channel_password"] = "";
clientinit["client_server_password"] = "";
clientinit["client_meta_data"] = "";
clientinit["client_key_offset"] = this->clientIdentity->lastValidKeyOffset();
clientinit["client_nickname_phonetic"] = "";
clientinit["client_default_token"] = "";
clientinit["hwid"] = "123,456123123123";
sendCommand(clientinit);
this->autoHandle = true;
/*
response = readNextPacket();
if (!response) {
errorMessage = "could not get a valid response!";
return false;
}
*/
return true;
}