pssht  latest
SSH server library written in PHP
INIT.php
1 <?php
2 
3 /*
4 * This file is part of pssht.
5 *
6 * (c) François Poirotte <clicky@erebot.net>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 
13 
18 {
19  // SSH_MSG_KEXDH_INIT = 30
20  public function handle(
21  $msgType,
22  \fpoirotte\Pssht\Wire\Decoder $decoder,
23  \fpoirotte\Pssht\Transport $transport,
24  array &$context
25  ) {
26  $hostAlgo = null;
27  foreach ($context['kex']['client']->getServerHostKeyAlgos() as $algo) {
28  if (isset($context['serverKeys'][$algo])) {
29  $hostAlgo = $algo;
30  break;
31  }
32  }
33  if ($hostAlgo === null) {
34  throw new \RuntimeException();
35  }
36  $response = $this->createResponse($decoder, $transport, $context, $hostAlgo);
37 
38  $logging = \Plop\Plop::getInstance();
39  $secret = gmp_strval($response->getSharedSecret(), 16);
40  $logging->debug(
41  "Shared secret:\r\n%s",
42  array(
43  wordwrap($secret, 16, ' ', true)
44  )
45  );
46 
47  $logging->debug(
48  'Hash: %s',
49  array(
50  wordwrap(bin2hex($response->getExchangeHash()), 16, ' ', true)
51  )
52  );
53 
54  if (!isset($context['sessionIdentifier'])) {
55  $context['sessionIdentifier'] = $response->getExchangeHash();
56  }
57  $context['DH'] = $response;
58  $transport->writeMessage($response);
59  return true;
60  }
61 
62  protected function createResponse(
63  \fpoirotte\Pssht\Wire\Decoder $decoder,
64  \fpoirotte\Pssht\Transport $transport,
65  array &$context,
66  $hostAlgo
67  ) {
68  $kexAlgo = $context['kexAlgo'];
69  $kexAlgo = new $kexAlgo();
71 
72 /*
73  // @TODO: we ought to check whether the given public key is valid.
74  //
75  // Unfortunately, the current API is broken as getQ() only exists
76  // for ECDH. Also, even though the regular DH has a getE() method,
77  // it returns raw GMP resources/objects which are unusable here.
78  if (!$message->getQ()->isValid()) {
79  throw new \InvalidArgumentException();
80  }
81 */
82 
83  return new \fpoirotte\Pssht\Messages\KEXDH\REPLY(
84  $message,
85  $context['serverKeys'][$hostAlgo],
86  $transport->getEncryptor(),
87  $kexAlgo,
88  $context['kex']['server'],
89  $context['kex']['client'],
90  $context['identity']['server'],
91  $context['identity']['client']
92  );
93  }
94 }
static unserialize(\fpoirotte\Pssht\Wire\Decoder $decoder)
Definition: INIT.php:49
handle($msgType,\fpoirotte\Pssht\Wire\Decoder $decoder,\fpoirotte\Pssht\Transport $transport, array &$context)
Definition: INIT.php:20