Exemplo n.º 1
0
 /**
  * Gets the available keys in the keyring
  *
  * Calls GPG with the <kbd>--list-keys</kbd> command and grabs keys. See
  * the first section of <b>doc/DETAILS</b> in the
  * {@link http://www.gnupg.org/download/ GPG package} for a detailed
  * description of how the GPG command output is parsed.
  *
  * @param string $keyId optional. Only keys with that match the specified
  *                      pattern are returned. The pattern may be part of
  *                      a user id, a key id or a key fingerprint. If not
  *                      specified, all keys are returned.
  *
  * @return array an array of {@link Crypt_GPG_Key} objects. If no keys
  *               match the specified <kbd>$keyId</kbd> an empty array is
  *               returned.
  *
  * @throws Crypt_GPG_Exception if an unknown or unexpected error occurs.
  *         Use the <kbd>debug</kbd> option and file a bug report if these
  *         exceptions occur.
  *
  * @see Crypt_GPG_Key
  */
 protected function _getKeys($keyId = '')
 {
     // get private key fingerprints
     if ($keyId == '') {
         $operation = '--list-secret-keys';
     } else {
         $operation = '--utf8-strings --list-secret-keys ' . escapeshellarg($keyId);
     }
     // According to The file 'doc/DETAILS' in the GnuPG distribution, using
     // double '--with-fingerprint' also prints the fingerprint for subkeys.
     $arguments = array('--with-colons', '--with-fingerprint', '--with-fingerprint', '--fixed-list-mode');
     $output = '';
     $this->engine->reset();
     $this->engine->setOutput($output);
     $this->engine->setOperation($operation, $arguments);
     $this->engine->run();
     $privateKeyFingerprints = array();
     foreach (explode(PHP_EOL, $output) as $line) {
         $lineExp = explode(':', $line);
         if ($lineExp[0] == 'fpr') {
             $privateKeyFingerprints[] = $lineExp[9];
         }
     }
     // get public keys
     if ($keyId == '') {
         $operation = '--list-public-keys';
     } else {
         $operation = '--utf8-strings --list-public-keys ' . escapeshellarg($keyId);
     }
     $output = '';
     $this->engine->reset();
     $this->engine->setOutput($output);
     $this->engine->setOperation($operation, $arguments);
     $this->engine->run();
     $keys = array();
     $key = null;
     // current key
     $subKey = null;
     // current sub-key
     foreach (explode(PHP_EOL, $output) as $line) {
         $lineExp = explode(':', $line);
         if ($lineExp[0] == 'pub') {
             // new primary key means last key should be added to the array
             if ($key !== null) {
                 $keys[] = $key;
             }
             $key = new Crypt_GPG_Key();
             $subKey = Crypt_GPG_SubKey::parse($line);
             $key->addSubKey($subKey);
         } elseif ($lineExp[0] == 'sub') {
             $subKey = Crypt_GPG_SubKey::parse($line);
             $key->addSubKey($subKey);
         } elseif ($lineExp[0] == 'fpr') {
             $fingerprint = $lineExp[9];
             // set current sub-key fingerprint
             $subKey->setFingerprint($fingerprint);
             // if private key exists, set has private to true
             if (in_array($fingerprint, $privateKeyFingerprints)) {
                 $subKey->setHasPrivate(true);
             }
         } elseif ($lineExp[0] == 'uid') {
             $string = stripcslashes($lineExp[9]);
             // as per documentation
             $userId = new Crypt_GPG_UserId($string);
             if ($lineExp[1] == 'r') {
                 $userId->setRevoked(true);
             }
             $key->addUserId($userId);
         }
     }
     // add last key
     if ($key !== null) {
         $keys[] = $key;
     }
     return $keys;
 }
