调试Python SAML断言验证

2024-05-29 02:22:59 发布

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

以前也有人问过类似的问题,但没有提供有关ho的调试信息。在

如果我使用此代码:

from lxml import etree
import base64
from M2Crypto import EVP, RSA, X509

decoded_assertion = base64.b64decode(assertion)

root = etree.XML(decoded_assertion)
signature_node = root.find('{http://www.w3.org/2000/09/xmldsig#}Signature')
signature_value = signature_node.find('{http://www.w3.org/2000/09/xmldsig#}SignatureValue').text
signed_info = signature_node.find('{http://www.w3.org/2000/09/xmldsig#}SignedInfo')
signed_info_string_c14n = etree.tostring(signed_info,method="c14n")

certificate_node = root.find('{http://www.w3.org/2000/09/xmldsig#}Signature')\
        .find('{http://www.w3.org/2000/09/xmldsig#}KeyInfo')\
        .find('{http://www.w3.org/2000/09/xmldsig#}X509Data')\
        .find('{http://www.w3.org/2000/09/xmldsig#}X509Certificate')

x509 = X509.load_cert_string(base64.decodestring(certificate_node.text), X509.FORMAT_DER)
pubkey = x509.get_pubkey().get_rsa()

verify_EVP = EVP.PKey()
verify_EVP.assign_rsa(pubkey)
verify_EVP.reset_context(md='sha256')
verify_EVP.verify_init()
verify_EVP.verify_update(signed_info_string_c14n)
result = verify_EVP.verify_final(signature_value.decode('base64'))

print result

有什么方法可以告诉验证吗_最终验证副总裁在验证失败时不仅仅返回0?我不知道从哪里开始调试。在


Tags: orgimportinfonodehttpwwwfindetree
1条回答
网友
1楼 · 发布于 2024-05-29 02:22:59

我曾经遇到过在Python中以加密方式验证SAML断言的问题,但是从m2crypto(据我所知,它是未维护的、不受支持的、不兼容python3)和其他库中都找不到现成的解决方案。所以我编写了自己的库SignXML(https://github.com/kislyuk/signxml)。下面是用它验证SAML断言的基本模式:

from lxml import etree
from base64 import b64decode
from signxml import xmldsig

with open("metadata.xml", "rb") as fh:
    cert = etree.parse(fh).find("//ds:X509Certificate").text

assertion_data = xmldsig(b64decode(assertion_body)).verify(x509_cert=cert)

相关问题 更多 >

    热门问题