Reputation: 1
A similar question has been asked before, but no information was provided on ho to debug.
If I am working with this code:
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
Is there any way to tell verify_EVP.verify_final to do more than just return 0 when validation fails? I have no idea on where to begin to debug.
Upvotes: 0
Views: 1091
Reputation: 1983
I have faced the problem of verifying SAML assertions cryptographically in Python, and found no good ready-made solution from m2crypto (which, as far as I know, is unmaintained, unsupported, and not Python 3 compatible) nor in other libraries. So I wrote my own library, SignXML (https://github.com/kislyuk/signxml). Here is the basic pattern of verifying a SAML assertion with it:
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)
Upvotes: 1