diff --git a/framework/Crypt/lib/Horde/Crypt/Pgp.php b/framework/Crypt/lib/Horde/Crypt/Pgp.php index ca72c41df44..cea5d0406e9 100644 --- a/framework/Crypt/lib/Horde/Crypt/Pgp.php +++ b/framework/Crypt/lib/Horde/Crypt/Pgp.php @@ -88,7 +88,7 @@ public function generateKey($realname, $email, $passphrase, $comment = '', if ($ret !== false) { return $ret; } - } catch (Exception $e) {} + } catch (Horde_Crypt_Exception $e) {} } throw new Horde_Crypt_Exception( @@ -149,7 +149,7 @@ public function pgpPacketInformation($pgpdata) foreach ($this->_backends as $val) { try { return $val->packetInfo($pgpdata); - } catch (Exception $e) {} + } catch (Horde_Crypt_Exception $e) {} } return array(); @@ -374,7 +374,7 @@ public function getSignersKeyID($text) foreach ($this->_backends as $val) { try { return $val->getSignersKeyId($text); - } catch (Exception $e) {} + } catch (Horde_Crypt_Exception $e) {} } return null; @@ -482,7 +482,7 @@ public function getFingerprintsFromKey($pgpdata) foreach ($this->_backends as $val) { try { return $val->getFingerprintsFromKey($pgpdata); - } catch (Exception $e) {} + } catch (Horde_Crypt_Exception $e) {} } return array(); @@ -503,7 +503,7 @@ public function getPublicKeyFromPrivateKey($data) foreach ($this->_backends as $val) { try { return $val->getPublicKeyFromPrivateKey($data); - } catch (Exception $e) {} + } catch (Horde_Crypt_Exception $e) {} } return null; @@ -557,7 +557,7 @@ public function encrypt($text, $params = array()) foreach ($this->_backends as $val) { try { return $val->$func($text, $params); - } catch (Exception $e) {} + } catch (Horde_Crypt_Exception $e) {} } throw new Horde_Crypt_Exception($error); @@ -621,7 +621,7 @@ public function decrypt($text, $params = array()) foreach ($this->_backends as $val) { try { return $val->$func($text, $params); - } catch (Exception $e) {} + } catch (Horde_Crypt_Exception $e) {} } throw new Horde_Crypt_Exception( @@ -647,7 +647,7 @@ public function encryptedSymmetrically($text) foreach ($this->_backends as $val) { try { return $val->isEncryptedSymmetrically($text); - } catch (Exception $e) {} + } catch (Horde_Crypt_Exception $e) {} } throw new Horde_Crypt_Exception( diff --git a/framework/Crypt/lib/Horde/Crypt/Pgp/Backend/Binary.php b/framework/Crypt/lib/Horde/Crypt/Pgp/Backend/Binary.php index d97665be49d..ea9d45536f7 100644 --- a/framework/Crypt/lib/Horde/Crypt/Pgp/Backend/Binary.php +++ b/framework/Crypt/lib/Horde/Crypt/Pgp/Backend/Binary.php @@ -132,7 +132,7 @@ public function generateKey($opts) /* If either key is empty, something went wrong. */ if (empty($public_key) || empty($secret_key)) { - throw new RuntimeException(); + throw new Horde_Crypt_Exception(_("Cannot generate PGP keys")); } return array( @@ -307,7 +307,7 @@ public function getSignersKeyID($text) return $matches[1]; } - throw new RuntimeException(); + throw new Horde_Crypt_Exception(_("Cannot read PGP key ID")); } /** @@ -328,9 +328,7 @@ public function getFingerprintsFromKey($pgpdata) false, true ); - if (!$result || !$result->stdout) { - throw new RuntimeException(); - } + $this->_ensureResult($result); /* Parse fingerprints and key ids from output. */ $fingerprints = array(); @@ -410,13 +408,7 @@ public function encryptMessage($text, $params) true, true ); - - // TODO: error logging - // $error = preg_replace('/\n.*/', '', $result->stderr); - - if (empty($result->output)) { - throw new RuntimeException(); - } + $this->_ensureResult($result); return $result->output; } @@ -454,13 +446,7 @@ public function encryptSignature($text, $params) true, true ); - - // TODO: error logging - // $error = preg_replace('/\n.*/', '', $result->stderr); - - if (empty($result->output)) { - throw new RuntimeException(); - } + $this->_ensureResult($result); return $result->output; } @@ -502,13 +488,7 @@ public function decryptMessage($text, $params) true, true ); - - // TODO: error logging - // $error = preg_replace('/\n.*/', '', $result->stderr); - - if (empty($result->output)) { - throw new RuntimeException(); - } + $this->_ensureResult($result); return $this->_checkSignatureResult($result->stderr, $result->output); } @@ -567,10 +547,7 @@ public function getPublicKeyFromPrivateKey($data) ); $result = $this->_callGpg($cmdline, 'r', array(), true, true); - - if (empty($result->output)) { - throw new RuntimeException(); - } + $this->_ensureResult($result); return $result->output; } @@ -772,6 +749,27 @@ protected function _putInKeyring($keys = array(), $type = 'public') return $keyring; } + /** + * Checks whether there was some valid output. + * + * @param object $result A result from _callGpg(). + * + * @throws Horde_Crypt_Exception with messages from stderr if the result + * output is empty. + */ + protected function _ensureResult($result) + { + if (empty($result->output) && empty($result->stdout)) { + throw new Horde_Crypt_Exception( + preg_replace( + array('/^gpg: /', '/\n/'), + array('', '. '), + $result->stderr + ) + ); + } + } + /** * Create a temporary file that will be deleted at the end of this * process.