. */ namespace SP\Core; defined('APP_ROOT') || die(); use phpseclib\Crypt\RSA; use SP\Core\Exceptions\FileNotFoundException; use SP\Core\Exceptions\SPException; use SP\Core\Traits\InjectableTrait; use SP\Log\Log; /** * Class CryptPKI para el manejo de las funciones para PKI * * @package SP */ class CryptPKI { use InjectableTrait; /** * @var RSA */ protected $rsa; /** * @throws SPException * @throws Dic\ContainerException */ public function __construct() { $this->injectDependencies(); if (!file_exists($this->getPublicKeyFile()) || !file_exists($this->getPrivateKeyFile())) { if (!$this->createKeys()) { throw new SPException(__('No es posible generar las claves RSA', false), SPException::CRITICAL); } } } /** * @param RSA $rsa */ public function inject(RSA $rsa) { $this->rsa = $rsa; } /** * Devuelve la ruta al archivo de la clave pública * * @return string */ private function getPublicKeyFile() { return APP_ROOT . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'pubkey.pem'; } /** * Devuelve la ruta al archivo de la clave privada * * @return string */ private function getPrivateKeyFile() { return APP_ROOT . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'key.pem'; } /** * Crea el par de claves pública y privada */ public function createKeys() { $keys = $this->rsa->createKey(1024); $priv = file_put_contents($this->getPrivateKeyFile(), $keys['privatekey']); $pub = file_put_contents($this->getPublicKeyFile(), $keys['publickey']); chmod($this->getPrivateKeyFile(), 0600); return ($priv && $pub); } /** * Encriptar datos con la clave pública * * @param string $data los datos a encriptar * @return string * @throws \SP\Core\Exceptions\FileNotFoundException */ public function encryptRSA($data) { $this->rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1); $this->rsa->loadKey($this->getPublicKey()); return $this->rsa->encrypt($data); } /** * Devuelve la clave pública desde el archivo * * @return string * @throws \SP\Core\Exceptions\FileNotFoundException */ public function getPublicKey() { $file = $this->getPublicKeyFile(); if (!file_exists($file)) { Log::writeNewLog(__FUNCTION__, __('El archivo de clave no existe', false), Log::NOTICE); throw new FileNotFoundException(SPException::ERROR, __('El archivo de clave no existe')); } return file_get_contents($file); } /** * Desencriptar datos cifrados con la clave pública * * @param string $data los datos a desencriptar * @return string * @throws \SP\Core\Exceptions\FileNotFoundException */ public function decryptRSA($data) { $this->rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1); $this->rsa->loadKey($this->getPrivateKey()); return $this->rsa->decrypt($data); } /** * Devuelve la clave privada desde el archivo * * @return string * @throws \SP\Core\Exceptions\FileNotFoundException */ private function getPrivateKey() { $file = $this->getPrivateKeyFile(); if (!file_exists($file)) { Log::writeNewLog(__FUNCTION__, __('El archivo de clave no existe', false), Log::NOTICE); throw new FileNotFoundException(SPException::ERROR, __('El archivo de clave no existe')); } return file_get_contents($file); } }