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

Commit 05da12e

Browse files
committed
Using new signature classes.
1 parent 096a9e9 commit 05da12e

File tree

2 files changed

+76
-73
lines changed

2 files changed

+76
-73
lines changed

src/lib/Herrera/Box/Signature.php

Lines changed: 19 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Herrera\Box\Exception\Exception;
66
use Herrera\Box\Exception\FileException;
77
use Herrera\Box\Exception\OpenSslException;
8+
use Herrera\Box\Signature\VerifyInterface;
89
use PharException;
910

1011
/**
@@ -48,27 +49,32 @@ class Signature
4849
array(
4950
'name' => 'MD5',
5051
'flag' => 0x01,
51-
'size' => 16
52+
'size' => 16,
53+
'class' => 'Herrera\\Box\\Signature\\Hash'
5254
),
5355
array(
5456
'name' => 'SHA-1',
5557
'flag' => 0x02,
56-
'size' => 20
58+
'size' => 20,
59+
'class' => 'Herrera\\Box\\Signature\\Hash'
5760
),
5861
array(
5962
'name' => 'SHA-256',
6063
'flag' => 0x03,
61-
'size' => 32
64+
'size' => 32,
65+
'class' => 'Herrera\\Box\\Signature\\Hash'
6266
),
6367
array(
6468
'name' => 'SHA-512',
6569
'flag' => 0x04,
66-
'size' => 64
70+
'size' => 64,
71+
'class' => 'Herrera\\Box\\Signature\\Hash'
6772
),
6873
array(
6974
'name' => 'OpenSSL',
7075
'flag' => 0x10,
71-
'size' => null
76+
'size' => null,
77+
'class' => 'Herrera\\Box\\Signature\\PublicKeyDelegate'
7278
),
7379
);
7480

@@ -147,7 +153,7 @@ public function get($required = null)
147153
);
148154
}
149155

150-
return;
156+
return null;
151157
}
152158

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

232238
$this->seek(0);
233239

234-
if (0x10 === $type['flag']) {
235-
if (!extension_loaded('openssl')) {
236-
throw OpenSslException::create(
237-
'The "openssl" extension is required to verify signatures using a public key.'
238-
);
239-
}
240-
$file = $this->file . '.pubkey';
241-
242-
if (false === ($key = @file_get_contents($file))) {
243-
throw FileException::lastError();
244-
}
245-
246-
/*
247-
* At the moment, there doesn't seem to be an efficient way of
248-
* generating a progressive hash without resorting to using both
249-
* "openssl" and "phar" extensions.
250-
*/
251-
OpenSslException::reset();
252-
253-
ob_start();
254-
255-
$result = openssl_verify(
256-
$this->read($size),
257-
pack('H*', $signature['hash']),
258-
$key
259-
);
260-
261-
$error = trim(ob_get_clean());
262-
263-
if (-1 === $result) {
264-
throw OpenSslException::lastError();
265-
} elseif (!empty($error)) {
266-
throw new OpenSslException($error);
267-
}
268-
269-
return (1 === $result);
270-
}
271-
272-
$context = @hash_init(
273-
strtolower(
274-
preg_replace('/\-/', '', $signature['hash_type'])
275-
)
276-
);
277-
278-
if (false === $context) {
279-
throw Exception::lastError();
280-
}
240+
/** @var $verify VerifyInterface */
241+
$verify = new $type['class']();
242+
$verify->init($type['name'], $this->file);
281243

282244
$buffer = 64;
283245

@@ -287,14 +249,12 @@ public function verify()
287249
$size = 0;
288250
}
289251

290-
hash_update($context, $this->read($buffer));
252+
$verify->update($this->read($buffer));
291253

292254
$size -= $buffer;
293255
}
294256

295-
$hash = strtoupper(hash_final($context));
296-
297-
return ($signature['hash'] === $hash);
257+
return $verify->verify($signature['hash']);
298258
}
299259

300260
/**
@@ -303,7 +263,7 @@ public function verify()
303263
private function close()
304264
{
305265
if ($this->handle) {
306-
fclose($this->handle);
266+
@fclose($this->handle);
307267

308268
$this->handle = null;
309269
}
@@ -342,7 +302,7 @@ private function handle()
342302
*/
343303
private function read($bytes)
344304
{
345-
if (false === ($read = fread($this->handle(), $bytes))) {
305+
if (false === ($read = @fread($this->handle(), $bytes))) {
346306
throw FileException::lastError();
347307
}
348308

src/tests/Herrera/Box/Tests/SignatureTest.php

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
class SignatureTest extends TestCase
1212
{
13+
private $types;
14+
1315
public function getPhars()
1416
{
1517
return array(
@@ -114,36 +116,77 @@ public function testVerify($path)
114116
$this->assertTrue($sig->verify());
115117
}
116118

117-
public function testVerifyMissingKey()
118-
{
119-
$dir = $this->createDir();
119+
// private methods
120120

121-
copy(RES_DIR . '/openssl.phar', "$dir/openssl.phar");
121+
public function testHandle()
122+
{
123+
$sig = new Signature(__FILE__);
122124

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

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

130-
$sig->verify();
132+
$this->callMethod($sig, 'handle');
131133
}
132134

133-
public function testVerifyErrorHandlingBug()
135+
public function testRead()
134136
{
135-
$dir = $this->createDir();
137+
$sig = new Signature(__FILE__);
138+
139+
$this->setPropertyValue($sig, 'handle', true);
136140

137-
copy(RES_DIR . '/openssl.phar', "$dir/openssl.phar");
138-
touch("$dir/openssl.phar.pubkey");
141+
$this->setExpectedException(
142+
'Herrera\\Box\\Exception\\FileException',
143+
'boolean given'
144+
);
139145

140-
$sig = new Signature("$dir/openssl.phar");
146+
$this->callMethod($sig, 'read', array(123));
147+
}
148+
149+
public function testReadShort()
150+
{
151+
$file = $this->createFile();
152+
$sig = new Signature($file);
141153

142154
$this->setExpectedException(
143-
'Herrera\\Box\\Exception\\OpenSslException',
144-
'cannot be coerced'
155+
'Herrera\\Box\\Exception\\FileException',
156+
"Only read 0 of 1 bytes from \"$file\"."
157+
);
158+
159+
$this->callMethod($sig, 'read', array(1));
160+
}
161+
162+
public function testSeek()
163+
{
164+
$file = $this->createFile();
165+
$sig = new Signature($file);
166+
167+
$this->setExpectedException(
168+
'Herrera\\Box\\Exception\\FileException'
169+
);
170+
171+
$this->callMethod($sig, 'seek', array(-1));
172+
}
173+
174+
protected function setUp()
175+
{
176+
$this->types = $this->getPropertyValue(
177+
'Herrera\\Box\\Signature',
178+
'types'
179+
);
180+
}
181+
182+
protected function tearDown()
183+
{
184+
$this->setPropertyValue(
185+
'Herrera\\Box\\Signature',
186+
'types',
187+
$this->types
145188
);
146189

147-
$sig->verify();
190+
parent::tearDown();
148191
}
149192
}

0 commit comments

Comments
 (0)