Figured out fingerprint generation.

This commit is contained in:
danogentili 2016-08-07 21:11:46 +02:00
parent 7e4656d5fd
commit 09951563c1
3 changed files with 16 additions and 5 deletions

3
TL.php
View File

@ -131,7 +131,7 @@ class TL
case 'bytes':
$l = strlen($value);
$concat = '';
if ($l < 254) {
if ($l <= 253) {
$concat .= $this->struct->pack('<b', $l);
$concat .= $value;
$concat .= pack('@'.posmod((-$l - 1), 4));
@ -141,6 +141,7 @@ class TL
$concat .= $value;
$concat .= pack('@'.posmod(-$l, 4));
}
return $concat;
break;
default:
break;

View File

@ -235,20 +235,27 @@ class Session
public function create_auth_key()
{
// Load the RSA key
$f = file_get_contents(__DIR__.'/rsa.pub');
$key = new \phpseclib\Crypt\RSA();
$key->load($f);
// Make pq request
$nonce = \phpseclib\Crypt\Random::string(16);
pyjslib_printnl('Requesting pq');
$ResPQ = $this->method_call('req_pq', ['nonce' => $nonce]);
$server_nonce = $ResPQ['server_nonce'];
if ($ResPQ['nonce'] !== $nonce) {
throw new Exception('Handshake: wrong nonce');
}
$server_nonce = $ResPQ['server_nonce'];
$public_key_fingerprint = (int) $ResPQ['server_public_key_fingerprints'][0];
$pq_bytes = $ResPQ['pq'];
var_dump(
(int)$this->struct->unpack("<q", substr(sha1($this->tl->serialize_param('bytes', $key->modulus->toBytes()) . $this->tl->serialize_param('bytes', $key->exponent->toBytes()), true), -8))[0],
$public_key_fingerprint
);
// Compute p and q
$pq = new \phpseclib\Math\BigInteger($pq_bytes, 256);
list($p, $q) = $this->PrimeModule->primefactors($pq);
$p = new \phpseclib\Math\BigInteger($p);
@ -259,7 +266,10 @@ class Session
if (!(($pq->equals($p->multiply($q))) && ($p < $q))) {
throw new Exception("Handshake: couldn't compute p or q.");
}
pyjslib_printnl(sprintf('Factorization %s = %s * %s', $pq, $p, $q));
$p_bytes = $this->struct->pack('>Q', (string) $p);
$q_bytes = $this->struct->pack('>Q', (string) $q);
$new_nonce = \phpseclib\Crypt\Random::string(32);

View File

@ -141,6 +141,9 @@ class Session:
nonce = os.urandom(16)
print("Requesting pq")
f = open(os.path.join(os.path.dirname(__file__), "rsa.pub"))
key = RSA.importKey(f.read())
print(getattr(key.key, 'n') . getattr(key.key, 'e'))
ResPQ = self.method_call('req_pq', nonce=nonce)
server_nonce = ResPQ['server_nonce']
# TODO: selecting RSA public key based on this fingerprint
@ -155,9 +158,6 @@ class Session:
print("Factorization %d = %d * %d" % (pq, p, q))
p_bytes = long_to_bytes(p)
q_bytes = long_to_bytes(q)
f = open(os.path.join(os.path.dirname(__file__), "rsa.pub"))
key = RSA.importKey(f.read())
print(key.exportKey('OpenSSH'))
new_nonce = os.urandom(32)
data = TL.serialize_obj('p_q_inner_data',