Ejemplo n.º 1
0
 /**
  * @param $keyName
  * @return bool|string
  * @throws \Exception
  */
 public function getKey($keyName)
 {
     if (!is_callable($this->privateKeyCallback)) {
         throw new \Exception("Missing private key callback");
     }
     $SafeController = new SafeController();
     $SafeController->setContainer($this->getContainer());
     $SecretController = new SecretController();
     $SecretController->setContainer($this->getContainer());
     $safe = $this->getSafe();
     if (is_string($safe)) {
         $safe = $SafeController->view($this->getSafe());
         if (!$safe) {
             throw new \Exception("Invalid Portunus safe");
         }
     }
     $secret = $SecretController->view($safe, $keyName);
     $callback = $this->privateKeyCallback;
     $privateKeyString = $callback($safe->getName());
     if (empty($privateKeyString)) {
         throw new \Exception("Invalid private key");
     }
     $PrivateKey = new PrivateKey();
     $PrivateKey->setKey($privateKeyString);
     try {
         $result = $secret->getValue($PrivateKey);
     } catch (\Exception $e) {
         $result = false;
     }
     return $result;
 }
Ejemplo n.º 2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $SafeController = new SafeController();
     $SecretController = new SecretController();
     $safeName = $input->getArgument('safe');
     if (empty($safeName)) {
         $safeNames = $SafeController->getSafeNames();
         $helper = $this->getHelper('question');
         $question = new ChoiceQuestion('<question>Please select the safe for this secret:</question> ', $safeNames);
         $safeName = $helper->ask($input, $output, $question);
     }
     if (empty($safeName)) {
         throw new \Exception("Invalid safe name");
     }
     $output->writeln(sprintf("<info>Using safe '%s'... </info>", $safeName));
     $safe = $SafeController->view($safeName);
     $keyName = $input->getArgument('key');
     if (empty($keyName)) {
         $keyNames = $SecretController->getKeys($safe);
         $helper = $this->getHelper('question');
         $question = new ChoiceQuestion('<question>Please select the key to decrypt:</question> ', $keyNames);
         $keyName = $helper->ask($input, $output, $question);
     }
     if (empty($keyName)) {
         throw new \Exception("Invalid key name");
     }
     $privateKey = $input->getArgument('privatekey');
     if (empty($privateKey) || !file_exists($privateKey)) {
         throw new \Exception("Invalid private key");
     }
     $output->writeln('');
     $output->write(sprintf("Decrypting secret '%s'... ", $keyName));
     $PrivateKey = new PrivateKey();
     $PrivateKey->setKey(file_get_contents($privateKey));
     try {
         $secret = $SecretController->view($safe, $keyName);
         $plainText = $secret->getValue($PrivateKey);
     } catch (\Exception $e) {
         $output->writeln('<error>FAILED</error>');
         $output->writeln('');
         $output->writeln('<error>' . $e->getMessage() . '</error>');
         return;
     }
     $output->writeln('<info>DONE</info>');
     $output->writeln('');
     $output->writeln(sprintf("<comment>'%s'</comment> = '%s'", $keyName, $plainText));
     $output->writeln('');
 }
Ejemplo n.º 3
0
 public function getValue(PrivateKey $privateKey = null)
 {
     $value = $this->value;
     if (!$privateKey) {
         return $value;
     }
     $chunkSize = $privateKey->getKeySize() / 8;
     $chunkCount = intval(ceil(strlen($value) / $chunkSize));
     $plainText = '';
     for ($i = 0; $i < $chunkCount; $i++) {
         $chunkData = substr($value, $i * $chunkSize, $chunkSize);
         $plainText .= $privateKey->decrypt($chunkData);
     }
     if (empty($plainText)) {
         throw new \Exception(sprintf("Error decrypting text - OpenSSL Error string '%s'", openssl_error_string()));
     }
     return $plainText;
 }