diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 049ef222..34f65fe7 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -2010,14 +2010,14 @@ class RSA * @access private * @param \phpseclib\Math\BigInteger $x * @param Integer $xLen + * @throws \OutOfBoundsException if strlen($x) > $xLen * @return String */ function _i2osp($x, $xLen) { $x = $x->toBytes(); if (strlen($x) > $xLen) { - user_error('Integer too large'); - return false; + throw new \OutOfBoundsException('Integer too large'); } return str_pad($x, $xLen, chr(0), STR_PAD_LEFT); } @@ -2172,13 +2172,13 @@ class RSA * * @access private * @param \phpseclib\Math\BigInteger $m + * @throws \OutOfRangeException if $m < 0 or $m > $this->modulus * @return \phpseclib\Math\BigInteger */ function _rsaep($m) { if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) { - user_error('Message representative out of range'); - return false; + throw new \OutOfRangeException('Message representative out of range'); } return $this->_exponentiate($m); } @@ -2190,13 +2190,13 @@ class RSA * * @access private * @param \phpseclib\Math\BigInteger $c + * @throws \OutOfRangeException if $c < 0 or $c > $this->modulus * @return \phpseclib\Math\BigInteger */ function _rsadp($c) { if ($c->compare($this->zero) < 0 || $c->compare($this->modulus) > 0) { - user_error('Ciphertext representative out of range'); - return false; + throw new \OutOfRangeException('Ciphertext representative out of range'); } return $this->_exponentiate($c); } @@ -2208,13 +2208,13 @@ class RSA * * @access private * @param \phpseclib\Math\BigInteger $m + * @throws \OutOfRangeException if $m < 0 or $m > $this->modulus * @return \phpseclib\Math\BigInteger */ function _rsasp1($m) { if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) { - user_error('Message representative out of range'); - return false; + throw new \OutOfRangeException('Message representative out of range'); } return $this->_exponentiate($m); } @@ -2226,13 +2226,13 @@ class RSA * * @access private * @param \phpseclib\Math\BigInteger $s + * @throws \OutOfRangeException if $s < 0 or $s > $this->modulus * @return \phpseclib\Math\BigInteger */ function _rsavp1($s) { if ($s->compare($this->zero) < 0 || $s->compare($this->modulus) > 0) { - user_error('Signature representative out of range'); - return false; + throw new \OutOfRangeException('Signature representative out of range'); } return $this->_exponentiate($s); } @@ -2270,6 +2270,7 @@ class RSA * @access private * @param String $m * @param String $l + * @throws \OutOfBoundsException if strlen($m) > $this->k - 2 * $this->hLen - 2 * @return String */ function _rsaes_oaep_encrypt($m, $l = '') @@ -2282,8 +2283,7 @@ class RSA // be output. if ($mLen > $this->k - 2 * $this->hLen - 2) { - user_error('Message too long'); - return false; + throw new \OutOfBoundsException('Message too long'); } // EME-OAEP encoding @@ -2333,6 +2333,7 @@ class RSA * @access private * @param String $c * @param String $l + * @throws \RuntimeException on decryption error * @return String */ function _rsaes_oaep_decrypt($c, $l = '') @@ -2343,8 +2344,7 @@ class RSA // be output. if (strlen($c) != $this->k || $this->k < 2 * $this->hLen + 2) { - user_error('Decryption error'); - return false; + throw new \RuntimeException('Decryption error'); } // RSA decryption @@ -2352,8 +2352,7 @@ class RSA $c = $this->_os2ip($c); $m = $this->_rsadp($c); if ($m === false) { - user_error('Decryption error'); - return false; + throw new \RuntimeException('Decryption error'); } $em = $this->_i2osp($m, $this->k); @@ -2370,13 +2369,11 @@ class RSA $lHash2 = substr($db, 0, $this->hLen); $m = substr($db, $this->hLen); if ($lHash != $lHash2) { - user_error('Decryption error'); - return false; + throw new \RuntimeException('Decryption error'); } $m = ltrim($m, chr(0)); if (ord($m[0]) != 1) { - user_error('Decryption error'); - return false; + throw new \RuntimeException('Decryption error'); } // Output the message M @@ -2407,6 +2404,7 @@ class RSA * * @access private * @param String $m + * @throws \OutOfBoundsException if strlen($m) > $this->k - 11 * @return String */ function _rsaes_pkcs1_v1_5_encrypt($m) @@ -2416,8 +2414,7 @@ class RSA // Length checking if ($mLen > $this->k - 11) { - user_error('Message too long'); - return false; + throw new \OutOfBoundsException('Message too long'); } // EME-PKCS1-v1_5 encoding @@ -2466,6 +2463,7 @@ class RSA * * @access private * @param String $c + * @throws \RuntimeException on decryption error * @return String */ function _rsaes_pkcs1_v1_5_decrypt($c) @@ -2473,8 +2471,7 @@ class RSA // Length checking if (strlen($c) != $this->k) { // or if k < 11 - user_error('Decryption error'); - return false; + throw new \RuntimeException('Decryption error'); } // RSA decryption @@ -2483,24 +2480,21 @@ class RSA $m = $this->_rsadp($c); if ($m === false) { - user_error('Decryption error'); - return false; + throw new \RuntimeException('Decryption error'); } $em = $this->_i2osp($m, $this->k); // EME-PKCS1-v1_5 decoding if (ord($em[0]) != 0 || ord($em[1]) > 2) { - user_error('Decryption error'); - return false; + throw new \RuntimeException('Decryption error'); } $ps = substr($em, 2, strpos($em, chr(0), 2) - 2); $m = substr($em, strlen($ps) + 3); if (strlen($ps) < 8) { - user_error('Decryption error'); - return false; + throw new \RuntimeException('Decryption error'); } // Output M @@ -2515,6 +2509,7 @@ class RSA * * @access private * @param String $m + * @throws \RuntimeException on encoding error * @param Integer $emBits */ function _emsa_pss_encode($m, $emBits) @@ -2527,8 +2522,7 @@ class RSA $mHash = $this->hash->hash($m); if ($emLen < $this->hLen + $sLen + 2) { - user_error('Encoding error'); - return false; + throw new \RuntimeException('Encoding error'); } $salt = Random::string($sLen); @@ -2625,6 +2619,7 @@ class RSA * @access private * @param String $m * @param String $s + * @throws \RuntimeException on invalid signature * @return String */ function _rsassa_pss_verify($m, $s) @@ -2632,8 +2627,7 @@ class RSA // Length checking if (strlen($s) != $this->k) { - user_error('Invalid signature'); - return false; + throw new \RuntimeException('Invalid signature'); } // RSA verification @@ -2643,13 +2637,11 @@ class RSA $s2 = $this->_os2ip($s); $m2 = $this->_rsavp1($s2); if ($m2 === false) { - user_error('Invalid signature'); - return false; + throw new \RuntimeException('Invalid signature'); } $em = $this->_i2osp($m2, $modBits >> 3); if ($em === false) { - user_error('Invalid signature'); - return false; + throw new \RuntimeException('Invalid signature'); } // EMSA-PSS verification @@ -2665,6 +2657,7 @@ class RSA * @access private * @param String $m * @param Integer $emLen + * @throws \LengthException if the intended encoded message length is too short * @return String */ function _emsa_pkcs1_v1_5_encode($m, $emLen) @@ -2698,8 +2691,7 @@ class RSA $tLen = strlen($t); if ($emLen < $tLen + 11) { - user_error('Intended encoded message length too short'); - return false; + throw new \LengthException('Intended encoded message length too short'); } $ps = str_repeat(chr(0xFF), $emLen - $tLen - 3); @@ -2716,6 +2708,7 @@ class RSA * * @access private * @param String $m + * @throws \LengthException if the RSA modulus is too short * @return String */ function _rsassa_pkcs1_v1_5_sign($m) @@ -2724,8 +2717,7 @@ class RSA $em = $this->_emsa_pkcs1_v1_5_encode($m, $this->k); if ($em === false) { - user_error('RSA modulus too short'); - return false; + throw new \LengthException('RSA modulus too short'); } // RSA signature @@ -2746,6 +2738,8 @@ class RSA * * @access private * @param String $m + * @throws \RuntimeException if the signature is invalid + * @throws \LengthException if the RSA modulus is too short * @return String */ function _rsassa_pkcs1_v1_5_verify($m, $s) @@ -2753,8 +2747,7 @@ class RSA // Length checking if (strlen($s) != $this->k) { - user_error('Invalid signature'); - return false; + throw new \RuntimeSignature('Invalid signature'); } // RSA verification @@ -2762,21 +2755,18 @@ class RSA $s = $this->_os2ip($s); $m2 = $this->_rsavp1($s); if ($m2 === false) { - user_error('Invalid signature'); - return false; + throw new \RuntimeSignature('Invalid signature'); } $em = $this->_i2osp($m2, $this->k); if ($em === false) { - user_error('Invalid signature'); - return false; + throw new \RuntimeSignature('Invalid signature'); } // EMSA-PKCS1-v1_5 encoding $em2 = $this->_emsa_pkcs1_v1_5_encode($m, $this->k); if ($em2 === false) { - user_error('RSA modulus too short'); - return false; + throw new \LengthException('RSA modulus too short'); } // Compare diff --git a/phpseclib/Exception/DecryptionException.php b/phpseclib/Exception/DecryptionException.php new file mode 100644 index 00000000..edc7963d --- /dev/null +++ b/phpseclib/Exception/DecryptionException.php @@ -0,0 +1,26 @@ + + * @copyright 2015 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link http://phpseclib.sourceforge.net + */ + +namespace phpseclib\Exception; + +/** + * KeyGenerationException + * + * @package DecryptionException + * @author Jim Wigginton + */ +class DecryptionException extends \Exception +{ +}