From 74afee62669d277b3a92a86d2f5e1fa367e773d7 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sun, 20 Jan 2013 22:15:49 -0500 Subject: [PATCH] Test signing at all --- lib/openpgp.php | 8 +++++--- lib/openpgp_crypt_rsa.php | 5 +++-- tests/phpseclib_suite.php | 9 +++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/openpgp.php b/lib/openpgp.php index d36a5c0..3b81b36 100644 --- a/lib/openpgp.php +++ b/lib/openpgp.php @@ -534,10 +534,10 @@ class OpenPGP_SignaturePacket extends OpenPGP_Packet { * $signers in the same format as $verifiers for OpenPGP_Message. */ function sign_data($signers) { - $this->trailer = $this->body(true); + $this->trailer = $this->calculate_trailer(); $signer = $signers[$this->key_algorithm_name()][$this->hash_algorithm_name()]; $this->data = call_user_func($signer, $this->data.$this->trailer); - $unpacked = unpack('n', substr($this->data, 0, 2)); + $unpacked = unpack('n', substr(implode('',$this->data), 0, 2)); $this->hash_head = reset($unpacked); } @@ -608,6 +608,8 @@ class OpenPGP_SignaturePacket extends OpenPGP_Packet { $hashed_subpackets .= $p->to_bytes(); } $body .= pack('n', strlen($hashed_subpackets)).$hashed_subpackets; + + return $body; } function body() { @@ -653,7 +655,7 @@ class OpenPGP_SignaturePacket extends OpenPGP_Packet { $body .= pack('n', $this->hash_head); - foreach($this->data as $mpi) { + foreach((array)$this->data as $mpi) { $body .= pack('n', OpenPGP::bitlength($mpi)).$mpi; } diff --git a/lib/openpgp_crypt_rsa.php b/lib/openpgp_crypt_rsa.php index 9b455ac..7953d6b 100644 --- a/lib/openpgp_crypt_rsa.php +++ b/lib/openpgp_crypt_rsa.php @@ -110,7 +110,8 @@ class OpenPGP_Crypt_RSA { if(!$key || !$message) return NULL; // Missing some data if($message instanceof OpenPGP_Message) { - list($dummy, $message) = $message->signature_and_data(); + $sign = $message->signatures(); + $message = $sign[0][0]; } if(!($key instanceof Crypt_RSA)) { @@ -122,7 +123,7 @@ class OpenPGP_Crypt_RSA { $sig = new OpenPGP_SignaturePacket($message, 'RSA', strtoupper($hash)); $sig->hashed_subpackets[] = new OpenPGP_SignaturePacket_IssuerPacket($keyid); - $sig->sign_data(array('RSA' => array($hash => array($key, 'sign')))); + $sig->sign_data(array('RSA' => array($hash => function($data) use($key) {return array($key->sign($data));}))); return new OpenPGP_Message(array($sig, $message)); } diff --git a/tests/phpseclib_suite.php b/tests/phpseclib_suite.php index 90e0725..19dd665 100644 --- a/tests/phpseclib_suite.php +++ b/tests/phpseclib_suite.php @@ -29,6 +29,15 @@ class MessageVerification extends PHPUnit_Framework_TestCase { $this->oneMessageRSA('pubring.gpg', 'compressedsig-bzip2.gpg'); } + public function testSigningMessages() { + $wkey = OpenPGP_Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg')); + $data = new OpenPGP_LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt')); + $sign = new OpenPGP_Crypt_RSA($wkey); + $m = $sign->sign($data)->to_bytes(); + $reparsedM = OpenPGP_Message::parse($m); + $this->assertSame($sign->verify($reparsedM), $reparsedM->signatures()); + } + /* public function testUncompressedOpsDSA() { $this->oneMessageDSA('pubring.gpg', 'uncompressed-ops-dsa.gpg');