【发布时间】:2019-01-25 09:04:40
【问题描述】:
这个问题与this 相关,但更具体一点。我怀疑我没有正确计算我的 pdf 的哈希值。
我想计算签名 PDF 的 SHA256 哈希值。
根据PDF32000我应该:
- 获取
\ByteRange值 - 连接两个块
- 计算 SHA256
这是我所做的:
$ grep -aPo 'ByteRange\[\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*\]' dummy-signed.pdf
ByteRange[ 0 59718 72772 5058]
$ dd if=dummy-signed.pdf of=head.bin bs=1 skip=0 count=59718
59718 bytes (60 kB, 58 KiB) copied, 0.630196 s, 94.8 kB/s
$ dd if=dummy-signed.pdf of=tail.bin bs=1 skip=72772 count=5058
5058 bytes (5.1 kB, 4.9 KiB) copied, 0.064317 s, 78.6 kB/s
$ cat head.bin tail.bin > whole.bin
$ sha256sum whole.bin
04b69f55f12fa5cc7923f4307154f2702efde43b32e4a8d9dbb0507a56fcecd3 whole.bin
我检查了我不包括< 和> 字符:
$ hexdump -C head.bin | tail -n3
0000e930 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 43 6f | /Co|
0000e940 6e 74 65 6e 74 73 |ntents|
0000e946
$ hexdump -C tail.bin | head -n3
00000000 2f 46 69 6c 74 65 72 2f 41 64 6f 62 65 2e 50 50 |/Filter/Adobe.PP|
00000010 4b 4c 69 74 65 2f 4d 28 44 3a 32 30 31 39 30 31 |KLite/M(D:201901|
00000020 32 38 31 33 34 30 35 38 2b 30 31 27 30 30 27 29 |28134058+01'00')|
不幸的是,我的签名似乎是错误的,但在解码 PKCS7 签名后,我再次检查了哈希是 sha256WithRSAEncryption,所以在验证了这个摘要之后,我得到了另一个哈希,而不是我计算的那个。
我的/SubFilter 是:
$ grep -aPo '/SubFilter.*?(?=>)' dummy-signed.pdf
/SubFilter/adbe.pkcs7.detached/Type/Sig
我的 PDF 版本是:
$ grep -aPo '%PDF-\d.\d' dummy-signed.pdf
%PDF-1.6
因此,从带有adbe.pkcs7.detached 和 PDF 1.6 的 PDF32000 开始,HASH 应该是 SHA256,它与我在 PKCS7 中找到的内容兼容。
仅作记录,我从签名中得到的哈希是:
#!/bin/bash
PKCS7='out.pkcs7'
# Extract Digest (SHA256)
OFFSET=$(openssl asn1parse -inform der -in $PKCS7 | \
perl -ne 'print $1 + $2 if /(\d+):d=\d\s+hl=(\d).*?256 prim.*HEX DUMP/m')
dd if=$PKCS7 of=signed-sha256.bin bs=1 skip=$OFFSET count=256
# Extract Public key
openssl pkcs7 -print_certs -inform der -in $PKCS7 | \
tac | sed '/-----BEGIN/q' | tac > client.pem
openssl x509 -in client.pem -pubkey -noout > client.pub.pem
# Verify the signature
openssl rsautl -verify -pubin -inkey client.pub.pem < signed-sha256.bin > verified.bin
# Get Hash and compare with the computed hash from the PDF
openssl asn1parse -inform der -in verified.bin | grep -Po '\[HEX DUMP\]:\K\w+$' | tr A-F a-f
$ ./verify-signature.sh
256+0 records in
256+0 records out
256 bytes copied, 0.029548 s, 8.7 kB/s
2a3f629f7bdce750321da7f219ec5759dc9ed14818acbd3cd0b6092d5371c03a
您可以通过我的gist访问测试PDF文件dummy-signed.pdf
curl https://gist.githubusercontent.com/nowox/94dd54e484df877e1232c18bd7b91c97/raw/d249f3757137e9b665e895c900f08b1156f1bc4f/dummy-signed.pdf.base64 | base64 --decode > dummy-signed.pdf
【问题讨论】:
-
您完全忽略了签名的 Subfilter 条目。根据子过滤器的值,签名容器的格式和实际签名的数据确实不同。
-
请分享有问题的 pdf 文件以供进一步分析。
-
@mkl 我已经分享了我的 PDF。对不起base64。否则我不知道如何分享 pdf。
-
共享 PDF(或任意二进制文件,内嵌显示的图像除外)的最简单方法是使用公共文件共享(Dropbox 或 Google Drive 公共共享)。但是您的 base64 编码要点也有效。