Skip to content

Commit

Permalink
Merge branch '2.0' into 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
terrafrost committed Nov 22, 2023
2 parents eafbc8a + 8489922 commit 7cc1814
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions phpseclib/Net/SSH2.php
Expand Up @@ -343,6 +343,14 @@ class SSH2
*/
private $languages_client_to_server = false;

/**
* Server Signature Algorithms
*
* @link https://www.rfc-editor.org/rfc/rfc8308.html#section-3.1
* @var array|false
*/
private $server_sig_algs = false;

/**
* Preferred Algorithms
*
Expand Down Expand Up @@ -1122,6 +1130,7 @@ public function __construct($host, $port = 22, $timeout = 10)
4 => 'NET_SSH2_MSG_DEBUG',
5 => 'NET_SSH2_MSG_SERVICE_REQUEST',
6 => 'NET_SSH2_MSG_SERVICE_ACCEPT',
7 => 'NET_SSH2_MSG_EXT_INFO', // RFC 8308
20 => 'NET_SSH2_MSG_KEXINIT',
21 => 'NET_SSH2_MSG_NEWKEYS',
30 => 'NET_SSH2_MSG_KEXDH_INIT',
Expand Down Expand Up @@ -1535,6 +1544,8 @@ private function key_exchange($kexinit_payload_server = false)
$preferred['client_to_server']['comp'] :
SSH2::getSupportedCompressionAlgorithms();

$kex_algorithms = array_merge($kex_algorithms, array('ext-info-c'));

// some SSH servers have buggy implementations of some of the above algorithms
switch (true) {
case $this->server_identifier == 'SSH-2.0-SSHD':
Expand Down Expand Up @@ -2332,7 +2343,23 @@ private function login_helper($username, $password = null)
throw $e;
}

list($type, $service) = Strings::unpackSSH2('Cs', $response);
list($type) = Strings::unpackSSH2('C', $response);

if ($type == NET_SSH2_MSG_EXT_INFO) {
list($nr_extensions) = Strings::unpackSSH2('N', $response);
for ($i = 0; $i < $nr_extensions; $i++) {
list($extension_name, $extension_value) = Strings::unpackSSH2('ss', $response);
if ($extension_name == 'server-sig-algs') {
$this->server_sig_algs = explode(',', $extension_value);
}
}

$response = $this->get_binary_packet();
list($type) = Strings::unpackSSH2('C', $response);
}

list($service) = Strings::unpackSSH2('s', $response);

if ($type != NET_SSH2_MSG_SERVICE_ACCEPT || $service != 'ssh-userauth') {
$this->disconnect_helper(NET_SSH2_DISCONNECT_PROTOCOL_ERROR);
throw new \UnexpectedValueException('Expected SSH_MSG_SERVICE_ACCEPT');
Expand Down Expand Up @@ -2607,7 +2634,9 @@ private function privatekey_login($username, PrivateKey $privatekey)
if ($publickey instanceof RSA) {
$privatekey = $privatekey->withPadding(RSA::SIGNATURE_PKCS1);
$algos = ['rsa-sha2-256', 'rsa-sha2-512', 'ssh-rsa'];
if (isset($this->preferred['hostkey'])) {
if ($this->server_sig_algs) {
$algos = array_intersect($this->server_sig_algs, $algos);
} elseif (isset($this->preferred['hostkey'])) {
$algos = array_intersect($this->preferred['hostkey'], $algos);
}
$algo = self::array_intersect_first($algos, $this->supported_private_key_algorithms);
Expand Down

0 comments on commit 7cc1814

Please sign in to comment.