diff --git a/README.md b/README.md index 75a13c77e..6ea2517d4 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 6de09a6a5..006a006f2 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -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', diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index 79764b00e..ceed6b36c 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -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('struct->unpack('struct->unpack('settings['authorization']['temp_auth_key']['id'])[0]; + $perm_auth_key_id= $this->struct->unpack('settings['authorization']['auth_key']['id'])[0]; + $temp_session_id = $this->struct->unpack('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('struct->pack('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.'); } } diff --git a/src/danog/MadelineProto/MTProtoTools/Crypt.php b/src/danog/MadelineProto/MTProtoTools/Crypt.php index 4556563d5..3003d90ae 100644 --- a/src/danog/MadelineProto/MTProtoTools/Crypt.php +++ b/src/danog/MadelineProto/MTProtoTools/Crypt.php @@ -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); diff --git a/src/danog/MadelineProto/MTProtoTools/MessageHandler.php b/src/danog/MadelineProto/MTProtoTools/MessageHandler.php index 0ed99c89b..d8829f4ef 100644 --- a/src/danog/MadelineProto/MTProtoTools/MessageHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/MessageHandler.php @@ -32,7 +32,7 @@ class MessageHandler extends Crypt $encrypted_data = $this->struct->pack('settings['authorization']['temp_auth_key']['server_salt']).$this->settings['authorization']['session_id'].$message_id.$this->struct->pack('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('struct->pack('constructor_type['vector']->id); + + $concat .= $this->struct->pack('serialize_param($subtype, null, $curv); }