Beispiel #1
0
 /**
  * Verifies data
  *
  * @param string  $data      the signed data to be verified.
  * @param boolean $isFile    whether or not the data is a filename.
  * @param string  $signature if verifying a file signed using a detached
  *                           signature, this must be the detached signature
  *                           data. Otherwise, specify ''.
  *
  * @return array an array of {@link Crypt_GPG_Signature} objects for the
  *               signed data.
  *
  * @throws Crypt_GPG_NoDataException if the provided data is not signed
  *         data.
  *
  * @throws Crypt_GPG_FileException if the input file is not readable.
  *
  * @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_Signature
  */
 protected function _verify($data, $isFile, $signature)
 {
     if ($signature == '') {
         $operation = '--verify';
         $arguments = array();
     } else {
         // Signed data goes in FD_MESSAGE, detached signature data goes in
         // FD_INPUT.
         $operation = '--verify - "-&' . Crypt_GPG_Engine::FD_MESSAGE . '"';
         $arguments = array('--enable-special-filenames');
     }
     $handler = new Crypt_GPG_VerifyStatusHandler();
     if ($isFile) {
         $input = @fopen($data, 'rb');
         if ($input === false) {
             throw new Crypt_GPG_FileException('Could not open input file "' . $data . '" for verifying.', 0, $data);
         }
     } else {
         $input = strval($data);
         if ($input == '') {
             throw new Crypt_GPG_NoDataException('No valid signature data found.', self::ERROR_NO_DATA);
         }
     }
     $this->engine->reset();
     $this->engine->addStatusHandler(array($handler, 'handle'));
     if ($signature == '') {
         // signed or clearsigned data
         $this->engine->setInput($input);
     } else {
         // detached signature
         $this->engine->setInput($signature);
         $this->engine->setMessage($input);
     }
     $this->engine->setOperation($operation, $arguments);
     $this->engine->run();
     if ($isFile) {
         fclose($input);
     }
     $code = $this->engine->getErrorCode();
     switch ($code) {
         case self::ERROR_NONE:
         case self::ERROR_BAD_SIGNATURE:
             break;
         case self::ERROR_NO_DATA:
             throw new Crypt_GPG_NoDataException('No valid signature data found.', $code);
         case self::ERROR_KEY_NOT_FOUND:
             throw new Crypt_GPG_KeyNotFoundException('Public key required for data verification not in keyring.', $code, $this->engine->getErrorKeyId());
         default:
             throw new Crypt_GPG_Exception('Unknown error validating signature details. Please use the ' . '\'debug\' option when creating the Crypt_GPG object, and ' . 'file a bug report at ' . self::BUG_URI, $code);
     }
     return $handler->getSignatures();
 }