Skip to content
Browse files

Replace user_error() with new _handle_error() function

To use exceptions do define('PHPSECLIB_USE_EXCEPTIONS', true).

To have the exceptions thrown by phpseclib be of a certain class define PHPSECLIB_EXCEPTION_CLASS.
  • Loading branch information...
1 parent 8622537 commit 3caaa91160d7e59569362928241f685d967d3722 @terrafrost terrafrost committed Nov 28, 2012
View
20 phpseclib/Crypt/DES.php
@@ -937,7 +937,7 @@ function _pad($text)
if (($length & 7) == 0) {
return $text;
} else {
- user_error("The plaintext's length ($length) is not a multiple of the block size (8)", E_USER_NOTICE);
+ $this->_handle_error("The plaintext's length ($length) is not a multiple of the block size (8)");
$this->padding = true;
}
}
@@ -1289,6 +1289,24 @@ function _string_shift(&$string, $index = 1)
$string = substr($string, $index);
return $substr;
}
+
+ /**
+ * Error Handler
+ *
+ * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined.
+ * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions.
+ *
+ * @param String $string
+ * @access private
+ */
+ function _handle_error($err_msg) {
+ if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) {
+ $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception';
+ throw(new $class($err_msg));
+ } else {
+ user_error($err_msg);
+ }
+ }
}
// vim: ts=4:sw=4:et:
View
68 phpseclib/Crypt/RSA.php
@@ -1746,7 +1746,7 @@ function _i2osp($x, $xLen)
{
$x = $x->toBytes();
if (strlen($x) > $xLen) {
- user_error('Integer too large', E_USER_NOTICE);
+ $this->_handle_error('Integer too large');
return false;
}
return str_pad($x, $xLen, chr(0), STR_PAD_LEFT);
@@ -1908,7 +1908,7 @@ function _equals($x, $y)
function _rsaep($m)
{
if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) {
- user_error('Message representative out of range', E_USER_NOTICE);
+ $this->_handle_error('Message representative out of range');
return false;
}
return $this->_exponentiate($m);
@@ -1926,7 +1926,7 @@ function _rsaep($m)
function _rsadp($c)
{
if ($c->compare($this->zero) < 0 || $c->compare($this->modulus) > 0) {
- user_error('Ciphertext representative out of range', E_USER_NOTICE);
+ $this->_handle_error('Ciphertext representative out of range');
return false;
}
return $this->_exponentiate($c);
@@ -1944,7 +1944,7 @@ function _rsadp($c)
function _rsasp1($m)
{
if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) {
- user_error('Message representative out of range', E_USER_NOTICE);
+ $this->_handle_error('Message representative out of range');
return false;
}
return $this->_exponentiate($m);
@@ -1962,7 +1962,7 @@ function _rsasp1($m)
function _rsavp1($s)
{
if ($s->compare($this->zero) < 0 || $s->compare($this->modulus) > 0) {
- user_error('Signature representative out of range', E_USER_NOTICE);
+ $this->_handle_error('Signature representative out of range');
return false;
}
return $this->_exponentiate($s);
@@ -2013,7 +2013,7 @@ function _rsaes_oaep_encrypt($m, $l = '')
// be output.
if ($mLen > $this->k - 2 * $this->hLen - 2) {
- user_error('Message too long', E_USER_NOTICE);
+ $this->_handle_error('Message too long');
return false;
}
@@ -2074,7 +2074,7 @@ function _rsaes_oaep_decrypt($c, $l = '')
// be output.
if (strlen($c) != $this->k || $this->k < 2 * $this->hLen + 2) {
- user_error('Decryption error', E_USER_NOTICE);
+ $this->_handle_error('Decryption error');
return false;
}
@@ -2083,7 +2083,7 @@ function _rsaes_oaep_decrypt($c, $l = '')
$c = $this->_os2ip($c);
$m = $this->_rsadp($c);
if ($m === false) {
- user_error('Decryption error', E_USER_NOTICE);
+ $this->_handle_error('Decryption error');
return false;
}
$em = $this->_i2osp($m, $this->k);
@@ -2101,12 +2101,12 @@ function _rsaes_oaep_decrypt($c, $l = '')
$lHash2 = substr($db, 0, $this->hLen);
$m = substr($db, $this->hLen);
if ($lHash != $lHash2) {
- user_error('Decryption error', E_USER_NOTICE);
+ $this->_handle_error('Decryption error');
return false;
}
$m = ltrim($m, chr(0));
if (ord($m[0]) != 1) {
- user_error('Decryption error', E_USER_NOTICE);
+ $this->_handle_error('Decryption error');
return false;
}
@@ -2131,7 +2131,7 @@ function _rsaes_pkcs1_v1_5_encrypt($m)
// Length checking
if ($mLen > $this->k - 11) {
- user_error('Message too long', E_USER_NOTICE);
+ $this->_handle_error('Message too long');
return false;
}
@@ -2175,7 +2175,7 @@ function _rsaes_pkcs1_v1_5_decrypt($c)
// Length checking
if (strlen($c) != $this->k) { // or if k < 11
- user_error('Decryption error', E_USER_NOTICE);
+ $this->_handle_error('Decryption error');
return false;
}
@@ -2185,23 +2185,23 @@ function _rsaes_pkcs1_v1_5_decrypt($c)
$m = $this->_rsadp($c);
if ($m === false) {
- user_error('Decryption error', E_USER_NOTICE);
+ $this->_handle_error('Decryption error');
return false;
}
$em = $this->_i2osp($m, $this->k);
// EME-PKCS1-v1_5 decoding
if (ord($em[0]) != 0 || ord($em[1]) > 2) {
- user_error('Decryption error', E_USER_NOTICE);
+ $this->_handle_error('Decryption error');
return false;
}
$ps = substr($em, 2, strpos($em, chr(0), 2) - 2);
$m = substr($em, strlen($ps) + 3);
if (strlen($ps) < 8) {
- user_error('Decryption error', E_USER_NOTICE);
+ $this->_handle_error('Decryption error');
return false;
}
@@ -2229,7 +2229,7 @@ function _emsa_pss_encode($m, $emBits)
$mHash = $this->hash->hash($m);
if ($emLen < $this->hLen + $sLen + 2) {
- user_error('Encoding error', E_USER_NOTICE);
+ $this->_handle_error('Encoding error');
return false;
}
@@ -2334,7 +2334,7 @@ function _rsassa_pss_verify($m, $s)
// Length checking
if (strlen($s) != $this->k) {
- user_error('Invalid signature', E_USER_NOTICE);
+ $this->_handle_error('Invalid signature');
return false;
}
@@ -2345,12 +2345,12 @@ function _rsassa_pss_verify($m, $s)
$s2 = $this->_os2ip($s);
$m2 = $this->_rsavp1($s2);
if ($m2 === false) {
- user_error('Invalid signature', E_USER_NOTICE);
+ $this->_handle_error('Invalid signature');
return false;
}
$em = $this->_i2osp($m2, $modBits >> 3);
if ($em === false) {
- user_error('Invalid signature', E_USER_NOTICE);
+ $this->_handle_error('Invalid signature');
return false;
}
@@ -2400,7 +2400,7 @@ function _emsa_pkcs1_v1_5_encode($m, $emLen)
$tLen = strlen($t);
if ($emLen < $tLen + 11) {
- user_error('Intended encoded message length too short', E_USER_NOTICE);
+ $this->_handle_error('Intended encoded message length too short');
return false;
}
@@ -2426,7 +2426,7 @@ function _rsassa_pkcs1_v1_5_sign($m)
$em = $this->_emsa_pkcs1_v1_5_encode($m, $this->k);
if ($em === false) {
- user_error('RSA modulus too short', E_USER_NOTICE);
+ $this->_handle_error('RSA modulus too short');
return false;
}
@@ -2455,7 +2455,7 @@ function _rsassa_pkcs1_v1_5_verify($m, $s)
// Length checking
if (strlen($s) != $this->k) {
- user_error('Invalid signature', E_USER_NOTICE);
+ $this->_handle_error('Invalid signature');
return false;
}
@@ -2464,20 +2464,20 @@ function _rsassa_pkcs1_v1_5_verify($m, $s)
$s = $this->_os2ip($s);
$m2 = $this->_rsavp1($s);
if ($m2 === false) {
- user_error('Invalid signature', E_USER_NOTICE);
+ $this->_handle_error('Invalid signature');
return false;
}
$em = $this->_i2osp($m2, $this->k);
if ($em === false) {
- user_error('Invalid signature', E_USER_NOTICE);
+ $this->_handle_error('Invalid signature');
return false;
}
// EMSA-PKCS1-v1_5 encoding
$em2 = $this->_emsa_pkcs1_v1_5_encode($m, $this->k);
if ($em2 === false) {
- user_error('RSA modulus too short', E_USER_NOTICE);
+ $this->_handle_error('RSA modulus too short');
return false;
}
@@ -2639,4 +2639,22 @@ function verify($message, $signature)
return $this->_rsassa_pss_verify($message, $signature);
}
}
+
+ /**
+ * Error Handler
+ *
+ * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined.
+ * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions.
+ *
+ * @param String $string
+ * @access private
+ */
+ function _handle_error($err_msg) {
+ if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) {
+ $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception';
+ throw(new $class($err_msg));
+ } else {
+ $this->_handle_error($err_msg);
+ }
+ }
}
View
26 phpseclib/Crypt/Rijndael.php
@@ -582,7 +582,7 @@ function setKeyLength($length)
*
* Depending on what $method is set to, setPassword()'s (optional) parameters are as follows:
* {@link http://en.wikipedia.org/wiki/PBKDF2 pbkdf2}:
- * $hash, $salt, $count
+ * $hash, $salt, $method
* Set $dkLen by calling setKeyLength()
*
* @param String $password
@@ -601,7 +601,7 @@ function setPassword($password, $method = 'pbkdf2')
}
// WPA and WPA use the SSID as the salt
if (!isset($salt)) {
- $salt = 'phpseclib/salt';
+ $salt = 'phpseclib';
}
// RFC2898#section-4.2 uses 1,000 iterations by default
// WPA and WPA2 use 4,096.
@@ -1365,7 +1365,7 @@ function _pad($text)
if ($length % $this->block_size == 0) {
return $text;
} else {
- user_error("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size})", E_USER_NOTICE);
+ $this->_handle_error("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size})");
$this->padding = true;
}
}
@@ -1472,7 +1472,25 @@ function _string_shift(&$string, $index = 1)
$string = substr($string, $index);
return $substr;
}
+
+ /**
+ * Error Handler
+ *
+ * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined.
+ * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions.
+ *
+ * @param String $string
+ * @access private
+ */
+ function _handle_error($err_msg) {
+ if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) {
+ $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception';
+ throw(new $class($err_msg));
+ } else {
+ user_error($err_msg);
+ }
+ }
}
// vim: ts=4:sw=4:et:
-// vim6: fdl=1:
+// vim6: fdl=1:
View
20 phpseclib/File/ANSI.php
@@ -409,7 +409,7 @@ function appendString($source)
case 47: $back = 'white'; break;
default:
- user_error('Unsupported attribute: ' . $mod);
+ $this->_handle_error('Unsupported attribute: ' . $mod);
$this->ansi = '';
break 2;
}
@@ -537,4 +537,22 @@ function getHistory()
return '<pre style="color: white; background: black" width="' . ($this->max_x + 1) . '">' . $scrollback . '</pre>';
}
+
+ /**
+ * Error Handler
+ *
+ * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined.
+ * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions.
+ *
+ * @param String $string
+ * @access private
+ */
+ function _handle_error($err_msg) {
+ if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) {
+ $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception';
+ throw(new $class($err_msg));
+ } else {
+ $this->_handle_error($err_msg);
+ }
+ }
}
View
24 phpseclib/File/ASN1.php
@@ -955,7 +955,7 @@ function _encode_der($source, $mapping, $idx = NULL)
case FILE_ASN1_TYPE_OBJECT_IDENTIFIER:
$oid = preg_match('#(?:\d+\.)+#', $source) ? $source : array_search($source, $this->oids);
if ($oid === false) {
- user_error('Invalid OID', E_USER_NOTICE);
+ $this->_handle_error('Invalid OID');
return false;
}
$value = '';
@@ -1008,7 +1008,7 @@ function _encode_der($source, $mapping, $idx = NULL)
$filters = $filters[$part];
}
if ($filters === false) {
- user_error('No filters defined for ' . implode('/', $loc), E_USER_NOTICE);
+ $this->_handle_error('No filters defined for ' . implode('/', $loc));
return false;
}
return $this->_encode_der($source, $filters + $mapping);
@@ -1032,7 +1032,7 @@ function _encode_der($source, $mapping, $idx = NULL)
$value = $source ? "\xFF" : "\x00";
break;
default:
- user_error('Mapping provides no type definition for ' . implode('/', $this->location), E_USER_NOTICE);
+ $this->_handle_error('Mapping provides no type definition for ' . implode('/', $this->location));
return false;
}
@@ -1270,4 +1270,22 @@ function convert($in, $from = FILE_ASN1_TYPE_UTF8_STRING, $to = FILE_ASN1_TYPE_U
}
return $out;
}
+
+ /**
+ * Error Handler
+ *
+ * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined.
+ * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions.
+ *
+ * @param String $string
+ * @access private
+ */
+ function _handle_error($err_msg) {
+ if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) {
+ $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception';
+ throw(new $class($err_msg));
+ } else {
+ $this->_handle_error($err_msg);
+ }
+ }
}
View
20 phpseclib/File/X509.php
@@ -1569,7 +1569,7 @@ function _mapOutExtensions(&$root, $path, $asn1)
$map = $this->_getMapping($id);
if (is_bool($map)) {
if (!$map) {
- user_error($id . ' is not a currently supported extension', E_USER_NOTICE);
+ $this->_handle_error($id . ' is not a currently supported extension');
unset($extensions[$i]);
}
} else {
@@ -3839,4 +3839,22 @@ function setRevokedCertificateExtension($serial, $id, $value, $critical = false,
return false;
}
+
+ /**
+ * Error Handler
+ *
+ * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined.
+ * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions.
+ *
+ * @param String $string
+ * @access private
+ */
+ function _handle_error($err_msg) {
+ if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) {
+ $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception';
+ throw(new $class($err_msg));
+ } else {
+ $this->_handle_error($err_msg);
+ }
+ }
}
View
42 phpseclib/Net/SFTP.php
@@ -399,7 +399,7 @@ function login($username, $password = '')
$response = $this->_get_sftp_packet();
if ($this->packet_type != NET_SFTP_VERSION) {
- user_error('Expected SSH_FXP_VERSION', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_VERSION');
return false;
}
@@ -588,7 +588,7 @@ function _realpath($dir, $check_dir = true)
$this->_logError($response);
return false;
default:
- user_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS');
return false;
}
@@ -645,7 +645,7 @@ function chdir($dir)
$this->_logError($response);
return false;
default:
- user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
return false;
}
@@ -655,7 +655,7 @@ function chdir($dir)
$response = $this->_get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_STATUS');
return false;
}
@@ -731,7 +731,7 @@ function _list($dir, $raw = true, $realpath = true)
$this->_logError($response);
return false;
default:
- user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
return false;
}
@@ -780,7 +780,7 @@ function _list($dir, $raw = true, $realpath = true)
}
break 2;
default:
- user_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS');
return false;
}
}
@@ -793,7 +793,7 @@ function _list($dir, $raw = true, $realpath = true)
// -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.1.3
$response = $this->_get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_STATUS');
return false;
}
@@ -996,7 +996,7 @@ function _stat($filename, $type)
return false;
}
- user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS');
return false;
}
@@ -1089,7 +1089,7 @@ function chmod($mode, $filename, $recursive = false)
*/
$response = $this->_get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_STATUS');
return false;
}
@@ -1116,7 +1116,7 @@ function chmod($mode, $filename, $recursive = false)
return false;
}
- user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS');
return false;
}
@@ -1249,7 +1249,7 @@ function _mkdir_helper($dir)
$response = $this->_get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_STATUS');
return false;
}
@@ -1288,7 +1288,7 @@ function rmdir($dir)
$response = $this->_get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_STATUS');
return false;
}
@@ -1365,7 +1365,7 @@ function put($remote_file, $data, $mode = NET_SFTP_STRING)
$this->_logError($response);
return false;
default:
- user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
return false;
}
@@ -1374,7 +1374,7 @@ function put($remote_file, $data, $mode = NET_SFTP_STRING)
// http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.3
if ($mode & NET_SFTP_LOCAL_FILE) {
if (!is_file($data)) {
- user_error("$data is not a valid file", E_USER_NOTICE);
+ $this->_handle_error("$data is not a valid file");
return false;
}
$fp = @fopen($data, 'rb');
@@ -1425,7 +1425,7 @@ function put($remote_file, $data, $mode = NET_SFTP_STRING)
$response = $this->_get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_STATUS');
return false;
}
@@ -1453,7 +1453,7 @@ function _read_put_responses($i)
while ($i--) {
$response = $this->_get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_STATUS');
return false;
}
@@ -1504,7 +1504,7 @@ function get($remote_file, $local_file = false)
$this->_logError($response);
return false;
default:
- user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
return false;
}
@@ -1542,7 +1542,7 @@ function get($remote_file, $local_file = false)
$this->_logError($response);
break 2;
default:
- user_error('Expected SSH_FXP_DATA or SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_DATA or SSH_FXP_STATUS');
if ($local_file !== false) {
fclose($fp);
}
@@ -1560,7 +1560,7 @@ function get($remote_file, $local_file = false)
$response = $this->_get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_STATUS');
return false;
}
@@ -1604,7 +1604,7 @@ function delete($path, $recursive = true)
$response = $this->_get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_STATUS');
return false;
}
@@ -1723,7 +1723,7 @@ function rename($oldname, $newname)
$response = $this->_get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_FXP_STATUS');
return false;
}
View
68 phpseclib/Net/SSH1.php
@@ -467,7 +467,7 @@ function Net_SSH1($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3D
$this->fsock = @fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$this->fsock) {
- user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"), E_USER_NOTICE);
+ $this->_handle_error(rtrim("Cannot connect to $host. Error $errno. $errstr"));
return;
}
@@ -484,19 +484,19 @@ function Net_SSH1($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3D
}
if (!preg_match('#SSH-([0-9\.]+)-(.+)#', $init_line, $parts)) {
- user_error('Can only connect to SSH servers', E_USER_NOTICE);
+ $this->_handle_error('Can only connect to SSH servers');
return;
}
if ($parts[1][0] != 1) {
- user_error("Cannot connect to SSH $parts[1] servers", E_USER_NOTICE);
+ $this->_handle_error("Cannot connect to SSH $parts[1] servers");
return;
}
fputs($this->fsock, $this->identifier."\r\n");
$response = $this->_get_binary_packet();
if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_PUBLIC_KEY) {
- user_error('Expected SSH_SMSG_PUBLIC_KEY', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_SMSG_PUBLIC_KEY');
return;
}
@@ -584,7 +584,7 @@ function Net_SSH1($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3D
$data = pack('C2a*na*N', NET_SSH1_CMSG_SESSION_KEY, $cipher, $anti_spoofing_cookie, 8 * strlen($double_encrypted_session_key), $double_encrypted_session_key, 0);
if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_SESSION_KEY', E_USER_NOTICE);
+ $this->_handle_error('Error sending SSH_CMSG_SESSION_KEY');
return;
}
@@ -614,7 +614,7 @@ function Net_SSH1($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3D
$response = $this->_get_binary_packet();
if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) {
- user_error('Expected SSH_SMSG_SUCCESS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_SMSG_SUCCESS');
return;
}
@@ -638,7 +638,7 @@ function login($username, $password = '')
$data = pack('CNa*', NET_SSH1_CMSG_USER, strlen($username), $username);
if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_USER', E_USER_NOTICE);
+ $this->_handle_error('Error sending SSH_CMSG_USER');
return false;
}
@@ -648,14 +648,14 @@ function login($username, $password = '')
$this->bitmap |= NET_SSH1_MASK_LOGIN;
return true;
} else if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_FAILURE) {
- user_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE');
return false;
}
$data = pack('CNa*', NET_SSH1_CMSG_AUTH_PASSWORD, strlen($password), $password);
if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_AUTH_PASSWORD', E_USER_NOTICE);
+ $this->_handle_error('Error sending SSH_CMSG_AUTH_PASSWORD');
return false;
}
@@ -673,7 +673,7 @@ function login($username, $password = '')
} else if ($response[NET_SSH1_RESPONSE_TYPE] == NET_SSH1_SMSG_FAILURE) {
return false;
} else {
- user_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE');
return false;
}
}
@@ -701,14 +701,14 @@ function login($username, $password = '')
function exec($cmd, $block = true)
{
if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()', E_USER_NOTICE);
+ $this->_handle_error('Operation disallowed prior to login()');
return false;
}
$data = pack('CNa*', NET_SSH1_CMSG_EXEC_CMD, strlen($cmd), $cmd);
if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_EXEC_CMD', E_USER_NOTICE);
+ $this->_handle_error('Error sending SSH_CMSG_EXEC_CMD');
return false;
}
@@ -753,21 +753,21 @@ function _initShell()
$data = pack('CNa*N4C', NET_SSH1_CMSG_REQUEST_PTY, strlen('vt100'), 'vt100', 24, 80, 0, 0, NET_SSH1_TTY_OP_END);
if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_REQUEST_PTY', E_USER_NOTICE);
+ $this->_handle_error('Error sending SSH_CMSG_REQUEST_PTY');
return false;
}
$response = $this->_get_binary_packet();
if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) {
- user_error('Expected SSH_SMSG_SUCCESS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_SMSG_SUCCESS');
return false;
}
$data = pack('C', NET_SSH1_CMSG_EXEC_SHELL);
if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_EXEC_SHELL', E_USER_NOTICE);
+ $this->_handle_error('Error sending SSH_CMSG_EXEC_SHELL');
return false;
}
@@ -806,12 +806,12 @@ function write($cmd)
function read($expect, $mode = NET_SSH1_READ_SIMPLE)
{
if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()', E_USER_NOTICE);
+ $this->_handle_error('Operation disallowed prior to login()');
return false;
}
if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) {
- user_error('Unable to initiate an interactive shell session', E_USER_NOTICE);
+ $this->_handle_error('Unable to initiate an interactive shell session');
return false;
}
@@ -841,19 +841,19 @@ function read($expect, $mode = NET_SSH1_READ_SIMPLE)
function interactiveWrite($cmd)
{
if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()', E_USER_NOTICE);
+ $this->_handle_error('Operation disallowed prior to login()');
return false;
}
if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) {
- user_error('Unable to initiate an interactive shell session', E_USER_NOTICE);
+ $this->_handle_error('Unable to initiate an interactive shell session');
return false;
}
$data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($cmd), $cmd);
if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_STDIN', E_USER_NOTICE);
+ $this->_handle_error('Error sending SSH_CMSG_STDIN');
return false;
}
@@ -876,12 +876,12 @@ function interactiveWrite($cmd)
function interactiveRead()
{
if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()', E_USER_NOTICE);
+ $this->_handle_error('Operation disallowed prior to login()');
return false;
}
if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) {
- user_error('Unable to initiate an interactive shell session', E_USER_NOTICE);
+ $this->_handle_error('Unable to initiate an interactive shell session');
return false;
}
@@ -960,7 +960,7 @@ function _disconnect($msg = 'Client Quit')
function _get_binary_packet()
{
if (feof($this->fsock)) {
- //user_error('connection closed prematurely', E_USER_NOTICE);
+ //$this->_handle_error('connection closed prematurely');
return false;
}
@@ -984,7 +984,7 @@ function _get_binary_packet()
$temp = unpack('Ncrc', substr($raw, -4));
//if ( $temp['crc'] != $this->_crc($padding . $type . $data) ) {
- // user_error('Bad CRC in packet from server', E_USER_NOTICE);
+ // $this->_handle_error('Bad CRC in packet from server');
// return false;
//}
@@ -1017,7 +1017,7 @@ function _get_binary_packet()
*/
function _send_binary_packet($data) {
if (feof($this->fsock)) {
- //user_error('connection closed prematurely', E_USER_NOTICE);
+ //$this->_handle_error('connection closed prematurely');
return false;
}
@@ -1418,4 +1418,22 @@ function getServerIdentification()
{
return rtrim($this->server_identification);
}
+
+ /**
+ * Error Handler
+ *
+ * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined.
+ * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions.
+ *
+ * @param String $string
+ * @access private
+ */
+ function _handle_error($err_msg) {
+ if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) {
+ $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception';
+ throw(new $class($err_msg));
+ } else {
+ $this->_handle_error($err_msg);
+ }
+ }
}
View
116 phpseclib/Net/SSH2.php
@@ -793,15 +793,15 @@ function Net_SSH2($host, $port = 22, $timeout = 10)
$start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
$this->fsock = @fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$this->fsock) {
- user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"), E_USER_NOTICE);
+ $this->_handle_error(rtrim("Cannot connect to $host. Error $errno. $errstr"));
return;
}
$elapsed = strtok(microtime(), ' ') + strtok('') - $start;
$timeout-= $elapsed;
if ($timeout <= 0) {
- user_error(rtrim("Cannot connect to $host. Timeout error"), E_USER_NOTICE);
+ $this->_handle_error(rtrim("Cannot connect to $host. Timeout error"));
return;
}
@@ -814,7 +814,7 @@ function Net_SSH2($host, $port = 22, $timeout = 10)
// on windows this returns a "Warning: Invalid CRT parameters detected" error
// the !count() is done as a workaround for <https://bugs.php.net/42682>
if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) {
- user_error(rtrim("Cannot connect to $host. Banner timeout"), E_USER_NOTICE);
+ $this->_handle_error(rtrim("Cannot connect to $host. Banner timeout"));
return;
}
@@ -836,7 +836,7 @@ function Net_SSH2($host, $port = 22, $timeout = 10)
}
if (feof($this->fsock)) {
- user_error('Connection closed by server', E_USER_NOTICE);
+ $this->_handle_error('Connection closed by server');
return false;
}
@@ -870,20 +870,20 @@ function Net_SSH2($host, $port = 22, $timeout = 10)
}
if ($matches[1] != '1.99' && $matches[1] != '2.0') {
- user_error("Cannot connect to SSH $matches[1] servers", E_USER_NOTICE);
+ $this->_handle_error("Cannot connect to SSH $matches[1] servers");
return;
}
fputs($this->fsock, $this->identifier . "\r\n");
$response = $this->_get_binary_packet();
if ($response === false) {
- user_error('Connection closed by server', E_USER_NOTICE);
+ $this->_handle_error('Connection closed by server');
return;
}
if (ord($response[0]) != NET_SSH2_MSG_KEXINIT) {
- user_error('Expected SSH_MSG_KEXINIT', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_MSG_KEXINIT');
return;
}
@@ -1028,7 +1028,7 @@ function _key_exchange($kexinit_payload_server)
// we need to decide upon the symmetric encryption algorithms before we do the diffie-hellman key exchange
for ($i = 0; $i < count($encryption_algorithms) && !in_array($encryption_algorithms[$i], $this->encryption_algorithms_server_to_client); $i++);
if ($i == count($encryption_algorithms)) {
- user_error('No compatible server to client encryption algorithms found', E_USER_NOTICE);
+ $this->_handle_error('No compatible server to client encryption algorithms found');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
@@ -1065,7 +1065,7 @@ function _key_exchange($kexinit_payload_server)
for ($i = 0; $i < count($encryption_algorithms) && !in_array($encryption_algorithms[$i], $this->encryption_algorithms_client_to_server); $i++);
if ($i == count($encryption_algorithms)) {
- user_error('No compatible client to server encryption algorithms found', E_USER_NOTICE);
+ $this->_handle_error('No compatible client to server encryption algorithms found');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
@@ -1103,7 +1103,7 @@ function _key_exchange($kexinit_payload_server)
// through diffie-hellman key exchange a symmetric key is obtained
for ($i = 0; $i < count($kex_algorithms) && !in_array($kex_algorithms[$i], $this->kex_algorithms); $i++);
if ($i == count($kex_algorithms)) {
- user_error('No compatible key exchange algorithms found', E_USER_NOTICE);
+ $this->_handle_error('No compatible key exchange algorithms found');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
@@ -1156,19 +1156,19 @@ function _key_exchange($kexinit_payload_server)
$data = pack('CNa*', NET_SSH2_MSG_KEXDH_INIT, strlen($eBytes), $eBytes);
if (!$this->_send_binary_packet($data)) {
- user_error('Connection closed by server', E_USER_NOTICE);
+ $this->_handle_error('Connection closed by server');
return false;
}
$response = $this->_get_binary_packet();
if ($response === false) {
- user_error('Connection closed by server', E_USER_NOTICE);
+ $this->_handle_error('Connection closed by server');
return false;
}
extract(unpack('Ctype', $this->_string_shift($response, 1)));
if ($type != NET_SSH2_MSG_KEXDH_REPLY) {
- user_error('Expected SSH_MSG_KEXDH_REPLY', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_MSG_KEXDH_REPLY');
return false;
}
@@ -1206,12 +1206,12 @@ function _key_exchange($kexinit_payload_server)
for ($i = 0; $i < count($server_host_key_algorithms) && !in_array($server_host_key_algorithms[$i], $this->server_host_key_algorithms); $i++);
if ($i == count($server_host_key_algorithms)) {
- user_error('No compatible server host key algorithms found', E_USER_NOTICE);
+ $this->_handle_error('No compatible server host key algorithms found');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
if ($public_key_format != $server_host_key_algorithms[$i] || $this->signature_format != $server_host_key_algorithms[$i]) {
- user_error('Sever Host Key Algorithm Mismatch', E_USER_NOTICE);
+ $this->_handle_error('Sever Host Key Algorithm Mismatch');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
@@ -1226,14 +1226,14 @@ function _key_exchange($kexinit_payload_server)
$response = $this->_get_binary_packet();
if ($response === false) {
- user_error('Connection closed by server', E_USER_NOTICE);
+ $this->_handle_error('Connection closed by server');
return false;
}
extract(unpack('Ctype', $this->_string_shift($response, 1)));
if ($type != NET_SSH2_MSG_NEWKEYS) {
- user_error('Expected SSH_MSG_NEWKEYS', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_MSG_NEWKEYS');
return false;
}
@@ -1347,7 +1347,7 @@ function _key_exchange($kexinit_payload_server)
for ($i = 0; $i < count($mac_algorithms) && !in_array($mac_algorithms[$i], $this->mac_algorithms_client_to_server); $i++);
if ($i == count($mac_algorithms)) {
- user_error('No compatible client to server message authentication algorithms found', E_USER_NOTICE);
+ $this->_handle_error('No compatible client to server message authentication algorithms found');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
@@ -1372,7 +1372,7 @@ function _key_exchange($kexinit_payload_server)
for ($i = 0; $i < count($mac_algorithms) && !in_array($mac_algorithms[$i], $this->mac_algorithms_server_to_client); $i++);
if ($i == count($mac_algorithms)) {
- user_error('No compatible server to client message authentication algorithms found', E_USER_NOTICE);
+ $this->_handle_error('No compatible server to client message authentication algorithms found');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
@@ -1414,14 +1414,14 @@ function _key_exchange($kexinit_payload_server)
for ($i = 0; $i < count($compression_algorithms) && !in_array($compression_algorithms[$i], $this->compression_algorithms_server_to_client); $i++);
if ($i == count($compression_algorithms)) {
- user_error('No compatible server to client compression algorithms found', E_USER_NOTICE);
+ $this->_handle_error('No compatible server to client compression algorithms found');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
$this->decompress = $compression_algorithms[$i] == 'zlib';
for ($i = 0; $i < count($compression_algorithms) && !in_array($compression_algorithms[$i], $this->compression_algorithms_client_to_server); $i++);
if ($i == count($compression_algorithms)) {
- user_error('No compatible client to server compression algorithms found', E_USER_NOTICE);
+ $this->_handle_error('No compatible client to server compression algorithms found');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
$this->compress = $compression_algorithms[$i] == 'zlib';
@@ -1457,14 +1457,14 @@ function login($username, $password = null)
$response = $this->_get_binary_packet();
if ($response === false) {
- user_error('Connection closed by server', E_USER_NOTICE);
+ $this->_handle_error('Connection closed by server');
return false;
}
extract(unpack('Ctype', $this->_string_shift($response, 1)));
if ($type != NET_SSH2_MSG_SERVICE_ACCEPT) {
- user_error('Expected SSH_MSG_SERVICE_ACCEPT', E_USER_NOTICE);
+ $this->_handle_error('Expected SSH_MSG_SERVICE_ACCEPT');
return false;
}
@@ -1485,7 +1485,7 @@ function login($username, $password = null)
$response = $this->_get_binary_packet();
if ($response === false) {
- user_error('Connection closed by server', E_USER_NOTICE);
+ $this->_handle_error('Connection closed by server');
return false;
}
@@ -1521,7 +1521,7 @@ function login($username, $password = null)
$response = $this->_get_binary_packet();
if ($response === false) {
- user_error('Connection closed by server', E_USER_NOTICE);
+ $this->_handle_error('Connection closed by server');
return false;
}
@@ -1593,7 +1593,7 @@ function _keyboard_interactive_process()
$response = $this->_get_binary_packet();
if ($response === false) {
- user_error('Connection closed by server', E_USER_NOTICE);
+ $this->_handle_error('Connection closed by server');
return false;
}
@@ -1706,7 +1706,7 @@ function _privatekey_login($username, $privatekey)
$response = $this->_get_binary_packet();
if ($response === false) {
- user_error('Connection closed by server', E_USER_NOTICE);
+ $this->_handle_error('Connection closed by server');
return false;
}
@@ -1741,7 +1741,7 @@ function _privatekey_login($username, $privatekey)
$response = $this->_get_binary_packet();
if ($response === false) {
- user_error('Connection closed by server', E_USER_NOTICE);
+ $this->_handle_error('Connection closed by server');
return false;
}
@@ -1894,7 +1894,7 @@ function _initShell()
$response = $this->_get_binary_packet();
if ($response === false) {
- user_error('Connection closed by server', E_USER_NOTICE);
+ $this->_handle_error('Connection closed by server');
return false;
}
@@ -1905,7 +1905,7 @@ function _initShell()
break;
case NET_SSH2_MSG_CHANNEL_FAILURE:
default:
- user_error('Unable to request pseudo-terminal', E_USER_NOTICE);
+ $this->_handle_error('Unable to request pseudo-terminal');
return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
}
@@ -1946,12 +1946,12 @@ function read($expect = '', $mode = NET_SSH2_READ_SIMPLE)
$this->curTimeout = $this->timeout;
if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()', E_USER_NOTICE);
+ $this->_handle_error('Operation disallowed prior to login()');
return false;
}
if (!($this->bitmap & NET_SSH2_MASK_SHELL) && !$this->_initShell()) {
- user_error('Unable to initiate an interactive shell session', E_USER_NOTICE);
+ $this->_handle_error('Unable to initiate an interactive shell session');
return false;
}
@@ -1985,12 +1985,12 @@ function read($expect = '', $mode = NET_SSH2_READ_SIMPLE)
function write($cmd)
{
if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()', E_USER_NOTICE);
+ $this->_handle_error('Operation disallowed prior to login()');
return false;
}
if (!($this->bitmap & NET_SSH2_MASK_SHELL) && !$this->_initShell()) {
- user_error('Unable to initiate an interactive shell session', E_USER_NOTICE);
+ $this->_handle_error('Unable to initiate an interactive shell session');
return false;
}
@@ -2035,7 +2035,7 @@ function __destruct()
function _get_binary_packet()
{
if (!is_resource($this->fsock) || feof($this->fsock)) {
- user_error('Connection closed prematurely', E_USER_NOTICE);
+ $this->_handle_error('Connection closed prematurely');
$this->bitmask = 0;
return false;
}
@@ -2052,7 +2052,7 @@ function _get_binary_packet()
$raw = $this->decrypt->decrypt($raw);
}
if ($raw === false) {
- user_error('Unable to decrypt content', E_USER_NOTICE);
+ $this->_handle_error('Unable to decrypt content');
return false;
}
@@ -2076,7 +2076,7 @@ function _get_binary_packet()
if ($this->hmac_check !== false) {
$hmac = fread($this->fsock, $this->hmac_size);
if ($hmac != $this->hmac_check->hash(pack('NNCa*', $this->get_seq_no, $packet_length, $padding_length, $payload . $padding))) {
- user_error('Invalid HMAC', E_USER_NOTICE);
+ $this->_handle_error('Invalid HMAC');
return false;
}
}
@@ -2243,7 +2243,7 @@ function _get_channel_packet($client_channel, $skip_extended = false)
$response = $this->_get_binary_packet();
if ($response === false) {
- user_error('Connection closed by server', E_USER_NOTICE);
+ $this->_handle_error('Connection closed by server');
return false;
}
@@ -2265,7 +2265,7 @@ function _get_channel_packet($client_channel, $skip_extended = false)
return $client_channel == $channel ? true : $this->_get_channel_packet($client_channel, $skip_extended);
//case NET_SSH2_MSG_CHANNEL_OPEN_FAILURE:
default:
- user_error('Unable to open channel', E_USER_NOTICE);
+ $this->_handle_error('Unable to open channel');
return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
}
break;
@@ -2275,7 +2275,7 @@ function _get_channel_packet($client_channel, $skip_extended = false)
return true;
//case NET_SSH2_MSG_CHANNEL_FAILURE:
default:
- user_error('Unable to request pseudo-terminal', E_USER_NOTICE);
+ $this->_handle_error('Unable to request pseudo-terminal');
return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
}
case NET_SSH2_MSG_CHANNEL_CLOSE:
@@ -2364,7 +2364,7 @@ function _get_channel_packet($client_channel, $skip_extended = false)
case NET_SSH2_MSG_CHANNEL_EOF:
break;
default:
- user_error('Error reading channel data', E_USER_NOTICE);
+ $this->_handle_error('Error reading channel data');
return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
}
}
@@ -2383,7 +2383,7 @@ function _get_channel_packet($client_channel, $skip_extended = false)
function _send_binary_packet($data)
{
if (!is_resource($this->fsock) || feof($this->fsock)) {
- user_error('Connection closed prematurely', E_USER_NOTICE);
+ $this->_handle_error('Connection closed prematurely');
$this->bitmask = 0;
return false;
}
@@ -2894,15 +2894,15 @@ function getServerPublicHostKey()
padding, unsigned, and in network byte order). */
$temp = unpack('Nlength', $this->_string_shift($signature, 4));
if ($temp['length'] != 40) {
- user_error('Invalid signature', E_USER_NOTICE);
+ $this->_handle_error('Invalid signature');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
$r = new Math_BigInteger($this->_string_shift($signature, 20), 256);
$s = new Math_BigInteger($this->_string_shift($signature, 20), 256);
if ($r->compare($q) >= 0 || $s->compare($q) >= 0) {
- user_error('Invalid signature', E_USER_NOTICE);
+ $this->_handle_error('Invalid signature');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
@@ -2922,7 +2922,7 @@ function getServerPublicHostKey()
list(, $v) = $v->divide($q);
if (!$v->equals($r)) {
- user_error('Bad server signature', E_USER_NOTICE);
+ $this->_handle_error('Bad server signature');
return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
}
@@ -2947,7 +2947,7 @@ function getServerPublicHostKey()
$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
$rsa->loadKey(array('e' => $e, 'n' => $n), CRYPT_RSA_PUBLIC_FORMAT_RAW);
if (!$rsa->verify($this->exchange_hash, $signature)) {
- user_error('Bad server signature', E_USER_NOTICE);
+ $this->_handle_error('Bad server signature');
return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
}
*/
@@ -2962,7 +2962,7 @@ function getServerPublicHostKey()
// also, see SSHRSA.c (rsa2_verifysig) in PuTTy's source.
if ($s->compare(new Math_BigInteger()) < 0 || $s->compare($n->subtract(new Math_BigInteger(1))) > 0) {
- user_error('Invalid signature', E_USER_NOTICE);
+ $this->_handle_error('Invalid signature');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
@@ -2973,15 +2973,33 @@ function getServerPublicHostKey()
$h = chr(0x01) . str_repeat(chr(0xFF), $nLength - 3 - strlen($h)) . $h;
if ($s != $h) {
- user_error('Bad server signature', E_USER_NOTICE);
+ $this->_handle_error('Bad server signature');
return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
}
break;
default:
- user_error('Unsupported signature format', E_USER_NOTICE);
+ $this->_handle_error('Unsupported signature format');
return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
}
return $this->signature_format . ' ' . base64_encode($this->server_public_host_key);
}
+
+ /**
+ * Error Handler
+ *
+ * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined.
+ * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions.
+ *
+ * @param String $string
+ * @access private
+ */
+ function _handle_error($err_msg) {
+ if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) {
+ $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception';
+ throw(new $class($err_msg));
+ } else {
+ $this->_handle_error($err_msg);
+ }
+ }
}

0 comments on commit 3caaa91

Please sign in to comment.
Something went wrong with that request. Please try again.