Skip to content
This repository has been archived by the owner on May 19, 2021. It is now read-only.

Commit

Permalink
Using new signature classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
kherge committed Jul 23, 2013
1 parent 096a9e9 commit 05da12e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 73 deletions.
78 changes: 19 additions & 59 deletions src/lib/Herrera/Box/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Herrera\Box\Exception\Exception;
use Herrera\Box\Exception\FileException;
use Herrera\Box\Exception\OpenSslException;
use Herrera\Box\Signature\VerifyInterface;
use PharException;

/**
Expand Down Expand Up @@ -48,27 +49,32 @@ class Signature
array(
'name' => 'MD5',
'flag' => 0x01,
'size' => 16
'size' => 16,
'class' => 'Herrera\\Box\\Signature\\Hash'
),
array(
'name' => 'SHA-1',
'flag' => 0x02,
'size' => 20
'size' => 20,
'class' => 'Herrera\\Box\\Signature\\Hash'
),
array(
'name' => 'SHA-256',
'flag' => 0x03,
'size' => 32
'size' => 32,
'class' => 'Herrera\\Box\\Signature\\Hash'
),
array(
'name' => 'SHA-512',
'flag' => 0x04,
'size' => 64
'size' => 64,
'class' => 'Herrera\\Box\\Signature\\Hash'
),
array(
'name' => 'OpenSSL',
'flag' => 0x10,
'size' => null
'size' => null,
'class' => 'Herrera\\Box\\Signature\\PublicKeyDelegate'
),
);

Expand Down Expand Up @@ -147,7 +153,7 @@ public function get($required = null)
);
}

return;
return null;
}

$this->seek(-8, SEEK_END);
Expand Down Expand Up @@ -231,53 +237,9 @@ public function verify()

$this->seek(0);

if (0x10 === $type['flag']) {
if (!extension_loaded('openssl')) {
throw OpenSslException::create(
'The "openssl" extension is required to verify signatures using a public key.'
);
}
$file = $this->file . '.pubkey';

if (false === ($key = @file_get_contents($file))) {
throw FileException::lastError();
}

/*
* At the moment, there doesn't seem to be an efficient way of
* generating a progressive hash without resorting to using both
* "openssl" and "phar" extensions.
*/
OpenSslException::reset();

ob_start();

$result = openssl_verify(
$this->read($size),
pack('H*', $signature['hash']),
$key
);

$error = trim(ob_get_clean());

if (-1 === $result) {
throw OpenSslException::lastError();
} elseif (!empty($error)) {
throw new OpenSslException($error);
}

return (1 === $result);
}

$context = @hash_init(
strtolower(
preg_replace('/\-/', '', $signature['hash_type'])
)
);

if (false === $context) {
throw Exception::lastError();
}
/** @var $verify VerifyInterface */
$verify = new $type['class']();
$verify->init($type['name'], $this->file);

$buffer = 64;

Expand All @@ -287,14 +249,12 @@ public function verify()
$size = 0;
}

hash_update($context, $this->read($buffer));
$verify->update($this->read($buffer));

$size -= $buffer;
}

$hash = strtoupper(hash_final($context));

return ($signature['hash'] === $hash);
return $verify->verify($signature['hash']);
}

/**
Expand All @@ -303,7 +263,7 @@ public function verify()
private function close()
{
if ($this->handle) {
fclose($this->handle);
@fclose($this->handle);

$this->handle = null;
}
Expand Down Expand Up @@ -342,7 +302,7 @@ private function handle()
*/
private function read($bytes)
{
if (false === ($read = fread($this->handle(), $bytes))) {
if (false === ($read = @fread($this->handle(), $bytes))) {
throw FileException::lastError();
}

Expand Down
71 changes: 57 additions & 14 deletions src/tests/Herrera/Box/Tests/SignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class SignatureTest extends TestCase
{
private $types;

public function getPhars()
{
return array(
Expand Down Expand Up @@ -114,36 +116,77 @@ public function testVerify($path)
$this->assertTrue($sig->verify());
}

public function testVerifyMissingKey()
{
$dir = $this->createDir();
// private methods

copy(RES_DIR . '/openssl.phar', "$dir/openssl.phar");
public function testHandle()
{
$sig = new Signature(__FILE__);

$sig = new Signature("$dir/openssl.phar");
$this->setPropertyValue($sig, 'file', '/does/not/exist');

$this->setExpectedException(
'Herrera\\Box\\Exception\\FileException',
'No such file or directory'
);

$sig->verify();
$this->callMethod($sig, 'handle');
}

public function testVerifyErrorHandlingBug()
public function testRead()
{
$dir = $this->createDir();
$sig = new Signature(__FILE__);

$this->setPropertyValue($sig, 'handle', true);

copy(RES_DIR . '/openssl.phar', "$dir/openssl.phar");
touch("$dir/openssl.phar.pubkey");
$this->setExpectedException(
'Herrera\\Box\\Exception\\FileException',
'boolean given'
);

$sig = new Signature("$dir/openssl.phar");
$this->callMethod($sig, 'read', array(123));
}

public function testReadShort()
{
$file = $this->createFile();
$sig = new Signature($file);

$this->setExpectedException(
'Herrera\\Box\\Exception\\OpenSslException',
'cannot be coerced'
'Herrera\\Box\\Exception\\FileException',
"Only read 0 of 1 bytes from \"$file\"."
);

$this->callMethod($sig, 'read', array(1));
}

public function testSeek()
{
$file = $this->createFile();
$sig = new Signature($file);

$this->setExpectedException(
'Herrera\\Box\\Exception\\FileException'
);

$this->callMethod($sig, 'seek', array(-1));
}

protected function setUp()
{
$this->types = $this->getPropertyValue(
'Herrera\\Box\\Signature',
'types'
);
}

protected function tearDown()
{
$this->setPropertyValue(
'Herrera\\Box\\Signature',
'types',
$this->types
);

$sig->verify();
parent::tearDown();
}
}

0 comments on commit 05da12e

Please sign in to comment.