Skip to content

Commit 42a0114

Browse files
authored
Raising ConnectionError on invalid ocsp certificates - with status information (#1907)
1 parent 24cdd70 commit 42a0114

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

redis/ocsp.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,14 @@ def _check_certificate(issuer_cert, ocsp_bytes, validate=True):
5656
raise AuthorizationError("you are not authorized to view this ocsp certificate")
5757
if ocsp_response.response_status == ocsp.OCSPResponseStatus.SUCCESSFUL:
5858
if ocsp_response.certificate_status != ocsp.OCSPCertStatus.GOOD:
59-
return False
59+
raise ConnectionError(
60+
f'Received an {str(ocsp_response.certificate_status).split(".")[1]} '
61+
"ocsp certificate status"
62+
)
6063
else:
61-
return False
64+
raise ConnectionError(
65+
"failed to retrieve a sucessful response from the ocsp responder"
66+
)
6267

6368
if ocsp_response.this_update >= datetime.datetime.now():
6469
raise ConnectionError("ocsp certificate was issued in the future")

tests/test_ssl.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def test_ssl_ocsp_called_withcrypto(self, request):
107107
def test_valid_ocsp_cert_http(self):
108108
from redis.ocsp import OCSPVerifier
109109

110-
hostnames = ["github.com", "aws.amazon.com", "ynet.co.il", "microsoft.com"]
110+
hostnames = ["github.com", "aws.amazon.com", "ynet.co.il"]
111111
for hostname in hostnames:
112112
context = ssl.create_default_context()
113113
with socket.create_connection((hostname, 443)) as sock:
@@ -124,7 +124,9 @@ def test_revoked_ocsp_certificate(self):
124124
with socket.create_connection((hostname, 443)) as sock:
125125
with context.wrap_socket(sock, server_hostname=hostname) as wrapped:
126126
ocsp = OCSPVerifier(wrapped, hostname, 443)
127-
assert ocsp.is_valid() is False
127+
with pytest.raises(ConnectionError) as e:
128+
assert ocsp.is_valid()
129+
assert "REVOKED" in str(e)
128130

129131
@skip_if_nocryptography()
130132
def test_unauthorized_ocsp(self):
@@ -147,7 +149,9 @@ def test_ocsp_not_present_in_response(self):
147149
with socket.create_connection((hostname, 443)) as sock:
148150
with context.wrap_socket(sock, server_hostname=hostname) as wrapped:
149151
ocsp = OCSPVerifier(wrapped, hostname, 443)
150-
assert ocsp.is_valid() is False
152+
with pytest.raises(ConnectionError) as e:
153+
assert ocsp.is_valid()
154+
assert "from the" in str(e)
151155

152156
@skip_if_nocryptography()
153157
def test_unauthorized_then_direct(self):

0 commit comments

Comments
 (0)