Skip to content
Open
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
33 changes: 31 additions & 2 deletions lib/Handler/DocMdpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function extractDocMdpData($resource): array {
}

rewind($resource);
$content = stream_get_contents($resource);
$content = $this->removeUnreferencedSignatures(stream_get_contents($resource));
$isoStatus = $this->getIsoComplianceStatus($content);

rewind($resource);
Expand Down Expand Up @@ -85,7 +85,7 @@ public function extractDocMdpData($resource): array {
*/
private function extractDocMdpLevel($pdfResource): DocMdpLevel {
rewind($pdfResource);
$content = stream_get_contents($pdfResource);
$content = $this->removeUnreferencedSignatures(stream_get_contents($pdfResource));

$isoStatus = $this->getIsoComplianceStatus($content);
if (!$this->isIsoCompliant($isoStatus) && !$this->isOnlySingleDocMdpViolation($isoStatus)) {
Expand All @@ -100,6 +100,35 @@ private function extractDocMdpLevel($pdfResource): DocMdpLevel {
return DocMdpLevel::tryFrom($pValue) ?? DocMdpLevel::NOT_CERTIFIED;
}

/**
* Drop signature dictionaries that no other object references.
*
* A signature dictionary only takes effect when something points to it,
* such as a signature field (/V) or the catalog (/Perms /DocMDP). A
* leftover one that no other object references is not an active
* certification.
*
* References inside compressed object streams cannot be seen in the raw
* content, so PDFs with object streams are returned unchanged.
*/
private function removeUnreferencedSignatures(string $content): string {
if (preg_match('/\/Type\s*\/ObjStm\b/', $content)) {
return $content;
}

return preg_replace_callback(
'/(?<!\d)(\d+)\s+(\d+)\s+obj\b.*?endobj/s',
function (array $match) use ($content): string {
if (!preg_match('/\/Type\s*\/Sig\b/', $match[0])) {
return $match[0];
}
$reference = '/(?<!\d)' . $match[1] . '\s+' . $match[2] . '\s+R\b/';
return preg_match($reference, $content) ? $match[0] : '';
},
$content
) ?? $content;
}

/**
* Validate all ISO 32000-1 DocMDP requirements
*
Expand Down
24 changes: 24 additions & 0 deletions tests/php/Unit/Handler/DocMdpHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,30 @@ public function testAdditionalSignaturesBasedOnDocMdpLevel(string|int $level, bo
$this->assertSame($expectedAllowed, $result);
}

public static function standaloneDocMdpSignatureProvider(): array {
return [
'Unreferenced P=1 dictionary is not a certification' => ['', '', true],
'A reference to another object number does not count' => ['/Metadata 15 0 R', '', true],
'P=1 dictionary referenced from /Perms still blocks' => ['/Perms << /DocMDP 5 0 R >>', '', false],
'P=1 dictionary referenced from a signature field still blocks' => ['/AcroForm << /Fields [6 0 R] >>', "6 0 obj\n<< /FT /Sig /T (Signature1) /V 5 0 R >>\nendobj\n", false],
'Object streams may hide the reference, so P=1 still blocks' => ['', "7 0 obj\n<< /Type /ObjStm /N 0 /First 0 /Length 0 >>\nstream\n\nendstream\nendobj\n", false],
];
}

#[DataProvider('standaloneDocMdpSignatureProvider')]
public function testOnlyReferencedSignatureDictionariesCertify(string $catalogExtra, string $extraObject, bool $expectedAllowed): void {
$pdfContent = PdfGenerator::createPdfWithStandaloneDocMdpSignature(1, $catalogExtra, $extraObject);

$resource = PdfGenerator::createResourceFromContent($pdfContent);
$allowed = $this->handler->allowsAdditionalSignatures($resource);
$data = $this->handler->extractDocMdpData($resource);
fclose($resource);

$this->assertSame($expectedAllowed, $allowed);
$expectedLevel = $expectedAllowed ? DocMdpLevel::NOT_CERTIFIED : DocMdpLevel::CERTIFIED_NO_CHANGES_ALLOWED;
$this->assertSame($expectedLevel->value, $data['docmdp']['level']);
}

public function testRealJSignPdfWithDocMdpLevel1(): void {
$pdfPath = __DIR__ . '/../../fixtures/pdfs/real_jsignpdf_level1.pdf';

Expand Down
17 changes: 17 additions & 0 deletions tests/php/fixtures/PdfGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ public static function createPdfWithDocMdp(int $pValue, bool $withModifications
return $pdf;
}

/**
* Unsigned PDF with a leftover DocMDP signature dictionary.
* Object 5 is referenced from nowhere unless a referrer is given, such as
* '/Perms << /DocMDP 5 0 R >>' for the catalog.
*/
public static function createPdfWithStandaloneDocMdpSignature(int $pValue, string $catalogExtra = '', string $extraObject = ''): string {
$pdf = "%PDF-1.7\n";
$pdf .= "1 0 obj\n<< /Type /Catalog /Pages 2 0 R $catalogExtra >>\nendobj\n";
$pdf .= "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n";
$pdf .= "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\nendobj\n";
$pdf .= "5 0 obj\n<< /Type /Sig /Filter /Adobe.PPKLite /SubFilter /adbe.pkcs7.detached /ByteRange [0 0 0 0] /Contents <00>\n";
$pdf .= "/Reference [<< /Type /SigRef /TransformMethod /DocMDP /TransformParams << /Type /TransformParams /P $pValue /V /1.2 >> >>] >>\nendobj\n";
$pdf .= $extraObject;
$pdf .= "xref\n0 6\ntrailer\n<< /Size 6 /Root 1 0 R >>\nstartxref\n" . strlen($pdf) . "\n%%EOF";
return $pdf;
}

public static function createPdfWithFormFieldModification(int $pValue): string {
$pdf = "%PDF-1.7\n";
$pdf .= "1 0 obj\n<< /Type /Catalog /Pages 2 0 R /AcroForm 4 0 R /Perms << /DocMDP 5 0 R >> >>\nendobj\n";
Expand Down
Loading