使用PDFBox验证pdf文件中PDSignature的字节范围

问题描述 投票:0回答:1

我已经加载了PDDocument。

我检索了名为sig的PDSignature对象。

签名的字节范围由sig.getByteRange()提供。以我为例:

0-18373 43144-46015

我要验证签名的字节范围有效。因为签名必须验证整个文件本身。签名还提供了字节范围,因此我不能依靠它。

我可以检查第一个值为0,最后一个值必须为文件-1的大小。

但是我还需要验证第二个和第三个值(18373和43144)。因此,我需要知道PDSignature在文档中的位置及其长度。

我如何获得这些?

java pdf pdfbox
1个回答
2
投票

[查看PDFBox示例ShowSignature。它间接执行此操作:它检查字节范围间隙中的字节是否与文档解析确定的签名值完全一致。

在方法ShowSignature中:

showSignature

辅助方法int[] byteRange = sig.getByteRange(); if (byteRange.length != 4) { System.err.println("Signature byteRange must have 4 items"); } else { long fileLen = infile.length(); long rangeMax = byteRange[2] + (long) byteRange[3]; // multiply content length with 2 (because it is in hex in the PDF) and add 2 for < and > int contentLen = contents.getString().length() * 2 + 2; if (fileLen != rangeMax || byteRange[0] != 0 || byteRange[1] + contentLen != byteRange[2]) { // a false result doesn't necessarily mean that the PDF is a fake // see this answer why: // https://stackoverflow.com/a/48185913/535646 System.out.println("Signature does not cover whole document"); } else { System.out.println("Signature covers whole document"); } checkContentValueWithFile(infile, byteRange, contents); }

checkContentValueWithFile

(严格来说,用普通括号括起来的二进制字符串也可以。

© www.soinside.com 2019 - 2024. All rights reserved.