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
2020-07-29 19:05:35 +02:00
namespace ts : : connection {
class CryptHandler {
public :
typedef std : : array < uint8_t , 16 > key_t ;
typedef std : : array < uint8_t , 16 > nonce_t ;
CryptHandler ( ) ;
~ CryptHandler ( ) ;
2019-06-26 22:11:22 +02:00
2020-07-29 19:05:35 +02:00
void reset ( ) ;
2019-06-26 22:11:22 +02:00
2020-07-29 19:05:35 +02:00
//TeamSpeak old
2021-02-05 17:20:38 +01:00
bool setupSharedSecret ( const std : : string & /* alpha */ , const std : : string & /* beta */ , ecc_key * /* remote_public_key */ , ecc_key * /* own_private_key */ , std : : string & /* error */ ) ;
bool setupSharedSecret ( const std : : string & /* alpha */ , const std : : string & /* beta */ , const std : : string & /* shared_key */ , std : : string & /* error */ ) ;
2019-06-26 22:11:22 +02:00
2020-07-29 19:05:35 +02:00
//TeamSpeak new
bool setupSharedSecretNew ( const std : : string & alpha , const std : : string & beta , const char privateKey [ 32 ] , const char publicKey [ 32 ] ) ;
2019-06-26 22:11:22 +02:00
2020-07-29 19:05:35 +02:00
bool encrypt (
const void * /* header */ , size_t /* header length */ ,
void * /* payload */ , size_t /* payload length */ ,
2021-02-05 17:20:38 +01:00
void * /* mac */ , /* mac must be 8 bytes long! */
2020-07-29 19:05:35 +02:00
const key_t & /* key */ , const nonce_t & /* nonce */ ,
std : : string & /* error */ ) ;
2020-01-27 02:21:39 +01:00
2020-07-29 19:05:35 +02:00
bool decrypt (
const void * /* header */ , size_t /* header length */ ,
void * /* payload */ , size_t /* payload length */ ,
2021-02-05 17:20:38 +01:00
const void * /* mac */ , /* mac must be 8 bytes long! */
2020-07-29 19:05:35 +02:00
const key_t & /* key */ , const nonce_t & /* nonce */ ,
2021-02-05 17:20:38 +01:00
std : : string & /* error */ ) const ;
2020-01-27 02:21:39 +01:00
2020-07-29 19:05:35 +02:00
bool generate_key_nonce ( bool /* to server */ , uint8_t /* packet type */ , uint16_t /* packet id */ , uint16_t /* generation */ , key_t & /* key */ , nonce_t & /* nonce */ ) ;
2021-02-05 17:20:38 +01:00
bool verify_encryption ( const pipes : : buffer_view & /* data */ , uint16_t /* packet id */ , uint16_t /* generation */ ) ;
2019-06-26 22:11:22 +02:00
2020-07-29 19:05:35 +02:00
inline void write_default_mac ( void * buffer ) {
memcpy ( buffer , this - > current_mac , 8 ) ;
}
2019-06-26 22:11:22 +02:00
2021-02-05 17:20:38 +01:00
[ [ nodiscard ] ] inline bool encryption_initialized ( ) const { return ! this - > encryption_initialized_ ; }
2019-06-26 22:11:22 +02:00
2020-07-29 19:05:35 +02:00
static constexpr key_t kDefaultKey { ' c ' , ' : ' , ' \\ ' , ' w ' , ' i ' , ' n ' , ' d ' , ' o ' , ' w ' , ' s ' , ' \\ ' , ' s ' , ' y ' , ' s ' , ' t ' , ' e ' } ; //c:\windows\syste
static constexpr nonce_t kDefaultNonce { ' m ' , ' \\ ' , ' f ' , ' i ' , ' r ' , ' e ' , ' w ' , ' a ' , ' l ' , ' l ' , ' 3 ' , ' 2 ' , ' . ' , ' c ' , ' p ' , ' l ' } ; //m\firewall32.cpl
private :
static constexpr char default_mac [ 8 ] = { ' T ' , ' S ' , ' 3 ' , ' I ' , ' N ' , ' I ' , ' T ' , ' 1 ' } ; //TS3INIT1
2019-06-26 22:11:22 +02:00
2021-02-05 17:20:38 +01:00
struct KeyCache {
uint16_t generation = 0xFFEF ;
union {
struct {
uint8_t key [ 16 ] ;
uint8_t nonce [ 16 ] ;
} ;
uint8_t key_nonce [ 32 ] ;
} ;
} ;
2019-06-26 22:11:22 +02:00
2021-02-05 17:20:38 +01:00
bool encryption_initialized_ { false } ;
int cipher_code { - 1 } ;
2019-06-26 22:11:22 +02:00
2020-07-29 19:05:35 +02:00
/* for the old protocol SHA1 length for the new 64 bytes */
uint8_t iv_struct [ 64 ] ;
2021-02-05 17:20:38 +01:00
uint8_t iv_struct_length { 0 } ;
2019-06-26 22:11:22 +02:00
2021-02-05 17:20:38 +01:00
uint8_t current_mac [ 8 ] { } ;
2019-06-26 22:11:22 +02:00
2021-02-05 17:20:38 +01:00
std : : mutex cache_key_lock { } ;
std : : array < KeyCache , protocol : : PACKET_MAX > cache_key_client { } ;
std : : array < KeyCache , protocol : : PACKET_MAX > cache_key_server { } ;
2020-07-29 19:05:35 +02:00
static_assert ( sizeof ( current_mac ) = = sizeof ( default_mac ) , " invalid mac " ) ;
static_assert ( sizeof ( iv_struct ) = = 64 , " invalid iv struct " ) ;
} ;
2019-06-26 22:11:22 +02:00
}