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