Skip to content

PYTHON-5740 - Fix weak OCSP hashing algorithm#2748

Open
NoahStapp wants to merge 3 commits intomongodb:masterfrom
NoahStapp:PYTHON-5740
Open

PYTHON-5740 - Fix weak OCSP hashing algorithm#2748
NoahStapp wants to merge 3 commits intomongodb:masterfrom
NoahStapp:PYTHON-5740

Conversation

@NoahStapp
Copy link
Copy Markdown
Contributor

@NoahStapp NoahStapp commented Apr 8, 2026

PYTHON-5740

Changes in this PR

Use SHA-256 instead of SHA1 to align with modern practices for OCSP authentication.

Test Plan

Use existing tests. Requires mongodb-labs/drivers-evergreen-tools#762 to be merged first for test server compatibility.

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)?
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add link(s).

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

Copilot AI review requested due to automatic review settings April 8, 2026 15:34
@NoahStapp NoahStapp requested a review from a team as a code owner April 8, 2026 15:34
@NoahStapp NoahStapp requested a review from aclark4life April 8, 2026 15:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to address weak hashing in OCSP handling by switching from SHA-1 to SHA-256.

Changes:

  • Updated OCSP-related hashing in pymongo/ocsp_support.py to use SHA-256.
  • Updated the changelog to document the hashing change.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
pymongo/ocsp_support.py Swaps OCSP hashing from SHA-1 to SHA-256 in responder key hashing and OCSP request construction.
doc/changelog.rst Adds a release note describing the OCSP hashing change.

Comment on lines 166 to +170
elif isinstance(public_key, _EllipticCurvePublicKey):
pbytes = public_key.public_bytes(_Encoding.X962, _PublicFormat.UncompressedPoint)
else:
pbytes = public_key.public_bytes(_Encoding.DER, _PublicFormat.SubjectPublicKeyInfo)
digest = _Hash(_SHA1(), backend=_default_backend()) # noqa: S303
digest = _Hash(_SHA256(), backend=_default_backend())
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_public_key_hash now computes a SHA-256 digest, but the OCSP ResponderID byKey KeyHash is defined as a SHA-1 hash in RFC2560/RFC6960. response.responder_key_hash from cryptography follows the RFC, so switching this to SHA-256 will prevent _get_certs_by_key_hash from ever finding a matching responder cert (and can cause valid delegated OCSP responses to be rejected). Keep SHA-1 for the responder KeyHash computation (and update the comment accordingly), even if the OCSP CertID/request hashing moves to SHA-256.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NoahStapp Good catch copilot ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to verify more, the robots have given different answers on each ask.

