Skip to content

Commit 826f4f5

Browse files
committed
Improve how fingerprint is calcultated
1 parent fad881b commit 826f4f5

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
@@ -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')

tests/src/OneLogin/saml2_tests/response_test.py

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

14321432
settings_info_3 = self.loadSettingsJSON('settings2.json')
1433-
idp_cert = settings_info_3['idp']['x509cert']
1433+
idp_cert = OneLogin_Saml2_Utils.format_cert(settings_info_3['idp']['x509cert'])
14341434
settings_info_3['idp']['certFingerprint'] = OneLogin_Saml2_Utils.calculate_x509_fingerprint(idp_cert)
14351435
settings_info_3['idp']['x509cert'] = ''
14361436
settings_3 = OneLogin_Saml2_Settings(settings_info_3)

0 commit comments

Comments
 (0)