Test signing at all

This commit is contained in:
Stephen Paul Weber 2013-01-20 22:15:49 -05:00
parent 7a1510f2e1
commit 74afee6266
3 changed files with 17 additions and 5 deletions

View File

@ -534,10 +534,10 @@ class OpenPGP_SignaturePacket extends OpenPGP_Packet {
* $signers in the same format as $verifiers for OpenPGP_Message. * $signers in the same format as $verifiers for OpenPGP_Message.
*/ */
function sign_data($signers) { function sign_data($signers) {
$this->trailer = $this->body(true); $this->trailer = $this->calculate_trailer();
$signer = $signers[$this->key_algorithm_name()][$this->hash_algorithm_name()]; $signer = $signers[$this->key_algorithm_name()][$this->hash_algorithm_name()];
$this->data = call_user_func($signer, $this->data.$this->trailer); $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); $this->hash_head = reset($unpacked);
} }
@ -608,6 +608,8 @@ class OpenPGP_SignaturePacket extends OpenPGP_Packet {
$hashed_subpackets .= $p->to_bytes(); $hashed_subpackets .= $p->to_bytes();
} }
$body .= pack('n', strlen($hashed_subpackets)).$hashed_subpackets; $body .= pack('n', strlen($hashed_subpackets)).$hashed_subpackets;
return $body;
} }
function body() { function body() {
@ -653,7 +655,7 @@ class OpenPGP_SignaturePacket extends OpenPGP_Packet {
$body .= pack('n', $this->hash_head); $body .= pack('n', $this->hash_head);
foreach($this->data as $mpi) { foreach((array)$this->data as $mpi) {
$body .= pack('n', OpenPGP::bitlength($mpi)).$mpi; $body .= pack('n', OpenPGP::bitlength($mpi)).$mpi;
} }

View File

@ -110,7 +110,8 @@ class OpenPGP_Crypt_RSA {
if(!$key || !$message) return NULL; // Missing some data if(!$key || !$message) return NULL; // Missing some data
if($message instanceof OpenPGP_Message) { if($message instanceof OpenPGP_Message) {
list($dummy, $message) = $message->signature_and_data(); $sign = $message->signatures();
$message = $sign[0][0];
} }
if(!($key instanceof Crypt_RSA)) { if(!($key instanceof Crypt_RSA)) {
@ -122,7 +123,7 @@ class OpenPGP_Crypt_RSA {
$sig = new OpenPGP_SignaturePacket($message, 'RSA', strtoupper($hash)); $sig = new OpenPGP_SignaturePacket($message, 'RSA', strtoupper($hash));
$sig->hashed_subpackets[] = new OpenPGP_SignaturePacket_IssuerPacket($keyid); $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)); return new OpenPGP_Message(array($sig, $message));
} }

View File

@ -29,6 +29,15 @@ class MessageVerification extends PHPUnit_Framework_TestCase {
$this->oneMessageRSA('pubring.gpg', 'compressedsig-bzip2.gpg'); $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() { public function testUncompressedOpsDSA() {
$this->oneMessageDSA('pubring.gpg', 'uncompressed-ops-dsa.gpg'); $this->oneMessageDSA('pubring.gpg', 'uncompressed-ops-dsa.gpg');