From 7829da358d80bc0a07b6b86dfc01f52abe7bce2f Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Sun, 25 Sep 2016 13:05:21 +0200 Subject: [PATCH] GnuPG 2.1: Fix importing newly generated (secret) keys using GnuPG 2.1 --- plugins/enigma/enigma.js | 5 +++-- plugins/enigma/lib/enigma_driver.php | 7 ++++--- plugins/enigma/lib/enigma_driver_gnupg.php | 14 ++++++++++---- plugins/enigma/lib/enigma_driver_phpssl.php | 2 +- plugins/enigma/lib/enigma_engine.php | 6 +++--- plugins/enigma/lib/enigma_ui.php | 3 ++- 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/plugins/enigma/enigma.js b/plugins/enigma/enigma.js index 081fea21b..3dc3e9a89 100644 --- a/plugins/enigma/enigma.js +++ b/plugins/enigma/enigma.js @@ -99,7 +99,7 @@ rcube_webmail.prototype.enigma_key_create_save = function() size = $('#key-size').val(); $('[name="identity[]"]:checked').each(function() { - users.push(this.value); + users.push(this.value); }); // validate the form @@ -124,7 +124,8 @@ rcube_webmail.prototype.enigma_key_create_save = function() openpgp.generateKeyPair(options).then(function(keypair) { // success - var post = {_a: 'import', _keys: keypair.privateKeyArmored, _generated: 1}; + var post = {_a: 'import', _keys: keypair.privateKeyArmored, _generated: 1, + _passwd: password, _keyid: keypair.key.primaryKey.fingerprint}; // send request to server rcmail.http_post('plugin.enigmakeys', post, lock); diff --git a/plugins/enigma/lib/enigma_driver.php b/plugins/enigma/lib/enigma_driver.php index 1ed5fbc3d..eaa4364b4 100644 --- a/plugins/enigma/lib/enigma_driver.php +++ b/plugins/enigma/lib/enigma_driver.php @@ -77,12 +77,13 @@ abstract class enigma_driver /** * Key/Cert file import. * - * @param string File name or file content - * @param bollean True if first argument is a filename + * @param string File name or file content + * @param bolean True if first argument is a filename + * @param array Optional key => password map * * @return mixed Import status array or enigma_error */ - abstract function import($content, $isfile = false); + abstract function import($content, $isfile = false, $passwords = array()); /** * Key/Cert export. diff --git a/plugins/enigma/lib/enigma_driver_gnupg.php b/plugins/enigma/lib/enigma_driver_gnupg.php index 267af4fd1..b6f87946e 100644 --- a/plugins/enigma/lib/enigma_driver_gnupg.php +++ b/plugins/enigma/lib/enigma_driver_gnupg.php @@ -201,14 +201,20 @@ class enigma_driver_gnupg extends enigma_driver /** * Key file import. * - * @param string File name or file content - * @param bollean True if first argument is a filename + * @param string File name or file content + * @param bolean True if first argument is a filename + * @param array Optional key => password map * * @return mixed Import status array or enigma_error */ - public function import($content, $isfile=false) + public function import($content, $isfile = false, $passwords = array()) { try { + // GnuPG 2.1 requires secret key passphrases on import + foreach ($passwords as $keyid => $pass) { + $this->gpg->addPassphrase($keyid, $pass); + } + if ($isfile) return $this->gpg->importKeyFile($content); else @@ -251,7 +257,7 @@ class enigma_driver_gnupg extends enigma_driver * * @return mixed Array of enigma_key objects or enigma_error */ - public function list_keys($pattern='') + public function list_keys($pattern = '') { try { $keys = $this->gpg->getKeys($pattern); diff --git a/plugins/enigma/lib/enigma_driver_phpssl.php b/plugins/enigma/lib/enigma_driver_phpssl.php index 967811da0..8cffc62f0 100644 --- a/plugins/enigma/lib/enigma_driver_phpssl.php +++ b/plugins/enigma/lib/enigma_driver_phpssl.php @@ -122,7 +122,7 @@ class enigma_driver_phpssl extends enigma_driver return $sig; } - public function import($content, $isfile=false) + public function import($content, $isfile = false, $passwords = array()) { } diff --git a/plugins/enigma/lib/enigma_engine.php b/plugins/enigma/lib/enigma_engine.php index f32367789..f75be3657 100644 --- a/plugins/enigma/lib/enigma_engine.php +++ b/plugins/enigma/lib/enigma_engine.php @@ -1113,10 +1113,10 @@ class enigma_engine * * @return mixed Import status data array or enigma_error */ - function import_key($content, $isfile=false) + function import_key($content, $isfile = false) { $this->load_pgp_driver(); - $result = $this->pgp_driver->import($content, $isfile); + $result = $this->pgp_driver->import($content, $isfile, $this->get_passwords()); if ($result instanceof enigma_error) { rcube::raise_error(array( @@ -1174,7 +1174,7 @@ class enigma_engine $passwd = rcube_utils::get_input_value('_passwd', rcube_utils::INPUT_POST, true); if ($keyid && $passwd !== null && strlen($passwd)) { - $this->save_password($keyid, $passwd); + $this->save_password(strtoupper($keyid), $passwd); } } diff --git a/plugins/enigma/lib/enigma_ui.php b/plugins/enigma/lib/enigma_ui.php index b7fc01161..650ea2166 100644 --- a/plugins/enigma/lib/enigma_ui.php +++ b/plugins/enigma/lib/enigma_ui.php @@ -498,8 +498,9 @@ class enigma_ui { // Import process if ($data = rcube_utils::get_input_value('_keys', rcube_utils::INPUT_POST)) { - // Import from generation form (ajax request) $this->enigma->load_engine(); + $this->enigma->engine->password_handler(); + $result = $this->enigma->engine->import_key($data); if (is_array($result)) {