调试Python SAML断言验证

时间:2014-12-11 15:13:48

标签: python xml authentication saml m2crypto

之前已经提出过类似的问题,但没有提供有关调试的信息。

如果我正在使用此代码:

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?我不知道从哪里开始调试。

1 个答案:

答案 0 :(得分: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)
相关问题