Skip to content

Commit 3ed4e5c

Browse files
committed
Improve how fingerprint is calcultated
1 parent 349757d commit 3ed4e5c

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/onelogin/saml2/utils.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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(

tests/src/OneLogin/saml2_tests/response_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ def testIsValid2(self):
13321332
self.assertTrue(response_2.is_valid(self.get_request_data()))
13331333

13341334
settings_info_3 = self.loadSettingsJSON('settings2.json')
1335-
idp_cert = settings_info_3['idp']['x509cert']
1335+
idp_cert = OneLogin_Saml2_Utils.format_cert(settings_info_3['idp']['x509cert'])
13361336
settings_info_3['idp']['certFingerprint'] = OneLogin_Saml2_Utils.calculate_x509_fingerprint(idp_cert)
13371337
settings_info_3['idp']['x509cert'] = ''
13381338
settings_3 = OneLogin_Saml2_Settings(settings_info_3)

0 commit comments

Comments
 (0)