From a9d399155d205ae41015d7d205c6dacd7ecfc0d2 Mon Sep 17 00:00:00 2001 From: Kyle Francis Date: Wed, 30 Mar 2016 09:33:53 -0400 Subject: [PATCH 1/2] Implemented attaching pub PGP key to outgoing messages. --- plugins/enigma/lib/enigma_driver_gnupg.php | 11 ++++++++ plugins/enigma/lib/enigma_engine.php | 32 ++++++++++++++++++++++ plugins/enigma/lib/enigma_ui.php | 24 +++++++++++++++- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/plugins/enigma/lib/enigma_driver_gnupg.php b/plugins/enigma/lib/enigma_driver_gnupg.php index 5ddf724be..ab4aa4541 100644 --- a/plugins/enigma/lib/enigma_driver_gnupg.php +++ b/plugins/enigma/lib/enigma_driver_gnupg.php @@ -339,6 +339,17 @@ class enigma_driver_gnupg extends enigma_driver } } + public function pubkey_for_attach($email) + { + try { + $pubkey = $this->gpg->exportPublicKey($email, true); + return $pubkey; + } + catch (Exception $e) { + return $this->get_error_from_exception($e); + } + } + /** * Converts Crypt_GPG exception into Enigma's error object * diff --git a/plugins/enigma/lib/enigma_engine.php b/plugins/enigma/lib/enigma_engine.php index 96f792d90..d2f3972d2 100644 --- a/plugins/enigma/lib/enigma_engine.php +++ b/plugins/enigma/lib/enigma_engine.php @@ -921,6 +921,38 @@ class enigma_engine return $result; } + function get_gpg_pubkey_for_attach($email) + { + $this->load_pgp_driver(); + $result = $this->pgp_driver->pubkey_for_attach($email); + + if ($result instanceof enigma_error) { + rcube::raise_error(array( + 'code' => 600, 'type' => 'php', + 'file' => __FILE__, 'line' => __LINE__, + 'message' => "Enigma plugin: " . $result->getMessage() + ), true, false); + } + + return $result; + } + + function get_keyID($email) + { + $this->load_pgp_driver(); + $result = $this->pgp_driver->get_keyID($email); + + if ($result instanceof enigma_error) { + rcube::raise_error(array( + 'code' => 600, 'type' => 'php', + 'file' => __FILE__, 'line' => __LINE__, + 'message' => "Enigma plugin: " . $result->getMessage() + ), true, false); + } + + return $result; + } + /** * Find PGP private/public key * diff --git a/plugins/enigma/lib/enigma_ui.php b/plugins/enigma/lib/enigma_ui.php index 6ae69daff..f0963dca6 100644 --- a/plugins/enigma/lib/enigma_ui.php +++ b/plugins/enigma/lib/enigma_ui.php @@ -929,12 +929,16 @@ class enigma_ui } /** - * Handle message_ready hook (encryption/signing) + * Handle message_ready hook (encryption/signing/attach public key) */ function message_ready($p) { $savedraft = !empty($_POST['_draft']) && empty($_GET['_saveonly']); + if (!$savedraft && rcube_utils::get_input_value('_enigma_attachpubkey', rcube_utils::INPUT_POST)) { + $p = $this->attach_public($p); + } + if (!$savedraft && rcube_utils::get_input_value('_enigma_sign', rcube_utils::INPUT_POST)) { $this->enigma->load_engine(); $status = $this->enigma->engine->sign_message($p['message']); @@ -972,6 +976,24 @@ class enigma_ui } /** + * Add sender's public key (PGP). + */ + function attach_public($p) + { + // get sender's PGP pubkey for attachment + $this->enigma->load_engine(); + $key = $this->enigma->engine->list_keys($p['message']->headers()['From']); + $keyID = $key[0]->subkeys[0]->get_short_id(); + $pubkey_armor = $this->enigma->engine->get_gpg_pubkey_for_attach($p['message']->headers()['From']); + + if(!$pubkey_armor instanceof enigma_error) { + $p['message']->addAttachment($pubkey_armor, 'application/pgp-keys', "0x$keyID.asc", false); + } + + return $p; + } + + /** * Handler for message_compose_body hook * Display error when the message cannot be encrypted * and provide a way to try again with a password. From 7ce958ecd987745e545fc40fdc090c6430053b5d Mon Sep 17 00:00:00 2001 From: Kyle Francis Date: Fri, 1 Apr 2016 12:32:57 -0400 Subject: [PATCH 2/2] Added attach pubkey checkbox in compose UI, added preferences setting for attach pubkey by default --- plugins/enigma/config.inc.php.dist | 3 +++ plugins/enigma/enigma.php | 20 ++++++++++++++++++++ plugins/enigma/lib/enigma_ui.php | 5 +++++ plugins/enigma/localization/en_US.inc | 2 ++ 4 files changed, 30 insertions(+) diff --git a/plugins/enigma/config.inc.php.dist b/plugins/enigma/config.inc.php.dist index 17e3265b6..57dfcad81 100644 --- a/plugins/enigma/config.inc.php.dist +++ b/plugins/enigma/config.inc.php.dist @@ -28,6 +28,9 @@ $config['enigma_sign_all'] = false; // Enable encrypting all messages by default $config['enigma_encrypt_all'] = false; +// Enable signing all messages by default +$config['enigma_attach_pubkey'] = false; + // Default for how long to store private key passwords (in minutes). // When set to 0 passwords will be stored for the whole session. $config['enigma_password_time'] = 5; diff --git a/plugins/enigma/enigma.php b/plugins/enigma/enigma.php index 8e8ded2a1..c9471acbc 100644 --- a/plugins/enigma/enigma.php +++ b/plugins/enigma/enigma.php @@ -339,6 +339,25 @@ class enigma extends rcube_plugin ); } + if (!isset($no_override['enigma_attach_pubkey'])) { + if (!$p['current']) { + $p['blocks']['main']['content'] = true; + return $p; + } + + $field_id = 'rcmfd_enigma_attach_pubkey'; + $input = new html_checkbox(array( + 'name' => '_enigma_attach_pubkey', + 'id' => $field_id, + 'value' => 1, + )); + + $p['blocks']['main']['options']['enigma_encrypt_all'] = array( + 'title' => html::label($field_id, $this->gettext('attachpubkeydefault')), + 'content' => $input->show($this->rc->config->get('enigma_attach_pubkey') ? 1 : 0), + ); + } + if (!isset($no_override['enigma_password_time'])) { if (!$p['current']) { $p['blocks']['main']['content'] = true; @@ -380,6 +399,7 @@ class enigma extends rcube_plugin 'enigma_encryption' => (bool) rcube_utils::get_input_value('_enigma_encryption', rcube_utils::INPUT_POST), 'enigma_sign_all' => (bool) rcube_utils::get_input_value('_enigma_sign_all', rcube_utils::INPUT_POST), 'enigma_encrypt_all' => (bool) rcube_utils::get_input_value('_enigma_encrypt_all', rcube_utils::INPUT_POST), + 'enigma_attach_pubkey' => (bool) rcube_utils::get_input_value('_enigma_attach_pubkey', rcube_utils::INPUT_POST), 'enigma_password_time' => intval(rcube_utils::get_input_value('_enigma_password_time', rcube_utils::INPUT_POST)), ); } diff --git a/plugins/enigma/lib/enigma_ui.php b/plugins/enigma/lib/enigma_ui.php index f0963dca6..3ccacd45e 100644 --- a/plugins/enigma/lib/enigma_ui.php +++ b/plugins/enigma/lib/enigma_ui.php @@ -730,6 +730,11 @@ class enigma_ui $menu->add(null, $chbox->show($this->rc->config->get('enigma_encrypt_all') ? 1 : 0, array('name' => '_enigma_encrypt', 'id' => 'enigmaencryptopt'))); + $menu->add(null, html::label(array('for' => 'enigmaattachpubkeyopt'), + rcube::Q($this->enigma->gettext('attachpubkeymsg')))); + $menu->add(null, $chbox->show($this->rc->config->get('enigma_attach_pubkey') ? 1 : 0, + array('name' => '_enigma_attachpubkey', 'id' => 'enigmaattachpubkeyopt'))); + $menu = html::div(array('id' => 'enigmamenu', 'class' => 'popupmenu'), $menu->show()); // Options menu contents diff --git a/plugins/enigma/localization/en_US.inc b/plugins/enigma/localization/en_US.inc index 0e4d2b43d..5224ca556 100644 --- a/plugins/enigma/localization/en_US.inc +++ b/plugins/enigma/localization/en_US.inc @@ -53,6 +53,7 @@ $labels['supportsignatures'] = 'Enable message signatures verification'; $labels['supportdecryption'] = 'Enable message decryption'; $labels['signdefault'] = 'Sign all messages by default'; $labels['encryptdefault'] = 'Encrypt all messages by default'; +$labels['attachpubkeydefault'] = 'Attach my public PGP key by default'; $labels['passwordtime'] = 'Keep private key passwords for'; $labels['nminutes'] = '$m minute(s)'; $labels['wholesession'] = 'the whole session'; @@ -82,6 +83,7 @@ $labels['signmsg'] = 'Digitally sign this message'; $labels['enterkeypasstitle'] = 'Enter key passphrase'; $labels['enterkeypass'] = 'A passphrase is needed to unlock the secret key ($keyid) for user: $user.'; $labels['arialabelkeyexportoptions'] = 'Keys export options'; +$labels['attachpubkeymsg'] = 'Attach my public key'; $messages = array(); $messages['sigvalid'] = 'Verified signature from $sender.';