Exemplo n.º 1
0
    /**
     * Generate keys
     *
     * @param  array $options
     * @return ArrayObject
     * @throws Rsa\Exception\RuntimeException
     */
    public function generateKeys(array $options = null)
    {
        $config = array(
            'private_key_bits' => self::DEFAULT_KEY_SIZE,
            'private_key_type' => OPENSSL_KEYTYPE_RSA
        );

        if (isset($options['pass_phrase'])) {
            $passPhrase = $options['pass_phrase'];
        } else {
            $passPhrase = $this->options->getPassPhrase();
        }

        if (isset($options['private_key_bits'])) {
            $config['private_key_bits'] = $options['private_key_bits'];
        }

        // generate
        $privateKey = null;
        $publicKey  = null;
        $resource   = openssl_pkey_new($config);
        $result     = openssl_pkey_export($resource, $private, $passPhrase);
        if (false === $result) {
            throw new Exception\RuntimeException(
                'Can not export key; openssl ' . openssl_error_string()
            );
        }

        $privateKey = new Rsa\PrivateKey($private, $passPhrase);
        $details    = openssl_pkey_get_details($resource);
        $publicKey  = new Rsa\PublicKey($details['key']);

        return new ArrayObject(array(
            'privateKey' => $privateKey,
            'publicKey' => $publicKey
        ), ArrayObject::ARRAY_AS_PROPS);
    }
Exemplo n.º 2
0
 /**
  * Generate new private/public key pair
  * @see RsaOptions::generateKeys()
  *
  * @param  array $opensslConfig
  * @return Rsa
  * @throws Rsa\Exception\RuntimeException
  */
 public function generateKeys(array $opensslConfig = array())
 {
     $this->options->generateKeys($opensslConfig);
     return $this;
 }
Exemplo n.º 3
0
 public function testRsaLoadsPassphrasedKeys()
 {
     $rsaOptions = new RsaOptions(array('pass_phrase' => '0987654321'));
     $rsaOptions->generateKeys(array('config' => $this->userOpenSslConf, 'private_key_bits' => 512));
     Rsa::factory(array('pass_phrase' => '0987654321', 'private_key' => $rsaOptions->getPrivateKey()->toString()));
 }
 /**
  * Controller Action for generation of a new key pair
  * @return ViewModel
  */
 public function generateAction()
 {
     $view = new ViewModel();
     $view->form = $this->getGenerateForm();
     $view->error = false;
     $redirectUrl = $this->url()->fromRoute(static::ROUTE_GEN_KEYS);
     $prg = $this->prg($redirectUrl, true);
     if ($prg instanceof Response) {
         return $prg;
     }
     if ($prg === false) {
         return $view;
     }
     $view->form->setData($prg);
     if (!$view->form->isValid()) {
         $view->error = true;
         return $view;
     }
     $post = $view->form->getData();
     $keys = $this->getKeyStorage();
     try {
         $rsaOptions = array();
         if (!empty($post['keyPassPhrase'])) {
             $rsaOptions['passPhrase'] = $post['keyPassPhrase'];
         }
         $rsaOptions['binaryOutput'] = $post['outputType'] === 'binary';
         $rsaOptions['hashAlgorithm'] = $post['digestAlgo'];
         $rsaOptions = new RsaOptions($rsaOptions);
         $rsaOptions->generateKeys(array('private_key_bits' => $post['keySize']));
         $rsa = new Rsa($rsaOptions);
         $keys->set($rsa, $post['keyName']);
     } catch (Exception $e) {
         $view->error = true;
         $message = 'Failed to Create Key Pair: ' . $e->getMessage();
         $form->setMessages(array('keyName' => $message));
         return $view;
     }
     $this->flashMessenger()->addSuccessMessage('New RSA Key Pair Created Successfully');
     return $this->redirect()->toRoute(static::ROUTE_HOME);
 }
Exemplo n.º 5
0
 public function testRsaLoadsPassphrasedKeys()
 {
     $rsaOptions = new RsaOptions(array('pass_phrase' => '0987654321'));
     $rsaOptions->generateKeys(array('config' => $this->userOpenSslConf, 'private_key_bits' => 512));
     try {
         $rsa = Rsa::factory(array('pass_phrase' => '0987654321', 'private_key' => $rsaOptions->getPrivateKey()->toString()));
     } catch (Exception\RuntimeException $e) {
         $this->fail('Passphrase loading of a private key failed');
     }
 }