当前位置: 首页 > 知识库问答 >
问题:

pdf完整性验证失败

陆翰学
2023-03-14

我正在尝试通过bash命令验证pdf文件的完整性。

然后我破译了PKC通过

xxd -r -p pkcs7_extracted > pkcs7_extracted.bin

openssl asn1parse -inform DER <pkcs7_extracted.bin >pkcs7_extracted_decoded

从解码的pkcs7中我得到了一些有用的信息

 0:d=0  hl=4 l=5498 cons: SEQUENCE         
 4:d=1  hl=2 l=   9 prim: OBJECT            :pkcs7-signedData
 15:d=1  hl=4 l=5483 cons: cont [ 0 ]        
 19:d=2  hl=4 l=5479 cons: SEQUENCE          
 23:d=3  hl=2 l=   1 prim: INTEGER           :01
 26:d=3  hl=2 l=  15 cons: SET               
 28:d=4  hl=2 l=  13 cons: SEQUENCE          
 30:d=5  hl=2 l=   9 prim: OBJECT            :sha256
 41:d=5  hl=2 l=   0 prim: NULL              
 43:d=3  hl=2 l=  11 cons: SEQUENCE          
 ...
 5154:d=7  hl=2 l=   9 prim: OBJECT            :contentType
 5165:d=7  hl=2 l=  11 cons: SET               
 5167:d=8  hl=2 l=   9 prim: OBJECT            :pkcs7-data
 5178:d=6  hl=2 l=  47 cons: SEQUENCE          
 5180:d=7  hl=2 l=   9 prim: OBJECT            :messageDigest
 5191:d=7  hl=2 l=  34 cons: SET               
 5193:d=8  hl=2 l=  32 prim: OCTET STRING      [HEX DUMP]:18B399D208A08815DDF23C93B1B63B13757A6AA24B1932569D7A69D0DB3A34C2
 5227:d=5  hl=2 l=  13 cons: SEQUENCE          
 5229:d=6  hl=2 l=   9 prim: OBJECT            :sha256WithRSAEncryption
 5240:d=6  hl=2 l=   0 prim: NULL              
 5242:d=5  hl=4 l= 256 prim: OCTET STRING      [HEX DUMP]:8F4B21914173EC57E6B0533BB5E04FB7054F23AC299C1BDBF589ED164A3EABB611727BE9117AAC3161D9C18DCA08BD113DD3AA90E5922009FA12BA59E7F6587E81CD79BDED09F862C2C76F35D950926F1A31A3DCCE999A52DCE0C7F67D081E81A44397E8AF96A1051B8E51F2E2271221B06D05C9895E1846B1DBE02B558F5B9EF97C7EB0FF9A7C71A9764D5E205900818F07E82027D79D3F9A5AA72B3A0CF131F1B890D0BCBF3C4DD8A0229FABE15F6C2CA0CE079EB925B3998A1A6190596A88D8F07C1C12B8750636E69108E30E643A653B285A400080C9C5590C112451F6D69BAFC2686D6F1107B37A5DB36B9F797C49E61D4B44E62E17DD541778DE763AC5
 5502:d=0  hl=2 l=   0 prim: EOC              

特别是,我注意到messageDigest字段等于使用ByTerange获得的signedContent的计算摘要。

我已经提取了加密的散列,用我的公钥对其进行了解密,并用asn1命令再次进行了解码。

dd if=pkcs7_extracted.bin of=extracted.sign.bin bs=1 skip=$[ 5242 + 4 ] count=256

#decrypt

openssl rsautl -verify -pubin -inkey publickey.pem < extracted.sign.bin > verified.bin

#decode of result
openssl asn1parse -inform der -in verified.bin

结果是这个对象

0:d=0  hl=2 l=  49 cons: SEQUENCE          
2:d=1  hl=2 l=  13 cons: SEQUENCE          
4:d=2  hl=2 l=   9 prim: OBJECT            :sha256
15:d=2  hl=2 l=   0 prim: NULL              
17:d=1  hl=2 l=  32 prim: OCTET STRING      [HEX DUMP]:EBAA31519CD0CCA793FEC34AA6BDD8DFA5E4D5F63BA4711F6C8ECE5D20FEF393

我非常肯定解密工作了,因为对象被正确解码了,并且正如我所期望的那样包含一个sha256对象,但是正如您所看到的,摘要值是不同的...

此外,Acrobat当然会验证该签名的文档的完整性。

提前道谢!

共有1个答案

邬楚青
2023-03-14

请注意,在signeddata对象中,有多个哈希值需要考虑,这些值通常不相等。

请查看RFC3852中加密消息语法(CMS)对象的定义

