roundcubemail/plugins/enigma/lib/enigma_engine.php
function sign_message(&$message, $mode = null)
    {
        $mime = new enigma_mime_message($message, enigma_mime_message::PGP_SIGNED);
        $from = $mime->getFromAddress();

        // find private key
        $key = $this->find_key($from, true);

        if (empty($key)) {
            return new enigma_error(enigma_error::KEYNOTFOUND);
        }

        // check if we have password for this key
        $passwords = $this->get_passwords();
        $pass      = $passwords[$key->id];

        if ($pass === null) {
            // ask for password
            $error = array('missing' => array($key->id => $key->name));
            return new enigma_error(enigma_error::BADPASS, '', $error);
        }

        $key->password = $pass;

        // select mode
        switch ($mode) {
        case self::SIGN_MODE_BODY:
            $pgp_mode = Crypt_GPG::SIGN_MODE_CLEAR;
            break;

        case self::SIGN_MODE_MIME:
            $pgp_mode = Crypt_GPG::SIGN_MODE_DETACHED;
            break;

        default:
            if ($mime->isMultipart()) {
                $pgp_mode = Crypt_GPG::SIGN_MODE_DETACHED;
            }
            else {
                $pgp_mode = Crypt_GPG::SIGN_MODE_CLEAR;
            }
        }

        // get message body
        if ($pgp_mode == Crypt_GPG::SIGN_MODE_CLEAR) {
            // in this mode we'll replace text part
            // with the one containing signature
            $body = $message->getTXTBody();

            $text_charset = $message->getParam('text_charset');
            $line_length  = $this->rc->config->get('line_length', 72);

            // We can't use format=flowed for signed messages
            if (strpos($text_charset, 'format=flowed')) {
                list($charset, $params) = explode(';', $text_charset);
                $body = rcube_mime::unfold_flowed($body);
                $body = rcube_mime::wordwrap($body, $line_length, "\r\n", false, $charset);

                $text_charset = str_replace(";\r\n format=flowed", '', $text_charset);
            }
        }
        else {
            // here we'll build PGP/MIME message
            $body = $mime->getOrigBody();
        }

        // sign the body
        $result = $this->pgp_sign($body, $key, $pgp_mode);

        if ($result !== true) {
            if ($result->getCode() == enigma_error::BADPASS) {
                // ask for password
                $error = array('bad' => array($key->id => $key->name));
                return new enigma_error(enigma_error::BADPASS, '', $error);
            }

            return $result;
        }

        // replace message body
        if ($pgp_mode == Crypt_GPG::SIGN_MODE_CLEAR) {
            $message->setTXTBody($body);
            $message->setParam('text_charset', $text_charset);
        }
        else {
            $mime->addPGPSignature($body, $this->pgp_driver->signature_algorithm());
            $message = $mime;
        }
    }
function sign_message(&$message, $mode = null)
    {
        $mime = new enigma_mime_message($message, enigma_mime_message::PGP_SIGNED);
        $from = $mime->getFromAddress();

        // find private key
        $key = $this->find_key($from, true);

        if (empty($key)) {
            return new enigma_error(enigma_error::KEYNOTFOUND);
        }

        // check if we have password for this key
        $passwords = $this->get_passwords();
        $pass      = $passwords[$key->id];

        if ($pass === null) {
            // ask for password
            $error = array('missing' => array($key->id => $key->name));
            return new enigma_error(enigma_error::BADPASS, '', $error);
        }

        $key->password = $pass;

        // select mode
        switch ($mode) {
        case self::SIGN_MODE_BODY:
            $pgp_mode = Crypt_GPG::SIGN_MODE_CLEAR;
            break;

        case self::SIGN_MODE_MIME:
            $pgp_mode = Crypt_GPG::SIGN_MODE_DETACHED;
            break;

        default:
            if ($mime->isMultipart()) {
                $pgp_mode = Crypt_GPG::SIGN_MODE_DETACHED;
            }
            else {
                $pgp_mode = Crypt_GPG::SIGN_MODE_CLEAR;
            }
        }

        // get message body
        if ($pgp_mode == Crypt_GPG::SIGN_MODE_CLEAR) {
            // in this mode we'll replace text part
            // with the one containing signature
            $body = $message->getTXTBody();

            $text_charset = $message->getParam('text_charset');
            $line_length  = $this->rc->config->get('line_length', 72);

            // We can't use format=flowed for signed messages
            if (strpos($text_charset, 'format=flowed')) {
                list($charset, $params) = explode(';', $text_charset);
                $body = rcube_mime::unfold_flowed($body);
                $body = rcube_mime::wordwrap($body, $line_length, "\r\n", false, $charset);

                $text_charset = str_replace(";\r\n format=flowed", '', $text_charset);
            }
        }
        else {
            // here we'll build PGP/MIME message
            $body = $mime->getOrigBody();
        }

        // sign the body
        $result = $this->pgp_sign($body, $key, $pgp_mode);

        if ($result !== true) {
            if ($result->getCode() == enigma_error::BADPASS) {
                // ask for password
                $error = array('bad' => array($key->id => $key->name));
                return new enigma_error(enigma_error::BADPASS, '', $error);
            }

            return $result;
        }

        // replace message body
        if ($pgp_mode == Crypt_GPG::SIGN_MODE_CLEAR) {
            $message->setTXTBody($body);
            $message->setParam('text_charset', $text_charset);
        }
        else {
            $mime->addPGPSignature($body, $this->pgp_driver->signature_algorithm());
            $message = $mime;
        }
    }
