1

How I can get values (date, signing name etc.) from sign('s) inside PDF? At my application on Joomla and Fabrik component i can run PHP and JS code.

The code I am currently using from Stack is not working as expected. If I read on browser $content variable, i cannot see /ByteRange, but when i take it to txt file, there is. On my XAMPP instance i have installed Composer and ASN1 and X509 libraries.

For all, this is my code with some changes for take content form file (I thought maybe it would work, but it doesnt) :

<?php
require_once('C:\Windows\System32\vendor\autoload.php');

use Sop\ASN1\Type\Constructed\Sequence;
use Sop\ASN1\Element;
use Sop\X509\Certificate\Certificate;  



$currentFile = 'C:\xampp\htdocs\ArchiTW\upload\kprocajlo\dokumenty\test.pdf';

$content = file_get_contents($currentFile);
file_put_contents('C:\xampp\htdocs\ArchiTW\upload\kprocajlo\dokumenty\text.txt',$content);

$txt='C:\xampp\htdocs\ArchiTW\upload\kprocajlo\dokumenty\text.txt';


$regexp = '/ByteRange\ \[\s*(\d+) (\d+) (\d+)/'; // subexpressions are used to extract b and c
//$regexp = '#ByteRange\s*\[(\d+) (\d+) (\d+)#';


$result = [];
$abc=preg_match_all($regexp, $txt, $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($currentFile, 'rb')) {
        $signature = stream_get_contents($stream, $end - $start - 2, $start + 1); // because we need to exclude < and > from start and end
        fclose($stream);
    
    }
 

    $binaryData = hex2bin($signature);
    
    $seq = Sequence::fromDER($binaryData);
    $signed_data = $seq->getTagged(0)->asExplicit()->asSequence();
    // ExtendedCertificatesAndCertificates: https://tools.ietf.org/html/rfc2315#section-6.6
    $ecac = $signed_data->getTagged(0)->asImplicit(Element::TYPE_SET)->asSet();
    // ExtendedCertificateOrCertificate: https://tools.ietf.org/html/rfc2315#section-6.5
    $ecoc = $ecac->at($ecac->count() - 1);
    $cert = Certificate::fromASN1($ecoc->asSequence());
    $commonNameValue = $cert->tbsCertificate()->subject()->toString();
    $StatusMsg = $commonNameValue;
    echo $commonNameValue;
    
}
Krzysztof
  • 11
  • 1

0 Answers0