1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
|
<?php
require_once IMP_BASE . '/lib/Crypt/PGP.php';
/**
* The IMP_MIME_Viewer_pgp class allows viewing/decrypting of PGP
* formatted messages. This class implements RFC 3156.
*
* This class handles the following MIME types:
* application/pgp-encryption
* application/pgp-keys
* application/pgp-signature
*
* This class may add the following parameters to the URL:
* 'pgp_verify_msg' -- Do verification of PGP signed data.
* 'rawpgpkey' -- Display the PGP Public Key in raw, text format
*
* $Horde: imp/lib/MIME/Viewer/pgp.php,v 1.96.6.23 2008/05/08 07:03:23 slusarz Exp $
*
* Copyright 2002-2008 The Horde Project (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
*
* @author Michael Slusarz <slusarz@horde.org>
* @package Horde_MIME_Viewer
*/
class IMP_MIME_Viewer_pgp extends MIME_Viewer {
/**
* IMP_PGP object.
*
* @var IMP_PGP
*/
var $_imp_pgp;
/**
* The address of the sender.
*
* @var string
*/
var $_address;
/**
* Pointer to the MIME_Contents item.
*
* @var MIME_Contents
*/
var $_contents = null;
/**
* Classwide cache for icons for status messages.
*
* @var string
*/
var $_icon = null;
/**
* Classwide cache for status messages.
*
* @var array
*/
var $_status = array();
/**
* The MIME content-type of this part.
*
* @var string
*/
var $_type = 'text/html';
/**
* Render out the currently set contents.
*
* @param array $params An array with a reference to a MIME_Contents
* object.
*
* @return string The rendered text in HTML.
*/
function render($params)
{
global $conf, $prefs;
/* Set the MIME_Contents class variable. */
$this->_contents = &$params[0];
$msg = '';
if (empty($this->_imp_pgp) && !empty($conf['utils']['gnupg'])) {
$this->_imp_pgp = new IMP_PGP();
}
/* Determine the address of the sender. */
if (empty($this->_address)) {
$base_ob = &$this->_contents->getBaseObjectPtr();
$this->_address = $base_ob->getFromAddress();
}
/* We need to insert JavaScript code now if PGP support is active. */
if (!empty($conf['utils']['gnupg']) &&
$prefs->getValue('use_pgp') &&
!Util::getFormData('rawpgpkey')) {
$msg = Util::bufferOutput(array('Horde', 'addScriptFile'), 'prototype.js', 'imp', true);
$msg .= Util::bufferOutput(array('Horde', 'addScriptFile'), 'popup.js', 'imp', true);
}
/* For RFC 2015/3156, there are 3 types of messages:
+ multipart/encrypted
+ multipart/signed
+ application/pgp-keys */
switch ($this->mime_part->getType()) {
case 'application/pgp-keys':
$msg .= $this->_outputPGPKey();
break;
case 'multipart/signed':
case 'application/pgp-signature':
$msg .= $this->_outputPGPSigned();
break;
case 'multipart/encrypted':
case 'application/pgp-encrypted':
$msg .= $this->_outputPGPEncrypted();
break;
}
return $msg;
}
/**
* Return the content-type of the output.
*
* @return string The MIME content type of the output.
*/
function getType()
{
return $this->_type;
}
/**
* Generates HTML output for 'application/pgp-keys' MIME_Parts..
*
* @access private
*
* @return string The HTML output.
*/
function _outputPGPKey()
{
global $conf, $prefs;
$mime = &$this->mime_part;
$part = $this->_contents->getDecodedMIMEPart($mime->getMIMEId());
if (empty($conf['utils']['gnupg'])) {
$text = '<pre>' . $part->getContents() . '</pre>';
} elseif (Util::getFormData('rawpgpkey')) {
$text = $part->getContents();
$this->_type = 'text/plain';
} else {
require_once 'Horde/Text.php';
$pgp_key = $mime->getContents();
/* Initialize status message. */
$this->_initStatus($this->getIcon($mime->getType()), _("PGP"));
$msg = _("This PGP Public Key was attached to the message.");
if ($prefs->getValue('use_pgp') &&
$GLOBALS['registry']->hasMethod('contacts/addField') &&
$prefs->getValue('add_source')) {
$msg .= ' ' . Horde::link('#', '', '', '', $this->_imp_pgp->savePublicKeyURL($mime) . ' return false;') . _("[Save the key to your Address book]") . '</a>';
}
$this->_status[] = $msg . ' ' . $this->_contents->linkViewJS($part, 'view_attach', _("[View the raw key]"), '', null, array('rawpgpkey' => 1));
$text = $this->_outputStatus(false) .
'<span class="fixed">' . nl2br(str_replace(' ', ' ', $this->_imp_pgp->pgpPrettyKey($pgp_key))) . '</span>';
}
return $text;
}
/**
* Generates HTML output for 'multipart/signed' and
* 'application/pgp-signature' MIME_Parts.
*
* @access private
*
* @return string The HTML output.
*/
function _outputPGPSigned()
{
global $conf, $prefs;
$active = ($prefs->getValue('use_pgp') && !empty($conf['utils']['gnupg']));
$mime = &$this->mime_part;
$mimetype = $mime->getType();
$text = '';
$signenc = $mime->getInformation('pgp_signenc');
if (!$active) {
if ($signenc) {
$this->_status[] = _("The message below has been digitally signed and encrypted with PGP, but the signature cannot be verified.");
} else {
$this->_status[] = _("The message below has been digitally signed with PGP, but the signature cannot be verified.");
}
} else {
if ($signenc) {
$this->_status[] = _("The message below has been digitally signed and encrypted with PGP.");
} else {
$this->_status[] = _("The message below has been digitally signed with PGP.");
}
}
$this->_initStatus($this->getIcon($mimetype), _("PGP"));
/* Store PGP results in $sig_result; store text in $data. */
$sig_result = null;
if ($mimetype == 'multipart/signed') {
/* If the MIME ID is 0, we need to store the body of the message
in the MIME_Part object. */
if (!$signenc) {
if (($mimeID = $mime->getMIMEId())) {
$mime->setContents($this->_contents->getBodyPart($mimeID));
} else {
$mime->setContents($this->_contents->getBody());
}
$mime->splitContents();
}
/* Data that is signed appears in the first MIME subpart. */
$subpart = $mime->getPart($mime->getRelativeMIMEId(1));
$signature_data = rtrim($subpart->getCanonicalContents(), "\r");
require_once 'Horde/MIME/Structure.php';
$mime_message = &MIME_Structure::parseTextMIMEMessage($signature_data);
/* The PGP signature appears in the second MIME subpart. */
$subpart = $mime->getPart($mime->getRelativeMIMEId(2));
if ($subpart && $subpart->getType() == 'application/pgp-signature') {
if ($active) {
if ($GLOBALS['prefs']->getValue('pgp_verify') ||
Util::getFormData('pgp_verify_msg')) {
$subpart->transferDecodeContents();
$sig_result = $this->_imp_pgp->verifySignature($signature_data, $this->_address, $subpart->getContents());
} elseif (isset($_SESSION['imp']['viewmode']) &&
($_SESSION['imp']['viewmode'] == 'imp')) {
// TODO: Fix to work with DIMP
$base_ob = &$this->_contents->getBaseObjectPtr();
$this->_status[] = Horde::link(Util::addParameter(IMP::generateIMPUrl(Horde::selfUrl(), $GLOBALS['imp_mbox']['mailbox'], $GLOBALS['imp_mbox']['index'], $GLOBALS['imp_mbox']['thismailbox']), 'pgp_verify_msg', 1)) . _("Click HERE to verify the message.") . '</a>';
}
}
} else {
$this->_status[] = _("The message below does not appear to be in the correct PGP format (according to RFC 2015).");
}
} elseif ($mimetype == 'application/pgp-signature') {
/* Get the signed message to output. */
require_once 'Horde/MIME/Message.php';
$mime_message = new MIME_Message();
$mime_message->setType('text/plain');
$mime->transferDecodeContents();
if (empty($this->_imp_pgp)) {
$mime_message->setContents($mime->getContents());
} else {
$mime_message->setContents($this->_imp_pgp->getSignedMessage($mime));
}
/* Pass the signed text straight through to PGP program */
if ($active) {
if ($GLOBALS['prefs']->getValue('pgp_verify') ||
Util::getFormData('pgp_verify_msg')) {
$sig_result = $this->_imp_pgp->verifySignature($mime->getContents(), $this->_address);
} elseif (isset($_SESSION['imp']['viewmode']) &&
($_SESSION['imp']['viewmode'] == 'imp')) {
// TODO: Fix to work with DIMP
$this->_status[] = Horde::link(Util::addParameter(Horde::selfUrl(true), 'pgp_verify_msg', 1)) . _("Click HERE to verify the message.") . '</a>';
}
}
}
$text = $this->_outputStatus();
if ($sig_result !== null) {
$text .= $this->_outputPGPSignatureTest($sig_result);
}
/* We need to stick the output into a MIME_Contents object. */
$mc = new MIME_Contents($mime_message, array('download' => 'download_attach', 'view' => 'view_attach'), array(&$this->_contents));
$mc->buildMessage();
return $text . '<table cellspacing="0">' . $mc->getMessage(true) . '</table>';
}
/**
* Generates HTML output for 'multipart/encrypted' and
* 'application/pgp-encrypted' MIME_Parts.
*
* @access private
*
* @return string The HTML output.
*/
function _outputPGPEncrypted()
{
global $conf, $prefs;
$mime = &$this->mime_part;
$mimetype = $mime->getType();
$text = '';
$this->_initStatus($this->getIcon($mimetype), _("PGP"));
/* Print out message now if PGP is not active. */
if (empty($conf['utils']['gnupg']) || !$prefs->getValue('use_pgp')) {
$this->_status[] = _("The message below has been encrypted with PGP, however, PGP support is disabled so the message cannot be decrypted.");
return $this->_outputStatus();
}
if ($mimetype == 'multipart/encrypted') {
/* PGP control information appears in the first MIME subpart. We
don't currently need to do anything with this information. The
encrypted data appears in the second MIME subpart. */
$subpart = $mime->getPart($mime->getRelativeMIMEId(2));
if (!$subpart) {
return $text;
}
$encrypted_data = $this->_contents->getBodyPart($subpart->getMIMEId());
} elseif ($mimetype == 'application/pgp-encrypted') {
$encrypted_data = $mime->getContents();
} else {
return $text;
}
/* Check if this is a literal compressed message. */
$info = $this->_imp_pgp->pgpPacketInformation($encrypted_data);
$literal = !empty($info['literal']);
/* Check if this a symmetrically encrypted message. */
$symmetric = is_callable(array($this->_imp_pgp, 'encryptedSymmetrically'))
? $this->_imp_pgp->encryptedSymmetrically($encrypted_data)
: false;
if ($symmetric && !$this->_imp_pgp->getSymmetricPassphrase()) {
if (isset($_SESSION['imp']['viewmode']) &&
($_SESSION['imp']['viewmode'] == 'imp')) {
// TODO: Fix to work with DIMP
/* Ask for the correct passphrase if this is encrypted
* symmetrically. */
$url = $this->_imp_pgp->getJSOpenWinCode('open_symmetric_passphrase_dialog');
$this->_status[] = Horde::link('#', _("The message below has been encrypted with PGP. You must enter the passphrase that was used to encrypt this message."), null, null, $url . ' return false;') . _("You must enter the passphrase that was used to encrypt this message.") . '</a>';
$text .= $this->_outputStatus() .
Util::bufferOutput(array('IMP', 'addInlineScript'), $url);
}
} elseif (!$literal && !$symmetric &&
!$this->_imp_pgp->getPersonalPrivateKey()) {
/* Output if there is no personal private key to decrypt with. */
$this->_status[] = _("The message below has been encrypted with PGP, however, no personal private key exists so the message cannot be decrypted.");
return $this->_outputStatus();
} elseif (!$literal && !$symmetric &&
!$this->_imp_pgp->getPassphrase()) {
if (isset($_SESSION['imp']['viewmode']) &&
($_SESSION['imp']['viewmode'] == 'imp')) {
// TODO: Fix to work with DIMP
/* Ask for the private key's passphrase if this is encrypted
* asymmetrically. */
$url = $this->_imp_pgp->getJSOpenWinCode('open_passphrase_dialog');
$this->_status[] = Horde::link('#', _("The message below has been encrypted with PGP. You must enter the passphrase for your PGP private key before it can be decrypted."), null, null, $url . ' return false;') . _("You must enter the passphrase for your PGP private key to view this message.") . '</a>';
$text .= $this->_outputStatus() .
Util::bufferOutput(array('IMP', 'addInlineScript'), $url);
}
} else {
/* Decrypt this message. */
$this->_status[] = $literal ? _("The message below has been compressed with PGP.") : _("The message below has been encrypted with PGP.");
if ($mimetype == 'multipart/encrypted') {
if ($subpart->getType() == 'application/octet-stream') {
$decrypted_data = $this->_imp_pgp->decryptMessage($encrypted_data, $symmetric, !$literal);
if (is_a($decrypted_data, 'PEAR_Error')) {
$this->_status[] = _("The message below does not appear to be a valid PGP encrypted message. Error: ") . $decrypted_data->getMessage();
$text .= $this->_outputStatus();
} else {
/* We need to check if this is a signed/encrypted
message. */
require_once 'Horde/MIME/Structure.php';
$mime_message = &MIME_Structure::parseTextMIMEMessage($decrypted_data->message);
if (!$mime_message) {
require_once 'Horde/Text/Filter.php';
$text .= $this->_signedOutput($decrypted_data->sig_result);
$decrypted_message = String::convertCharset($decrypted_data->message, $subpart->getCharset());
$text .= '<span class="fixed">' . Text_Filter::filter($decrypted_message, 'text2html', array('parselevel' => TEXT_HTML_SYNTAX)) . '</span>';
} else {
$mimetype = $mime_message->getType();
if (($mimetype == 'multipart/signed') ||
($mimetype == 'application/pgp-signature')) {
$mime_message->setInformation('pgp_signenc', true);
$mime_message->setContents($decrypted_data->message);
$mime_message->splitContents();
} else {
$text .= $this->_signedOutput($decrypted_data->sig_result);
}
/* We need to stick the output into a
MIME_Contents object. */
$mc = new MIME_Contents($mime_message, array('download' => 'download_attach', 'view' => 'view_attach'), array(&$this->_contents));
$mc->buildMessage();
if ($mime_message->getInformation('pgp_signenc')) {
$text .= $mc->getMessage(true);
} else {
$text .= '<table cellspacing="0">' . $mc->getMessage(true) . '</table>';
}
}
}
} else {
$this->_status[] = _("The message below does not appear to be in the correct PGP format (according to RFC 2015).");
$text .= $this->_outputStatus();
}
} elseif ($mimetype == 'application/pgp-encrypted') {
$decrypted_data = $this->_imp_pgp->decryptMessage($encrypted_data, $symmetric, !$literal);
if (is_a($decrypted_data, 'PEAR_Error')) {
$decrypted_message = $decrypted_data->getMessage();
$text .= $this->_outputStatus();
} else {
$text .= $this->_signedOutput($decrypted_data->sig_result);
$decrypted_message = String::convertCharset($decrypted_data->message, $mime->getCharset());
}
require_once 'Horde/Text/Filter.php';
$text .= '<span class="fixed">' . Text_Filter::filter($decrypted_message, 'text2html', array('parselevel' => TEXT_HTML_SYNTAX)) . '</span>';
}
}
$this->_imp_pgp->unsetSymmetricPassphrase();
return $text;
}
/**
* Generates HTML output for the PGP signature test.
*
* @access private
*
* @param string $result Result string of the PGP output concerning the
* signature test.
*
* @return string The HTML output.
*/
function _outputPGPSignatureTest($result)
{
$text = '';
if (is_a($result, 'PEAR_Error')) {
$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 = _("The message below has been verified.");
}
}
require_once 'Horde/Text/Filter.php';
$this->_status[] = Text_Filter::filter($result, 'text2html', array('parselevel' => TEXT_HTML_NOHTML));
return $this->_outputStatus();
}
/**
* Output signed message status.
*
* @access private
*
* @param string $result The signature result.
*
* @return string HTML output.
*/
function _signedOutput($result)
{
if (!empty($result)) {
return $this->_outputPGPSignatureTest($result);
} else {
return $this->_outputStatus();
}
}
/* Various formatting helper functions */
function _initStatus($src, $alt = '')
{
if ($this->_icon === null) {
$this->_icon = Horde::img($src, $alt, 'height="16" width="16"', '');
}
}
function _outputStatus($printable = true)
{
$output = '';
if (!empty($this->_status)) {
$output = $this->_contents->formatStatusMsg($this->_status, $this->_icon, $printable);
}
$this->_icon = null;
$this->_status = array();
return $output;
}
}
|