|
23 | 23 | import ssl
|
24 | 24 | import sys
|
25 | 25 | import warnings
|
26 |
| -from backports.ssl_match_hostname import match_hostname |
27 | 26 |
|
| 27 | +from .sslcompat import _match_hostname, _match_has_ipaddress |
28 | 28 | from thrift.transport import TSocket
|
29 | 29 | from thrift.transport.TTransport import TTransportException
|
30 | 30 |
|
@@ -259,7 +259,7 @@ def __init__(self, host='localhost', port=9090, *args, **kwargs):
|
259 | 259 | kwargs['cert_reqs'] = ssl.CERT_REQUIRED if validate else ssl.CERT_NONE
|
260 | 260 |
|
261 | 261 | unix_socket = kwargs.pop('unix_socket', None)
|
262 |
| - self._validate_callback = kwargs.pop('validate_callback', match_hostname) |
| 262 | + self._validate_callback = kwargs.pop('validate_callback', _match_hostname) |
263 | 263 | TSSLBase.__init__(self, False, host, kwargs)
|
264 | 264 | TSocket.TSocket.__init__(self, host, port, unix_socket)
|
265 | 265 |
|
@@ -297,45 +297,6 @@ def open(self):
|
297 | 297 | except Exception as ex:
|
298 | 298 | raise TTransportException(TTransportException.UNKNOWN, str(ex))
|
299 | 299 |
|
300 |
| - @staticmethod |
301 |
| - def legacy_validate_callback(self, cert, hostname): |
302 |
| - """legacy method to validate the peer's SSL certificate, and to check |
303 |
| - the commonName of the certificate to ensure it matches the hostname we |
304 |
| - used to make this connection. Does not support subjectAltName records |
305 |
| - in certificates. |
306 |
| -
|
307 |
| - raises TTransportException if the certificate fails validation. |
308 |
| - """ |
309 |
| - if 'subject' not in cert: |
310 |
| - raise TTransportException( |
311 |
| - TTransportException.NOT_OPEN, |
312 |
| - 'No SSL certificate found from %s:%s' % (self.host, self.port)) |
313 |
| - fields = cert['subject'] |
314 |
| - for field in fields: |
315 |
| - # ensure structure we get back is what we expect |
316 |
| - if not isinstance(field, tuple): |
317 |
| - continue |
318 |
| - cert_pair = field[0] |
319 |
| - if len(cert_pair) < 2: |
320 |
| - continue |
321 |
| - cert_key, cert_value = cert_pair[0:2] |
322 |
| - if cert_key != 'commonName': |
323 |
| - continue |
324 |
| - certhost = cert_value |
325 |
| - # this check should be performed by some sort of Access Manager |
326 |
| - if certhost == hostname: |
327 |
| - # success, cert commonName matches desired hostname |
328 |
| - return |
329 |
| - else: |
330 |
| - raise TTransportException( |
331 |
| - TTransportException.UNKNOWN, |
332 |
| - 'Hostname we connected to "%s" doesn\'t match certificate ' |
333 |
| - 'provided commonName "%s"' % (self.host, certhost)) |
334 |
| - raise TTransportException( |
335 |
| - TTransportException.UNKNOWN, |
336 |
| - 'Could not validate SSL certificate from host "%s". Cert=%s' |
337 |
| - % (hostname, cert)) |
338 |
| - |
339 | 300 |
|
340 | 301 | class TSSLServerSocket(TSocket.TServerSocket, TSSLBase):
|
341 | 302 | """SSL implementation of TServerSocket
|
@@ -381,9 +342,12 @@ def __init__(self, host=None, port=9090, *args, **kwargs):
|
381 | 342 |
|
382 | 343 | unix_socket = kwargs.pop('unix_socket', None)
|
383 | 344 | self._validate_callback = \
|
384 |
| - kwargs.pop('validate_callback', match_hostname) |
| 345 | + kwargs.pop('validate_callback', _match_hostname) |
385 | 346 | TSSLBase.__init__(self, True, None, kwargs)
|
386 | 347 | TSocket.TServerSocket.__init__(self, host, port, unix_socket)
|
| 348 | + if self._should_verify and not _match_has_ipaddress: |
| 349 | + raise ValueError('Need ipaddress and backports.ssl_match_hostname' |
| 350 | + 'module to verify client certificate') |
387 | 351 |
|
388 | 352 | def setCertfile(self, certfile):
|
389 | 353 | """Set or change the server certificate file used to wrap new
|
|
0 commit comments