diff --git a/lib/Handler/DocMdpHandler.php b/lib/Handler/DocMdpHandler.php index 8f6c254be9..cd58776786 100644 --- a/lib/Handler/DocMdpHandler.php +++ b/lib/Handler/DocMdpHandler.php @@ -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); @@ -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)) { @@ -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( + '/(?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'; diff --git a/tests/php/fixtures/PdfGenerator.php b/tests/php/fixtures/PdfGenerator.php index f5790c64a7..64eec08f2f 100644 --- a/tests/php/fixtures/PdfGenerator.php +++ b/tests/php/fixtures/PdfGenerator.php @@ -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";