Example #1
0
 /**
  * Return the unencrypted version of the private key.
  *
  * @param string $passphrase  The passphrase used to encrypt the key.
  *
  * @return Horde_Pgp_Element_PrivateKey  Unencrypted key.
  * @throws Horde_Pgp_Exception
  */
 public function getUnencryptedKey($passphrase = null)
 {
     $out = null;
     foreach ($this->_getSecretKeyPackets() as $k => $v) {
         if (!strlen($v->encrypted_data)) {
             continue;
         }
         if (is_null($out)) {
             $out = clone $this->message;
         }
         $out[$k] = OpenPGP_Crypt_Symmetric::decryptSecretKey($passphrase, $v);
         if (is_null($out[$k])) {
             throw new Horde_Pgp_Exception(Horde_Pgp_Translation::t("Could not unencrypt private key."));
         }
     }
     return new Horde_Pgp_Element_PrivateKey($out);
 }
 function decrypt($packet)
 {
     if (!is_object($packet)) {
         $packet = OpenPGP_Message::parse($packet);
     }
     if ($packet instanceof OpenPGP_SecretKeyPacket || $packet instanceof Crypt_RSA || $packet instanceof ArrayAccess && $packet[0] instanceof OpenPGP_SecretKeyPacket) {
         $keys = $packet;
         $message = $this->message;
     } else {
         $keys = $this->key;
         $message = $packet;
     }
     if (!$keys || !$message) {
         return NULL;
     }
     // Missing some data
     if (!$keys instanceof Crypt_RSA) {
         $keys = new self($keys);
     }
     foreach ($message as $p) {
         if ($p instanceof OpenPGP_AsymmetricSessionKeyPacket) {
             if ($keys instanceof Crypt_RSA) {
                 $sk = self::try_decrypt_session($keys, substr($p->encyrpted_data, 2));
             } else {
                 if (strlen(str_replace('0', '', $p->keyid)) < 1) {
                     foreach ($keys->key as $k) {
                         $sk = self::try_decrypt_session(self::convert_private_key($k), substr($p->encyrpted_data, 2));
                         if ($sk) {
                             break;
                         }
                     }
                 } else {
                     $key = $keys->private_key($p->keyid);
                     $sk = self::try_decrypt_session($key, substr($p->encrypted_data, 2));
                 }
             }
             if (!$sk) {
                 continue;
             }
             $r = OpenPGP_Crypt_Symmetric::decryptPacket(OpenPGP_Crypt_Symmetric::getEncryptedData($message), $sk[0], $sk[1]);
             if ($r) {
                 return $r;
             }
         }
     }
     return NULL;
     /* Failed */
 }
Example #3
0
<?php

// USAGE: php examples/deASCIIdeCrypt.php secretkey.asc password message.asc
// This will fail if the algo on key or message is not 3DES or AES
require_once dirname(__FILE__) . '/../lib/openpgp.php';
require_once dirname(__FILE__) . '/../lib/openpgp_crypt_rsa.php';
require_once dirname(__FILE__) . '/../lib/openpgp_crypt_symmetric.php';
$keyASCII = file_get_contents($argv[1]);
$msgASCII = file_get_contents($argv[3]);
$keyEncrypted = OpenPGP_Message::parse(OpenPGP::unarmor($keyASCII, 'PGP PRIVATE KEY BLOCK'));
// Try each secret key packet
foreach ($keyEncrypted as $p) {
    if (!$p instanceof OpenPGP_SecretKeyPacket) {
        continue;
    }
    $key = OpenPGP_Crypt_Symmetric::decryptSecretKey($argv[2], $p);
    $msg = OpenPGP_Message::parse(OpenPGP::unarmor($msgASCII, 'PGP MESSAGE'));
    $decryptor = new OpenPGP_Crypt_RSA($key);
    $decrypted = $decryptor->decrypt($msg);
    var_dump($decrypted);
}
Example #4
0
 public function testEncryptAsymmetric()
 {
     $key = OpenPGP_Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg'));
     $data = new OpenPGP_LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt'));
     $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data)));
     $decryptor = new OpenPGP_Crypt_RSA($key);
     $decrypted = $decryptor->decrypt($encrypted);
     $this->assertEquals($decrypted[0]->data, 'This is text.');
 }
