From 96c3d84cddb861956cfbc719d694eb972343f1c3 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Sat, 2 Apr 2016 10:35:17 +0200 Subject: [PATCH] Enigma: Varius fixes and cleanup in code for attaching pubkeys --- plugins/enigma/README | 2 +- plugins/enigma/composer.json | 6 +-- plugins/enigma/config.inc.php.dist | 2 +- plugins/enigma/enigma.php | 2 +- plugins/enigma/lib/enigma_driver_gnupg.php | 11 ---- plugins/enigma/lib/enigma_engine.php | 59 ++++++++++------------ plugins/enigma/lib/enigma_ui.php | 21 +------- 7 files changed, 35 insertions(+), 68 deletions(-) diff --git a/plugins/enigma/README b/plugins/enigma/README index ac20b790a..d9fc3799b 100644 --- a/plugins/enigma/README +++ b/plugins/enigma/README @@ -18,6 +18,7 @@ Implemented features: + PGP: key generation (client- or server-side) + Handling of PGP keys attached to incoming messages + User preferences to disable plugin features ++ Attaching public keys to email TODO: @@ -33,7 +34,6 @@ TODO: - Generate revocation certs - Search filter to see invalid/expired keys - Key server(s) support (search, import, upload, refresh) -- Attaching public keys to email - Mark keys as trusted/untrasted, display appropriate message in verify/decrypt status - Change attachment icon on messages list for encrypted messages (like vcard_attachment plugin does) - Support for multi-server installations (store keys in sql database?) diff --git a/plugins/enigma/composer.json b/plugins/enigma/composer.json index 3245ee7b3..4cfc44d54 100644 --- a/plugins/enigma/composer.json +++ b/plugins/enigma/composer.json @@ -3,7 +3,7 @@ "type": "roundcube-plugin", "description": "PGP Encryption for Roundcube", "license": "GPLv3+", - "version": "0.4", + "version": "0.5", "authors": [ { "name": "Aleksander Machniak", @@ -23,7 +23,7 @@ ], "require": { "php": ">=5.3.0", - "roundcube/plugin-installer": ">=0.1.3", - "pear-pear.php.net/crypt_gpg": "*" + "roundcube/plugin-installer": "~0.1.6", + "pear-pear.php.net/crypt_gpg": "~1.4.0" } } diff --git a/plugins/enigma/config.inc.php.dist b/plugins/enigma/config.inc.php.dist index 57dfcad81..2cce5ee95 100644 --- a/plugins/enigma/config.inc.php.dist +++ b/plugins/enigma/config.inc.php.dist @@ -28,7 +28,7 @@ $config['enigma_sign_all'] = false; // Enable encrypting all messages by default $config['enigma_encrypt_all'] = false; -// Enable signing all messages by default +// Enable attaching a public key to all messages by default $config['enigma_attach_pubkey'] = false; // Default for how long to store private key passwords (in minutes). diff --git a/plugins/enigma/enigma.php b/plugins/enigma/enigma.php index c9471acbc..2ebe2cc66 100644 --- a/plugins/enigma/enigma.php +++ b/plugins/enigma/enigma.php @@ -352,7 +352,7 @@ class enigma extends rcube_plugin 'value' => 1, )); - $p['blocks']['main']['options']['enigma_encrypt_all'] = array( + $p['blocks']['main']['options']['enigma_attach_pubkey'] = array( 'title' => html::label($field_id, $this->gettext('attachpubkeydefault')), 'content' => $input->show($this->rc->config->get('enigma_attach_pubkey') ? 1 : 0), ); diff --git a/plugins/enigma/lib/enigma_driver_gnupg.php b/plugins/enigma/lib/enigma_driver_gnupg.php index ab4aa4541..5ddf724be 100644 --- a/plugins/enigma/lib/enigma_driver_gnupg.php +++ b/plugins/enigma/lib/enigma_driver_gnupg.php @@ -339,17 +339,6 @@ 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 d2f3972d2..c97093329 100644 --- a/plugins/enigma/lib/enigma_engine.php +++ b/plugins/enigma/lib/enigma_engine.php @@ -312,6 +312,33 @@ class enigma_engine } } + /** + * Handler for attaching public key to a message + * + * @param Mail_mime Original message + * + * @return bool True on success, False on failure + */ + function attach_public_key(&$message) + { + $headers = $message->headers(); + $from = rcube_mime::decode_address_list($headers['From'], 1, false, null, true); + $from = $from[1]; + + // find my key + if ($from && ($key = $this->find_key($from))) { + $pubkey_armor = $this->export_key($key->id); + + if (!$pubkey_armor instanceof enigma_error) { + $pubkey_name = '0x' . enigma_key::format_id($key->id) . '.asc'; + $message->addAttachment($pubkey_armor, 'application/pgp-keys', $pubkey_name, false, '7bit'); + return true; + } + } + + return false; + } + /** * Handler for message_part_structure hook. * Called for every part of the message. @@ -921,38 +948,6 @@ 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 3ccacd45e..37c1d414a 100644 --- a/plugins/enigma/lib/enigma_ui.php +++ b/plugins/enigma/lib/enigma_ui.php @@ -941,7 +941,8 @@ class enigma_ui $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); + $this->enigma->load_engine(); + $this->enigma->engine->attach_public_key($p['message']); } if (!$savedraft && rcube_utils::get_input_value('_enigma_sign', rcube_utils::INPUT_POST)) { @@ -980,24 +981,6 @@ class enigma_ui return $p; } - /** - * 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