From 123eee71509f7dc33e69125a8a968de38bfff144 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Fri, 18 Mar 2016 12:00:41 -0500 Subject: [PATCH 1/7] RC4: key wasn't being truncated correctly --- phpseclib/Crypt/RC4.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpseclib/Crypt/RC4.php b/phpseclib/Crypt/RC4.php index 4c871256..eccf682c 100644 --- a/phpseclib/Crypt/RC4.php +++ b/phpseclib/Crypt/RC4.php @@ -234,7 +234,7 @@ class Crypt_RC4 extends Crypt_Base if ($length < 8) { $this->key_length = 1; } elseif ($length > 2048) { - $this->key_length = 248; + $this->key_length = 256; } else { $this->key_length = $length >> 3; } From d74bfb73e47f564ab6b004d1c31ee6947ae9fd22 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Fri, 18 Mar 2016 16:34:22 -0500 Subject: [PATCH 2/7] RC2: getKeyLength didn't always return key length --- phpseclib/Crypt/RC2.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/phpseclib/Crypt/RC2.php b/phpseclib/Crypt/RC2.php index 0f34880d..e0932e28 100644 --- a/phpseclib/Crypt/RC2.php +++ b/phpseclib/Crypt/RC2.php @@ -387,7 +387,7 @@ class Crypt_RC2 extends Crypt_Base /** * 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 * Crypt_RC2::setKey() call. * @@ -396,9 +396,16 @@ class Crypt_RC2 extends Crypt_Base */ 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->current_key_length = $this->default_key_length; + + parent::setKeyLength($length); } /** @@ -415,7 +422,7 @@ class Crypt_RC2 extends Crypt_Base /** * 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 * has more then 128 bytes in it, and set $key to a single null byte if * it is empty. From 56acb6723cd6f8cddc27e87f41f74afed1137f46 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 19 Mar 2016 12:06:12 -0500 Subject: [PATCH 3/7] Tests/BigInteger: add unit test for abhishektaneja's sliding window fix --- tests/Unit/Math/BigInteger/TestCase.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/Unit/Math/BigInteger/TestCase.php b/tests/Unit/Math/BigInteger/TestCase.php index b42c0ca6..d5a6b75b 100644 --- a/tests/Unit/Math/BigInteger/TestCase.php +++ b/tests/Unit/Math/BigInteger/TestCase.php @@ -374,4 +374,15 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase $this->assertContains('[value] => 0x32', $str); return $str; } + + /** + * @group github954 + */ + public function testSlidingWindow() + { + $e = $this->getInstance(str_repeat('1', 1794), 2); + $x = $this->getInstance(1); + $n = $this->getInstance(2); + $x->powMod($e, $n); + } } From cb2ccea219ac050ad92c7aaa296fe3110e55d634 Mon Sep 17 00:00:00 2001 From: Abhishek Taneja Date: Fri, 18 Mar 2016 16:35:38 +0100 Subject: [PATCH 4/7] changing valid condition order, _slidingWindow biginteger --- phpseclib/Math/BigInteger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 0a9a2915..d09677ac 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -1842,7 +1842,7 @@ class Math_BigInteger // calculate the appropriate window size. // $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; From ac97ce5d69888f785b1db37d1c4d4dc34e942aff Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 3 Apr 2016 21:34:11 -0500 Subject: [PATCH 5/7] RSA: backport macdabby's changes --- phpseclib/Crypt/RSA.php | 4 ++-- tests/Unit/Crypt/RSA/LoadKeyTest.php | 34 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 9369d437..997d2d86 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -1786,7 +1786,7 @@ class Crypt_RSA function setPrivateKey($key = false, $type = false) { if ($key === false && !empty($this->publicExponent)) { - unset($this->publicExponent); + $this->publicExponent = false; return true; } @@ -1794,7 +1794,7 @@ class Crypt_RSA if (!$rsa->loadKey($key, $type)) { return false; } - unset($rsa->publicExponent); + $rsa->publicExponent = false; // don't overwrite the old key if the new key is invalid $this->loadKey($rsa); diff --git a/tests/Unit/Crypt/RSA/LoadKeyTest.php b/tests/Unit/Crypt/RSA/LoadKeyTest.php index 7c09a890..91539bc0 100644 --- a/tests/Unit/Crypt/RSA/LoadKeyTest.php +++ b/tests/Unit/Crypt/RSA/LoadKeyTest.php @@ -345,4 +345,38 @@ Z2sKniRCcDT1ZP4= $this->assertTrue($result); } + + /** + * @group github960 + */ + public function testSetLoad() + { + $key = 'PuTTY-User-Key-File-2: ssh-rsa +Encryption: aes256-cbc +Comment: phpseclib-generated-key +Public-Lines: 4 +AAAAB3NzaC1yc2EAAAADAQABAAAAgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4 +eCZ0FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RK +NUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0Tp0GbMJDy +R4e9T04ZZw== +Private-Lines: 8 +llx04QMegql0/nE5RvcJSrGrodxt6ytuv/JX2caeZBUyQwQc2WBNYagLHyHPM9jI +9OUWz59FLhjFXZMDNMoUXxVmjwQpOAaVPYNxxFM9AF6/NXFji64K7huD9n4A+kLn +sHwMLWPR5a/tZA0r05DZNz9ULA3mQu7Hz4EQ8ifu3uTPJuTmL51x6RmudYKysb20 +fM8VzC3ukvzzRh0pujUVTr/yQdmciASVFnZlt4xQy+ZEOVUAOfwjd//AFfXTvk6x +7A45rNlU/uicHwLgoY1APvRHCFxw7F+uVW5L4mSX7NNzqBKkZ+1qpQTAfQvIfEIb +444+CXsgIyOpqt6VxJH2u6elAtE1wau3YaFR8Alm8m97rFYzRi3oDP5NZYkTCWSV +EOpSeghXSs7IilJu8I6/sB1w5dakdeBSFkIynrlFXkO0uUw+QJJWjxY8SypzgIuP +DzduF6XsQrCyo6dnIpGQCQ== +Private-MAC: 35134b7434bf828b21404099861d455e660e8740'; + + $rsa = new Crypt_RSA(); + $rsa->setPrivateKey($key); + $rsa->loadKey($key); + + $rsa = new Crypt_RSA(); + $rsa->loadKey($key); + $rsa->setPrivateKey(); + $rsa->loadKey($rsa); + } } From 6a97ddfa725b6e099c3af7fc830f8ff92f7c00ac Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 10 Apr 2016 10:58:49 -0500 Subject: [PATCH 6/7] SSH/Agent: if comment is empty don't include it --- phpseclib/System/SSH/Agent.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/phpseclib/System/SSH/Agent.php b/phpseclib/System/SSH/Agent.php index 4ca34d4c..0f4874dc 100644 --- a/phpseclib/System/SSH/Agent.php +++ b/phpseclib/System/SSH/Agent.php @@ -320,9 +320,10 @@ class System_SSH_Agent for ($i = 0; $i < $keyCount; $i++) { $length = current(unpack('N', fread($this->fsock, 4))); $key_blob = fread($this->fsock, $length); + $key_str = 'ssh-rsa ' . base64_encode($key_blob); $length = current(unpack('N', fread($this->fsock, 4))); if ($length) { - $key_comment = fread($this->fsock, $length); + $key_str.= ' ' . fread($this->fsock, $length); } $length = current(unpack('N', substr($key_blob, 0, 4))); $key_type = substr($key_blob, 4, $length); @@ -332,7 +333,7 @@ class System_SSH_Agent include_once 'Crypt/RSA.php'; } $key = new Crypt_RSA(); - $key->loadKey('ssh-rsa ' . base64_encode($key_blob) . ' ' . $key_comment); + $key->loadKey($key_str); break; case 'ssh-dss': // not currently supported From d22bcd63cc8a0c3ca3290c2a50e9fe8fe39ea4a6 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Mon, 11 Apr 2016 15:18:06 -0500 Subject: [PATCH 7/7] SFTP: nlist() on a non-existent directory resulted in error --- phpseclib/Net/SFTP.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index 361aac78..4d10dbe1 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -789,7 +789,7 @@ class Net_SFTP extends Net_SSH2 { $files = $this->_list($dir, false); - if (!$recursive) { + if (!$recursive || $files === false) { return $files; }