Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/Handler/SignEngine/Pkcs12Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,17 @@ private function getSignatures($resource): iterable {
rewind($resource);

$signature = stream_get_contents($resource, $signatureLength, $signatureStart);
if ($signature === false) {
yield null;
continue;
}

yield hex2bin($signature);
$decodedSignature = @hex2bin($signature);
if ($decodedSignature === false) {
yield null;
continue;
}
yield $decodedSignature;
}

$this->tempManager->clean();
Expand Down
25 changes: 25 additions & 0 deletions tests/php/Unit/Handler/SignEngine/Pkcs12HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,31 @@ public function testGetCertificateChainWithInvalidInput(): void {
fclose($invalidResource);
}

public function testGetCertificateChainWithCorruptedSignature(): void {
$handler = $this->getHandler();

$corruptedPdf = "%PDF-1.4\n"
. "1 0 obj<</Type/Sig/ByteRange [0 10 50 10]>>endobj\n"
. "ZZZZZZZZ\n" // Invalid hex data - will cause hex2bin to fail
. str_repeat('x', 100);

$resource = fopen('php://memory', 'r+');
fwrite($resource, $corruptedPdf);
rewind($resource);

$result = $handler->getCertificateChain($resource);

$this->assertIsArray($result);
$this->assertCount(1, $result);
$this->assertArrayHasKey('chain', $result[0]);
$this->assertArrayHasKey('signature_validation', $result[0]['chain'][0]);
$this->assertIsArray($result[0]['chain'][0]['signature_validation']);
$this->assertEquals(3, $result[0]['chain'][0]['signature_validation']['id']); // Digest Mismatch
$this->assertStringContainsString('Digest mismatch', $result[0]['chain'][0]['signature_validation']['label']);

fclose($resource);
}

public function testRealWorldUsagePattern(): void {
$handler = $this->getHandler();

Expand Down
Loading