RSA: setEngine -> setPreferredEngine (make it like SymmetricKey)

This commit is contained in:
terrafrost 2016-12-11 09:44:37 -06:00
parent 242e0dcb7f
commit 9ae5206588
2 changed files with 51 additions and 19 deletions

View File

@ -422,16 +422,58 @@ class RSA
} }
/** /**
* Sets the engine * Tests engine validity
*
* Only used in the constructor. Valid values are RSA::ENGINE_OPENSSL and RSA::ENGINE_INTERNAL
* *
* @access public * @access public
* @param int $val * @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(); self::initialize_static_variables();
if (!isset(self::$engine)) { if (!isset(self::$engine)) {
switch (true) { self::setPreferredEngine(self::ENGINE_OPENSSL);
// 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;
}
} }
// OpenSSL uses 65537 as the exponent and requires RSA keys be 384 bits minimum // OpenSSL uses 65537 as the exponent and requires RSA keys be 384 bits minimum

View File

@ -38,7 +38,7 @@ class Unit_Crypt_RSA_CreateKeyTest extends PhpseclibTestCase
public function testMultiPrime() public function testMultiPrime()
{ {
RSA::setEngine(RSA::ENGINE_INTERNAL); RSA::setPreferredEngine(RSA::ENGINE_INTERNAL);
RSA::setSmallestPrime(256); RSA::setSmallestPrime(256);
extract(RSA::createKey(1024)); extract(RSA::createKey(1024));
$this->assertInstanceOf('\phpseclib\Crypt\RSA', $privatekey); $this->assertInstanceOf('\phpseclib\Crypt\RSA', $privatekey);
@ -61,5 +61,7 @@ class Unit_Crypt_RSA_CreateKeyTest extends PhpseclibTestCase
$signature = $rsa->sign('zzz'); $signature = $rsa->sign('zzz');
$rsa->load($rsa->getPublicKey()); $rsa->load($rsa->getPublicKey());
$this->assertTrue($rsa->verify('zzz', $signature)); $this->assertTrue($rsa->verify('zzz', $signature));
RSA::setPreferredEngine(RSA::ENGINE_OPENSSL);
} }
} }