php - 如何使用 PHP 验证 pdf 文档的签名?
问题描述
背景
我们允许用户在我们的后端系统 (Laravel/php) 中上传一个 pdf 文件,其中包含他们从荷兰政府机构 (UWV Verzekeringsbericht) 的网站下载的工资信息。本文档有数字签名。
目标
我们想要验证和验证这个签名,所以我们知道用户没有更改这个文件的任何内容并且文件是正确的。您可以如何在 adobe acrobat reader 中验证证书。
问题
在过去的几个小时里,我一直在努力做到这一点,但由于我的知识/经验有限,我似乎无法让它发挥作用。任何有关如何完成此操作的信息和/或其他信息/教程将不胜感激。我愿意阅读有关这方面的基础知识,但我真的不知道从哪里开始。
代码
哦,男孩,准备好让我的代码大吃一惊。这是因为没有真正了解事情是如何工作的,而是在堆栈溢出中搜索并尝试解决方案。
public function first_attempt(): void
{
$path = app_path('/Verzekeringsbericht.pdf');
$content = file_get_contents($path);
$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($path, '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));
shell_exec('openssl pkcs7 -in signature.pkcs7 -inform DER -print_certs > cert.pem');
}
}
public function second_attempt(): void
{
$path = 'file://' . app_path('/qveucag4.pem');
$authorKey = openssl_pkey_get_public($path);
}
public function third_attempt(): void
{
$pemPath = app_path('/qveucag4.pem');
$fileCertPath = app_path('/cert.pem');
shell_exec("openssl verify -verbose -CAfile $pemPath $fileCertPath");
}
解决方案
推荐阅读
- python-3.x - 如何生成具有已知概率的随机数?
- java - 在 Linux 上的 Spring Boot 中使用 JNA 加载 .so 文件的问题
- javascript - Firebase 云功能在事务中创建文档并使用它来更新另一个文档
- java - 如何从 REST API 战争中访问主应用程序 WAR 中使用的远程 ejb
- java - 将自己的应用程序添加到 whatsapp 音频选择
- r - 如何在注释函数中使用表达式 enquo()
- python - python浮动到没有点的指数中的字符串
- ruby-on-rails - 将www url重定向到rails中的非www url
- xml - 使用 XPath 从 Postgres XML 列中提取数据
- symfony - 升级到 symfony 3.4 后访问 js 和 css