Author: Michael Slusarz (slusarz@horde.org)
Inheritance: extends Horde_Pgp_Element_Key
Beispiel #1
0
 /**
  * Verifies text using a PGP public key and a detached signature.
  *
  * @param mixed $text  The text to be verified
  * @param mixed $sig   The detached signature.
  * @param mixed $key   The public key used for signing.
  *
  * @return  {@see detach()}
  * @throws Horde_Pgp_Exception
  */
 public function verifyDetached($text, $sig, $key)
 {
     if (is_null($sig)) {
         if ($text instanceof Horde_Pgp_Element) {
             $data = $text;
         } else {
             $armor = new Horde_Pgp_Armor($text);
             foreach ($armor as $val) {
                 if ($val instanceof Horde_Pgp_Element_Message || $val instanceof Horde_Pgp_Element_SignedMessage) {
                     $data = $val;
                     break;
                 }
             }
         }
     } else {
         $sig = Horde_Pgp_Element_Signature::create($sig);
         $data = new Horde_Pgp_Element_SignedMessage(new OpenPGP_Message(array(new OpenPGP_LiteralDataPacket($text, array('format' => $sig->message[0]->signature_type === 0x0 ? 'b' : 't')), $sig->message[0])));
     }
     return $this->_runInBackend('verify', array($data, Horde_Pgp_Element_PublicKey::create($key)), Horde_Pgp_Translation::t("Could not verify PGP data."));
 }
Beispiel #2
0
 /**
  * Generate a Horde_Mime_Part object that contains a public key (RFC
  * 3156 [7]).
  *
  * @param mixed $key  The public key.
  *
  * @return Horde_Mime_Part  An object that contains the public key.
  */
 public function publicKeyPart($key)
 {
     $key = Horde_Pgp_Element_PublicKey::create($key);
     $part = new Horde_Mime_Part();
     $part->setType('application/pgp-keys');
     $part->setHeaderCharset('UTF-8');
     $part->setDescription(Horde_Crypt_Translation::t("PGP Public Key"));
     $part->setContents(strval($key), array('encoding' => '7bit'));
     return $part;
 }
Beispiel #3
0
 /**
  * Returns the first matching key for an email address from a public
  * keyserver.
  *
  * @param string $address  The email address to search for.
  *
  * @return Horde_Pgp_Element_PublicKey  The PGP public key.
  * @throws Horde_Pgp_Exception
  */
 public function getKeyByEmail($address)
 {
     /* Connect to the public keyserver. */
     $url = $this->_createUrl('/pks/lookup', array('op' => 'index', 'options' => 'mr', 'search' => $address));
     try {
         $output = ltrim($this->_http->get($url)->getBody());
     } catch (Horde_Http_Exception $e) {
         throw new Horde_Pgp_Exception($e);
     }
     if (strpos($output, '-----BEGIN PGP PUBLIC KEY BLOCK') !== false) {
         return Horde_Pgp_Element_PublicKey::create($output);
     } elseif (strpos($output, 'pub:') !== false) {
         $output = explode("\n", $output);
         $keyids = $keyuids = array();
         $curid = null;
         foreach ($output as $line) {
             if (substr($line, 0, 4) == 'pub:') {
                 $line = explode(':', $line);
                 /* Ignore invalid lines and expired keys. */
                 if (count($line) != 7 || !empty($line[5]) && $line[5] <= time()) {
                     continue;
                 }
                 $curid = $line[4];
                 $keyids[$curid] = $line[1];
             } elseif (!is_null($curid) && substr($line, 0, 4) == 'uid:') {
                 preg_match("/<([^>]+)>/", $line, $matches);
                 $keyuids[$curid][] = $matches[1];
             }
         }
         /* Remove keys without a matching UID. */
         foreach ($keyuids as $id => $uids) {
             $match = false;
             foreach ($uids as $uid) {
                 if ($uid == $address) {
                     $match = true;
                     break;
                 }
             }
             if (!$match) {
                 unset($keyids[$id]);
             }
         }
         /* Sort by timestamp to use the newest key. */
         if (count($keyids)) {
             ksort($keyids);
             return $this->get(array_pop($keyids));
         }
     }
     throw new Horde_Pgp_Exception(Horde_Pgp_Translation::t("Could not obtain public key from the keyserver."));
 }