Beispiel #1
0
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  * @param QuestionHelper $helper
  * @param string $curveName
  * @param bool $useEncryption
  * @return array
  */
 public function generateKeyData(InputInterface $input, OutputInterface $output, QuestionHelper $helper, $curveName, $useEncryption)
 {
     if (!is_bool($useEncryption)) {
         throw new \InvalidArgumentException('useEncryption parameter must be a boolean');
     }
     /**
      * @var GeneratorPoint $generator
      */
     list(, $generator) = Curves::load($curveName);
     $key = $generator->createPrivateKey();
     if ($useEncryption) {
         $password = $this->promptForPassword($input, $output, $helper);
         $encrypted = new EncryptedPrivateKey($key, 'AES-128-CBC', random_bytes(16));
         $serializer = new EncryptedPrivateKeySerializer(new DerPrivateKeySerializer());
         $keyData = $serializer->serialize($encrypted, $password);
     } else {
         $serializer = new PemPrivateKeySerializer(new DerPrivateKeySerializer());
         $keyData = $serializer->serialize($key);
     }
     $adapter = EccFactory::getAdapter();
     $publicKey = $key->getPublicKey();
     $publicSerializer = new SshPublicKeySerializer($adapter, new UncompressedPointSerializer($adapter));
     $publicData = $publicSerializer->serialize($curveName, $publicKey);
     $localUser = posix_getpwuid(posix_geteuid());
     $localHost = gethostname();
     $publicData = sprintf("ecdsa-sha2-%s %s %s@%s\n", $curveName, $publicData, $localUser['name'], $localHost);
     return [$keyData, $publicData];
 }
    /**
     * @expectedException \RuntimeException
     * @expectedExceptionMessage Decryption failed
     */
    public function testDecryptionFailure()
    {
        $key = '-----BEGIN EC PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,ABABABABABABABABABABABABABABABAB

dGhpcyBpcyBpbnZhbGlkIGRhdGE=
-----END EC PRIVATE KEY-----';
        $adapter = EccFactory::getAdapter();
        $serializer = new EncryptedPrivateKeySerializer(new DerPrivateKeySerializer($adapter));
        $serializer->unserialize($key, 'password');
    }