Exemplo n.º 2
0
 /**
  * Gets the available keys in the keyring
  *
  * Calls GPG with the <kbd>--list-keys</kbd> command and grabs keys. See
  * the first section of <b>doc/DETAILS</b> in the
  * {@link http://www.gnupg.org/download/ GPG package} for a detailed
  * description of how the GPG command output is parsed.
  *
  * @param string $keyId optional. Only keys with that match the specified
  *                      pattern are returned. The pattern may be part of
  *                      a user id, a key id or a key fingerprint. If not
  *                      specified, all keys are returned.
  *
  * @return array an array of {@link Crypt_GPG_Key} objects. If no keys
  *               match the specified <kbd>$keyId</kbd> an empty array is
  *               returned.
  *
  * @throws Crypt_GPG_Exception if an unknown or unexpected error occurs.
  *         Use the <kbd>debug</kbd> option and file a bug report if these
  *         exceptions occur.
  *
  * @see Crypt_GPG_Key
  */
 public function getKeys($keyId = '')
 {
     // get private key fingerprints
     if ($keyId == '') {
         $operation = '--list-secret-keys';
     } else {
         $operation = '--list-secret-keys ' . escapeshellarg($keyId);
     }
     // According to The file 'doc/DETAILS' in the GnuPG distribution, using
     // double '--with-fingerprint' also prints the fingerprint for subkeys.
     $arguments = array('--with-colons', '--with-fingerprint', '--with-fingerprint', '--fixed-list-mode');
     $output = '';
     $this->engine->reset();
     $this->engine->setOutput($output);
     $this->engine->setOperation($operation, $arguments);
     $this->engine->run();
     $code = $this->engine->getErrorCode();
     switch ($code) {
         case Crypt_GPG::ERROR_NONE:
         case Crypt_GPG::ERROR_KEY_NOT_FOUND:
             // ignore not found key errors
             break;
         case Crypt_GPG::ERROR_FILE_PERMISSIONS:
             $filename = $this->engine->getErrorFilename();
             if ($filename) {
                 throw new Crypt_GPG_FileException(sprintf('Error reading GnuPG data file \'%s\'. Check to make ' . 'sure it is readable by the current user.', $filename), $code, $filename);
             }
             throw new Crypt_GPG_FileException('Error reading GnuPG data file. Check to make GnuPG data ' . 'files are readable by the current user.', $code);
         default:
             throw new Crypt_GPG_Exception('Unknown error getting keys. Please use the \'debug\' option ' . 'when creating the Crypt_GPG object, and file a bug report ' . 'at ' . self::BUG_URI, $code);
     }
     $privateKeyFingerprints = array();
     $lines = explode(PHP_EOL, $output);
     foreach ($lines as $line) {
         $lineExp = explode(':', $line);
         if ($lineExp[0] == 'fpr') {
             $privateKeyFingerprints[] = $lineExp[9];
         }
     }
     // get public keys
     if ($keyId == '') {
         $operation = '--list-public-keys';
     } else {
         $operation = '--list-public-keys ' . escapeshellarg($keyId);
     }
     $output = '';
     $this->engine->reset();
     $this->engine->setOutput($output);
     $this->engine->setOperation($operation, $arguments);
     $this->engine->run();
     $code = $this->engine->getErrorCode();
     switch ($code) {
         case Crypt_GPG::ERROR_NONE:
         case Crypt_GPG::ERROR_KEY_NOT_FOUND:
             // ignore not found key errors
             break;
         case Crypt_GPG::ERROR_FILE_PERMISSIONS:
             $filename = $this->engine->getErrorFilename();
             if ($filename) {
                 throw new Crypt_GPG_FileException(sprintf('Error reading GnuPG data file \'%s\'. Check to make ' . 'sure it is readable by the current user.', $filename), $code, $filename);
             }
             throw new Crypt_GPG_FileException('Error reading GnuPG data file. Check to make GnuPG data ' . 'files are readable by the current user.', $code);
         default:
             throw new Crypt_GPG_Exception('Unknown error getting keys. Please use the \'debug\' option ' . 'when creating the Crypt_GPG object, and file a bug report ' . 'at ' . self::BUG_URI, $code);
     }
     $keys = array();
     $key = null;
     // current key
     $subKey = null;
     // current sub-key
     $lines = explode(PHP_EOL, $output);
     foreach ($lines as $line) {
         $lineExp = explode(':', $line);
         if ($lineExp[0] == 'pub') {
             // new primary key means last key should be added to the array
             if ($key !== null) {
                 $keys[] = $key;
             }
             $key = new Crypt_GPG_Key();
             $subKey = Crypt_GPG_SubKey::parse($line);
             $key->addSubKey($subKey);
         } elseif ($lineExp[0] == 'sub') {
             $subKey = Crypt_GPG_SubKey::parse($line);
             $key->addSubKey($subKey);
         } elseif ($lineExp[0] == 'fpr') {
             $fingerprint = $lineExp[9];
             // set current sub-key fingerprint
             $subKey->setFingerprint($fingerprint);
             // if private key exists, set has private to true
             if (in_array($fingerprint, $privateKeyFingerprints)) {
                 $subKey->setHasPrivate(true);
             }
         } elseif ($lineExp[0] == 'uid') {
             $string = stripcslashes($lineExp[9]);
             // as per documentation
             $userId = new Crypt_GPG_UserId($string);
             if ($lineExp[1] == 'r') {
                 $userId->setRevoked(true);
             }
             $key->addUserId($userId);
         }
     }
     // add last key
     if ($key !== null) {
         $keys[] = $key;
     }
     return $keys;
 }
Exemplo n.º 3
0
 /**
  * @group parse
  */
 public function testParseCreationDateIso()
 {
     $expectedSubKey = new Crypt_GPG_SubKey(array('id' => '8C37DBD2A01B7976', 'algorithm' => Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC, 'length' => 2048, 'creation' => 1221442255, 'expiration' => 0, 'canSign' => false, 'canEncrypt' => true));
     $string = 'sub:u:2048:16:8C37DBD2A01B7976:20080915T013055::::::e:';
     $subKey = Crypt_GPG_SubKey::parse($string);
     $this->assertEquals($expectedSubKey, $subKey);
 }