phpseclib 1.0.2

This commit is contained in:
Roland Gruber 2016-05-10 18:14:55 +02:00
parent 88b7a32187
commit c4075de648
15 changed files with 20634 additions and 20515 deletions

View File

@ -746,10 +746,13 @@ class Crypt_Base
return !defined('OPENSSL_RAW_DATA') ? substr($result, 0, -$this->block_size) : $result; return !defined('OPENSSL_RAW_DATA') ? substr($result, 0, -$this->block_size) : $result;
case CRYPT_MODE_CBC: case CRYPT_MODE_CBC:
$result = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $this->encryptIV); $result = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $this->encryptIV);
if (!defined('OPENSSL_RAW_DATA')) {
$result = substr($result, 0, -$this->block_size);
}
if ($this->continuousBuffer) { if ($this->continuousBuffer) {
$this->encryptIV = substr($result, -$this->block_size); $this->encryptIV = substr($result, -$this->block_size);
} }
return !defined('OPENSSL_RAW_DATA') ? substr($result, 0, -$this->block_size) : $result; return $result;
case CRYPT_MODE_CTR: case CRYPT_MODE_CTR:
return $this->_openssl_ctr_process($plaintext, $this->encryptIV, $this->enbuffer); return $this->_openssl_ctr_process($plaintext, $this->encryptIV, $this->enbuffer);
case CRYPT_MODE_CFB: case CRYPT_MODE_CFB:
@ -1052,10 +1055,13 @@ class Crypt_Base
if (!defined('OPENSSL_RAW_DATA')) { if (!defined('OPENSSL_RAW_DATA')) {
$padding = str_repeat(chr($this->block_size), $this->block_size) ^ substr($ciphertext, -$this->block_size); $padding = str_repeat(chr($this->block_size), $this->block_size) ^ substr($ciphertext, -$this->block_size);
$ciphertext.= substr(openssl_encrypt($padding, $this->cipher_name_openssl_ecb, $this->key, true), 0, $this->block_size); $ciphertext.= substr(openssl_encrypt($padding, $this->cipher_name_openssl_ecb, $this->key, true), 0, $this->block_size);
$offset = 2 * $this->block_size;
} else {
$offset = $this->block_size;
} }
$plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $this->decryptIV); $plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $this->decryptIV);
if ($this->continuousBuffer) { if ($this->continuousBuffer) {
$this->decryptIV = substr($ciphertext, -$this->block_size); $this->decryptIV = substr($ciphertext, -$offset, $this->block_size);
} }
break; break;
case CRYPT_MODE_CTR: case CRYPT_MODE_CTR:

View File

