Skip to content

Commit 0609fea

Browse files
committed
PYTHON-2132 cache OCSP responses
1 parent 47a6718 commit 0609fea

File tree

4 files changed

+271
-33
lines changed

4 files changed

+271
-33
lines changed

pymongo/ocsp_cache.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright 2020-present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Utilities for caching OCSP responses."""
16+
17+
from collections import namedtuple
18+
from datetime import datetime as _datetime
19+
from threading import Lock
20+
21+
22+
class _OCSPCache(object):
23+
"""A cache for OCSP responses."""
24+
CACHE_KEY_TYPE = namedtuple('OcspResponseCacheKey',
25+
['hash_algorithm', 'issuer_name_hash',
26+
'issuer_key_hash', 'serial_number'])
27+
28+
def __init__(self):
29+
self._data = {}
30+
# Hold this lock when accessing _data.
31+
self._lock = Lock()
32+
33+
def _get_cache_key(self, ocsp_request):
34+
return self.CACHE_KEY_TYPE(
35+
hash_algorithm=ocsp_request.hash_algorithm.name.lower(),
36+
issuer_name_hash=ocsp_request.issuer_name_hash,
37+
issuer_key_hash=ocsp_request.issuer_key_hash,
38+
serial_number=ocsp_request.serial_number)
39+
40+
def __setitem__(self, key, value):
41+
"""Add/update a cache entry.
42+
43+
'key' is of type cryptography.x509.ocsp.OCSPRequest
44+
'value' is of type cryptography.x509.ocsp.OCSPResponse
45+
46+
Validity of the OCSP response must be checked by caller.
47+
"""
48+
with self._lock:
49+
cache_key = self._get_cache_key(key)
50+
51+
# As per the OCSP protocol, if the response's nextUpdate field is
52+
# not set, the responder is indicating that newer revocation
53+
# information is available all the time.
54+
if value.next_update is None:
55+
self._data.pop(cache_key, None)
56+
return
57+
58+
# Do nothing if the response is invalid.
59+
if not (value.this_update <= _datetime.utcnow()
60+
< value.next_update):
61+
return
62+
63+
# Cache new response OR update cached response if new response
64+
# has longer validity.
65+
cached_value = self._data.get(cache_key, None)
66+
if (cached_value is None or
67+
cached_value.next_update < value.next_update):
68+
self._data[cache_key] = value
69+
70+
def __getitem__(self, item):
71+
"""Get a cache entry if it exists.
72+
73+
'item' is of type cryptography.x509.ocsp.OCSPRequest
74+
75+
Raises KeyError if the item is not in the cache.
76+
"""
77+
with self._lock:
78+
cache_key = self._get_cache_key(item)
79+
value = self._data[cache_key]
80+
81+
# Return cached response if it is still valid.
82+
if (value.this_update <= _datetime.utcnow() <
83+
value.next_update):
84+
return value
85+
86+
self._data.pop(cache_key, None)
87+
raise KeyError(cache_key)

pymongo/ocsp_support.py

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -207,37 +207,11 @@ def _verify_response_signature(issuer, response):
207207
return ret
208208

209209

210-
def _request_ocsp(cert, issuer, uri):
210+
def _build_ocsp_request(cert, issuer):
211211
# https://cryptography.io/en/latest/x509/ocsp/#creating-requests
212212
builder = _OCSPRequestBuilder()
213-
# add_certificate returns a new instance
214213
builder = builder.add_certificate(cert, issuer, _SHA1())
215-
ocsp_request = builder.build()
216-
try:
217-
response = _post(
218-
uri,
219-
data=ocsp_request.public_bytes(_Encoding.DER),
220-
headers={'Content-Type': 'application/ocsp-request'},
221-
timeout=5)
222-
except _RequestException:
223-
_LOGGER.debug("HTTP request failed")
224-
return None
225-
if response.status_code != 200:
226-
_LOGGER.debug("HTTP request returned %d", response.status_code)
227-
return None
228-
ocsp_response = _load_der_ocsp_response(response.content)
229-
_LOGGER.debug(
230-
"OCSP response status: %r", ocsp_response.response_status)
231-
if ocsp_response.response_status != _OCSPResponseStatus.SUCCESSFUL:
232-
return None
233-
# RFC6960, Section 3.2, Number 1. Only relevant if we need to
234-
# talk to the responder directly.
235-
# Accessing response.serial_number raises if response status is not
236-
# SUCCESSFUL.
237-
if ocsp_response.serial_number != ocsp_request.serial_number:
238-
_LOGGER.debug("Response serial number does not match request")
239-
return None
240-
return ocsp_response
214+
return builder.build()
241215

242216

243217
def _verify_response(issuer, response):
@@ -261,6 +235,45 @@ def _verify_response(issuer, response):
261235
return 1
262236

263237

238+
def _get_ocsp_response(cert, issuer, uri, ocsp_response_cache):
239+
ocsp_request = _build_ocsp_request(cert, issuer)
240+
try:
241+
ocsp_response = ocsp_response_cache[ocsp_request]
242+
_LOGGER.debug("Using cached OCSP response.")
243+
except KeyError:
244+
try:
245+
response = _post(
246+
uri,
247+
data=ocsp_request.public_bytes(_Encoding.DER),
248+
headers={'Content-Type': 'application/ocsp-request'},
249+
timeout=5)
250+
except _RequestException:
251+
_LOGGER.debug("HTTP request failed")
252+
return None
253+
if response.status_code != 200:
254+
_LOGGER.debug("HTTP request returned %d", response.status_code)
255+
return None
256+
ocsp_response = _load_der_ocsp_response(response.content)
257+
_LOGGER.debug(
258+
"OCSP response status: %r", ocsp_response.response_status)
259+
if ocsp_response.response_status != _OCSPResponseStatus.SUCCESSFUL:
260+
return None
261+
# RFC6960, Section 3.2, Number 1. Only relevant if we need to
262+
# talk to the responder directly.
263+
# Accessing response.serial_number raises if response status is not
264+
# SUCCESSFUL.
265+
if ocsp_response.serial_number != ocsp_request.serial_number:
266+
_LOGGER.debug("Response serial number does not match request")
267+
return None
268+
if not _verify_response(issuer, ocsp_response):
269+
# The response failed verification.
270+
return None
271+
_LOGGER.debug("Caching OCSP response.")
272+
ocsp_response_cache[ocsp_request] = ocsp_response
273+
274+
return ocsp_response
275+
276+
264277
def _ocsp_callback(conn, ocsp_bytes, user_data):
265278
"""Callback for use with OpenSSL.SSL.Context.set_ocsp_client_callback."""
266279
cert = conn.get_peer_certificate()
@@ -283,6 +296,8 @@ def _ocsp_callback(conn, ocsp_bytes, user_data):
283296
_LOGGER.debug("Peer presented a must-staple cert")
284297
must_staple = True
285298
break
299+
ocsp_response_cache = user_data.ocsp_response_cache
300+
286301
# No stapled OCSP response
287302
if ocsp_bytes == b'':
288303
_LOGGER.debug("Peer did not staple an OCSP response")
@@ -314,13 +329,12 @@ def _ocsp_callback(conn, ocsp_bytes, user_data):
314329
# successful, valid responses with a certificate status of REVOKED.
315330
for uri in uris:
316331
_LOGGER.debug("Trying %s", uri)
317-
response = _request_ocsp(cert, issuer, uri)
332+
response = _get_ocsp_response(
333+
cert, issuer, uri, ocsp_response_cache)
318334
if response is None:
319335
# The endpoint didn't respond in time, or the response was
320-
# unsuccessful or didn't match the request.
321-
continue
322-
if not _verify_response(issuer, response):
323-
# The response failed verification.
336+
# unsuccessful or didn't match the request, or the response
337+
# failed verification.
324338
continue
325339
_LOGGER.debug("OCSP cert status: %r", response.certificate_status)
326340
if response.certificate_status == _OCSPCertStatus.GOOD:
@@ -344,6 +358,8 @@ def _ocsp_callback(conn, ocsp_bytes, user_data):
344358
return 0
345359
if not _verify_response(issuer, response):
346360
return 0
361+
# Cache the verified, stapled response.
362+
ocsp_response_cache[_build_ocsp_request(cert, issuer)] = response
347363
_LOGGER.debug("OCSP cert status: %r", response.certificate_status)
348364
if response.certificate_status == _OCSPCertStatus.REVOKED:
349365
return 0

pymongo/pyopenssl_context.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from pymongo.ocsp_support import (
4141
_load_trusted_ca_certs,
4242
_ocsp_callback)
43+
from pymongo.ocsp_cache import _OCSPCache
4344
from pymongo.socket_checker import (
4445
_errno_from_exception, SocketChecker as _SocketChecker)
4546

@@ -142,6 +143,7 @@ class _CallbackData(object):
142143
def __init__(self):
143144
self.trusted_ca_certs = None
144145
self.check_ocsp_endpoint = None
146+
self.ocsp_response_cache = _OCSPCache()
145147

146148

147149
class SSLContext(object):

test/test_ocsp_cache.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Copyright 2020-present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Test the pymongo ocsp_support module."""
16+
17+
from collections import namedtuple
18+
from datetime import datetime, timedelta
19+
from os import urandom
20+
import random
21+
import sys
22+
from time import sleep
23+
24+
sys.path[0:0] = [""]
25+
26+
from pymongo.ocsp_cache import _OCSPCache
27+
from test import unittest
28+
29+
30+
class TestOcspCache(unittest.TestCase):
31+
@classmethod
32+
def setUpClass(cls):
33+
cls.MockHashAlgorithm = namedtuple(
34+
"MockHashAlgorithm", ['name'])
35+
cls.MockOcspRequest = namedtuple(
36+
"MockOcspRequest", ['hash_algorithm', 'issuer_name_hash',
37+
'issuer_key_hash', 'serial_number'])
38+
cls.MockOcspResponse = namedtuple(
39+
"MockOcspResponse", ["this_update", "next_update"])
40+
41+
def setUp(self):
42+
self.cache = _OCSPCache()
43+
44+
def _create_mock_request(self):
45+
hash_algorithm = self.MockHashAlgorithm(
46+
random.choice(['sha1', 'md5', 'sha256']))
47+
issuer_name_hash = urandom(8)
48+
issuer_key_hash = urandom(8)
49+
serial_number = random.randint(0, 10**10)
50+
return self.MockOcspRequest(
51+
hash_algorithm=hash_algorithm,
52+
issuer_name_hash=issuer_name_hash,
53+
issuer_key_hash=issuer_key_hash,
54+
serial_number=serial_number)
55+
56+
def _create_mock_response(self, this_update_delta_seconds,
57+
next_update_delta_seconds):
58+
now = datetime.utcnow()
59+
this_update = now + timedelta(seconds=this_update_delta_seconds)
60+
if next_update_delta_seconds is not None:
61+
next_update = now + timedelta(seconds=next_update_delta_seconds)
62+
else:
63+
next_update = None
64+
return self.MockOcspResponse(
65+
this_update=this_update,
66+
next_update=next_update)
67+
68+
def _add_mock_cache_entry(self, mock_request, mock_response):
69+
key = self.cache._get_cache_key(mock_request)
70+
self.cache._data[key] = mock_response
71+
72+
def test_simple(self):
73+
# Start with 1 valid entry in the cache.
74+
request = self._create_mock_request()
75+
response = self._create_mock_response(-10, +3600)
76+
self._add_mock_cache_entry(request, response)
77+
78+
# Ensure entry can be retrieved.
79+
self.assertEqual(self.cache[request], response)
80+
81+
# Valid entries with an earlier next_update have no effect.
82+
response_1 = self._create_mock_response(-20, +1800)
83+
self.cache[request] = response_1
84+
self.assertEqual(self.cache[request], response)
85+
86+
# Invalid entries with a later this_update have no effect.
87+
response_2 = self._create_mock_response(+20, +1800)
88+
self.cache[request] = response_2
89+
self.assertEqual(self.cache[request], response)
90+
91+
# Invalid entries with passed next_update have no effect.
92+
response_3 = self._create_mock_response(-10, -5)
93+
self.cache[request] = response_3
94+
self.assertEqual(self.cache[request], response)
95+
96+
# Valid entries with a later next_update update the cache.
97+
response_new = self._create_mock_response(-5, +7200)
98+
self.cache[request] = response_new
99+
self.assertEqual(self.cache[request], response_new)
100+
101+
# Entries with an unset next_update purge the cache.
102+
response_notset = self._create_mock_response(-5, None)
103+
self.cache[request] = response_notset
104+
with self.assertRaises(KeyError):
105+
_ = self.cache[request]
106+
107+
def test_invalidate(self):
108+
# Start with 1 valid entry in the cache.
109+
request = self._create_mock_request()
110+
response = self._create_mock_response(-10, +0.25)
111+
self._add_mock_cache_entry(request, response)
112+
113+
# Ensure entry can be retrieved.
114+
self.assertEqual(self.cache[request], response)
115+
116+
# Wait for entry to become invalid and ensure KeyError is raised.
117+
sleep(0.5)
118+
with self.assertRaises(KeyError):
119+
_ = self.cache[request]
120+
121+
def test_non_existent(self):
122+
# Start with 1 valid entry in the cache.
123+
request = self._create_mock_request()
124+
response = self._create_mock_response(-10, +10)
125+
self._add_mock_cache_entry(request, response)
126+
127+
# Attempt to retrieve non-existent entry must raise KeyError.
128+
with self.assertRaises(KeyError):
129+
_ = self.cache[self._create_mock_request()]
130+
131+
132+
if __name__ == "__main__":
133+
unittest.main()

0 commit comments

Comments
 (0)