1212import base64
1313from datetime import datetime
1414import calendar
15- from hashlib import sha1
15+ from hashlib import sha1 , sha256 , sha384 , sha512
1616from isodate import parse_duration as duration_parser
1717from lxml import etree
1818from defusedxml .lxml import tostring , fromstring
@@ -522,14 +522,17 @@ def delete_local_session(callback=None):
522522 callback ()
523523
524524 @staticmethod
525- def calculate_x509_fingerprint (x509_cert ):
525+ def calculate_x509_fingerprint (x509_cert , alg = 'sha1' ):
526526 """
527527 Calculates the fingerprint of a x509cert.
528528
529529 :param x509_cert: x509 cert
530530 :type: string
531531
532- :returns: Formated fingerprint
532+ :param alg: The algorithm to build the fingerprint
533+ :type: string
534+
535+ :returns: fingerprint
533536 :rtype: string
534537 """
535538 assert isinstance (x509_cert , basestring )
@@ -552,9 +555,19 @@ def calculate_x509_fingerprint(x509_cert):
552555 else :
553556 # Append the current line to the certificate data.
554557 data += line
555- # "data" now contains the certificate as a base64-encoded string. The
556- # fingerprint of the certificate is the sha1-hash of the certificate.
557- return sha1 (base64 .b64decode (data )).hexdigest ().lower ()
558+
559+ decoded_data = base64 .b64decode (data )
560+
561+ if alg == 'sha512' :
562+ fingerprint = sha512 (decoded_data )
563+ elif alg == 'sha384' :
564+ fingerprint = sha384 (decoded_data )
565+ elif alg == 'sha256' :
566+ fingerprint = sha256 (decoded_data )
567+ else :
568+ fingerprint = sha1 (decoded_data )
569+
570+ return fingerprint .hexdigest ().lower ()
558571
559572 @staticmethod
560573 def format_finger_print (fingerprint ):
@@ -837,7 +850,7 @@ def add_sign(xml, key, cert, debug=False):
837850 return newdoc .saveXML (newdoc .firstChild )
838851
839852 @staticmethod
840- def validate_sign (xml , cert = None , fingerprint = None , validatecert = False , debug = False ):
853+ def validate_sign (xml , cert = None , fingerprint = None , fingerprintalg = 'sha1' , validatecert = False , debug = False ):
841854 """
842855 Validates a signature (Message or Assertion).
843856
@@ -850,6 +863,9 @@ def validate_sign(xml, cert=None, fingerprint=None, validatecert=False, debug=Fa
850863 :param fingerprint: The fingerprint of the public cert
851864 :type: string
852865
866+ :param fingerprintalg: The algorithm used to build the fingerprint
867+ :type: string
868+
853869 :param validatecert: If true, will verify the signature and if the cert is valid.
854870 :type: bool
855871
@@ -899,7 +915,7 @@ def validate_sign(xml, cert=None, fingerprint=None, validatecert=False, debug=Fa
899915 if len (x509_certificate_nodes ) > 0 :
900916 x509_certificate_node = x509_certificate_nodes [0 ]
901917 x509_cert_value = x509_certificate_node .text
902- x509_fingerprint_value = OneLogin_Saml2_Utils .calculate_x509_fingerprint (x509_cert_value )
918+ x509_fingerprint_value = OneLogin_Saml2_Utils .calculate_x509_fingerprint (x509_cert_value , fingerprintalg )
903919 if fingerprint == x509_fingerprint_value :
904920 cert = OneLogin_Saml2_Utils .format_cert (x509_cert_value )
905921
0 commit comments