Comment on lines 249 to 253
def _build_ocsp_request(cert: Certificate, issuer: Certificate) -> OCSPRequest:
# https://cryptography.io/en/latest/x509/ocsp/#creating-requests
builder = _OCSPRequestBuilder()
builder = builder.add_certificate(cert, issuer, _SHA1()) # noqa: S303
builder = builder.add_certificate(cert, issuer, _SHA256())
return builder.build()
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the OCSP request CertID hash algorithm to SHA-256 updates response.issuer_key_hash (it is computed using the request's hash algorithm). The response ResponderID responder_key_hash is not derived from the CertID hash algorithm, so the existing responder-is-issuer shortcut (rkey_hash == ikey_hash) can stop working and may incorrectly treat an issuer responder as a delegate, causing verification failures when no responder cert is embedded. Update the issuer-responder detection logic to compare like-for-like (e.g., compare responder_key_hash against a SHA-1 KeyHash of the issuer cert per RFC, or rely on responder_name when present) rather than comparing to issuer_key_hash.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.66%. Comparing base (db4db92) to head (0f3a61a).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2748      +/-   ##
==========================================
+ Coverage   87.62%   87.66%   +0.04%     
==========================================
  Files         141      141              
  Lines       24137    24137              
  Branches     4122     4122              
==========================================
+ Hits        21149    21159      +10     
+ Misses       2097     2093       -4     
+ Partials      891      885       -6     
Flag Coverage Δ
auth-aws-rhel8-test-auth-aws-rapid-web-identity-python3.14-cov 35.09% <ø> (ø)
auth-aws-win64-test-auth-aws-rapid-web-identity-python3.14-cov 35.09% <ø> (-0.02%) ⬇️
auth-enterprise-macos-test-standard-auth-latest-python3.11-auth-ssl-sharded-cluster-cov 43.73% <ø> (ø)
auth-enterprise-rhel8-test-standard-auth-latest-python3.11-auth-ssl-sharded-cluster-cov 43.74% <ø> (-0.01%) ⬇️
auth-enterprise-win64-test-standard-auth-latest-python3.11-auth-ssl-sharded-cluster-cov 43.78% <ø> (+<0.01%) ⬆️
auth-oidc-local-ubuntu-22-test-auth-oidc-default 48.72% <ø> (+0.01%) ⬆️
compression-snappy-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 55.23% <ø> (+<0.01%) ⬆️
compression-snappy-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 57.14% <ø> (ø)
compression-snappy-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 56.55% <ø> (-0.02%) ⬇️
compression-snappy-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 54.86% <ø> (-0.01%) ⬇️
compression-zlib-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 55.23% <ø> (+<0.01%) ⬆️
compression-zlib-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 57.14% <ø> (ø)
compression-zlib-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 56.55% <ø> (-0.02%) ⬇️
compression-zlib-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 54.87% <ø> (-0.01%) ⬇️
compression-zstd-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 55.24% <ø> (+<0.01%) ⬆️
compression-zstd-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 57.14% <ø> (ø)
compression-zstd-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 56.55% <ø> (-0.02%) ⬇️
compression-zstd-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 54.87% <ø> (+<0.01%) ⬆️
compression-zstd-ubuntu-22-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 54.85% <ø> (ø)
coverage-report-coverage-report 87.63% <100.00%> (+6.91%) ⬆️
disable-test-commands-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 55.22% <ø> (-0.01%) ⬇️
disable-test-commands-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 57.14% <ø> (ø)
disable-test-commands-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 56.55% <ø> (-0.02%) ⬇️
disable-test-commands-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 54.87% <ø> (ø)
encryption-crypt_shared-macos-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.82% <ø> (ø)
encryption-crypt_shared-macos-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.75% <ø> (ø)
encryption-crypt_shared-macos-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.54% <ø> (ø)
encryption-crypt_shared-rhel8-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.83% <ø> (+<0.01%) ⬆️
encryption-crypt_shared-rhel8-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.62% <ø> (-0.01%) ⬇️
encryption-crypt_shared-rhel8-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.57% <ø> (+0.03%) ⬆️
encryption-crypt_shared-win64-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.71% <ø> (-0.06%) ⬇️
encryption-crypt_shared-win64-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.75% <ø> (+0.09%) ⬆️
encryption-crypt_shared-win64-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.57% <ø> (-0.03%) ⬇️
encryption-macos-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.83% <ø> (+<0.01%) ⬆️
encryption-macos-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.75% <ø> (+0.02%) ⬆️
encryption-macos-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.56% <ø> (+0.02%) ⬆️
encryption-pyopenssl-rhel8-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 53.54% <100.00%> (+0.04%) ⬆️
encryption-pyopenssl-rhel8-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 55.36% <100.00%> (+0.05%) ⬆️
encryption-pyopenssl-rhel8-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 55.26% <100.00%> (ø)
encryption-rhel8-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.83% <ø> (+<0.01%) ⬆️
encryption-rhel8-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.62% <ø> (ø)
encryption-rhel8-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.53% <ø> (-0.02%) ⬇️
encryption-win64-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.71% <ø> (-0.07%) ⬇️
encryption-win64-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.66% <ø> (-0.06%) ⬇️
encryption-win64-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.65% <ø> (+0.05%) ⬆️
load-balancer-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 48.39% <ø> (+0.05%) ⬆️
mongodb-latest-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 56.96% <ø> (-0.02%) ⬇️
mongodb-latest-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 55.24% <ø> (-0.02%) ⬇️
mongodb-latest-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 59.13% <ø> (ø)
mongodb-latest-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 59.28% <ø> (ø)
mongodb-latest-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 57.09% <ø> (ø)
mongodb-rapid-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 56.97% <ø> (ø)
mongodb-rapid-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 55.26% <ø> (+<0.01%) ⬆️
mongodb-rapid-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 59.13% <ø> (ø)
mongodb-rapid-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 59.28% <ø> (ø)
mongodb-rapid-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 57.08% <ø> (-0.01%) ⬇️
mongodb-v4.2-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 54.66% <ø> (+0.01%) ⬆️
mongodb-v4.2-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 53.20% <ø> (-0.03%) ⬇️
mongodb-v4.2-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 56.83% <ø> (ø)
mongodb-v4.2-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 56.95% <ø> (ø)
mongodb-v4.2-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 54.81% <ø> (+<0.01%) ⬆️
mongodb-v4.4-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 55.06% <ø> (-0.02%) ⬇️
mongodb-v4.4-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 53.49% <ø> (+<0.01%) ⬆️
mongodb-v4.4-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.24% <ø> (ø)
mongodb-v4.4-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.32% <ø> (ø)
mongodb-v4.4-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 55.11% <ø> (+0.01%) ⬆️
mongodb-v5.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 55.24% <ø> (-0.01%) ⬇️
mongodb-v5.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 53.64% <ø> (+<0.01%) ⬆️
mongodb-v5.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.43% <ø> (ø)
mongodb-v5.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.56% <ø> (ø)
mongodb-v5.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 55.35% <ø> (-0.01%) ⬇️
mongodb-v6.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 55.27% <ø> (ø)
mongodb-v6.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 53.65% <ø> (+<0.01%) ⬆️
mongodb-v6.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.45% <ø> (ø)
mongodb-v6.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.60% <ø> (ø)
mongodb-v6.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 55.39% <ø> (ø)
mongodb-v7.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 55.28% <ø> (+<0.01%) ⬆️
mongodb-v7.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 53.64% <ø> (ø)
mongodb-v7.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.46% <ø> (ø)
mongodb-v7.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.60% <ø> (ø)
mongodb-v7.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 55.39% <ø> (+0.01%) ⬆️
mongodb-v8.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 56.97% <ø> (-0.01%) ⬇️
mongodb-v8.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 55.25% <ø> (-0.01%) ⬇️
mongodb-v8.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 59.13% <ø> (ø)
mongodb-v8.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 59.28% <ø> (ø)
mongodb-v8.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 57.09% <ø> (+<0.01%) ⬆️
no-c-ext-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 56.44% <ø> (+<0.01%) ⬆️
no-c-ext-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 58.36% <ø> (ø)
no-c-ext-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 57.76% <ø> (-0.02%) ⬇️
no-c-ext-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 56.08% <ø> (ø)
ocsp-rhel8-test-ocsp-ecdsa-valid-cert-server-staples-latest-python3.14-cov 34.06% <66.66%> (-0.02%) ⬇️
ocsp-rhel8-test-ocsp-rsa-valid-cert-server-staples-latest-python3.14-cov 34.06% <66.66%> (?)
pyopenssl-macos-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 57.05% <33.33%> (ø)
pyopenssl-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 57.05% <33.33%> (ø)
pyopenssl-win64-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 57.00% <33.33%> (ø)
stable-api-accept-v2-rhel8-auth-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 55.23% <ø> (+<0.01%) ⬆️
stable-api-accept-v2-rhel8-auth-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 54.87% <ø> (ø)
stable-api-require-v1-rhel8-auth-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 55.21% <ø> (-0.01%) ⬇️
stable-api-require-v1-rhel8-auth-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 56.42% <ø> (-0.03%) ⬇️
stable-api-require-v1-rhel8-auth-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 54.85% <ø> (+<0.01%) ⬆️
storage-inmemory-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 55.22% <ø> (-0.01%) ⬇️
storage-inmemory-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 54.87% <ø> (-0.01%) ⬇️
test-macos-arm64-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 55.20% <ø> (ø)
test-macos-arm64-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 57.14% <ø> (ø)
test-macos-arm64-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 56.55% <ø> (-0.02%) ⬇️
test-macos-arm64-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 54.83% <ø> (+<0.01%) ⬆️
test-macos-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 55.21% <ø> (-0.01%) ⬇️
test-macos-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 57.14% <ø> (ø)
test-macos-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 56.56% <ø> (+0.01%) ⬆️
test-macos-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 54.83% <ø> (-0.01%) ⬇️
test-numpy-macos-arm64-test-numpy-python3.14-python3.14-cov 32.27% <ø> (ø)
test-numpy-macos-test-numpy-python3.14-python3.14-cov 32.27% <ø> (-0.02%) ⬇️
test-numpy-rhel8-test-numpy-python3.14-python3.14-cov 32.27% <ø> (ø)
test-numpy-win32-test-numpy-python3.14-python3.14-cov 32.23% <ø> (-0.02%) ⬇️
test-numpy-win64-test-numpy-python3.14-python3.14-cov 32.23% <ø> (ø)
test-win32-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 55.07% <ø> (+0.01%) ⬆️
test-win32-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 57.09% <ø> (+<0.01%) ⬆️
test-win32-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 56.48% <ø> (-0.01%) ⬇️
test-win32-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 54.70% <ø> (-0.02%) ⬇️
test-win64-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 55.06% <ø> (ø)
test-win64-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 57.09% <ø> (ø)
test-win64-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 56.47% <ø> (-0.02%) ⬇️
test-win64-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 54.71% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants