From 20b9abf5896d0bf5928de78153424c4beed9e3e2 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Tue, 7 Dec 2021 22:10:29 -0600 Subject: [PATCH] SSH2: show a more helpful error message when logging in with pubkey --- phpseclib/Net/SSH2.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 0a847d07..362902f3 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -52,6 +52,7 @@ use phpseclib3\Crypt\Hash; use phpseclib3\Crypt\Random; use phpseclib3\Crypt\RC4; use phpseclib3\Crypt\Rijndael; +use phpseclib3\Crypt\Common\PublicKey; use phpseclib3\Crypt\Common\PrivateKey; use phpseclib3\Crypt\RSA; use phpseclib3\Crypt\DSA; @@ -2171,6 +2172,20 @@ class SSH2 return $this->login_helper($username); } + foreach ($args as $arg) { + switch (true) { + case $arg instanceof PublicKey: + throw new \UnexpectedValueException('A PublicKey object was passed to the login method instead of a PrivateKey object'); + case $arg instanceof PrivateKey: + case $arg instanceof Agent: + case is_array($arg): + case is_string($arg): + break; + default: + throw new \UnexpectedValueException('$password needs to either be an instance of \phpseclib3\Crypt\Common\PrivateKey, \System\SSH\Agent, an array or a string'); + } + } + while (count($args)) { if (!$this->auth_methods_to_continue || !$this->smartMFA) { $newargs = $args; @@ -2320,10 +2335,6 @@ class SSH2 } } - if (!is_string($password)) { - throw new \UnexpectedValueException('$password needs to either be an instance of \phpseclib3\Crypt\Common\PrivateKey, \System\SSH\Agent, an array or a string'); - } - $packet = Strings::packSSH2( 'Cs3bs', NET_SSH2_MSG_USERAUTH_REQUEST,