嗯,它很复杂(我甚至说不可能,但是谁知道)只能用PHP来实现这一点。
首先,请阅读article about digital signature in Adobe PDF
其次,看完这个你就知道签名存储b和c字节根据/ BYTERANGE [ABCD]指示灯
第三,我们可以提取和B之间c从文档中提取签名本身(指导说它将是十六进制编码的PKCS7#对象)。
<?php
$content = file_get_contents('test.pdf');
$regexp = '#ByteRange\[(\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));
}
第四步,我们在文件signature.pkcs7中有PKCS#7对象。不幸的是,我不知道使用PHP从签名中提取信息的方法。所以,你必须能够运行shell命令来使用OpenSSL的
openssl pkcs7 -in signature.pkcs7 -inform DER -print_certs > info.txt
运行在文件中这个命令info.txt,你将有一个证书链后。最后一个是你需要的。你可以看到文件的结构并解析所需的数据。
请同时参阅this question,this question和this topic
编辑在2017年10月9日 明知劝你看到exactly this question 有,您可以调整您需要的代码。
use ASN1\Type\Constructed\Sequence;
use ASN1\Element;
use X509\Certificate\Certificate;
$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();
echo $commonNameValue;
我已经为你调整过了,但请自行完成。
你看过fdf函数吗? –
如果解决方案@Dennis发布,不适合你然后让我知道,我想给这个问题一个镜头 –
你有没有考虑添加c代码作为php的扩展? – Nitin