From 211929876b84beba330a8fed2130feaed616cd9f Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Sun, 6 Sep 2015 15:52:31 +0200 Subject: [PATCH] Enigma: GPG keys export --- plugins/enigma/README | 18 ++------ plugins/enigma/enigma.js | 38 +++++++++++++---- plugins/enigma/lib/enigma_driver.php | 9 ++++ plugins/enigma/lib/enigma_driver_gnupg.php | 10 +++++ plugins/enigma/lib/enigma_engine.php | 31 ++++++++++++++ plugins/enigma/lib/enigma_ui.php | 41 +++++++++++++++++-- plugins/enigma/localization/en_US.inc | 1 + plugins/enigma/skins/classic/enigma.css | 2 +- .../enigma/skins/classic/templates/keys.html | 13 +++++- plugins/enigma/skins/larry/enigma.css | 2 +- .../enigma/skins/larry/templates/keys.html | 15 +++++-- 11 files changed, 145 insertions(+), 35 deletions(-) diff --git a/plugins/enigma/README b/plugins/enigma/README index 1cd0e2def..ac20b790a 100644 --- a/plugins/enigma/README +++ b/plugins/enigma/README @@ -8,25 +8,19 @@ The plugin uses gpg binary on the server and stores all keys Encryption/decryption is done server-side. So, this plugin is for users that trust the server. -WARNING! The plugin is in very early state. See below for a list -of missing features and known issues. - Implemented features: --------------------- + PGP: signatures verification + PGP: messages decryption + PGP: Sending of encrypted/signed messages -+ PGP: keys management UI (key import, delete) ++ PGP: keys management UI (key import, export, delete) + PGP: key generation (client- or server-side) + Handling of PGP keys attached to incoming messages + User preferences to disable plugin features -TODO (must have): ------------------ -- Keys export to file -TODO (later): +TODO: ------------- - Handling of big messages with temp files - Key info in contact details page (optional) @@ -35,6 +29,7 @@ TODO (later): - revoke, - change expiration date, change passphrase, add photo, - manage user IDs + - export private keys - Generate revocation certs - Search filter to see invalid/expired keys - Key server(s) support (search, import, upload, refresh) @@ -53,10 +48,3 @@ TODO (later): - S/MIME: Sending signed/encrypted messages - S/MIME: Handling of certs attached to incoming messages - S/MIME: Certificate info in Contacts details page (optional) - -Known issues: -------------- -1. There are Crypt_GPG issues when using gnupg >= 2.0 - - http://pear.php.net/bugs/bug.php?id=19914 - - http://pear.php.net/bugs/bug.php?id=20453 - - http://pear.php.net/bugs/bug.php?id=20527 diff --git a/plugins/enigma/enigma.js b/plugins/enigma/enigma.js index 8479c7944..c9a2a750b 100644 --- a/plugins/enigma/enigma.js +++ b/plugins/enigma/enigma.js @@ -6,7 +6,7 @@ window.rcmail && rcmail.addEventListener('init', function(evt) { if (rcmail.gui_objects.keyslist) { rcmail.keys_list = new rcube_list_widget(rcmail.gui_objects.keyslist, - {multiselect:false, draggable:false, keyboard:false}); + {multiselect:true, draggable:false, keyboard:false}); rcmail.keys_list .addEventListener('select', function(o) { rcmail.enigma_keylist_select(o); }) .addEventListener('keypress', function(o) { rcmail.enigma_keylist_keypress(o); }) @@ -25,11 +25,16 @@ window.rcmail && rcmail.addEventListener('init', function(evt) { rcmail.register_command('search', function(props) {return rcmail.enigma_search(props); }, true); rcmail.register_command('reset-search', function(props) {return rcmail.enigma_search_reset(props); }, true); rcmail.register_command('plugin.enigma-import', function() { rcmail.enigma_import(); }, true); -// rcmail.register_command('plugin.enigma-export', function() { rcmail.enigma_key_export(); }, true); - rcmail.register_command('plugin.enigma-key-import', function() { rcmail.enigma_key_import() }, true); - rcmail.register_command('plugin.enigma-key-delete', function(props) { return rcmail.enigma_key_delete(); }); + rcmail.register_command('plugin.enigma-key-export', function() { rcmail.enigma_export(); }); + rcmail.register_command('plugin.enigma-key-export-selected', function() { rcmail.enigma_export(true); }); + rcmail.register_command('plugin.enigma-key-import', function() { rcmail.enigma_key_import(); }, true); + rcmail.register_command('plugin.enigma-key-delete', function(props) { return rcmail.enigma_delete(); }); rcmail.register_command('plugin.enigma-key-create', function(props) { return rcmail.enigma_key_create(); }, true); rcmail.register_command('plugin.enigma-key-save', function(props) { return rcmail.enigma_key_create_save(); }, true); + + rcmail.addEventListener('responseafterplugin.enigmakeys', function() { + rcmail.enable_command('plugin.enigma-key-export', rcmail.env.rowcount > 0); + }); } } else if (rcmail.env.task == 'mail') { @@ -125,7 +130,7 @@ rcube_webmail.prototype.enigma_key_create_success = function() }; // Delete key(s) -rcube_webmail.prototype.enigma_key_delete = function() +rcube_webmail.prototype.enigma_delete = function() { var keys = this.keys_list.get_selection(); @@ -139,6 +144,17 @@ rcube_webmail.prototype.enigma_key_delete = function() this.http_post('plugin.enigmakeys', post, lock); }; +// Export key(s) +rcube_webmail.prototype.enigma_export = function(selected) +{ + var keys = selected ? this.keys_list.get_selection().join(',') : '*'; + + if (!keys.length) + return; + + this.goto_url('plugin.enigmakeys', {_a: 'export', _keys: keys}); +}; + // Submit key(s) import form rcube_webmail.prototype.enigma_import = function() { @@ -163,11 +179,13 @@ rcube_webmail.prototype.enigma_import = function() // list row selection handler rcube_webmail.prototype.enigma_keylist_select = function(list) { - var id; - if (id = list.get_single_selection()) - this.enigma_loadframe('&_action=plugin.enigmakeys&_a=info&_id=' + id); + var id = list.get_single_selection(), url; - this.enable_command('plugin.enigma-key-delete', list.selection.length > 0); + if (id) + url = '&_action=plugin.enigmakeys&_a=info&_id=' + id; + + this.enigma_loadframe(url); + this.enable_command('plugin.enigma-key-delete', 'plugin.enigma-key-export-selected', list.selection.length > 0); }; rcube_webmail.prototype.enigma_keylist_keypress = function(list) @@ -275,6 +293,8 @@ rcube_webmail.prototype.enigma_clear_list = function() this.enigma_loadframe(); if (this.keys_list) this.keys_list.clear(true); + + this.enable_command('plugin.enigma-key-delete', 'plugin.enigma-key-delete-selected', false); } // Adds a row to the list diff --git a/plugins/enigma/lib/enigma_driver.php b/plugins/enigma/lib/enigma_driver.php index 6326ef79e..10c6d1c04 100644 --- a/plugins/enigma/lib/enigma_driver.php +++ b/plugins/enigma/lib/enigma_driver.php @@ -69,6 +69,15 @@ abstract class enigma_driver */ abstract function import($content, $isfile = false); + /** + * Key/Cert export. + * + * @param string Key ID + * + * @return mixed Key content or enigma_error + */ + abstract function export($key); + /** * Keys listing. * diff --git a/plugins/enigma/lib/enigma_driver_gnupg.php b/plugins/enigma/lib/enigma_driver_gnupg.php index c7fc2dce2..1339c7bbf 100644 --- a/plugins/enigma/lib/enigma_driver_gnupg.php +++ b/plugins/enigma/lib/enigma_driver_gnupg.php @@ -158,6 +158,16 @@ class enigma_driver_gnupg extends enigma_driver } } + public function export($keyid) + { + try { + return $this->gpg->exportPublicKey($keyid, true); + } + catch (Exception $e) { + return $this->get_error_from_exception($e); + } + } + public function list_keys($pattern='') { try { diff --git a/plugins/enigma/lib/enigma_engine.php b/plugins/enigma/lib/enigma_engine.php index 58982ca0b..aa6a7284e 100644 --- a/plugins/enigma/lib/enigma_engine.php +++ b/plugins/enigma/lib/enigma_engine.php @@ -1002,6 +1002,37 @@ class enigma_engine $this->rc->output->send(); } + /** + * PGP keys/certs export.. + * + * @param string Key ID + * @param resource Optional output stream + * + * @return mixed Key content or enigma_error + */ + function export_key($key, $fp = null) + { + $this->load_pgp_driver(); + $result = $this->pgp_driver->export($key, $fp); + + 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; + } + + if ($fp) { + fwrite($fp, $result); + } + else { + return $result; + } + } + /** * Registers password for specified key/cert sent by the password prompt. */ diff --git a/plugins/enigma/lib/enigma_ui.php b/plugins/enigma/lib/enigma_ui.php index 8bb29d6df..455821bc2 100644 --- a/plugins/enigma/lib/enigma_ui.php +++ b/plugins/enigma/lib/enigma_ui.php @@ -59,6 +59,10 @@ class enigma_ui $this->key_import(); break; + case 'export': + $this->key_export(); + break; + case 'generate': $this->key_generate(); break; @@ -260,6 +264,7 @@ class enigma_ui } } + $this->rc->output->set_env('rowcount', $size); $this->rc->output->set_env('search_request', $search); $this->rc->output->set_env('pagecount', ceil($listsize/$pagesize)); $this->rc->output->set_env('current_page', $page); @@ -400,6 +405,35 @@ class enigma_ui return $out; } + /** + * Key(s) export handler + */ + private function key_export() + { + $keys = rcube_utils::get_input_value('_keys', rcube_utils::INPUT_GPC); + $engine = $this->enigma->load_engine(); + $list = $keys == '*' ? $engine->list_keys() : explode(',', $keys); + + if (is_array($list)) { + $filename = 'export.pgp'; + if (count($list) == 1) { + $filename = (is_object($list[0]) ? $list[0]->id : $list[0]) . '.pgp'; + } + + // send downlaod headers + header('Content-Type: application/pgp-keys'); + header('Content-Disposition: attachment; filename="' . $filename . '"'); + + if ($fp = fopen('php://output', 'w')) { + foreach ($list as $key) { + $engine->export_key(is_object($key) ? $key->id : $key, $fp); + } + } + } + + exit; + } + /** * Key import (page) handler */ @@ -594,12 +628,11 @@ class enigma_ui */ private function key_delete() { - $keys = rcube_utils::get_input_value('_keys', rcube_utils::INPUT_POST); - - $this->enigma->load_engine(); + $keys = rcube_utils::get_input_value('_keys', rcube_utils::INPUT_POST); + $engine = $this->enigma->load_engine(); foreach ((array)$keys as $key) { - $res = $this->enigma->engine->delete_key($key); + $res = $engine->delete_key($key); if ($res !== true) { $this->rc->output->show_message('enigma.keyremoveerror', 'error'); diff --git a/plugins/enigma/localization/en_US.inc b/plugins/enigma/localization/en_US.inc index f4f6d54b9..4e4984a84 100644 --- a/plugins/enigma/localization/en_US.inc +++ b/plugins/enigma/localization/en_US.inc @@ -66,6 +66,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'; $messages = array(); $messages['sigvalid'] = 'Verified signature from $sender.'; diff --git a/plugins/enigma/skins/classic/enigma.css b/plugins/enigma/skins/classic/enigma.css index bbc4af99d..2f5d331e4 100644 --- a/plugins/enigma/skins/classic/enigma.css +++ b/plugins/enigma/skins/classic/enigma.css @@ -163,7 +163,7 @@ div.enigmascreen width: 32px; height: 32px; padding: 0; - margin-right: 10px; + margin: 0 5px; overflow: hidden; background: url(keys_toolbar.png) 0 0 no-repeat transparent; opacity: 0.99; /* this is needed to make buttons appear correctly in Chrome */ diff --git a/plugins/enigma/skins/classic/templates/keys.html b/plugins/enigma/skins/classic/templates/keys.html index 5de74fd2c..fea7340a5 100644 --- a/plugins/enigma/skins/classic/templates/keys.html +++ b/plugins/enigma/skins/classic/templates/keys.html @@ -24,9 +24,10 @@
- + +
@@ -36,6 +37,7 @@ +
@@ -78,6 +80,13 @@
+
+
    +
  • +
  • +
+
+ diff --git a/plugins/enigma/skins/larry/enigma.css b/plugins/enigma/skins/larry/enigma.css index 0f393a022..f9ea49999 100644 --- a/plugins/enigma/skins/larry/enigma.css +++ b/plugins/enigma/skins/larry/enigma.css @@ -161,5 +161,5 @@ p.enigmaattachment a { } #keystoolbar a.export { - background-position: center 0; + background-position: center -40px; } diff --git a/plugins/enigma/skins/larry/templates/keys.html b/plugins/enigma/skins/larry/templates/keys.html index 35b137e9a..6996904c2 100644 --- a/plugins/enigma/skins/larry/templates/keys.html +++ b/plugins/enigma/skins/larry/templates/keys.html @@ -16,9 +16,18 @@

+ +