Skip to content

Commit 443b308

Browse files
authored
bpo-40479: Fix hashlib's usedforsecurity for OpenSSL 3.0.0 (GH-30455)
1 parent a6ca8ee commit 443b308

File tree

9 files changed

+358
-249
lines changed

9 files changed

+358
-249
lines changed

Doc/library/hashlib.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ More condensed:
120120

121121
Using :func:`new` with an algorithm provided by OpenSSL:
122122

123-
>>> h = hashlib.new('sha512_256')
123+
>>> h = hashlib.new('sha256')
124124
>>> h.update(b"Nobody inspects the spammish repetition")
125125
>>> h.hexdigest()
126-
'19197dc4d03829df858011c6c87600f994a858103bbc19005f20987aa19a97e2'
126+
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
127127

128128
Hashlib provides the following constant attributes:
129129

Lib/test/test_hashlib.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@
4848
builtin_hashlib = None
4949

5050
try:
51-
from _hashlib import HASH, HASHXOF, openssl_md_meth_names
51+
from _hashlib import HASH, HASHXOF, openssl_md_meth_names, get_fips_mode
5252
except ImportError:
5353
HASH = None
5454
HASHXOF = None
5555
openssl_md_meth_names = frozenset()
5656

57+
def get_fips_mode():
58+
return 0
59+
5760
try:
5861
import _blake2
5962
except ImportError:
@@ -192,10 +195,7 @@ def hash_constructors(self):
192195

193196
@property
194197
def is_fips_mode(self):
195-
if hasattr(self._hashlib, "get_fips_mode"):
196-
return self._hashlib.get_fips_mode()
197-
else:
198-
return None
198+
return get_fips_mode()
199199

200200
def test_hash_array(self):
201201
a = array.array("b", range(10))
@@ -1017,7 +1017,7 @@ def _test_pbkdf2_hmac(self, pbkdf2, supported):
10171017
self.assertEqual(out, expected,
10181018
(digest_name, password, salt, rounds))
10191019

1020-
with self.assertRaisesRegex(ValueError, 'unsupported hash type'):
1020+
with self.assertRaisesRegex(ValueError, '.*unsupported.*'):
10211021
pbkdf2('unknown', b'pass', b'salt', 1)
10221022

10231023
if 'sha1' in supported:
@@ -1057,6 +1057,7 @@ def test_pbkdf2_hmac_c(self):
10571057

10581058
@unittest.skipUnless(hasattr(hashlib, 'scrypt'),
10591059
' test requires OpenSSL > 1.1')
1060+
@unittest.skipIf(get_fips_mode(), reason="scrypt is blocked in FIPS mode")
10601061
def test_scrypt(self):
10611062
for password, salt, n, r, p, expected in self.scrypt_test_vectors:
10621063
result = hashlib.scrypt(password, salt=salt, n=n, r=r, p=p)

Lib/test/test_imaplib.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def cmd_AUTHENTICATE(self, tag, args):
387387
self.assertEqual(code, 'OK')
388388
self.assertEqual(server.response, b'ZmFrZQ==\r\n') # b64 encoded 'fake'
389389

390-
@hashlib_helper.requires_hashdigest('md5')
390+
@hashlib_helper.requires_hashdigest('md5', openssl=True)
391391
def test_login_cram_md5_bytes(self):
392392
class AuthHandler(SimpleIMAPHandler):
393393
capabilities = 'LOGINDISABLED AUTH=CRAM-MD5'
@@ -405,7 +405,7 @@ def cmd_AUTHENTICATE(self, tag, args):
405405
ret, _ = client.login_cram_md5("tim", b"tanstaaftanstaaf")
406406
self.assertEqual(ret, "OK")
407407

408-
@hashlib_helper.requires_hashdigest('md5')
408+
@hashlib_helper.requires_hashdigest('md5', openssl=True)
409409
def test_login_cram_md5_plain_text(self):
410410
class AuthHandler(SimpleIMAPHandler):
411411
capabilities = 'LOGINDISABLED AUTH=CRAM-MD5'
@@ -851,7 +851,7 @@ def cmd_AUTHENTICATE(self, tag, args):
851851
b'ZmFrZQ==\r\n') # b64 encoded 'fake'
852852

853853
@threading_helper.reap_threads
854-
@hashlib_helper.requires_hashdigest('md5')
854+
@hashlib_helper.requires_hashdigest('md5', openssl=True)
855855
def test_login_cram_md5(self):
856856

857857
class AuthHandler(SimpleIMAPHandler):

Lib/test/test_poplib.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,11 @@ def test_noop(self):
318318
def test_rpop(self):
319319
self.assertOK(self.client.rpop('foo'))
320320

321-
@hashlib_helper.requires_hashdigest('md5')
321+
@hashlib_helper.requires_hashdigest('md5', openssl=True)
322322
def test_apop_normal(self):
323323
self.assertOK(self.client.apop('foo', 'dummypassword'))
324324

325-
@hashlib_helper.requires_hashdigest('md5')
325+
@hashlib_helper.requires_hashdigest('md5', openssl=True)
326326
def test_apop_REDOS(self):
327327
# Replace welcome with very long evil welcome.
328328
# NB The upper bound on welcome length is currently 2048.

Lib/test/test_smtplib.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ def auth_buggy(challenge=None):
11711171
finally:
11721172
smtp.close()
11731173

1174-
@hashlib_helper.requires_hashdigest('md5')
1174+
@hashlib_helper.requires_hashdigest('md5', openssl=True)
11751175
def testAUTH_CRAM_MD5(self):
11761176
self.serv.add_feature("AUTH CRAM-MD5")
11771177
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
@@ -1180,7 +1180,7 @@ def testAUTH_CRAM_MD5(self):
11801180
self.assertEqual(resp, (235, b'Authentication Succeeded'))
11811181
smtp.close()
11821182

1183-
@hashlib_helper.requires_hashdigest('md5')
1183+
@hashlib_helper.requires_hashdigest('md5', openssl=True)
11841184
def testAUTH_multiple(self):
11851185
# Test that multiple authentication methods are tried.
11861186
self.serv.add_feature("AUTH BOGUS PLAIN LOGIN CRAM-MD5")

Lib/test/test_tools/test_md5sum.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
skip_if_missing()
1313

14-
@hashlib_helper.requires_hashdigest('md5')
14+
@hashlib_helper.requires_hashdigest('md5', openssl=True)
1515
class MD5SumTests(unittest.TestCase):
1616
@classmethod
1717
def setUpClass(cls):

Lib/test/test_urllib2_localnet.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def test_basic_auth_httperror(self):
317317
self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen, self.server_url)
318318

319319

320-
@hashlib_helper.requires_hashdigest("md5")
320+
@hashlib_helper.requires_hashdigest("md5", openssl=True)
321321
class ProxyAuthTests(unittest.TestCase):
322322
URL = "http://localhost"
323323

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :mod:`hashlib` *usedforsecurity* option to work correctly with OpenSSL
2+
3.0.0 in FIPS mode.

0 commit comments

Comments
 (0)