|
20 | 20 |
|
21 | 21 | ssl = support.import_module("ssl") |
22 | 22 |
|
23 | | -PROTOCOLS = [ |
24 | | - ssl.PROTOCOL_SSLv3, |
25 | | - ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1 |
26 | | -] |
27 | | -if hasattr(ssl, 'PROTOCOL_SSLv2'): |
28 | | - PROTOCOLS.append(ssl.PROTOCOL_SSLv2) |
29 | | - |
| 23 | +PROTOCOLS = sorted(ssl._PROTOCOL_NAMES) |
30 | 24 | HOST = support.HOST |
31 | 25 |
|
32 | 26 | data_file = lambda name: os.path.join(os.path.dirname(__file__), name) |
@@ -101,10 +95,6 @@ def f(*args, **kwargs): |
101 | 95 | class BasicSocketTests(unittest.TestCase): |
102 | 96 |
|
103 | 97 | def test_constants(self): |
104 | | - #ssl.PROTOCOL_SSLv2 |
105 | | - ssl.PROTOCOL_SSLv23 |
106 | | - ssl.PROTOCOL_SSLv3 |
107 | | - ssl.PROTOCOL_TLSv1 |
108 | 98 | ssl.CERT_NONE |
109 | 99 | ssl.CERT_OPTIONAL |
110 | 100 | ssl.CERT_REQUIRED |
@@ -396,11 +386,8 @@ class ContextTests(unittest.TestCase): |
396 | 386 |
|
397 | 387 | @skip_if_broken_ubuntu_ssl |
398 | 388 | def test_constructor(self): |
399 | | - if hasattr(ssl, 'PROTOCOL_SSLv2'): |
400 | | - ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv2) |
401 | | - ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
402 | | - ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv3) |
403 | | - ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 389 | + for protocol in PROTOCOLS: |
| 390 | + ssl.SSLContext(protocol) |
404 | 391 | self.assertRaises(TypeError, ssl.SSLContext) |
405 | 392 | self.assertRaises(ValueError, ssl.SSLContext, -1) |
406 | 393 | self.assertRaises(ValueError, ssl.SSLContext, 42) |
@@ -1360,12 +1347,15 @@ def try_protocol_combo(server_protocol, client_protocol, expect_success, |
1360 | 1347 | client_context.options = ssl.OP_ALL | client_options |
1361 | 1348 | server_context = ssl.SSLContext(server_protocol) |
1362 | 1349 | server_context.options = ssl.OP_ALL | server_options |
| 1350 | + |
| 1351 | + # NOTE: we must enable "ALL" ciphers on the client, otherwise an |
| 1352 | + # SSLv23 client will send an SSLv3 hello (rather than SSLv2) |
| 1353 | + # starting from OpenSSL 1.0.0 (see issue #8322). |
| 1354 | + if client_context.protocol == ssl.PROTOCOL_SSLv23: |
| 1355 | + client_context.set_ciphers("ALL") |
| 1356 | + |
1363 | 1357 | for ctx in (client_context, server_context): |
1364 | 1358 | ctx.verify_mode = certsreqs |
1365 | | - # NOTE: we must enable "ALL" ciphers, otherwise an SSLv23 client |
1366 | | - # will send an SSLv3 hello (rather than SSLv2) starting from |
1367 | | - # OpenSSL 1.0.0 (see issue #8322). |
1368 | | - ctx.set_ciphers("ALL") |
1369 | 1359 | ctx.load_cert_chain(CERTFILE) |
1370 | 1360 | ctx.load_verify_locations(CERTFILE) |
1371 | 1361 | try: |
@@ -1581,6 +1571,49 @@ def test_protocol_tlsv1(self): |
1581 | 1571 | try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv23, False, |
1582 | 1572 | client_options=ssl.OP_NO_TLSv1) |
1583 | 1573 |
|
| 1574 | + @skip_if_broken_ubuntu_ssl |
| 1575 | + @unittest.skipUnless(hasattr(ssl, "PROTOCOL_TLSv1_1"), |
| 1576 | + "TLS version 1.1 not supported.") |
| 1577 | + def test_protocol_tlsv1_1(self): |
| 1578 | + """Connecting to a TLSv1.1 server with various client options. |
| 1579 | + Testing against older TLS versions.""" |
| 1580 | + if support.verbose: |
| 1581 | + sys.stdout.write("\n") |
| 1582 | + try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_TLSv1_1, True) |
| 1583 | + if hasattr(ssl, 'PROTOCOL_SSLv2'): |
| 1584 | + try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_SSLv2, False) |
| 1585 | + try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_SSLv3, False) |
| 1586 | + try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_SSLv23, False, |
| 1587 | + client_options=ssl.OP_NO_TLSv1_1) |
| 1588 | + |
| 1589 | + try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1_1, True) |
| 1590 | + try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_TLSv1, False) |
| 1591 | + try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1_1, False) |
| 1592 | + |
| 1593 | + |
| 1594 | + @skip_if_broken_ubuntu_ssl |
| 1595 | + @unittest.skipUnless(hasattr(ssl, "PROTOCOL_TLSv1_2"), |
| 1596 | + "TLS version 1.2 not supported.") |
| 1597 | + def test_protocol_tlsv1_2(self): |
| 1598 | + """Connecting to a TLSv1.2 server with various client options. |
| 1599 | + Testing against older TLS versions.""" |
| 1600 | + if support.verbose: |
| 1601 | + sys.stdout.write("\n") |
| 1602 | + try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1_2, True, |
| 1603 | + server_options=ssl.OP_NO_SSLv3|ssl.OP_NO_SSLv2, |
| 1604 | + client_options=ssl.OP_NO_SSLv3|ssl.OP_NO_SSLv2,) |
| 1605 | + if hasattr(ssl, 'PROTOCOL_SSLv2'): |
| 1606 | + try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_SSLv2, False) |
| 1607 | + try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_SSLv3, False) |
| 1608 | + try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_SSLv23, False, |
| 1609 | + client_options=ssl.OP_NO_TLSv1_2) |
| 1610 | + |
| 1611 | + try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1_2, True) |
| 1612 | + try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1, False) |
| 1613 | + try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1_2, False) |
| 1614 | + try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1_1, False) |
| 1615 | + try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_TLSv1_2, False) |
| 1616 | + |
1584 | 1617 | def test_starttls(self): |
1585 | 1618 | """Switching from clear text to encrypted and back again.""" |
1586 | 1619 | msgs = (b"msg 1", b"MSG 2", b"STARTTLS", b"MSG 3", b"msg 4", b"ENDTLS", b"msg 5", b"msg 6") |
|
0 commit comments