From 317efaf28a02f380a292068443f47dd0ef660fd1 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Thu, 17 Apr 2014 10:30:32 -0500 Subject: [PATCH 01/12] RSA: auto-detect public keys vs private keys --- phpseclib/Crypt/RSA.php | 53 +++++++++++++++++++++++- tests/Crypt/RSA/LoadKeyTest.php | 71 ++++++++++++++++++++++++++++++--- 2 files changed, 117 insertions(+), 7 deletions(-) diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index b6015178..d53d0e2f 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -477,7 +477,7 @@ class Crypt_RSA case extension_loaded('openssl') && version_compare(PHP_VERSION, '4.2.0', '>=') && file_exists($this->configFile): // some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work ob_start(); - phpinfo(); + @phpinfo(); $content = ob_get_contents(); ob_end_clean(); @@ -1478,6 +1478,19 @@ class Crypt_RSA $this->publicExponent = false; } + switch ($type) { + case CRYPT_RSA_PUBLIC_FORMAT_OPENSSH: + case CRYPT_RSA_PUBLIC_FORMAT_RAW: + $this->setPublicKey(); + break; + case CRYPT_RSA_PRIVATE_FORMAT_PKCS1: + switch (true) { + case strpos($key, '-BEGIN PUBLIC KEY-') !== false: + case strpos($key, '-BEGIN RSA PUBLIC KEY-') !== false: + $this->setPublicKey(); + } + } + return true; } @@ -1504,7 +1517,9 @@ class Crypt_RSA * used in certain contexts. For example, in SSH-2, RSA authentication works by sending the public key along with a * message signed by the private key to the server. The SSH-2 server looks the public key up in an index of public keys * and if it's present then proceeds to verify the signature. Problem is, if your private key doesn't include the public - * exponent this won't work unless you manually add the public exponent. + * exponent this won't work unless you manually add the public exponent. phpseclib tries to guess if the key being used + * is the public key but in the event that it guesses incorrectly you might still want to explicitly set the key as being + * public. * * Do note that when a new key is loaded the index will be cleared. * @@ -1560,6 +1575,40 @@ class Crypt_RSA return true; } + /** + * Defines the private key + * + * If phpseclib guessed a private key was a public key and loaded it as such it might be desirable to force + * phpseclib to treat the key as a private key. This function will do that. + * + * Do note that when a new key is loaded the index will be cleared. + * + * Returns true on success, false on failure + * + * @see getPublicKey() + * @access public + * @param String $key optional + * @param Integer $type optional + * @return Boolean + */ + function setPrivateKey($key = false, $type = false) + { + if ($key === false && !empty($this->publicExponent)) { + unset($this->publicExponent); + return true; + } + + $rsa = new Crypt_RSA(); + if (!$rsa->loadKey($key, $type)) { + return false; + } + unset($rsa->publicExponent); + + // don't overwrite the old key if the new key is invalid + $this->loadKey($rsa); + return true; + } + /** * Returns the public key * diff --git a/tests/Crypt/RSA/LoadKeyTest.php b/tests/Crypt/RSA/LoadKeyTest.php index 95239d84..23594c01 100644 --- a/tests/Crypt/RSA/LoadKeyTest.php +++ b/tests/Crypt/RSA/LoadKeyTest.php @@ -36,7 +36,7 @@ U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ 37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0= -----END RSA PRIVATE KEY-----'; - $this->assertTrue($rsa->loadKey($key)); + $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPrivateKey())); } public function testPKCS1SpacesKey() @@ -58,7 +58,7 @@ U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ -----END RSA PRIVATE KEY-----'; $key = str_replace(array("\r", "\n", "\r\n"), ' ', $key); - $this->assertTrue($rsa->loadKey($key)); + $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPrivateKey())); } public function testPKCS1NoHeaderKey() @@ -77,7 +77,7 @@ X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ 37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0='; - $this->assertTrue($rsa->loadKey($key)); + $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPrivateKey())); } public function testPKCS1NoWhitespaceNoHeaderKey() @@ -95,7 +95,7 @@ U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ 'X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl' . 'U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ' . '37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0='; - $this->assertTrue($rsa->loadKey($key)); + $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPrivateKey())); } public function testRawPKCS1Key() @@ -115,6 +115,67 @@ U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ '37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0='; $key = base64_decode($key); - $this->assertTrue($rsa->loadKey($key)); + $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPrivateKey())); + } + + public function testPubKey1() + { + $rsa = new Crypt_RSA(); + + $key = '-----BEGIN RSA PUBLIC KEY----- +MIIBCgKCAQEA61BjmfXGEvWmegnBGSuS+rU9soUg2FnODva32D1AqhwdziwHINFa +D1MVlcrYG6XRKfkcxnaXGfFDWHLEvNBSEVCgJjtHAGZIm5GL/KA86KDp/CwDFMSw +luowcXwDwoyinmeOY9eKyh6aY72xJh7noLBBq1N0bWi1e2i+83txOCg4yV2oVXhB +o8pYEJ8LT3el6Smxol3C1oFMVdwPgc0vTl25XucMcG/ALE/KNY6pqC2AQ6R2ERlV +gPiUWOPatVkt7+Bs3h5Ramxh7XjBOXeulmCpGSynXNcpZ/06+vofGi/2MlpQZNhH +Ao8eayMp6FcvNucIpUndo1X8dKMv3Y26ZQIDAQAB +-----END RSA PUBLIC KEY-----'; + + $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPublicKey()) && $rsa->getPrivateKey() === false); + } + + public function testPubKey2() + { + $rsa = new Crypt_RSA(); + + $key = '-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA61BjmfXGEvWmegnBGSuS ++rU9soUg2FnODva32D1AqhwdziwHINFaD1MVlcrYG6XRKfkcxnaXGfFDWHLEvNBS +EVCgJjtHAGZIm5GL/KA86KDp/CwDFMSwluowcXwDwoyinmeOY9eKyh6aY72xJh7n +oLBBq1N0bWi1e2i+83txOCg4yV2oVXhBo8pYEJ8LT3el6Smxol3C1oFMVdwPgc0v +Tl25XucMcG/ALE/KNY6pqC2AQ6R2ERlVgPiUWOPatVkt7+Bs3h5Ramxh7XjBOXeu +lmCpGSynXNcpZ/06+vofGi/2MlpQZNhHAo8eayMp6FcvNucIpUndo1X8dKMv3Y26 +ZQIDAQAB +-----END PUBLIC KEY-----'; + + $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPublicKey()) && $rsa->getPrivateKey() === false); + } + + public function testPubKey3() + { + $rsa = new Crypt_RSA(); + + $key = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4e' . + 'CZ0FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMS' . + 'GkVb1/3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZw== ' . + 'phpseclib-generated-key'; + + $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPublicKey()) && $rsa->getPrivateKey() === false); + } + + public function testSetPrivate() + { + $rsa = new Crypt_RSA(); + + $key = '-----BEGIN RSA PUBLIC KEY----- +MIIBCgKCAQEA61BjmfXGEvWmegnBGSuS+rU9soUg2FnODva32D1AqhwdziwHINFa +D1MVlcrYG6XRKfkcxnaXGfFDWHLEvNBSEVCgJjtHAGZIm5GL/KA86KDp/CwDFMSw +luowcXwDwoyinmeOY9eKyh6aY72xJh7noLBBq1N0bWi1e2i+83txOCg4yV2oVXhB +o8pYEJ8LT3el6Smxol3C1oFMVdwPgc0vTl25XucMcG/ALE/KNY6pqC2AQ6R2ERlV +gPiUWOPatVkt7+Bs3h5Ramxh7XjBOXeulmCpGSynXNcpZ/06+vofGi/2MlpQZNhH +Ao8eayMp6FcvNucIpUndo1X8dKMv3Y26ZQIDAQAB +-----END RSA PUBLIC KEY-----'; + + $this->assertTrue($rsa->loadKey($key) && $rsa->setPrivateKey() && is_string("$rsa") && $rsa->getPublicKey() === false); } } From d9d224572b584d85885636f0f42aaa875aebad6d Mon Sep 17 00:00:00 2001 From: terrafrost Date: Fri, 18 Apr 2014 11:41:08 -0500 Subject: [PATCH 02/12] RSA: update unit test file --- tests/Crypt/RSA/LoadKeyTest.php | 35 +++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/tests/Crypt/RSA/LoadKeyTest.php b/tests/Crypt/RSA/LoadKeyTest.php index 23594c01..55d2face 100644 --- a/tests/Crypt/RSA/LoadKeyTest.php +++ b/tests/Crypt/RSA/LoadKeyTest.php @@ -36,7 +36,8 @@ U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ 37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0= -----END RSA PRIVATE KEY-----'; - $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPrivateKey())); + $this->assertTrue($rsa->loadKey($key)); + $this->assertInternalType('string', $rsa->getPrivateKey()); } public function testPKCS1SpacesKey() @@ -58,7 +59,8 @@ U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ -----END RSA PRIVATE KEY-----'; $key = str_replace(array("\r", "\n", "\r\n"), ' ', $key); - $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPrivateKey())); + $this->assertTrue($rsa->loadKey($key)); + $this->assertInternalType('string', $rsa->getPrivateKey()); } public function testPKCS1NoHeaderKey() @@ -77,7 +79,8 @@ X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ 37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0='; - $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPrivateKey())); + $this->assertTrue($rsa->loadKey($key)); + $this->assertInternalType('string', $rsa->getPrivateKey()); } public function testPKCS1NoWhitespaceNoHeaderKey() @@ -95,7 +98,9 @@ U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ 'X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl' . 'U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ' . '37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0='; - $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPrivateKey())); + + $this->assertTrue($rsa->loadKey($key)); + $this->assertInternalType('string', $rsa->getPrivateKey()); } public function testRawPKCS1Key() @@ -115,7 +120,8 @@ U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ '37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0='; $key = base64_decode($key); - $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPrivateKey())); + $this->assertTrue($rsa->loadKey($key)); + $this->assertInternalType('string', $rsa->getPrivateKey()); } public function testPubKey1() @@ -131,7 +137,9 @@ gPiUWOPatVkt7+Bs3h5Ramxh7XjBOXeulmCpGSynXNcpZ/06+vofGi/2MlpQZNhH Ao8eayMp6FcvNucIpUndo1X8dKMv3Y26ZQIDAQAB -----END RSA PUBLIC KEY-----'; - $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPublicKey()) && $rsa->getPrivateKey() === false); + $this->assertTrue($rsa->loadKey($key)); + $this->assertInternalType('string', $rsa->getPublicKey()); + $this->assertFalse($rsa->getPrivateKey()); } public function testPubKey2() @@ -148,10 +156,12 @@ lmCpGSynXNcpZ/06+vofGi/2MlpQZNhHAo8eayMp6FcvNucIpUndo1X8dKMv3Y26 ZQIDAQAB -----END PUBLIC KEY-----'; - $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPublicKey()) && $rsa->getPrivateKey() === false); + $this->assertTrue($rsa->loadKey($key)); + $this->assertInternalType('string', $rsa->getPublicKey()); + $this->assertFalse($rsa->getPrivateKey()); } - public function testPubKey3() + public function testSSHPubKey() { $rsa = new Crypt_RSA(); @@ -160,7 +170,9 @@ ZQIDAQAB 'GkVb1/3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZw== ' . 'phpseclib-generated-key'; - $this->assertTrue($rsa->loadKey($key) && is_string($rsa->getPublicKey()) && $rsa->getPrivateKey() === false); + $this->assertTrue($rsa->loadKey($key)); + $this->assertInternalType('string', $rsa->getPublicKey()); + $this->assertFalse($rsa->getPrivateKey()); } public function testSetPrivate() @@ -176,6 +188,9 @@ gPiUWOPatVkt7+Bs3h5Ramxh7XjBOXeulmCpGSynXNcpZ/06+vofGi/2MlpQZNhH Ao8eayMp6FcvNucIpUndo1X8dKMv3Y26ZQIDAQAB -----END RSA PUBLIC KEY-----'; - $this->assertTrue($rsa->loadKey($key) && $rsa->setPrivateKey() && is_string("$rsa") && $rsa->getPublicKey() === false); + $this->assertTrue($rsa->loadKey($key)); + $this->assertTrue($rsa->setPrivateKey()); + $this->assertGreaterThanOrEqual(1, strlen("$rsa")); + $this->assertFalse($rsa->getPublicKey()); } } From 8aa17c285c95c66b719343b98fc9eb7677db56b6 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 1 Jun 2014 13:12:42 +0200 Subject: [PATCH 03/12] Produce a clover.xml code coverage file. --- travis/run-phpunit.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/travis/run-phpunit.sh b/travis/run-phpunit.sh index 013cf3ae..713e98da 100755 --- a/travis/run-phpunit.sh +++ b/travis/run-phpunit.sh @@ -24,4 +24,5 @@ fi $PHPUNIT_EXTRA_ARGS \ --verbose \ --coverage-text \ + --coverage-clover code_coverage/clover.xml \ --coverage-html code_coverage/ From a29b46693cc184d7f7902e9727049be34b01e7ab Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 1 Jun 2014 13:17:22 +0200 Subject: [PATCH 04/12] Upload clover.xml from Travis CI into Scrutinizer CI. --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index c6f56a9b..f12b4e0e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,3 +26,5 @@ script: after_success: - sh -c "if $TRAVIS_SECURE_ENV_VARS; then travis/upload-code-coverage.sh; fi" + - sh -c "if [ '$TRAVIS_PHP_VERSION' != '5.2' ]; then wget https://scrutinizer-ci.com/ocular.phar; fi" + - sh -c "if [ '$TRAVIS_PHP_VERSION' != '5.2' ]; then php ocular.phar code-coverage:upload --format=php-clover code_coverage/clover.xml; fi" From 2c00f074e593e46b63bf60081ec47acbe8fdb213 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 1 Jun 2014 13:21:36 +0200 Subject: [PATCH 05/12] Configure Scrutinizer CI to expect external code coverage data. --- .scrutinizer.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .scrutinizer.yml diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 00000000..d29cda28 --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,5 @@ +imports: + - php + +tools: + external_code_coverage: true From b5fed807f3b7983d3bbe4dacec7eb7ee33aa2fcc Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 1 Jun 2014 13:45:51 +0200 Subject: [PATCH 06/12] Add -html suffix to existing code-coverage shell script. --- .travis.yml | 2 +- .../{upload-code-coverage.sh => upload-code-coverage-html.sh} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename travis/{upload-code-coverage.sh => upload-code-coverage-html.sh} (100%) diff --git a/.travis.yml b/.travis.yml index f12b4e0e..1f858845 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,6 +25,6 @@ script: - travis/run-phpunit.sh after_success: - - sh -c "if $TRAVIS_SECURE_ENV_VARS; then travis/upload-code-coverage.sh; fi" + - sh -c "if $TRAVIS_SECURE_ENV_VARS; then travis/upload-code-coverage-html.sh; fi" - sh -c "if [ '$TRAVIS_PHP_VERSION' != '5.2' ]; then wget https://scrutinizer-ci.com/ocular.phar; fi" - sh -c "if [ '$TRAVIS_PHP_VERSION' != '5.2' ]; then php ocular.phar code-coverage:upload --format=php-clover code_coverage/clover.xml; fi" diff --git a/travis/upload-code-coverage.sh b/travis/upload-code-coverage-html.sh similarity index 100% rename from travis/upload-code-coverage.sh rename to travis/upload-code-coverage-html.sh From 33e62401c386eb7efed9266b57333ad433790c83 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 1 Jun 2014 13:49:15 +0200 Subject: [PATCH 07/12] Extract into upload-code-coverage-scrutinizer.sh --- .travis.yml | 3 +-- travis/upload-code-coverage-scrutinizer.sh | 13 +++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100755 travis/upload-code-coverage-scrutinizer.sh diff --git a/.travis.yml b/.travis.yml index 1f858845..0ca3896d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,5 +26,4 @@ script: after_success: - sh -c "if $TRAVIS_SECURE_ENV_VARS; then travis/upload-code-coverage-html.sh; fi" - - sh -c "if [ '$TRAVIS_PHP_VERSION' != '5.2' ]; then wget https://scrutinizer-ci.com/ocular.phar; fi" - - sh -c "if [ '$TRAVIS_PHP_VERSION' != '5.2' ]; then php ocular.phar code-coverage:upload --format=php-clover code_coverage/clover.xml; fi" + - sh -c "if [ '$TRAVIS_PHP_VERSION' != '5.2' ]; then travis/upload-code-coverage-scrutinizer.sh; fi" diff --git a/travis/upload-code-coverage-scrutinizer.sh b/travis/upload-code-coverage-scrutinizer.sh new file mode 100755 index 00000000..efcff08c --- /dev/null +++ b/travis/upload-code-coverage-scrutinizer.sh @@ -0,0 +1,13 @@ +#!/bin/sh +# +# This file is part of the phpseclib project. +# +# (c) Andreas Fischer +# +# For the full copyright and license information, please view the LICENSE +# file that was distributed with this source code. +# +set -e + +wget https://scrutinizer-ci.com/ocular.phar +php ocular.phar code-coverage:upload --format=php-clover code_coverage/clover.xml From 825cd124aaa016eace39f178359b7b1c97cadac7 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 1 Jun 2014 13:50:10 +0200 Subject: [PATCH 08/12] No Scrutinizer Code Coverage on HHVM either. Fatal error: Uncaught exception 'PharException' with message 'Corrupt phar, can't read 4 bytes starting at offset 1663508419' in : Stack trace: 0 (): Phar::bytesToInt() 1 (): Phar->parsePhar() 2 (): Phar->__construct() 3 /home/travis/build/phpseclib/phpseclib/ocular.phar(9): Phar::mapPhar() 4 {main} --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0ca3896d..661becfb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,4 +26,4 @@ script: after_success: - sh -c "if $TRAVIS_SECURE_ENV_VARS; then travis/upload-code-coverage-html.sh; fi" - - sh -c "if [ '$TRAVIS_PHP_VERSION' != '5.2' ]; then travis/upload-code-coverage-scrutinizer.sh; fi" + - sh -c "if [ '$TRAVIS_PHP_VERSION' != '5.2' -a '$TRAVIS_PHP_VERSION' != 'hhvm' ]; then travis/upload-code-coverage-scrutinizer.sh; fi" From 3cda3178625f73c8bf3ecf2f8cd555bee010fafe Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 1 Jun 2014 13:52:24 +0200 Subject: [PATCH 09/12] Specify the runs option for external code coverage. --- .scrutinizer.yml | 3 ++- .travis.yml | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index d29cda28..4ad008a5 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -2,4 +2,5 @@ imports: - php tools: - external_code_coverage: true + external_code_coverage: + runs: 5 # No Code Coverage on PHP 5.2 and HHVM diff --git a/.travis.yml b/.travis.yml index 661becfb..9872b17c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: php +# When adding environments here, the number of runs specified in .scrutinizer.yml +# may have to be adjusted. php: - 5.2 - 5.3.3 From 4af647f170be8e0a0a1b816fe2907f25678ef469 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 1 Jun 2014 21:13:20 +0200 Subject: [PATCH 10/12] Split Unit/Functional Test Suites. --- phpunit.xml.dist | 7 +++++-- .../Net/SFTPUserStoryTest.php} | 2 +- .../SSH2FunctionalTest.php => Functional/Net/SSH2Test.php} | 2 +- tests/{ => Unit}/Crypt/AES/ContinuousBufferTest.php | 2 +- tests/{ => Unit}/Crypt/AES/TestCase.php | 2 +- tests/{ => Unit}/Crypt/Hash/MD5Test.php | 2 +- tests/{ => Unit}/Crypt/Hash/TestCase.php | 2 +- tests/{ => Unit}/Crypt/RSA/LoadKeyTest.php | 2 +- tests/{ => Unit}/File/ASN1/DevTest.php | 2 +- tests/{ => Unit}/Math/BigInteger/BCMathTest.php | 2 +- tests/{ => Unit}/Math/BigInteger/GMPTest.php | 2 +- tests/{ => Unit}/Math/BigInteger/InternalOpenSSLTest.php | 2 +- tests/{ => Unit}/Math/BigInteger/InternalTest.php | 2 +- tests/{ => Unit}/Math/BigInteger/TestCase.php | 2 +- tests/{ => Unit}/Net/SSH1Test.php | 2 +- tests/{ => Unit}/Net/SSH2Test.php | 2 +- 16 files changed, 20 insertions(+), 17 deletions(-) rename tests/{Net/SFTPFunctionalTest.php => Functional/Net/SFTPUserStoryTest.php} (99%) rename tests/{Net/SSH2FunctionalTest.php => Functional/Net/SSH2Test.php} (95%) rename tests/{ => Unit}/Crypt/AES/ContinuousBufferTest.php (95%) rename tests/{ => Unit}/Crypt/AES/TestCase.php (91%) rename tests/{ => Unit}/Crypt/Hash/MD5Test.php (95%) rename tests/{ => Unit}/Crypt/Hash/TestCase.php (95%) rename tests/{ => Unit}/Crypt/RSA/LoadKeyTest.php (99%) rename tests/{ => Unit}/File/ASN1/DevTest.php (99%) rename tests/{ => Unit}/Math/BigInteger/BCMathTest.php (86%) rename tests/{ => Unit}/Math/BigInteger/GMPTest.php (87%) rename tests/{ => Unit}/Math/BigInteger/InternalOpenSSLTest.php (85%) rename tests/{ => Unit}/Math/BigInteger/InternalTest.php (89%) rename tests/{ => Unit}/Math/BigInteger/TestCase.php (99%) rename tests/{ => Unit}/Net/SSH1Test.php (96%) rename tests/{ => Unit}/Net/SSH2Test.php (98%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f579ab4f..09f8b8ff 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -4,8 +4,11 @@ colors="true" > - - ./tests/ + + ./tests/Unit/ + + + ./tests/Functional/ diff --git a/tests/Net/SFTPFunctionalTest.php b/tests/Functional/Net/SFTPUserStoryTest.php similarity index 99% rename from tests/Net/SFTPFunctionalTest.php rename to tests/Functional/Net/SFTPUserStoryTest.php index 9c43b55b..05c316ba 100644 --- a/tests/Net/SFTPFunctionalTest.php +++ b/tests/Functional/Net/SFTPUserStoryTest.php @@ -6,7 +6,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -class Net_SFTPFunctionalTest extends PhpseclibFunctionalTestCase +class Functional_Net_SFTPUserStoryTest extends PhpseclibFunctionalTestCase { static protected $scratchDir; static protected $exampleData; diff --git a/tests/Net/SSH2FunctionalTest.php b/tests/Functional/Net/SSH2Test.php similarity index 95% rename from tests/Net/SSH2FunctionalTest.php rename to tests/Functional/Net/SSH2Test.php index 1fd7e271..0f92ca20 100644 --- a/tests/Net/SSH2FunctionalTest.php +++ b/tests/Functional/Net/SSH2Test.php @@ -6,7 +6,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -class Net_SSH2FunctionalTest extends PhpseclibFunctionalTestCase +class Functional_Net_SSH2Test extends PhpseclibFunctionalTestCase { public function setUp() { diff --git a/tests/Crypt/AES/ContinuousBufferTest.php b/tests/Unit/Crypt/AES/ContinuousBufferTest.php similarity index 95% rename from tests/Crypt/AES/ContinuousBufferTest.php rename to tests/Unit/Crypt/AES/ContinuousBufferTest.php index 1d712597..e032dab6 100644 --- a/tests/Crypt/AES/ContinuousBufferTest.php +++ b/tests/Unit/Crypt/AES/ContinuousBufferTest.php @@ -5,7 +5,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -class Crypt_AES_ContinuousBufferTest extends Crypt_AES_TestCase +class Unit_Crypt_AES_ContinuousBufferTest extends Unit_Crypt_AES_TestCase { // String intented protected $modes = array( diff --git a/tests/Crypt/AES/TestCase.php b/tests/Unit/Crypt/AES/TestCase.php similarity index 91% rename from tests/Crypt/AES/TestCase.php rename to tests/Unit/Crypt/AES/TestCase.php index 317f1e52..c4bf57e6 100644 --- a/tests/Crypt/AES/TestCase.php +++ b/tests/Unit/Crypt/AES/TestCase.php @@ -7,7 +7,7 @@ require_once 'Crypt/AES.php'; -abstract class Crypt_AES_TestCase extends PhpseclibTestCase +abstract class Unit_Crypt_AES_TestCase extends PhpseclibTestCase { static public function setUpBeforeClass() { diff --git a/tests/Crypt/Hash/MD5Test.php b/tests/Unit/Crypt/Hash/MD5Test.php similarity index 95% rename from tests/Crypt/Hash/MD5Test.php rename to tests/Unit/Crypt/Hash/MD5Test.php index 4b0635de..3651147e 100644 --- a/tests/Crypt/Hash/MD5Test.php +++ b/tests/Unit/Crypt/Hash/MD5Test.php @@ -5,7 +5,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -class Crypt_Hash_MD5Test extends Crypt_Hash_TestCase +class Unit_Crypt_Hash_MD5Test extends Unit_Crypt_Hash_TestCase { public function getInstance() { diff --git a/tests/Crypt/Hash/TestCase.php b/tests/Unit/Crypt/Hash/TestCase.php similarity index 95% rename from tests/Crypt/Hash/TestCase.php rename to tests/Unit/Crypt/Hash/TestCase.php index 5f469751..040bce2c 100644 --- a/tests/Crypt/Hash/TestCase.php +++ b/tests/Unit/Crypt/Hash/TestCase.php @@ -7,7 +7,7 @@ require_once 'Crypt/Hash.php'; -abstract class Crypt_Hash_TestCase extends PhpseclibTestCase +abstract class Unit_Crypt_Hash_TestCase extends PhpseclibTestCase { static public function setUpBeforeClass() { diff --git a/tests/Crypt/RSA/LoadKeyTest.php b/tests/Unit/Crypt/RSA/LoadKeyTest.php similarity index 99% rename from tests/Crypt/RSA/LoadKeyTest.php rename to tests/Unit/Crypt/RSA/LoadKeyTest.php index 95239d84..da36b0c4 100644 --- a/tests/Crypt/RSA/LoadKeyTest.php +++ b/tests/Unit/Crypt/RSA/LoadKeyTest.php @@ -7,7 +7,7 @@ require_once 'Crypt/RSA.php' ; -class Crypt_RSA_LoadKeyTest extends PhpseclibTestCase +class Unit_Crypt_RSA_LoadKeyTest extends PhpseclibTestCase { public function testBadKey() { diff --git a/tests/File/ASN1/DevTest.php b/tests/Unit/File/ASN1/DevTest.php similarity index 99% rename from tests/File/ASN1/DevTest.php rename to tests/Unit/File/ASN1/DevTest.php index 6b9d9d99..d0d51042 100644 --- a/tests/File/ASN1/DevTest.php +++ b/tests/Unit/File/ASN1/DevTest.php @@ -7,7 +7,7 @@ require_once 'File/ASN1.php'; -class File_ASN1_DevTest extends PhpseclibTestCase +class Unit_File_ASN1_DevTest extends PhpseclibTestCase { /** * on older versions of File_ASN1 this would yield a PHP Warning diff --git a/tests/Math/BigInteger/BCMathTest.php b/tests/Unit/Math/BigInteger/BCMathTest.php similarity index 86% rename from tests/Math/BigInteger/BCMathTest.php rename to tests/Unit/Math/BigInteger/BCMathTest.php index 72c574d1..d856c1a8 100644 --- a/tests/Math/BigInteger/BCMathTest.php +++ b/tests/Unit/Math/BigInteger/BCMathTest.php @@ -5,7 +5,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -class Math_BigInteger_BCMathTest extends Math_BigInteger_TestCase +class Unit_Math_BigInteger_BCMathTest extends Unit_Math_BigInteger_TestCase { static public function setUpBeforeClass() { diff --git a/tests/Math/BigInteger/GMPTest.php b/tests/Unit/Math/BigInteger/GMPTest.php similarity index 87% rename from tests/Math/BigInteger/GMPTest.php rename to tests/Unit/Math/BigInteger/GMPTest.php index 27c7a6a4..02a1cae8 100644 --- a/tests/Math/BigInteger/GMPTest.php +++ b/tests/Unit/Math/BigInteger/GMPTest.php @@ -5,7 +5,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -class Math_BigInteger_GMPTest extends Math_BigInteger_TestCase +class Unit_Math_BigInteger_GMPTest extends Unit_Math_BigInteger_TestCase { static public function setUpBeforeClass() { diff --git a/tests/Math/BigInteger/InternalOpenSSLTest.php b/tests/Unit/Math/BigInteger/InternalOpenSSLTest.php similarity index 85% rename from tests/Math/BigInteger/InternalOpenSSLTest.php rename to tests/Unit/Math/BigInteger/InternalOpenSSLTest.php index 378caacc..06d57a6f 100644 --- a/tests/Math/BigInteger/InternalOpenSSLTest.php +++ b/tests/Unit/Math/BigInteger/InternalOpenSSLTest.php @@ -5,7 +5,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -class Math_BigInteger_InternalOpenSSLTest extends Math_BigInteger_TestCase +class Unit_Math_BigInteger_InternalOpenSSLTest extends Unit_Math_BigInteger_TestCase { static public function setUpBeforeClass() { diff --git a/tests/Math/BigInteger/InternalTest.php b/tests/Unit/Math/BigInteger/InternalTest.php similarity index 89% rename from tests/Math/BigInteger/InternalTest.php rename to tests/Unit/Math/BigInteger/InternalTest.php index 744b2df1..faa9bb79 100644 --- a/tests/Math/BigInteger/InternalTest.php +++ b/tests/Unit/Math/BigInteger/InternalTest.php @@ -5,7 +5,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -class Math_BigInteger_InternalTest extends Math_BigInteger_TestCase +class Math_BigInteger_InternalTest extends Unit_Math_BigInteger_TestCase { static public function setUpBeforeClass() { diff --git a/tests/Math/BigInteger/TestCase.php b/tests/Unit/Math/BigInteger/TestCase.php similarity index 99% rename from tests/Math/BigInteger/TestCase.php rename to tests/Unit/Math/BigInteger/TestCase.php index d62dcec5..d233dfb4 100644 --- a/tests/Math/BigInteger/TestCase.php +++ b/tests/Unit/Math/BigInteger/TestCase.php @@ -7,7 +7,7 @@ require_once 'Math/BigInteger.php'; -abstract class Math_BigInteger_TestCase extends PhpseclibTestCase +abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase { static public function setUpBeforeClass() { diff --git a/tests/Net/SSH1Test.php b/tests/Unit/Net/SSH1Test.php similarity index 96% rename from tests/Net/SSH1Test.php rename to tests/Unit/Net/SSH1Test.php index 7c58ceb3..5f4778f3 100644 --- a/tests/Net/SSH1Test.php +++ b/tests/Unit/Net/SSH1Test.php @@ -5,7 +5,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -class Net_SSH1Test extends PhpseclibTestCase +class Unit_Net_SSH1Test extends PhpseclibTestCase { public function formatLogDataProvider() { diff --git a/tests/Net/SSH2Test.php b/tests/Unit/Net/SSH2Test.php similarity index 98% rename from tests/Net/SSH2Test.php rename to tests/Unit/Net/SSH2Test.php index 26d6e872..221d6a4f 100644 --- a/tests/Net/SSH2Test.php +++ b/tests/Unit/Net/SSH2Test.php @@ -6,7 +6,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -class Net_SSH2Test extends PhpseclibTestCase +class Unit_Net_SSH2Test extends PhpseclibTestCase { public function formatLogDataProvider() { From 83ddfe09a6033f250e11d782874992a55eb699e9 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 1 Jun 2014 21:49:40 +0200 Subject: [PATCH 11/12] Reimplement testGenerateIdentifier to fix #321 --- tests/Net/SSH2Test.php | 45 ++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/tests/Net/SSH2Test.php b/tests/Net/SSH2Test.php index 26d6e872..3f21088d 100644 --- a/tests/Net/SSH2Test.php +++ b/tests/Net/SSH2Test.php @@ -36,38 +36,27 @@ class Net_SSH2Test extends PhpseclibTestCase $this->assertEquals($expected, $result); } - public function generateIdentifierProvider() + public function testGenerateIdentifier() { - return array( - array('SSH-2.0-phpseclib_0.3', array()), - array('SSH-2.0-phpseclib_0.3 (gmp)', array('gmp')), - array('SSH-2.0-phpseclib_0.3 (bcmath)', array('bcmath')), - array('SSH-2.0-phpseclib_0.3 (mcrypt)', array('mcrypt')), - array('SSH-2.0-phpseclib_0.3 (mcrypt, gmp)', array('mcrypt', 'gmp')), - array('SSH-2.0-phpseclib_0.3 (mcrypt, bcmath)', array('mcrypt', 'bcmath')), - ); - } + $identifier = $this->createSSHMock()->_generate_identifier(); + $this->assertStringStartsWith('SSH-2.0-phpseclib_0.3', $identifier); - /** - * @dataProvider generateIdentifierProvider - */ - public function testGenerateIdentifier($expected, array $requiredExtensions) - { - $notAllowed = array('gmp', 'bcmath', 'mcrypt', 'gmp'); - foreach ($notAllowed as $notAllowedExtension) { - if (in_array($notAllowedExtension, $requiredExtensions)) { - continue; - } - - if (extension_loaded($notAllowedExtension)) { - $this->markTestSkipped('Extension ' . $notAllowedExtension . ' is not allowed for this data-set'); - } + if (extension_loaded('mcrypt')) { + $this->assertContains('mcrypt', $identifier); + } else { + $this->assertNotContains('mcrypt', $identifier); } - $ssh = $this->createSSHMock(); - $identifier = $ssh->_generate_identifier(); - - $this->assertEquals($expected, $identifier); + if (extension_loaded('gmp')) { + $this->assertContains('gmp', $identifier); + $this->assertNotContains('bcmath', $identifier); + } else if (extension_loaded('bcmath')) { + $this->assertNotContains('gmp', $identifier); + $this->assertContains('bcmath', $identifier); + } else { + $this->assertNotContains('gmp', $identifier); + $this->assertNotContains('bcmath', $identifier); + } } public function testGetExitStatusIfNotConnected() From e6f87318f522132178839bd90fd21762cba869c9 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 1 Jun 2014 23:28:49 +0200 Subject: [PATCH 12/12] Adjust documentation to coding guidelines: No () around include. --- phpseclib/Crypt/AES.php | 2 +- phpseclib/Crypt/Blowfish.php | 2 +- phpseclib/Crypt/DES.php | 2 +- phpseclib/Crypt/Hash.php | 2 +- phpseclib/Crypt/RC2.php | 2 +- phpseclib/Crypt/RC4.php | 2 +- phpseclib/Crypt/RSA.php | 4 ++-- phpseclib/Crypt/Random.php | 2 +- phpseclib/Crypt/Rijndael.php | 2 +- phpseclib/Crypt/TripleDES.php | 2 +- phpseclib/Crypt/Twofish.php | 2 +- phpseclib/Math/BigInteger.php | 28 ++++++++++++++-------------- phpseclib/Net/SCP.php | 4 ++-- phpseclib/Net/SFTP.php | 2 +- phpseclib/Net/SSH1.php | 6 +++--- phpseclib/Net/SSH2.php | 6 +++--- phpseclib/System/SSH/Agent.php | 4 ++-- 17 files changed, 37 insertions(+), 37 deletions(-) diff --git a/phpseclib/Crypt/AES.php b/phpseclib/Crypt/AES.php index 9cf78f10..26b1ae8f 100644 --- a/phpseclib/Crypt/AES.php +++ b/phpseclib/Crypt/AES.php @@ -19,7 +19,7 @@ * Here's a short example of how to use this library: * * * * * * * * createKey()); @@ -26,7 +26,7 @@ * Here's an example of how to create signatures and verify signatures with this library: * * createKey()); diff --git a/phpseclib/Crypt/Random.php b/phpseclib/Crypt/Random.php index ec69f2a8..02773931 100644 --- a/phpseclib/Crypt/Random.php +++ b/phpseclib/Crypt/Random.php @@ -8,7 +8,7 @@ * Here's a short example of how to use this library: * * diff --git a/phpseclib/Crypt/Rijndael.php b/phpseclib/Crypt/Rijndael.php index 8bf4581c..821547df 100644 --- a/phpseclib/Crypt/Rijndael.php +++ b/phpseclib/Crypt/Rijndael.php @@ -28,7 +28,7 @@ * Here's a short example of how to use this library: * * * * * * * * * * * * * * * * * * * login('username', 'password')) { diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index 4dd38805..002c8e6d 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -14,7 +14,7 @@ * Here's a short example of how to use this library: * * login('username', 'password')) { diff --git a/phpseclib/Net/SSH1.php b/phpseclib/Net/SSH1.php index 649668d7..15e659a9 100644 --- a/phpseclib/Net/SSH1.php +++ b/phpseclib/Net/SSH1.php @@ -8,7 +8,7 @@ * Here's a short example of how to use this library: * * login('username', 'password')) { @@ -22,7 +22,7 @@ * Here's another short example: * * login('username', 'password')) { @@ -704,7 +704,7 @@ class Net_SSH1 break; //case NET_SSH1_CIPHER_RC4: // if (!class_exists('Crypt_RC4')) { - // include_once('Crypt/RC4.php'); + // include_once 'Crypt/RC4.php'; // } // $this->crypto = new Crypt_RC4(); // $this->crypto->enableContinuousBuffer(); diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index efc4656b..6dd29ed1 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -8,7 +8,7 @@ * Here are some examples of how to use this library: * * login('username', 'password')) { @@ -22,8 +22,8 @@ * * * setPassword('whatever'); diff --git a/phpseclib/System/SSH/Agent.php b/phpseclib/System/SSH/Agent.php index 6d1882f0..ead905fa 100644 --- a/phpseclib/System/SSH/Agent.php +++ b/phpseclib/System/SSH/Agent.php @@ -7,8 +7,8 @@ * Here are some examples of how to use this library: * *