简体   繁体   English

使用XML直接签署Office Word文档

[英]Directly signing an Office Word document using XML

I'm trying to sign a Microsoft Office Word file using C# in direct XML format. 我正在尝试使用直接XML格式的C#签署Microsoft Office Word文件。

There are some questions and ambiguousness arises here like: 有一些问题,这里出现了模棱两可的问题:

  • How should I fill in the digest values, what value should I exactly digest for each reference, and what value should I digest for each transform? 我应该如何填写摘要值,我应该为每个参考准确消化什么值,以及我应该为每个转换消化什么值?
  • What value should I sign to create the SignatureValue ? 我应该签署什么价值来创建SignatureValue
  • And what certificate should I include in x509 data and how? 我应该在x509数据中包含哪些证书以及如何? (We don't exactly have a certificate so a guide for creating one is appreciated.) (我们没有确切的证书,所以我们非常感谢您创建一个证书。)

The last question is am I doing this correctly? 最后一个问题是我正确地这样做了吗? Maybe there is something wrong here and I have missed a package that would otherwise itself interact with the openxml document and sign it. 也许这里有问题,我错过了一个软件包,否则它本身会与openxml文件交互并签名。

But remember though that I am actually trying to sign a data with an external token which has a pkcs11 interface available(but no CSP) so I have used the PKCS11 Interop library to migrate it to c#, but I'm still a little confused here. 但请记住,我实际上是尝试使用外部令牌签署数据,该外部令牌具有可用的pkcs11接口(但没有CSP),所以我使用了PKCS11 Interop库将其迁移到c#,但我在这里仍然有点困惑。

  • Is there a package that I can use to sign word documents? 有没有我可以用来签署word文档的包?
  • Or is there a package that eases openXML interation? 或者是否有一个简化openXML交互的包?
  • Or do I have to do it manually? 或者我必须手动完成吗?

Just for heads up any library that does the openXML interaction should also allow me to sign the documents using the pkcs11 commands (ie which gives me the bytestream to sign and digest but does the other parts itself, pkcs11 is really should not be considered an importance here(you can replace it with my custom algorithm)) 只是为了抬头任何执行openXML交互的库也应该允许我使用pkcs11命令对文档进行签名(即,它给我字节流来签名和消化但是其他部分本身,pkcs11实际上不应该被视为重要性在这里(你可以用我的自定义算法替换它))

BTW a signature builder in XML looks like this: BTW中的签名构建器如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#" Id="idPackageSignature">
  <SignedInfo>
    <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
    <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
    <Reference Type="http://www.w3.org/2000/09/xmldsig#Object" URI="#idPackageObject">
      <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
      <DigestValue/>
    </Reference>
    <Reference Type="http://www.w3.org/2000/09/xmldsig#Object" URI="#idOfficeObject">
      <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
      <DigestValue/>
    </Reference>
    <Reference Type="http://uri.etsi.org/01903#SignedProperties" URI="#idSignedProperties">
      <Transforms>
        <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
      </Transforms>
      <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
      <DigestValue/>
    </Reference>
  </SignedInfo>
  <SignatureValue/>
  <KeyInfo>
    <X509Data>
      <X509Certificate/>
    </X509Data>
  </KeyInfo>
  <Object Id="idPackageObject">
    <Manifest>
      <Reference URI="/_rels/.rels?ContentType=application/vnd.openxmlformats-package.relationships+xml">
        <Transforms>
          <Transform Algorithm="http://schemas.openxmlformats.org/package/2006/RelationshipTransform">
            <mdssi:RelationshipReference xmlns:mdssi="http://schemas.openxmlformats.org/package/2006/digital-signature" SourceId="rId1"/>
          </Transform>
          <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
        <DigestValue/>
      </Reference>
      <Reference URI="/word/_rels/document.xml.rels?ContentType=application/vnd.openxmlformats-package.relationships+xml">
        <Transforms>
          <Transform Algorithm="http://schemas.openxmlformats.org/package/2006/RelationshipTransform">
            <mdssi:RelationshipReference xmlns:mdssi="http://schemas.openxmlformats.org/package/2006/digital-signature" SourceId="rId5"/>
            <mdssi:RelationshipReference xmlns:mdssi="http://schemas.openxmlformats.org/package/2006/digital-signature" SourceId="rId4"/>
            <mdssi:RelationshipReference xmlns:mdssi="http://schemas.openxmlformats.org/package/2006/digital-signature" SourceId="rId3"/>
            <mdssi:RelationshipReference xmlns:mdssi="http://schemas.openxmlformats.org/package/2006/digital-signature" SourceId="rId2"/>
            <mdssi:RelationshipReference xmlns:mdssi="http://schemas.openxmlformats.org/package/2006/digital-signature" SourceId="rId1"/>
          </Transform>
          <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
        <DigestValue/>
      </Reference>
      <Reference URI="/word/document.xml?ContentType=application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
        <DigestValue/>
      </Reference>
      <Reference URI="/word/fontTable.xml?ContentType=application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml">
        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
        <DigestValue/>
      </Reference>
      <Reference URI="/word/settings.xml?ContentType=application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml">
        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
        <DigestValue/>
      </Reference>
      <Reference URI="/word/styles.xml?ContentType=application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml">
        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
        <DigestValue/>
      </Reference>
      <Reference URI="/word/theme/theme1.xml?ContentType=application/vnd.openxmlformats-officedocument.theme+xml">
        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
        <DigestValue/>
      </Reference>
      <Reference URI="/word/webSettings.xml?ContentType=application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml">
        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
        <DigestValue/>
      </Reference>
    </Manifest>
    <SignatureProperties>
      <SignatureProperty Id="idSignatureTime" Target="#idPackageSignature">
        <mdssi:SignatureTime xmlns:mdssi="http://schemas.openxmlformats.org/package/2006/digital-signature">
          <mdssi:Format>YYYY-MM-DDThh:mm:ssTZD</mdssi:Format>
          <mdssi:Value>2015-11-22T17:21:28Z</mdssi:Value>
        </mdssi:SignatureTime>
      </SignatureProperty>
    </SignatureProperties>
  </Object>
  <Object Id="idOfficeObject">
    <SignatureProperties>
      <SignatureProperty Id="idOfficeV1Details" Target="#idPackageSignature">
        <SignatureInfoV1 xmlns="http://schemas.microsoft.com/office/2006/digsig">
          <SetupID/>
          <SignatureText/>
          <SignatureImage/>
          <SignatureComments>TEST SIGNING</SignatureComments>
          <WindowsVersion>10.0</WindowsVersion>
          <OfficeVersion>15.0</OfficeVersion>
          <ApplicationVersion>15.0</ApplicationVersion>
          <Monitors>1</Monitors>
          <HorizontalResolution>1920</HorizontalResolution>
          <VerticalResolution>1080</VerticalResolution>
          <ColorDepth>32</ColorDepth>
          <SignatureProviderId>{00000000-0000-0000-0000-000000000000}</SignatureProviderId>
          <SignatureProviderUrl/>
          <SignatureProviderDetails>9</SignatureProviderDetails>
          <SignatureType>1</SignatureType>
        </SignatureInfoV1>
      </SignatureProperty>
    </SignatureProperties>
  </Object>
  <Object>
    <xd:QualifyingProperties xmlns:xd="http://uri.etsi.org/01903/v1.3.2#" Target="#idPackageSignature">
      <xd:SignedProperties Id="idSignedProperties">
        <xd:SignedSignatureProperties>
          <xd:SigningTime>2015-11-22T17:21:28Z</xd:SigningTime>
          <xd:SigningCertificate>
            <xd:Cert>
              <xd:CertDigest>
                <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
                <DigestValue/>
              </xd:CertDigest>
              <xd:IssuerSerial>
                <X509IssuerName>E=ali@ali.com, CN=ALI-PC, OU=ALI-PC, O=ALI-PC, L=ALI-PC, S=ALI-PC, C=98</X509IssuerName>
                <X509SerialNumber>1</X509SerialNumber>
              </xd:IssuerSerial>
            </xd:Cert>
          </xd:SigningCertificate>
          <xd:SignaturePolicyIdentifier>
            <xd:SignaturePolicyImplied/>
          </xd:SignaturePolicyIdentifier>
        </xd:SignedSignatureProperties>
        <xd:SignedDataObjectProperties>
          <xd:CommitmentTypeIndication>
            <xd:CommitmentTypeId>
              <xd:Identifier>http://uri.etsi.org/01903/v1.2.2#ProofOfOrigin</xd:Identifier>
              <xd:Description>Created and approved this document</xd:Description>
            </xd:CommitmentTypeId>
            <xd:AllSignedDataObjects/>
            <xd:CommitmentTypeQualifiers>
              <xd:CommitmentTypeQualifier>TEST SIGNING</xd:CommitmentTypeQualifier>
            </xd:CommitmentTypeQualifiers>
          </xd:CommitmentTypeIndication>
        </xd:SignedDataObjectProperties>
      </xd:SignedProperties>
    </xd:QualifyingProperties>
  </Object>
</Signature>

You can have a look at the source code of this project , it covers basic digital signatures for MS Office documents. 您可以查看该项目的源代码,它涵盖了MS Office文档的基本数字签名。 Note that the code is a bit outdated but the principles are the same 请注意,代码有点过时,但原则是相同的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Office Interop API枚举Word文档? - How to enumerate word document using office interop API? 使用.Net中的Microsoft.Office.Interop.Word将文档导出为HTML? - Export document as HTML using Microsoft.Office.Interop.Word in .Net? 在Office Word文档中查找单词 - Finding words in an office word document 使用XML和C#创建Word文档 - Creating a Word Document using XML and C# 使用包修改Word文档XML - Modifying Word Document XML using Packages 使用Microsoft.Office.Interop.Word将Word文档转换为SharePoint 2013中的PDF - Convert word document to PDF in SharePoint 2013 Using Microsoft.Office.Interop.Word 如何将整个datagridview导出成word文档(在C#中使用office word interop DLL) - How to export the entire datagridview into a word document (using office word interop DLL in C#) 如何使用Microsoft.Office.Interop.Word获取Word文档中的形状文本或图片? - how to get the text in shapes or picture in word document using Microsoft.Office.Interop.Word? 使用Microsoft.Office.Interop.Word创建的Word文档的复选框已禁用 - Checkbox disabled for Word document created using Microsoft.Office.Interop.Word 如何使用Microsoft.Office.Interop.Word在某些Word文档中查找和修改TextBox? - How to find and modify a TextBox in some word document using Microsoft.Office.Interop.Word?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM