Skip to content

Commit

Permalink
Throw Horde_Crypt_Exception and return actual error.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Jan 26, 2016
1 parent 3bf6f3f commit cc271c8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
16 changes: 8 additions & 8 deletions framework/Crypt/lib/Horde/Crypt/Pgp.php
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
58 changes: 28 additions & 30 deletions framework/Crypt/lib/Horde/Crypt/Pgp/Backend/Binary.php
Expand Up @@ -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(
Expand Down Expand Up @@ -307,7 +307,7 @@ public function getSignersKeyID($text)
return $matches[1];
}

throw new RuntimeException();
throw new Horde_Crypt_Exception(_("Cannot read PGP key ID"));
}

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit cc271c8

Please sign in to comment.