/**
  * Handler for message encryption
  *
  * @param Mail_mime Original message
  * @param int       Encryption mode
  * @param bool      Is draft-save action - use only sender's key for encryption
  *
  * @return enigma_error On error returns error object
  */
 function encrypt_message(&$message, $mode = null, $is_draft = false)
 {
     $mime = new enigma_mime_message($message, enigma_mime_message::PGP_ENCRYPTED);
     // always use sender's key
     $recipients = array($mime->getFromAddress());
     // if it's not a draft we add all recipients' keys
     if (!$is_draft) {
         $recipients = array_merge($recipients, $mime->getRecipients());
     }
     if (empty($recipients)) {
         return new enigma_error(enigma_error::KEYNOTFOUND);
     }
     $recipients = array_unique($recipients);
     // find recipient public keys
     foreach ((array) $recipients as $email) {
         $key = $this->find_key($email);
         if (empty($key)) {
             return new enigma_error(enigma_error::KEYNOTFOUND, '', array('missing' => $email));
         }
         $keys[] = $key->id;
     }
     // select mode
     switch ($mode) {
         case self::ENCRYPT_MODE_BODY:
             $encrypt_mode = $mode;
             break;
         case self::ENCRYPT_MODE_MIME:
             $encrypt_mode = $mode;
             break;
         default:
             $encrypt_mode = $mime->isMultipart() ? self::ENCRYPT_MODE_MIME : self::ENCRYPT_MODE_BODY;
     }
     // get message body
     if ($encrypt_mode == self::ENCRYPT_MODE_BODY) {
         // in this mode we'll replace text part
         // with the one containing encrypted message
         $body = $message->getTXTBody();
     } else {
         // here we'll build PGP/MIME message
         $body = $mime->getOrigBody();
     }
     // sign the body
     $result = $this->pgp_encrypt($body, $keys);
     if ($result !== true) {
         return $result;
     }
     // replace message body
     if ($encrypt_mode == self::ENCRYPT_MODE_BODY) {
         $message->setTXTBody($body);
     } else {
         $mime->setPGPEncryptedBody($body);
         $message = $mime;
     }
 }
Esempio n. 2
0
 /**
  * Handler for message encryption
  *
  * @param Mail_mime Original message
  * @param int       Encryption mode
  * @param bool      Is draft-save action - use only sender's key for encryption
  *
  * @return enigma_error On error returns error object
  */
 function encrypt_message(&$message, $mode = null, $is_draft = false)
 {
     $mime = new enigma_mime_message($message, enigma_mime_message::PGP_ENCRYPTED);
     // always use sender's key
     $from = $mime->getFromAddress();
     // check senders key for signing
     if ($mode & self::ENCRYPT_MODE_SIGN) {
         $sign_key = $this->find_key($from, true);
         if (empty($sign_key)) {
             return new enigma_error(enigma_error::KEYNOTFOUND);
         }
         // check if we have password for this key
         $passwords = $this->get_passwords();
         $sign_pass = $passwords[$sign_key->id];
         if ($sign_pass === null) {
             // ask for password
             $error = array('missing' => array($sign_key->id => $sign_key->name));
             return new enigma_error(enigma_error::BADPASS, '', $error);
         }
         $sign_key->password = $sign_pass;
     }
     $recipients = array($from);
     // if it's not a draft we add all recipients' keys
     if (!$is_draft) {
         $recipients = array_merge($recipients, $mime->getRecipients());
     }
     if (empty($recipients)) {
         return new enigma_error(enigma_error::KEYNOTFOUND);
     }
     $recipients = array_unique($recipients);
     // find recipient public keys
     foreach ((array) $recipients as $email) {
         if ($email == $from && $sign_key) {
             $key = $sign_key;
         } else {
             $key = $this->find_key($email);
         }
         if (empty($key)) {
             return new enigma_error(enigma_error::KEYNOTFOUND, '', array('missing' => $email));
         }
         $keys[] = $key;
     }
     // select mode
     if ($mode & self::ENCRYPT_MODE_BODY) {
         $encrypt_mode = $mode;
     } else {
         if ($mode & self::ENCRYPT_MODE_MIME) {
             $encrypt_mode = $mode;
         } else {
             $encrypt_mode = $mime->isMultipart() ? self::ENCRYPT_MODE_MIME : self::ENCRYPT_MODE_BODY;
         }
     }
     // get message body
     if ($encrypt_mode == self::ENCRYPT_MODE_BODY) {
         // in this mode we'll replace text part
         // with the one containing encrypted message
         $body = $message->getTXTBody();
     } else {
         // here we'll build PGP/MIME message
         $body = $mime->getOrigBody();
     }
     // sign the body
     $result = $this->pgp_encrypt($body, $keys, $sign_key);
     if ($result !== true) {
         if ($result->getCode() == enigma_error::BADPASS) {
             // ask for password
             $error = array('bad' => array($sign_key->id => $sign_key->name));
             return new enigma_error(enigma_error::BADPASS, '', $error);
         }
         return $result;
     }
     // replace message body
     if ($encrypt_mode == self::ENCRYPT_MODE_BODY) {
         $message->setTXTBody($body);
     } else {
         $mime->setPGPEncryptedBody($body);
         $message = $mime;
     }
 }