pssht  latest
SSH server library written in PHP
InitialState.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 
12 namespace fpoirotte\Pssht\Handlers;
13 
18 {
19  // Initial state
20  public function handle(
21  $msgType,
22  \fpoirotte\Pssht\Wire\Decoder $decoder,
23  \fpoirotte\Pssht\Transport $transport,
24  array &$context
25  ) {
26  $ident = $decoder->getBuffer()->get("\r\n");
27  if ($ident === null) {
28  throw new \RuntimeException();
29  }
30  $context['identity']['client'] = (string) substr($ident, 0, -2);
31  if (strncmp($ident, 'SSH-2.0-', 8) !== 0) {
32  throw new \fpoirotte\Pssht\Messages\DISCONNECT();
33  }
34 
35  $context['rekeying'] = 'server';
36  return $this->handleKEXINIT($transport, $context);
37  }
38 
39  public function handleKEXINIT(
40  \fpoirotte\Pssht\Transport $transport,
41  array &$context
42  ) {
44 
45  // Cookie
46  $random = new \fpoirotte\Pssht\Random\OpenSSL();
47 
48  // KEX
49  $kexAlgos = $algos->getAlgorithms('KEX');
50  if (!count($kexAlgos)) {
51  throw new \RuntimeException();
52  }
53 
54  // Server key
55  $serverHostKeyAlgos = array_intersect(
56  $algos->getAlgorithms('Key'),
57  array_keys($context['serverKeys'])
58  );
59  if (!count($serverHostKeyAlgos)) {
60  throw new \RuntimeException();
61  }
62 
63  // Encryption
64  $encAlgosC2S = array_diff(
65  $algos->getAlgorithms('Encryption'),
66  array('none')
67  );
68  $encAlgosS2C = $encAlgosC2S;
69  if (!count($encAlgosC2S)) {
70  throw new \RuntimeException();
71  }
72 
73  // MAC
74  $macAlgosC2S = array_diff($algos->getAlgorithms('MAC'), array('none'));
75  $macAlgosS2C = $macAlgosC2S;
76  if (!count($macAlgosC2S)) {
77  throw new \RuntimeException();
78  }
79 
80  // Compression
81  $compAlgosC2S = $algos->getAlgorithms('Compression');
82  $compAlgosS2C = $compAlgosC2S;
83  if (!count($compAlgosC2S)) {
84  throw new \RuntimeException();
85  }
86 
87  $kex = new \fpoirotte\Pssht\Messages\KEXINIT(
88  $random,
89  $kexAlgos,
90  $serverHostKeyAlgos,
91  $encAlgosC2S,
92  $encAlgosS2C,
93  $macAlgosC2S,
94  $macAlgosS2C,
95  $compAlgosC2S,
96  $compAlgosS2C
97  );
98  $context['kex']['server'] = $kex;
99  $transport->writeMessage($kex);
100 
101  return true;
102  }
103 }
handle($msgType,\fpoirotte\Pssht\Wire\Decoder $decoder,\fpoirotte\Pssht\Transport $transport, array &$context)