|
26 | 26 |
|
27 | 27 | PROTOCOLS = sorted(ssl._PROTOCOL_NAMES)
|
28 | 28 | HOST = support.HOST
|
| 29 | +IS_LIBRESSL = ssl.OPENSSL_VERSION.startswith('LibreSSL') |
| 30 | +IS_OPENSSL_1_1 = not IS_LIBRESSL and ssl.OPENSSL_VERSION_INFO >= (1, 1, 0) |
| 31 | + |
29 | 32 |
|
30 | 33 | def data_file(*name):
|
31 | 34 | return os.path.join(os.path.dirname(__file__), *name)
|
@@ -164,7 +167,6 @@ def test_constants(self):
|
164 | 167 | self.assertIn(ssl.HAS_SNI, {True, False})
|
165 | 168 | self.assertIn(ssl.HAS_ECDH, {True, False})
|
166 | 169 |
|
167 |
| - |
168 | 170 | def test_random(self):
|
169 | 171 | v = ssl.RAND_status()
|
170 | 172 | if support.verbose:
|
@@ -281,9 +283,9 @@ def test_openssl_version(self):
|
281 | 283 | self.assertGreaterEqual(status, 0)
|
282 | 284 | self.assertLessEqual(status, 15)
|
283 | 285 | # Version string as returned by {Open,Libre}SSL, the format might change
|
284 |
| - if "LibreSSL" in s: |
285 |
| - self.assertTrue(s.startswith("LibreSSL {:d}.{:d}".format(major, minor)), |
286 |
| - (s, t)) |
| 286 | + if IS_LIBRESSL: |
| 287 | + self.assertTrue(s.startswith("LibreSSL {:d}".format(major)), |
| 288 | + (s, t, hex(n))) |
287 | 289 | else:
|
288 | 290 | self.assertTrue(s.startswith("OpenSSL {:d}.{:d}.{:d}".format(major, minor, fix)),
|
289 | 291 | (s, t))
|
@@ -742,15 +744,15 @@ def test_ciphers(self):
|
742 | 744 | def test_options(self):
|
743 | 745 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
|
744 | 746 | # OP_ALL | OP_NO_SSLv2 | OP_NO_SSLv3 is the default value
|
745 |
| - self.assertEqual(ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3, |
746 |
| - ctx.options) |
| 747 | + default = (ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3) |
| 748 | + if not IS_LIBRESSL and ssl.OPENSSL_VERSION_INFO >= (1, 1, 0): |
| 749 | + default |= ssl.OP_NO_COMPRESSION |
| 750 | + self.assertEqual(default, ctx.options) |
747 | 751 | ctx.options |= ssl.OP_NO_TLSv1
|
748 |
| - self.assertEqual(ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 | ssl.OP_NO_TLSv1, |
749 |
| - ctx.options) |
| 752 | + self.assertEqual(default | ssl.OP_NO_TLSv1, ctx.options) |
750 | 753 | if can_clear_options():
|
751 |
| - ctx.options = (ctx.options & ~ssl.OP_NO_SSLv2) | ssl.OP_NO_TLSv1 |
752 |
| - self.assertEqual(ssl.OP_ALL | ssl.OP_NO_TLSv1 | ssl.OP_NO_SSLv3, |
753 |
| - ctx.options) |
| 754 | + ctx.options = (ctx.options & ~ssl.OP_NO_TLSv1) |
| 755 | + self.assertEqual(default, ctx.options) |
754 | 756 | ctx.options = 0
|
755 | 757 | self.assertEqual(0, ctx.options)
|
756 | 758 | else:
|
@@ -1088,6 +1090,7 @@ def test_load_default_certs(self):
|
1088 | 1090 | self.assertRaises(TypeError, ctx.load_default_certs, 'SERVER_AUTH')
|
1089 | 1091 |
|
1090 | 1092 | @unittest.skipIf(sys.platform == "win32", "not-Windows specific")
|
| 1093 | + @unittest.skipIf(IS_LIBRESSL, "LibreSSL doesn't support env vars") |
1091 | 1094 | def test_load_default_certs_env(self):
|
1092 | 1095 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
|
1093 | 1096 | with support.EnvironmentVarGuard() as env:
|
@@ -1534,7 +1537,6 @@ def _test_get_server_certificate(host, port, cert=None):
|
1534 | 1537 | sys.stdout.write("%s\n" % x)
|
1535 | 1538 | else:
|
1536 | 1539 | self.fail("Got server certificate %s for %s:%s!" % (pem, host, port))
|
1537 |
| - |
1538 | 1540 | pem = ssl.get_server_certificate((host, port),
|
1539 | 1541 | ca_certs=cert)
|
1540 | 1542 | if not pem:
|
@@ -2783,7 +2785,7 @@ def test_version_basic(self):
|
2783 | 2785 | with closing(context.wrap_socket(socket.socket())) as s:
|
2784 | 2786 | self.assertIs(s.version(), None)
|
2785 | 2787 | s.connect((HOST, server.port))
|
2786 |
| - self.assertEqual(s.version(), "TLSv1") |
| 2788 | + self.assertEqual(s.version(), 'TLSv1') |
2787 | 2789 | self.assertIs(s.version(), None)
|
2788 | 2790 |
|
2789 | 2791 | @unittest.skipUnless(ssl.HAS_ECDH, "test requires ECDH-enabled OpenSSL")
|
@@ -2925,24 +2927,36 @@ def test_alpn_protocols(self):
|
2925 | 2927 | (['http/3.0', 'http/4.0'], None)
|
2926 | 2928 | ]
|
2927 | 2929 | for client_protocols, expected in protocol_tests:
|
2928 |
| - server_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 2930 | + server_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) |
2929 | 2931 | server_context.load_cert_chain(CERTFILE)
|
2930 | 2932 | server_context.set_alpn_protocols(server_protocols)
|
2931 |
| - client_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 2933 | + client_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) |
2932 | 2934 | client_context.load_cert_chain(CERTFILE)
|
2933 | 2935 | client_context.set_alpn_protocols(client_protocols)
|
2934 |
| - stats = server_params_test(client_context, server_context, |
2935 |
| - chatty=True, connectionchatty=True) |
2936 | 2936 |
|
2937 |
| - msg = "failed trying %s (s) and %s (c).\n" \ |
2938 |
| - "was expecting %s, but got %%s from the %%s" \ |
2939 |
| - % (str(server_protocols), str(client_protocols), |
2940 |
| - str(expected)) |
2941 |
| - client_result = stats['client_alpn_protocol'] |
2942 |
| - self.assertEqual(client_result, expected, msg % (client_result, "client")) |
2943 |
| - server_result = stats['server_alpn_protocols'][-1] \ |
2944 |
| - if len(stats['server_alpn_protocols']) else 'nothing' |
2945 |
| - self.assertEqual(server_result, expected, msg % (server_result, "server")) |
| 2937 | + try: |
| 2938 | + stats = server_params_test(client_context, |
| 2939 | + server_context, |
| 2940 | + chatty=True, |
| 2941 | + connectionchatty=True) |
| 2942 | + except ssl.SSLError as e: |
| 2943 | + stats = e |
| 2944 | + |
| 2945 | + if expected is None and IS_OPENSSL_1_1: |
| 2946 | + # OpenSSL 1.1.0 raises handshake error |
| 2947 | + self.assertIsInstance(stats, ssl.SSLError) |
| 2948 | + else: |
| 2949 | + msg = "failed trying %s (s) and %s (c).\n" \ |
| 2950 | + "was expecting %s, but got %%s from the %%s" \ |
| 2951 | + % (str(server_protocols), str(client_protocols), |
| 2952 | + str(expected)) |
| 2953 | + client_result = stats['client_alpn_protocol'] |
| 2954 | + self.assertEqual(client_result, expected, |
| 2955 | + msg % (client_result, "client")) |
| 2956 | + server_result = stats['server_alpn_protocols'][-1] \ |
| 2957 | + if len(stats['server_alpn_protocols']) else 'nothing' |
| 2958 | + self.assertEqual(server_result, expected, |
| 2959 | + msg % (server_result, "server")) |
2946 | 2960 |
|
2947 | 2961 | def test_selected_npn_protocol(self):
|
2948 | 2962 | # selected_npn_protocol() is None unless NPN is used
|
|
0 commit comments