【问题标题】:Debugging Python SAML assertion validation调试 Python SAML 断言验证
【发布时间】:2014-12-11 15:13:48
【问题描述】:

之前有人问过类似的问题,但没有提供关于 ho to debug 的信息。

如果我正在使用此代码:

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

有没有办法告诉 verify_EVP.verify_final 在验证失败时做更多的事情而不是仅仅返回 0?我不知道从哪里开始调试。

【问题讨论】:

    标签: python xml authentication saml m2crypto


    【解决方案1】:

    我遇到了在 Python 中以加密方式验证 SAML 断言的问题,并且在 m2crypto(据我所知,它未维护、不受支持且与 Python 3 不兼容)或其他库中都没有找到好的现成解决方案.所以我编写了自己的库 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 2017-07-23
      • 2015-06-29
      相关资源
      最近更新 更多