Thomas Bruederli
2015-04-17 a3644638aaf0418598196a870204e0b632a4c8ad
plugins/enigma/lib/enigma_driver_gnupg.php
@@ -3,18 +3,11 @@
 +-------------------------------------------------------------------------+
 | GnuPG (PGP) driver for the Enigma Plugin                                |
 |                                                                         |
 | This program is free software; you can redistribute it and/or modify    |
 | it under the terms of the GNU General Public License version 2          |
 | as published by the Free Software Foundation.                           |
 | Copyright (C) 2010-2015 The Roundcube Dev Team                          |
 |                                                                         |
 | This program is distributed in the hope that it will be useful,         |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
 | GNU General Public License for more details.                            |
 |                                                                         |
 | You should have received a copy of the GNU General Public License along |
 | with this program; if not, write to the Free Software Foundation, Inc., |
 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.             |
 | Licensed under the GNU General Public License version 3 or              |
 | any later version with exceptions for skins & plugins.                  |
 | See the README file for a full license statement.                       |
 |                                                                         |
 +-------------------------------------------------------------------------+
 | Author: Aleksander Machniak <alec@alec.pl>                              |
@@ -30,10 +23,10 @@
    private $homedir;
    private $user;
    function __construct($user)
    {
        $rcmail = rcmail::get_instance();
        $this->rc = $rcmail;
        $this->rc   = rcmail::get_instance();
        $this->user = $user;
    }
@@ -45,7 +38,7 @@
     */
    function init()
    {
        $homedir = $this->rc->config->get('enigma_pgp_homedir', INSTALL_PATH . '/plugins/enigma/home');
        $homedir = $this->rc->config->get('enigma_pgp_homedir', INSTALL_PATH . 'plugins/enigma/home');
        if (!$homedir)
            return new enigma_error(enigma_error::E_INTERNAL,
@@ -76,9 +69,10 @@
        // Create Crypt_GPG object
        try {
           $this->gpg = new Crypt_GPG(array(
            $this->gpg = new Crypt_GPG(array(
                'homedir'   => $this->homedir,
//                'debug'     => true,
                // 'binary'    => '/usr/bin/gpg2',
                // 'debug'     => true,
          ));
        }
        catch (Exception $e) {
@@ -86,43 +80,64 @@
        }
    }
    /**
     * Encrypt a message
     *
     * @param string The message
     * @param array  List of keys
     */
    function encrypt($text, $keys)
    {
/*
       foreach ($keys as $key) {
          $this->gpg->addEncryptKey($key);
       }
       $enc = $this->gpg->encrypt($text);
       return $enc;
*/
    }
    function decrypt($text, $key, $passwd)
    {
//       $this->gpg->addDecryptKey($key, $passwd);
        try {
           $dec = $this->gpg->decrypt($text);
           return $dec;
            foreach ($keys as $key) {
                $this->gpg->addEncryptKey($key);
            }
            $dec = $this->gpg->encrypt($text, true);
            return $dec;
        }
        catch (Exception $e) {
            return $this->get_error_from_exception($e);
        }
    }
    function sign($text, $key, $passwd)
    /**
     * Decrypt a message
     *
     * @param string Encrypted message
     * @param array  List of key-password mapping
     */
    function decrypt($text, $keys = array())
    {
/*
       $this->gpg->addSignKey($key, $passwd);
       $signed = $this->gpg->sign($text, Crypt_GPG::SIGN_MODE_DETACHED);
       return $signed;
*/
        try {
            foreach ($keys as $key => $password) {
                $this->gpg->addDecryptKey($key, $password);
            }
            $dec = $this->gpg->decrypt($text);
            return $dec;
        }
        catch (Exception $e) {
            return $this->get_error_from_exception($e);
        }
    }
    function sign($text, $key, $passwd, $mode = null)
    {
        try {
            $this->gpg->addSignKey($key, $passwd);
            return $this->gpg->sign($text, $mode, CRYPT_GPG::ARMOR_ASCII, true);
        }
        catch (Exception $e) {
            return $this->get_error_from_exception($e);
        }
    }
    function verify($text, $signature)
    {
        try {
           $verified = $this->gpg->verify($text, $signature);
             return $this->parse_signature($verified[0]);
            $verified = $this->gpg->verify($text, $signature);
            return $this->parse_signature($verified[0]);
        }
        catch (Exception $e) {
            return $this->get_error_from_exception($e);
@@ -141,25 +156,25 @@
            return $this->get_error_from_exception($e);
        }
    }
    public function list_keys($pattern='')
    {
        try {
           $keys = $this->gpg->getKeys($pattern);
            $keys = $this->gpg->getKeys($pattern);
            $result = array();
//print_r($keys);
            foreach ($keys as $idx => $key) {
                $result[] = $this->parse_key($key);
                unset($keys[$idx]);
            }
//print_r($result);
             return $result;
            return $result;
        }
        catch (Exception $e) {
            return $this->get_error_from_exception($e);
        }
    }
    public function get_key($keyid)
    {
        $list = $this->list_keys($keyid);
@@ -167,7 +182,7 @@
        if (is_array($list))
            return array_shift($list);
        // error
        // error
        return $list;
    }
@@ -175,17 +190,23 @@
    {
    }
    public function del_key($keyid)
    public function delete_key($keyid)
    {
//        $this->get_key($keyid);
        // delete public key
        $result = $this->delete_pubkey($keyid);
        // if not found, delete private key
        if ($result !== true && $result->getCode() == enigma_error::E_KEYNOTFOUND) {
            $result = $this->delete_privkey($keyid);
        }
        return $result;
    }
    public function del_privkey($keyid)
    public function delete_privkey($keyid)
    {
        try {
           $this->gpg->deletePrivateKey($keyid);
            $this->gpg->deletePrivateKey($keyid);
            return true;
        }
        catch (Exception $e) {
@@ -193,17 +214,17 @@
        }
    }
    public function del_pubkey($keyid)
    public function delete_pubkey($keyid)
    {
        try {
           $this->gpg->deletePublicKey($keyid);
            $this->gpg->deletePublicKey($keyid);
            return true;
        }
        catch (Exception $e) {
            return $this->get_error_from_exception($e);
        }
    }
    /**
     * Converts Crypt_GPG exception into Enigma's error object
     *
@@ -281,7 +302,7 @@
            $ekey->users[$idx] = $id;
        }
        $ekey->name = trim($ekey->users[0]->name . ' <' . $ekey->users[0]->email . '>');
        foreach ($key->getSubKeys() as $idx => $subkey) {
@@ -297,9 +318,9 @@
                $ekey->subkeys[$idx] = $skey;
        };
        $ekey->id = $ekey->subkeys[0]->id;
        return $ekey;
    }
}