sapp icon indicating copy to clipboard operation
sapp copied to clipboard

Extracting all signatures

Open henrywood opened this issue 1 year ago • 5 comments

How can I extract all signatures from a PDF (to validate them one by one) ?

Please help !

Thanks !

henrywood avatar Oct 08 '23 11:10 henrywood

Do you need the signatures? Or the signed documents at each version?

Please check method get_signatures

dealfonso avatar Oct 08 '23 13:10 dealfonso

@dealfonso Thanks ! I don't know how I was able to overlook that method :)

henrywood avatar Oct 08 '23 15:10 henrywood

This line:

https://github.com/dealfonso/sapp/blob/main/src/PDFDoc.php#L1076C1-L1076C60

throws a TypeError on some signed PDFs:

PHP Warning: Undefined array key 0 in /tmp/sapp/vendor/ddn/sapp/src/PDFDoc.php on line 1076 PHP Fatal error: Uncaught TypeError: Unsupported operand types: array + bool in /tmp/sapp/vendor/ddn/sapp/src/PDFDoc.php:1076 Stack trace: #0 /tmp/sapp/vendor/ddn/sapp/src/PDFDoc.php(1090): ddn\sapp\PDFDoc->get_signatures() #1 /tmp/sapp/test_verify.php(19): ddn\sapp\PDFDoc->get_signature_count() #2 {main} thrown in /tmp/sapp/vendor/ddn/sapp/src/PDFDoc.php on line 1076

(I am running PHP 8.2)

Apparently $cert[0] is not set such cases ?

I tested using this script:

<?php

use ddn\sapp\PDFDoc;

require_once('vendor/autoload.php');

if (($argc < 2) || ($argc > 3))
        fwrite(STDERR, sprintf("usage: %s <filename> [<output>]", $argv[0]));
else {
        if (!file_exists($argv[1]))
                fwrite(STDERR, "failed to open file " . $argv[1]);
        else {
                $obj = PDFDoc::from_string(file_get_contents($argv[1]));

                if ($obj === false)
                        fwrite(STDERR, "failed to parse file " . $argv[1]);
                else {

                        $sigCount = $obj->get_signature_count();

                        if ($sigCount > 0) {

                                $signatures = $obj->get_signatures();

                                $okCount = 0;

                                var_dump($signatures);

                                foreach($signatures as $signature) {

                                        // TODO


                                }



                        } else {

                                // Document does not have a digital signature
                                throw new Exception('This document contains no digital signatures');
                        }
                }
        }
}

henrywood avatar Oct 08 '23 16:10 henrywood

Works: pdf_digital_signature_timestamp.pdf Produces the typeError above: sample01.pdf

henrywood avatar Oct 08 '23 16:10 henrywood

#59 avoids the TypeError

erikn69 avatar Oct 09 '23 16:10 erikn69