Applied fixes from StyleCI

This commit is contained in:
Daniil Gentili 2016-08-22 13:19:28 +00:00 committed by StyleCI Bot
parent b4f4fa5fb2
commit 3177d57fdd
2 changed files with 118 additions and 117 deletions

View File

@ -141,13 +141,14 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
$in = $content_related ? 1 : 0; $in = $content_related ? 1 : 0;
$value = $this->seq_no; $value = $this->seq_no;
$this->seq_no += $in; $this->seq_no += $in;
var_dump((($value*2)+ $in)); var_dump((($value * 2) + $in));
return (($value*2) + $in);
return ($value * 2) + $in;
} }
public function anknowledge($msg_id) public function anknowledge($msg_id)
{ {
return $this->send_message($this->tl->serialize_obj("msgs_ack", ["msg_ids" => [$msg_id]])); return $this->send_message($this->tl->serialize_obj('msgs_ack', ['msg_ids' => [$msg_id]]));
} }
/** /**
@ -158,7 +159,7 @@ var_dump((($value*2)+ $in));
{ {
$message_id = $this->struct->pack('<Q', (int) ((time() + $this->timedelta) * pow(2, 30)) * 4); $message_id = $this->struct->pack('<Q', (int) ((time() + $this->timedelta) * pow(2, 30)) * 4);
$this->check_message_id($message_id, true); $this->check_message_id($message_id, true);
$seq_no = $this->generate_seq_no(true); $seq_no = $this->generate_seq_no(true);
if (($this->settings['authorization']['temp_auth_key']['auth_key'] == null) || ($this->settings['authorization']['temp_auth_key']['server_salt'] == null)) { if (($this->settings['authorization']['temp_auth_key']['auth_key'] == null) || ($this->settings['authorization']['temp_auth_key']['server_salt'] == null)) {
$message = Tools::string2bin('\x00\x00\x00\x00\x00\x00\x00\x00').$message_id.$this->struct->pack('<I', strlen($message_data)).$message_data; $message = Tools::string2bin('\x00\x00\x00\x00\x00\x00\x00\x00').$message_id.$this->struct->pack('<I', strlen($message_data)).$message_data;
} else { } else {
@ -241,7 +242,7 @@ var_dump((($value*2)+ $in));
{ {
foreach (range(1, $this->settings['max_tries']['query']) as $i) { foreach (range(1, $this->settings['max_tries']['query']) as $i) {
try { try {
var_dump($method); var_dump($method);
$this->send_message($this->tl->serialize_method($method, $kwargs)); $this->send_message($this->tl->serialize_method($method, $kwargs));
$server_answer = $this->recv_message(); $server_answer = $this->recv_message();
} catch (Exception $e) { } catch (Exception $e) {
@ -263,60 +264,60 @@ var_dump($method);
public function create_auth_key($expires_in = -1) public function create_auth_key($expires_in = -1)
{ {
foreach (Tools::range(0, $this->settings['max_tries']['authorization']) as $retry_id_total) { foreach (Tools::range(0, $this->settings['max_tries']['authorization']) as $retry_id_total) {
// Make pq request // Make pq request
$nonce = \phpseclib\Crypt\Random::string(16); $nonce = \phpseclib\Crypt\Random::string(16);
$this->log->log('Handshake: Requesting pq'); $this->log->log('Handshake: Requesting pq');
$ResPQ = $this->method_call('req_pq', ['nonce' => $nonce]); $ResPQ = $this->method_call('req_pq', ['nonce' => $nonce]);
$server_nonce = $ResPQ['server_nonce']; $server_nonce = $ResPQ['server_nonce'];
if ($ResPQ['nonce'] !== $nonce) { if ($ResPQ['nonce'] !== $nonce) {
throw new Exception('Handshake: wrong nonce'); throw new Exception('Handshake: wrong nonce');
}
foreach ($ResPQ['server_public_key_fingerprints'] as $curfp) {
$curfp_biginteger = new \phpseclib\Math\BigInteger($curfp);
if ($this->key->fp->equals($curfp_biginteger)) {
$public_key_fingerprint = $curfp;
break;
} }
} foreach ($ResPQ['server_public_key_fingerprints'] as $curfp) {
if (!isset($public_key_fingerprint)) { $curfp_biginteger = new \phpseclib\Math\BigInteger($curfp);
throw new Exception("Handshake: couldn't find our key in the server_public_key_fingerprints vector."); if ($this->key->fp->equals($curfp_biginteger)) {
} $public_key_fingerprint = $curfp;
$pq_bytes = $ResPQ['pq']; break;
}
}
if (!isset($public_key_fingerprint)) {
throw new Exception("Handshake: couldn't find our key in the server_public_key_fingerprints vector.");
}
$pq_bytes = $ResPQ['pq'];
// Compute p and q // Compute p and q
$pq = new \phpseclib\Math\BigInteger($pq_bytes, 256); $pq = new \phpseclib\Math\BigInteger($pq_bytes, 256);
list($p, $q) = $this->PrimeModule->primefactors($pq); list($p, $q) = $this->PrimeModule->primefactors($pq);
$p = new \phpseclib\Math\BigInteger($p); $p = new \phpseclib\Math\BigInteger($p);
$q = new \phpseclib\Math\BigInteger($q); $q = new \phpseclib\Math\BigInteger($q);
if ($p->compare($q) > 0) { if ($p->compare($q) > 0) {
list($p, $q) = [$q, $p]; list($p, $q) = [$q, $p];
} }
if (!($pq->equals($p->multiply($q)) && $p->compare($q) < 0)) { if (!($pq->equals($p->multiply($q)) && $p->compare($q) < 0)) {
throw new Exception("Handshake: couldn't compute p and q."); throw new Exception("Handshake: couldn't compute p and q.");
} }
$this->log->log(sprintf('Factorization %s = %s * %s', $pq, $p, $q)); $this->log->log(sprintf('Factorization %s = %s * %s', $pq, $p, $q));
// Serialize object for req_DH_params // Serialize object for req_DH_params
$p_bytes = $this->struct->pack('>I', (string) $p); $p_bytes = $this->struct->pack('>I', (string) $p);
$q_bytes = $this->struct->pack('>I', (string) $q); $q_bytes = $this->struct->pack('>I', (string) $q);
$new_nonce = \phpseclib\Crypt\Random::string(32); $new_nonce = \phpseclib\Crypt\Random::string(32);
if ($expires_in < 0) { if ($expires_in < 0) {
$data = $this->tl->serialize_obj('p_q_inner_data', ['pq' => $pq_bytes, 'p' => $p_bytes, 'q' => $q_bytes, 'nonce' => $nonce, 'server_nonce' => $server_nonce, 'new_nonce' => $new_nonce]); $data = $this->tl->serialize_obj('p_q_inner_data', ['pq' => $pq_bytes, 'p' => $p_bytes, 'q' => $q_bytes, 'nonce' => $nonce, 'server_nonce' => $server_nonce, 'new_nonce' => $new_nonce]);
} else { } else {
$data = $this->tl->serialize_obj('p_q_inner_data_temp', ['pq' => $pq_bytes, 'p' => $p_bytes, 'q' => $q_bytes, 'nonce' => $nonce, 'server_nonce' => $server_nonce, 'new_nonce' => $new_nonce, 'expires_in' => $expires_in]); $data = $this->tl->serialize_obj('p_q_inner_data_temp', ['pq' => $pq_bytes, 'p' => $p_bytes, 'q' => $q_bytes, 'nonce' => $nonce, 'server_nonce' => $server_nonce, 'new_nonce' => $new_nonce, 'expires_in' => $expires_in]);
} }
$sha_digest = sha1($data, true); $sha_digest = sha1($data, true);
// Encrypt serialized object // Encrypt serialized object
$random_bytes = \phpseclib\Crypt\Random::string(255 - strlen($data) - strlen($sha_digest)); $random_bytes = \phpseclib\Crypt\Random::string(255 - strlen($data) - strlen($sha_digest));
$to_encrypt = $sha_digest.$data.$random_bytes; $to_encrypt = $sha_digest.$data.$random_bytes;
$encrypted_data = $this->key->encrypt($to_encrypt); $encrypted_data = $this->key->encrypt($to_encrypt);
// req_DH_params // req_DH_params
$this->log->log('Starting Diffie Hellman key exchange'); $this->log->log('Starting Diffie Hellman key exchange');
$server_dh_params = $this->method_call('req_DH_params', $server_dh_params = $this->method_call('req_DH_params',
[ [
'nonce' => $nonce, 'nonce' => $nonce,
'server_nonce' => $server_nonce, 'server_nonce' => $server_nonce,
@ -330,52 +331,52 @@ var_dump($method);
if ($nonce != $server_dh_params['nonce']) { if ($nonce != $server_dh_params['nonce']) {
throw new Exception('Handshake: wrong nonce.'); throw new Exception('Handshake: wrong nonce.');
} }
if ($server_nonce != $server_dh_params['server_nonce']) { if ($server_nonce != $server_dh_params['server_nonce']) {
throw new Exception('Handshake: wrong server nonce.'); throw new Exception('Handshake: wrong server nonce.');
} }
if (isset($server_dh_params['new_nonce_hash']) && substr(sha1($new_nonce), -32) != $server_dh_params['new_nonce_hash']) { if (isset($server_dh_params['new_nonce_hash']) && substr(sha1($new_nonce), -32) != $server_dh_params['new_nonce_hash']) {
throw new Exception('Handshake: wrong new nonce hash.'); throw new Exception('Handshake: wrong new nonce hash.');
} }
// Get key and iv and decrypt answer // Get key and iv and decrypt answer
$encrypted_answer = $server_dh_params['encrypted_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); $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 = Crypt::ige_decrypt($encrypted_answer, $tmp_aes_key, $tmp_aes_iv); $answer_with_hash = Crypt::ige_decrypt($encrypted_answer, $tmp_aes_key, $tmp_aes_iv);
// Separate answer and hash // Separate answer and hash
$answer_hash = substr($answer_with_hash, 0, 20); $answer_hash = substr($answer_with_hash, 0, 20);
$answer = substr($answer_with_hash, 20); $answer = substr($answer_with_hash, 20);
// Deserialize // Deserialize
$server_DH_inner_data = $this->tl->deserialize(Tools::fopen_and_write('php://memory', 'rw+b', $answer)); $server_DH_inner_data = $this->tl->deserialize(Tools::fopen_and_write('php://memory', 'rw+b', $answer));
// Do some checks // Do some checks
$server_DH_inner_data_length = $this->tl->get_length(Tools::fopen_and_write('php://memory', 'rw+b', $answer)); $server_DH_inner_data_length = $this->tl->get_length(Tools::fopen_and_write('php://memory', 'rw+b', $answer));
if (sha1(substr($answer, 0, $server_DH_inner_data_length), true) != $answer_hash) { if (sha1(substr($answer, 0, $server_DH_inner_data_length), true) != $answer_hash) {
throw new Exception('Handshake: answer_hash mismatch.'); throw new Exception('Handshake: answer_hash mismatch.');
} }
if ($nonce != $server_DH_inner_data['nonce']) { if ($nonce != $server_DH_inner_data['nonce']) {
throw new Exception('Handshake: wrong nonce'); throw new Exception('Handshake: wrong nonce');
} }
if ($server_nonce != $server_DH_inner_data['server_nonce']) { if ($server_nonce != $server_DH_inner_data['server_nonce']) {
throw new Exception('Handshake: wrong server nonce'); throw new Exception('Handshake: wrong server nonce');
} }
$g = new \phpseclib\Math\BigInteger($server_DH_inner_data['g']); $g = new \phpseclib\Math\BigInteger($server_DH_inner_data['g']);
$g_a = new \phpseclib\Math\BigInteger($server_DH_inner_data['g_a'], 256); $g_a = new \phpseclib\Math\BigInteger($server_DH_inner_data['g_a'], 256);
$dh_prime = new \phpseclib\Math\BigInteger($server_DH_inner_data['dh_prime'], 256); $dh_prime = new \phpseclib\Math\BigInteger($server_DH_inner_data['dh_prime'], 256);
// Time delta // Time delta
$server_time = $server_DH_inner_data['server_time']; $server_time = $server_DH_inner_data['server_time'];
$this->timedelta = ($server_time - time()); $this->timedelta = ($server_time - time());
$this->log->log(sprintf('Server-client time delta = %.1f s', $this->timedelta)); $this->log->log(sprintf('Server-client time delta = %.1f s', $this->timedelta));
// Define some needed numbers for BigInteger // Define some needed numbers for BigInteger
$one = new \phpseclib\Math\BigInteger(1); $one = new \phpseclib\Math\BigInteger(1);
$two = new \phpseclib\Math\BigInteger(2); $two = new \phpseclib\Math\BigInteger(2);
$twoe2047 = new \phpseclib\Math\BigInteger('16158503035655503650357438344334975980222051334857742016065172713762327569433945446598600705761456731844358980460949009747059779575245460547544076193224141560315438683650498045875098875194826053398028819192033784138396109321309878080919047169238085235290822926018152521443787945770532904303776199561965192760957166694834171210342487393282284747428088017663161029038902829665513096354230157075129296432088558362971801859230928678799175576150822952201848806616643615613562842355410104862578550863465661734839271290328348967522998634176499319107762583194718667771801067716614802322659239302476074096777926805529798115328'); $twoe2047 = new \phpseclib\Math\BigInteger('16158503035655503650357438344334975980222051334857742016065172713762327569433945446598600705761456731844358980460949009747059779575245460547544076193224141560315438683650498045875098875194826053398028819192033784138396109321309878080919047169238085235290822926018152521443787945770532904303776199561965192760957166694834171210342487393282284747428088017663161029038902829665513096354230157075129296432088558362971801859230928678799175576150822952201848806616643615613562842355410104862578550863465661734839271290328348967522998634176499319107762583194718667771801067716614802322659239302476074096777926805529798115328');
$twoe2048 = new \phpseclib\Math\BigInteger('32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230656'); $twoe2048 = new \phpseclib\Math\BigInteger('32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230656');
// Check validity of dh_prime // Check validity of dh_prime
if (!$dh_prime->isPrime()) { if (!$dh_prime->isPrime()) {
@ -410,9 +411,9 @@ var_dump($method);
throw new Exception('Handshake: g_a is invalid (1 < g_a < dh_prime - 1 is false).'); throw new Exception('Handshake: g_a is invalid (1 < g_a < dh_prime - 1 is false).');
} }
foreach (Tools::range(0, $this->settings['max_tries']['authorization']) as $retry_id) { foreach (Tools::range(0, $this->settings['max_tries']['authorization']) as $retry_id) {
$b = new \phpseclib\Math\BigInteger(\phpseclib\Crypt\Random::string(256), 256); $b = new \phpseclib\Math\BigInteger(\phpseclib\Crypt\Random::string(256), 256);
$g_b = $g->powMod($b, $dh_prime); $g_b = $g->powMod($b, $dh_prime);
// Check validity of g_b // Check validity of g_b
// 1 < g_b < dh_prime - 1 // 1 < g_b < dh_prime - 1
@ -422,12 +423,12 @@ var_dump($method);
throw new Exception('Handshake: g_b is invalid (1 < g_b < dh_prime - 1 is false).'); throw new Exception('Handshake: g_b is invalid (1 < g_b < dh_prime - 1 is false).');
} }
$g_b_str = $g_b->toBytes(); $g_b_str = $g_b->toBytes();
// serialize client_DH_inner_data // serialize client_DH_inner_data
$data = $this->tl->serialize_obj('client_DH_inner_data', ['nonce' => $nonce, 'server_nonce' => $server_nonce, 'retry_id' => $retry_id, 'g_b' => $g_b_str]); $data = $this->tl->serialize_obj('client_DH_inner_data', ['nonce' => $nonce, 'server_nonce' => $server_nonce, 'retry_id' => $retry_id, 'g_b' => $g_b_str]);
$data_with_sha = sha1($data, true).$data; $data_with_sha = sha1($data, true).$data;
$data_with_sha_padded = $data_with_sha.\phpseclib\Crypt\Random::string(Tools::posmod(-strlen($data_with_sha), 16)); $data_with_sha_padded = $data_with_sha.\phpseclib\Crypt\Random::string(Tools::posmod(-strlen($data_with_sha), 16));
// encrypt client_DH_inner_data // encrypt client_DH_inner_data
$encrypted_data = Crypt::ige_encrypt($data_with_sha_padded, $tmp_aes_key, $tmp_aes_iv); $encrypted_data = Crypt::ige_encrypt($data_with_sha_padded, $tmp_aes_key, $tmp_aes_iv);
@ -437,51 +438,51 @@ var_dump($method);
// Generate auth_key // Generate auth_key
$auth_key = $g_a->powMod($b, $dh_prime); $auth_key = $g_a->powMod($b, $dh_prime);
$auth_key_str = $auth_key->toBytes(); $auth_key_str = $auth_key->toBytes();
$auth_key_sha = sha1($auth_key_str, true); $auth_key_sha = sha1($auth_key_str, true);
$auth_key_aux_hash = substr($auth_key_sha, 0, 8); $auth_key_aux_hash = substr($auth_key_sha, 0, 8);
$new_nonce_hash1 = substr(sha1($new_nonce.chr(1).$auth_key_aux_hash, true), -16); $new_nonce_hash1 = substr(sha1($new_nonce.chr(1).$auth_key_aux_hash, true), -16);
$new_nonce_hash2 = substr(sha1($new_nonce.chr(2).$auth_key_aux_hash, true), -16); $new_nonce_hash2 = substr(sha1($new_nonce.chr(2).$auth_key_aux_hash, true), -16);
$new_nonce_hash3 = substr(sha1($new_nonce.chr(3).$auth_key_aux_hash, true), -16); $new_nonce_hash3 = substr(sha1($new_nonce.chr(3).$auth_key_aux_hash, true), -16);
if ($Set_client_DH_params_answer['nonce'] != $nonce) { if ($Set_client_DH_params_answer['nonce'] != $nonce) {
throw new Exception('Handshake: wrong nonce.'); throw new Exception('Handshake: wrong nonce.');
}
if ($Set_client_DH_params_answer['server_nonce'] != $server_nonce) {
throw new Exception('Handshake: wrong server nonce');
}
if ($Set_client_DH_params_answer['_'] == 'dh_gen_ok') {
if ($Set_client_DH_params_answer['new_nonce_hash1'] != $new_nonce_hash1) {
throw new Exception('Handshake: wrong new_nonce_hash1');
} }
$this->log->log('Diffie Hellman key exchange processed successfully'); if ($Set_client_DH_params_answer['server_nonce'] != $server_nonce) {
throw new Exception('Handshake: wrong server nonce');
}
if ($Set_client_DH_params_answer['_'] == 'dh_gen_ok') {
if ($Set_client_DH_params_answer['new_nonce_hash1'] != $new_nonce_hash1) {
throw new Exception('Handshake: wrong new_nonce_hash1');
}
$this->log->log('Diffie Hellman key exchange processed successfully');
$res_authorization = ['server_salt' => substr($new_nonce, 0, 8 - 0) ^ substr($server_nonce, 0, 8 - 0)]; $res_authorization = ['server_salt' => substr($new_nonce, 0, 8 - 0) ^ substr($server_nonce, 0, 8 - 0)];
$res_authorization['auth_key'] = $auth_key_str; $res_authorization['auth_key'] = $auth_key_str;
$res_authorization['id'] = substr($auth_key_sha, -8); $res_authorization['id'] = substr($auth_key_sha, -8);
if ($expires_in < 0) { if ($expires_in < 0) {
$res_authorization['expires_in'] = $expires_in; $res_authorization['expires_in'] = $expires_in;
} }
$this->log->log('Auth key generated'); $this->log->log('Auth key generated');
$this->timedelta = 0; $this->timedelta = 0;
return $res_authorization; return $res_authorization;
} elseif ($Set_client_DH_params_answer['_'] == 'dh_gen_retry') { } elseif ($Set_client_DH_params_answer['_'] == 'dh_gen_retry') {
if ($Set_client_DH_params_answer['new_nonce_hash2'] != $new_nonce_hash2) { if ($Set_client_DH_params_answer['new_nonce_hash2'] != $new_nonce_hash2) {
throw new Exception('Handshake: wrong new_nonce_hash_2'); throw new Exception('Handshake: wrong new_nonce_hash_2');
}
$this->log->log('Retrying Auth');
} elseif ($Set_client_DH_params_answer['_'] == 'dh_gen_fail') {
if ($Set_client_DH_params_answer['new_nonce_hash3'] != $new_nonce_hash3) {
throw new Exception('Handshake: wrong new_nonce_hash_3');
}
$this->log->log('Auth Failed');
break;
} else {
throw new Exception('Response Error');
} }
$this->log->log('Retrying Auth');
} elseif ($Set_client_DH_params_answer['_'] == 'dh_gen_fail') {
if ($Set_client_DH_params_answer['new_nonce_hash3'] != $new_nonce_hash3) {
throw new Exception('Handshake: wrong new_nonce_hash_3');
}
$this->log->log('Auth Failed');
break;
} else {
throw new Exception('Response Error');
} }
} }
}
throw new Exception('Auth Failed'); throw new Exception('Auth Failed');
} }

View File

@ -17,10 +17,10 @@ class TL
public function __construct($filename) public function __construct($filename)
{ {
if (is_array($filename)) { if (is_array($filename)) {
$TL_dict = ["constructors" => [], "methods" => []]; $TL_dict = ['constructors' => [], 'methods' => []];
foreach ($filename as $file) { foreach ($filename as $file) {
$TL_dict["constructors"] = array_merge(json_decode(file_get_contents($file), true)["constructors"], $TL_dict["constructors"]); $TL_dict['constructors'] = array_merge(json_decode(file_get_contents($file), true)['constructors'], $TL_dict['constructors']);
$TL_dict["methods"] = array_merge(json_decode(file_get_contents($file), true)["methods"], $TL_dict["methods"]); $TL_dict['methods'] = array_merge(json_decode(file_get_contents($file), true)['methods'], $TL_dict['methods']);
} }
} else { } else {
$TL_dict = json_decode(file_get_contents($file), true); $TL_dict = json_decode(file_get_contents($file), true);