diff --git a/lib/SP/Core/Context/ContextBase.php b/lib/SP/Core/Context/ContextBase.php index df5b8c85..ba5d18c3 100644 --- a/lib/SP/Core/Context/ContextBase.php +++ b/lib/SP/Core/Context/ContextBase.php @@ -47,10 +47,8 @@ abstract class ContextBase implements ContextInterface /** * ContextBase constructor. - * - * @param ContextCollection $trasient */ - public function __construct(ContextCollection $trasient) + public function __construct() { $this->trasient = new ContextCollection(); } diff --git a/lib/SP/Core/Crypt/CryptPKI.php b/lib/SP/Core/Crypt/CryptPKI.php index 017b876d..8fb28f06 100644 --- a/lib/SP/Core/Crypt/CryptPKI.php +++ b/lib/SP/Core/Crypt/CryptPKI.php @@ -51,19 +51,27 @@ final class CryptPKI { $this->rsa = $rsa; - if (!file_exists($this->getPublicKeyFile()) || !file_exists($this->getPrivateKeyFile())) { - if (!$this->createKeys()) { - throw new SPException(__u('No es posible generar las claves RSA'), SPException::CRITICAL); - } + if (!$this->checkKeys()) { + $this->createKeys(); } } + /** + * Check if private and public keys exist + * + * @return bool + */ + public function checkKeys() + { + return file_exists($this->getPublicKeyFile()) && file_exists($this->getPrivateKeyFile()); + } + /** * Devuelve la ruta al archivo de la clave pública * * @return string */ - private function getPublicKeyFile() + public function getPublicKeyFile() { return CONFIG_PATH . DIRECTORY_SEPARATOR . 'pubkey.pem'; } @@ -73,13 +81,15 @@ final class CryptPKI * * @return string */ - private function getPrivateKeyFile() + public function getPrivateKeyFile() { return CONFIG_PATH . DIRECTORY_SEPARATOR . 'key.pem'; } /** * Crea el par de claves pública y privada + * + * @throws SPException */ public function createKeys() { @@ -88,9 +98,11 @@ final class CryptPKI $priv = file_put_contents($this->getPrivateKeyFile(), $keys['privatekey']); $pub = file_put_contents($this->getPublicKeyFile(), $keys['publickey']); - chmod($this->getPrivateKeyFile(), 0600); + if (!$priv || !$pub) { + throw new SPException(__u('No es posible generar las claves RSA'), SPException::CRITICAL); + } - return ($priv && $pub); + chmod($this->getPrivateKeyFile(), 0600); } /** @@ -148,7 +160,7 @@ final class CryptPKI * @return string * @throws \SP\Core\Exceptions\FileNotFoundException */ - private function getPrivateKey() + public function getPrivateKey() { $file = $this->getPrivateKeyFile(); diff --git a/test/SP/Core/Crypt/CryptPKITest.php b/test/SP/Core/Crypt/CryptPKITest.php new file mode 100644 index 00000000..ebe6fdac --- /dev/null +++ b/test/SP/Core/Crypt/CryptPKITest.php @@ -0,0 +1,150 @@ +. + */ + +namespace SP\Tests\SP\Core\Crypt; + +use phpseclib\Crypt\RSA; +use PHPUnit\Framework\TestCase; +use SP\Core\Crypt\CryptPKI; +use SP\Util\Util; + +/** + * Class CryptPKITest + * + * @package SP\Tests\SP\Core\Crypt + */ +class CryptPKITest extends TestCase +{ + /** + * @var CryptPKI + */ + private $cryptPki; + + /** + * @throws \SP\Core\Exceptions\FileNotFoundException + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + */ + public function testDecryptRSA() + { + $random = Util::generateRandomBytes(); + + $data = $this->cryptPki->encryptRSA($random); + + $this->assertNotEmpty($data); + + $this->assertEquals($random, $this->cryptPki->decryptRSA($data)); + + $this->assertFalse($this->cryptPki->decryptRSA('test123')); + } + + /** + * @throws \SP\Core\Exceptions\FileNotFoundException + */ + public function testGetPublicKey() + { + $key = $this->cryptPki->getPublicKey(); + + $this->assertNotEmpty($key); + + $this->assertRegExp('/^-----BEGIN PUBLIC KEY-----.*/', $key); + } + + /** + * @throws \SP\Core\Exceptions\FileNotFoundException + */ + public function testGetPrivateKey() + { + $key = $this->cryptPki->getPrivateKey(); + + $this->assertNotEmpty($key); + + $this->assertRegExp('/^-----BEGIN RSA PRIVATE KEY-----.*/', $key); + } + + /** + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + * @throws \SP\Core\Exceptions\FileNotFoundException + */ + public function testEncryptRSA() + { + $random = Util::generateRandomBytes(); + + $data = $this->cryptPki->encryptRSA($random); + + $this->assertNotEmpty($data); + + $this->assertEquals($random, $this->cryptPki->decryptRSA($data)); + + // Encrypt a long message + $random = Util::generateRandomBytes(128); + + $data = $this->cryptPki->encryptRSA($random); + + $this->assertNotEmpty($data); + + $this->assertEquals($random, $this->cryptPki->decryptRSA($data)); + } + + /** + * @throws \SP\Core\Exceptions\SPException + */ + public function testCreateKeys() + { + $this->cryptPki->createKeys(); + + $this->assertFileExists($this->cryptPki->getPublicKeyFile()); + $this->assertFileExists($this->cryptPki->getPrivateKeyFile()); + } + + /** + * testCheckKeys + */ + public function testCheckKeys() + { + $this->assertTrue($this->cryptPki->checkKeys()); + } + + /** + * Sets up the fixture, for example, open a network connection. + * This method is called before a test is executed. + * + * @throws \SP\Core\Exceptions\SPException + */ + protected function setUp() + { + $this->cryptPki = new CryptPKI(new RSA()); + } + + /** + * Tears down the fixture, for example, close a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + unlink($this->cryptPki->getPublicKeyFile()); + unlink($this->cryptPki->getPrivateKeyFile()); + } + + +}