function sign_message ( & $ message , $ mode = null ) { $ mime = new enigma_mime_message ( $ message , enigma_mime_message :: PGP_SIGNED ) ; $ from = $ mime -> getFromAddress ( ) ; // find private key $ key = $ this -> find_key ( $ from , true ) ; if ( empty ( $ key ) ) { return new enigma_error ( enigma_error :: KEYNOTFOUND ) ; } // check if we have password for this key $ passwords = $ this -> get_passwords ( ) ; $ pass = $ passwords [ $ key -> id ] ; if ( $ pass === null ) { // ask for password $ error = array ( 'missing' => array ( $ key -> id => $ key -> name ) ) ; return new enigma_error ( enigma_error :: BADPASS , '' , $ error ) ; } $ key -> password = $ pass ; // select mode switch ( $ mode ) { case self :: SIGN_MODE_BODY : $ pgp_mode = Crypt_GPG :: SIGN_MODE_CLEAR ; break ; case self :: SIGN_MODE_MIME : $ pgp_mode = Crypt_GPG :: SIGN_MODE_DETACHED ; break ; default : if ( $ mime -> isMultipart ( ) ) { $ pgp_mode = Crypt_GPG :: SIGN_MODE_DETACHED ; } else { $ pgp_mode = Crypt_GPG :: SIGN_MODE_CLEAR ; } } // get message body if ( $ pgp_mode == Crypt_GPG :: SIGN_MODE_CLEAR ) { // in this mode we'll replace text part // with the one containing signature $ body = $ message -> getTXTBody ( ) ; $ text_charset = $ message -> getParam ( 'text_charset' ) ; $ line_length = $ this -> rc -> config -> get ( 'line_length' , 72 ) ; // We can't use format=flowed for signed messages if ( strpos ( $ text_charset , 'format=flowed' ) ) { list ( $ charset , $ params ) = explode ( ';' , $ text_charset ) ; $ body = rcube_mime :: unfold_flowed ( $ body ) ; $ body = rcube_mime :: wordwrap ( $ body , $ line_length , "\r\n" , false , $ charset ) ; $ text_charset = str_replace ( ";\r\n format=flowed" , '' , $ text_charset ) ; } } else { // here we'll build PGP/MIME message $ body = $ mime -> getOrigBody ( ) ; } // sign the body $ result = $ this -> pgp_sign ( $ body , $ key , $ pgp_mode ) ; if ( $ result !== true ) { if ( $ result -> getCode ( ) == enigma_error :: BADPASS ) { // ask for password $ error = array ( 'bad' => array ( $ key -> id => $ key -> name ) ) ; return new enigma_error ( enigma_error :: BADPASS , '' , $ error ) ; } return $ result ; } // replace message body if ( $ pgp_mode == Crypt_GPG :: SIGN_MODE_CLEAR ) { $ message -> setTXTBody ( $ body ) ; $ message -> setParam ( 'text_charset' , $ text_charset ) ; } else { $ mime -> addPGPSignature ( $ body , $ this -> pgp_driver -> signature_algorithm ( ) ) ; $ message = $ mime ; } }
Handler for message signing

@param Mail_mime Original message
@param int       Encryption mode

@return enigma_error On error returns error object
Handler for message signing