Fixed serialization of vectors, written bind_temp_auth_key function

This commit is contained in:
Daniil Gentili 2016-10-18 16:46:31 +00:00
parent d1e7cce35b
commit 7b468c9d0d
6 changed files with 48 additions and 12 deletions

View File

@ -34,9 +34,9 @@ src/danog/MadelineProto/
DataCenter - Handles mtproto datacenters
DebugTools - Various debugging tools
Exception - Handles exceptions in the main namespace
MTProto - Extends MTProtoTools, handles initial connection, generation of authorization keys, istantiation of classes
MTProto - Extends MTProtoTools, handles initial connection, generation of authorization keys, istantiation of classes, writing of client info
MTProtoTools - Extends all of the classes in MTProtoTools/
prime.py - prime module (python) for p and q generation
prime.py and getpq.py - prime module (python) for p and q generation
PrimeModule.php - prime module (php) for p and q generation by wrapping the python module, using wolfram alpha or a built in PHP engine
RSA - Handles RSA public keys and signatures
Tools - Various tools (positive modulus, string2bin, python-like range)

View File

@ -115,9 +115,13 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
$this->connection->dc_connect(2);
// Load rsa key
$this->log->log('Loading RSA key...');
$this->key = new RSA($settings['authorization']['rsa_key']);
// Istantiate struct class
$this->log->log('Initializing StructTools...');
$this->struct = new \danog\PHP\StructTools();
// Istantiate TL class
$this->log->log('Translating tl schemas...');
$this->tl = new TL\TL($this->settings['tl_schema']['src']);
@ -130,11 +134,14 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
if ($this->settings['authorization']['temp_auth_key'] == null || $this->settings['authorization']['auth_key'] == null) {
if ($this->settings['authorization']['auth_key'] == null) {
$this->log->log('Generating permanent authorization key...');
$this->settings['authorization']['auth_key'] = $this->create_auth_key(-1);
}
$this->log->log('Generating temporary authorization key...');
$this->settings['authorization']['temp_auth_key'] = $this->create_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in']);
}
$this->write_client_info();
$this->bind_temp_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in']);
$nearestDc = $this->method_call('auth.sendCode', [
'phone_number' => '393373737',
'sms_type' => 5,
@ -146,6 +153,7 @@ var_dump($nearestDc);
}
public function write_client_info() {
$this->log->log('Writing client info...');
$nearestDc = $this->method_call('invokeWithLayer', [
'layer' => $this->settings['tl_schema']['layer'],
'query' => $this->tl->serialize_method('initConnection',

View File

@ -458,9 +458,35 @@ class AuthKeyHandler extends AckHandler
throw new Exception('Auth Failed');
}
public function bind_temp_auth_key($expires_in = 86400) {
$nonce = $this->struct->unpack('<q', \phpseclib\Crypt\Random::string(8));
public function bind_temp_auth_key($expires_in) {
$nonce = $this->struct->unpack('<q', \phpseclib\Crypt\Random::string(8))[0];
$expires_at = time() + $expires_in;
$temp_auth_key_id = $this->struct->unpack('<q', $this->settings['authorization']['temp_auth_key']['id'])[0];
$perm_auth_key_id= $this->struct->unpack('<q', $this->settings['authorization']['auth_key']['id'])[0];
$temp_session_id = $this->struct->unpack('<q', $this->settings['authorization']['session_id'])[0];
$message_data = $this->tl->serialize_obj('bind_auth_key_inner',
[
'nonce' => $nonce,
'temp_auth_key_id' => $temp_auth_key_id,
'perm_auth_key_id' => $perm_auth_key_id,
'temp_session_id' => $temp_session_id,
'expires_at' => $expires_at,
]
);
$int_message_id = $this->generate_message_id();
$message_id = $this->struct->pack('<Q', $int_message_id);
$seq_no = 0;
$encrypted_data = \phpseclib\Crypt\Random::string(16).$message_id.$this->struct->pack('<II', $seq_no, strlen($message_data)).$message_data;
$message_key = substr(sha1($encrypted_data, true), -16);
$padding = \phpseclib\Crypt\Random::string($this->posmod(-strlen($encrypted_data), 16));
list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->settings['authorization']['auth_key']['auth_key']);
$encrypted_message = $this->settings['authorization']['auth_key']['id'].$message_key.$this->ige_encrypt($encrypted_data.$padding, $aes_key, $aes_iv);
if ($this->method_call('auth.bindTempAuthKey', ['perm_auth_key_id' => $perm_auth_key_id, 'nonce' => $nonce, 'expires_at' => $expires_at, 'encrypted_message' => $encrypted_message])) {
$this->log->log('Successfully binded temporary and permanent authorization keys.');
$this->write_client_info();
return true;
}
throw new Exception('An error occurred while binding temporary and permanent authorization keys.');
}
}

View File

@ -14,13 +14,13 @@ namespace danog\MadelineProto\MTProtoTools;
class Crypt extends CallHandler
{
public function aes_calculate($msg_key, $direction = 'to server')
public function aes_calculate($msg_key, $auth_key, $direction = 'to server')
{
$x = ($direction == 'to server') ? 0 : 8;
$sha1_a = sha1($msg_key.substr($this->settings['authorization']['temp_auth_key']['auth_key'], $x, ($x + 32) - $x), true);
$sha1_b = sha1(substr($this->settings['authorization']['temp_auth_key']['auth_key'], ($x + 32), ($x + 48) - ($x + 32)).$msg_key.substr($this->settings['authorization']['temp_auth_key']['auth_key'], (48 + $x), (64 + $x) - (48 + $x)), true);
$sha1_c = sha1(substr($this->settings['authorization']['temp_auth_key']['auth_key'], ($x + 64), ($x + 96) - ($x + 64)).$msg_key, true);
$sha1_d = sha1($msg_key.substr($this->settings['authorization']['temp_auth_key']['auth_key'], ($x + 96), ($x + 128) - ($x + 96)), true);
$sha1_a = sha1($msg_key.substr($auth_key, $x, ($x + 32) - $x), true);
$sha1_b = sha1(substr($auth_key, ($x + 32), ($x + 48) - ($x + 32)).$msg_key.substr($auth_key, (48 + $x), (64 + $x) - (48 + $x)), true);
$sha1_c = sha1(substr($auth_key, ($x + 64), ($x + 96) - ($x + 64)).$msg_key, true);
$sha1_d = sha1($msg_key.substr($auth_key, ($x + 96), ($x + 128) - ($x + 96)), true);
$aes_key = substr($sha1_a, 0, 8 - 0).substr($sha1_b, 8, 20 - 8).substr($sha1_c, 4, 16 - 4);
$aes_iv = substr($sha1_a, 8, 20 - 8).substr($sha1_b, 0, 8 - 0).substr($sha1_c, 16, 20 - 16).substr($sha1_d, 0, 8 - 0);

View File

@ -32,7 +32,7 @@ class MessageHandler extends Crypt
$encrypted_data = $this->struct->pack('<q', $this->settings['authorization']['temp_auth_key']['server_salt']).$this->settings['authorization']['session_id'].$message_id.$this->struct->pack('<II', $seq_no, strlen($message_data)).$message_data;
$message_key = substr(sha1($encrypted_data, true), -16);
$padding = \phpseclib\Crypt\Random::string($this->posmod(-strlen($encrypted_data), 16));
list($aes_key, $aes_iv) = $this->aes_calculate($message_key);
list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->settings['authorization']['temp_auth_key']['auth_key']);
$message = $this->settings['authorization']['temp_auth_key']['id'].$message_key.$this->ige_encrypt($encrypted_data.$padding, $aes_key, $aes_iv);
$this->outgoing_messages[$int_message_id]['seq_no'] = $seq_no;
}
@ -58,7 +58,7 @@ class MessageHandler extends Crypt
} elseif ($auth_key_id == $this->settings['authorization']['temp_auth_key']['id']) {
$message_key = fread($payload, 16);
$encrypted_data = stream_get_contents($payload);
list($aes_key, $aes_iv) = $this->aes_calculate($message_key, 'from server');
list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->settings['authorization']['temp_auth_key']['auth_key'], 'from server');
$decrypted_data = $this->ige_decrypt($encrypted_data, $aes_key, $aes_iv);
$server_salt = $this->struct->unpack('<q', substr($decrypted_data, 0, 8))[0];

View File

@ -168,6 +168,8 @@ class TL extends \danog\MadelineProto\Tools
return $value;
case 'Vector t':
$concat = $this->struct->pack('<i', $this->constructor_type['vector']->id);
$concat .= $this->struct->pack('<l', count($value));
foreach ($value as $curv) {
$concat .= $this->serialize_param($subtype, null, $curv);
}