@ -387,7 +387,7 @@ class Crypt_RC2 extends Crypt_Base
/** /**
* Sets the key length. * Sets the key length.
* *
* Valid key lengths are 1 to 1024. * Valid key lengths are 8 to 1024.
* Calling this function after setting the key has no effect until the next * Calling this function after setting the key has no effect until the next
* Crypt_RC2::setKey() call. * Crypt_RC2::setKey() call.
* *
@ -396,9 +396,16 @@ class Crypt_RC2 extends Crypt_Base
*/ */
function setKeyLength($length) function setKeyLength($length)
{ {
if ($length >= 1 && $length <= 1024) { if ($length < 8) {
$this->default_key_length = 8;
} elseif ($length > 1024) {
$this->default_key_length = 128;
} else {
$this->default_key_length = $length; $this->default_key_length = $length;
} }
$this->current_key_length = $this->default_key_length;
parent::setKeyLength($length);
} }
/** /**
@ -415,7 +422,7 @@ class Crypt_RC2 extends Crypt_Base
/** /**
* Sets the key. * Sets the key.
* *
* Keys can be of any length. RC2, itself, uses 1 to 1024 bit keys (eg. * Keys can be of any length. RC2, itself, uses 8 to 1024 bit keys (eg.
* strlen($key) <= 128), however, we only use the first 128 bytes if $key * strlen($key) <= 128), however, we only use the first 128 bytes if $key
* has more then 128 bytes in it, and set $key to a single null byte if * has more then 128 bytes in it, and set $key to a single null byte if
* it is empty. * it is empty.
@ -514,7 +521,7 @@ class Crypt_RC2 extends Crypt_Base
return $result; return $result;
} }
return parent::encrypt($ciphertext); return parent::decrypt($ciphertext);
} }
/** /**

View File

@ -234,7 +234,7 @@ class Crypt_RC4 extends Crypt_Base
if ($length < 8) { if ($length < 8) {
$this->key_length = 1; $this->key_length = 1;
} elseif ($length > 2048) { } elseif ($length > 2048) {
$this->key_length = 248; $this->key_length = 256;
} else { } else {
$this->key_length = $length >> 3; $this->key_length = $length >> 3;
} }

View File

@ -1786,7 +1786,7 @@ class Crypt_RSA
function setPrivateKey($key = false, $type = false) function setPrivateKey($key = false, $type = false)
{ {
if ($key === false && !empty($this->publicExponent)) { if ($key === false && !empty($this->publicExponent)) {
unset($this->publicExponent); $this->publicExponent = false;
return true; return true;
} }
@ -1794,7 +1794,7 @@ class Crypt_RSA
if (!$rsa->loadKey($key, $type)) { if (!$rsa->loadKey($key, $type)) {
return false; return false;
} }
unset($rsa->publicExponent); $rsa->publicExponent = false;
// don't overwrite the old key if the new key is invalid // don't overwrite the old key if the new key is invalid
$this->loadKey($rsa); $this->loadKey($rsa);
@ -1838,7 +1838,7 @@ class Crypt_RSA
* for invalid values. * for invalid values.
* @return mixed * @return mixed
*/ */
public function getPublicKeyFingerprint($algorithm = 'md5') function getPublicKeyFingerprint($algorithm = 'md5')
{ {
if (empty($this->modulus) || empty($this->publicExponent)) { if (empty($this->modulus) || empty($this->publicExponent)) {
return false; return false;

View File

@ -148,13 +148,13 @@ if (!function_exists('crypt_random_string')) {
session_start(); session_start();
$v = $seed = $_SESSION['seed'] = pack('H*', sha1( $v = $seed = $_SESSION['seed'] = pack('H*', sha1(
serialize($_SERVER) . (isset($_SERVER) ? phpseclib_safe_serialize($_SERVER) : '') .
serialize($_POST) . (isset($_POST) ? phpseclib_safe_serialize($_POST) : '') .
serialize($_GET) . (isset($_GET) ? phpseclib_safe_serialize($_GET) : '') .
serialize($_COOKIE) . (isset($_COOKIE) ? phpseclib_safe_serialize($_COOKIE) : '') .
serialize($GLOBALS) . phpseclib_safe_serialize($GLOBALS) .
serialize($_SESSION) . phpseclib_safe_serialize($_SESSION) .
serialize($_OLD_SESSION) phpseclib_safe_serialize($_OLD_SESSION)
)); ));
if (!isset($_SESSION['count'])) { if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0; $_SESSION['count'] = 0;
@ -260,6 +260,41 @@ if (!function_exists('crypt_random_string')) {
} }
} }
if (!function_exists('phpseclib_safe_serialize')) {
/**
* Safely serialize variables
*
* If a class has a private __sleep() method it'll give a fatal error on PHP 5.2 and earlier.
* PHP 5.3 will emit a warning.
*
* @param mixed $arr
* @access public
*/
function phpseclib_safe_serialize(&$arr)
{
if (is_object($arr)) {
return '';
}
if (!is_array($arr)) {
return serialize($arr);
}
// prevent circular array recursion
if (isset($arr['__phpseclib_marker'])) {
return '';
}
$safearr = array();
$arr['__phpseclib_marker'] = true;
foreach (array_keys($arr) as $key) {
// do not recurse on the '__phpseclib_marker' key itself, for smaller memory usage
if ($key !== '__phpseclib_marker') {
$safearr[$key] = phpseclib_safe_serialize($arr[$key]);
}
}
unset($arr['__phpseclib_marker']);
return serialize($safearr);
}
}
if (!function_exists('phpseclib_resolve_include_path')) { if (!function_exists('phpseclib_resolve_include_path')) {
/** /**
* Resolve filename against the include path. * Resolve filename against the include path.

View File

@ -1842,7 +1842,7 @@ class Math_BigInteger
// calculate the appropriate window size. // calculate the appropriate window size.
// $window_size == 3 if $window_ranges is between 25 and 81, for example. // $window_size == 3 if $window_ranges is between 25 and 81, for example.
for ($i = 0, $window_size = 1; $e_length > $window_ranges[$i] && $i < count($window_ranges); ++$window_size, ++$i) { for ($i = 0, $window_size = 1; $i < count($window_ranges) && $e_length > $window_ranges[$i]; ++$window_size, ++$i) {
} }
$n_value = $n->value; $n_value = $n->value;

View File

@ -789,7 +789,7 @@ class Net_SFTP extends Net_SSH2
{ {
$files = $this->_list($dir, false); $files = $this->_list($dir, false);
if (!$recursive) { if (!$recursive || $files === false) {
return $files; return $files;
} }
@ -2350,6 +2350,76 @@ class Net_SFTP extends Net_SSH2
return $result === NET_SFTP_TYPE_SYMLINK; return $result === NET_SFTP_TYPE_SYMLINK;
} }
/**
* Tells whether a file exists and is readable
*
* @param string $path
* @return bool
* @access public
*/
function is_readable($path)
{
$path = $this->_realpath($path);
$packet = pack('Na*N2', strlen($path), $path, NET_SFTP_OPEN_READ, 0);
if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
return false;
}
$response = $this->_get_sftp_packet();
switch ($this->packet_type) {
case NET_SFTP_HANDLE:
return true;
case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
return false;
default:
user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
return false;
}
}
/**
* Tells whether the filename is writable
*
* @param string $path
* @return bool
* @access public
*/
function is_writable($path)
{
$path = $this->_realpath($path);
$packet = pack('Na*N2', strlen($path), $path, NET_SFTP_OPEN_WRITE, 0);
if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
return false;
}
$response = $this->_get_sftp_packet();
switch ($this->packet_type) {
case NET_SFTP_HANDLE:
return true;
case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
return false;
default:
user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
return false;
}
}
/**
* Tells whether the filename is writeable
*
* Alias of is_writable
*
* @param string $path
* @return bool
* @access public
*/
function is_writeable($path)
{
return $this->is_writable($path);
}
/** /**
* Gets last access time of file * Gets last access time of file
* *

View File

@ -3875,7 +3875,7 @@ class Net_SSH2
/** /**
* Returns all errors * Returns all errors
* *
* @return string * @return string[]
* @access public * @access public
*/ */
function getErrors() function getErrors()

View File

@ -320,9 +320,10 @@ class System_SSH_Agent
for ($i = 0; $i < $keyCount; $i++) { for ($i = 0; $i < $keyCount; $i++) {
$length = current(unpack('N', fread($this->fsock, 4))); $length = current(unpack('N', fread($this->fsock, 4)));
$key_blob = fread($this->fsock, $length); $key_blob = fread($this->fsock, $length);
$key_str = 'ssh-rsa ' . base64_encode($key_blob);
$length = current(unpack('N', fread($this->fsock, 4))); $length = current(unpack('N', fread($this->fsock, 4)));
if ($length) { if ($length) {
$key_comment = fread($this->fsock, $length); $key_str.= ' ' . fread($this->fsock, $length);
} }
$length = current(unpack('N', substr($key_blob, 0, 4))); $length = current(unpack('N', substr($key_blob, 0, 4)));
$key_type = substr($key_blob, 4, $length); $key_type = substr($key_blob, 4, $length);
@ -332,7 +333,7 @@ class System_SSH_Agent
include_once 'Crypt/RSA.php'; include_once 'Crypt/RSA.php';
} }
$key = new Crypt_RSA(); $key = new Crypt_RSA();
$key->loadKey('ssh-rsa ' . base64_encode($key_blob) . ' ' . $key_comment); $key->loadKey($key_str);
break; break;
case 'ssh-dss': case 'ssh-dss':
// not currently supported // not currently supported