How to retrieve digital signature information from PDF with PHP?

✔ Recommended Answer

Well, it's complicated (I would say even impossible, but who knows) to achieve this only with PHP.

At first, please read article about digital signature in Adobe PDF

Second, after reading this you will know that signature is stored between b and c bytes according to /ByteRange[a b c d] indicator

Third, we can extract b and c from document and then extract signature itself (guide says it will be hexdecoded PKCS7# object).

<?php $content = file_get_contents('test.pdf'); $regexp = '#ByteRange\[\s*(\d+) (\d+) (\d+)#'; // subexpressions are used to extract b and c $result = []; preg_match_all($regexp, $content, $result); // $result[2][0] and $result[3][0] are b and c if (isset($result[2]) && isset($result[3]) && isset($result[2][0]) && isset($result[3][0])) {     $start = $result[2][0];     $end = $result[3][0];     if ($stream = fopen('test.pdf', 'rb')) {         $signature = stream_get_contents($stream, $end - $start - 2, $start + 1); // because we need to exclude < and > from start and end         fclose($stream);     }     file_put_contents('signature.pkcs7', hex2bin($signature));}

Forth, after third step we have PKCS#7 object in file signature.pkcs7. Unfortunately, I don't know methods to extract information from signature using PHP. So you must be able to run shell commands to use openssl

openssl pkcs7 -in signature.pkcs7 -inform DER -print_certs > info.txt

After running this command in file info.txt you will have a chain of certificates. Last one is the one you need. You can see the structure of the file and parse needed data.

Please also refer to this question, this question and this topic

EDIT at 2017-10-09I knowingly advised you to see exactly this questionThere is a code that you can adjust to your needs.

use ASN1\Type\Constructed\Sequence;use ASN1\Element;use X509\Certificate\Certificate;       $seq = Sequence::fromDER($binaryData);$signed_data = $seq->getTagged(0)->asExplicit()->asSequence();// ExtendedCertificatesAndCertificates:$ecac = $signed_data->getTagged(0)->asImplicit(Element::TYPE_SET)->asSet();// ExtendedCertificateOrCertificate:$ecoc = $ecac->at($ecac->count() - 1);$cert = Certificate::fromASN1($ecoc->asSequence());$commonNameValue = $cert->tbsCertificate()->subject()->toString();echo $commonNameValue;

I've adjusted it for you, but please make the rest by yourself.


Answered By: Denis Alimov

Method #2

To retrieve digital signature information from a PDF using PHP, you can use the "Zend_Pdf" class library. This library provides various methods to read and manipulate PDF files. Here are the steps you can follow:

  1. Load the PDF file into Zend_Pdf:
$pdf = Zend_Pdf::load('example.pdf');
  1. Get the list of signature fields present in the PDF:
$signatureFields = $pdf->getSignatureFields();
  1. Loop through the signature fields and retrieve the signature information:
foreach ($signatureFields as $field) { $signature = $field->getSignature(); //Retrieve signature information using various methods of the $signature object }
  1. The $signature object provides various methods to retrieve the signature information, such as the signer's name, signing time, certificate details, etc. Here are some examples:
$signature->getName(); $signature->getDate(); $signature->getCertificate();

You can use these methods to retrieve the digital signature information from the PDF using PHP. Note that you will need to have the "Zend_Pdf" library installed in your PHP environment to use this method.


Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755