Skip to content

Commit

Permalink
iostream_test: Test check_hostname functionality.
Browse files Browse the repository at this point in the history
In #3337, the removal of ssl.match_hostname revealed that we did not
have any test coverage of hostname checking in tornado.iostream.
Since we were forced to remove the manual check that we had in place
for old versions of Python, we need a test to make sure that we didn't
inadvertently break hostname checking.
  • Loading branch information
bdarnell committed Nov 14, 2023
1 parent a6dfd70 commit 06e1a65
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 45 deletions.
75 changes: 75 additions & 0 deletions tornado/test/iostream_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,81 @@ def handle_stream(self, stream, address):
yield handshake_future


class TestIOStreamCheckHostname(AsyncTestCase):
# This test ensures that hostname checks are working correctly after
# #3337 revealed that we have no test coverage in this area, and we
# removed a manual hostname check that was needed only for very old
# versions of python.
def setUp(self):
super().setUp()
self.listener, self.port = bind_unused_port()

def accept_callback(connection, address):
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_ctx.load_cert_chain(
os.path.join(os.path.dirname(__file__), "test.crt"),
os.path.join(os.path.dirname(__file__), "test.key"),
)
connection = ssl_ctx.wrap_socket(
connection,
server_side=True,
do_handshake_on_connect=False,
)
SSLIOStream(connection)

netutil.add_accept_handler(self.listener, accept_callback)

# Our self-signed cert is its own CA. We have to pass the CA check before
# the hostname check will be performed.
self.client_ssl_ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
self.client_ssl_ctx.load_verify_locations(
os.path.join(os.path.dirname(__file__), "test.crt")
)

def tearDown(self):
self.io_loop.remove_handler(self.listener.fileno())
self.listener.close()
super().tearDown()

@gen_test
async def test_match(self):
stream = SSLIOStream(socket.socket(), ssl_options=self.client_ssl_ctx)
await stream.connect(
("127.0.0.1", self.port),
server_hostname="foo.example.com",
)
stream.close()

@gen_test
async def test_no_match(self):
stream = SSLIOStream(socket.socket(), ssl_options=self.client_ssl_ctx)
with ExpectLog(gen_log, ".*alert bad certificate", level=logging.WARNING):
with self.assertRaises(ssl.SSLCertVerificationError):
with ExpectLog(
gen_log,
".*(certificate verify failed: Hostname mismatch)",
level=logging.WARNING,
):
await stream.connect(
("127.0.0.1", self.port),
server_hostname="bar.example.com",
)
# The server logs a warning while cleaning up the failed connection.
# Unfortunately there's no good hook to wait for this logging.
await asyncio.sleep(0.1)

@gen_test
async def test_check_disabled(self):
# check_hostname can be set to false and the connection will succeed even though it doesn't
# have the right hostname.
self.client_ssl_ctx.check_hostname = False
stream = SSLIOStream(socket.socket(), ssl_options=self.client_ssl_ctx)
await stream.connect(
("127.0.0.1", self.port),
server_hostname="bar.example.com",
)


