TeaSpeakLibrary/src/protocol/CryptionHandler.h

74 lines
3.3 KiB
C++

#pragma once
#include <array>
#include <string>
#include <tomcrypt.h>
#include "Packet.h"
namespace ts {
namespace connection {
class CryptionHandler {
enum Methode {
TEAMSPEAK_3_1,
TEAMSPEAK_3
};
struct KeyCache {
uint16_t generation = 0xFFEF;
uint8_t key[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t nonce[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
};
public:
CryptionHandler();
~CryptionHandler();
void reset();
//TeamSpeak old
bool setupSharedSecret(const std::string& alpha, const std::string& beta, ecc_key* publicKey, ecc_key* ownKey, std::string &error);
bool setupSharedSecret(const std::string& alpha, const std::string& beta, std::string sharedKey, std::string &error);
//TeamSpeak new
bool setupSharedSecretNew(const std::string& alpha, const std::string& beta, const char privateKey[32], const char publicKey[32]);
bool progressPacketOut(protocol::BasicPacket*, std::string&, bool use_default);
bool progressPacketIn(protocol::BasicPacket*, std::string&, bool use_default);
bool verify_encryption(const pipes::buffer_view& data, uint16_t packet_id, uint16_t generation);
bool block(){ blocked = true; return true; }
bool unblock(){ blocked = false; return true; }
bool isBlocked(){ return blocked; }
bool use_default() { return this->useDefaultChipherKeyNonce; }
private:
static constexpr char default_key[16] = {'c', ':', '\\', 'w', 'i', 'n', 'd', 'o', 'w', 's', '\\', 's', 'y', 's', 't', 'e'}; //c:\windows\syste
static constexpr char default_nonce[16] = {'m', '\\', 'f', 'i', 'r', 'e', 'w', 'a', 'l', 'l', '3', '2', '.', 'c', 'p', 'l'}; //m\firewall32.cpl
static constexpr char default_mac[8] = {'T', 'S', '3', 'I', 'N', 'I', 'T', '1'}; //TS3INIT1
bool decryptPacket(protocol::BasicPacket *, std::string &, bool use_default);
bool encryptPacket(protocol::BasicPacket *, std::string &, bool use_default);
bool generate_key_nonce(bool /* to server */, protocol::PacketType /* type */, uint16_t /* packet id */, uint16_t /* generation */, bool /* use default */, uint8_t(&)[16] /* key */, uint8_t(&)[16] /* nonce */);
bool generate_key_nonce(protocol::BasicPacket* packet, bool use_default, uint8_t(&)[16] /* key */, uint8_t(&)[16] /* nonce */);
//The default key and nonce
bool useDefaultChipherKeyNonce = true;
bool blocked = false;
/* for the old protocol SHA1 length for the new 64 bytes */
uint8_t iv_struct[64];
uint8_t iv_struct_length = 0;
uint8_t current_mac[8];
std::mutex cache_key_lock;
std::array<KeyCache, protocol::PACKET_MAX> cache_key_client;
std::array<KeyCache, protocol::PACKET_MAX> cache_key_server;
static_assert(sizeof(current_mac) == sizeof(default_mac), "invalid mac");
static_assert(sizeof(iv_struct) == 64, "invalid iv struct");
};
}
}