Example #5
0
<?php

require_once dirname(__FILE__) . '/../lib/openpgp.php';
require_once dirname(__FILE__) . '/../lib/openpgp_crypt_rsa.php';
require_once dirname(__FILE__) . '/../lib/openpgp_crypt_symmetric.php';
$key = OpenPGP_Message::parse(file_get_contents(dirname(__FILE__) . '/../tests/data/helloKey.gpg'));
$data = new OpenPGP_LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt'));
$encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data)));
// Now decrypt it with the same key
$decryptor = new OpenPGP_Crypt_RSA($key);
$decrypted = $decryptor->decrypt($encrypted);
var_dump($decrypted);
Example #6
0
 /**
  */
 public function decryptSymmetric($msg, $passphrase)
 {
     $decrypted = OpenPGP_Crypt_Symmetric::decryptSymmetric($passphrase, $msg->message);
     if (!is_null($decrypted)) {
         /* It is possible data could be decrypted to junk PGP data
          * ($decrypted != NULL). Search for valid packets if $decrypted is
          * returned. */
         foreach ($decrypted as $val) {
             switch (get_class($val)) {
                 case 'OpenPGP_Packet':
                 case 'OpenPGP_ExperimentalPacket':
                     /* Assume that these packets are not valid. */
                     break;
                 default:
                     return new Horde_Pgp_Element_Message($decrypted);
             }
         }
     }
     throw new RuntimeException();
 }
 public function signDocument()
 {
     $document = new \StdClass();
     foreach ($this->resourceData as $term => $value) {
         $document->{$term} = $this->normalizeData($value);
     }
     unset($document->digital_signature);
     unset($document->_id);
     unset($document->_rev);
     unset($document->doc_id);
     unset($document->publishing_node);
     unset($document->update_timestamp);
     unset($document->node_timestamp);
     unset($document->create_timestamp);
     $bencoder = new \LearningRegistry\Bencode\LearningRegistryBencodeEncoderTrial();
     $document = (array) $document;
     $bencodedDocument = utf8_encode($bencoder->encode($document));
     $hashedDocument = hash('SHA256', $bencodedDocument);
     global $loader;
     if (!isset($loader)) {
         $loader = $this->getLoader();
     }
     spl_autoload_unregister(array($loader, 'loadClass'));
     require_once dirname(__FILE__) . '/../OpenPGP/openpgp.php';
     require_once dirname(__FILE__) . '/../OpenPGP/openpgp_crypt_rsa.php';
     require_once dirname(__FILE__) . '/../OpenPGP/openpgp_crypt_symmetric.php';
     $keyASCII = $this->getKey();
     $keyEncrypted = \OpenPGP_Message::parse(\OpenPGP::unarmor($keyASCII, 'PGP PRIVATE KEY BLOCK'));
     foreach ($keyEncrypted as $p) {
         if (!$p instanceof \OpenPGP_SecretKeyPacket) {
             continue;
         }
         $key = \OpenPGP_Crypt_Symmetric::decryptSecretKey($this->getPassPhrase(), $p);
     }
     $data = new \OpenPGP_LiteralDataPacket($hashedDocument, array('format' => 'u'));
     $sign = new \OpenPGP_Crypt_RSA($key);
     $m = $sign->sign($data);
     $packets = $m->signatures()[0];
     $message = "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA256\n\n";
     $message .= $packets[0]->data . "\n";
     $message .= "-----BEGIN PGP SIGNATURE-----\n\n";
     $signed_data = str_replace("-----BEGIN -----", "", str_replace("-----END -----", "", \OpenPGP::enarmor($packets[1][0]->to_bytes(), "")));
     $signature = str_split(trim($signed_data), 65);
     foreach ($signature as $line) {
         $message .= $line . "\n";
     }
     $message .= "-----END PGP SIGNATURE-----\n";
     $this->setSigFields(array('signature' => $message, 'key_owner' => $this->getKeyOwner(), 'key_location' => array($this->getPublicKeyPath()), 'signing_method' => "LR-PGP.1.0"));
     spl_autoload_register(array($loader, 'loadClass'));
     $this->document = $this->createDocument();
 }