首页 > 解决方案 > Signxml 验证失败

问题描述

我有一个场景,我必须签署一些数据并通过 TCP 套接字将其发送给客户端。

因此,在服务器上,我使用 xmlsign 对其进行签名并将其作为字符串发送

#in server
signed_xml_obj = XMLSigner().sign(root, key=PRIVATEKEY)
xml_str = ElementTree.tostring(signed_xml_obj)
#sendtoclient(xml_str)

比在客户端中从字符串创建一个 ElementTree.Element 并尝试验证它但它失败了

#somewhere in client
signed_xml_obj = ElementTree.fromstring(signiautreString)
XMLVerifier().verify(signed_xml_obj, x509_cert=cer)

追溯

    Traceback (most recent call last):
  File "/eltioni/.local/lib/python3.7/site-packages/signxml/__init__.py", line 729, in verify
    verify(signing_cert, raw_signature, signed_info_c14n, signature_digest_method)
  File "/usr/lib/python3/dist-packages/OpenSSL/crypto.py", line 2928, in verify
    _raise_current_error()
  File "/usr/lib/python3/dist-packages/OpenSSL/_util.py", line 54, in exception_from_error_queue
    raise exception_type(errors)
OpenSSL.crypto.Error: [('rsa routines', 'int_rsa_verify', 'bad signature')]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "sig.py", line 15, in <module>
    XMLVerifier().verify(xml2, x509_cert=cert)
  File "/eltioni/.local/lib/python3.7/site-packages/signxml/__init__.py", line 735, in verify
    raise InvalidSignature("Signature verification failed: {}".format(reason))
signxml.exceptions.InvalidSignature: Signature verification failed: bad signature

我还注意到,当我在服务器中打印 signed_xml_obj 时,我得到了

print(signed_xml_obj)
#<Element Student at 0x7ff94085f188>

当我在客户端打印 signed_xml_obj 时,我得到“学生”(带引号)

print(signed_xml_obj)
#<Element 'Student' at 0x7ff94085f188>

标签: pythondigital-signaturexml-signature

解决方案


推荐阅读