From 58918b6fadd2899a34ee4b4d08cc93be252257d5 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 15 Dec 2019 13:21:57 +0100 Subject: [PATCH] ECDH improvements --- composer.json | 2 +- .../MadelineProto/MTProtoTools/Crypt.php | 10 +- src/danog/MadelineProto/Magic.php | 159 +++++++++++++++++- src/danog/MadelineProto/TL/TL.php | 14 +- src/danog/MadelineProto/TON/API.php | 4 +- src/danog/MadelineProto/TON/APIFactory.php | 4 +- src/danog/MadelineProto/TON/Lite.php | 21 ++- src/danog/MadelineProto/TON/Tools.php | 43 ----- src/danog/MadelineProto/Tools.php | 20 +++ 9 files changed, 209 insertions(+), 68 deletions(-) delete mode 100644 src/danog/MadelineProto/TON/Tools.php diff --git a/composer.json b/composer.json index f4cc2135..d73f05d5 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "krakjoe/pthreads-polyfill": "*" }, "require": { - "php": ">=7.1.0", + "php": ">=7.4.0", "danog/primemodule": "^1", "danog/magicalserializer": "^1.0", "phpseclib/phpseclib": "dev-master#f715b2928976aaef389839a056c947aa8023277b as 2.0.15", diff --git a/src/danog/MadelineProto/MTProtoTools/Crypt.php b/src/danog/MadelineProto/MTProtoTools/Crypt.php index a0d60d4b..6df52a72 100644 --- a/src/danog/MadelineProto/MTProtoTools/Crypt.php +++ b/src/danog/MadelineProto/MTProtoTools/Crypt.php @@ -21,7 +21,7 @@ namespace danog\MadelineProto\MTProtoTools; trait Crypt { - public function aesCalculate($msg_key, $auth_key, $to_server = true) + public static function aesCalculate($msg_key, $auth_key, $to_server = true) { $x = $to_server ? 0 : 8; $sha256_a = \hash('sha256', $msg_key.\substr($auth_key, $x, 36), true); @@ -32,7 +32,7 @@ trait Crypt return [$aes_key, $aes_iv]; } - public function oldAesCalculate($msg_key, $auth_key, $to_server = true) + public static function oldAesCalculate($msg_key, $auth_key, $to_server = true) { $x = $to_server ? 0 : 8; $sha1_a = \sha1($msg_key.\substr($auth_key, $x, 32), true); @@ -45,7 +45,7 @@ trait Crypt return [$aes_key, $aes_iv]; } - public function ctrEncrypt($message, $key, $iv) + public static function ctrEncrypt($message, $key, $iv) { $cipher = new \phpseclib3\Crypt\AES('ctr'); $cipher->setKey($key); @@ -54,7 +54,7 @@ trait Crypt return @$cipher->encrypt($message); } - public function igeEncrypt($message, $key, $iv) + public static function igeEncrypt($message, $key, $iv) { $cipher = new \phpseclib3\Crypt\AES('ige'); $cipher->setKey($key); @@ -62,7 +62,7 @@ trait Crypt return @$cipher->encrypt($message); } - public function igeDecrypt($message, $key, $iv) + public static function igeDecrypt($message, $key, $iv) { $cipher = new \phpseclib3\Crypt\AES('ige'); $cipher->setKey($key); diff --git a/src/danog/MadelineProto/Magic.php b/src/danog/MadelineProto/Magic.php index cc126555..e18a6e38 100644 --- a/src/danog/MadelineProto/Magic.php +++ b/src/danog/MadelineProto/Magic.php @@ -33,35 +33,186 @@ use function Amp\Promise\wait; class Magic { + /** + * Static storage. + * + * @var array + */ public static $storage = []; + /** + * Whether has threads. + * + * @var boolean + */ public static $has_thread = false; + /** + * Whether this system is bigendian. + * + * @var boolean + */ public static $BIG_ENDIAN = false; + /** + * Whether this system is 32-bit and requires bigint. + * + * @var boolean + */ public static $bigint = true; + /** + * Whether this is a TTY console. + * + * @var boolean + */ public static $isatty = false; + /** + * Whether we're in a fork. + * + * @var boolean + */ public static $isFork = false; + /** + * Whether we can get our PID. + * + * @var boolean + */ public static $can_getmypid = true; + /** + * Whether we can amphp/parallel. + * + * @var boolean + */ public static $can_parallel = false; + /** + * Whether we can get our CWD. + * + * @var boolean + */ public static $can_getcwd = false; + /** + * Whether we've processed forks. + * + * @var boolean + */ public static $processed_fork = false; - public static $ipv6; + /** + * Whether we can use ipv6. + * + * @var bool + */ + public static $ipv6 = false; + /** + * Our PID. + * + * @var int + */ public static $pid; + /** + * Whether we've inited all static constants. + * + * @var boolean + */ public static $inited = false; + /** + * Bigint zero. + * + * @var \phpseclib3\Math\BigInteger + */ public static $zero; + /** + * Bigint one. + * + * @var \phpseclib3\Math\BigInteger + */ public static $one; + /** + * Bigint two. + * + * @var \phpseclib3\Math\BigInteger + */ public static $two; + /** + * Bigint three. + * + * @var \phpseclib3\Math\BigInteger + */ public static $three; + /** + * Bigint four. + * + * @var \phpseclib3\Math\BigInteger + */ public static $four; + /** + * Bigint 2^1984. + * + * @var \phpseclib3\Math\BigInteger + */ public static $twoe1984; + /** + * Bigint 2^2047. + * + * @var \phpseclib3\Math\BigInteger + */ public static $twoe2047; + /** + * Bigint 2^2048. + * + * @var \phpseclib3\Math\BigInteger + */ public static $twoe2048; + /** + * Bigint 2^31. + * + * @var \phpseclib3\Math\BigInteger + */ public static $zeroeight; + /** + * Bigint 20261. + * + * @var \phpseclib3\Math\BigInteger + */ public static $twozerotwosixone; + /** + * Bigint ECDH num. + * + * @var \phpseclib3\Math\BigInteger + */ + public static $ton; + /** + * Decoded UTF8 emojis for call fingerprint. + * + * @var array + */ public static $emojis; + /** + * MadelineProto revision. + * + * @var string + */ public static $revision; + /** + * Our CWD. + * + * @var string + */ public static $cwd; + /** + * Caller script CWD. + * + * @var string + */ public static $script_cwd; + /** + * Whether we're running on altervista. + * + * @var boolean + */ public static $altervista = false; + /** + * Wether we're running on 000webhost (yuck). + * + * @var boolean + */ public static $zerowebhost = false; /** @@ -71,6 +222,11 @@ class Magic */ public static $signaled = false; + /** + * Encoded emojis. + * + * @var string + */ const JSON_EMOJIS = '["\\ud83d\\ude09","\\ud83d\\ude0d","\\ud83d\\ude1b","\\ud83d\\ude2d","\\ud83d\\ude31","\\ud83d\\ude21","\\ud83d\\ude0e","\\ud83d\\ude34","\\ud83d\\ude35","\\ud83d\\ude08","\\ud83d\\ude2c","\\ud83d\\ude07","\\ud83d\\ude0f","\\ud83d\\udc6e","\\ud83d\\udc77","\\ud83d\\udc82","\\ud83d\\udc76","\\ud83d\\udc68","\\ud83d\\udc69","\\ud83d\\udc74","\\ud83d\\udc75","\\ud83d\\ude3b","\\ud83d\\ude3d","\\ud83d\\ude40","\\ud83d\\udc7a","\\ud83d\\ude48","\\ud83d\\ude49","\\ud83d\\ude4a","\\ud83d\\udc80","\\ud83d\\udc7d","\\ud83d\\udca9","\\ud83d\\udd25","\\ud83d\\udca5","\\ud83d\\udca4","\\ud83d\\udc42","\\ud83d\\udc40","\\ud83d\\udc43","\\ud83d\\udc45","\\ud83d\\udc44","\\ud83d\\udc4d","\\ud83d\\udc4e","\\ud83d\\udc4c","\\ud83d\\udc4a","\\u270c","\\u270b","\\ud83d\\udc50","\\ud83d\\udc46","\\ud83d\\udc47","\\ud83d\\udc49","\\ud83d\\udc48","\\ud83d\\ude4f","\\ud83d\\udc4f","\\ud83d\\udcaa","\\ud83d\\udeb6","\\ud83c\\udfc3","\\ud83d\\udc83","\\ud83d\\udc6b","\\ud83d\\udc6a","\\ud83d\\udc6c","\\ud83d\\udc6d","\\ud83d\\udc85","\\ud83c\\udfa9","\\ud83d\\udc51","\\ud83d\\udc52","\\ud83d\\udc5f","\\ud83d\\udc5e","\\ud83d\\udc60","\\ud83d\\udc55","\\ud83d\\udc57","\\ud83d\\udc56","\\ud83d\\udc59","\\ud83d\\udc5c","\\ud83d\\udc53","\\ud83c\\udf80","\\ud83d\\udc84","\\ud83d\\udc9b","\\ud83d\\udc99","\\ud83d\\udc9c","\\ud83d\\udc9a","\\ud83d\\udc8d","\\ud83d\\udc8e","\\ud83d\\udc36","\\ud83d\\udc3a","\\ud83d\\udc31","\\ud83d\\udc2d","\\ud83d\\udc39","\\ud83d\\udc30","\\ud83d\\udc38","\\ud83d\\udc2f","\\ud83d\\udc28","\\ud83d\\udc3b","\\ud83d\\udc37","\\ud83d\\udc2e","\\ud83d\\udc17","\\ud83d\\udc34","\\ud83d\\udc11","\\ud83d\\udc18","\\ud83d\\udc3c","\\ud83d\\udc27","\\ud83d\\udc25","\\ud83d\\udc14","\\ud83d\\udc0d","\\ud83d\\udc22","\\ud83d\\udc1b","\\ud83d\\udc1d","\\ud83d\\udc1c","\\ud83d\\udc1e","\\ud83d\\udc0c","\\ud83d\\udc19","\\ud83d\\udc1a","\\ud83d\\udc1f","\\ud83d\\udc2c","\\ud83d\\udc0b","\\ud83d\\udc10","\\ud83d\\udc0a","\\ud83d\\udc2b","\\ud83c\\udf40","\\ud83c\\udf39","\\ud83c\\udf3b","\\ud83c\\udf41","\\ud83c\\udf3e","\\ud83c\\udf44","\\ud83c\\udf35","\\ud83c\\udf34","\\ud83c\\udf33","\\ud83c\\udf1e","\\ud83c\\udf1a","\\ud83c\\udf19","\\ud83c\\udf0e","\\ud83c\\udf0b","\\u26a1","\\u2614","\\u2744","\\u26c4","\\ud83c\\udf00","\\ud83c\\udf08","\\ud83c\\udf0a","\\ud83c\\udf93","\\ud83c\\udf86","\\ud83c\\udf83","\\ud83d\\udc7b","\\ud83c\\udf85","\\ud83c\\udf84","\\ud83c\\udf81","\\ud83c\\udf88","\\ud83d\\udd2e","\\ud83c\\udfa5","\\ud83d\\udcf7","\\ud83d\\udcbf","\\ud83d\\udcbb","\\u260e","\\ud83d\\udce1","\\ud83d\\udcfa","\\ud83d\\udcfb","\\ud83d\\udd09","\\ud83d\\udd14","\\u23f3","\\u23f0","\\u231a","\\ud83d\\udd12","\\ud83d\\udd11","\\ud83d\\udd0e","\\ud83d\\udca1","\\ud83d\\udd26","\\ud83d\\udd0c","\\ud83d\\udd0b","\\ud83d\\udebf","\\ud83d\\udebd","\\ud83d\\udd27","\\ud83d\\udd28","\\ud83d\\udeaa","\\ud83d\\udeac","\\ud83d\\udca3","\\ud83d\\udd2b","\\ud83d\\udd2a","\\ud83d\\udc8a","\\ud83d\\udc89","\\ud83d\\udcb0","\\ud83d\\udcb5","\\ud83d\\udcb3","\\u2709","\\ud83d\\udceb","\\ud83d\\udce6","\\ud83d\\udcc5","\\ud83d\\udcc1","\\u2702","\\ud83d\\udccc","\\ud83d\\udcce","\\u2712","\\u270f","\\ud83d\\udcd0","\\ud83d\\udcda","\\ud83d\\udd2c","\\ud83d\\udd2d","\\ud83c\\udfa8","\\ud83c\\udfac","\\ud83c\\udfa4","\\ud83c\\udfa7","\\ud83c\\udfb5","\\ud83c\\udfb9","\\ud83c\\udfbb","\\ud83c\\udfba","\\ud83c\\udfb8","\\ud83d\\udc7e","\\ud83c\\udfae","\\ud83c\\udccf","\\ud83c\\udfb2","\\ud83c\\udfaf","\\ud83c\\udfc8","\\ud83c\\udfc0","\\u26bd","\\u26be","\\ud83c\\udfbe","\\ud83c\\udfb1","\\ud83c\\udfc9","\\ud83c\\udfb3","\\ud83c\\udfc1","\\ud83c\\udfc7","\\ud83c\\udfc6","\\ud83c\\udfca","\\ud83c\\udfc4","\\u2615","\\ud83c\\udf7c","\\ud83c\\udf7a","\\ud83c\\udf77","\\ud83c\\udf74","\\ud83c\\udf55","\\ud83c\\udf54","\\ud83c\\udf5f","\\ud83c\\udf57","\\ud83c\\udf71","\\ud83c\\udf5a","\\ud83c\\udf5c","\\ud83c\\udf61","\\ud83c\\udf73","\\ud83c\\udf5e","\\ud83c\\udf69","\\ud83c\\udf66","\\ud83c\\udf82","\\ud83c\\udf70","\\ud83c\\udf6a","\\ud83c\\udf6b","\\ud83c\\udf6d","\\ud83c\\udf6f","\\ud83c\\udf4e","\\ud83c\\udf4f","\\ud83c\\udf4a","\\ud83c\\udf4b","\\ud83c\\udf52","\\ud83c\\udf47","\\ud83c\\udf49","\\ud83c\\udf53","\\ud83c\\udf51","\\ud83c\\udf4c","\\ud83c\\udf50","\\ud83c\\udf4d","\\ud83c\\udf46","\\ud83c\\udf45","\\ud83c\\udf3d","\\ud83c\\udfe1","\\ud83c\\udfe5","\\ud83c\\udfe6","\\u26ea","\\ud83c\\udff0","\\u26fa","\\ud83c\\udfed","\\ud83d\\uddfb","\\ud83d\\uddfd","\\ud83c\\udfa0","\\ud83c\\udfa1","\\u26f2","\\ud83c\\udfa2","\\ud83d\\udea2","\\ud83d\\udea4","\\u2693","\\ud83d\\ude80","\\u2708","\\ud83d\\ude81","\\ud83d\\ude82","\\ud83d\\ude8b","\\ud83d\\ude8e","\\ud83d\\ude8c","\\ud83d\\ude99","\\ud83d\\ude97","\\ud83d\\ude95","\\ud83d\\ude9b","\\ud83d\\udea8","\\ud83d\\ude94","\\ud83d\\ude92","\\ud83d\\ude91","\\ud83d\\udeb2","\\ud83d\\udea0","\\ud83d\\ude9c","\\ud83d\\udea6","\\u26a0","\\ud83d\\udea7","\\u26fd","\\ud83c\\udfb0","\\ud83d\\uddff","\\ud83c\\udfaa","\\ud83c\\udfad","\\ud83c\\uddef\\ud83c\\uddf5","\\ud83c\\uddf0\\ud83c\\uddf7","\\ud83c\\udde9\\ud83c\\uddea","\\ud83c\\udde8\\ud83c\\uddf3","\\ud83c\\uddfa\\ud83c\\uddf8","\\ud83c\\uddeb\\ud83c\\uddf7","\\ud83c\\uddea\\ud83c\\uddf8","\\ud83c\\uddee\\ud83c\\uddf9","\\ud83c\\uddf7\\ud83c\\uddfa","\\ud83c\\uddec\\ud83c\\udde7","1\\u20e3","2\\u20e3","3\\u20e3","4\\u20e3","5\\u20e3","6\\u20e3","7\\u20e3","8\\u20e3","9\\u20e3","0\\u20e3","\\ud83d\\udd1f","\\u2757","\\u2753","\\u2665","\\u2666","\\ud83d\\udcaf","\\ud83d\\udd17","\\ud83d\\udd31","\\ud83d\\udd34","\\ud83d\\udd35","\\ud83d\\udd36","\\ud83d\\udd37"]'; /** @@ -115,6 +271,7 @@ class Magic self::$twoe2048 = new \phpseclib3\Math\BigInteger('32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230656'); self::$twozerotwosixone = new \phpseclib3\Math\BigInteger(20261); self::$zeroeight = new \phpseclib3\Math\BigInteger('2147483648'); + self::$ton = new \phpseclib3\Math\BigInteger('7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed', 16); try { self::$isatty = \defined('STDOUT') && \function_exists('posix_isatty') && \posix_isatty(STDOUT); diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index 7d16fe04..c4cbfd64 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -262,11 +262,6 @@ class TL $TL_dict[$type][$key]['params'][] = ['name' => $explode[0], 'type' => $explode[1]]; } } - /* - if (!$TL_dict[$type][$key][$type === 'constructors' ? 'predicate' : 'method']) { - var_dump($line); - \var_dump($TL_dict[$type][$key]); - }*/ $key++; } @@ -584,17 +579,14 @@ class TL if ($bare = $type['type'] != '' && $type['type'][0] === '%') { $type['type'] = \substr($type['type'], 1); } - if ($predicate === $type['type'] && !$auto) { + if ($predicate === $type['type']) {//} && !$auto) { $bare = true; } if ($predicate === 'messageEntityMentionName') { $constructorData = $this->constructors->findByPredicate('inputMessageEntityMentionName'); } - $concat = ''; - if (!$bare) { - $concat = $constructorData['id']; - } + $concat = $bare ? '' : $constructorData['id']; return $concat.yield $this->serializeParams($constructorData, $object, '', $layer); } @@ -876,8 +868,6 @@ class TL */ public function deserialize($stream, $type = ['type' => '']) { - //var_dump($type); - if (\is_string($stream)) { $res = \fopen('php://memory', 'rw+b'); \fwrite($res, $stream); diff --git a/src/danog/MadelineProto/TON/API.php b/src/danog/MadelineProto/TON/API.php index cf4ef83f..4edf438d 100644 --- a/src/danog/MadelineProto/TON/API.php +++ b/src/danog/MadelineProto/TON/API.php @@ -27,7 +27,7 @@ use danog\MadelineProto\Magic; class API extends InternalDoc { /** - * Construct API + * Construct API. * * @param array $settings Settings */ @@ -37,7 +37,7 @@ class API extends InternalDoc $this->API = new Lite($settings); foreach (\get_class_methods($this->API) as $method) { - $this->methods[$method] = [$this->API, strtolower($method)]; + $this->methods[$method] = [$this->API, \strtolower($method)]; } } } diff --git a/src/danog/MadelineProto/TON/APIFactory.php b/src/danog/MadelineProto/TON/APIFactory.php index 896a4362..14d70471 100644 --- a/src/danog/MadelineProto/TON/APIFactory.php +++ b/src/danog/MadelineProto/TON/APIFactory.php @@ -79,11 +79,11 @@ class APIFactory extends AbstractAPIFactory public $liteServer; /** - * Just proxy async requests to API + * Just proxy async requests to API. * * @param string $name Method name * @param array $arguments Arguments - * + * * @return mixed */ public function __call_async($name, $arguments) diff --git a/src/danog/MadelineProto/TON/Lite.php b/src/danog/MadelineProto/TON/Lite.php index 6adcc438..c8931d4e 100644 --- a/src/danog/MadelineProto/TON/Lite.php +++ b/src/danog/MadelineProto/TON/Lite.php @@ -21,6 +21,7 @@ namespace danog\MadelineProto\TON; use danog\MadelineProto\Logger; use danog\MadelineProto\TL\TL; +use danog\MadelineProto\Tools; use function Amp\File\get; @@ -53,6 +54,12 @@ class Lite * @var Logger */ public $logger; + /** + * Liteserver connections. + * + * @var ADNLConnection[] + */ + private $connections = []; /** * Construct settings. * @@ -83,7 +90,7 @@ class Lite $config['_'] = 'liteclient.config.global'; $config = Tools::convertJsonTL($config); $config['validator']['init_block'] = $config['validator']['init_block'] ?? $config['validator']['zero_state']; - + $this->config = yield $this->TL->deserialize( yield $this->TL->serializeObject( ['type' => ''], @@ -91,7 +98,17 @@ class Lite 'cleanup' ) ); - var_dump($this->config); + + foreach ($this->config['liteservers'] as $lite) { + $this->connections[] = $connection = new ADNLConnection($this->TL); + yield $connection->connect($lite); + yield $connection->send( + [ + '_' => 'liteServer.getTime' + ] + ); + } + yield Tools::sleep(10); } /** diff --git a/src/danog/MadelineProto/TON/Tools.php b/src/danog/MadelineProto/TON/Tools.php deleted file mode 100644 index 9311ad7a..00000000 --- a/src/danog/MadelineProto/TON/Tools.php +++ /dev/null @@ -1,43 +0,0 @@ -. - * - * @author Daniil Gentili - * @copyright 2016-2019 Daniil Gentili - * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 - * - * @link https://docs.madelineproto.xyz MadelineProto documentation - */ - -namespace danog\MadelineProto\TON; - -class Tools -{ - /** - * Sanify TL obtained from JSON for TL serialization. - * - * @param array $input Data to sanitize - - * @return array - */ - public static function convertJsonTL(array $input): array - { - $cb = static function (&$val) use (&$cb) { - if (isset($val['@type'])) { - $val['_'] = $val['@type']; - } elseif (\is_array($val)) { - \array_walk($val, $cb); - } - }; - \array_walk($input, $cb); - return $input; - } -} diff --git a/src/danog/MadelineProto/Tools.php b/src/danog/MadelineProto/Tools.php index f5d7413a..49dac6da 100644 --- a/src/danog/MadelineProto/Tools.php +++ b/src/danog/MadelineProto/Tools.php @@ -42,6 +42,26 @@ use function Amp\Promise\wait; */ trait Tools { + + /** + * Sanify TL obtained from JSON for TL serialization. + * + * @param array $input Data to sanitize + + * @return array + */ + public static function convertJsonTL(array $input): array + { + $cb = static function (&$val) use (&$cb) { + if (isset($val['@type'])) { + $val['_'] = $val['@type']; + } elseif (\is_array($val)) { + \array_walk($val, $cb); + } + }; + \array_walk($input, $cb); + return $input; + } /** * Generate MTProto vector hash. *