3

在我尝试使用的 SOAP Web 服务中,有必要发送具有类似于以下结构的 XML:

<?xml version="1.0"?>
<TheData>
  <Father Id="zzz">
      <SomeInfo>1</SomeInfo>
      <List>
        <ElementOfList>
          <Child Id="foo">foo</Child>
          <Signature>
            ...
          </Signature>
        </ElementOfList>
        <ElementOfList>
          <Child Id="bar">bar</Child>
          <Signature>
            ...
          </Signature>
        </ElementOfList>
      </List>
  </Father>
  <Signature>
    ...
  </Signature>
</TheData>

其中<Signature>有内容:

<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
  <SignedInfo>
    <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"/>
    <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
    <Reference URI="#[the _Id_ attr of this Signature's sibling element]">
      <Transforms>
        <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
      </Transforms>
      <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
      <DigestValue>uCMzpgMnLCP9iESFQVgpmtQ5TRE=</DigestValue>
    </Reference>
  </SignedInfo>
  <SignatureValue>...</SignatureValue>
  <KeyInfo>
    <X509Data>
      <X509Certificate>...</X509Certificate>
    </X509Data>
  </KeyInfo>
</Signature>

我该怎么做?

我正在尝试xmlsec1 --id-attr:Id Father --id-attr:Id Child,将字段为空白的文件传递给它Signature,但它只填充其中的第一个。

我还尝试单独签署孩子,将其放在父亲模板中,他们试图签署父亲,但xmlsec1忽略第二个元素(并更改第一个签名的值 - 它不应该封装在其元素中吗? )。


可能无关,但谁知道呢?

我宁愿从 Python 代码中执行此操作,但是我尝试使用的三个库python-xmlsec、pyxmlsecxmldsig无法生成/重新调整. 可能是因为他们缺少of ,但是我得到的这些问题揭示了一个事实,即我并不真正理解这些 XML 签名的东西,因此,我做错了事情并把它搞砸了。请帮助我理解它。URI<Reference>--id-attrxmlsec1


编辑

我似乎有很多人在这些 XML 签名主题方面遇到困难,但没有人试图在同一个 XML 文件中签署两个不同的元素。w3C 场景常见问题解答中也没有列出这种情况,这让一切看起来都很奇怪,因为我的网络服务需要我多重签名。或者不?这是他们发布的架构:https ://github.com/proge/PyNFSe/blob/master/pysped_nfse/nfse.xsd#L539 (请参阅此元素<xsd:element name="EnviarLoteRpsEnvio">和子元素)。

4

1 回答 1

1

我认为使用 3rd 方库没有办法做到这一点,大多数应用程序只需要所有 XML 内容的签名,而不需要特定元素的签名。

也许,这可以帮助你:

根据 XSD 相对于 "RecepcionarLoteRps" 方法

<s:element name="RecepcionarLoteRps">
    <s:complexType>
        <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="EnviarLoteRpsEnvio" type="tns:EnviarLoteRpsEnvio"/>
        </s:sequence>
    </s:complexType>
</s:element>
<s:complexType name="EnviarLoteRpsEnvio">
    <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="LoteRps" type="tns:tcLoteRps"/>
        <s:element minOccurs="0" maxOccurs="1" name="Signature" type="s1:SignatureType"/>
    </s:sequence>
</s:complexType>
<s:complexType name="tcLoteRps">
    <s:sequence>
        <s:element minOccurs="1" maxOccurs="1" name="NumeroLote" type="s:unsignedLong"/>
        <s:element minOccurs="0" maxOccurs="1" name="Cnpj" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="InscricaoMunicipal" type="s:string"/>
        <s:element minOccurs="1" maxOccurs="1" name="QuantidadeRps" type="s:int"/>
        <s:element minOccurs="0" maxOccurs="1" name="ListaRps" type="tns:ArrayOfRps"/>
    </s:sequence>
</s:complexType>
<s:complexType name="ArrayOfRps">
    <s:sequence>
        <s:element minOccurs="0" maxOccurs="unbounded" name="Rps" nillable="true" type="tns:Rps"/>
    </s:sequence>
</s:complexType>
<s:complexType name="Rps">
    <s:complexContent mixed="false">
        <s:extension base="tns:tcRps"/>
    </s:complexContent>
</s:complexType>
<s:complexType name="tcRps">
    <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="InfRps" type="tns:tcInfRps"/>
        <s:element minOccurs="0" maxOccurs="1" name="Signature" type="s1:SignatureType"/>
    </s:sequence>
</s:complexType>

您需要在“LoteRps”内的“ListaRps”上签署每个“Rps”,最后签署“LoteRps”元素。

您可以在逐步生成 XML 时执行此操作,生成带有所有“Rps”的“ListaRps”元素,然后获取所有“InfRps”元素并签署所有这些元素,或者您可以在创建后立即签署“InfRps”并添加到“ListaRps”,最后你应该签署“LoteRps”。

也许您也可以这样做,生成完整的 XML,然后解析它获取元素并以适当的顺序一一签名。

无论如何,您都必须编写代码以根据需要对 XML 进行签名。

看看assembla nfseWsClient,它是用 Java 和 JAX-WS 开发的,但它是一个很好的起点。

这两个课程对你来说很有趣

于 2013-12-10T12:16:31.550 回答