@@ -498,9 +498,9 @@ def delete_local_session(callback=None):
498498 @staticmethod
499499 def calculate_x509_fingerprint (x509_cert , alg = 'sha1' ):
500500 """
501- Calculates the fingerprint of a x509cert.
501+ Calculates the fingerprint of a formatted x509cert.
502502
503- :param x509_cert: x509 cert
503+ :param x509_cert: x509 cert formatted
504504 :type: string
505505
506506 :param alg: The algorithm to build the fingerprint
@@ -513,22 +513,27 @@ def calculate_x509_fingerprint(x509_cert, alg='sha1'):
513513
514514 lines = x509_cert .split ('\n ' )
515515 data = ''
516+ inData = False
516517
517518 for line in lines :
518519 # Remove '\r' from end of line if present.
519520 line = line .rstrip ()
520- if line == '-----BEGIN CERTIFICATE-----' :
521- # Delete junk from before the certificate.
522- data = ''
523- elif line == '-----END CERTIFICATE-----' :
524- # Ignore data after the certificate.
525- break
526- elif line == '-----BEGIN PUBLIC KEY-----' or line == '-----BEGIN RSA PRIVATE KEY-----' :
527- # This isn't an X509 certificate.
528- return None
521+ if not inData :
522+ if line == '-----BEGIN CERTIFICATE-----' :
523+ inData = True
524+ elif line == '-----BEGIN PUBLIC KEY-----' or line == '-----BEGIN RSA PRIVATE KEY-----' :
525+ # This isn't an X509 certificate.
526+ return None
529527 else :
528+ if line == '-----END CERTIFICATE-----' :
529+ break
530+
530531 # Append the current line to the certificate data.
531532 data += line
533+
534+ if not data :
535+ return None
536+
532537 decoded_data = base64 .b64decode (compat .to_bytes (data ))
533538
534539 if alg == 'sha512' :
@@ -932,9 +937,10 @@ def validate_node_sign(signature_node, elem, cert=None, fingerprint=None, finger
932937 if len (x509_certificate_nodes ) > 0 :
933938 x509_certificate_node = x509_certificate_nodes [0 ]
934939 x509_cert_value = OneLogin_Saml2_XML .element_text (x509_certificate_node )
935- x509_fingerprint_value = OneLogin_Saml2_Utils .calculate_x509_fingerprint (x509_cert_value , fingerprintalg )
940+ x509_cert_value_formatted = OneLogin_Saml2_Utils .format_cert (x509_cert_value )
941+ x509_fingerprint_value = OneLogin_Saml2_Utils .calculate_x509_fingerprint (x509_cert_value_formatted , fingerprintalg )
936942 if fingerprint == x509_fingerprint_value :
937- cert = OneLogin_Saml2_Utils . format_cert ( x509_cert_value )
943+ cert = x509_cert_value_formatted
938944
939945 if cert is None or cert == '' :
940946 raise OneLogin_Saml2_Error (
0 commit comments