@@ -557,9 +557,9 @@ def delete_local_session(callback=None):
557557 @staticmethod
558558 def calculate_x509_fingerprint (x509_cert , alg = 'sha1' ):
559559 """
560- Calculates the fingerprint of a x509cert.
560+ Calculates the fingerprint of a formatted x509cert.
561561
562- :param x509_cert: x509 cert
562+ :param x509_cert: x509 cert formatted
563563 :type: string
564564
565565 :param alg: The algorithm to build the fingerprint
@@ -572,23 +572,27 @@ def calculate_x509_fingerprint(x509_cert, alg='sha1'):
572572
573573 lines = x509_cert .split ('\n ' )
574574 data = ''
575+ inData = False
575576
576577 for line in lines :
577578 # Remove '\r' from end of line if present.
578579 line = line .rstrip ()
579- if line == '-----BEGIN CERTIFICATE-----' :
580- # Delete junk from before the certificate.
581- data = ''
582- elif line == '-----END CERTIFICATE-----' :
583- # Ignore data after the certificate.
584- break
585- elif line == '-----BEGIN PUBLIC KEY-----' or line == '-----BEGIN RSA PRIVATE KEY-----' :
586- # This isn't an X509 certificate.
587- return None
580+ if not inData :
581+ if line == '-----BEGIN CERTIFICATE-----' :
582+ inData = True
583+ elif line == '-----BEGIN PUBLIC KEY-----' or line == '-----BEGIN RSA PRIVATE KEY-----' :
584+ # This isn't an X509 certificate.
585+ return None
588586 else :
587+ if line == '-----END CERTIFICATE-----' :
588+ break
589+
589590 # Append the current line to the certificate data.
590591 data += line
591592
593+ if not data :
594+ return None
595+
592596 decoded_data = base64 .b64decode (data )
593597
594598 if alg == 'sha512' :
@@ -1131,9 +1135,11 @@ def validate_node_sign(signature_node, elem, cert=None, fingerprint=None, finger
11311135 if len (x509_certificate_nodes ) > 0 :
11321136 x509_certificate_node = x509_certificate_nodes [0 ]
11331137 x509_cert_value = OneLogin_Saml2_Utils .element_text (x509_certificate_node )
1134- x509_fingerprint_value = OneLogin_Saml2_Utils .calculate_x509_fingerprint (x509_cert_value , fingerprintalg )
1138+ x509_cert_value_formatted = OneLogin_Saml2_Utils .format_cert (x509_cert_value )
1139+ x509_fingerprint_value = OneLogin_Saml2_Utils .calculate_x509_fingerprint (x509_cert_value_formatted , fingerprintalg )
1140+
11351141 if fingerprint == x509_fingerprint_value :
1136- cert = OneLogin_Saml2_Utils . format_cert ( x509_cert_value )
1142+ cert = x509_cert_value_formatted
11371143
11381144 # Check if Reference URI is empty
11391145 # reference_elem = OneLogin_Saml2_Utils.query(signature_node, '//ds:Reference')
0 commit comments