Skip to content

Commit cbae04f

Browse files
committed
PYTHON-2147 Use verified peer cert chain in OCSP when available (#877)
(cherry picked from commit 7a8f6b3)
1 parent 22e84f0 commit cbae04f

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

pymongo/ocsp_support.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,18 @@ def _ocsp_callback(conn, ocsp_bytes, user_data):
275275
_LOGGER.debug("No peer cert?")
276276
return 0
277277
cert = cert.to_cryptography()
278-
chain = conn.get_peer_cert_chain()
278+
# Use the verified chain when available (pyopenssl>=20.0).
279+
if hasattr(conn, "get_verified_chain"):
280+
chain = conn.get_verified_chain()
281+
trusted_ca_certs = None
282+
else:
283+
chain = conn.get_peer_cert_chain()
284+
trusted_ca_certs = user_data.trusted_ca_certs
279285
if not chain:
280286
_LOGGER.debug("No peer cert chain?")
281287
return 0
282288
chain = [cer.to_cryptography() for cer in chain]
283-
issuer = _get_issuer_cert(cert, chain, user_data.trusted_ca_certs)
289+
issuer = _get_issuer_cert(cert, chain, trusted_ca_certs)
284290
must_staple = False
285291
# https://tools.ietf.org/html/rfc7633#section-4.2.3.1
286292
ext = _get_extension(cert, _TLSFeature)

pymongo/pyopenssl_context.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ def load_verify_locations(self, cafile=None, capath=None):
274274
ssl.CERT_NONE.
275275
"""
276276
self._ctx.load_verify_locations(cafile, capath)
277-
self._callback_data.trusted_ca_certs = _load_trusted_ca_certs(cafile)
277+
# Manually load the CA certs when get_verified_chain is not available (pyopenssl<20).
278+
if not hasattr(_SSL.Connection, "get_verified_chain"):
279+
self._callback_data.trusted_ca_certs = _load_trusted_ca_certs(cafile)
278280

279281
def _load_certifi(self):
280282
"""Attempt to load CA certs from certifi."""

0 commit comments

Comments
 (0)