1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-12-02 17:17:48 +01:00

Applied fixes from StyleCI

This commit is contained in:
Daniil Gentili 2016-10-07 09:57:07 +00:00 committed by StyleCI Bot
parent b4cd001fe9
commit 246579d645

View File

@ -22,17 +22,19 @@ class AuthKeyHandler extends AckHandler
{ {
public function create_auth_key($expires_in = -1) public function create_auth_key($expires_in = -1)
{ {
foreach (\danog\MadelineProto\Tools::range(0, $this->settings['max_tries']['authorization']) as $retry_id_total) foreach (\danog\MadelineProto\Tools::range(0, $this->settings['max_tries']['authorization']) as $retry_id_total) {
{
$this->log->log('Handshake: Requesting pq'); $this->log->log('Handshake: Requesting pq');
/** /**
* *********************************************************************** * ***********************************************************************
* Make pq request, DH exchange initiation * Make pq request, DH exchange initiation.
*
* @method req_pq * @method req_pq
*
* @param [ * @param [
* int128 $nonce : The value of nonce is selected randomly by the client (random number) and identifies the client within this communication * int128 $nonce : The value of nonce is selected randomly by the client (random number) and identifies the client within this communication
* ] * ]
*
* @return ResPQ [ * @return ResPQ [
* int128 $nonce : The value of nonce is selected randomly by the server * int128 $nonce : The value of nonce is selected randomly by the server
* int128 $server_nonce : The value of server_nonce is selected randomly by the server * int128 $server_nonce : The value of server_nonce is selected randomly by the server
@ -43,11 +45,11 @@ class AuthKeyHandler extends AckHandler
$nonce = \phpseclib\Crypt\Random::string(16); $nonce = \phpseclib\Crypt\Random::string(16);
$ResPQ = $this->method_call('req_pq', $ResPQ = $this->method_call('req_pq',
[ [
'nonce' => $nonce 'nonce' => $nonce,
] ]
); );
/** /*
* *********************************************************************** * ***********************************************************************
* Compare the equal of nonce client and nonce server * Compare the equal of nonce client and nonce server
*/ */
@ -55,12 +57,11 @@ class AuthKeyHandler extends AckHandler
throw new Exception('Handshake: wrong nonce'); throw new Exception('Handshake: wrong nonce');
} }
/** /*
* *********************************************************************** * ***********************************************************************
* Find our key in the server_public_key_fingerprints vector * Find our key in the server_public_key_fingerprints vector
*/ */
foreach ($ResPQ['server_public_key_fingerprints'] as $curfp) foreach ($ResPQ['server_public_key_fingerprints'] as $curfp) {
{
$curfp_biginteger = new \phpseclib\Math\BigInteger($curfp); $curfp_biginteger = new \phpseclib\Math\BigInteger($curfp);
if ($this->key->fp->equals($curfp_biginteger)) { if ($this->key->fp->equals($curfp_biginteger)) {
@ -76,7 +77,7 @@ class AuthKeyHandler extends AckHandler
$pq_bytes = $ResPQ['pq']; $pq_bytes = $ResPQ['pq'];
$server_nonce = $ResPQ['server_nonce']; $server_nonce = $ResPQ['server_nonce'];
/** /*
* *********************************************************************** * ***********************************************************************
* Compute p and q * Compute p and q
*/ */
@ -95,7 +96,7 @@ class AuthKeyHandler extends AckHandler
$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
*/ */
@ -103,8 +104,7 @@ class AuthKeyHandler extends AckHandler
$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', $data = $this->tl->serialize_obj('p_q_inner_data',
[ [
'pq' => $pq_bytes, 'pq' => $pq_bytes,
@ -112,11 +112,10 @@ class AuthKeyHandler extends AckHandler
'q' => $q_bytes, 'q' => $q_bytes,
'nonce' => $nonce, 'nonce' => $nonce,
'server_nonce' => $server_nonce, 'server_nonce' => $server_nonce,
'new_nonce' => $new_nonce 'new_nonce' => $new_nonce,
] ]
); );
} } else {
else {
$data = $this->tl->serialize_obj('p_q_inner_data_temp', $data = $this->tl->serialize_obj('p_q_inner_data_temp',
[ [
'pq' => $pq_bytes, 'pq' => $pq_bytes,
@ -125,12 +124,12 @@ class AuthKeyHandler extends AckHandler
'nonce' => $nonce, 'nonce' => $nonce,
'server_nonce' => $server_nonce, 'server_nonce' => $server_nonce,
'new_nonce' => $new_nonce, 'new_nonce' => $new_nonce,
'expires_in' => $expires_in 'expires_in' => $expires_in,
] ]
); );
} }
/** /*
* *********************************************************************** * ***********************************************************************
* Encrypt serialized object * Encrypt serialized object
*/ */
@ -140,7 +139,7 @@ class AuthKeyHandler extends AckHandler
$encrypted_data = $this->key->encrypt($to_encrypt); $encrypted_data = $this->key->encrypt($to_encrypt);
$this->log->log('Starting Diffie Hellman key exchange'); $this->log->log('Starting Diffie Hellman key exchange');
/** /*
* *********************************************************************** * ***********************************************************************
* Starting Diffie Hellman key exchange, Server authentication * Starting Diffie Hellman key exchange, Server authentication
* @method req_DH_params * @method req_DH_params
@ -171,7 +170,7 @@ class AuthKeyHandler extends AckHandler
] ]
); );
/** /*
* *********************************************************************** * ***********************************************************************
* Compare the equal of nonce client and nonce server * Compare the equal of nonce client and nonce server
*/ */
@ -179,7 +178,7 @@ class AuthKeyHandler extends AckHandler
throw new Exception('Handshake: wrong nonce.'); throw new Exception('Handshake: wrong nonce.');
} }
/** /*
* *********************************************************************** * ***********************************************************************
* Compare the equal of server_nonce and new server_nonce * Compare the equal of server_nonce and new server_nonce
*/ */
@ -187,7 +186,7 @@ class AuthKeyHandler extends AckHandler
throw new Exception('Handshake: wrong server nonce.'); throw new Exception('Handshake: wrong server nonce.');
} }
/** /*
* *********************************************************************** * ***********************************************************************
* Check valid new nonce hash if return from server * Check valid new nonce hash if return from server
* new nonce hash return in server_DH_params_fail * new nonce hash return in server_DH_params_fail
@ -196,7 +195,7 @@ class AuthKeyHandler extends AckHandler
throw new Exception('Handshake: wrong new nonce hash.'); throw new Exception('Handshake: wrong new nonce hash.');
} }
/** /*
* *********************************************************************** * ***********************************************************************
* Get key, iv and decrypt answer * Get key, iv and decrypt answer
*/ */
@ -206,14 +205,14 @@ class AuthKeyHandler extends AckHandler
$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 = $this->ige_decrypt($encrypted_answer, $tmp_aes_key, $tmp_aes_iv); $answer_with_hash = $this->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 answer * Deserialize answer
* @return Server_DH_inner_data [ * @return Server_DH_inner_data [
@ -227,7 +226,7 @@ class AuthKeyHandler extends AckHandler
*/ */
$server_DH_inner_data = $this->tl->deserialize(\danog\MadelineProto\Tools::fopen_and_write('php://memory', 'rw+b', $answer)); $server_DH_inner_data = $this->tl->deserialize(\danog\MadelineProto\Tools::fopen_and_write('php://memory', 'rw+b', $answer));
/** /*
* *********************************************************************** * ***********************************************************************
* Do some checks * Do some checks
*/ */
@ -248,7 +247,7 @@ class AuthKeyHandler extends AckHandler
$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
*/ */
@ -258,7 +257,7 @@ class AuthKeyHandler extends AckHandler
$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
*/ */
@ -267,7 +266,7 @@ class AuthKeyHandler extends AckHandler
$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
* 2^2047 < dh_prime < 2^2048 * 2^2047 < dh_prime < 2^2048
@ -289,7 +288,7 @@ class AuthKeyHandler extends AckHandler
throw new Exception("Handshake: g isn't a safe 2048-bit prime (2^2047 < dh_prime < 2^2048 is false)."); throw new Exception("Handshake: g isn't a safe 2048-bit prime (2^2047 < dh_prime < 2^2048 is false).");
} }
/** /*
* *********************************************************************** * ***********************************************************************
* Check validity of g * Check validity of g
* 1 < g < dh_prime - 1 * 1 < g < dh_prime - 1
@ -300,7 +299,7 @@ class AuthKeyHandler extends AckHandler
throw new Exception('Handshake: g is invalid (1 < g < dh_prime - 1 is false).'); throw new Exception('Handshake: g is invalid (1 < g < dh_prime - 1 is false).');
} }
/** /*
* *********************************************************************** * ***********************************************************************
* Check validity of g_a * Check validity of g_a
* 1 < g_a < dh_prime - 1 * 1 < g_a < dh_prime - 1
@ -311,12 +310,11 @@ class AuthKeyHandler extends AckHandler
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 (\danog\MadelineProto\Tools::range(0, $this->settings['max_tries']['authorization']) as $retry_id) foreach (\danog\MadelineProto\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
@ -329,7 +327,7 @@ class AuthKeyHandler extends AckHandler
$g_b_str = $g_b->toBytes(); $g_b_str = $g_b->toBytes();
/** /*
* *********************************************************************** * ***********************************************************************
* serialize client_DH_inner_data * serialize client_DH_inner_data
* @method client_DH_inner_data * @method client_DH_inner_data
@ -345,11 +343,11 @@ class AuthKeyHandler extends AckHandler
'nonce' => $nonce, 'nonce' => $nonce,
'server_nonce' => $server_nonce, 'server_nonce' => $server_nonce,
'retry_id' => $retry_id, 'retry_id' => $retry_id,
'g_b' => $g_b_str 'g_b' => $g_b_str,
] ]
); );
/** /*
* *********************************************************************** * ***********************************************************************
* encrypt client_DH_inner_data * encrypt client_DH_inner_data
*/ */
@ -357,7 +355,7 @@ class AuthKeyHandler extends AckHandler
$data_with_sha_padded = $data_with_sha.\phpseclib\Crypt\Random::string(\danog\MadelineProto\Tools::posmod(-strlen($data_with_sha), 16)); $data_with_sha_padded = $data_with_sha.\phpseclib\Crypt\Random::string(\danog\MadelineProto\Tools::posmod(-strlen($data_with_sha), 16));
$encrypted_data = $this->ige_encrypt($data_with_sha_padded, $tmp_aes_key, $tmp_aes_iv); $encrypted_data = $this->ige_encrypt($data_with_sha_padded, $tmp_aes_key, $tmp_aes_iv);
/** /*
* *********************************************************************** * ***********************************************************************
* Send set_client_DH_params query * Send set_client_DH_params query
* @method set_client_DH_params * @method set_client_DH_params
@ -378,11 +376,11 @@ class AuthKeyHandler extends AckHandler
[ [
'nonce' => $nonce, 'nonce' => $nonce,
'server_nonce' => $server_nonce, 'server_nonce' => $server_nonce,
'encrypted_data' => $encrypted_data 'encrypted_data' => $encrypted_data,
] ]
); );
/** /*
* *********************************************************************** * ***********************************************************************
* Generate auth_key * Generate auth_key
*/ */
@ -395,7 +393,7 @@ class AuthKeyHandler extends AckHandler
$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);
/** /*
* *********************************************************************** * ***********************************************************************
* Compare the equal of nonce client and nonce server * Compare the equal of nonce client and nonce server
*/ */
@ -403,7 +401,7 @@ class AuthKeyHandler extends AckHandler
throw new Exception('Handshake: wrong nonce.'); throw new Exception('Handshake: wrong nonce.');
} }
/** /*
* *********************************************************************** * ***********************************************************************
* Compare the equal of server_nonce and new server_nonce * Compare the equal of server_nonce and new server_nonce
*/ */
@ -411,12 +409,11 @@ class AuthKeyHandler extends AckHandler
throw new Exception('Handshake: wrong server nonce'); throw new Exception('Handshake: wrong server nonce');
} }
/** /*
* *********************************************************************** * ***********************************************************************
* Check Set_client_DH_params_answer type * Check Set_client_DH_params_answer type
*/ */
if ($Set_client_DH_params_answer['_'] == 'dh_gen_ok') if ($Set_client_DH_params_answer['_'] == 'dh_gen_ok') {
{
if ($Set_client_DH_params_answer['new_nonce_hash1'] != $new_nonce_hash1) { if ($Set_client_DH_params_answer['new_nonce_hash1'] != $new_nonce_hash1) {
throw new Exception('Handshake: wrong new_nonce_hash1'); throw new Exception('Handshake: wrong new_nonce_hash1');
} }
@ -435,26 +432,21 @@ class AuthKeyHandler extends AckHandler
$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');
} }
//repeat foreach //repeat foreach
$this->log->log('Retrying Auth'); $this->log->log('Retrying Auth');
} } elseif ($Set_client_DH_params_answer['_'] == 'dh_gen_fail') {
elseif ($Set_client_DH_params_answer['_'] == 'dh_gen_fail')
{
if ($Set_client_DH_params_answer['new_nonce_hash3'] != $new_nonce_hash3) { if ($Set_client_DH_params_answer['new_nonce_hash3'] != $new_nonce_hash3) {
throw new Exception('Handshake: wrong new_nonce_hash_3'); throw new Exception('Handshake: wrong new_nonce_hash_3');
} }
$this->log->log('Auth Failed'); $this->log->log('Auth Failed');
break; break;
} } else {
else {
throw new Exception('Response Error'); throw new Exception('Response Error');
} }
} }