From 6e3acf6b8321c5c2b1dc06ec281b5a696957b076 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Mon, 24 Nov 2008 21:43:22 -0700 Subject: [PATCH] Updates to S/MIME viewer. Still not working. Renamed from pkcs7 to smime. --- imp/config/mime_drivers.php.dist | 10 +-- imp/lib/Crypt/smime.php | 14 ++-- imp/lib/Mime/Viewer/{pkcs7.php => smime.php} | 113 ++++++++------------------- imp/smime.php | 13 ++- 4 files changed, 53 insertions(+), 97 deletions(-) rename imp/lib/Mime/Viewer/{pkcs7.php => smime.php} (76%) diff --git a/imp/config/mime_drivers.php.dist b/imp/config/mime_drivers.php.dist index 2082cbd58..6b9f44e49 100644 --- a/imp/config/mime_drivers.php.dist +++ b/imp/config/mime_drivers.php.dist @@ -25,18 +25,18 @@ * partial message/partial parts * pdf Portable Document Format (PDF) files * pgp PGP signed/encrypted messages - * pkcs7 S/MIME signed/encrypted messages * plain URL syntax highlighting for text/plain parts * related multipart/related parts * smil SMIL documents + * smime S/MIME signed/encrypted messages * status Mail delivery status messages * tnef MS-TNEF attachments * zip ZIP attachments */ $mime_drivers_map['imp']['registered'] = array( 'alternative', 'appledouble', 'enriched', 'html', 'images', 'itip', - 'mdn', 'partial', 'pdf', 'pgp', 'pkcs7', 'plain', 'related', - 'smil', 'status', 'tnef', 'zip' + 'mdn', 'partial', 'pdf', 'pgp', 'plain', 'related', 'smil', 'smime', + 'status', 'tnef', 'zip' ); /** @@ -164,9 +164,9 @@ $mime_drivers['imp']['pgp'] = array( ); /** - * PKCS7 settings (S/MIME) + * S/MIME settings */ -$mime_drivers['imp']['pkcs7'] = array( +$mime_drivers['imp']['smime'] = array( 'inline' => true, 'handles' => array( 'application/x-pkcs7-signature', 'application/x-pkcs7-mime', diff --git a/imp/lib/Crypt/smime.php b/imp/lib/Crypt/smime.php index 7983fb07d..5ba5c089d 100644 --- a/imp/lib/Crypt/smime.php +++ b/imp/lib/Crypt/smime.php @@ -322,19 +322,15 @@ class IMP_Horde_Crypt_smime extends Horde_Crypt_smime /** * Generates the javascript code for saving public keys. * - * @param MIME_Part $mime_part The MIME_Part containing the public key. + * @param string $mailbox The mailbox of the message. + * @param integer $uid The UID of the message. + * @param string $id The MIME ID of the message. * * @return string The URL for saving public keys. */ - public function savePublicKeyURL($mime_part) + public function savePublicKeyURL($mailbox, $uid, $id) { - if (empty($cache)) { - require_once 'Horde/SessionObjects.php'; - $cacheSess = &Horde_SessionObjects::singleton(); - $oid = $cacheSess->storeOid($mime_part); - } - - return $this->getJSOpenWinCode('save_attachment_public_key', false, array('cert' => $oid)); + return $this->getJSOpenWinCode('save_attachment_public_key', false, array('mailbox' => $mailbox, 'uid' => $uid, 'mime_id' => $id)); } /** diff --git a/imp/lib/Mime/Viewer/pkcs7.php b/imp/lib/Mime/Viewer/smime.php similarity index 76% rename from imp/lib/Mime/Viewer/pkcs7.php rename to imp/lib/Mime/Viewer/smime.php index ce0914a8e..62f91a40e 100644 --- a/imp/lib/Mime/Viewer/pkcs7.php +++ b/imp/lib/Mime/Viewer/smime.php @@ -1,6 +1,6 @@ * @package Horde_Mime_Viewer */ -class IMP_Horde_Mime_Viewer_pkcs7 extends Horde_Mime_Viewer_Driver +class IMP_Horde_Mime_Viewer_smime extends Horde_Mime_Viewer_Driver { /** * Can this driver render various views? @@ -62,7 +62,7 @@ class IMP_Horde_Mime_Viewer_pkcs7 extends Horde_Mime_Viewer_Driver $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')) { + if (is_a($openssl_check, 'PEAR_Error')) { $this->_impsmime = null; } } @@ -111,7 +111,8 @@ class IMP_Horde_Mime_Viewer_pkcs7 extends Horde_Mime_Viewer_Driver } else { $mime = &$this->mime_part; $signenc = $mime->getInformation('smime_signenc'); - $raw_text = $this->_getRawSMIMEText(); +// TODO + $raw_text = Horde_Imap_Client::removeBareNewlines($this->_params['contents']->getBodyPart($signed_id, array('mimeheaders' => true))); if ($signenc && $mime->getInformation('smime_from')) { $smime_from = $mime->getInformation('smime_from'); $raw_text = "From: $smime_from\n" . $raw_text; @@ -159,8 +160,8 @@ class IMP_Horde_Mime_Viewer_pkcs7 extends Horde_Mime_Viewer_Driver $status[] = _("This message has been digitally signed via S/MIME."); - /* Store S/MIME results in $sig_result. */ - $raw_text = $this->_getRawSMIMEText(); + $raw_text = Horde_Imap_Client::removeBareNewlines($this->_params['contents']->getBodyPart($signed_id, array('mimeheaders' => true))); + $sig_result = null; if ($GLOBALS['prefs']->getValue('smime_verify') || Util::getFormData('smime_verify_msg')) { @@ -169,21 +170,39 @@ class IMP_Horde_Mime_Viewer_pkcs7 extends Horde_Mime_Viewer_Driver ($_SESSION['imp']['viewmode'] == 'imp')) { // TODO: Fix to work with DIMP $status[] = Horde::link(Util::addParameter(Horde::selfUrl(true), 'smime_verify_msg', 1)) . _("Click HERE to verify the message.") . ''; + return $ret; } + $subpart = $this->_params['contents']->getBodyPart($sig_id); if (!isset($subpart)) { $msg_data = $this->_impsmime->extractSignedContents($raw_text); if (is_a($msg_data, 'PEAR_Error')) { $this->_status[] = $msg_data->getMessage(); - $mime_message = $mime; + $mime_message = $this->_mimepart; } else { - $mime_message = Horde_Mime_Message::parseMessage($msg_data); + $mime_message = Horde_Mime_Part::parseMessage($msg_data); } } - $text = $this->_outputStatus(); - if (!is_null($sig_result)) { - $text .= $this->_outputSMIMESignatureTest($sig_result->result, $sig_result->email); + $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)); + } + if (!empty($sig_result->cert)) { $cert_details = $this->_impsmime->parseCert($sig_result->cert); if (isset($cert_details['certificate']['subject']['CommonName'])) { @@ -194,24 +213,20 @@ class IMP_Horde_Mime_Viewer_pkcs7 extends Horde_Mime_Viewer_Driver $subject = $sig_result->email; } elseif (isset($smime_from)) { $subject = $smime_from; - } elseif (($from = $this->_headers->getValue('from'))) { - $subject = $from; } else { $subject = null; } + if (isset($subpart) && !empty($subject) && $GLOBALS['registry']->hasMethod('contacts/addField') && $GLOBALS['prefs']->getValue('add_source')) { - $this->_status[] = sprintf(_("The S/MIME certificate of %s: "), @htmlspecialchars($subject, ENT_COMPAT, NLS::getCharset())) . - $this->_contents->linkViewJS($subpart, 'view_attach', _("View"), '', null, array('viewkey' => 1)) . '/' . - Horde::link('#', '', null, null, $this->_impsmime->savePublicKeyURL($sig_result->cert) . ' return false;') . _("Save in your Address Book") . ''; - $text .= $this->_outputStatus(); + $status[] = sprintf(_("The S/MIME certificate of %s: "), @htmlspecialchars($subject, ENT_COMPAT, NLS::getCharset())) . $this->_params['contents']->linkViewJS($subpart, 'view_attach', _("View"), array('params' => array('viewkey' => 1))) . '/' . Horde::link('#', '', null, null, $this->_impsmime->savePublicKeyURL($sig_result->cert) . ' return false;') . _("Save in your Address Book") . ''; } } } - return array(); + return $ret; } /** @@ -292,44 +307,6 @@ class IMP_Horde_Mime_Viewer_pkcs7 extends Horde_Mime_Viewer_Driver } /** - * Return text/html as the content-type. - * - * @return string "text/html" constant. - */ - public function getType() - { - return 'text/html; charset=' . NLS::getCharset(); - } - - /** - * Get the headers of the S/MIME message. - */ - protected function _getRawSMIMEText() - { - $mime->setContents($this->_contents->getBody()); - if (is_a($this->_contents, 'IMP_Contents') && - (($mime->getMIMEId() == 0) || - ($mime->splitContents() == false))) { - $this->_headers = $this->_contents->getHeaderOb(); - return $this->_contents->fullMessageText(); - } else { - $header_text = $mime->getCanonicalContents(); - $header_text = substr($header_text, 0, strpos($header_text, "\r\n\r\n")); - $this->_headers = MIME_Headers::parseHeaders($header_text); - - $mime_headers = new MIME_Headers(); - foreach (array('Content-Type', 'From', 'To') as $val) { - $tmp = $this->_headers->getValue($val); - if (!empty($tmp)) { - $mime_headers->addHeader($val, $tmp); - } - } - - return $mime_headers->toString() . $mime->toCanonicalString(); - } - } - - /** * Generates HTML output for the S/MIME signature test. * * @param string $result Result string of the S/MIME output concerning @@ -340,30 +317,6 @@ class IMP_Horde_Mime_Viewer_pkcs7 extends Horde_Mime_Viewer_Driver */ protected function _outputSMIMESignatureTest($result, $email) { - $text = ''; - - if (is_a($result, 'PEAR_Error')) { - if ($result->getCode() == 'horde.warning') { - $this->_initStatus($GLOBALS['registry']->getImageDir('horde') . '/alerts/warning.png', _("Warning")); - } else { - $this->_initStatus($GLOBALS['registry']->getImageDir('horde') . '/alerts/error.png', _("Error")); - } - $result = $result->getMessage(); - } else { - $this->_initStatus($GLOBALS['registry']->getImageDir('horde') . '/alerts/success.png', _("Success")); - /* This message has been verified but there was no output - from the PGP program. */ - if (empty($result) || ($result === true)) { - $email = (is_array($email)) ? implode(', ', $email): $email; - $result = sprintf(_("The message has been verified. Sender: %s."), htmlspecialchars($email)); - } - } - - require_once 'Horde/Text/Filter.php'; - - $this->_status[] = Text_Filter::filter($result, 'text2html', array('parselevel' => TEXT_HTML_NOHTML)); - - return $this->_outputStatus(); } /** diff --git a/imp/smime.php b/imp/smime.php index 790087de5..a683a58db 100644 --- a/imp/smime.php +++ b/imp/smime.php @@ -224,11 +224,18 @@ case 'process_import_personal_certs': exit; case 'save_attachment_public_key': - $cacheSess = &Horde_SessionObjects::singleton(); - $cert = $cacheSess->query(Util::getFormData('cert')); + /* Retrieve the key from the message. */ + $contents = &IMP_Contents::singleton(Util::getFormData('uid') . IMP::IDX_SEP . Util::getFormData('mailbox')); + if (is_a($contents, 'PEAR_Error')) { + Horde::fatal($contents, __FILE__, __LINE__); + } + $mime_part = $contents->getMIMEPart(Util::getFormData('mime_id')); + if (empty($mime_part)) { + Horde::fatal('Cannot retrieve public key from message.', __FILE__, __LINE__); + } /* Add the public key to the storage system. */ - $cert = $imp_smime->addPublicKey($cert); + $cert = $imp_smime->addPublicKey($mime_part); if ($cert == false) { $notification->push(_("No Certificate found"), 'horde.error'); } else { -- 2.11.0