From c79c6dfcd3fe5fefabd73a5eff965823577abe74 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Thu, 26 Feb 2009 00:04:03 -0700 Subject: [PATCH] Update to new Horde_Crypt code --- imp/ajax.php | 46 ++++++++------ imp/compose.php | 2 +- imp/lib/Compose.php | 6 +- imp/lib/Crypt/{pgp.php => Pgp.php} | 112 +++++++++++++-------------------- imp/lib/Crypt/{smime.php => Smime.php} | 61 +++++------------- imp/lib/Mime/Viewer/pgp.php | 21 ++++--- imp/lib/Mime/Viewer/plain.php | 14 ++--- imp/lib/Mime/Viewer/smime.php | 90 +++++++++++++------------- imp/pgp.php | 18 ++++-- imp/smime.php | 23 +++++-- 10 files changed, 188 insertions(+), 205 deletions(-) rename imp/lib/Crypt/{pgp.php => Pgp.php} (84%) rename imp/lib/Crypt/{smime.php => Smime.php} (89%) diff --git a/imp/ajax.php b/imp/ajax.php index 1f993743e..9fb6fb59a 100644 --- a/imp/ajax.php +++ b/imp/ajax.php @@ -697,29 +697,39 @@ case 'SMIMEPersonal': $passphrase = Util::getFormData('dialog_input'); if ($action == 'SMIMEPersonal') { - $imp_smime = Horde_Crypt::singleton(array('imp', 'smime')); - $secure_check = $imp_smime->requireSecureConnection(); - if (!is_a($secure_check, 'PEAR_Error') && $passphrase) { - $res = $imp_smime->storePassphrase($passphrase); + $imp_smime = Horde_Crypt::singleton(array('IMP', 'Smime')); + try { + $imp_smime->requireSecureConnection(); + if ($passphrase) { + if ($imp_smime->storePassphrase($passphrase)) { + $result->success = 1; + } else { + $result->error = _("Invalid passphrase entered."); + } + } else { + $result->error = _("No passphrase entered."); + } + } catch (Horde_Exception $e) { + $result->error = $e->getMessage(); } } else { - $imp_pgp = Horde_Crypt::singleton(array('imp', 'pgp')); - $secure_check = $imp_pgp->requireSecureConnection(); - if (is_a($secure_check, 'PEAR_Error') && $passphrase) { - $res = $imp_pgp->storePassphrase(($action == 'PGPSymmetric') ? 'symmetric' : 'personal', $passphrase, Util::getFormData('symmetricid')); + $imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp')); + try { + $imp_pgp->requireSecureConnection(); + if ($passphrase) { + if ($imp_pgp->storePassphrase(($action == 'PGPSymmetric') ? 'symmetric' : 'personal', $passphrase, Util::getFormData('symmetricid'))) { + $result->success = 1; + } else { + $result->error = _("Invalid passphrase entered."); + } + } else { + $result->error = _("No passphrase entered."); + } + } catch (Horde_Exception $e) { + $result->error = $e->getMessage(); } } - if (is_a($secure_check, 'PEAR_Error')) { - $result->error = $secure_check->getMessage(); - } elseif (!$passphrase) { - $result->error = _("No passphrase entered."); - } elseif ($res) { - $result->success = 1; - } else { - $result->error = _("Invalid passphrase entered."); - } - if ($_SESSION['imp']['view'] != 'dimp') { $notify = false; } diff --git a/imp/compose.php b/imp/compose.php index 02f8ee59a..f3796cf01 100644 --- a/imp/compose.php +++ b/imp/compose.php @@ -748,7 +748,7 @@ if ($prefs->getValue('use_pgp')) { try { $addrs = $imp_compose->recipientList($header); if (!empty($addrs['list'])) { - $imp_pgp = &Horde_Crypt::singleton(array('imp', 'pgp')); + $imp_pgp = &Horde_Crypt::singleton(array('IMP', 'Pgp')); foreach ($addrs['list'] as $val) { $imp_pgp->getPublicKey($val); } diff --git a/imp/lib/Compose.php b/imp/lib/Compose.php index f257ef745..cf18add65 100644 --- a/imp/lib/Compose.php +++ b/imp/lib/Compose.php @@ -1022,7 +1022,7 @@ class IMP_Compose if ($attach_flag) { if ($this->_pgpAttachPubkey) { - $imp_pgp = Horde_Crypt::singleton(array('imp', 'pgp')); + $imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp')); $base->addPart($imp_pgp->publicKeyMIMEPart()); } @@ -1036,7 +1036,7 @@ class IMP_Compose if ($GLOBALS['prefs']->getValue('use_pgp') && !empty($GLOBALS['conf']['utils']['gnupg']) && in_array($encrypt, array(IMP::PGP_ENCRYPT, IMP::PGP_SIGN, IMP::PGP_SIGNENC, IMP::PGP_SYM_ENCRYPT, IMP::PGP_SYM_SIGNENC))) { - $imp_pgp = Horde_Crypt::singleton(array('imp', 'pgp')); + $imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp')); switch ($encrypt) { case IMP::PGP_SIGN: @@ -1092,7 +1092,7 @@ class IMP_Compose } } elseif ($GLOBALS['prefs']->getValue('use_smime') && in_array($encrypt, array(IMP::SMIME_ENCRYPT, IMP::SMIME_SIGN, IMP::SMIME_SIGNENC))) { - $imp_smime = Horde_Crypt::singleton(array('imp', 'smime')); + $imp_smime = Horde_Crypt::singleton(array('IMP', 'Smime')); /* Check to see if we have the user's passphrase yet. */ if (in_array($encrypt, array(IMP::SMIME_SIGN, IMP::SMIME_SIGNENC))) { diff --git a/imp/lib/Crypt/pgp.php b/imp/lib/Crypt/Pgp.php similarity index 84% rename from imp/lib/Crypt/pgp.php rename to imp/lib/Crypt/Pgp.php index 3633b3120..fba601469 100644 --- a/imp/lib/Crypt/pgp.php +++ b/imp/lib/Crypt/Pgp.php @@ -1,6 +1,6 @@ * @package IMP */ -class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp +class IMP_Crypt_Pgp extends Horde_Crypt_Pgp { /* Name of PGP public key field in addressbook. */ const PUBKEY_FIELD = 'pgpPublicKey'; @@ -30,11 +30,11 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp /** * Generate the personal Public/Private keypair and store in prefs. * - * @param string $realname See Horde_Crypt_pgp:: - * @param string $email See Horde_Crypt_pgp:: - * @param string $passphrase See Horde_Crypt_pgp:: - * @param string $comment See Horde_Crypt_pgp:: - * @param string $keylength See Horde_Crypt_pgp:: + * @param string $realname See Horde_Crypt_Pgp:: + * @param string $email See Horde_Crypt_Pgp:: + * @param string $passphrase See Horde_Crypt_Pgp:: + * @param string $comment See Horde_Crypt_Pgp:: + * @param string $keylength See Horde_Crypt_Pgp:: * * @throws Horde_Exception */ @@ -42,9 +42,6 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp $comment = '', $keylength = 1024) { $keys = $this->generateKey($name, $email, $passphrase, $comment, $keylength); - if (is_a($keys, 'PEAR_Error')) { - throw new Horde_Exception($keys); - } /* Store the keys in the user's preferences. */ $this->addPersonalPublicKey($keys['public']); @@ -109,7 +106,7 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp * * @param string $public_key An PGP public key. * - * @return array See Horde_Crypt_pgp::pgpPacketInformation() + * @return array See Horde_Crypt_Pgp::pgpPacketInformation() * @throws Horde_Exception */ public function addPublicKey($public_key) @@ -156,7 +153,7 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp * * @param string $address The e-mail address to search by. * @param string $fingerprint The fingerprint of the user's key. - * @param boolean $server Whether to check the publick key servers for + * @param boolean $server Whether to check the public key servers for * the key. * * @return string The PGP public key requested. @@ -261,7 +258,7 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp * @param string $fingerprint The fingerprint of the requested key. * @param string $address The email address of the requested key. * - * @return string See Horde_Crypt_pgp::getPublicKeyserver() + * @return string See Horde_Crypt_Pgp::getPublicKeyserver() * @throws Horde_Exception */ public function getFromPublicKeyserver($fingerprint, $address = null) @@ -274,7 +271,7 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp * * @param string $pubkey The PGP public key. * - * @return string See Horde_Crypt_pgp::putPublicKeyserver() + * @return string See Horde_Crypt_Pgp::putPublicKeyserver() * @throws Horde_Exception */ public function sendToPublicKeyserver($pubkey) @@ -289,30 +286,30 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp * @param string $method The method to use - either 'get' or 'put'. * @param string $additional Any additional data. * - * @return string See Horde_Crypt_pgp::getPublicKeyserver() -or- - * Horde_Crypt_pgp::putPublicKeyserver(). + * @return string See Horde_Crypt_Pgp::getPublicKeyserver() -or- + * Horde_Crypt_Pgp::putPublicKeyserver(). * @throws Horde_Exception */ protected function _keyserverConnect($data, $method, $additional = null) { global $conf; - if (!empty($conf['utils']['gnupg_keyserver'])) { - $timeout = (empty($conf['utils']['gnupg_timeout'])) ? PGP_KEYSERVER_TIMEOUT : $conf['utils']['gnupg_timeout']; - if ($method == 'get') { - foreach ($conf['utils']['gnupg_keyserver'] as $server) { - $result = $this->getPublicKeyserver($data, $server, $timeout, $additional); - if (!is_a($result, 'PEAR_Error')) { - return $result; - } - } - throw new Horde_Exception(_("Could not connect to public PGP keyserver")); - } else { - return $this->putPublicKeyserver($data, $conf['utils']['gnupg_keyserver'][0], $timeout); - } + if (empty($conf['utils']['gnupg_keyserver'])) { + throw new Horde_Exception(_("Public PGP keyserver support has been disabled."), 'horde.warning'); } - throw new Horde_Exception(_("Public PGP keyserver support has been disabled."), 'horde.warning'); + $timeout = (empty($conf['utils']['gnupg_timeout'])) ? PGP_KEYSERVER_TIMEOUT : $conf['utils']['gnupg_timeout']; + + if ($method == 'put') { + return $this->putPublicKeyserver($data, $conf['utils']['gnupg_keyserver'][0], $timeout); + } + + foreach ($conf['utils']['gnupg_keyserver'] as $server) { + try { + return $this->getPublicKeyserver($data, $server, $timeout, $additional); + } catch (Horde_Exception $e) {} + } + throw new Horde_Exception(_("Could not connect to public PGP keyserver")); } /** @@ -322,8 +319,7 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp * @param string $address E-mail address of public key. * @param string $signature A PGP signature block. * - * @return string See Horde_Crypt_pgp::decryptSignature() -or- - * Horde_Crypt_pgp::decryptDetachedSignature(). + * @return stdClass See Horde_Crypt_Pgp::decrypt(). * @throws Horde_Exception */ public function verifySignature($text, $address, $signature = '') @@ -349,13 +345,7 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp } $options['pubkey'] = $public_key; - /* decrypt() returns a PEAR_Error object on error. */ - $res = $this->decrypt($text, $options); - if (is_a($res, 'PEAR_Error')) { - throw new Horde_Exception($res); - } - - return $res; + return $this->decrypt($text, $options); } /** @@ -367,30 +357,23 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp * @param boolean $passphrase If $type is 'personal' or 'symmetrical', * the passphrase to use. * - * @return string The decrypted message. + * @return stdClass See Horde_Crypt_Pgp::decrypt(). * @throws Horde_Exception */ public function decryptMessage($text, $type, $passphrase = null) { switch ($type) { case 'literal': - $res = $this->decrypt($text, array('type' => 'message', 'no_passphrase' => true)); + return $this->decrypt($text, array('type' => 'message', 'no_passphrase' => true)); break; case 'symmetric': - $res = $this->decrypt($text, array('type' => 'message', 'passphrase' => $passphrase)); + return $this->decrypt($text, array('type' => 'message', 'passphrase' => $passphrase)); break; case 'personal': - $res = $this->decrypt($text, array('type' => 'message', 'pubkey' => $this->getPersonalPublicKey(), 'privkey' => $this->getPersonalPrivateKey(), 'passphrase' => $passphrase)); - break; + return $this->decrypt($text, array('type' => 'message', 'pubkey' => $this->getPersonalPublicKey(), 'privkey' => $this->getPersonalPrivateKey(), 'passphrase' => $passphrase)); } - - if (is_a($res, 'PEAR_Error')) { - throw new Horde_Exception($res); - } - - return $res; } /** @@ -541,16 +524,12 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp * * @param Horde_Mime_Part $mime_part The object to sign. * - * @return Horde_Mime_Part See Horde_Crypt_pgp::signMIMEPart(). + * @return Horde_Mime_Part See Horde_Crypt_Pgp::signMIMEPart(). * @throws Horde_Exception */ public function IMPsignMIMEPart($mime_part) { - $res = $this->signMIMEPart($mime_part, $this->_signParameters()); - if (is_a($res, 'PEAR_Error')) { - throw new Horde_Exception($res); - } - return $res; + return $this->signMIMEPart($mime_part, $this->_signParameters()); } /** @@ -563,21 +542,18 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp * use for encrypting. If null, uses * the personal key. * - * @return Horde_Mime_Part See Horde_Crypt_pgp::encryptMIMEPart(). + * @return Horde_Mime_Part See Horde_Crypt_Pgp::encryptMIMEPart(). * @throws Horde_Exception */ public function IMPencryptMIMEPart($mime_part, $addresses, $symmetric = null) { - $res = $this->encryptMIMEPart($mime_part, $this->_encryptParameters($addresses, $symmetric)); - if (is_a($res, 'PEAR_Error')) { - throw new Horde_Exception($res); - } - return $res; + return $this->encryptMIMEPart($mime_part, $this->_encryptParameters($addresses, $symmetric)); } /** - * Sign and Encrypt a Horde_Mime_Part using PGP using IMP default parameters. + * Sign and Encrypt a Horde_Mime_Part using PGP using IMP default + * parameters. * * @param Horde_Mime_Part $mime_part The object to sign and encrypt. * @param array $addresses The e-mail address of the keys to @@ -586,24 +562,20 @@ class IMP_Horde_Crypt_pgp extends Horde_Crypt_pgp * use for encrypting. If null, uses * the personal key. * - * @return Horde_Mime_Part See Horde_Crypt_pgp::signAndencryptMIMEPart(). + * @return Horde_Mime_Part See Horde_Crypt_Pgp::signAndencryptMIMEPart(). * @throws Horde_Exception */ public function IMPsignAndEncryptMIMEPart($mime_part, $addresses, $symmetric = null) { - $res = $this->signAndEncryptMIMEPart($mime_part, $this->_signParameters(), $this->_encryptParameters($addresses, $symmetric)); - if (is_a($res, 'PEAR_Error')) { - throw new Horde_Exception($res); - } - return $res; + return $this->signAndEncryptMIMEPart($mime_part, $this->_signParameters(), $this->_encryptParameters($addresses, $symmetric)); } /** * Generate a Horde_Mime_Part object, in accordance with RFC 2015/3156, * that contains the user's public key. * - * @return Horde_Mime_Part See Horde_Crypt_pgp::publicKeyMIMEPart(). + * @return Horde_Mime_Part See Horde_Crypt_Pgp::publicKeyMIMEPart(). */ public function publicKeyMIMEPart() { diff --git a/imp/lib/Crypt/smime.php b/imp/lib/Crypt/Smime.php similarity index 89% rename from imp/lib/Crypt/smime.php rename to imp/lib/Crypt/Smime.php index 450a0582d..c9764de58 100644 --- a/imp/lib/Crypt/smime.php +++ b/imp/lib/Crypt/Smime.php @@ -1,6 +1,6 @@ * @package IMP */ -class IMP_Horde_Crypt_smime extends Horde_Crypt_smime +class IMP_Crypt_Smime extends Horde_Crypt_Smime { /* Name of the S/MIME public key field in addressbook. */ const PUBKEY_FIELD = 'smimePublicKey'; @@ -243,7 +243,8 @@ class IMP_Horde_Crypt_smime extends Horde_Crypt_smime * * @param string $text The text to verify. * - * @return stdClass See Horde_Crypt_smime::verify(). + * @return stdClass See Horde_Crypt_Smime::verify(). + * @throws Horde_Exception */ public function verifySignature($text) { @@ -256,18 +257,12 @@ class IMP_Horde_Crypt_smime extends Horde_Crypt_smime * * @param string $text The text to decrypt. * - * @return string See Horde_Crypt_smime::decrypt(). + * @return string See Horde_Crypt_Smime::decrypt(). * @throws Horde_Exception */ public function decryptMessage($text) { - /* decrypt() returns a PEAR_Error object on error. */ - $res = $this->decrypt($text, array('type' => 'message', 'pubkey' => $this->getPersonalPublicKey(), 'privkey' => $this->getPersonalPrivateKey(), 'passphrase' => $this->getPassphrase())); - if (is_a($res, 'PEAR_Error')) { - throw new Horde_Exception($res); - } - - return $res; + return $this->decrypt($text, array('type' => 'message', 'pubkey' => $this->getPersonalPublicKey(), 'privkey' => $this->getPersonalPrivateKey(), 'passphrase' => $this->getPassphrase())); } /** @@ -353,17 +348,12 @@ class IMP_Horde_Crypt_smime extends Horde_Crypt_smime * @param mixed $to_address The e-mail address of the key to use for * encryption. * - * @return MIME_Part See Horde_Crypt_smime::encryptMIMEPart(). + * @return MIME_Part See Horde_Crypt_Smime::encryptMIMEPart(). * @throws Horde_Exception */ public function IMPencryptMIMEPart($mime_part, $to_address) { - $res = $this->encryptMIMEPart($mime_part, $this->_encryptParameters($to_address)); - if (is_a($res, 'PEAR_Error')) { - throw new Horde_Exception($res); - } - - return $res; + return $this->encryptMIMEPart($mime_part, $this->_encryptParameters($to_address)); } /** @@ -371,17 +361,12 @@ class IMP_Horde_Crypt_smime extends Horde_Crypt_smime * * @param MIME_Part $mime_part The MIME_Part object to sign. * - * @return MIME_Part See Horde_Crypt_smime::signMIMEPart(). + * @return MIME_Part See Horde_Crypt_Smime::signMIMEPart(). * @throws Horde_Exception */ public function IMPsignMIMEPart($mime_part) { - $res = $this->signMIMEPart($mime_part, $this->_signParameters()); - if (is_a($res, 'PEAR_Error')) { - throw new Horde_Exception($res); - } - - return $res; + return $this->signMIMEPart($mime_part, $this->_signParameters()); } /** @@ -391,17 +376,12 @@ class IMP_Horde_Crypt_smime extends Horde_Crypt_smime * @param string $to_address The e-mail address of the key to use for * encryption. * - * @return MIME_Part See Horde_Crypt_smime::signAndencryptMIMEPart(). + * @return MIME_Part See Horde_Crypt_Smime::signAndencryptMIMEPart(). * @throws Horde_Exception */ public function IMPsignAndEncryptMIMEPart($mime_part, $to_address) { - $res = $this->signAndEncryptMIMEPart($mime_part, $this->_signParameters(), $this->_encryptParameters($to_address)); - if (is_a($res, 'PEAR_Error')) { - throw new Horde_Exception($res); - } - - return $res; + return $this->signAndEncryptMIMEPart($mime_part, $this->_signParameters(), $this->_encryptParameters($to_address)); } /** @@ -416,21 +396,13 @@ class IMP_Horde_Crypt_smime extends Horde_Crypt_smime */ public function addFromPKCS12($pkcs12, $password, $pkpass = null) { - $openssl = $this->checkForOpenSSL(); - if (is_a($openssl, 'PEAR_Error')) { - throw new Horde_Exception($openssl); - } - $sslpath = (empty($GLOBALS['conf']['utils']['openssl_binary'])) ? null : $GLOBALS['conf']['utils']['openssl_binary']; $params = array('sslpath' => $sslpath, 'password' => $password); if (!empty($pkpass)) { $params['newpassword'] = $pkpass; } - $res = $this->parsePKCS12Data($pkcs12, $params); - if (is_a($res, 'PEAR_Error')) { - throw new Horde_Exception($res); - } + $res = $this->parsePKCS12Data($pkcs12, $params); $this->addPersonalPrivateKey($res->private); $this->addPersonalPublicKey($res->public); $this->addAdditionalCert($res->certs); @@ -450,12 +422,7 @@ class IMP_Horde_Crypt_smime extends Horde_Crypt_smime ? null : $GLOBALS['conf']['utils']['openssl_binary']; - $res = parent::extractSignedContents($data, $sslpath); - if (is_a($res, 'PEAR_Error')) { - throw new Horde_Exception($res); - } - - return $res; + return parent::extractSignedContents($data, $sslpath); } } diff --git a/imp/lib/Mime/Viewer/pgp.php b/imp/lib/Mime/Viewer/pgp.php index 1eb7ee591..2a31d2874 100644 --- a/imp/lib/Mime/Viewer/pgp.php +++ b/imp/lib/Mime/Viewer/pgp.php @@ -36,9 +36,9 @@ class IMP_Horde_Mime_Viewer_pgp extends Horde_Mime_Viewer_Driver ); /** - * IMP_Horde_Crypt_PGP object. + * IMP_Crypt_Pgp object. * - * @var IMP_Horde_Crypt_PGP + * @var IMP_Crypt_Pgp */ protected $_imppgp; @@ -65,7 +65,7 @@ class IMP_Horde_Mime_Viewer_pgp extends Horde_Mime_Viewer_Driver { if (empty($this->_imppgp) && !empty($GLOBALS['conf']['utils']['gnupg'])) { - $this->_imppgp = &Horde_Crypt::singleton(array('imp', 'pgp')); + $this->_imppgp = Horde_Crypt::singleton(array('IMP', 'Pgp')); } if (Util::getFormData('rawpgpkey')) { @@ -146,7 +146,7 @@ class IMP_Horde_Mime_Viewer_pgp extends Horde_Mime_Viewer_Driver } if (empty($this->_imppgp)) { - $this->_imppgp = &Horde_Crypt::singleton(array('imp', 'pgp')); + $this->_imppgp = Horde_Crypt::singleton(array('IMP', 'Pgp')); } /* PGP version information appears in the first MIME subpart. We @@ -330,20 +330,23 @@ class IMP_Horde_Mime_Viewer_pgp extends Horde_Mime_Viewer_Driver ? $this->_imppgp->verifySignature($signed_data, $this->_address) : $this->_imppgp->verifySignature($signed_data, $this->_address, $sig_part->getContents()); - $icon = Horde::img('alerts/success.png', _("Success"), null, $graphicsdir); - if (empty($sig_result)) { - $sig_result = _("The message below has been verified."); + if ($sig_result->result) { + $icon = Horde::img('alerts/success.png', _("Success"), null, $graphicsdir); + $sig_text = $sig_result->message; + } else { + $icon = Horde::img('alerts/warning.png', _("Warning"), null, $graphicsdir); + $sig_text = _("The signature could not be checked because the sender's key could not be found."); } } catch (Horde_Exception $e) { $icon = Horde::img('alerts/error.png', _("Error"), null, $graphicsdir); - $sig_result = $e->getMessage(); + $sig_text = $e->getMessage(); } require_once 'Horde/Text/Filter.php'; $ret[$base_id]['status'][] = array( 'icon' => $icon, 'text' => array( - Text_Filter::filter($sig_result, 'text2html', array('parselevel' => TEXT_HTML_NOHTML)) + Text_Filter::filter($sig_text, 'text2html', array('parselevel' => TEXT_HTML_NOHTML)) ) ); } else { diff --git a/imp/lib/Mime/Viewer/plain.php b/imp/lib/Mime/Viewer/plain.php index e875c14da..a448eab63 100644 --- a/imp/lib/Mime/Viewer/plain.php +++ b/imp/lib/Mime/Viewer/plain.php @@ -166,11 +166,11 @@ class IMP_Horde_Mime_Viewer_plain extends Horde_Mime_Viewer_plain protected function _parsePGP() { /* Avoid infinite loop. */ - $imp_pgp = &Horde_Crypt::singleton(array('imp', 'pgp')); + $imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp')); $parts = $imp_pgp->parsePGPData($this->_mimepart->getContents()); if (empty($parts) || ((count($parts) == 1) && - ($parts[0]['type'] == Horde_Crypt_pgp::ARMOR_TEXT))) { + ($parts[0]['type'] == Horde_Crypt_Pgp::ARMOR_TEXT))) { return null; } @@ -181,7 +181,7 @@ class IMP_Horde_Mime_Viewer_plain extends Horde_Mime_Viewer_plain while (list(,$val) = each($parts)) { switch ($val['type']) { - case Horde_Crypt_pgp::ARMOR_TEXT: + case Horde_Crypt_Pgp::ARMOR_TEXT: $part = new Horde_Mime_Part(); $part->setType('text/plain'); $part->setCharset($charset); @@ -189,14 +189,14 @@ class IMP_Horde_Mime_Viewer_plain extends Horde_Mime_Viewer_plain $new_part->addPart($part); break; - case Horde_Crypt_pgp::ARMOR_PUBLIC_KEY: + case Horde_Crypt_Pgp::ARMOR_PUBLIC_KEY: $part = new Horde_Mime_Part(); $part->setType('application/pgp-keys'); $part->setContents(implode("\n", $val['data'])); $new_part->addPart($part); break; - case Horde_Crypt_pgp::ARMOR_MESSAGE: + case Horde_Crypt_Pgp::ARMOR_MESSAGE: $part = new Horde_Mime_Part(); $part->setType('multipart/signed'); // TODO: add micalg parameter @@ -217,9 +217,9 @@ class IMP_Horde_Mime_Viewer_plain extends Horde_Mime_Viewer_plain $new_part->addPart($part); break; - case Horde_Crypt_pgp::ARMOR_SIGNED_MESSAGE: + case Horde_Crypt_Pgp::ARMOR_SIGNED_MESSAGE: if (($sig = current($parts)) && - ($sig['type'] == Horde_Crypt_pgp::ARMOR_SIGNATURE)) { + ($sig['type'] == Horde_Crypt_Pgp::ARMOR_SIGNATURE)) { $part = new Horde_Mime_Part(); $part->setType('multipart/signed'); // TODO: add micalg parameter diff --git a/imp/lib/Mime/Viewer/smime.php b/imp/lib/Mime/Viewer/smime.php index 7d27ee3f9..ff42f87f6 100644 --- a/imp/lib/Mime/Viewer/smime.php +++ b/imp/lib/Mime/Viewer/smime.php @@ -39,9 +39,9 @@ class IMP_Horde_Mime_Viewer_smime extends Horde_Mime_Viewer_Driver ); /** - * IMP_Horde_Crypt_smime object. + * IMP_Crypt_Smime object. * - * @var IMP_Horde_Crypt_smime + * @var IMP_Crypt_Smime */ protected $_impsmime = null; @@ -176,7 +176,11 @@ class IMP_Horde_Mime_Viewer_smime extends Horde_Mime_Viewer_Driver } $raw_text = $GLOBALS['imp_imap']->utils->removeBareNewlines($this->_params['contents']->getBodyPart($this->_mimepart->getMimeId(), array('mimeheaders' => true))); - $sig_result = $this->_impsmime->verifySignature($raw_text); + + try { + $sig_result = $this->_impsmime->verifySignature($raw_text); + } catch (Horde_Exception $e) {} + return array( $this->_mimepart->getMimeId() => array( 'data' => $this->_impsmime->certToHTML($sig_result->cert), @@ -193,9 +197,10 @@ class IMP_Horde_Mime_Viewer_smime extends Horde_Mime_Viewer_Driver { if (is_null($this->_impsmime) && $GLOBALS['prefs']->getValue('use_smime')) { - $this->_impsmime = &Horde_Crypt::singleton(array('imp', 'smime')); - $openssl_check = $this->_impsmime->checkForOpenSSL(); - if (is_a($openssl_check, 'PEAR_Error')) { + try { + $this->_impsmime = Horde_Crypt::singleton(array('IMP', 'Smime')); + $this->_impsmime->checkForOpenSSL(); + } catch (Horde_Exception $e) { $this->_impsmime = null; } } @@ -204,7 +209,7 @@ class IMP_Horde_Mime_Viewer_smime extends Horde_Mime_Viewer_Driver /** * Generates HTML output for 'multipart/signed' MIME parts. * - * @return array TODo + * @return array TODO */ protected function _outputSMIMESigned() { @@ -241,7 +246,15 @@ class IMP_Horde_Mime_Viewer_smime extends Horde_Mime_Viewer_Driver if ($GLOBALS['prefs']->getValue('smime_verify') || Util::getFormData('smime_verify_msg')) { - $sig_result = $this->_impsmime->verifySignature($raw_text); + try { + $sig_result = $this->_impsmime->verifySignature($raw_text); + } catch (Horde_Exception $e) { + $ret[$base_id]['status'][0]['icon'] = ($e->getCode() == 'horde.warning') + ? Horde::img('alerts/warning.png', _("Warning"), null, $graphicsdir) + : Horde::img('alerts/error.png', _("Error"), null, $graphicsdir); + $status[] = $e->getMessage(); + return $ret; + } } else { switch ($_SESSION['imp']['view']) { case 'imp': @@ -268,42 +281,35 @@ class IMP_Horde_Mime_Viewer_smime extends Horde_Mime_Viewer_Driver $graphicsdir = $GLOBALS['registry']->getImageDir('horde'); - if (is_a($sig_result->result, 'PEAR_Error')) { - $ret[$base_id]['status'][0]['icon'] = ($sig_result->result->getCode() == 'horde.warning') - ? Horde::img('alerts/warning.png', _("Warning"), null, $graphicsdir) - : Horde::img('alerts/error.png', _("Error"), null, $graphicsdir); - $status[] = $sig_result->result->getMessage(); - } else { - $ret[$base_id]['status'][0]['icon'] = Horde::img('alerts/success.png', _("Success"), null, $graphicsdir); - - /* This message has been verified but there was no output - * from the PGP program. */ - if (empty($sig_result->result) || ($sig_result->result === true)) { - $email = (is_array($sig_result->email)) - ? implode(', ', $sig_result->email) - : $sig_result->email; - $status[] = sprintf(_("The message has been verified. Sender: %s."), htmlspecialchars($email)); + $ret[$base_id]['status'][0]['icon'] = Horde::img('alerts/success.png', _("Success"), null, $graphicsdir); + + /* This message has been verified but there was no output + * from the PGP program. */ + if (empty($sig_result->result) || ($sig_result->result === true)) { + $email = (is_array($sig_result->email)) + ? implode(', ', $sig_result->email) + : $sig_result->email; + $status[] = sprintf(_("The message has been verified. Sender: %s."), htmlspecialchars($email)); + } + + if (!empty($sig_result->cert)) { + $cert_details = $this->_impsmime->parseCert($sig_result->cert); + if (isset($cert_details['certificate']['subject']['CommonName'])) { + $subject = $cert_details['certificate']['subject']['CommonName']; + } elseif (isset($cert_details['certificate']['subject']['Email'])) { + $subject = $cert_details['certificate']['subject']['Email']; + } elseif (isset($sig_result->email)) { + $subject = $sig_result->email; + } elseif (isset($smime_from)) { + $subject = $smime_from; + } else { + $subject = null; } - if (!empty($sig_result->cert)) { - $cert_details = $this->_impsmime->parseCert($sig_result->cert); - if (isset($cert_details['certificate']['subject']['CommonName'])) { - $subject = $cert_details['certificate']['subject']['CommonName']; - } elseif (isset($cert_details['certificate']['subject']['Email'])) { - $subject = $cert_details['certificate']['subject']['Email']; - } elseif (isset($sig_result->email)) { - $subject = $sig_result->email; - } elseif (isset($smime_from)) { - $subject = $smime_from; - } else { - $subject = null; - } - - if (!empty($subject) && - $GLOBALS['registry']->hasMethod('contacts/addField') && - $GLOBALS['prefs']->getValue('add_source')) { - $status[] = sprintf(_("The S/MIME certificate of %s: "), @htmlspecialchars($subject, ENT_COMPAT, NLS::getCharset())) . $this->_params['contents']->linkViewJS($this->_mimepart, 'view_attach', _("View"), array('params' => array('mode' => IMP_Contents::RENDER_INLINE, 'view_smime_key' => 1))) . '/' . Horde::link('#', '', null, null, $this->_impsmime->savePublicKeyURL($sig_result->cert, $this->_params['contents']->getIndex(), $sig_id) . ' return false;') . _("Save in your Address Book") . ''; - } + if (!empty($subject) && + $GLOBALS['registry']->hasMethod('contacts/addField') && + $GLOBALS['prefs']->getValue('add_source')) { + $status[] = sprintf(_("The S/MIME certificate of %s: "), @htmlspecialchars($subject, ENT_COMPAT, NLS::getCharset())) . $this->_params['contents']->linkViewJS($this->_mimepart, 'view_attach', _("View"), array('params' => array('mode' => IMP_Contents::RENDER_INLINE, 'view_smime_key' => 1))) . '/' . Horde::link('#', '', null, null, $this->_impsmime->savePublicKeyURL($sig_result->cert, $this->_params['contents']->getIndex(), $sig_id) . ' return false;') . _("Save in your Address Book") . ''; } } diff --git a/imp/pgp.php b/imp/pgp.php index b1d0cf3fe..67474ceb5 100644 --- a/imp/pgp.php +++ b/imp/pgp.php @@ -66,8 +66,18 @@ function _reloadWindow() require_once dirname(__FILE__) . '/lib/base.php'; -$imp_pgp = &Horde_Crypt::singleton(array('imp', 'pgp')); -$secure_check = $imp_pgp->requireSecureConnection(); +try { + $imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp')); +} catch (Horde_Exception $e) { + Horde::fatal($e, __FILE__, __LINE__); +} + +try { + $imp_pgp->requireSecureConnection(); + $secure_check = true; +} catch (Horde_Exception $e) { + $secure_check = false; +} /* Run through the action handlers */ $actionID = Util::getFormData('actionID'); @@ -345,8 +355,8 @@ if ($prefs->getValue('use_pgp')) { } $t->set('personalkey-help', Help::link('imp', 'pgp-overview-personalkey')); - $t->set('secure_check', is_a($secure_check, 'PEAR_Error')); - if (!$t->get('secure_check')) { + $t->set('secure_check', !$secure_check); + if ($secure_check) { $t->set('has_key', $prefs->getValue('pgp_public_key') && $prefs->getValue('pgp_private_key')); if ($t->get('has_key')) { $t->set('viewpublic', Horde::link(Util::addParameter($selfURL, 'actionID', 'view_personal_public_key'), _("View Personal Public Key"), null, 'view_key')); diff --git a/imp/smime.php b/imp/smime.php index 5fab5b17a..205eea0b4 100644 --- a/imp/smime.php +++ b/imp/smime.php @@ -80,8 +80,18 @@ function _printKeyInfo($cert) require_once dirname(__FILE__) . '/lib/base.php'; -$imp_smime = &Horde_Crypt::singleton(array('imp', 'smime')); -$secure_check = $imp_smime->requireSecureConnection(); +try { + $imp_smime = Horde_Crypt::singleton(array('IMP', 'Smime')); +} catch (Horde_Exception $e) { + Horde::fatal($e, __FILE__, __LINE__); +} + +try { + $imp_smime->requireSecureConnection(); + $secure_check = true; +} catch (Horde_Exception $e) { + $secure_check = false; +} /* Run through the action handlers */ $actionID = Util::getFormData('actionID'); @@ -230,13 +240,18 @@ $selfURL = Horde::applicationUrl('smime.php'); /* If S/MIME preference not active, or openssl PHP extension not available, do * NOT show S/MIME Admin screen. */ -$openssl_check = $imp_smime->checkForOpenSSL(); +try { + $imp_smime->checkForOpenSSL(); + $openssl_check = true; +} catch + $openssl_check = false; +} /* If S/MIME preference not active, do NOT show S/MIME Admin screen. */ $t = new IMP_Template(); $t->setOption('gettext', true); $t->set('use_smime_help', Help::link('imp', 'smime-overview')); -if (!is_a($openssl_check, 'PEAR_Error') && $prefs->getValue('use_smime')) { +if ($openssl_check && $prefs->getValue('use_smime')) { Horde::addScriptFile('imp.js', 'imp', true); $t->set('smimeactive', true); $t->set('manage_pubkey-help', Help::link('imp', 'smime-manage-pubkey')); -- 2.11.0