-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-127298: When in FIPS mode ensure builtin hashes check for usedforsecurity=False #127301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xnox
wants to merge
12
commits into
python:main
Choose a base branch
from
xnox:hashlib-builtin-fips-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+152
−19
Open
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
28d8035
gh-127298: When in FIPS mode ensure builtin hashes check for usedfors…
xnox c6375c1
use != 0 rather than == 1 and update a comment.
gpshead b3aa6cb
Update the NEWS to reflect reality.
gpshead 8ec61b5
address _some_ correctness details in the test.
gpshead 56f0f7a
avoid repeated calls, reword comment.
gpshead f8c5133
Update 2024-11-26-16-31-40.gh-issue-127298.jqYJvn.rst
gpshead 0dbef0d
avoid rerunning test in refleak hunting mode.
gpshead 6e2aa85
Change to spawn _test_hashlib_fips from test_hashlib with an env.
gpshead dc964b9
Use subTest to id the algorithm.
gpshead b5a846e
lint
gpshead c22d643
remove the no longer appropriate mention from ssltests.
gpshead 905ba7f
fix subTest context manager nesting.
gpshead File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Activate base provider only, with default properties fips=yes. It | ||
# means that fips mode is on, and no digest implementations are | ||
# available. Perfect for mock testing builtin FIPS wrappers. | ||
|
||
config_diagnostics = 1 | ||
openssl_conf = openssl_init | ||
|
||
[openssl_init] | ||
providers = provider_sect | ||
alg_section = algorithm_sect | ||
|
||
[provider_sect] | ||
base = base_sect | ||
|
||
[base_sect] | ||
activate = 1 | ||
|
||
[algorithm_sect] | ||
default_properties = fips=yes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Test the hashlib module usedforsecurity wrappers under fips. | ||
# | ||
# Copyright (C) 2024 Dimitri John Ledkov ([email protected]) | ||
# Licensed to PSF under a Contributor Agreement. | ||
# | ||
|
||
import os | ||
import sys | ||
import unittest | ||
|
||
OPENSSL_CONF_BACKUP = os.environ.get("OPENSSL_CONF") | ||
|
||
|
||
class HashLibFIPSTestCase(unittest.TestCase): | ||
_executions = 0 # prevent re-running on in refleak hunting mode, etc. | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
if cls._executions > 0: | ||
raise unittest.SkipTest("Cannot run this test within the same Python process.") | ||
if sys.modules.get("_hashlib") or sys.modules.get("_ssl"): | ||
raise AssertionError("_hashlib or _ssl already imported, too late to change OPENSSL_CONF.") | ||
# This openssl.cnf mocks FIPS mode without any digest | ||
# loaded. It means all digests must raise ValueError when | ||
# usedforsecurity=True via either openssl or builtin | ||
# constructors | ||
OPENSSL_CONF = os.path.join(os.path.dirname(__file__), "hashlibdata", "openssl.cnf") | ||
os.environ["OPENSSL_CONF"] = OPENSSL_CONF | ||
# Ensure hashlib is loading a fresh libcrypto with openssl | ||
# context affected by the above config file. Check if this can | ||
# be folded into test_hashlib.py, specifically if | ||
# import_fresh_module() results in a fresh library context | ||
import hashlib | ||
|
||
def setUp(self): | ||
try: | ||
from _hashlib import get_fips_mode | ||
except ImportError: | ||
self.skipTest('_hashlib not available') | ||
|
||
if get_fips_mode() != 1: | ||
self.skipTest('mocking fips mode failed') | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
if OPENSSL_CONF_BACKUP is not None: | ||
os.environ["OPENSSL_CONF"] = OPENSSL_CONF_BACKUP | ||
else: | ||
os.environ.pop("OPENSSL_CONF", None) | ||
cls._executions += 1 | ||
|
||
def test_algorithms_available(self): | ||
import hashlib | ||
self.assertTrue(set(hashlib.algorithms_guaranteed). | ||
issubset(hashlib.algorithms_available)) | ||
# all available algorithms must be loadable, bpo-47101 | ||
self.assertNotIn("undefined", hashlib.algorithms_available) | ||
for name in hashlib.algorithms_available: | ||
digest = hashlib.new(name, usedforsecurity=False) | ||
|
||
def test_usedforsecurity_true(self): | ||
import hashlib | ||
for name in hashlib.algorithms_available: | ||
with self.assertRaises(ValueError): | ||
digest = hashlib.new(name, usedforsecurity=True) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
Misc/NEWS.d/next/Library/2024-11-26-16-31-40.gh-issue-127298.jqYJvn.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
:mod:`hashlib`'s builtin hash implementations now check ``usedforsecurity=False``, | ||
when the OpenSSL library default provider is in OpenSSL's FIPS mode. This helps | ||
ensure that only US FIPS approved implementations are in use by default on systems | ||
configured as such. | ||
|
||
This is only active when :mod:`hashlib` has been built with OpenSSL implementation | ||
support and said OpenSSL library includes the FIPS mode feature. Not all variants | ||
do, and OpenSSL is not a *required* build time dependency of ``hashlib``. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.