Geo*_*iev 2 xml ms-word wordml xml-dsig
我正在尝试使用数字签名验证 MS Word *.docx 文件。为了进行验证,我必须计算引用节点的摘要并检查它是否与签名(sig1.xml)中给出的摘要相同。我找不到有关如何实现关系转换以计算该摘要的信息。
签名XML(sig1.xml)部分如下:
<Object Id="idPackageObject" xmlns:mdssi="http://schemas.openxmlformats.org/package/2006/digital-signature">
<Manifest><Reference URI="/_rels/.rels?ContentType=application/vnd.openxmlformats-package.relationships+xml">
<Transforms><Transform Algorithm="http://schemas.openxmlformats.org/package/2006/RelationshipTransform">
<mdssi:RelationshipReference SourceId="rId1"/></Transform>
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/></Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>1vWU/YTF/7t6ZjnE44gAFTbZvvA=</DigestValue>....(next ref node ....)..
<Reference URI="/word/document.xml?ContentType=application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>s2yQEJrQSfC0YoRe1hvm+IGBpJQ=</DigestValue></Reference>.....More Reference Nodes.....
Run Code Online (Sandbox Code Playgroud)
/_rels/.rels 文件本身:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/origin" Target="_xmlsignatures/origin.sigs"/>
</Relationships>
Run Code Online (Sandbox Code Playgroud)
所以我需要计算/_rels/.rels的SHA1,但在计算之前我必须应用关系变换和C14N。
当我计算没有关系变换的节点摘要时(例如,此节点的:)
<Reference URI="/word/document.xml?ContentType=application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>s2yQEJrQSfC0YoRe1hvm+IGBpJQ=</DigestValue>
</Reference>
Run Code Online (Sandbox Code Playgroud)
一切都很好,只需对引用的 URI(在这种情况下为 /word/document.xml)进行 SHA1,我就会得到与给定 int 签名节点相同的哈希值。但是当涉及到具有关系变换的节点时 - 计算永远不会给出与签名中所述相同的值。
我的问题一般是在哪里可以找到有关此关系转换的信息以及如何实现它?
谢谢,
乔治
小智 5
在这种情况下,有关转换和关系转换的主要信息来源可以在 ECMA 的“ Office Open XML 文件格式 — 开放打包约定”论文中找到。链接在这里。
重要部分是 13.2.4.24。
关系转换应创建 .rels 文件的副本,在本例中为“/_rels/.rels”,并删除所有与SourceId不匹配的关系节点。该文件是最终散列并创建摘要的内容。
在转换定义中指定的 SourceId 和 SourceType 值中,包实现者应删除所有没有与任何 SourceId 值匹配的Id 值或与任何 SourceType 值匹配的 Type 值的关系元素。
在第 3 步“准备规范化”下,它还指出:
如果Relationship 元素中缺少此可选属性,则包实现者应添加具有默认值的TargetMode属性。
因为我们是在同一个包中的文件之间建立关系,所以我们的值为“ Internal ”。您需要在散列之前添加此属性。
所以在转换和 c14n 之后,你应该有:
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="word/document.xml" TargetMode="Internal" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"></Relationship></Relationships>
Run Code Online (Sandbox Code Playgroud)
注意:如果您使用的是 unix 系统,请注意换行符,OPC 使用 CRLF 而不是 LF。