(RFC 3852是从当前PDF规范ISO 32000-1引用的RFC;因此,即使它已被RFC 5652淘汰,更新的RFC中的更改可能不适用于此上下文。)

  SignedData ::= SEQUENCE {
    version CMSVersion,
    digestAlgorithms DigestAlgorithmIdentifiers,
    encapContentInfo EncapsulatedContentInfo,
    certificates [0] IMPLICIT CertificateSet OPTIONAL,
    crls [1] IMPLICIT RevocationInfoChoices OPTIONAL,
    signerInfos SignerInfos }

...

  SignerInfo ::= SEQUENCE {
    version CMSVersion,
    sid SignerIdentifier,
    digestAlgorithm DigestAlgorithmIdentifier,
    signedAttrs [0] IMPLICIT SignedAttributes OPTIONAL,
    signatureAlgorithm SignatureAlgorithmIdentifier,
    signature SignatureValue,
    unsignedAttrs [1] IMPLICIT UnsignedAttributes OPTIONAL }

...

  SignedAttributes ::= SET SIZE (1..MAX) OF Attribute

...

  signedAttrs is a collection of attributes that are signed.  The
  field is optional, but it MUST be present if the content type of
  the EncapsulatedContentInfo value being signed is not id-data.
  SignedAttributes MUST be DER encoded, even if the rest of the
  structure is BER encoded.  Useful attribute types, such as signing
  time, are defined in Section 11.  If the field is present, it MUST
  contain, at a minimum, the following two attributes:

     A content-type attribute having as its value the content type
     of the EncapsulatedContentInfo value being signed.  Section
     11.1 defines the content-type attribute.  However, the
     content-type attribute MUST NOT be used as part of a
     countersignature unsigned attribute as defined in section 11.4.

     A message-digest attribute, having as its value the message
     digest of the content.  Section 11.2 defines the message-digest
     attribute.

...

  The result of the message digest calculation process depends on
  whether the signedAttrs field is present.  When the field is absent,
  the result is just the message digest of the content as described
  above.  When the field is present, however, the result is the message
  digest of the complete DER encoding of the SignedAttrs value
  contained in the signedAttrs field.  Since the SignedAttrs value,
  when present, must contain the content-type and the message-digest
  attributes, those values are indirectly included in the result.

因此,你的观察

messageDigest字段等于使用ByTerange获得的signedContent的计算摘要。

 5178:d=6  hl=2 l=  47 cons: SEQUENCE          
 5180:d=7  hl=2 l=   9 prim: OBJECT            :messageDigest
 5191:d=7  hl=2 l=  34 cons: SET               
 5193:d=8  hl=2 l=  32 prim: OCTET STRING      [HEX DUMP]:18B399D208A08815DDF23C93B1B63B13757A6AA24B1932569D7A69D0DB3A34C2

指示正确的数据已签名,因为message-digest属性的值应为内容的消息摘要。

PPS:OP通过解密签名字节、提取包含的散列并将其与实际的散列进行比较来验证。这对于基于RSA的签名是可以的。基于DSA或ECDSA的签名无法解密,因此无法提取哈希值。必须使用特殊的验证程序对其进行验证。

PPP:有不同风格的集成PDF签名。虽然这里使用的样式(PKCS7/CADES detached)是最常见和推荐的样式,但在通用解决方案中,必须事先进行检查并进行相应的验证。

 类似资料:
  • 我们正在构建一个需要精确性的apiendpoint。我们希望对POST/PUT到服务器的参数实施严格的验证。 如果api用户发送了一个不受支持的对(例如,我们允许参数[first\u name,last\u name],并且用户包含一个不受支持的参数[country]),我们希望验证失败。 已尝试构建名为(用作)的自定义验证器,但要使其在数组中可用,必须将其应用于嵌套/子属性列表的父级(…因为我们

  • 1.1.1.完整性 Android是一个完整的平台,即为移动设备提供的一套完整的软件架构。 面向开发者,Android提供了一套完整的工具和框架,以简化开发过程、提高开发效率。想要开发Android应用的话,Android SDK就是你所需的一切——甚至不需要一台真正的手机。 面向用户,Android开机即用。而且,用户可以按照自己的喜好做出相当程度的自定义。 面向生产厂商,Android就是令他

  • 在每个纪元结束时,我会得到以下输出: 谁能给我解释一下损失、准确性、验证损失和验证准确性之间有什么区别吗?

  • 本文向大家介绍vuejs简单验证码功能完整示例,包括了vuejs简单验证码功能完整示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了vuejs简单验证码功能。分享给大家供大家参考,具体如下: 使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun运行上述代码,可得到如下运行结果: 希望本文所述对大家vue.js

  • - 我运行了此脚本,但出现了此错误。我怎么做?

  • 问题内容: 我想知道为什么这不起作用 如果它是整数,如何验证从GET / POST传递的数据? 问题答案: 该 手册 说: 要测试变量是数字还是数字字符串(例如表单输入,它始终是字符串),必须使用is_numeric()。 另外,您可以使用基于正则表达式的测试: