AsymmetricKey: error out on unsupported operations

This commit is contained in:
terrafrost 2023-02-05 17:44:51 -06:00
parent 508eaa7197
commit 2487192558
16 changed files with 48 additions and 13 deletions

View File

@ -136,6 +136,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

@ -81,6 +81,11 @@ abstract class DH extends AsymmetricKey
*/ */
public static function createParameters(...$args) 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(); $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, $length = 0) 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); $one = new BigInteger(1);
if ($length) { if ($length) {
$max = $one->bitwise_leftShift($length); $max = $one->bitwise_leftShift($length);
@ -387,9 +397,9 @@ abstract class DH extends AsymmetricKey
*/ */
public function getParameters() public function getParameters()
{ {
$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

@ -18,7 +18,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

@ -19,7 +19,7 @@ use phpseclib3\Crypt\DH;
* *
* @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

@ -19,7 +19,7 @@ use phpseclib3\Crypt\DH;
* *
* @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

@ -105,6 +105,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();
} }
@ -180,6 +185,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

@ -18,7 +18,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

@ -21,7 +21,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

@ -20,7 +20,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

@ -140,6 +140,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

@ -18,7 +18,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

@ -29,7 +29,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

@ -28,7 +28,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

@ -304,6 +304,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

@ -23,7 +23,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

@ -28,7 +28,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;