Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1 | # Test the support for SSL and sockets |
| 2 | |
| 3 | import sys |
| 4 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | from test import support |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 6 | import socket |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 7 | import select |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8 | import time |
Antoine Pitrou | cfcd8ad | 2010-04-23 23:31:47 +0000 | [diff] [blame] | 9 | import gc |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 10 | import os |
Antoine Pitrou | cfcd8ad | 2010-04-23 23:31:47 +0000 | [diff] [blame] | 11 | import errno |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12 | import pprint |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 13 | import tempfile |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 14 | import urllib.request |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 15 | import traceback |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 16 | import asyncore |
Antoine Pitrou | 9d54366 | 2010-04-23 23:10:32 +0000 | [diff] [blame] | 17 | import weakref |
Antoine Pitrou | 15cee62 | 2010-08-04 16:45:21 +0000 | [diff] [blame] | 18 | import platform |
Antoine Pitrou | 23df483 | 2010-08-04 17:14:06 +0000 | [diff] [blame] | 19 | import functools |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 20 | |
Antoine Pitrou | 05d936d | 2010-10-13 11:38:36 +0000 | [diff] [blame] | 21 | ssl = support.import_module("ssl") |
| 22 | |
| 23 | PROTOCOLS = [ |
| 24 | ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv3, |
| 25 | ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1 |
| 26 | ] |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 27 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 28 | HOST = support.HOST |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 29 | |
| 30 | data_file = lambda name: os.path.join(os.path.dirname(__file__), name) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 31 | |
Antoine Pitrou | 8156409 | 2010-10-08 23:06:24 +0000 | [diff] [blame] | 32 | # The custom key and certificate files used in test_ssl are generated |
| 33 | # using Lib/test/make_ssl_certs.py. |
| 34 | # Other certificates are simply fetched from the Internet servers they |
| 35 | # are meant to authenticate. |
| 36 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 37 | CERTFILE = data_file("keycert.pem") |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 38 | BYTES_CERTFILE = os.fsencode(CERTFILE) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 39 | ONLYCERT = data_file("ssl_cert.pem") |
| 40 | ONLYKEY = data_file("ssl_key.pem") |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 41 | BYTES_ONLYCERT = os.fsencode(ONLYCERT) |
| 42 | BYTES_ONLYKEY = os.fsencode(ONLYKEY) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 43 | CAPATH = data_file("capath") |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 44 | BYTES_CAPATH = os.fsencode(CAPATH) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 45 | |
| 46 | SVN_PYTHON_ORG_ROOT_CERT = data_file("https_svn_python_org_root.pem") |
| 47 | |
| 48 | EMPTYCERT = data_file("nullcert.pem") |
| 49 | BADCERT = data_file("badcert.pem") |
| 50 | WRONGCERT = data_file("XXXnonexisting.pem") |
| 51 | BADKEY = data_file("badkey.pem") |
| 52 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 53 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 54 | def handle_error(prefix): |
| 55 | exc_format = ' '.join(traceback.format_exception(*sys.exc_info())) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 56 | if support.verbose: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 57 | sys.stdout.write(prefix + exc_format) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 58 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 59 | def can_clear_options(): |
| 60 | # 0.9.8m or higher |
| 61 | return ssl.OPENSSL_VERSION_INFO >= (0, 9, 8, 13, 15) |
| 62 | |
| 63 | def no_sslv2_implies_sslv3_hello(): |
| 64 | # 0.9.7h or higher |
| 65 | return ssl.OPENSSL_VERSION_INFO >= (0, 9, 7, 8, 15) |
| 66 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 67 | |
Antoine Pitrou | 23df483 | 2010-08-04 17:14:06 +0000 | [diff] [blame] | 68 | # Issue #9415: Ubuntu hijacks their OpenSSL and forcefully disables SSLv2 |
| 69 | def skip_if_broken_ubuntu_ssl(func): |
| 70 | @functools.wraps(func) |
| 71 | def f(*args, **kwargs): |
| 72 | try: |
| 73 | ssl.SSLContext(ssl.PROTOCOL_SSLv2) |
| 74 | except ssl.SSLError: |
| 75 | if (ssl.OPENSSL_VERSION_INFO == (0, 9, 8, 15, 15) and |
| 76 | platform.linux_distribution() == ('debian', 'squeeze/sid', '')): |
| 77 | raise unittest.SkipTest("Patched Ubuntu OpenSSL breaks behaviour") |
| 78 | return func(*args, **kwargs) |
| 79 | return f |
| 80 | |
| 81 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 82 | class BasicSocketTests(unittest.TestCase): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 83 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 84 | def test_constants(self): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 85 | ssl.PROTOCOL_SSLv2 |
| 86 | ssl.PROTOCOL_SSLv23 |
| 87 | ssl.PROTOCOL_SSLv3 |
| 88 | ssl.PROTOCOL_TLSv1 |
| 89 | ssl.CERT_NONE |
| 90 | ssl.CERT_OPTIONAL |
| 91 | ssl.CERT_REQUIRED |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 92 | self.assertIn(ssl.HAS_SNI, {True, False}) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 93 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 94 | def test_random(self): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 95 | v = ssl.RAND_status() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 96 | if support.verbose: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 97 | sys.stdout.write("\n RAND_status is %d (%s)\n" |
| 98 | % (v, (v and "sufficient randomness") or |
| 99 | "insufficient randomness")) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 100 | try: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 101 | ssl.RAND_egd(1) |
| 102 | except TypeError: |
| 103 | pass |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 104 | else: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 105 | print("didn't raise TypeError") |
| 106 | ssl.RAND_add("this is a random string", 75.0) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 107 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 108 | def test_parse_cert(self): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 109 | # note that this uses an 'unofficial' function in _ssl.c, |
| 110 | # provided solely for this test, to exercise the certificate |
| 111 | # parsing code |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 112 | p = ssl._ssl._test_decode_cert(CERTFILE) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 113 | if support.verbose: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 114 | sys.stdout.write("\n" + pprint.pformat(p) + "\n") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 115 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 116 | def test_DER_to_PEM(self): |
| 117 | with open(SVN_PYTHON_ORG_ROOT_CERT, 'r') as f: |
| 118 | pem = f.read() |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 119 | d1 = ssl.PEM_cert_to_DER_cert(pem) |
| 120 | p2 = ssl.DER_cert_to_PEM_cert(d1) |
| 121 | d2 = ssl.PEM_cert_to_DER_cert(p2) |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 122 | self.assertEqual(d1, d2) |
Antoine Pitrou | 9bfbe61 | 2010-04-27 22:08:08 +0000 | [diff] [blame] | 123 | if not p2.startswith(ssl.PEM_HEADER + '\n'): |
| 124 | self.fail("DER-to-PEM didn't include correct header:\n%r\n" % p2) |
| 125 | if not p2.endswith('\n' + ssl.PEM_FOOTER + '\n'): |
| 126 | self.fail("DER-to-PEM didn't include correct footer:\n%r\n" % p2) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 128 | def test_openssl_version(self): |
| 129 | n = ssl.OPENSSL_VERSION_NUMBER |
| 130 | t = ssl.OPENSSL_VERSION_INFO |
| 131 | s = ssl.OPENSSL_VERSION |
| 132 | self.assertIsInstance(n, int) |
| 133 | self.assertIsInstance(t, tuple) |
| 134 | self.assertIsInstance(s, str) |
| 135 | # Some sanity checks follow |
| 136 | # >= 0.9 |
| 137 | self.assertGreaterEqual(n, 0x900000) |
| 138 | # < 2.0 |
| 139 | self.assertLess(n, 0x20000000) |
| 140 | major, minor, fix, patch, status = t |
| 141 | self.assertGreaterEqual(major, 0) |
| 142 | self.assertLess(major, 2) |
| 143 | self.assertGreaterEqual(minor, 0) |
| 144 | self.assertLess(minor, 256) |
| 145 | self.assertGreaterEqual(fix, 0) |
| 146 | self.assertLess(fix, 256) |
| 147 | self.assertGreaterEqual(patch, 0) |
| 148 | self.assertLessEqual(patch, 26) |
| 149 | self.assertGreaterEqual(status, 0) |
| 150 | self.assertLessEqual(status, 15) |
| 151 | # Version string as returned by OpenSSL, the format might change |
| 152 | self.assertTrue(s.startswith("OpenSSL {:d}.{:d}.{:d}".format(major, minor, fix)), |
| 153 | (s, t)) |
| 154 | |
Antoine Pitrou | 9d54366 | 2010-04-23 23:10:32 +0000 | [diff] [blame] | 155 | @support.cpython_only |
| 156 | def test_refcycle(self): |
| 157 | # Issue #7943: an SSL object doesn't create reference cycles with |
| 158 | # itself. |
| 159 | s = socket.socket(socket.AF_INET) |
| 160 | ss = ssl.wrap_socket(s) |
| 161 | wr = weakref.ref(ss) |
| 162 | del ss |
| 163 | self.assertEqual(wr(), None) |
| 164 | |
Antoine Pitrou | a468adc | 2010-09-14 14:43:44 +0000 | [diff] [blame] | 165 | def test_wrapped_unconnected(self): |
| 166 | # Methods on an unconnected SSLSocket propagate the original |
| 167 | # socket.error raise by the underlying socket object. |
| 168 | s = socket.socket(socket.AF_INET) |
| 169 | ss = ssl.wrap_socket(s) |
| 170 | self.assertRaises(socket.error, ss.recv, 1) |
| 171 | self.assertRaises(socket.error, ss.recv_into, bytearray(b'x')) |
| 172 | self.assertRaises(socket.error, ss.recvfrom, 1) |
| 173 | self.assertRaises(socket.error, ss.recvfrom_into, bytearray(b'x'), 1) |
| 174 | self.assertRaises(socket.error, ss.send, b'x') |
| 175 | self.assertRaises(socket.error, ss.sendto, b'x', ('0.0.0.0', 0)) |
| 176 | |
Antoine Pitrou | 40f0874 | 2010-04-24 22:04:40 +0000 | [diff] [blame] | 177 | def test_timeout(self): |
| 178 | # Issue #8524: when creating an SSL socket, the timeout of the |
| 179 | # original socket should be retained. |
| 180 | for timeout in (None, 0.0, 5.0): |
| 181 | s = socket.socket(socket.AF_INET) |
| 182 | s.settimeout(timeout) |
| 183 | ss = ssl.wrap_socket(s) |
| 184 | self.assertEqual(timeout, ss.gettimeout()) |
| 185 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 186 | def test_errors(self): |
| 187 | sock = socket.socket() |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 188 | self.assertRaisesRegex(ValueError, |
Giampaolo Rodolà | 8b7da62 | 2010-08-30 18:28:05 +0000 | [diff] [blame] | 189 | "certfile must be specified", |
| 190 | ssl.wrap_socket, sock, keyfile=CERTFILE) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 191 | self.assertRaisesRegex(ValueError, |
Giampaolo Rodolà | 8b7da62 | 2010-08-30 18:28:05 +0000 | [diff] [blame] | 192 | "certfile must be specified for server-side operations", |
| 193 | ssl.wrap_socket, sock, server_side=True) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 194 | self.assertRaisesRegex(ValueError, |
Giampaolo Rodolà | 8b7da62 | 2010-08-30 18:28:05 +0000 | [diff] [blame] | 195 | "certfile must be specified for server-side operations", |
| 196 | ssl.wrap_socket, sock, server_side=True, certfile="") |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 197 | s = ssl.wrap_socket(sock, server_side=True, certfile=CERTFILE) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 198 | self.assertRaisesRegex(ValueError, "can't connect in server-side mode", |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 199 | s.connect, (HOST, 8080)) |
Giampaolo Rodolà | 4a656eb | 2010-08-29 22:50:39 +0000 | [diff] [blame] | 200 | with self.assertRaises(IOError) as cm: |
Antoine Pitrou | d2eca37 | 2010-10-29 23:41:37 +0000 | [diff] [blame] | 201 | with socket.socket() as sock: |
| 202 | ssl.wrap_socket(sock, certfile=WRONGCERT) |
Giampaolo Rodolà | 4a656eb | 2010-08-29 22:50:39 +0000 | [diff] [blame] | 203 | self.assertEqual(cm.exception.errno, errno.ENOENT) |
Giampaolo Rodolà | 8b7da62 | 2010-08-30 18:28:05 +0000 | [diff] [blame] | 204 | with self.assertRaises(IOError) as cm: |
Antoine Pitrou | d2eca37 | 2010-10-29 23:41:37 +0000 | [diff] [blame] | 205 | with socket.socket() as sock: |
| 206 | ssl.wrap_socket(sock, certfile=CERTFILE, keyfile=WRONGCERT) |
Giampaolo Rodolà | 8b7da62 | 2010-08-30 18:28:05 +0000 | [diff] [blame] | 207 | self.assertEqual(cm.exception.errno, errno.ENOENT) |
Giampaolo Rodolà | 4a656eb | 2010-08-29 22:50:39 +0000 | [diff] [blame] | 208 | with self.assertRaises(IOError) as cm: |
Antoine Pitrou | d2eca37 | 2010-10-29 23:41:37 +0000 | [diff] [blame] | 209 | with socket.socket() as sock: |
| 210 | ssl.wrap_socket(sock, certfile=WRONGCERT, keyfile=WRONGCERT) |
Giampaolo Rodolà | 4a656eb | 2010-08-29 22:50:39 +0000 | [diff] [blame] | 211 | self.assertEqual(cm.exception.errno, errno.ENOENT) |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 212 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 213 | def test_match_hostname(self): |
| 214 | def ok(cert, hostname): |
| 215 | ssl.match_hostname(cert, hostname) |
| 216 | def fail(cert, hostname): |
| 217 | self.assertRaises(ssl.CertificateError, |
| 218 | ssl.match_hostname, cert, hostname) |
| 219 | |
| 220 | cert = {'subject': ((('commonName', 'example.com'),),)} |
| 221 | ok(cert, 'example.com') |
| 222 | ok(cert, 'ExAmple.cOm') |
| 223 | fail(cert, 'www.example.com') |
| 224 | fail(cert, '.example.com') |
| 225 | fail(cert, 'example.org') |
| 226 | fail(cert, 'exampleXcom') |
| 227 | |
| 228 | cert = {'subject': ((('commonName', '*.a.com'),),)} |
| 229 | ok(cert, 'foo.a.com') |
| 230 | fail(cert, 'bar.foo.a.com') |
| 231 | fail(cert, 'a.com') |
| 232 | fail(cert, 'Xa.com') |
| 233 | fail(cert, '.a.com') |
| 234 | |
| 235 | cert = {'subject': ((('commonName', 'a.*.com'),),)} |
| 236 | ok(cert, 'a.foo.com') |
| 237 | fail(cert, 'a..com') |
| 238 | fail(cert, 'a.com') |
| 239 | |
| 240 | cert = {'subject': ((('commonName', 'f*.com'),),)} |
| 241 | ok(cert, 'foo.com') |
| 242 | ok(cert, 'f.com') |
| 243 | fail(cert, 'bar.com') |
| 244 | fail(cert, 'foo.a.com') |
| 245 | fail(cert, 'bar.foo.com') |
| 246 | |
| 247 | # Slightly fake real-world example |
| 248 | cert = {'notAfter': 'Jun 26 21:41:46 2011 GMT', |
| 249 | 'subject': ((('commonName', 'linuxfrz.org'),),), |
| 250 | 'subjectAltName': (('DNS', 'linuxfr.org'), |
| 251 | ('DNS', 'linuxfr.com'), |
| 252 | ('othername', '<unsupported>'))} |
| 253 | ok(cert, 'linuxfr.org') |
| 254 | ok(cert, 'linuxfr.com') |
| 255 | # Not a "DNS" entry |
| 256 | fail(cert, '<unsupported>') |
| 257 | # When there is a subjectAltName, commonName isn't used |
| 258 | fail(cert, 'linuxfrz.org') |
| 259 | |
| 260 | # A pristine real-world example |
| 261 | cert = {'notAfter': 'Dec 18 23:59:59 2011 GMT', |
| 262 | 'subject': ((('countryName', 'US'),), |
| 263 | (('stateOrProvinceName', 'California'),), |
| 264 | (('localityName', 'Mountain View'),), |
| 265 | (('organizationName', 'Google Inc'),), |
| 266 | (('commonName', 'mail.google.com'),))} |
| 267 | ok(cert, 'mail.google.com') |
| 268 | fail(cert, 'gmail.com') |
| 269 | # Only commonName is considered |
| 270 | fail(cert, 'California') |
| 271 | |
| 272 | # Neither commonName nor subjectAltName |
| 273 | cert = {'notAfter': 'Dec 18 23:59:59 2011 GMT', |
| 274 | 'subject': ((('countryName', 'US'),), |
| 275 | (('stateOrProvinceName', 'California'),), |
| 276 | (('localityName', 'Mountain View'),), |
| 277 | (('organizationName', 'Google Inc'),))} |
| 278 | fail(cert, 'mail.google.com') |
| 279 | |
Antoine Pitrou | 1c86b44 | 2011-05-06 15:19:49 +0200 | [diff] [blame^] | 280 | # No DNS entry in subjectAltName but a commonName |
| 281 | cert = {'notAfter': 'Dec 18 23:59:59 2099 GMT', |
| 282 | 'subject': ((('countryName', 'US'),), |
| 283 | (('stateOrProvinceName', 'California'),), |
| 284 | (('localityName', 'Mountain View'),), |
| 285 | (('commonName', 'mail.google.com'),)), |
| 286 | 'subjectAltName': (('othername', 'blabla'), )} |
| 287 | ok(cert, 'mail.google.com') |
| 288 | |
| 289 | # No DNS entry subjectAltName and no commonName |
| 290 | cert = {'notAfter': 'Dec 18 23:59:59 2099 GMT', |
| 291 | 'subject': ((('countryName', 'US'),), |
| 292 | (('stateOrProvinceName', 'California'),), |
| 293 | (('localityName', 'Mountain View'),), |
| 294 | (('organizationName', 'Google Inc'),)), |
| 295 | 'subjectAltName': (('othername', 'blabla'),)} |
| 296 | fail(cert, 'google.com') |
| 297 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 298 | # Empty cert / no cert |
| 299 | self.assertRaises(ValueError, ssl.match_hostname, None, 'example.com') |
| 300 | self.assertRaises(ValueError, ssl.match_hostname, {}, 'example.com') |
| 301 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 302 | def test_server_side(self): |
| 303 | # server_hostname doesn't work for server sockets |
| 304 | ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
Antoine Pitrou | d2eca37 | 2010-10-29 23:41:37 +0000 | [diff] [blame] | 305 | with socket.socket() as sock: |
| 306 | self.assertRaises(ValueError, ctx.wrap_socket, sock, True, |
| 307 | server_hostname="some.hostname") |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 308 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 309 | class ContextTests(unittest.TestCase): |
| 310 | |
Antoine Pitrou | 23df483 | 2010-08-04 17:14:06 +0000 | [diff] [blame] | 311 | @skip_if_broken_ubuntu_ssl |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 312 | def test_constructor(self): |
| 313 | ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv2) |
| 314 | ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
| 315 | ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv3) |
| 316 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 317 | self.assertRaises(TypeError, ssl.SSLContext) |
| 318 | self.assertRaises(ValueError, ssl.SSLContext, -1) |
| 319 | self.assertRaises(ValueError, ssl.SSLContext, 42) |
| 320 | |
Antoine Pitrou | 23df483 | 2010-08-04 17:14:06 +0000 | [diff] [blame] | 321 | @skip_if_broken_ubuntu_ssl |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 322 | def test_protocol(self): |
| 323 | for proto in PROTOCOLS: |
| 324 | ctx = ssl.SSLContext(proto) |
| 325 | self.assertEqual(ctx.protocol, proto) |
| 326 | |
| 327 | def test_ciphers(self): |
| 328 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 329 | ctx.set_ciphers("ALL") |
| 330 | ctx.set_ciphers("DEFAULT") |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 331 | with self.assertRaisesRegex(ssl.SSLError, "No cipher can be selected"): |
Antoine Pitrou | 3047406 | 2010-05-16 23:46:26 +0000 | [diff] [blame] | 332 | ctx.set_ciphers("^$:,;?*'dorothyx") |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | 23df483 | 2010-08-04 17:14:06 +0000 | [diff] [blame] | 334 | @skip_if_broken_ubuntu_ssl |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 335 | def test_options(self): |
| 336 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 337 | # OP_ALL is the default value |
| 338 | self.assertEqual(ssl.OP_ALL, ctx.options) |
| 339 | ctx.options |= ssl.OP_NO_SSLv2 |
| 340 | self.assertEqual(ssl.OP_ALL | ssl.OP_NO_SSLv2, |
| 341 | ctx.options) |
| 342 | ctx.options |= ssl.OP_NO_SSLv3 |
| 343 | self.assertEqual(ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3, |
| 344 | ctx.options) |
| 345 | if can_clear_options(): |
| 346 | ctx.options = (ctx.options & ~ssl.OP_NO_SSLv2) | ssl.OP_NO_TLSv1 |
| 347 | self.assertEqual(ssl.OP_ALL | ssl.OP_NO_TLSv1 | ssl.OP_NO_SSLv3, |
| 348 | ctx.options) |
| 349 | ctx.options = 0 |
| 350 | self.assertEqual(0, ctx.options) |
| 351 | else: |
| 352 | with self.assertRaises(ValueError): |
| 353 | ctx.options = 0 |
| 354 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 355 | def test_verify(self): |
| 356 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 357 | # Default value |
| 358 | self.assertEqual(ctx.verify_mode, ssl.CERT_NONE) |
| 359 | ctx.verify_mode = ssl.CERT_OPTIONAL |
| 360 | self.assertEqual(ctx.verify_mode, ssl.CERT_OPTIONAL) |
| 361 | ctx.verify_mode = ssl.CERT_REQUIRED |
| 362 | self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) |
| 363 | ctx.verify_mode = ssl.CERT_NONE |
| 364 | self.assertEqual(ctx.verify_mode, ssl.CERT_NONE) |
| 365 | with self.assertRaises(TypeError): |
| 366 | ctx.verify_mode = None |
| 367 | with self.assertRaises(ValueError): |
| 368 | ctx.verify_mode = 42 |
| 369 | |
| 370 | def test_load_cert_chain(self): |
| 371 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 372 | # Combined key and cert in a single file |
| 373 | ctx.load_cert_chain(CERTFILE) |
| 374 | ctx.load_cert_chain(CERTFILE, keyfile=CERTFILE) |
| 375 | self.assertRaises(TypeError, ctx.load_cert_chain, keyfile=CERTFILE) |
Giampaolo Rodolà | 4a656eb | 2010-08-29 22:50:39 +0000 | [diff] [blame] | 376 | with self.assertRaises(IOError) as cm: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 377 | ctx.load_cert_chain(WRONGCERT) |
Giampaolo Rodolà | 4a656eb | 2010-08-29 22:50:39 +0000 | [diff] [blame] | 378 | self.assertEqual(cm.exception.errno, errno.ENOENT) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 379 | with self.assertRaisesRegex(ssl.SSLError, "PEM lib"): |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 380 | ctx.load_cert_chain(BADCERT) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 381 | with self.assertRaisesRegex(ssl.SSLError, "PEM lib"): |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 382 | ctx.load_cert_chain(EMPTYCERT) |
| 383 | # Separate key and cert |
Antoine Pitrou | d091950 | 2010-05-17 10:30:00 +0000 | [diff] [blame] | 384 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 385 | ctx.load_cert_chain(ONLYCERT, ONLYKEY) |
| 386 | ctx.load_cert_chain(certfile=ONLYCERT, keyfile=ONLYKEY) |
| 387 | ctx.load_cert_chain(certfile=BYTES_ONLYCERT, keyfile=BYTES_ONLYKEY) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 388 | with self.assertRaisesRegex(ssl.SSLError, "PEM lib"): |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 389 | ctx.load_cert_chain(ONLYCERT) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 390 | with self.assertRaisesRegex(ssl.SSLError, "PEM lib"): |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 391 | ctx.load_cert_chain(ONLYKEY) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 392 | with self.assertRaisesRegex(ssl.SSLError, "PEM lib"): |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 393 | ctx.load_cert_chain(certfile=ONLYKEY, keyfile=ONLYCERT) |
| 394 | # Mismatching key and cert |
Antoine Pitrou | d091950 | 2010-05-17 10:30:00 +0000 | [diff] [blame] | 395 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 396 | with self.assertRaisesRegex(ssl.SSLError, "key values mismatch"): |
Antoine Pitrou | 8156409 | 2010-10-08 23:06:24 +0000 | [diff] [blame] | 397 | ctx.load_cert_chain(SVN_PYTHON_ORG_ROOT_CERT, ONLYKEY) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 398 | |
| 399 | def test_load_verify_locations(self): |
| 400 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 401 | ctx.load_verify_locations(CERTFILE) |
| 402 | ctx.load_verify_locations(cafile=CERTFILE, capath=None) |
| 403 | ctx.load_verify_locations(BYTES_CERTFILE) |
| 404 | ctx.load_verify_locations(cafile=BYTES_CERTFILE, capath=None) |
| 405 | self.assertRaises(TypeError, ctx.load_verify_locations) |
| 406 | self.assertRaises(TypeError, ctx.load_verify_locations, None, None) |
Giampaolo Rodolà | 4a656eb | 2010-08-29 22:50:39 +0000 | [diff] [blame] | 407 | with self.assertRaises(IOError) as cm: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 408 | ctx.load_verify_locations(WRONGCERT) |
Giampaolo Rodolà | 4a656eb | 2010-08-29 22:50:39 +0000 | [diff] [blame] | 409 | self.assertEqual(cm.exception.errno, errno.ENOENT) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 410 | with self.assertRaisesRegex(ssl.SSLError, "PEM lib"): |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 411 | ctx.load_verify_locations(BADCERT) |
| 412 | ctx.load_verify_locations(CERTFILE, CAPATH) |
| 413 | ctx.load_verify_locations(CERTFILE, capath=BYTES_CAPATH) |
| 414 | |
Victor Stinner | 80f75e6 | 2011-01-29 11:31:20 +0000 | [diff] [blame] | 415 | # Issue #10989: crash if the second argument type is invalid |
| 416 | self.assertRaises(TypeError, ctx.load_verify_locations, None, True) |
| 417 | |
Antoine Pitrou | eb585ad | 2010-10-22 18:24:20 +0000 | [diff] [blame] | 418 | @skip_if_broken_ubuntu_ssl |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 419 | def test_session_stats(self): |
| 420 | for proto in PROTOCOLS: |
| 421 | ctx = ssl.SSLContext(proto) |
| 422 | self.assertEqual(ctx.session_stats(), { |
| 423 | 'number': 0, |
| 424 | 'connect': 0, |
| 425 | 'connect_good': 0, |
| 426 | 'connect_renegotiate': 0, |
| 427 | 'accept': 0, |
| 428 | 'accept_good': 0, |
| 429 | 'accept_renegotiate': 0, |
| 430 | 'hits': 0, |
| 431 | 'misses': 0, |
| 432 | 'timeouts': 0, |
| 433 | 'cache_full': 0, |
| 434 | }) |
| 435 | |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 436 | def test_set_default_verify_paths(self): |
| 437 | # There's not much we can do to test that it acts as expected, |
| 438 | # so just check it doesn't crash or raise an exception. |
| 439 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 440 | ctx.set_default_verify_paths() |
| 441 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 442 | |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 443 | class NetworkedTests(unittest.TestCase): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 444 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 445 | def test_connect(self): |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 446 | with support.transient_internet("svn.python.org"): |
| 447 | s = ssl.wrap_socket(socket.socket(socket.AF_INET), |
| 448 | cert_reqs=ssl.CERT_NONE) |
| 449 | try: |
| 450 | s.connect(("svn.python.org", 443)) |
| 451 | self.assertEqual({}, s.getpeercert()) |
| 452 | finally: |
| 453 | s.close() |
| 454 | |
| 455 | # this should fail because we have no verification certs |
| 456 | s = ssl.wrap_socket(socket.socket(socket.AF_INET), |
| 457 | cert_reqs=ssl.CERT_REQUIRED) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 458 | self.assertRaisesRegex(ssl.SSLError, "certificate verify failed", |
| 459 | s.connect, ("svn.python.org", 443)) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 460 | s.close() |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 461 | |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 462 | # this should succeed because we specify the root cert |
| 463 | s = ssl.wrap_socket(socket.socket(socket.AF_INET), |
| 464 | cert_reqs=ssl.CERT_REQUIRED, |
| 465 | ca_certs=SVN_PYTHON_ORG_ROOT_CERT) |
| 466 | try: |
| 467 | s.connect(("svn.python.org", 443)) |
| 468 | self.assertTrue(s.getpeercert()) |
| 469 | finally: |
| 470 | s.close() |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 471 | |
Antoine Pitrou | 86cbfec | 2011-02-26 23:25:34 +0000 | [diff] [blame] | 472 | def test_connect_ex(self): |
| 473 | # Issue #11326: check connect_ex() implementation |
| 474 | with support.transient_internet("svn.python.org"): |
| 475 | s = ssl.wrap_socket(socket.socket(socket.AF_INET), |
| 476 | cert_reqs=ssl.CERT_REQUIRED, |
| 477 | ca_certs=SVN_PYTHON_ORG_ROOT_CERT) |
| 478 | try: |
| 479 | self.assertEqual(0, s.connect_ex(("svn.python.org", 443))) |
| 480 | self.assertTrue(s.getpeercert()) |
| 481 | finally: |
| 482 | s.close() |
| 483 | |
| 484 | def test_non_blocking_connect_ex(self): |
| 485 | # Issue #11326: non-blocking connect_ex() should allow handshake |
| 486 | # to proceed after the socket gets ready. |
| 487 | with support.transient_internet("svn.python.org"): |
| 488 | s = ssl.wrap_socket(socket.socket(socket.AF_INET), |
| 489 | cert_reqs=ssl.CERT_REQUIRED, |
| 490 | ca_certs=SVN_PYTHON_ORG_ROOT_CERT, |
| 491 | do_handshake_on_connect=False) |
| 492 | try: |
| 493 | s.setblocking(False) |
| 494 | rc = s.connect_ex(('svn.python.org', 443)) |
Antoine Pitrou | d1c9845 | 2011-02-27 15:45:16 +0000 | [diff] [blame] | 495 | # EWOULDBLOCK under Windows, EINPROGRESS elsewhere |
| 496 | self.assertIn(rc, (0, errno.EINPROGRESS, errno.EWOULDBLOCK)) |
Antoine Pitrou | 86cbfec | 2011-02-26 23:25:34 +0000 | [diff] [blame] | 497 | # Wait for connect to finish |
| 498 | select.select([], [s], [], 5.0) |
| 499 | # Non-blocking handshake |
| 500 | while True: |
| 501 | try: |
| 502 | s.do_handshake() |
| 503 | break |
| 504 | except ssl.SSLError as err: |
| 505 | if err.args[0] == ssl.SSL_ERROR_WANT_READ: |
| 506 | select.select([s], [], [], 5.0) |
| 507 | elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE: |
| 508 | select.select([], [s], [], 5.0) |
| 509 | else: |
| 510 | raise |
| 511 | # SSL established |
| 512 | self.assertTrue(s.getpeercert()) |
| 513 | finally: |
| 514 | s.close() |
| 515 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 516 | def test_connect_with_context(self): |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 517 | with support.transient_internet("svn.python.org"): |
| 518 | # Same as test_connect, but with a separately created context |
| 519 | ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
| 520 | s = ctx.wrap_socket(socket.socket(socket.AF_INET)) |
| 521 | s.connect(("svn.python.org", 443)) |
| 522 | try: |
| 523 | self.assertEqual({}, s.getpeercert()) |
| 524 | finally: |
| 525 | s.close() |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 526 | # Same with a server hostname |
| 527 | s = ctx.wrap_socket(socket.socket(socket.AF_INET), |
| 528 | server_hostname="svn.python.org") |
| 529 | if ssl.HAS_SNI: |
| 530 | s.connect(("svn.python.org", 443)) |
| 531 | s.close() |
| 532 | else: |
| 533 | self.assertRaises(ValueError, s.connect, ("svn.python.org", 443)) |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 534 | # This should fail because we have no verification certs |
| 535 | ctx.verify_mode = ssl.CERT_REQUIRED |
| 536 | s = ctx.wrap_socket(socket.socket(socket.AF_INET)) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 537 | self.assertRaisesRegex(ssl.SSLError, "certificate verify failed", |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 538 | s.connect, ("svn.python.org", 443)) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 539 | s.close() |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 540 | # This should succeed because we specify the root cert |
| 541 | ctx.load_verify_locations(SVN_PYTHON_ORG_ROOT_CERT) |
| 542 | s = ctx.wrap_socket(socket.socket(socket.AF_INET)) |
| 543 | s.connect(("svn.python.org", 443)) |
| 544 | try: |
| 545 | cert = s.getpeercert() |
| 546 | self.assertTrue(cert) |
| 547 | finally: |
| 548 | s.close() |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 549 | |
| 550 | def test_connect_capath(self): |
| 551 | # Verify server certificates using the `capath` argument |
Antoine Pitrou | 467f28d | 2010-05-16 19:22:44 +0000 | [diff] [blame] | 552 | # NOTE: the subject hashing algorithm has been changed between |
| 553 | # OpenSSL 0.9.8n and 1.0.0, as a result the capath directory must |
| 554 | # contain both versions of each certificate (same content, different |
Antoine Pitrou | d7e4c1c | 2010-05-17 14:13:10 +0000 | [diff] [blame] | 555 | # filename) for this test to be portable across OpenSSL releases. |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 556 | with support.transient_internet("svn.python.org"): |
| 557 | ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
| 558 | ctx.verify_mode = ssl.CERT_REQUIRED |
| 559 | ctx.load_verify_locations(capath=CAPATH) |
| 560 | s = ctx.wrap_socket(socket.socket(socket.AF_INET)) |
| 561 | s.connect(("svn.python.org", 443)) |
| 562 | try: |
| 563 | cert = s.getpeercert() |
| 564 | self.assertTrue(cert) |
| 565 | finally: |
| 566 | s.close() |
| 567 | # Same with a bytes `capath` argument |
| 568 | ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
| 569 | ctx.verify_mode = ssl.CERT_REQUIRED |
| 570 | ctx.load_verify_locations(capath=BYTES_CAPATH) |
| 571 | s = ctx.wrap_socket(socket.socket(socket.AF_INET)) |
| 572 | s.connect(("svn.python.org", 443)) |
| 573 | try: |
| 574 | cert = s.getpeercert() |
| 575 | self.assertTrue(cert) |
| 576 | finally: |
| 577 | s.close() |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 578 | |
Antoine Pitrou | e322024 | 2010-04-24 11:13:53 +0000 | [diff] [blame] | 579 | @unittest.skipIf(os.name == "nt", "Can't use a socket as a file under Windows") |
| 580 | def test_makefile_close(self): |
| 581 | # Issue #5238: creating a file-like object with makefile() shouldn't |
| 582 | # delay closing the underlying "real socket" (here tested with its |
| 583 | # file descriptor, hence skipping the test under Windows). |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 584 | with support.transient_internet("svn.python.org"): |
| 585 | ss = ssl.wrap_socket(socket.socket(socket.AF_INET)) |
| 586 | ss.connect(("svn.python.org", 443)) |
| 587 | fd = ss.fileno() |
| 588 | f = ss.makefile() |
| 589 | f.close() |
| 590 | # The fd is still open |
Antoine Pitrou | e322024 | 2010-04-24 11:13:53 +0000 | [diff] [blame] | 591 | os.read(fd, 0) |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 592 | # Closing the SSL socket should close the fd too |
| 593 | ss.close() |
| 594 | gc.collect() |
| 595 | with self.assertRaises(OSError) as e: |
| 596 | os.read(fd, 0) |
| 597 | self.assertEqual(e.exception.errno, errno.EBADF) |
Antoine Pitrou | e322024 | 2010-04-24 11:13:53 +0000 | [diff] [blame] | 598 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 599 | def test_non_blocking_handshake(self): |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 600 | with support.transient_internet("svn.python.org"): |
| 601 | s = socket.socket(socket.AF_INET) |
| 602 | s.connect(("svn.python.org", 443)) |
| 603 | s.setblocking(False) |
| 604 | s = ssl.wrap_socket(s, |
| 605 | cert_reqs=ssl.CERT_NONE, |
| 606 | do_handshake_on_connect=False) |
| 607 | count = 0 |
| 608 | while True: |
| 609 | try: |
| 610 | count += 1 |
| 611 | s.do_handshake() |
| 612 | break |
| 613 | except ssl.SSLError as err: |
| 614 | if err.args[0] == ssl.SSL_ERROR_WANT_READ: |
| 615 | select.select([s], [], []) |
| 616 | elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE: |
| 617 | select.select([], [s], []) |
| 618 | else: |
| 619 | raise |
| 620 | s.close() |
| 621 | if support.verbose: |
| 622 | sys.stdout.write("\nNeeded %d calls to do_handshake() to establish session.\n" % count) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 623 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 624 | def test_get_server_certificate(self): |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 625 | with support.transient_internet("svn.python.org"): |
| 626 | pem = ssl.get_server_certificate(("svn.python.org", 443)) |
| 627 | if not pem: |
| 628 | self.fail("No server certificate on svn.python.org:443!") |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 629 | |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 630 | try: |
| 631 | pem = ssl.get_server_certificate(("svn.python.org", 443), ca_certs=CERTFILE) |
| 632 | except ssl.SSLError as x: |
| 633 | #should fail |
| 634 | if support.verbose: |
| 635 | sys.stdout.write("%s\n" % x) |
| 636 | else: |
| 637 | self.fail("Got server certificate %s for svn.python.org!" % pem) |
| 638 | |
| 639 | pem = ssl.get_server_certificate(("svn.python.org", 443), ca_certs=SVN_PYTHON_ORG_ROOT_CERT) |
| 640 | if not pem: |
| 641 | self.fail("No server certificate on svn.python.org:443!") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 642 | if support.verbose: |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 643 | sys.stdout.write("\nVerified certificate for svn.python.org:443 is\n%s\n" % pem) |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | f4c7bad | 2010-08-15 23:02:22 +0000 | [diff] [blame] | 645 | def test_ciphers(self): |
| 646 | remote = ("svn.python.org", 443) |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 647 | with support.transient_internet(remote[0]): |
Antoine Pitrou | f4c7bad | 2010-08-15 23:02:22 +0000 | [diff] [blame] | 648 | s = ssl.wrap_socket(socket.socket(socket.AF_INET), |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 649 | cert_reqs=ssl.CERT_NONE, ciphers="ALL") |
Antoine Pitrou | f4c7bad | 2010-08-15 23:02:22 +0000 | [diff] [blame] | 650 | s.connect(remote) |
Antoine Pitrou | 350c722 | 2010-09-09 13:31:46 +0000 | [diff] [blame] | 651 | s = ssl.wrap_socket(socket.socket(socket.AF_INET), |
| 652 | cert_reqs=ssl.CERT_NONE, ciphers="DEFAULT") |
| 653 | s.connect(remote) |
| 654 | # Error checking can happen at instantiation or when connecting |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 655 | with self.assertRaisesRegex(ssl.SSLError, "No cipher can be selected"): |
Antoine Pitrou | d2eca37 | 2010-10-29 23:41:37 +0000 | [diff] [blame] | 656 | with socket.socket(socket.AF_INET) as sock: |
| 657 | s = ssl.wrap_socket(sock, |
| 658 | cert_reqs=ssl.CERT_NONE, ciphers="^$:,;?*'dorothyx") |
| 659 | s.connect(remote) |
Antoine Pitrou | f4c7bad | 2010-08-15 23:02:22 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | fec12ff | 2010-04-21 19:46:23 +0000 | [diff] [blame] | 661 | def test_algorithms(self): |
| 662 | # Issue #8484: all algorithms should be available when verifying a |
| 663 | # certificate. |
Antoine Pitrou | 29619b2 | 2010-04-22 18:43:31 +0000 | [diff] [blame] | 664 | # SHA256 was added in OpenSSL 0.9.8 |
| 665 | if ssl.OPENSSL_VERSION_INFO < (0, 9, 8, 0, 15): |
| 666 | self.skipTest("SHA256 not available on %r" % ssl.OPENSSL_VERSION) |
Victor Stinner | f332abb | 2011-01-08 03:16:05 +0000 | [diff] [blame] | 667 | # https://sha2.hboeck.de/ was used until 2011-01-08 (no route to host) |
| 668 | remote = ("sha256.tbs-internet.com", 443) |
Antoine Pitrou | fec12ff | 2010-04-21 19:46:23 +0000 | [diff] [blame] | 669 | sha256_cert = os.path.join(os.path.dirname(__file__), "sha256.pem") |
Antoine Pitrou | 160fd93 | 2011-01-08 10:23:29 +0000 | [diff] [blame] | 670 | with support.transient_internet("sha256.tbs-internet.com"): |
Antoine Pitrou | a88c83c | 2010-09-07 20:42:19 +0000 | [diff] [blame] | 671 | s = ssl.wrap_socket(socket.socket(socket.AF_INET), |
| 672 | cert_reqs=ssl.CERT_REQUIRED, |
| 673 | ca_certs=sha256_cert,) |
Antoine Pitrou | fec12ff | 2010-04-21 19:46:23 +0000 | [diff] [blame] | 674 | try: |
| 675 | s.connect(remote) |
| 676 | if support.verbose: |
| 677 | sys.stdout.write("\nCipher with %r is %r\n" % |
| 678 | (remote, s.cipher())) |
| 679 | sys.stdout.write("Certificate is:\n%s\n" % |
| 680 | pprint.pformat(s.getpeercert())) |
| 681 | finally: |
| 682 | s.close() |
| 683 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 684 | |
| 685 | try: |
| 686 | import threading |
| 687 | except ImportError: |
| 688 | _have_threads = False |
| 689 | else: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 690 | _have_threads = True |
| 691 | |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 692 | from test.ssl_servers import make_https_server |
| 693 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 694 | class ThreadedEchoServer(threading.Thread): |
| 695 | |
| 696 | class ConnectionHandler(threading.Thread): |
| 697 | |
| 698 | """A mildly complicated class, because we want it to work both |
| 699 | with and without the SSL wrapper around the socket connection, so |
| 700 | that we can test the STARTTLS functionality.""" |
| 701 | |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 702 | def __init__(self, server, connsock, addr): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 703 | self.server = server |
| 704 | self.running = False |
| 705 | self.sock = connsock |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 706 | self.addr = addr |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 707 | self.sock.setblocking(1) |
| 708 | self.sslconn = None |
| 709 | threading.Thread.__init__(self) |
Benjamin Peterson | 4171da5 | 2008-08-18 21:11:09 +0000 | [diff] [blame] | 710 | self.daemon = True |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 711 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 712 | def wrap_conn(self): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 713 | try: |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 714 | self.sslconn = self.server.context.wrap_socket( |
| 715 | self.sock, server_side=True) |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 716 | except ssl.SSLError: |
| 717 | # XXX Various errors can have happened here, for example |
| 718 | # a mismatching protocol version, an invalid certificate, |
| 719 | # or a low-level bug. This should be made more discriminating. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 720 | if self.server.chatty: |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 721 | handle_error("\n server: bad connection attempt from " + repr(self.addr) + ":\n") |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 722 | self.running = False |
| 723 | self.server.stop() |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 724 | self.close() |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 725 | return False |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 726 | else: |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 727 | if self.server.context.verify_mode == ssl.CERT_REQUIRED: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 728 | cert = self.sslconn.getpeercert() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 729 | if support.verbose and self.server.chatty: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 730 | sys.stdout.write(" client cert is " + pprint.pformat(cert) + "\n") |
| 731 | cert_binary = self.sslconn.getpeercert(True) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 732 | if support.verbose and self.server.chatty: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 733 | sys.stdout.write(" cert binary is " + str(len(cert_binary)) + " bytes\n") |
| 734 | cipher = self.sslconn.cipher() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 735 | if support.verbose and self.server.chatty: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 736 | sys.stdout.write(" server: connection cipher is now " + str(cipher) + "\n") |
| 737 | return True |
| 738 | |
| 739 | def read(self): |
| 740 | if self.sslconn: |
| 741 | return self.sslconn.read() |
| 742 | else: |
| 743 | return self.sock.recv(1024) |
| 744 | |
| 745 | def write(self, bytes): |
| 746 | if self.sslconn: |
| 747 | return self.sslconn.write(bytes) |
| 748 | else: |
| 749 | return self.sock.send(bytes) |
| 750 | |
| 751 | def close(self): |
| 752 | if self.sslconn: |
| 753 | self.sslconn.close() |
| 754 | else: |
| 755 | self.sock.close() |
| 756 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 757 | def run(self): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 758 | self.running = True |
| 759 | if not self.server.starttls_server: |
| 760 | if not self.wrap_conn(): |
| 761 | return |
| 762 | while self.running: |
| 763 | try: |
| 764 | msg = self.read() |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 765 | stripped = msg.strip() |
| 766 | if not stripped: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 767 | # eof, so quit this handler |
| 768 | self.running = False |
| 769 | self.close() |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 770 | elif stripped == b'over': |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 771 | if support.verbose and self.server.connectionchatty: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 772 | sys.stdout.write(" server: client closed connection\n") |
| 773 | self.close() |
| 774 | return |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 775 | elif (self.server.starttls_server and |
Antoine Pitrou | 764b878 | 2010-04-28 22:57:15 +0000 | [diff] [blame] | 776 | stripped == b'STARTTLS'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 777 | if support.verbose and self.server.connectionchatty: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 778 | sys.stdout.write(" server: read STARTTLS from client, sending OK...\n") |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 779 | self.write(b"OK\n") |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 780 | if not self.wrap_conn(): |
| 781 | return |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 782 | elif (self.server.starttls_server and self.sslconn |
Antoine Pitrou | 764b878 | 2010-04-28 22:57:15 +0000 | [diff] [blame] | 783 | and stripped == b'ENDTLS'): |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 784 | if support.verbose and self.server.connectionchatty: |
| 785 | sys.stdout.write(" server: read ENDTLS from client, sending OK...\n") |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 786 | self.write(b"OK\n") |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 787 | self.sock = self.sslconn.unwrap() |
| 788 | self.sslconn = None |
| 789 | if support.verbose and self.server.connectionchatty: |
| 790 | sys.stdout.write(" server: connection is now unencrypted...\n") |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 791 | else: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 792 | if (support.verbose and |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 793 | self.server.connectionchatty): |
| 794 | ctype = (self.sslconn and "encrypted") or "unencrypted" |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 795 | sys.stdout.write(" server: read %r (%s), sending back %r (%s)...\n" |
| 796 | % (msg, ctype, msg.lower(), ctype)) |
| 797 | self.write(msg.lower()) |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 798 | except socket.error: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 799 | if self.server.chatty: |
| 800 | handle_error("Test server failure:\n") |
| 801 | self.close() |
| 802 | self.running = False |
| 803 | # normally, we'd just stop here, but for the test |
| 804 | # harness, we want to stop the server |
| 805 | self.server.stop() |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 806 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 807 | def __init__(self, certificate=None, ssl_version=None, |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 808 | certreqs=None, cacerts=None, |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 809 | chatty=True, connectionchatty=False, starttls_server=False, |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 810 | ciphers=None, context=None): |
| 811 | if context: |
| 812 | self.context = context |
| 813 | else: |
| 814 | self.context = ssl.SSLContext(ssl_version |
| 815 | if ssl_version is not None |
| 816 | else ssl.PROTOCOL_TLSv1) |
| 817 | self.context.verify_mode = (certreqs if certreqs is not None |
| 818 | else ssl.CERT_NONE) |
| 819 | if cacerts: |
| 820 | self.context.load_verify_locations(cacerts) |
| 821 | if certificate: |
| 822 | self.context.load_cert_chain(certificate) |
| 823 | if ciphers: |
| 824 | self.context.set_ciphers(ciphers) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 825 | self.chatty = chatty |
| 826 | self.connectionchatty = connectionchatty |
| 827 | self.starttls_server = starttls_server |
| 828 | self.sock = socket.socket() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 829 | self.port = support.bind_port(self.sock) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 830 | self.flag = None |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 831 | self.active = False |
| 832 | threading.Thread.__init__(self) |
Benjamin Peterson | 4171da5 | 2008-08-18 21:11:09 +0000 | [diff] [blame] | 833 | self.daemon = True |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 834 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 835 | def start(self, flag=None): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 836 | self.flag = flag |
| 837 | threading.Thread.start(self) |
| 838 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 839 | def run(self): |
Antoine Pitrou | af7c602 | 2010-04-27 09:56:02 +0000 | [diff] [blame] | 840 | self.sock.settimeout(0.05) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 841 | self.sock.listen(5) |
| 842 | self.active = True |
| 843 | if self.flag: |
| 844 | # signal an event |
| 845 | self.flag.set() |
| 846 | while self.active: |
| 847 | try: |
| 848 | newconn, connaddr = self.sock.accept() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 849 | if support.verbose and self.chatty: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 850 | sys.stdout.write(' server: new connection from ' |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 851 | + repr(connaddr) + '\n') |
| 852 | handler = self.ConnectionHandler(self, newconn, connaddr) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 853 | handler.start() |
| 854 | except socket.timeout: |
| 855 | pass |
| 856 | except KeyboardInterrupt: |
| 857 | self.stop() |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 858 | self.sock.close() |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 859 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 860 | def stop(self): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 861 | self.active = False |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 862 | |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 863 | class AsyncoreEchoServer(threading.Thread): |
| 864 | |
| 865 | # this one's based on asyncore.dispatcher |
| 866 | |
| 867 | class EchoServer (asyncore.dispatcher): |
| 868 | |
| 869 | class ConnectionHandler (asyncore.dispatcher_with_send): |
| 870 | |
| 871 | def __init__(self, conn, certfile): |
| 872 | self.socket = ssl.wrap_socket(conn, server_side=True, |
| 873 | certfile=certfile, |
| 874 | do_handshake_on_connect=False) |
| 875 | asyncore.dispatcher_with_send.__init__(self, self.socket) |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 876 | self._ssl_accepting = True |
| 877 | self._do_ssl_handshake() |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 878 | |
| 879 | def readable(self): |
| 880 | if isinstance(self.socket, ssl.SSLSocket): |
| 881 | while self.socket.pending() > 0: |
| 882 | self.handle_read_event() |
| 883 | return True |
| 884 | |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 885 | def _do_ssl_handshake(self): |
| 886 | try: |
| 887 | self.socket.do_handshake() |
| 888 | except ssl.SSLError as err: |
| 889 | if err.args[0] in (ssl.SSL_ERROR_WANT_READ, |
| 890 | ssl.SSL_ERROR_WANT_WRITE): |
| 891 | return |
| 892 | elif err.args[0] == ssl.SSL_ERROR_EOF: |
| 893 | return self.handle_close() |
| 894 | raise |
| 895 | except socket.error as err: |
| 896 | if err.args[0] == errno.ECONNABORTED: |
| 897 | return self.handle_close() |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 898 | else: |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 899 | self._ssl_accepting = False |
| 900 | |
| 901 | def handle_read(self): |
| 902 | if self._ssl_accepting: |
| 903 | self._do_ssl_handshake() |
| 904 | else: |
| 905 | data = self.recv(1024) |
| 906 | if support.verbose: |
| 907 | sys.stdout.write(" server: read %s from client\n" % repr(data)) |
| 908 | if not data: |
| 909 | self.close() |
| 910 | else: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 911 | self.send(data.lower()) |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 912 | |
| 913 | def handle_close(self): |
Bill Janssen | 2f5799b | 2008-06-29 00:08:12 +0000 | [diff] [blame] | 914 | self.close() |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 915 | if support.verbose: |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 916 | sys.stdout.write(" server: closed connection %s\n" % self.socket) |
| 917 | |
| 918 | def handle_error(self): |
| 919 | raise |
| 920 | |
Antoine Pitrou | 773b5db | 2010-04-27 08:53:36 +0000 | [diff] [blame] | 921 | def __init__(self, certfile): |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 922 | self.certfile = certfile |
Antoine Pitrou | 773b5db | 2010-04-27 08:53:36 +0000 | [diff] [blame] | 923 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 924 | self.port = support.bind_port(sock, '') |
| 925 | asyncore.dispatcher.__init__(self, sock) |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 926 | self.listen(5) |
| 927 | |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 928 | def handle_accepted(self, sock_obj, addr): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 929 | if support.verbose: |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 930 | sys.stdout.write(" server: new connection from %s:%s\n" %addr) |
| 931 | self.ConnectionHandler(sock_obj, self.certfile) |
| 932 | |
| 933 | def handle_error(self): |
| 934 | raise |
| 935 | |
Trent Nelson | 7852000 | 2008-04-10 20:54:35 +0000 | [diff] [blame] | 936 | def __init__(self, certfile): |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 937 | self.flag = None |
| 938 | self.active = False |
Antoine Pitrou | 773b5db | 2010-04-27 08:53:36 +0000 | [diff] [blame] | 939 | self.server = self.EchoServer(certfile) |
| 940 | self.port = self.server.port |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 941 | threading.Thread.__init__(self) |
Benjamin Peterson | 4171da5 | 2008-08-18 21:11:09 +0000 | [diff] [blame] | 942 | self.daemon = True |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 943 | |
| 944 | def __str__(self): |
| 945 | return "<%s %s>" % (self.__class__.__name__, self.server) |
| 946 | |
| 947 | def start (self, flag=None): |
| 948 | self.flag = flag |
| 949 | threading.Thread.start(self) |
| 950 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 951 | def run(self): |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 952 | self.active = True |
| 953 | if self.flag: |
| 954 | self.flag.set() |
| 955 | while self.active: |
| 956 | try: |
| 957 | asyncore.loop(1) |
| 958 | except: |
| 959 | pass |
| 960 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 961 | def stop(self): |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 962 | self.active = False |
| 963 | self.server.close() |
| 964 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 965 | def bad_cert_test(certfile): |
| 966 | """ |
| 967 | Launch a server with CERT_REQUIRED, and check that trying to |
| 968 | connect to it with the given client certificate fails. |
| 969 | """ |
Trent Nelson | 7852000 | 2008-04-10 20:54:35 +0000 | [diff] [blame] | 970 | server = ThreadedEchoServer(CERTFILE, |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 971 | certreqs=ssl.CERT_REQUIRED, |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 972 | cacerts=CERTFILE, chatty=False, |
| 973 | connectionchatty=False) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 974 | flag = threading.Event() |
| 975 | server.start(flag) |
| 976 | # wait for it to start |
| 977 | flag.wait() |
| 978 | # try to connect |
| 979 | try: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 980 | try: |
Antoine Pitrou | d2eca37 | 2010-10-29 23:41:37 +0000 | [diff] [blame] | 981 | with socket.socket() as sock: |
| 982 | s = ssl.wrap_socket(sock, |
| 983 | certfile=certfile, |
| 984 | ssl_version=ssl.PROTOCOL_TLSv1) |
| 985 | s.connect((HOST, server.port)) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 986 | except ssl.SSLError as x: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 987 | if support.verbose: |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 988 | sys.stdout.write("\nSSLError is %s\n" % x.args[1]) |
Antoine Pitrou | 05830aa | 2010-04-27 13:15:18 +0000 | [diff] [blame] | 989 | except socket.error as x: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 990 | if support.verbose: |
Georg Brandl | b75b639 | 2010-10-24 14:20:22 +0000 | [diff] [blame] | 991 | sys.stdout.write("\nsocket.error is %s\n" % x.args[1]) |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 992 | except IOError as x: |
Giampaolo Rodolà | cd9dfb9 | 2010-08-29 20:56:56 +0000 | [diff] [blame] | 993 | if x.errno != errno.ENOENT: |
| 994 | raise |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 995 | if support.verbose: |
Giampaolo Rodolà | cd9dfb9 | 2010-08-29 20:56:56 +0000 | [diff] [blame] | 996 | sys.stdout.write("\IOError is %s\n" % str(x)) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 997 | else: |
Antoine Pitrou | d75b2a9 | 2010-05-06 14:15:10 +0000 | [diff] [blame] | 998 | raise AssertionError("Use of invalid cert should have failed!") |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 999 | finally: |
| 1000 | server.stop() |
| 1001 | server.join() |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1002 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1003 | def server_params_test(client_context, server_context, indata=b"FOO\n", |
| 1004 | chatty=True, connectionchatty=False): |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1005 | """ |
| 1006 | Launch a server, connect a client to it and try various reads |
| 1007 | and writes. |
| 1008 | """ |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1009 | server = ThreadedEchoServer(context=server_context, |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1010 | chatty=chatty, |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1011 | connectionchatty=False) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1012 | flag = threading.Event() |
| 1013 | server.start(flag) |
| 1014 | # wait for it to start |
| 1015 | flag.wait() |
| 1016 | # try to connect |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1017 | try: |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1018 | s = client_context.wrap_socket(socket.socket()) |
Trent Nelson | 7852000 | 2008-04-10 20:54:35 +0000 | [diff] [blame] | 1019 | s.connect((HOST, server.port)) |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1020 | for arg in [indata, bytearray(indata), memoryview(indata)]: |
Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 1021 | if connectionchatty: |
| 1022 | if support.verbose: |
| 1023 | sys.stdout.write( |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1024 | " client: sending %r...\n" % indata) |
Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 1025 | s.write(arg) |
| 1026 | outdata = s.read() |
| 1027 | if connectionchatty: |
| 1028 | if support.verbose: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1029 | sys.stdout.write(" client: read %r\n" % outdata) |
Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 1030 | if outdata != indata.lower(): |
Antoine Pitrou | d75b2a9 | 2010-05-06 14:15:10 +0000 | [diff] [blame] | 1031 | raise AssertionError( |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1032 | "bad data <<%r>> (%d) received; expected <<%r>> (%d)\n" |
| 1033 | % (outdata[:20], len(outdata), |
| 1034 | indata[:20].lower(), len(indata))) |
| 1035 | s.write(b"over\n") |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1036 | if connectionchatty: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1037 | if support.verbose: |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1038 | sys.stdout.write(" client: closing connection.\n") |
| 1039 | s.close() |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1040 | finally: |
| 1041 | server.stop() |
| 1042 | server.join() |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1043 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1044 | def try_protocol_combo(server_protocol, client_protocol, expect_success, |
| 1045 | certsreqs=None, server_options=0, client_options=0): |
Benjamin Peterson | 2a691a8 | 2008-03-31 01:51:45 +0000 | [diff] [blame] | 1046 | if certsreqs is None: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1047 | certsreqs = ssl.CERT_NONE |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1048 | certtype = { |
| 1049 | ssl.CERT_NONE: "CERT_NONE", |
| 1050 | ssl.CERT_OPTIONAL: "CERT_OPTIONAL", |
| 1051 | ssl.CERT_REQUIRED: "CERT_REQUIRED", |
| 1052 | }[certsreqs] |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1053 | if support.verbose: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1054 | formatstr = (expect_success and " %s->%s %s\n") or " {%s->%s} %s\n" |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1055 | sys.stdout.write(formatstr % |
| 1056 | (ssl.get_protocol_name(client_protocol), |
| 1057 | ssl.get_protocol_name(server_protocol), |
| 1058 | certtype)) |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1059 | client_context = ssl.SSLContext(client_protocol) |
| 1060 | client_context.options = ssl.OP_ALL | client_options |
| 1061 | server_context = ssl.SSLContext(server_protocol) |
| 1062 | server_context.options = ssl.OP_ALL | server_options |
| 1063 | for ctx in (client_context, server_context): |
| 1064 | ctx.verify_mode = certsreqs |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 1065 | # NOTE: we must enable "ALL" ciphers, otherwise an SSLv23 client |
| 1066 | # will send an SSLv3 hello (rather than SSLv2) starting from |
| 1067 | # OpenSSL 1.0.0 (see issue #8322). |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1068 | ctx.set_ciphers("ALL") |
| 1069 | ctx.load_cert_chain(CERTFILE) |
| 1070 | ctx.load_verify_locations(CERTFILE) |
| 1071 | try: |
| 1072 | server_params_test(client_context, server_context, |
| 1073 | chatty=False, connectionchatty=False) |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1074 | # Protocol mismatch can result in either an SSLError, or a |
| 1075 | # "Connection reset by peer" error. |
| 1076 | except ssl.SSLError: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1077 | if expect_success: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1078 | raise |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1079 | except socket.error as e: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1080 | if expect_success or e.errno != errno.ECONNRESET: |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1081 | raise |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1082 | else: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1083 | if not expect_success: |
Antoine Pitrou | d75b2a9 | 2010-05-06 14:15:10 +0000 | [diff] [blame] | 1084 | raise AssertionError( |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1085 | "Client protocol %s succeeded with server protocol %s!" |
| 1086 | % (ssl.get_protocol_name(client_protocol), |
| 1087 | ssl.get_protocol_name(server_protocol))) |
| 1088 | |
| 1089 | |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1090 | class ThreadedTests(unittest.TestCase): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1091 | |
Antoine Pitrou | 23df483 | 2010-08-04 17:14:06 +0000 | [diff] [blame] | 1092 | @skip_if_broken_ubuntu_ssl |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1093 | def test_echo(self): |
| 1094 | """Basic test of an SSL client connecting to a server""" |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1095 | if support.verbose: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1096 | sys.stdout.write("\n") |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1097 | for protocol in PROTOCOLS: |
| 1098 | context = ssl.SSLContext(protocol) |
| 1099 | context.load_cert_chain(CERTFILE) |
| 1100 | server_params_test(context, context, |
| 1101 | chatty=True, connectionchatty=True) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1102 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1103 | def test_getpeercert(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1104 | if support.verbose: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1105 | sys.stdout.write("\n") |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1106 | context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
| 1107 | context.verify_mode = ssl.CERT_REQUIRED |
| 1108 | context.load_verify_locations(CERTFILE) |
| 1109 | context.load_cert_chain(CERTFILE) |
| 1110 | server = ThreadedEchoServer(context=context, chatty=False) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1111 | flag = threading.Event() |
| 1112 | server.start(flag) |
| 1113 | # wait for it to start |
| 1114 | flag.wait() |
| 1115 | # try to connect |
| 1116 | try: |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1117 | s = context.wrap_socket(socket.socket()) |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1118 | s.connect((HOST, server.port)) |
| 1119 | cert = s.getpeercert() |
| 1120 | self.assertTrue(cert, "Can't get peer certificate.") |
| 1121 | cipher = s.cipher() |
| 1122 | if support.verbose: |
| 1123 | sys.stdout.write(pprint.pformat(cert) + '\n') |
| 1124 | sys.stdout.write("Connection cipher is " + str(cipher) + '.\n') |
| 1125 | if 'subject' not in cert: |
| 1126 | self.fail("No subject field in certificate: %s." % |
| 1127 | pprint.pformat(cert)) |
| 1128 | if ((('organizationName', 'Python Software Foundation'),) |
| 1129 | not in cert['subject']): |
| 1130 | self.fail( |
| 1131 | "Missing or invalid 'organizationName' field in certificate subject; " |
| 1132 | "should be 'Python Software Foundation'.") |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1133 | self.assertIn('notBefore', cert) |
| 1134 | self.assertIn('notAfter', cert) |
| 1135 | before = ssl.cert_time_to_seconds(cert['notBefore']) |
| 1136 | after = ssl.cert_time_to_seconds(cert['notAfter']) |
| 1137 | self.assertLess(before, after) |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1138 | s.close() |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1139 | finally: |
| 1140 | server.stop() |
| 1141 | server.join() |
| 1142 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1143 | def test_empty_cert(self): |
| 1144 | """Connecting with an empty cert file""" |
| 1145 | bad_cert_test(os.path.join(os.path.dirname(__file__) or os.curdir, |
| 1146 | "nullcert.pem")) |
| 1147 | def test_malformed_cert(self): |
| 1148 | """Connecting with a badly formatted certificate (syntax error)""" |
| 1149 | bad_cert_test(os.path.join(os.path.dirname(__file__) or os.curdir, |
| 1150 | "badcert.pem")) |
| 1151 | def test_nonexisting_cert(self): |
| 1152 | """Connecting with a non-existing cert file""" |
| 1153 | bad_cert_test(os.path.join(os.path.dirname(__file__) or os.curdir, |
| 1154 | "wrongcert.pem")) |
| 1155 | def test_malformed_key(self): |
| 1156 | """Connecting with a badly formatted key (syntax error)""" |
| 1157 | bad_cert_test(os.path.join(os.path.dirname(__file__) or os.curdir, |
| 1158 | "badkey.pem")) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1159 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1160 | def test_rude_shutdown(self): |
| 1161 | """A brutal shutdown of an SSL server should raise an IOError |
| 1162 | in the client when attempting handshake. |
| 1163 | """ |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1164 | listener_ready = threading.Event() |
| 1165 | listener_gone = threading.Event() |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1166 | |
Antoine Pitrou | 773b5db | 2010-04-27 08:53:36 +0000 | [diff] [blame] | 1167 | s = socket.socket() |
| 1168 | port = support.bind_port(s, HOST) |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1169 | |
Antoine Pitrou | 773b5db | 2010-04-27 08:53:36 +0000 | [diff] [blame] | 1170 | # `listener` runs in a thread. It sits in an accept() until |
| 1171 | # the main thread connects. Then it rudely closes the socket, |
| 1172 | # and sets Event `listener_gone` to let the main thread know |
| 1173 | # the socket is gone. |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1174 | def listener(): |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1175 | s.listen(5) |
| 1176 | listener_ready.set() |
Antoine Pitrou | d2eca37 | 2010-10-29 23:41:37 +0000 | [diff] [blame] | 1177 | newsock, addr = s.accept() |
| 1178 | newsock.close() |
Antoine Pitrou | 773b5db | 2010-04-27 08:53:36 +0000 | [diff] [blame] | 1179 | s.close() |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1180 | listener_gone.set() |
| 1181 | |
| 1182 | def connector(): |
| 1183 | listener_ready.wait() |
Antoine Pitrou | d2eca37 | 2010-10-29 23:41:37 +0000 | [diff] [blame] | 1184 | with socket.socket() as c: |
| 1185 | c.connect((HOST, port)) |
| 1186 | listener_gone.wait() |
| 1187 | try: |
| 1188 | ssl_sock = ssl.wrap_socket(c) |
| 1189 | except IOError: |
| 1190 | pass |
| 1191 | else: |
| 1192 | self.fail('connecting to closed SSL socket should have failed') |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1193 | |
| 1194 | t = threading.Thread(target=listener) |
| 1195 | t.start() |
Antoine Pitrou | 773b5db | 2010-04-27 08:53:36 +0000 | [diff] [blame] | 1196 | try: |
| 1197 | connector() |
| 1198 | finally: |
| 1199 | t.join() |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1200 | |
Antoine Pitrou | 23df483 | 2010-08-04 17:14:06 +0000 | [diff] [blame] | 1201 | @skip_if_broken_ubuntu_ssl |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1202 | def test_protocol_sslv2(self): |
| 1203 | """Connecting to an SSLv2 server with various client options""" |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1204 | if support.verbose: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1205 | sys.stdout.write("\n") |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1206 | try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv2, True) |
| 1207 | try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv2, True, ssl.CERT_OPTIONAL) |
| 1208 | try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv2, True, ssl.CERT_REQUIRED) |
| 1209 | try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv23, True) |
| 1210 | try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv3, False) |
| 1211 | try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_TLSv1, False) |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1212 | # SSLv23 client with specific SSL options |
| 1213 | if no_sslv2_implies_sslv3_hello(): |
| 1214 | # No SSLv2 => client will use an SSLv3 hello on recent OpenSSLs |
| 1215 | try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv23, False, |
| 1216 | client_options=ssl.OP_NO_SSLv2) |
| 1217 | try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv23, True, |
| 1218 | client_options=ssl.OP_NO_SSLv3) |
| 1219 | try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv23, True, |
| 1220 | client_options=ssl.OP_NO_TLSv1) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1221 | |
Antoine Pitrou | 23df483 | 2010-08-04 17:14:06 +0000 | [diff] [blame] | 1222 | @skip_if_broken_ubuntu_ssl |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1223 | def test_protocol_sslv23(self): |
| 1224 | """Connecting to an SSLv23 server with various client options""" |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1225 | if support.verbose: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1226 | sys.stdout.write("\n") |
| 1227 | try: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1228 | try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv2, True) |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1229 | except (ssl.SSLError, socket.error) as x: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1230 | # this fails on some older versions of OpenSSL (0.9.7l, for instance) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1231 | if support.verbose: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1232 | sys.stdout.write( |
| 1233 | " SSL2 client to SSL23 server test unexpectedly failed:\n %s\n" |
| 1234 | % str(x)) |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1235 | try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3, True) |
| 1236 | try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv23, True) |
| 1237 | try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1, True) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1238 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1239 | try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3, True, ssl.CERT_OPTIONAL) |
| 1240 | try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv23, True, ssl.CERT_OPTIONAL) |
| 1241 | try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1, True, ssl.CERT_OPTIONAL) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1242 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1243 | try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3, True, ssl.CERT_REQUIRED) |
| 1244 | try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv23, True, ssl.CERT_REQUIRED) |
| 1245 | try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1, True, ssl.CERT_REQUIRED) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1246 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1247 | # Server with specific SSL options |
| 1248 | try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3, False, |
| 1249 | server_options=ssl.OP_NO_SSLv3) |
| 1250 | # Will choose TLSv1 |
| 1251 | try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv23, True, |
| 1252 | server_options=ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3) |
| 1253 | try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1, False, |
| 1254 | server_options=ssl.OP_NO_TLSv1) |
| 1255 | |
| 1256 | |
Antoine Pitrou | 23df483 | 2010-08-04 17:14:06 +0000 | [diff] [blame] | 1257 | @skip_if_broken_ubuntu_ssl |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1258 | def test_protocol_sslv3(self): |
| 1259 | """Connecting to an SSLv3 server with various client options""" |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1260 | if support.verbose: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1261 | sys.stdout.write("\n") |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1262 | try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True) |
| 1263 | try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True, ssl.CERT_OPTIONAL) |
| 1264 | try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True, ssl.CERT_REQUIRED) |
| 1265 | try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv2, False) |
| 1266 | try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv23, False) |
| 1267 | try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_TLSv1, False) |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1268 | if no_sslv2_implies_sslv3_hello(): |
| 1269 | # No SSLv2 => client will use an SSLv3 hello on recent OpenSSLs |
| 1270 | try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv23, True, |
| 1271 | client_options=ssl.OP_NO_SSLv2) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1272 | |
Antoine Pitrou | 23df483 | 2010-08-04 17:14:06 +0000 | [diff] [blame] | 1273 | @skip_if_broken_ubuntu_ssl |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1274 | def test_protocol_tlsv1(self): |
| 1275 | """Connecting to a TLSv1 server with various client options""" |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1276 | if support.verbose: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1277 | sys.stdout.write("\n") |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1278 | try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True) |
| 1279 | try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True, ssl.CERT_OPTIONAL) |
| 1280 | try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True, ssl.CERT_REQUIRED) |
| 1281 | try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv2, False) |
| 1282 | try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv3, False) |
| 1283 | try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv23, False) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1284 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1285 | def test_starttls(self): |
| 1286 | """Switching from clear text to encrypted and back again.""" |
| 1287 | msgs = (b"msg 1", b"MSG 2", b"STARTTLS", b"MSG 3", b"msg 4", b"ENDTLS", b"msg 5", b"msg 6") |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1288 | |
Trent Nelson | 7852000 | 2008-04-10 20:54:35 +0000 | [diff] [blame] | 1289 | server = ThreadedEchoServer(CERTFILE, |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1290 | ssl_version=ssl.PROTOCOL_TLSv1, |
| 1291 | starttls_server=True, |
| 1292 | chatty=True, |
| 1293 | connectionchatty=True) |
| 1294 | flag = threading.Event() |
| 1295 | server.start(flag) |
| 1296 | # wait for it to start |
| 1297 | flag.wait() |
| 1298 | # try to connect |
| 1299 | wrapped = False |
| 1300 | try: |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1301 | s = socket.socket() |
| 1302 | s.setblocking(1) |
| 1303 | s.connect((HOST, server.port)) |
| 1304 | if support.verbose: |
| 1305 | sys.stdout.write("\n") |
| 1306 | for indata in msgs: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1307 | if support.verbose: |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1308 | sys.stdout.write( |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1309 | " client: sending %r...\n" % indata) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1310 | if wrapped: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1311 | conn.write(indata) |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1312 | outdata = conn.read() |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1313 | else: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1314 | s.send(indata) |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1315 | outdata = s.recv(1024) |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1316 | msg = outdata.strip().lower() |
| 1317 | if indata == b"STARTTLS" and msg.startswith(b"ok"): |
| 1318 | # STARTTLS ok, switch to secure mode |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1319 | if support.verbose: |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1320 | sys.stdout.write( |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1321 | " client: read %r from server, starting TLS...\n" |
| 1322 | % msg) |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1323 | conn = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_TLSv1) |
| 1324 | wrapped = True |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1325 | elif indata == b"ENDTLS" and msg.startswith(b"ok"): |
| 1326 | # ENDTLS ok, switch back to clear text |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1327 | if support.verbose: |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1328 | sys.stdout.write( |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1329 | " client: read %r from server, ending TLS...\n" |
| 1330 | % msg) |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1331 | s = conn.unwrap() |
| 1332 | wrapped = False |
| 1333 | else: |
| 1334 | if support.verbose: |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1335 | sys.stdout.write( |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1336 | " client: read %r from server\n" % msg) |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1337 | if support.verbose: |
| 1338 | sys.stdout.write(" client: closing connection.\n") |
| 1339 | if wrapped: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1340 | conn.write(b"over\n") |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1341 | else: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1342 | s.send(b"over\n") |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1343 | if wrapped: |
| 1344 | conn.close() |
| 1345 | else: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1346 | s.close() |
| 1347 | finally: |
| 1348 | server.stop() |
| 1349 | server.join() |
| 1350 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1351 | def test_socketserver(self): |
| 1352 | """Using a SocketServer to create and manage SSL connections.""" |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 1353 | server = make_https_server(self, CERTFILE) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1354 | # try to connect |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 1355 | if support.verbose: |
| 1356 | sys.stdout.write('\n') |
| 1357 | with open(CERTFILE, 'rb') as f: |
| 1358 | d1 = f.read() |
| 1359 | d2 = '' |
| 1360 | # now fetch the same data from the HTTPS server |
| 1361 | url = 'https://%s:%d/%s' % ( |
| 1362 | HOST, server.port, os.path.split(CERTFILE)[1]) |
| 1363 | f = urllib.request.urlopen(url) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1364 | try: |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 1365 | dlen = f.info().get("content-length") |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1366 | if dlen and (int(dlen) > 0): |
| 1367 | d2 = f.read(int(dlen)) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1368 | if support.verbose: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1369 | sys.stdout.write( |
| 1370 | " client: read %d bytes from remote server '%s'\n" |
| 1371 | % (len(d2), server)) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1372 | finally: |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 1373 | f.close() |
| 1374 | self.assertEqual(d1, d2) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1375 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1376 | def test_asyncore_server(self): |
| 1377 | """Check the example asyncore integration.""" |
| 1378 | indata = "TEST MESSAGE of mixed case\n" |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1379 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1380 | if support.verbose: |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1381 | sys.stdout.write("\n") |
| 1382 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1383 | indata = b"FOO\n" |
Trent Nelson | 7852000 | 2008-04-10 20:54:35 +0000 | [diff] [blame] | 1384 | server = AsyncoreEchoServer(CERTFILE) |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1385 | flag = threading.Event() |
| 1386 | server.start(flag) |
| 1387 | # wait for it to start |
| 1388 | flag.wait() |
| 1389 | # try to connect |
| 1390 | try: |
| 1391 | s = ssl.wrap_socket(socket.socket()) |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1392 | s.connect(('127.0.0.1', server.port)) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1393 | if support.verbose: |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1394 | sys.stdout.write( |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1395 | " client: sending %r...\n" % indata) |
| 1396 | s.write(indata) |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1397 | outdata = s.read() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1398 | if support.verbose: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1399 | sys.stdout.write(" client: read %r\n" % outdata) |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1400 | if outdata != indata.lower(): |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1401 | self.fail( |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1402 | "bad data <<%r>> (%d) received; expected <<%r>> (%d)\n" |
| 1403 | % (outdata[:20], len(outdata), |
| 1404 | indata[:20].lower(), len(indata))) |
| 1405 | s.write(b"over\n") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1406 | if support.verbose: |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1407 | sys.stdout.write(" client: closing connection.\n") |
| 1408 | s.close() |
Antoine Pitrou | ed98636 | 2010-08-15 23:28:10 +0000 | [diff] [blame] | 1409 | if support.verbose: |
| 1410 | sys.stdout.write(" client: connection closed.\n") |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1411 | finally: |
Antoine Pitrou | ed98636 | 2010-08-15 23:28:10 +0000 | [diff] [blame] | 1412 | if support.verbose: |
| 1413 | sys.stdout.write(" cleanup: stopping server.\n") |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1414 | server.stop() |
Antoine Pitrou | ed98636 | 2010-08-15 23:28:10 +0000 | [diff] [blame] | 1415 | if support.verbose: |
| 1416 | sys.stdout.write(" cleanup: joining server thread.\n") |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1417 | server.join() |
Antoine Pitrou | ed98636 | 2010-08-15 23:28:10 +0000 | [diff] [blame] | 1418 | if support.verbose: |
| 1419 | sys.stdout.write(" cleanup: successfully joined.\n") |
Trent Nelson | 6b240cd | 2008-04-10 20:12:06 +0000 | [diff] [blame] | 1420 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1421 | def test_recv_send(self): |
| 1422 | """Test recv(), send() and friends.""" |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1423 | if support.verbose: |
| 1424 | sys.stdout.write("\n") |
| 1425 | |
| 1426 | server = ThreadedEchoServer(CERTFILE, |
| 1427 | certreqs=ssl.CERT_NONE, |
| 1428 | ssl_version=ssl.PROTOCOL_TLSv1, |
| 1429 | cacerts=CERTFILE, |
| 1430 | chatty=True, |
| 1431 | connectionchatty=False) |
| 1432 | flag = threading.Event() |
| 1433 | server.start(flag) |
| 1434 | # wait for it to start |
| 1435 | flag.wait() |
| 1436 | # try to connect |
Antoine Pitrou | 18c913e | 2010-04-27 10:59:39 +0000 | [diff] [blame] | 1437 | s = ssl.wrap_socket(socket.socket(), |
| 1438 | server_side=False, |
| 1439 | certfile=CERTFILE, |
| 1440 | ca_certs=CERTFILE, |
| 1441 | cert_reqs=ssl.CERT_NONE, |
| 1442 | ssl_version=ssl.PROTOCOL_TLSv1) |
| 1443 | s.connect((HOST, server.port)) |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1444 | try: |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1445 | # helper methods for standardising recv* method signatures |
| 1446 | def _recv_into(): |
| 1447 | b = bytearray(b"\0"*100) |
| 1448 | count = s.recv_into(b) |
| 1449 | return b[:count] |
| 1450 | |
| 1451 | def _recvfrom_into(): |
| 1452 | b = bytearray(b"\0"*100) |
| 1453 | count, addr = s.recvfrom_into(b) |
| 1454 | return b[:count] |
| 1455 | |
| 1456 | # (name, method, whether to expect success, *args) |
| 1457 | send_methods = [ |
| 1458 | ('send', s.send, True, []), |
| 1459 | ('sendto', s.sendto, False, ["some.address"]), |
| 1460 | ('sendall', s.sendall, True, []), |
| 1461 | ] |
| 1462 | recv_methods = [ |
| 1463 | ('recv', s.recv, True, []), |
| 1464 | ('recvfrom', s.recvfrom, False, ["some.address"]), |
| 1465 | ('recv_into', _recv_into, True, []), |
| 1466 | ('recvfrom_into', _recvfrom_into, False, []), |
| 1467 | ] |
| 1468 | data_prefix = "PREFIX_" |
| 1469 | |
| 1470 | for meth_name, send_meth, expect_success, args in send_methods: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1471 | indata = (data_prefix + meth_name).encode('ascii') |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1472 | try: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1473 | send_meth(indata, *args) |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1474 | outdata = s.read() |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1475 | if outdata != indata.lower(): |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 1476 | self.fail( |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1477 | "While sending with <<{name:s}>> bad data " |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1478 | "<<{outdata:r}>> ({nout:d}) received; " |
| 1479 | "expected <<{indata:r}>> ({nin:d})\n".format( |
| 1480 | name=meth_name, outdata=outdata[:20], |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1481 | nout=len(outdata), |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1482 | indata=indata[:20], nin=len(indata) |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1483 | ) |
| 1484 | ) |
| 1485 | except ValueError as e: |
| 1486 | if expect_success: |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 1487 | self.fail( |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1488 | "Failed to send with method <<{name:s}>>; " |
| 1489 | "expected to succeed.\n".format(name=meth_name) |
| 1490 | ) |
| 1491 | if not str(e).startswith(meth_name): |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 1492 | self.fail( |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1493 | "Method <<{name:s}>> failed with unexpected " |
| 1494 | "exception message: {exp:s}\n".format( |
| 1495 | name=meth_name, exp=e |
| 1496 | ) |
| 1497 | ) |
| 1498 | |
| 1499 | for meth_name, recv_meth, expect_success, args in recv_methods: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1500 | indata = (data_prefix + meth_name).encode('ascii') |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1501 | try: |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1502 | s.send(indata) |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1503 | outdata = recv_meth(*args) |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1504 | if outdata != indata.lower(): |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 1505 | self.fail( |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1506 | "While receiving with <<{name:s}>> bad data " |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1507 | "<<{outdata:r}>> ({nout:d}) received; " |
| 1508 | "expected <<{indata:r}>> ({nin:d})\n".format( |
| 1509 | name=meth_name, outdata=outdata[:20], |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1510 | nout=len(outdata), |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1511 | indata=indata[:20], nin=len(indata) |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1512 | ) |
| 1513 | ) |
| 1514 | except ValueError as e: |
| 1515 | if expect_success: |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 1516 | self.fail( |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1517 | "Failed to receive with method <<{name:s}>>; " |
| 1518 | "expected to succeed.\n".format(name=meth_name) |
| 1519 | ) |
| 1520 | if not str(e).startswith(meth_name): |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 1521 | self.fail( |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1522 | "Method <<{name:s}>> failed with unexpected " |
| 1523 | "exception message: {exp:s}\n".format( |
| 1524 | name=meth_name, exp=e |
| 1525 | ) |
| 1526 | ) |
| 1527 | # consume data |
| 1528 | s.read() |
| 1529 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1530 | s.write(b"over\n") |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1531 | s.close() |
| 1532 | finally: |
| 1533 | server.stop() |
| 1534 | server.join() |
| 1535 | |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 1536 | def test_handshake_timeout(self): |
| 1537 | # Issue #5103: SSL handshake must respect the socket timeout |
| 1538 | server = socket.socket(socket.AF_INET) |
| 1539 | host = "127.0.0.1" |
| 1540 | port = support.bind_port(server) |
| 1541 | started = threading.Event() |
| 1542 | finish = False |
| 1543 | |
| 1544 | def serve(): |
| 1545 | server.listen(5) |
| 1546 | started.set() |
| 1547 | conns = [] |
| 1548 | while not finish: |
| 1549 | r, w, e = select.select([server], [], [], 0.1) |
| 1550 | if server in r: |
| 1551 | # Let the socket hang around rather than having |
| 1552 | # it closed by garbage collection. |
| 1553 | conns.append(server.accept()[0]) |
Antoine Pitrou | d2eca37 | 2010-10-29 23:41:37 +0000 | [diff] [blame] | 1554 | for sock in conns: |
| 1555 | sock.close() |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 1556 | |
| 1557 | t = threading.Thread(target=serve) |
| 1558 | t.start() |
| 1559 | started.wait() |
| 1560 | |
| 1561 | try: |
Antoine Pitrou | 40f0874 | 2010-04-24 22:04:40 +0000 | [diff] [blame] | 1562 | try: |
| 1563 | c = socket.socket(socket.AF_INET) |
| 1564 | c.settimeout(0.2) |
| 1565 | c.connect((host, port)) |
| 1566 | # Will attempt handshake and time out |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 1567 | self.assertRaisesRegex(socket.timeout, "timed out", |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1568 | ssl.wrap_socket, c) |
Antoine Pitrou | 40f0874 | 2010-04-24 22:04:40 +0000 | [diff] [blame] | 1569 | finally: |
| 1570 | c.close() |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 1571 | try: |
| 1572 | c = socket.socket(socket.AF_INET) |
| 1573 | c = ssl.wrap_socket(c) |
| 1574 | c.settimeout(0.2) |
| 1575 | # Will attempt handshake and time out |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 1576 | self.assertRaisesRegex(socket.timeout, "timed out", |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1577 | c.connect, (host, port)) |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 1578 | finally: |
| 1579 | c.close() |
| 1580 | finally: |
| 1581 | finish = True |
| 1582 | t.join() |
| 1583 | server.close() |
| 1584 | |
Bill Janssen | 58afe4c | 2008-09-08 16:45:19 +0000 | [diff] [blame] | 1585 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1586 | def test_main(verbose=False): |
Antoine Pitrou | 15cee62 | 2010-08-04 16:45:21 +0000 | [diff] [blame] | 1587 | if support.verbose: |
| 1588 | plats = { |
| 1589 | 'Linux': platform.linux_distribution, |
| 1590 | 'Mac': platform.mac_ver, |
| 1591 | 'Windows': platform.win32_ver, |
| 1592 | } |
| 1593 | for name, func in plats.items(): |
| 1594 | plat = func() |
| 1595 | if plat and plat[0]: |
| 1596 | plat = '%s %r' % (name, plat) |
| 1597 | break |
| 1598 | else: |
| 1599 | plat = repr(platform.platform()) |
| 1600 | print("test_ssl: testing with %r %r" % |
| 1601 | (ssl.OPENSSL_VERSION, ssl.OPENSSL_VERSION_INFO)) |
| 1602 | print(" under %s" % plat) |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 1603 | print(" HAS_SNI = %r" % ssl.HAS_SNI) |
Antoine Pitrou | 15cee62 | 2010-08-04 16:45:21 +0000 | [diff] [blame] | 1604 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1605 | for filename in [ |
| 1606 | CERTFILE, SVN_PYTHON_ORG_ROOT_CERT, BYTES_CERTFILE, |
| 1607 | ONLYCERT, ONLYKEY, BYTES_ONLYCERT, BYTES_ONLYKEY, |
| 1608 | BADCERT, BADKEY, EMPTYCERT]: |
| 1609 | if not os.path.exists(filename): |
| 1610 | raise support.TestFailed("Can't read certificate file %r" % filename) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1611 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1612 | tests = [ContextTests, BasicSocketTests] |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1613 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1614 | if support.is_resource_enabled('network'): |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1615 | tests.append(NetworkedTests) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1616 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1617 | if _have_threads: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1618 | thread_info = support.threading_setup() |
| 1619 | if thread_info and support.is_resource_enabled('network'): |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 1620 | tests.append(ThreadedTests) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1621 | |
Antoine Pitrou | 480a124 | 2010-04-28 21:37:09 +0000 | [diff] [blame] | 1622 | try: |
| 1623 | support.run_unittest(*tests) |
| 1624 | finally: |
| 1625 | if _have_threads: |
| 1626 | support.threading_cleanup(*thread_info) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1627 | |
| 1628 | if __name__ == "__main__": |
| 1629 | test_main() |