Permalink
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...
terrafrost committed Nov 29, 2012
1 parent 8622537 commit 3caaa91160d7e59569362928241f685d967d3722
View
@@ -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
@@ -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);
+ }
+ }
}
@@ -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
@@ -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);
+ }
+ }
}
Oops, something went wrong.

0 comments on commit 3caaa91

Please sign in to comment.