Merge branch '3.0'

This commit is contained in:
terrafrost 2023-02-05 23:47:08 -06:00
commit ca2c9588ea
22 changed files with 127 additions and 52 deletions

View File

@ -130,6 +130,11 @@ abstract class AsymmetricKey
{ {
self::initialize_static_variables(); self::initialize_static_variables();
$class = new \ReflectionClass(static::class);
if ($class->isFinal()) {
throw new \RuntimeException('load() should not be called from final classes (' . static::class . ')');
}
$components = false; $components = false;
foreach (self::$plugins[static::ALGORITHM]['Keys'] as $format) { foreach (self::$plugins[static::ALGORITHM]['Keys'] as $format) {
if (isset(self::$invisiblePlugins[static::ALGORITHM]) && in_array($format, self::$invisiblePlugins[static::ALGORITHM])) { if (isset(self::$invisiblePlugins[static::ALGORITHM]) && in_array($format, self::$invisiblePlugins[static::ALGORITHM])) {

View File

@ -82,6 +82,11 @@ abstract class DH extends AsymmetricKey
*/ */
public static function createParameters(...$args): Parameters public static function createParameters(...$args): Parameters
{ {
$class = new \ReflectionClass(static::class);
if ($class->isFinal()) {
throw new \RuntimeException('createParameters() should not be called from final classes (' . static::class . ')');
}
$params = new Parameters(); $params = new Parameters();
if (count($args) == 2 && $args[0] instanceof BigInteger && $args[1] instanceof BigInteger) { if (count($args) == 2 && $args[0] instanceof BigInteger && $args[1] instanceof BigInteger) {
//if (!$args[0]->isPrime()) { //if (!$args[0]->isPrime()) {
@ -242,6 +247,11 @@ abstract class DH extends AsymmetricKey
*/ */
public static function createKey(Parameters $params, int $length = 0): PrivateKey public static function createKey(Parameters $params, int $length = 0): PrivateKey
{ {
$class = new \ReflectionClass(static::class);
if ($class->isFinal()) {
throw new \RuntimeException('createKey() should not be called from final classes (' . static::class . ')');
}
$one = new BigInteger(1); $one = new BigInteger(1);
if ($length) { if ($length) {
$max = $one->bitwise_leftShift($length); $max = $one->bitwise_leftShift($length);
@ -380,9 +390,9 @@ abstract class DH extends AsymmetricKey
*/ */
public function getParameters(): AsymmetricKey public function getParameters(): AsymmetricKey
{ {
$type = self::validatePlugin('Keys', 'PKCS1', 'saveParameters'); $type = DH::validatePlugin('Keys', 'PKCS1', 'saveParameters');
$key = $type::saveParameters($this->prime, $this->base); $key = $type::saveParameters($this->prime, $this->base);
return self::load($key, 'PKCS1'); return DH::load($key, 'PKCS1');
} }
} }

View File

@ -20,7 +20,7 @@ use phpseclib3\Crypt\DH;
* *
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
*/ */
class Parameters extends DH final class Parameters extends DH
{ {
/** /**
* Returns the parameters * Returns the parameters

View File

@ -22,7 +22,7 @@ use phpseclib3\Math\BigInteger;
* *
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
*/ */
class PrivateKey extends DH final class PrivateKey extends DH
{ {
use Common\Traits\PasswordProtected; use Common\Traits\PasswordProtected;

View File

@ -22,7 +22,7 @@ use phpseclib3\Math\BigInteger;
* *
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
*/ */
class PublicKey extends DH final class PublicKey extends DH
{ {
use Common\Traits\Fingerprint; use Common\Traits\Fingerprint;

View File

@ -106,6 +106,11 @@ abstract class DSA extends AsymmetricKey
{ {
self::initialize_static_variables(); self::initialize_static_variables();
$class = new \ReflectionClass(static::class);
if ($class->isFinal()) {
throw new \RuntimeException('createParameters() should not be called from final classes (' . static::class . ')');
}
if (!isset(self::$engines['PHP'])) { if (!isset(self::$engines['PHP'])) {
self::useBestEngine(); self::useBestEngine();
} }
@ -181,6 +186,11 @@ abstract class DSA extends AsymmetricKey
{ {
self::initialize_static_variables(); self::initialize_static_variables();
$class = new \ReflectionClass(static::class);
if ($class->isFinal()) {
throw new \RuntimeException('createKey() should not be called from final classes (' . static::class . ')');
}
if (!isset(self::$engines['PHP'])) { if (!isset(self::$engines['PHP'])) {
self::useBestEngine(); self::useBestEngine();
} }

View File

@ -20,7 +20,7 @@ use phpseclib3\Crypt\DSA;
* *
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
*/ */
class Parameters extends DSA final class Parameters extends DSA
{ {
/** /**
* Returns the parameters * Returns the parameters

View File

@ -23,7 +23,7 @@ use phpseclib3\Math\BigInteger;
* *
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
*/ */
class PrivateKey extends DSA implements Common\PrivateKey final class PrivateKey extends DSA implements Common\PrivateKey
{ {
use Common\Traits\PasswordProtected; use Common\Traits\PasswordProtected;

View File

@ -22,7 +22,7 @@ use phpseclib3\Crypt\DSA\Formats\Signature\ASN1 as ASN1Signature;
* *
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
*/ */
class PublicKey extends DSA implements Common\PublicKey final class PublicKey extends DSA implements Common\PublicKey
{ {
use Common\Traits\Fingerprint; use Common\Traits\Fingerprint;

View File

@ -142,6 +142,11 @@ abstract class EC extends AsymmetricKey
{ {
self::initialize_static_variables(); self::initialize_static_variables();
$class = new \ReflectionClass(static::class);
if ($class->isFinal()) {
throw new \RuntimeException('createKey() should not be called from final classes (' . static::class . ')');
}
if (!isset(self::$engines['PHP'])) { if (!isset(self::$engines['PHP'])) {
self::useBestEngine(); self::useBestEngine();
} }

View File

@ -20,7 +20,7 @@ use phpseclib3\Crypt\EC;
* *
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
*/ */
class Parameters extends EC final class Parameters extends EC
{ {
/** /**
* Returns the parameters * Returns the parameters

View File

@ -32,7 +32,7 @@ use phpseclib3\Math\BigInteger;
* *
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
*/ */
class PrivateKey extends EC implements Common\PrivateKey final class PrivateKey extends EC implements Common\PrivateKey
{ {
use Common\Traits\PasswordProtected; use Common\Traits\PasswordProtected;

View File

@ -30,7 +30,7 @@ use phpseclib3\Math\BigInteger;
* *
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
*/ */
class PublicKey extends EC implements Common\PublicKey final class PublicKey extends EC implements Common\PublicKey
{ {
use Common\Traits\Fingerprint; use Common\Traits\Fingerprint;

View File

@ -299,6 +299,11 @@ abstract class RSA extends AsymmetricKey
{ {
self::initialize_static_variables(); self::initialize_static_variables();
$class = new \ReflectionClass(static::class);
if ($class->isFinal()) {
throw new \RuntimeException('createKey() should not be called from final classes (' . static::class . ')');
}
$regSize = $bits >> 1; // divide by two to see how many bits P and Q would be $regSize = $bits >> 1; // divide by two to see how many bits P and Q would be
if ($regSize > self::$smallestPrime) { if ($regSize > self::$smallestPrime) {
$num_primes = floor($bits / self::$smallestPrime); $num_primes = floor($bits / self::$smallestPrime);

View File

@ -28,7 +28,7 @@ use phpseclib3\Math\BigInteger;
* *
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
*/ */
class PrivateKey extends RSA implements Common\PrivateKey final class PrivateKey extends RSA implements Common\PrivateKey
{ {
use Common\Traits\PasswordProtected; use Common\Traits\PasswordProtected;

View File

@ -32,7 +32,7 @@ use phpseclib3\Math\BigInteger;
* *
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
*/ */
class PublicKey extends RSA implements Common\PublicKey final class PublicKey extends RSA implements Common\PublicKey
{ {
use Common\Traits\Fingerprint; use Common\Traits\Fingerprint;

View File

@ -110,6 +110,7 @@ class SSH2Test extends PhpseclibFunctionalTestCase
/** /**
* @depends testPasswordLogin * @depends testPasswordLogin
* @group github280 * @group github280
* @requires PHPUnit < 10
*/ */
public function testExecWithMethodCallback(SSH2 $ssh): SSH2 public function testExecWithMethodCallback(SSH2 $ssh): SSH2
{ {

View File

@ -99,66 +99,36 @@ abstract class PhpseclibTestCase extends TestCase
// assertIsArray was not introduced until PHPUnit 8 // assertIsArray was not introduced until PHPUnit 8
public static function assertIsArray($actual, string $message = ''): void public static function assertIsArray($actual, string $message = ''): void
{ {
if (method_exists(parent::class, 'assertIsArray')) {
parent::assertIsArray($actual, $message);
return;
}
parent::assertInternalType('array', $actual, $message); parent::assertInternalType('array', $actual, $message);
} }
// assertIsString was not introduced until PHPUnit 8 // assertIsString was not introduced until PHPUnit 8
public static function assertIsString($actual, string $message = ''): void public static function assertIsString($actual, string $message = ''): void
{ {
if (method_exists(parent::class, 'assertIsString')) {
parent::assertIsString($actual, $message);
return;
}
parent::assertInternalType('string', $actual, $message); parent::assertInternalType('string', $actual, $message);
} }
// assertIsResource was not introduced until PHPUnit 8 // assertIsResource was not introduced until PHPUnit 8
public static function assertIsResource($actual, string $message = ''): void public static function assertIsResource($actual, string $message = ''): void
{ {
if (method_exists(parent::class, 'assertIsResource')) {
parent::assertIsResource($actual, $message);
return;
}
parent::assertInternalType('resource', $actual, $message); parent::assertInternalType('resource', $actual, $message);
} }
// assertIsObject was not introduced until PHPUnit 8 // assertIsObject was not introduced until PHPUnit 8
public static function assertIsObject($actual, string $message = ''): void public static function assertIsObject($actual, string $message = ''): void
{ {
if (method_exists(parent::class, 'assertIsObject')) {
parent::assertIsObject($actual, $message);
return;
}
parent::assertInternalType('object', $actual, $message); parent::assertInternalType('object', $actual, $message);
} }
// assertContains is deprecated for strings in PHPUnit 8 // assertContains is deprecated for strings in PHPUnit 8
public static function assertStringContainsString(string $needle, string $haystack, string $message = ''): void public static function assertStringContainsString(string $needle, string $haystack, string $message = ''): void
{ {
if (method_exists(parent::class, 'assertStringContainsString')) {
parent::assertStringContainsString($needle, $haystack, $message);
return;
}
parent::assertContains($needle, $haystack, $message); parent::assertContains($needle, $haystack, $message);
} }
// assertNotContains is deprecated for strings in PHPUnit 8 // assertNotContains is deprecated for strings in PHPUnit 8
public static function assertStringNotContainsString(string $needle, string $haystack, string $message = ''): void public static function assertStringNotContainsString(string $needle, string $haystack, string $message = ''): void
{ {
if (method_exists(parent::class, 'assertStringContainsString')) {
parent::assertStringNotContainsString($needle, $haystack, $message);
return;
}
parent::assertNotContains($needle, $haystack, $message); parent::assertNotContains($needle, $haystack, $message);
} }
@ -167,10 +137,6 @@ abstract class PhpseclibTestCase extends TestCase
*/ */
public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
{ {
if (method_exists(parent::class, 'assertMatchesRegularExpression')) { parent::assertRegExp($pattern, $string, $message);
parent::assertMatchesRegularExpression($pattern, $string, $message);
} else {
parent::assertRegExp($pattern, $string, $message);
}
} }
} }

View File

@ -171,7 +171,7 @@ class HashTest extends PhpseclibTestCase
} }
/** /**
* @dataProvider hmacData() * @dataProvider hmacData
*/ */
public function testHMAC($hash, $key, $message, $result): void public function testHMAC($hash, $key, $message, $result): void
{ {
@ -179,7 +179,7 @@ class HashTest extends PhpseclibTestCase
} }
/** /**
* @dataProvider hmacData() * @dataProvider hmacData
*/ */
public function testHMAC96($hash, $key, $message, $result): void public function testHMAC96($hash, $key, $message, $result): void
{ {
@ -372,7 +372,7 @@ class HashTest extends PhpseclibTestCase
} }
/** /**
* @dataProvider hashData() * @dataProvider hashData
*/ */
public function testHash($hash, $message, $result): void public function testHash($hash, $message, $result): void
{ {
@ -380,7 +380,7 @@ class HashTest extends PhpseclibTestCase
} }
/** /**
* @dataProvider hashData() * @dataProvider hashData
*/ */
public function testHash96($hash, $message, $result): void public function testHash96($hash, $message, $result): void
{ {

View File

@ -34,6 +34,7 @@ class SSH2UnitTest extends PhpseclibTestCase
/** /**
* @dataProvider formatLogDataProvider * @dataProvider formatLogDataProvider
* @requires PHPUnit < 10
*/ */
public function testFormatLog(array $message_log, array $message_number_log, $expected): void public function testFormatLog(array $message_log, array $message_number_log, $expected): void
{ {
@ -43,6 +44,9 @@ class SSH2UnitTest extends PhpseclibTestCase
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/**
* @requires PHPUnit < 10
*/
public function testGenerateIdentifier(): void public function testGenerateIdentifier(): void
{ {
$identifier = self::callFunc($this->createSSHMock(), 'generate_identifier'); $identifier = self::callFunc($this->createSSHMock(), 'generate_identifier');
@ -70,6 +74,9 @@ class SSH2UnitTest extends PhpseclibTestCase
} }
} }
/**
* @requires PHPUnit < 10
*/
public function testGetExitStatusIfNotConnected(): void public function testGetExitStatusIfNotConnected(): void
{ {
$ssh = $this->createSSHMock(); $ssh = $this->createSSHMock();
@ -77,12 +84,18 @@ class SSH2UnitTest extends PhpseclibTestCase
$this->assertFalse($ssh->getExitStatus()); $this->assertFalse($ssh->getExitStatus());
} }
/**
* @requires PHPUnit < 10
*/
public function testPTYIDefaultValue(): void public function testPTYIDefaultValue(): void
{ {
$ssh = $this->createSSHMock(); $ssh = $this->createSSHMock();
$this->assertFalse($ssh->isPTYEnabled()); $this->assertFalse($ssh->isPTYEnabled());
} }
/**
* @requires PHPUnit < 10
*/
public function testEnablePTY(): void public function testEnablePTY(): void
{ {
$ssh = $this->createSSHMock(); $ssh = $this->createSSHMock();
@ -94,6 +107,9 @@ class SSH2UnitTest extends PhpseclibTestCase
$this->assertFalse($ssh->isPTYEnabled()); $this->assertFalse($ssh->isPTYEnabled());
} }
/**
* @requires PHPUnit < 10
*/
public function testQuietModeDefaultValue(): void public function testQuietModeDefaultValue(): void
{ {
$ssh = $this->createSSHMock(); $ssh = $this->createSSHMock();
@ -101,6 +117,9 @@ class SSH2UnitTest extends PhpseclibTestCase
$this->assertFalse($ssh->isQuietModeEnabled()); $this->assertFalse($ssh->isQuietModeEnabled());
} }
/**
* @requires PHPUnit < 10
*/
public function testEnableQuietMode(): void public function testEnableQuietMode(): void
{ {
$ssh = $this->createSSHMock(); $ssh = $this->createSSHMock();

View File

@ -0,0 +1,31 @@
<?php
/** @var iterable<SplFileInfo> $files */
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__));
foreach ($files as $file) {
if ($file->getExtension() === 'php' && $file->getPathname() !== __FILE__) {
$fileContents = file_get_contents($file->getPathname());
if ($fileContents === false) {
throw new \RuntimeException('file_get_contents() failed: ' . $file->getPathname());
}
$patternToReplacementMap = [
'~ function setUpBeforeClass\(\)~' => ' function setUpBeforeClass(): void',
'~ function setUp\(\)~' => ' function setUp(): void',
'~ function tearDown\(\)~' => ' function tearDown(): void',
'~ function assertIsArray\(\$actual, \$message = \'\'\)~' => ' function _assertIsArray($actual, string $message = \'\')',
'~ function assertIsResource\(\$actual, \$message = \'\'\)~' => ' function _assertIsResource($actual, string $message = \'\')',
'~ function assertIsObject\(\$actual, \$message = \'\'\)~' => ' function _assertIsObject($actual, string $message = \'\')',
'~ function assertIsString\(\$actual, \$message = \'\'\)~' => ' function _assertIsString($actual, string $message = \'\')',
'~ function assertStringContainsString\(\$needle, \$haystack, \$message = \'\'\)~' => ' function _assertStringContainsString(string $needle, string $haystack, string $message = \'\')',
'~ function assertStringNotContainsString\(\$needle, \$haystack, \$message = \'\'\)~' => ' function _assertStringNotContainsString(string $needle, string $haystack, string $message = \'\')'
];
$updatedFileContents = preg_replace(
array_keys($patternToReplacementMap),
array_values($patternToReplacementMap),
$fileContents
);
if (file_put_contents($file->getPathname(), $updatedFileContents) === false) {
throw new \RuntimeException('file_put_contents() failed: ' . $file->getPathname());
}
}
}

View File

@ -0,0 +1,23 @@
<?php
/** @var iterable<SplFileInfo> $files */
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__));
foreach ($files as $file) {
if ($file->getExtension() === 'php' && $file->getPathname() !== __FILE__) {
$fileContents = file_get_contents($file->getPathname());
if ($fileContents === false) {
throw new \RuntimeException('file_get_contents() failed: ' . $file->getPathname());
}
$patternToReplacementMap = [
'~ function assertMatchesRegularExpression\(\$pattern, \$string, \$message = \'\'\)~' => ' function _assertMatchesRegularExpression(string $pattern, string $string, string $message = \'\')',
];
$updatedFileContents = preg_replace(
array_keys($patternToReplacementMap),
array_values($patternToReplacementMap),
$fileContents
);
if (file_put_contents($file->getPathname(), $updatedFileContents) === false) {
throw new \RuntimeException('file_put_contents() failed: ' . $file->getPathname());
}
}
}