无法使用命名引用URI对xmlsec进行签名

2024-05-15 03:26:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用pythonxmlsec对模板中的xml文档进行签名。如果URI=“,它可以正常工作。但是,如果URI不为空,则出现错误“签名失败”

python 3.6版 来自https://github.com/mehcode/python-xmlsec的xmlsec

import xmlsec

xml_in='''<Envelope xmlns="urn:envelope">
  <Data ID="1234">
    Hello, World!
  </Data>
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
      <Reference URI="#1234">
        <Transforms>
          <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
        <DigestValue></DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue/>
    <KeyInfo>
    <KeyName/>
    </KeyInfo>
  </Signature>
</Envelope>'''

template=etree.ElementTree(etree.fromstring(xml_in)).getroot()

signature_node = xmlsec.tree.find_node(template, xmlsec.constants.NodeSignature)

ctx = xmlsec.SignatureContext()

ctx.key = xmlsec.Key.from_file('c:/certificates/ercot.key', xmlsec.constants.KeyDataFormatPem)

ctx.sign(signature_node)

print(etree.tostring(template).decode())


ERROR below:

Traceback (most recent call last):

  File "<ipython-input-243-4de30b62be4f>", line 5, in <module>
    ctx.sign(signature_node)

Error: (1, 'failed to sign')```

Tags: inorgnodehttpwwwtemplatexmluri

热门问题