blob: f1344e57d240bf3cffa5b51940a32a6129d7c215 [file] [log] [blame]
Nobuaki Sukegawaad835862015-12-23 23:32:09 +09001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +090020import inspect
Nobuaki Sukegawa355116e2016-02-11 18:01:20 +090021import logging
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090022import os
23import platform
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090024import ssl
25import sys
John Siroisac067042016-02-12 08:10:13 -070026import tempfile
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090027import threading
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090028import unittest
29import warnings
John Siroisac067042016-02-12 08:10:13 -070030from contextlib import contextmanager
31
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +090032import _import_local_thrift # noqa
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090033
34SCRIPT_DIR = os.path.realpath(os.path.dirname(__file__))
35ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(SCRIPT_DIR)))
36SERVER_PEM = os.path.join(ROOT_DIR, 'test', 'keys', 'server.pem')
37SERVER_CERT = os.path.join(ROOT_DIR, 'test', 'keys', 'server.crt')
38SERVER_KEY = os.path.join(ROOT_DIR, 'test', 'keys', 'server.key')
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +090039CLIENT_CERT_NO_IP = os.path.join(ROOT_DIR, 'test', 'keys', 'client.crt')
40CLIENT_KEY_NO_IP = os.path.join(ROOT_DIR, 'test', 'keys', 'client.key')
41CLIENT_CERT = os.path.join(ROOT_DIR, 'test', 'keys', 'client_v3.crt')
42CLIENT_KEY = os.path.join(ROOT_DIR, 'test', 'keys', 'client_v3.key')
43CLIENT_CA = os.path.join(ROOT_DIR, 'test', 'keys', 'CA.pem')
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090044
Aki Sukegawaeb9f65b2018-06-03 19:15:53 +090045TEST_CIPHERS = 'DES-CBC3-SHA:ECDHE-RSA-AES128-GCM-SHA256'
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090046
47
48class ServerAcceptor(threading.Thread):
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +090049 def __init__(self, server, expect_failure=False):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090050 super(ServerAcceptor, self).__init__()
John Siroisac067042016-02-12 08:10:13 -070051 self.daemon = True
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090052 self._server = server
John Siroisac067042016-02-12 08:10:13 -070053 self._listening = threading.Event()
54 self._port = None
55 self._port_bound = threading.Event()
56 self._client = None
57 self._client_accepted = threading.Event()
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +090058 self._expect_failure = expect_failure
59 frame = inspect.stack(3)[2]
60 self.name = frame[3]
61 del frame
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090062
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090063 def run(self):
64 self._server.listen()
John Siroisac067042016-02-12 08:10:13 -070065 self._listening.set()
66
67 try:
68 address = self._server.handle.getsockname()
69 if len(address) > 1:
70 # AF_INET addresses are 2-tuples (host, port) and AF_INET6 are
71 # 4-tuples (host, port, ...), but in each case port is in the second slot.
72 self._port = address[1]
73 finally:
74 self._port_bound.set()
75
76 try:
77 self._client = self._server.accept()
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +090078 except Exception:
79 logging.exception('error on server side (%s):' % self.name)
80 if not self._expect_failure:
81 raise
John Siroisac067042016-02-12 08:10:13 -070082 finally:
83 self._client_accepted.set()
84
85 def await_listening(self):
86 self._listening.wait()
87
88 @property
89 def port(self):
90 self._port_bound.wait()
91 return self._port
92
93 @property
94 def client(self):
95 self._client_accepted.wait()
96 return self._client
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090097
Aki Sukegawaeb9f65b2018-06-03 19:15:53 +090098 def close(self):
99 if self._client:
100 self._client.close()
101 self._server.close()
102
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900103
104# Python 2.6 compat
105class AssertRaises(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900106 def __init__(self, expected):
107 self._expected = expected
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900108
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900109 def __enter__(self):
110 pass
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900111
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900112 def __exit__(self, exc_type, exc_value, traceback):
113 if not exc_type or not issubclass(exc_type, self._expected):
114 raise Exception('fail')
115 return True
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900116
117
118class TSSLSocketTest(unittest.TestCase):
John Siroisac067042016-02-12 08:10:13 -0700119 def _server_socket(self, **kwargs):
120 return TSSLServerSocket(port=0, **kwargs)
121
122 @contextmanager
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +0900123 def _connectable_client(self, server, expect_failure=False, path=None, **client_kwargs):
124 acc = ServerAcceptor(server, expect_failure)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900125 try:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900126 acc.start()
John Siroisac067042016-02-12 08:10:13 -0700127 acc.await_listening()
128
129 host, port = ('localhost', acc.port) if path is None else (None, None)
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +0900130 client = TSSLSocket(host, port, unix_socket=path, **client_kwargs)
John Siroisac067042016-02-12 08:10:13 -0700131 yield acc, client
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900132 finally:
Aki Sukegawaeb9f65b2018-06-03 19:15:53 +0900133 acc.close()
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900134
John Siroisac067042016-02-12 08:10:13 -0700135 def _assert_connection_failure(self, server, path=None, **client_args):
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +0900136 logging.disable(logging.CRITICAL)
Nobuaki Sukegawad2b4f242016-09-04 18:49:23 +0900137 try:
138 with self._connectable_client(server, True, path=path, **client_args) as (acc, client):
John Siroisac067042016-02-12 08:10:13 -0700139 # We need to wait for a connection failure, but not too long. 20ms is a tunable
140 # compromise between test speed and stability
141 client.setTimeout(20)
142 with self._assert_raises(TTransportException):
143 client.open()
144 self.assertTrue(acc.client is None)
Nobuaki Sukegawad2b4f242016-09-04 18:49:23 +0900145 finally:
146 logging.disable(logging.NOTSET)
John Siroisac067042016-02-12 08:10:13 -0700147
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900148 def _assert_raises(self, exc):
149 if sys.hexversion >= 0x020700F0:
150 return self.assertRaises(exc)
151 else:
152 return AssertRaises(exc)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900153
John Siroisac067042016-02-12 08:10:13 -0700154 def _assert_connection_success(self, server, path=None, **client_args):
155 with self._connectable_client(server, path=path, **client_args) as (acc, client):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900156 client.open()
John Siroisac067042016-02-12 08:10:13 -0700157 try:
158 self.assertTrue(acc.client is not None)
159 finally:
160 client.close()
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900161
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900162 # deprecated feature
163 def test_deprecation(self):
164 with warnings.catch_warnings(record=True) as w:
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900165 warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
John Siroisac067042016-02-12 08:10:13 -0700166 TSSLSocket('localhost', 0, validate=True, ca_certs=SERVER_CERT)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900167 self.assertEqual(len(w), 1)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900168
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900169 with warnings.catch_warnings(record=True) as w:
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900170 warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900171 # Deprecated signature
172 # def __init__(self, host='localhost', port=9090, validate=True, ca_certs=None, keyfile=None, certfile=None, unix_socket=None, ciphers=None):
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900173 TSSLSocket('localhost', 0, True, SERVER_CERT, CLIENT_KEY, CLIENT_CERT, None, TEST_CIPHERS)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900174 self.assertEqual(len(w), 7)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900175
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900176 with warnings.catch_warnings(record=True) as w:
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900177 warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900178 # Deprecated signature
179 # def __init__(self, host=None, port=9090, certfile='cert.pem', unix_socket=None, ciphers=None):
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900180 TSSLServerSocket(None, 0, SERVER_PEM, None, TEST_CIPHERS)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900181 self.assertEqual(len(w), 3)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900182
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900183 # deprecated feature
184 def test_set_cert_reqs_by_validate(self):
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900185 with warnings.catch_warnings(record=True) as w:
186 warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
187 c1 = TSSLSocket('localhost', 0, validate=True, ca_certs=SERVER_CERT)
188 self.assertEqual(c1.cert_reqs, ssl.CERT_REQUIRED)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900189
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900190 c1 = TSSLSocket('localhost', 0, validate=False)
191 self.assertEqual(c1.cert_reqs, ssl.CERT_NONE)
192
193 self.assertEqual(len(w), 2)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900194
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900195 # deprecated feature
196 def test_set_validate_by_cert_reqs(self):
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900197 with warnings.catch_warnings(record=True) as w:
198 warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
199 c1 = TSSLSocket('localhost', 0, cert_reqs=ssl.CERT_NONE)
200 self.assertFalse(c1.validate)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900201
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900202 c2 = TSSLSocket('localhost', 0, cert_reqs=ssl.CERT_REQUIRED, ca_certs=SERVER_CERT)
203 self.assertTrue(c2.validate)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900204
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900205 c3 = TSSLSocket('localhost', 0, cert_reqs=ssl.CERT_OPTIONAL, ca_certs=SERVER_CERT)
206 self.assertTrue(c3.validate)
207
208 self.assertEqual(len(w), 3)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900209
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900210 def test_unix_domain_socket(self):
211 if platform.system() == 'Windows':
212 print('skipping test_unix_domain_socket')
213 return
John Siroisac067042016-02-12 08:10:13 -0700214 fd, path = tempfile.mkstemp()
215 os.close(fd)
216 try:
217 server = self._server_socket(unix_socket=path, keyfile=SERVER_KEY, certfile=SERVER_CERT)
218 self._assert_connection_success(server, path=path, cert_reqs=ssl.CERT_NONE)
219 finally:
220 os.unlink(path)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900221
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900222 def test_server_cert(self):
John Siroisac067042016-02-12 08:10:13 -0700223 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT)
224 self._assert_connection_success(server, cert_reqs=ssl.CERT_REQUIRED, ca_certs=SERVER_CERT)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900225
John Siroisac067042016-02-12 08:10:13 -0700226 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT)
227 # server cert not in ca_certs
228 self._assert_connection_failure(server, cert_reqs=ssl.CERT_REQUIRED, ca_certs=CLIENT_CERT)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900229
John Siroisac067042016-02-12 08:10:13 -0700230 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT)
231 self._assert_connection_success(server, cert_reqs=ssl.CERT_NONE)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900232
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900233 def test_set_server_cert(self):
John Siroisac067042016-02-12 08:10:13 -0700234 server = self._server_socket(keyfile=SERVER_KEY, certfile=CLIENT_CERT)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900235 with self._assert_raises(Exception):
236 server.certfile = 'foo'
237 with self._assert_raises(Exception):
238 server.certfile = None
239 server.certfile = SERVER_CERT
John Siroisac067042016-02-12 08:10:13 -0700240 self._assert_connection_success(server, cert_reqs=ssl.CERT_REQUIRED, ca_certs=SERVER_CERT)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900241
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900242 def test_client_cert(self):
Nobuaki Sukegawae8ba7872017-02-12 21:14:48 +0900243 if not _match_has_ipaddress:
244 print('skipping test_client_cert')
245 return
John Siroisac067042016-02-12 08:10:13 -0700246 server = self._server_socket(
247 cert_reqs=ssl.CERT_REQUIRED, keyfile=SERVER_KEY,
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900248 certfile=SERVER_CERT, ca_certs=CLIENT_CERT)
John Siroisac067042016-02-12 08:10:13 -0700249 self._assert_connection_failure(server, cert_reqs=ssl.CERT_NONE, certfile=SERVER_CERT, keyfile=SERVER_KEY)
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900250
John Siroisac067042016-02-12 08:10:13 -0700251 server = self._server_socket(
252 cert_reqs=ssl.CERT_REQUIRED, keyfile=SERVER_KEY,
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900253 certfile=SERVER_CERT, ca_certs=CLIENT_CA)
John Siroisac067042016-02-12 08:10:13 -0700254 self._assert_connection_failure(server, cert_reqs=ssl.CERT_NONE, certfile=CLIENT_CERT_NO_IP, keyfile=CLIENT_KEY_NO_IP)
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900255
John Siroisac067042016-02-12 08:10:13 -0700256 server = self._server_socket(
257 cert_reqs=ssl.CERT_REQUIRED, keyfile=SERVER_KEY,
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900258 certfile=SERVER_CERT, ca_certs=CLIENT_CA)
John Siroisac067042016-02-12 08:10:13 -0700259 self._assert_connection_success(server, cert_reqs=ssl.CERT_NONE, certfile=CLIENT_CERT, keyfile=CLIENT_KEY)
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900260
John Siroisac067042016-02-12 08:10:13 -0700261 server = self._server_socket(
262 cert_reqs=ssl.CERT_OPTIONAL, keyfile=SERVER_KEY,
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900263 certfile=SERVER_CERT, ca_certs=CLIENT_CA)
John Siroisac067042016-02-12 08:10:13 -0700264 self._assert_connection_success(server, cert_reqs=ssl.CERT_NONE, certfile=CLIENT_CERT, keyfile=CLIENT_KEY)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900265
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900266 def test_ciphers(self):
John Siroisac067042016-02-12 08:10:13 -0700267 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ciphers=TEST_CIPHERS)
268 self._assert_connection_success(server, ca_certs=SERVER_CERT, ciphers=TEST_CIPHERS)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900269
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900270 if not TSSLSocket._has_ciphers:
271 # unittest.skip is not available for Python 2.6
272 print('skipping test_ciphers')
273 return
John Siroisac067042016-02-12 08:10:13 -0700274 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT)
275 self._assert_connection_failure(server, ca_certs=SERVER_CERT, ciphers='NULL')
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900276
John Siroisac067042016-02-12 08:10:13 -0700277 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ciphers=TEST_CIPHERS)
278 self._assert_connection_failure(server, ca_certs=SERVER_CERT, ciphers='NULL')
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900279
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900280 def test_ssl2_and_ssl3_disabled(self):
281 if not hasattr(ssl, 'PROTOCOL_SSLv3'):
282 print('PROTOCOL_SSLv3 is not available')
283 else:
John Siroisac067042016-02-12 08:10:13 -0700284 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT)
285 self._assert_connection_failure(server, ca_certs=SERVER_CERT, ssl_version=ssl.PROTOCOL_SSLv3)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900286
John Siroisac067042016-02-12 08:10:13 -0700287 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ssl_version=ssl.PROTOCOL_SSLv3)
288 self._assert_connection_failure(server, ca_certs=SERVER_CERT)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900289
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900290 if not hasattr(ssl, 'PROTOCOL_SSLv2'):
291 print('PROTOCOL_SSLv2 is not available')
292 else:
John Siroisac067042016-02-12 08:10:13 -0700293 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT)
294 self._assert_connection_failure(server, ca_certs=SERVER_CERT, ssl_version=ssl.PROTOCOL_SSLv2)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900295
John Siroisac067042016-02-12 08:10:13 -0700296 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ssl_version=ssl.PROTOCOL_SSLv2)
297 self._assert_connection_failure(server, ca_certs=SERVER_CERT)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900298
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900299 def test_newer_tls(self):
300 if not TSSLSocket._has_ssl_context:
301 # unittest.skip is not available for Python 2.6
302 print('skipping test_newer_tls')
303 return
304 if not hasattr(ssl, 'PROTOCOL_TLSv1_2'):
305 print('PROTOCOL_TLSv1_2 is not available')
306 else:
John Siroisac067042016-02-12 08:10:13 -0700307 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ssl_version=ssl.PROTOCOL_TLSv1_2)
308 self._assert_connection_success(server, ca_certs=SERVER_CERT, ssl_version=ssl.PROTOCOL_TLSv1_2)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900309
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900310 if not hasattr(ssl, 'PROTOCOL_TLSv1_1'):
311 print('PROTOCOL_TLSv1_1 is not available')
312 else:
John Siroisac067042016-02-12 08:10:13 -0700313 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ssl_version=ssl.PROTOCOL_TLSv1_1)
314 self._assert_connection_success(server, ca_certs=SERVER_CERT, ssl_version=ssl.PROTOCOL_TLSv1_1)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900315
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900316 if not hasattr(ssl, 'PROTOCOL_TLSv1_1') or not hasattr(ssl, 'PROTOCOL_TLSv1_2'):
317 print('PROTOCOL_TLSv1_1 and/or PROTOCOL_TLSv1_2 is not available')
318 else:
John Siroisac067042016-02-12 08:10:13 -0700319 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ssl_version=ssl.PROTOCOL_TLSv1_2)
320 self._assert_connection_failure(server, ca_certs=SERVER_CERT, ssl_version=ssl.PROTOCOL_TLSv1_1)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900321
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900322 def test_ssl_context(self):
323 if not TSSLSocket._has_ssl_context:
324 # unittest.skip is not available for Python 2.6
325 print('skipping test_ssl_context')
326 return
327 server_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
328 server_context.load_cert_chain(SERVER_CERT, SERVER_KEY)
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900329 server_context.load_verify_locations(CLIENT_CA)
330 server_context.verify_mode = ssl.CERT_REQUIRED
John Siroisac067042016-02-12 08:10:13 -0700331 server = self._server_socket(ssl_context=server_context)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900332
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900333 client_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
334 client_context.load_cert_chain(CLIENT_CERT, CLIENT_KEY)
335 client_context.load_verify_locations(SERVER_CERT)
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900336 client_context.verify_mode = ssl.CERT_REQUIRED
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900337
John Siroisac067042016-02-12 08:10:13 -0700338 self._assert_connection_success(server, ssl_context=client_context)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900339
James E. King, III0ad20bd2017-09-30 15:44:16 -0700340
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900341if __name__ == '__main__':
Nobuaki Sukegawad2b4f242016-09-04 18:49:23 +0900342 logging.basicConfig(level=logging.WARN)
Nobuaki Sukegawae8ba7872017-02-12 21:14:48 +0900343 from thrift.transport.TSSLSocket import TSSLSocket, TSSLServerSocket, _match_has_ipaddress
Nobuaki Sukegawad2b4f242016-09-04 18:49:23 +0900344 from thrift.transport.TTransport import TTransportException
345
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900346 unittest.main()