Skip to content

Commit

Permalink
Crypt_RSA 1.2.0b
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.php.net/repository/pear/packages/Crypt_RSA/trunk@232769 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
cweiske committed Mar 27, 2007
1 parent 9e7045a commit c029790
Show file tree
Hide file tree
Showing 14 changed files with 666 additions and 293 deletions.
79 changes: 35 additions & 44 deletions RSA.php
Expand Up @@ -18,7 +18,7 @@
* @author Alexander Valyalkin <valyala@gmail.com>
* @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version 1.1.0
* @version 1.2.0b
* @link http://pear.php.net/package/Crypt_RSA
*/

Expand Down Expand Up @@ -189,7 +189,7 @@ function Crypt_RSA($params = null, $wrapper_name = 'default', $error_handler = '
$this->setErrorHandler($error_handler);
// try to load math wrapper
$obj = &Crypt_RSA_MathLoader::loadWrapper($wrapper_name);
if (PEAR::isError($obj)) {
if ($this->isError($obj)) {
// error during loading of math wrapper
// Crypt_RSA object is partially constructed.
$this->pushError($obj);
Expand Down Expand Up @@ -251,61 +251,57 @@ function &factory($params = null, $wrapper_name = 'default', $error_handler = ''
function setParams($params)
{
if (!is_array($params)) {
$obj = PEAR::raiseError('parameters must be passed to function as associative array', CRYPT_RSA_ERROR_WRONG_PARAMS);
$this->pushError($obj);
$this->pushError('parameters must be passed to function as associative array', CRYPT_RSA_ERROR_WRONG_PARAMS);
return false;
}

if (isset($params['enc_key'])) {
if (Crypt_RSA_Key::isValid($params['enc_key'])) {
$this->_enc_key = $params['enc_key'];
} else {
$obj = PEAR::raiseError('wrong encryption key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
$this->pushError($obj);
}
else {
$this->pushError('wrong encryption key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
return false;
}
}
if (isset($params['dec_key'])) {
if (Crypt_RSA_Key::isValid($params['dec_key'])) {
$this->_dec_key = $params['dec_key'];
} else {
$obj = PEAR::raiseError('wrong decryption key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
$this->pushError($obj);
}
else {
$this->pushError('wrong decryption key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
return false;
}
}
if (isset($params['private_key'])) {
if (Crypt_RSA_Key::isValid($params['private_key'])) {
if ($params['private_key']->getKeyType() != 'private') {
$obj = PEAR::raiseError('private key must have "private" attribute', CRYPT_RSA_ERROR_WRONG_KEY_TYPE);
$this->pushError($obj);
$this->pushError('private key must have "private" attribute', CRYPT_RSA_ERROR_WRONG_KEY_TYPE);
return false;
}
$this->_private_key = $params['private_key'];
} else {
$obj = PEAR::raiseError('wrong private key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
$this->pushError($obj);
}
else {
$this->pushError('wrong private key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
return false;
}
}
if (isset($params['public_key'])) {
if (Crypt_RSA_Key::isValid($params['public_key'])) {
if ($params['public_key']->getKeyType() != 'public') {
$obj = PEAR::raiseError('public key must have "public" attribute', CRYPT_RSA_ERROR_WRONG_KEY_TYPE);
$this->pushError($obj);
$this->pushError('public key must have "public" attribute', CRYPT_RSA_ERROR_WRONG_KEY_TYPE);
return false;
}
$this->_public_key = $params['public_key'];
} else {
$obj = PEAR::raiseError('wrong public key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
$this->pushError($obj);
}
else {
$this->pushError('wrong public key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
return false;
}
}
if (isset($params['hash_func'])) {
if (!function_exists($params['hash_func'])) {
$obj = PEAR::raiseError('cannot find hash function with name [' . $params['hash_func'] . ']', CRYPT_RSA_ERROR_WRONG_HASH_FUNC);
$this->pushError($obj);
$this->pushError('cannot find hash function with name [' . $params['hash_func'] . ']', CRYPT_RSA_ERROR_WRONG_HASH_FUNC);
return false;
}
$this->_hash_func = $params['hash_func'];
Expand Down Expand Up @@ -348,9 +344,9 @@ function encryptBinary($plain_data, $key = null)
if (is_null($key)) {
// use current encryption key
$key = $this->_enc_key;
} elseif (!Crypt_RSA_Key::isValid($key)) {
$obj = PEAR::raiseError('invalid encryption key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
$this->pushError($obj);
}
else if (!Crypt_RSA_Key::isValid($key)) {
$this->pushError('invalid encryption key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
return false;
}

Expand Down Expand Up @@ -410,9 +406,9 @@ function decryptBinary($enc_data, $key = null)
if (is_null($key)) {
// use current decryption key
$key = $this->_dec_key;
} elseif (!Crypt_RSA_Key::isValid($key)) {
$obj = PEAR::raiseError('invalid decryption key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
$this->pushError($obj);
}
else if (!Crypt_RSA_Key::isValid($key)) {
$this->pushError('invalid decryption key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
return false;
}

Expand All @@ -437,8 +433,7 @@ function decryptBinary($enc_data, $key = null)
// delete tail, containing of \x01
$tail = ord($result{strlen($result) - 1});
if ($tail != 1) {
$obj = PEAR::raiseError("Error tail of decrypted text = {$tail}. Expected 1", CRYPT_RSA_ERROR_WRONG_TAIL);
$this->pushError($obj);
$this->pushError("Error tail of decrypted text = {$tail}. Expected 1", CRYPT_RSA_ERROR_WRONG_TAIL);
return false;
}
return substr($result, 0, -1);
Expand All @@ -461,14 +456,13 @@ function createSign($document, $private_key = null, $hash_func = null)
// check private key
if (is_null($private_key)) {
$private_key = $this->_private_key;
} elseif (!Crypt_RSA_Key::isValid($private_key)) {
$obj = PEAR::raiseError('invalid private key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
$this->pushError($obj);
}
else if (!Crypt_RSA_Key::isValid($private_key)) {
$this->pushError('invalid private key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
return false;
}
if ($private_key->getKeyType() != 'private') {
$obj = PEAR::raiseError('signing key must be private', CRYPT_RSA_ERROR_NEED_PRV_KEY);
$this->pushError($obj);
$this->pushError('signing key must be private', CRYPT_RSA_ERROR_NEED_PRV_KEY);
return false;
}

Expand All @@ -477,8 +471,7 @@ function createSign($document, $private_key = null, $hash_func = null)
$hash_func = $this->_hash_func;
}
if (!function_exists($hash_func)) {
$obj = PEAR::raiseError('cannot find hash function with name [' . $hash_func . ']', CRYPT_RSA_ERROR_WRONG_HASH_FUNC);
$this->pushError($obj);
$this->pushError("cannot find hash function with name [$hash_func]", CRYPT_RSA_ERROR_WRONG_HASH_FUNC);
return false;
}

Expand All @@ -505,14 +498,13 @@ function validateSign($document, $signature, $public_key = null, $hash_func = nu
// check public key
if (is_null($public_key)) {
$public_key = $this->_public_key;
} elseif (!Crypt_RSA_Key::isValid($public_key)) {
$obj = PEAR::raiseError('invalid public key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
$this->pushError($obj);
}
else if (!Crypt_RSA_Key::isValid($public_key)) {
$this->pushError('invalid public key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
return null;
}
if ($public_key->getKeyType() != 'public') {
$obj = PEAR::raiseError('validating key must be public', CRYPT_RSA_ERROR_NEED_PUB_KEY);
$this->pushError($obj);
$this->pushError('validating key must be public', CRYPT_RSA_ERROR_NEED_PUB_KEY);
return null;
}

Expand All @@ -521,8 +513,7 @@ function validateSign($document, $signature, $public_key = null, $hash_func = nu
$hash_func = $this->_hash_func;
}
if (!function_exists($hash_func)) {
$obj = PEAR::raiseError('cannot find hash function with name [' . $hash_func . ']', CRYPT_RSA_ERROR_WRONG_HASH_FUNC);
$this->pushError($obj);
$this->pushError("cannot find hash function with name [$hash_func]", CRYPT_RSA_ERROR_WRONG_HASH_FUNC);
return null;
}

Expand Down
22 changes: 10 additions & 12 deletions RSA/ErrorHandler.php
Expand Up @@ -18,7 +18,7 @@
* @author Alexander Valyalkin <valyala@gmail.com>
* @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version 1.1.0
* @version 1.2.0b
* @link http://pear.php.net/package/Crypt_RSA
*/

Expand Down Expand Up @@ -106,7 +106,7 @@
* - isError() - returns true, if list contains errors, else returns false
* - getErrorList() - returns error list
* - getLastError() - returns last error from error list or false, if list is empty
* - pushError($error) - pushes $error into the error list
* - pushError($errstr) - pushes $errstr into the error list
* - setErrorHandler($new_error_handler) - sets error handler function
* - getErrorHandler() - returns name of error handler function
*
Expand Down Expand Up @@ -140,12 +140,13 @@ class Crypt_RSA_ErrorHandler
/**
* Returns true if list of errors is not empty, else returns false
*
* @return bool true, if list of errors is not empty, else false
* @param object
* @return bool true, if list of errors is not empty or $err is PEAR_Error object, else false
* @access public
*/
function isError()
function isError($err = null)
{
return sizeof($this->_errors) > 0;
return is_null($err) ? (sizeof($this->_errors) > 0) : PEAR::isError($err);
}

/**
Expand Down Expand Up @@ -177,17 +178,14 @@ function getLastError()
/**
* pushes error object $error to the error list
*
* @param object $error error object of PEAR_Error class
* @param string $errstr error string
* @param int $errno error number
* @return bool true on success, false on error
* @access public
*/
function pushError($error)
function pushError($errstr, $errno = 0)
{
if (!PEAR::isError($error)) {
// $error must be a PEAR_Error object
return false;
}
$this->_errors[] = $error;
$this->_errors[] = PEAR::raiseError($errstr, $errno);

if ($this->_error_handler != '') {
// call user defined error handler
Expand Down
10 changes: 4 additions & 6 deletions RSA/Key.php
Expand Up @@ -18,7 +18,7 @@
* @author Alexander Valyalkin <valyala@gmail.com>
* @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version 1.1.0
* @version 1.2.0b
* @link http://pear.php.net/package/Crypt_RSA
*/

Expand Down Expand Up @@ -156,7 +156,7 @@ function Crypt_RSA_Key($modulus, $exp, $key_type, $wrapper_name = 'default', $er
$this->setErrorHandler($error_handler);
// try to load math wrapper $wrapper_name
$obj = &Crypt_RSA_MathLoader::loadWrapper($wrapper_name);
if (PEAR::isError($obj)) {
if ($this->isError($obj)) {
// error during loading of math wrapper
$this->pushError($obj); // push error object into error list
return;
Expand All @@ -167,8 +167,7 @@ function Crypt_RSA_Key($modulus, $exp, $key_type, $wrapper_name = 'default', $er
$this->_exp = $exp;

if (!in_array($key_type, array('private', 'public'))) {
$obj = PEAR::raiseError('invalid key type. It must be private or public', CRYPT_RSA_ERROR_WRONG_KEY_TYPE);
$this->pushError($obj); // push error object into error list
$this->pushError('invalid key type. It must be private or public', CRYPT_RSA_ERROR_WRONG_KEY_TYPE);
return;
}
$this->_key_type = $key_type;
Expand All @@ -178,8 +177,7 @@ function Crypt_RSA_Key($modulus, $exp, $key_type, $wrapper_name = 'default', $er
$exp_num = $this->_math_obj->bin2int($this->_exp);

if ($this->_math_obj->cmpAbs($mod_num, $exp_num) <= 0) {
$obj = PEAR::raiseError('modulus must be greater than exponent', CRYPT_RSA_ERROR_EXP_GE_MOD);
$this->pushError($obj); // push error object into error list
$this->pushError('modulus must be greater than exponent', CRYPT_RSA_ERROR_EXP_GE_MOD);
return;
}
// determine key length
Expand Down

0 comments on commit c029790

Please sign in to comment.