首页 > 解决方案 > 如何使用 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");
}

标签: phppdfcertificatedigital-signaturesignature

解决方案


推荐阅读