diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 51bf5531..80636433 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -422,16 +422,58 @@ class RSA } /** - * Sets the engine - * - * Only used in the constructor. Valid values are RSA::ENGINE_OPENSSL and RSA::ENGINE_INTERNAL + * Tests engine validity * * @access public * @param int $val */ - public static function setEngine($val) + public static function isValidEngine($val) { - self::$engine = $val; + switch ($val) { + case self::ENGINE_OPENSSL: + return extension_loaded('openssl') && file_exists(self::$configFile); + case self::ENGINE_INTERNAL: + return true; + } + + return false; + } + + /** + * Sets the engine + * + * Only used in RSA::createKey. Valid values are RSA::ENGINE_OPENSSL and RSA::ENGINE_INTERNAL + * + * @access public + * @param int $val + */ + public static function setPreferredEngine($val) + { + self::$engine = null; + $candidateEngines = [ + $val, + self::ENGINE_OPENSSL + ]; + foreach ($candidateEngines as $engine) { + if (self::isValidEngine($engine)) { + self::$engine = $engine; + break; + } + } + if (!isset(self::$engine)) { + self::$engine = self::ENGINE_INTERNAL; + } + } + + /** + * Returns the engine + * + * @access public + * @return int + */ + public static function getEngine($val) + { + return self::$engine; } /** @@ -453,19 +495,7 @@ class RSA self::initialize_static_variables(); if (!isset(self::$engine)) { - switch (true) { - // Math/BigInteger's openssl requirements are a little less stringent than Crypt/RSA's. in particular, - // Math/BigInteger doesn't require an openssl.cfg file whereas Crypt/RSA does. so if Math/BigInteger - // can't use OpenSSL it can be pretty trivially assumed, then, that Crypt/RSA can't either. - case defined('MATH_BIGINTEGER_OPENSSL_DISABLE'): - self::$engine = self::ENGINE_INTERNAL; - break; - case extension_loaded('openssl') && file_exists(self::$configFile): - self::$engine = self::ENGINE_OPENSSL; - break; - default: - self::$engine = self::ENGINE_INTERNAL; - } + self::setPreferredEngine(self::ENGINE_OPENSSL); } // OpenSSL uses 65537 as the exponent and requires RSA keys be 384 bits minimum diff --git a/tests/Unit/Crypt/RSA/CreateKeyTest.php b/tests/Unit/Crypt/RSA/CreateKeyTest.php index 67ed2d90..057ebc21 100644 --- a/tests/Unit/Crypt/RSA/CreateKeyTest.php +++ b/tests/Unit/Crypt/RSA/CreateKeyTest.php @@ -38,7 +38,7 @@ class Unit_Crypt_RSA_CreateKeyTest extends PhpseclibTestCase public function testMultiPrime() { - RSA::setEngine(RSA::ENGINE_INTERNAL); + RSA::setPreferredEngine(RSA::ENGINE_INTERNAL); RSA::setSmallestPrime(256); extract(RSA::createKey(1024)); $this->assertInstanceOf('\phpseclib\Crypt\RSA', $privatekey); @@ -61,5 +61,7 @@ class Unit_Crypt_RSA_CreateKeyTest extends PhpseclibTestCase $signature = $rsa->sign('zzz'); $rsa->load($rsa->getPublicKey()); $this->assertTrue($rsa->verify('zzz', $signature)); + + RSA::setPreferredEngine(RSA::ENGINE_OPENSSL); } }