Skip to content

Commit fb26edd

Browse files
authored
Reduce MD5 and SHA1 dependency in tests (#1074)
* Reduce MD5 and SHA1 dependency in tests Most of the tests aren't specifically testing MD5 or SHA1. Switch those to a modern hash. * Fix line length
1 parent d184fbb commit fb26edd

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

tests/test_crypto.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def normalize_privatekey_pem(pem):
6464
GOOD_CIPHER = "blowfish"
6565
BAD_CIPHER = "zippers"
6666

67-
GOOD_DIGEST = "SHA1"
67+
GOOD_DIGEST = "SHA256"
6868
BAD_DIGEST = "monkeys"
6969

7070
old_root_cert_pem = b"""-----BEGIN CERTIFICATE-----
@@ -914,7 +914,7 @@ def test_unused_subject(self, x509_data):
914914
b"basicConstraints", False, b"CA:TRUE", subject=x509
915915
)
916916
x509.add_extensions([ext1])
917-
x509.sign(pkey, "sha1")
917+
x509.sign(pkey, "sha256")
918918
# This is a little lame. Can we think of a better way?
919919
text = dump_certificate(FILETYPE_TEXT, x509)
920920
assert b"X509v3 Basic Constraints:" in text
@@ -930,7 +930,7 @@ def test_subject(self, x509_data):
930930
b"subjectKeyIdentifier", False, b"hash", subject=x509
931931
)
932932
x509.add_extensions([ext3])
933-
x509.sign(pkey, "sha1")
933+
x509.sign(pkey, "sha256")
934934
text = dump_certificate(FILETYPE_TEXT, x509)
935935
assert b"X509v3 Subject Key Identifier:" in text
936936

@@ -963,7 +963,7 @@ def test_unused_issuer(self, x509_data):
963963
b"basicConstraints", False, b"CA:TRUE", issuer=x509
964964
)
965965
x509.add_extensions([ext1])
966-
x509.sign(pkey, "sha1")
966+
x509.sign(pkey, "sha256")
967967
text = dump_certificate(FILETYPE_TEXT, x509)
968968
assert b"X509v3 Basic Constraints:" in text
969969
assert b"CA:TRUE" in text
@@ -978,7 +978,7 @@ def test_issuer(self, x509_data):
978978
b"authorityKeyIdentifier", False, b"issuer:always", issuer=x509
979979
)
980980
x509.add_extensions([ext2])
981-
x509.sign(pkey, "sha1")
981+
x509.sign(pkey, "sha256")
982982
text = dump_certificate(FILETYPE_TEXT, x509)
983983
assert b"X509v3 Authority Key Identifier:" in text
984984
assert b"DirName:/CN=Yoda root CA" in text
@@ -1935,13 +1935,13 @@ def test_digest(self):
19351935
"""
19361936
cert = load_certificate(FILETYPE_PEM, old_root_cert_pem)
19371937
assert (
1938-
# This is MD5 instead of GOOD_DIGEST because the digest algorithm
1939-
# actually matters to the assertion (ie, another arbitrary, good
1940-
# digest will not product the same digest).
19411938
# Digest verified with the command:
1942-
# openssl x509 -in root_cert.pem -noout -fingerprint -md5
1943-
cert.digest("MD5")
1944-
== b"19:B3:05:26:2B:F8:F2:FF:0B:8F:21:07:A8:28:B8:75"
1939+
# openssl x509 -in root_cert.pem -noout -fingerprint -sha256
1940+
cert.digest("SHA256")
1941+
== (
1942+
b"3E:0F:16:39:6B:B1:3E:4F:08:85:C6:5F:10:0D:CB:2C:"
1943+
b"25:C2:91:4E:D0:4A:C2:29:06:BD:55:E3:A7:B3:B7:06"
1944+
)
19451945
)
19461946

19471947
def _extcert(self, pkey, extensions):
@@ -1957,7 +1957,7 @@ def _extcert(self, pkey, extensions):
19571957
cert.set_notAfter(when)
19581958

19591959
cert.add_extensions(extensions)
1960-
cert.sign(pkey, "sha1")
1960+
cert.sign(pkey, "sha256")
19611961
return load_certificate(
19621962
FILETYPE_PEM, dump_certificate(FILETYPE_PEM, cert)
19631963
)
@@ -3573,7 +3573,7 @@ def test_export_der(self):
35733573

35743574
# DER format
35753575
dumped_crl = self._get_crl().export(
3576-
self.cert, self.pkey, FILETYPE_ASN1, digest=b"md5"
3576+
self.cert, self.pkey, FILETYPE_ASN1, digest=b"sha256"
35773577
)
35783578
crl = x509.load_der_x509_crl(dumped_crl, backend)
35793579
revoked = crl.get_revoked_certificate_by_serial_number(0x03AB)
@@ -3600,7 +3600,7 @@ def test_export_text(self):
36003600

36013601
# text format
36023602
dumped_text = crl.export(
3603-
self.cert, self.pkey, type=FILETYPE_TEXT, digest=b"md5"
3603+
self.cert, self.pkey, type=FILETYPE_TEXT, digest=b"sha256"
36043604
)
36053605
assert len(dumped_text) > 500
36063606

@@ -3610,9 +3610,9 @@ def test_export_custom_digest(self):
36103610
signature algorithm based on that digest function.
36113611
"""
36123612
crl = self._get_crl()
3613-
dumped_crl = crl.export(self.cert, self.pkey, digest=b"sha1")
3613+
dumped_crl = crl.export(self.cert, self.pkey, digest=b"sha384")
36143614
text = _runopenssl(dumped_crl, b"crl", b"-noout", b"-text")
3615-
text.index(b"Signature Algorithm: sha1")
3615+
text.index(b"Signature Algorithm: sha384")
36163616

36173617
def test_export_md5_digest(self):
36183618
"""
@@ -4250,7 +4250,7 @@ def test_sign_verify(self):
42504250
# certificate unrelated to priv_key, used to trigger an error
42514251
bad_cert = load_certificate(FILETYPE_PEM, server_cert_pem)
42524252

4253-
for digest in ["md5", "sha1"]:
4253+
for digest in ["md5", "sha1", "sha256"]:
42544254
sig = sign(priv_key, content, digest)
42554255

42564256
# Verify the signature of content, will throw an exception if
@@ -4289,7 +4289,7 @@ def test_sign_verify_with_text(self):
42894289

42904290
priv_key = load_privatekey(FILETYPE_PEM, root_key_pem)
42914291
cert = load_certificate(FILETYPE_PEM, root_cert_pem)
4292-
for digest in ["md5", "sha1"]:
4292+
for digest in ["md5", "sha1", "sha256"]:
42934293
with pytest.warns(DeprecationWarning) as w:
42944294
simplefilter("always")
42954295
sig = sign(priv_key, content, digest)
@@ -4319,8 +4319,8 @@ def test_sign_verify_ecdsa(self):
43194319
)
43204320
priv_key = load_privatekey(FILETYPE_PEM, ec_root_key_pem)
43214321
cert = load_certificate(FILETYPE_PEM, ec_root_cert_pem)
4322-
sig = sign(priv_key, content, "sha1")
4323-
verify(cert, sig, content, "sha1")
4322+
sig = sign(priv_key, content, "sha256")
4323+
verify(cert, sig, content, "sha256")
43244324

43254325
def test_sign_nulls(self):
43264326
"""
@@ -4329,8 +4329,8 @@ def test_sign_nulls(self):
43294329
content = b"Watch out! \0 Did you see it?"
43304330
priv_key = load_privatekey(FILETYPE_PEM, root_key_pem)
43314331
good_cert = load_certificate(FILETYPE_PEM, root_cert_pem)
4332-
sig = sign(priv_key, content, "sha1")
4333-
verify(good_cert, sig, content, "sha1")
4332+
sig = sign(priv_key, content, "sha256")
4333+
verify(good_cert, sig, content, "sha256")
43344334

43354335
def test_sign_with_large_key(self):
43364336
"""
@@ -4345,7 +4345,7 @@ def test_sign_with_large_key(self):
43454345
)
43464346

43474347
priv_key = load_privatekey(FILETYPE_PEM, large_key_pem)
4348-
sign(priv_key, content, "sha1")
4348+
sign(priv_key, content, "sha256")
43494349

43504350

43514351
class TestEllipticCurve(object):

0 commit comments

Comments
 (0)