@skipIfNonUnix
class TestPipeIOStream(TestReadWriteMixin, AsyncTestCase):
@gen.coroutine
Expand Down
34 changes: 16 additions & 18 deletions tornado/test/test.crt
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
-----BEGIN CERTIFICATE-----
MIIDWzCCAkOgAwIBAgIUV4spou0CenmvKqa7Hml/MC+JKiAwDQYJKoZIhvcNAQEL
BQAwPTELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExGTAXBgNVBAoM
EFRvcm5hZG8gV2ViIFRlc3QwHhcNMTgwOTI5MTM1NjQ1WhcNMjgwOTI2MTM1NjQ1
WjA9MQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEZMBcGA1UECgwQ
VG9ybmFkbyBXZWIgVGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
AKT0LdyI8tW5uwP3ahE8BFSz+j3SsKBDv/0cKvqxVVE6sLEST2s3HjArZvIIG5sb
iBkWDrqnZ6UKDvB4jlobLGAkepxDbrxHWxK53n0C28XXGLqJQ01TlTZ5rpjttMeg
5SKNjHbxpOvpUwwQS4br4WjZKKyTGiXpFkFUty+tYVU35/U2yyvreWHmzpHx/25t
H7O2RBARVwJYKOGPtlH62lQjpIWfVfklY4Ip8Hjl3B6rBxPyBULmVQw0qgoZn648
oa4oLjs0wnYB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3dDeqt9sdabvj7LANqfygbt2cf9yYn
a25UTRcAN7CNdWwTEfvnOVMITzCE8F2FmKDvJR+TX30CAwEAAaMeMBwwGgYDVR0R
BBMwEYIPZm9vLmV4YW1wbGUuY29tMA0GCSqGSIb3DQEBCwUAA4IBAQBjKz4gM4Bz
JO7Ny1fwbBtraHCGYnDG8gBID3+/sQlMMFeuquJK+oc+1DOpr9wFlmgih67OszdM
X2Xl/HjtHPKwNqaDHXu5bQPFT5fXzAZ8HHEOXSV9IpHaNyS7TC7bYmD/ClCZeqXU
h7MBe5yPXfCCIqWyjZMZDQfT1v6J+WX3+lO9josMJCfNR5DzvJiPmSTUxrLD5SkT
+7iKxhM6eI83D+I188sGc2IMinkFp8jSRTlaH8WYiOd5QQ2r8GSYNM9M3z1sK7zv
0Bw3hWEQgpFbEaSH0OB72KYkMUZBqK9UoeSZWBrMXHFBNaY23tEKInEwlBGBELGc
acSinK6OBC0z
-----END CERTIFICATE-----
52 changes: 26 additions & 26 deletions tornado/test/test.key
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCk9C3ciPLVubsD
92oRPARUs/o90rCgQ7/9HCr6sVVROrCxEk9rNx4wK2byCBubG4gZFg66p2elCg7w
eI5aGyxgJHqcQ268R1sSud59AtvF1xi6iUNNU5U2ea6Y7bTHoOUijYx28aTr6VMM
EEuG6+Fo2Siskxol6RZBVLcvrWFVN+f1Nssr63lh5s6R8f9ubR+ztkQQEVcCWCjh
j7ZR+tpUI6SFn1X5JWOCKfB45dweqwcT8gVC5lUMNKoKGZ+uPKGuKC47NMJ2Ac9N
YFYzTAx3o+dkrAf4nh+1tU8RTM6jpXLx4eNbhW0CT4HF7K7kkvWeLcM0lpFKdr5P
wv/+Es9/AgMBAAECggEABi6AaXtYXloPgB6NgwfUwbfc8OQsalUfpMShd7OdluW0
KW6eO05de0ClIvzay/1EJGyH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hj
XbWsAFYKII+aUXC92sVL5sO33vNyhBbyMN1aSRXYe8B5EnwsP31o5csrHQw6i/Dp
jUx1AUBYkNPgL9ctqlTQf1nb0LenGlCUBD6jrSrJVHeOF4y+HIZHXNZ++otH7+eu
b3dbHgV/9F8CgYBTopO0utAvH3WdDGqNYk7fzUlvX1ao8Qs/mi2wL8MrzjIvRmmO
h137607X3Sfc3KyXvQ8b4relkMSJbAd34aohp+CHrpHCr9HcKbZjkwkQUWkEcRIW
EzLdJaE3yPBPq5an7y6j9qS0EP8DIxIZPwrS4xf9fuz1DdOAD+BqJS2SJwKBgQCt
zZzTpduxbnA+Qrx503cBVVJ28viVmsiwK2hn8DwbHu9eBegHnGDs0H/Th9UE1g+r
+TA4E85/BUaTcapUb5hlwKDJwh/QkaroYyeCEtgRQbnbw3d41w3Vsqw78atWpFoE
oetYD9nAdLJMReD+NZoRlzsKX9CXYS8fORkf19RPTwKBgQCQdvDMicrtnJ4U2MOA
y+59K7V77VRfHLecjAMGblBGmrtoQSBvQiFznXm0DUOSy3eZRITwe01/t+RUDhx9
MVLlyNzwRCVHzPe7kUI10GM4W5ZKAf8f/t0KjBrQBeYtRUOEI3QVzsVzc1hY8Fv8
YHOhmI8Tdd2biF+lqXKC6vGlvQ==
-----END PRIVATE KEY-----
4 changes: 3 additions & 1 deletion tornado/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,9 @@ def get_ssl_options(self) -> Dict[str, Any]:
def default_ssl_options() -> Dict[str, Any]:
# Testing keys were generated with:
# openssl req -new -keyout tornado/test/test.key \
# -out tornado/test/test.crt -nodes -days 3650 -x509
# -out tornado/test/test.crt \
# -nodes -days 3650 -x509 \
# -subj "/CN=foo.example.com" -addext "subjectAltName = DNS:foo.example.com"
module_dir = os.path.dirname(__file__)
return dict(
certfile=os.path.join(module_dir, "test", "test.crt"),
Expand Down

0 comments on commit 06e1a65

Please sign in to comment.