2019-06-26 22:11:22 +02:00
# pragma once
# include <array>
# include <string>
# include "Packet.h"
2019-07-02 02:13:42 +02:00
# include <tomcrypt.h>
# undef byte /* the macro byte gets defined by tomcrypt_macros. We have to undefine it */
2019-06-26 22:11:22 +02:00
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 ) ;
2019-07-05 20:02:40 +02:00
bool setupSharedSecret ( const std : : string & alpha , const std : : string & beta , const std : : string & sharedKey , std : : string & error ) ;
2019-06-26 22:11:22 +02:00
//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 " ) ;
} ;
}
}