2

我正在尝试使用数字签名验证 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.....

/_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>

所以我需要计算/_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> 

一切都很好,只需对引用的 URI(在这种情况下为 /word/document.xml)进行 SHA1 处理,就可以得到与签名节点中给定的哈希值相同的哈希值。但是当涉及到具有关系转换的节点时 - 计算永远不会给出与签名中所述相同的值。

我的问题一般是在哪里可以找到有关这种关系转换的信息以及如何实现它?

谢谢,

乔治

4

1 回答 1

5

在这种情况下,有关转换和关系转换的主要信息来源可以在 ECMA 的“ Office Open XML File Formats — Open Packaging Conventions ”论文中找到。链接在这里

重要的部分是 13.2.4.24。

关系转换应创建 .rels 文件的副本,在本例中为“/_rels/.rels”,并删除所有与SourceId不匹配的关系节点。该文件最终被散列并创建摘要。

在转换定义中指定的 SourceId 和 SourceType 值中,包实现者应删除所有没有与任何 SourceId 值匹配的 Id 值或与任何 SourceType 值匹配的 Type 值的关系元素。

在第 3 步“准备规范化”下,它还指出:

如果关系元素中缺少此可选属性,则包实现者应添加具有默认值的TargetMode属性

因为我们在同一个包中的文件之间创建关系,所以我们有“内部”的值。您需要在散列之前添加此属性。

所以在转换和 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>

注意:如果您使用的是 unix 系统,请注意换行符,OPC 使用 CRLF 而不是 LF。

于 2017-02-16T11:39:45.600 回答