You are here

class OpenSSL in DRD Agent 8.3

Same name and namespace in other branches
  1. 4.0.x src/Crypt/Method/OpenSSL.php \Drupal\drd_agent\Crypt\Method\OpenSSL

Provides OpenSSL encryption functionality.

Hierarchy

Expanded class hierarchy of OpenSSL

2 string references to 'OpenSSL'
Base::getMethods in src/Crypt/Base.php
Get a list of crypt methods, either just their ids or instances of each.
OpenSSL::getLabel in src/Crypt/Method/OpenSSL.php
Get the crypt method label.

File

src/Crypt/Method/OpenSSL.php, line 14

Namespace

Drupal\drd_agent\Crypt\Method
View source
class OpenSSL extends BaseMethod {
  private $cipher;
  private $iv;
  private $password;
  private $supportedCipher = [
    'aes-256-ctr' => 32,
    'aes-128-cbc' => 16,
  ];

  /**
   * {@inheritdoc}
   */
  public function __construct(ContainerInterface $container, array $settings = []) {
    parent::__construct($container);
    $this->cipher = $settings['cipher'] ?? '';
    $this->password = $settings['password'] ?? '';
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() : string {
    return 'OpenSSL';
  }

  /**
   * {@inheritdoc}
   */
  public function getCipher() : string {
    return $this->cipher;
  }

  /**
   * {@inheritdoc}
   */
  public function getPassword() : string {
    return base64_decode($this->password);
  }

  /**
   * {@inheritdoc}
   */
  public function isAvailable() : bool {
    return function_exists('openssl_encrypt');
  }

  /**
   * {@inheritdoc}
   */
  public function getCipherMethods() : array {
    $result = [];
    $available = openssl_get_cipher_methods();
    foreach ($this->supportedCipher as $cipher => $keyLength) {
      if (in_array($cipher, $available, TRUE)) {
        $result[$cipher] = $cipher;
      }
    }
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function getIv() : string {
    if (empty($this->iv)) {
      $nonceSize = openssl_cipher_iv_length($this->cipher);
      $strong = TRUE;

      /** @noinspection CryptographicallySecureRandomnessInspection */
      $this->iv = openssl_random_pseudo_bytes($nonceSize, $strong);
      if ($strong === FALSE || $this->iv === FALSE) {
        $this->logger
          ->warning('Your systm does not produce secure randomness.');
      }
    }
    return $this->iv;
  }

  /**
   * {@inheritdoc}
   */
  public function encrypt(array $args) : string {
    return empty($this->password) ? '' : openssl_encrypt(serialize($args), $this->cipher, $this
      ->getPassword(), OPENSSL_RAW_DATA, $this
      ->getIv());
  }

  /**
   * {@inheritdoc}
   */
  public function decrypt($body, $iv) {
    $this->iv = $iv;

    /** @noinspection UnserializeExploitsInspection */
    return unserialize(openssl_decrypt($body, $this->cipher, $this
      ->getPassword(), OPENSSL_RAW_DATA, $this->iv));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseMethod::$container protected property
BaseMethod::$logger protected property
BaseMethod::cryptFileExecute private function Callback to encrypt and decrypt files.
BaseMethod::encryptFile public function Encrypt a file. Overrides BaseMethodInterface::encryptFile
OpenSSL::$cipher private property
OpenSSL::$iv private property
OpenSSL::$password private property
OpenSSL::$supportedCipher private property
OpenSSL::decrypt public function Decode, decrypt and unserialize arguments from the other end. Overrides BaseMethodInterface::decrypt
OpenSSL::encrypt public function Encrypt and encode any list of arguments. Overrides BaseMethodInterface::encrypt
OpenSSL::getCipher public function Get the selected cipher. Overrides BaseMethodInterface::getCipher
OpenSSL::getCipherMethods public function Get a list of available cipher methods. Overrides BaseMethodInterface::getCipherMethods
OpenSSL::getIv public function Get an initialiation vector. Overrides BaseMethodInterface::getIv
OpenSSL::getLabel public function Get the crypt method label. Overrides BaseMethodInterface::getLabel
OpenSSL::getPassword public function Get the password. Overrides BaseMethodInterface::getPassword
OpenSSL::isAvailable public function Find out if the crypt method is available. Overrides BaseMethodInterface::isAvailable
OpenSSL::__construct public function BaseMethod constructor. Overrides BaseMethod::__construct