在我尝试使用的SOAP with服务中,有必要发送一个类似于以下结构的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、皮克斯密斯和xmldsig都无法生成/重新配置URI attr of <Reference>。可能是因为他们缺乏--id-attr of xmlsec1,但我遇到的这些问题暴露了这样一个事实:我并不真正理解这个XML签名的东西,因此,我做错了事情,把事情搞砸了。请帮我理解一下。
编辑
在这些XML签名主题上,我似乎有很多人遇到了困难,但他们中没有人试图在同一个XML文件中签名两个不同的元素。这个案例在w3C场景常见问题中也没有列出,这使得一切看起来都很奇怪,因为我的webservice要求我进行多个签名。还是不去?下面是他们发布的模式:nfse/nfse.xsd#L 539 (请参阅这个元素<xsd:element name="EnviarLoteRpsEnvio">和childs)。
发布于 2013-12-10 12:16:31
我认为在第三方库中没有这样做的方法,大多数应用程序只需要所有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>您需要在"ListaRps“中的"LoteRps”中的每个"Rps“上签名,最后对"LoteRps”元素进行签名。
您可以在逐步生成XML时这样做,用所有"Rps“生成"ListaRps”元素,在获取所有"InfRps“元素并对所有元素进行签名之后,或者在创建后立即对"InfRps”签名并添加到"ListaRps",最后您应该对"LoteRps“进行签名。
也许您也可以这样做,生成完整的XML,并在解析它之后,获取元素并使用批准顺序逐一对其进行签名。
无论如何,您必须编写代码来根据需要对XML进行签名。
看看汇编语言nfseWsClient,它是用JAX和JAX开发的,但是是一个很好的初始点。
这两个类将为您安装。
https://stackoverflow.com/questions/20275224
复制相似问题