From 0cb8d7f27e7864788bfd0ebb0b8eaabbb33cc178 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Wed, 11 Dec 2019 16:18:31 +0100 Subject: [PATCH] Misc bugfixes --- composer.json | 2 ++ .../Loop/Connection/ReadLoop.php | 3 +++ .../Loop/Connection/WriteLoop.php | 25 +++++++++++-------- src/danog/MadelineProto/MTProto.php | 2 +- .../MTProtoTools/AuthKeyHandler.php | 8 ++++-- src/danog/MadelineProto/ProxySocketPool.php | 2 +- .../Stream/Common/BufferedRawStream.php | 1 - .../Stream/Transport/WsStream.php | 1 - 8 files changed, 28 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 4d105a3e..b210647e 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,8 @@ "ext-xml": "*", "ext-fileinfo": "*", "amphp/amp": "^2.0", + + "amphp/websocket": "dev-master#db2da8c5b3ed22eae37da5ffa10ab3ea8de19342 as 1", "amphp/websocket-client": "dev-master#aff808025637bd705672338b4904ad03a4dbdc04 as 1", "amphp/socket": "0.10.12 as 1", diff --git a/src/danog/MadelineProto/Loop/Connection/ReadLoop.php b/src/danog/MadelineProto/Loop/Connection/ReadLoop.php index a2b35c41..7b9042f3 100644 --- a/src/danog/MadelineProto/Loop/Connection/ReadLoop.php +++ b/src/danog/MadelineProto/Loop/Connection/ReadLoop.php @@ -174,11 +174,14 @@ class ReadLoop extends SignalLoop $auth_key_id = yield $buffer->bufferRead(8); if ($auth_key_id === "\0\0\0\0\0\0\0\0") { + $message_id = yield $buffer->bufferRead(8); if (!\in_array($message_id, [1, 0])) { $connection->checkMessageId($message_id, ['outgoing' => false, 'container' => false]); } + $message_length = \unpack('V', yield $buffer->bufferRead(4))[1]; + $message_data = yield $buffer->bufferRead($message_length); $left = $payload_length - $message_length - 4 - 8 - 8; if ($left) { diff --git a/src/danog/MadelineProto/Loop/Connection/WriteLoop.php b/src/danog/MadelineProto/Loop/Connection/WriteLoop.php index 32e5b68c..3c506855 100644 --- a/src/danog/MadelineProto/Loop/Connection/WriteLoop.php +++ b/src/danog/MadelineProto/Loop/Connection/WriteLoop.php @@ -192,17 +192,19 @@ class WriteLoop extends ResumableSignalLoop $has_http_wait = false; $messages = []; $keys = []; - foreach ($connection->pending_outgoing as $message) { - if ($message['_'] === 'http_wait') { - $has_http_wait = true; - break; + if ($shared->isHttp()) { + foreach ($connection->pending_outgoing as $message) { + if ($message['_'] === 'http_wait') { + $has_http_wait = true; + break; + } + } + if (!$has_http_wait) { + $API->logger->logger("Adding http_wait {$connection->pending_outgoing_key}", Logger::ULTRA_VERBOSE); + $connection->pending_outgoing[$connection->pending_outgoing_key] = ['_' => 'http_wait', 'serialized_body' => yield $this->API->getTL()->serializeObject(['type' => ''], ['_' => 'http_wait', 'max_wait' => 30000, 'wait_after' => 0, 'max_delay' => 0], 'http_wait'), 'contentRelated' => true, 'unencrypted' => false, 'method' => true]; + $temporary_keys[$connection->pending_outgoing_key] = true; + $connection->pending_outgoing_key++; } - } - if ($shared->isHttp() && !$has_http_wait) { - $API->logger->logger("Adding http_wait {$connection->pending_outgoing_key}", Logger::ULTRA_VERBOSE); - $connection->pending_outgoing[$connection->pending_outgoing_key] = ['_' => 'http_wait', 'serialized_body' => yield $this->API->getTL()->serializeObject(['type' => ''], ['_' => 'http_wait', 'max_wait' => 30000, 'wait_after' => 0, 'max_delay' => 0], 'http_wait'), 'contentRelated' => true, 'unencrypted' => false, 'method' => true]; - $temporary_keys[$connection->pending_outgoing_key] = true; - $connection->pending_outgoing_key++; } $total_length = 0; @@ -341,8 +343,11 @@ class WriteLoop extends ResumableSignalLoop $padding += 16; } $padding = \danog\MadelineProto\Tools::random($padding); + $message_key = \substr(\hash('sha256', \substr($shared->getTempAuthKey()->getAuthKey(), 88, 32).$plaintext.$padding, true), 8, 16); + list($aes_key, $aes_iv) = $this->aesCalculate($message_key, $shared->getTempAuthKey()->getAuthKey()); + $message = $shared->getTempAuthKey()->getID().$message_key.$this->igeEncrypt($plaintext.$padding, $aes_key, $aes_iv); $buffer = yield $connection->stream->getWriteBuffer($len = \strlen($message)); diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 04218232..4edb0dda 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -1203,7 +1203,7 @@ class MTProto extends AsyncConstruct implements TLCallback // Extra parameters to pass to the proxy class using setExtra 'obfuscated' => false, 'transport' => 'tcp', - 'pfs' => \extension_loaded('gmp'), + 'pfs' => false,//\extension_loaded('gmp'), ], 'media_socket_count' => [ 'min' => 5, diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index db5352bb..cfa33d5a 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -184,6 +184,7 @@ trait AuthKeyHandler */ $p_bytes = $p->toBytes(); $q_bytes = $q->toBytes(); + $new_nonce = \danog\MadelineProto\Tools::random(32); $data_unserialized = ['pq' => $pq_bytes, 'p' => $p_bytes, 'q' => $q_bytes, 'nonce' => $nonce, 'server_nonce' => $server_nonce, 'new_nonce' => $new_nonce, 'expires_in' => $expires_in, 'dc' => \preg_replace('|_.*|', '', $datacenter)]; $p_q_inner_data = yield $this->TL->serializeObject(['type' => 'p_q_inner_data'.($expires_in < 0 ? '' : '_temp')], $data_unserialized, 'p_q_inner_data'); @@ -194,6 +195,7 @@ trait AuthKeyHandler $sha_digest = \sha1($p_q_inner_data, true); $random_bytes = \danog\MadelineProto\Tools::random(255 - \strlen($p_q_inner_data) - \strlen($sha_digest)); $to_encrypt = $sha_digest.$p_q_inner_data.$random_bytes; + $encrypted_data = $key->encrypt($to_encrypt); $this->logger->logger('Starting Diffie Hellman key exchange', \danog\MadelineProto\Logger::VERBOSE); /* @@ -244,8 +246,10 @@ trait AuthKeyHandler * Get key, iv and decrypt answer */ $encrypted_answer = $server_dh_params['encrypted_answer']; - $tmp_aes_key = \sha1($new_nonce.$server_nonce, true).\substr(\sha1($server_nonce.$new_nonce, true), 0, 12); + + $tmp_aes_key = \sha1($new_nonce.$server_nonce, true).\substr(\sha1($server_nonce.$new_nonce, true), 0, 12); $tmp_aes_iv = \substr(\sha1($server_nonce.$new_nonce, true), 12, 8).\sha1($new_nonce.$new_nonce, true).\substr($new_nonce, 0, 4); + $answer_with_hash = $this->igeDecrypt($encrypted_answer, $tmp_aes_key, $tmp_aes_iv); /* * *********************************************************************** @@ -388,7 +392,7 @@ trait AuthKeyHandler if ($expires_in >= 0) { $key->expires(\time() + $expires_in); } - $key->setServerSalt(\substr($new_nonce, 0, 8 - 0) ^ \substr($server_nonce, 0, 8 - 0)); + $key->setServerSalt(\substr($new_nonce, 0, 8) ^ \substr($server_nonce, 0, 8)); $key->setAuthKey($auth_key_str); $this->logger->logger('Auth key generated', \danog\MadelineProto\Logger::NOTICE); diff --git a/src/danog/MadelineProto/ProxySocketPool.php b/src/danog/MadelineProto/ProxySocketPool.php index 952652e3..a189fb58 100644 --- a/src/danog/MadelineProto/ProxySocketPool.php +++ b/src/danog/MadelineProto/ProxySocketPool.php @@ -54,7 +54,7 @@ class ProxySocketPool implements SocketPool } try { - $parts = Uri\parse($uri); + $parts = \League\Uri\UriString::parse($uri); } catch (\Exception $exception) { throw new SocketException('Could not parse URI', 0, $exception); } diff --git a/src/danog/MadelineProto/Stream/Common/BufferedRawStream.php b/src/danog/MadelineProto/Stream/Common/BufferedRawStream.php index 20aba9d5..2ccc0b68 100644 --- a/src/danog/MadelineProto/Stream/Common/BufferedRawStream.php +++ b/src/danog/MadelineProto/Stream/Common/BufferedRawStream.php @@ -168,7 +168,6 @@ class BufferedRawStream implements BufferedStreamInterface, BufferInterface, Raw if ($buffer_length >= $length) { return new Success(\fread($this->memory_stream, $length)); } - return \danog\MadelineProto\Tools::call($this->bufferReadGenerator($length)); } diff --git a/src/danog/MadelineProto/Stream/Transport/WsStream.php b/src/danog/MadelineProto/Stream/Transport/WsStream.php index aa56af39..46c8aa79 100644 --- a/src/danog/MadelineProto/Stream/Transport/WsStream.php +++ b/src/danog/MadelineProto/Stream/Transport/WsStream.php @@ -130,7 +130,6 @@ class WsStream implements RawStreamInterface return null; } - return $data; }