Kyle Francis
2016-03-30 a9d399155d205ae41015d7d205c6dacd7ecfc0d2
Implemented attaching pub PGP key to outgoing messages.
3 files modified
67 ■■■■■ changed files
plugins/enigma/lib/enigma_driver_gnupg.php 11 ●●●●● patch | view | raw | blame | history
plugins/enigma/lib/enigma_engine.php 32 ●●●●● patch | view | raw | blame | history
plugins/enigma/lib/enigma_ui.php 24 ●●●●● patch | view | raw | blame | history
plugins/enigma/lib/enigma_driver_gnupg.php
@@ -339,6 +339,17 @@
        }
    }
    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
     *
plugins/enigma/lib/enigma_engine.php
@@ -921,6 +921,38 @@
        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
     *
plugins/enigma/lib/enigma_ui.php
@@ -929,11 +929,15 @@
    }
    /**
     * 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();
@@ -972,6 +976,24 @@
    }
    /**
     * 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.