diff --git a/phpseclib/Crypt/Common/AsymmetricKey.php b/phpseclib/Crypt/Common/AsymmetricKey.php index a261dfc4..407f0369 100644 --- a/phpseclib/Crypt/Common/AsymmetricKey.php +++ b/phpseclib/Crypt/Common/AsymmetricKey.php @@ -136,6 +136,11 @@ abstract class AsymmetricKey { 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; foreach (self::$plugins[static::ALGORITHM]['Keys'] as $format) { if (isset(self::$invisiblePlugins[static::ALGORITHM]) && in_array($format, self::$invisiblePlugins[static::ALGORITHM])) { diff --git a/phpseclib/Crypt/DH.php b/phpseclib/Crypt/DH.php index 876cdbf8..e1deaf08 100644 --- a/phpseclib/Crypt/DH.php +++ b/phpseclib/Crypt/DH.php @@ -81,6 +81,11 @@ abstract class DH extends AsymmetricKey */ public static function createParameters(...$args) { + $class = new \ReflectionClass(static::class); + if ($class->isFinal()) { + throw new \RuntimeException('createParameters() should not be called from final classes (' . static::class . ')'); + } + $params = new Parameters(); if (count($args) == 2 && $args[0] instanceof BigInteger && $args[1] instanceof BigInteger) { //if (!$args[0]->isPrime()) { @@ -242,6 +247,11 @@ abstract class DH extends AsymmetricKey */ public static function createKey(Parameters $params, $length = 0) { + $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); if ($length) { $max = $one->bitwise_leftShift($length); @@ -387,9 +397,9 @@ abstract class DH extends AsymmetricKey */ public function getParameters() { - $type = self::validatePlugin('Keys', 'PKCS1', 'saveParameters'); + $type = DH::validatePlugin('Keys', 'PKCS1', 'saveParameters'); $key = $type::saveParameters($this->prime, $this->base); - return self::load($key, 'PKCS1'); + return DH::load($key, 'PKCS1'); } } diff --git a/phpseclib/Crypt/DH/Parameters.php b/phpseclib/Crypt/DH/Parameters.php index e4c15576..c0ded84c 100644 --- a/phpseclib/Crypt/DH/Parameters.php +++ b/phpseclib/Crypt/DH/Parameters.php @@ -18,7 +18,7 @@ use phpseclib3\Crypt\DH; * * @author Jim Wigginton */ -class Parameters extends DH +final class Parameters extends DH { /** * Returns the parameters diff --git a/phpseclib/Crypt/DH/PrivateKey.php b/phpseclib/Crypt/DH/PrivateKey.php index fe03452c..737781f8 100644 --- a/phpseclib/Crypt/DH/PrivateKey.php +++ b/phpseclib/Crypt/DH/PrivateKey.php @@ -19,7 +19,7 @@ use phpseclib3\Crypt\DH; * * @author Jim Wigginton */ -class PrivateKey extends DH +final class PrivateKey extends DH { use Common\Traits\PasswordProtected; diff --git a/phpseclib/Crypt/DH/PublicKey.php b/phpseclib/Crypt/DH/PublicKey.php index 3536360c..87726a5a 100644 --- a/phpseclib/Crypt/DH/PublicKey.php +++ b/phpseclib/Crypt/DH/PublicKey.php @@ -19,7 +19,7 @@ use phpseclib3\Crypt\DH; * * @author Jim Wigginton */ -class PublicKey extends DH +final class PublicKey extends DH { use Common\Traits\Fingerprint; diff --git a/phpseclib/Crypt/DSA.php b/phpseclib/Crypt/DSA.php index 1d265860..0123c66c 100644 --- a/phpseclib/Crypt/DSA.php +++ b/phpseclib/Crypt/DSA.php @@ -105,6 +105,11 @@ abstract class DSA extends AsymmetricKey { 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'])) { self::useBestEngine(); } @@ -180,6 +185,11 @@ abstract class DSA extends AsymmetricKey { 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'])) { self::useBestEngine(); } diff --git a/phpseclib/Crypt/DSA/Parameters.php b/phpseclib/Crypt/DSA/Parameters.php index 6bcb152d..84d16ba6 100644 --- a/phpseclib/Crypt/DSA/Parameters.php +++ b/phpseclib/Crypt/DSA/Parameters.php @@ -18,7 +18,7 @@ use phpseclib3\Crypt\DSA; * * @author Jim Wigginton */ -class Parameters extends DSA +final class Parameters extends DSA { /** * Returns the parameters diff --git a/phpseclib/Crypt/DSA/PrivateKey.php b/phpseclib/Crypt/DSA/PrivateKey.php index 7039941b..74d3e69e 100644 --- a/phpseclib/Crypt/DSA/PrivateKey.php +++ b/phpseclib/Crypt/DSA/PrivateKey.php @@ -21,7 +21,7 @@ use phpseclib3\Math\BigInteger; * * @author Jim Wigginton */ -class PrivateKey extends DSA implements Common\PrivateKey +final class PrivateKey extends DSA implements Common\PrivateKey { use Common\Traits\PasswordProtected; diff --git a/phpseclib/Crypt/DSA/PublicKey.php b/phpseclib/Crypt/DSA/PublicKey.php index 7e00e24a..c14ffbdf 100644 --- a/phpseclib/Crypt/DSA/PublicKey.php +++ b/phpseclib/Crypt/DSA/PublicKey.php @@ -20,7 +20,7 @@ use phpseclib3\Crypt\DSA\Formats\Signature\ASN1 as ASN1Signature; * * @author Jim Wigginton */ -class PublicKey extends DSA implements Common\PublicKey +final class PublicKey extends DSA implements Common\PublicKey { use Common\Traits\Fingerprint; diff --git a/phpseclib/Crypt/EC.php b/phpseclib/Crypt/EC.php index e61f0092..10b38254 100644 --- a/phpseclib/Crypt/EC.php +++ b/phpseclib/Crypt/EC.php @@ -140,6 +140,11 @@ abstract class EC extends AsymmetricKey { 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'])) { self::useBestEngine(); } diff --git a/phpseclib/Crypt/EC/Parameters.php b/phpseclib/Crypt/EC/Parameters.php index c9bf1bea..c0ed64a8 100644 --- a/phpseclib/Crypt/EC/Parameters.php +++ b/phpseclib/Crypt/EC/Parameters.php @@ -18,7 +18,7 @@ use phpseclib3\Crypt\EC; * * @author Jim Wigginton */ -class Parameters extends EC +final class Parameters extends EC { /** * Returns the parameters diff --git a/phpseclib/Crypt/EC/PrivateKey.php b/phpseclib/Crypt/EC/PrivateKey.php index 8bf86862..462ea1a3 100644 --- a/phpseclib/Crypt/EC/PrivateKey.php +++ b/phpseclib/Crypt/EC/PrivateKey.php @@ -29,7 +29,7 @@ use phpseclib3\Math\BigInteger; * * @author Jim Wigginton */ -class PrivateKey extends EC implements Common\PrivateKey +final class PrivateKey extends EC implements Common\PrivateKey { use Common\Traits\PasswordProtected; diff --git a/phpseclib/Crypt/EC/PublicKey.php b/phpseclib/Crypt/EC/PublicKey.php index 609d5960..4558ce34 100644 --- a/phpseclib/Crypt/EC/PublicKey.php +++ b/phpseclib/Crypt/EC/PublicKey.php @@ -28,7 +28,7 @@ use phpseclib3\Math\BigInteger; * * @author Jim Wigginton */ -class PublicKey extends EC implements Common\PublicKey +final class PublicKey extends EC implements Common\PublicKey { use Common\Traits\Fingerprint; diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 207a9051..7b935cc2 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -304,6 +304,11 @@ abstract class RSA extends AsymmetricKey { 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 if ($regSize > self::$smallestPrime) { $num_primes = floor($bits / self::$smallestPrime); diff --git a/phpseclib/Crypt/RSA/PrivateKey.php b/phpseclib/Crypt/RSA/PrivateKey.php index 84cede8e..37dd09a4 100644 --- a/phpseclib/Crypt/RSA/PrivateKey.php +++ b/phpseclib/Crypt/RSA/PrivateKey.php @@ -23,7 +23,7 @@ use phpseclib3\Math\BigInteger; * * @author Jim Wigginton */ -class PrivateKey extends RSA implements Common\PrivateKey +final class PrivateKey extends RSA implements Common\PrivateKey { use Common\Traits\PasswordProtected; diff --git a/phpseclib/Crypt/RSA/PublicKey.php b/phpseclib/Crypt/RSA/PublicKey.php index 89408792..58939a9f 100644 --- a/phpseclib/Crypt/RSA/PublicKey.php +++ b/phpseclib/Crypt/RSA/PublicKey.php @@ -28,7 +28,7 @@ use phpseclib3\Math\BigInteger; * * @author Jim Wigginton */ -class PublicKey extends RSA implements Common\PublicKey +final class PublicKey extends RSA implements Common\PublicKey { use Common\Traits\Fingerprint;