Skip to content

Commit bf99da5

Browse files
committed
Use new minimum_version to further harden TLS context and adjust unit tests
from #491 to fit. Tighten default context to never allow TLSv1 but force at least TLSv1_1 if `allow_pre_tlsv12` is explicitly requested.
1 parent c02f7b8 commit bf99da5

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

mig/shared/tlsserver.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,19 @@ def hardened_ssl_context(configuration, keyfile, certfile, dhparamsfile=None,
5757
# https://wiki.mozilla.org/Security/Server_Side_TLS
5858
ssl_options |= getattr(ssl, 'OP_NO_SSLv2', 0x1000000)
5959
ssl_options |= getattr(ssl, 'OP_NO_SSLv3', 0x2000000)
60+
ssl_options |= getattr(ssl, 'OP_NO_TLSv1', 0x4000000)
61+
ssl_ctx.minimum_version = ssl.TLSVersion.TLSv1_1
6062
# NOTE: refuse weak TLS protocols unless allow_pre_tlsv12
6163
if not allow_pre_tlsv12:
62-
ssl_options |= getattr(ssl, 'OP_NO_TLSv1', 0x4000000)
6364
ssl_options |= getattr(ssl, 'OP_NO_TLSv1_1', 0x10000000)
65+
ssl_ctx.minimum_version = ssl.TLSVersion.TLSv1_2
6466
# NOTE: refuse slightly dated TLS 1.2 protocol unless allow_pre_tlsv13
6567
if not allow_pre_tlsv13:
6668
if getattr(ssl, 'HAS_TLSv1_3', False):
6769
ssl_options |= getattr(ssl, 'OP_NO_TLSv1_2', 0x8000000)
6870
else:
6971
_logger.warning("won't disable TLS 1.2 without TLS 1.3 support")
72+
ssl_ctx.minimum_version = ssl.TLSVersion.TLSv1_3
7073
# NOTE: refuse client TLS renegotiation unless allow_renegotiation
7174
if not allow_renegotiation:
7275
ssl_options |= getattr(ssl, 'OP_NO_RENEGOTIATION', 0x40000000)

tests/test_mig_shared_tlsserver.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,11 @@ def test_hardened_ssl_context_options_default(self):
183183

184184
# Verify the options were OR'd into the context
185185
self.assertEqual(context.options & expected_options, expected_options)
186+
# Verify that the minimum TLS version is enforced
187+
self.assertEqual(context.minimum_version, ssl.TLSVersion.TLSv1_2)
186188

187-
def test_hardened_ssl_context_options_tls1_1_only(self):
188-
"""Test SSL context options are set correctly with TLS 1.1 only"""
189+
def test_hardened_ssl_context_options_tls1_1(self):
190+
"""Test SSL context options are set correctly with TLS 1.1 enabled"""
189191
config = self.configuration
190192
config.logger = self.logger
191193

@@ -197,15 +199,15 @@ def test_hardened_ssl_context_options_tls1_1_only(self):
197199
STRONG_TLS_CIPHERS,
198200
STRONG_TLS_CURVES,
199201
True,
200-
False,
202+
True,
201203
False
202204
)
203205

204206
# Verify options are set
205207
expected_options = (
206208
getattr(ssl, 'OP_NO_SSLv2', 0x1000000) |
207209
getattr(ssl, 'OP_NO_SSLv3', 0x2000000) |
208-
getattr(ssl, 'OP_NO_TLSv1_2', 0x8000000) |
210+
getattr(ssl, 'OP_NO_TLSv1', 0x4000000) |
209211
getattr(ssl, 'OP_NO_COMPRESSION', 0x20000) |
210212
getattr(ssl, 'OP_CIPHER_SERVER_PREFERENCE', 0x400000) |
211213
getattr(ssl, 'OP_SINGLE_ECDH_USE', 0x80000) |
@@ -216,6 +218,8 @@ def test_hardened_ssl_context_options_tls1_1_only(self):
216218

217219
# Verify the options were OR'd into the context
218220
self.assertEqual(context.options & expected_options, expected_options)
221+
# Verify that the minimum TLS version is enforced
222+
self.assertEqual(context.minimum_version, ssl.TLSVersion.TLSv1_1)
219223

220224
def test_hardened_ssl_context_options_tls1_3_only(self):
221225
"""Test SSL context options are set correctly with TLS 1.3 only"""
@@ -251,6 +255,8 @@ def test_hardened_ssl_context_options_tls1_3_only(self):
251255

252256
# Verify the options were OR'd into the context
253257
self.assertEqual(context.options & expected_options, expected_options)
258+
# Verify that the minimum TLS version is enforced
259+
self.assertEqual(context.minimum_version, ssl.TLSVersion.TLSv1_3)
254260

255261
def test_hardened_ssl_context_options_fail_reneg(self):
256262
"""Test SSL context options fail when different"""

0 commit comments

Comments
 (0)