Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 1 | # |
| 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 | # |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 19 | |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 20 | import logging |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 21 | import os |
| 22 | import socket |
| 23 | import ssl |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 24 | import sys |
| 25 | import warnings |
Bryan Duxbury | 2b969ad | 2011-02-22 18:20:53 +0000 | [diff] [blame] | 26 | |
Susanne Lindgren | 7ec4177 | 2024-10-08 08:12:27 +0200 | [diff] [blame] | 27 | from .sslcompat import _match_has_ipaddress |
Bryan Duxbury | 2b969ad | 2011-02-22 18:20:53 +0000 | [diff] [blame] | 28 | from thrift.transport import TSocket |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 29 | from thrift.transport.TTransport import TTransportException |
Bryan Duxbury | 2b969ad | 2011-02-22 18:20:53 +0000 | [diff] [blame] | 30 | |
Susanne Lindgren | 7ec4177 | 2024-10-08 08:12:27 +0200 | [diff] [blame] | 31 | _match_hostname = lambda cert, hostname: True |
| 32 | |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 33 | logger = logging.getLogger(__name__) |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 34 | warnings.filterwarnings( |
| 35 | 'default', category=DeprecationWarning, module=__name__) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 36 | |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 37 | |
| 38 | class TSSLBase(object): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 39 | # SSLContext is not available for Python < 2.7.9 |
| 40 | _has_ssl_context = sys.hexversion >= 0x020709F0 |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 41 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 42 | # ciphers argument is not available for Python < 2.7.0 |
| 43 | _has_ciphers = sys.hexversion >= 0x020700F0 |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 44 | |
James E. King, III | 36628a2 | 2017-02-13 15:25:41 -0500 | [diff] [blame] | 45 | # For python >= 2.7.9, use latest TLS that both client and server |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 46 | # supports. |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 47 | # SSL 2.0 and 3.0 are disabled via ssl.OP_NO_SSLv2 and ssl.OP_NO_SSLv3. |
James E. King, III | 36628a2 | 2017-02-13 15:25:41 -0500 | [diff] [blame] | 48 | # For python < 2.7.9, use TLS 1.0 since TLSv1_X nor OP_NO_SSLvX is |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 49 | # unavailable. |
Susanne Lindgren | 7ec4177 | 2024-10-08 08:12:27 +0200 | [diff] [blame] | 50 | _default_protocol = ssl.PROTOCOL_TLS_CLIENT if _has_ssl_context else \ |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 51 | ssl.PROTOCOL_TLSv1 |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 52 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 53 | def _init_context(self, ssl_version): |
| 54 | if self._has_ssl_context: |
| 55 | self._context = ssl.SSLContext(ssl_version) |
| 56 | if self._context.protocol == ssl.PROTOCOL_SSLv23: |
| 57 | self._context.options |= ssl.OP_NO_SSLv2 |
| 58 | self._context.options |= ssl.OP_NO_SSLv3 |
| 59 | else: |
| 60 | self._context = None |
| 61 | self._ssl_version = ssl_version |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 62 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 63 | @property |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 64 | def _should_verify(self): |
| 65 | if self._has_ssl_context: |
| 66 | return self._context.verify_mode != ssl.CERT_NONE |
| 67 | else: |
| 68 | return self.cert_reqs != ssl.CERT_NONE |
| 69 | |
| 70 | @property |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 71 | def ssl_version(self): |
| 72 | if self._has_ssl_context: |
| 73 | return self.ssl_context.protocol |
| 74 | else: |
| 75 | return self._ssl_version |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 76 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 77 | @property |
| 78 | def ssl_context(self): |
| 79 | return self._context |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 80 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 81 | SSL_VERSION = _default_protocol |
| 82 | """ |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 83 | Default SSL version. |
Tim Armstrong | 6b3f7d9 | 2019-02-14 14:59:22 -0800 | [diff] [blame] | 84 | For backwards compatibility, it can be modified. |
| 85 | Use __init__ keyword argument "ssl_version" instead. |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 86 | """ |
| 87 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 88 | def _deprecated_arg(self, args, kwargs, pos, key): |
| 89 | if len(args) <= pos: |
| 90 | return |
| 91 | real_pos = pos + 3 |
| 92 | warnings.warn( |
Nobuaki Sukegawa | 6a0ca7f | 2016-02-13 03:11:16 +0900 | [diff] [blame] | 93 | '%dth positional argument is deprecated.' |
Tim Armstrong | 6b3f7d9 | 2019-02-14 14:59:22 -0800 | [diff] [blame] | 94 | 'please use keyword argument instead.' |
Nobuaki Sukegawa | 6a0ca7f | 2016-02-13 03:11:16 +0900 | [diff] [blame] | 95 | % real_pos, DeprecationWarning, stacklevel=3) |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 96 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 97 | if key in kwargs: |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 98 | raise TypeError( |
Tim Armstrong | 6b3f7d9 | 2019-02-14 14:59:22 -0800 | [diff] [blame] | 99 | 'Duplicate argument: %dth argument and %s keyword argument.' |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 100 | % (real_pos, key)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 101 | kwargs[key] = args[pos] |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 102 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 103 | def _unix_socket_arg(self, host, port, args, kwargs): |
| 104 | key = 'unix_socket' |
| 105 | if host is None and port is None and len(args) == 1 and key not in kwargs: |
| 106 | kwargs[key] = args[0] |
| 107 | return True |
| 108 | return False |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 109 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 110 | def __getattr__(self, key): |
| 111 | if key == 'SSL_VERSION': |
Nobuaki Sukegawa | 6a0ca7f | 2016-02-13 03:11:16 +0900 | [diff] [blame] | 112 | warnings.warn( |
| 113 | 'SSL_VERSION is deprecated.' |
| 114 | 'please use ssl_version attribute instead.', |
| 115 | DeprecationWarning, stacklevel=2) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 116 | return self.ssl_version |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 117 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 118 | def __init__(self, server_side, host, ssl_opts): |
| 119 | self._server_side = server_side |
| 120 | if TSSLBase.SSL_VERSION != self._default_protocol: |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 121 | warnings.warn( |
Nobuaki Sukegawa | 6a0ca7f | 2016-02-13 03:11:16 +0900 | [diff] [blame] | 122 | 'SSL_VERSION is deprecated.' |
Tim Armstrong | 6b3f7d9 | 2019-02-14 14:59:22 -0800 | [diff] [blame] | 123 | 'please use ssl_version keyword argument instead.', |
Nobuaki Sukegawa | 6a0ca7f | 2016-02-13 03:11:16 +0900 | [diff] [blame] | 124 | DeprecationWarning, stacklevel=2) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 125 | self._context = ssl_opts.pop('ssl_context', None) |
| 126 | self._server_hostname = None |
| 127 | if not self._server_side: |
| 128 | self._server_hostname = ssl_opts.pop('server_hostname', host) |
| 129 | if self._context: |
| 130 | self._custom_context = True |
| 131 | if ssl_opts: |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 132 | raise ValueError( |
| 133 | 'Incompatible arguments: ssl_context and %s' |
| 134 | % ' '.join(ssl_opts.keys())) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 135 | if not self._has_ssl_context: |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 136 | raise ValueError( |
| 137 | 'ssl_context is not available for this version of Python') |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 138 | else: |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 139 | self._custom_context = False |
| 140 | ssl_version = ssl_opts.pop('ssl_version', TSSLBase.SSL_VERSION) |
| 141 | self._init_context(ssl_version) |
| 142 | self.cert_reqs = ssl_opts.pop('cert_reqs', ssl.CERT_REQUIRED) |
| 143 | self.ca_certs = ssl_opts.pop('ca_certs', None) |
| 144 | self.keyfile = ssl_opts.pop('keyfile', None) |
| 145 | self.certfile = ssl_opts.pop('certfile', None) |
| 146 | self.ciphers = ssl_opts.pop('ciphers', None) |
| 147 | |
| 148 | if ssl_opts: |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 149 | raise ValueError( |
| 150 | 'Unknown keyword arguments: ', ' '.join(ssl_opts.keys())) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 151 | |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 152 | if self._should_verify: |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 153 | if not self.ca_certs: |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 154 | raise ValueError( |
| 155 | 'ca_certs is needed when cert_reqs is not ssl.CERT_NONE') |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 156 | if not os.access(self.ca_certs, os.R_OK): |
| 157 | raise IOError('Certificate Authority ca_certs file "%s" ' |
| 158 | 'is not readable, cannot validate SSL ' |
| 159 | 'certificates.' % (self.ca_certs)) |
| 160 | |
| 161 | @property |
| 162 | def certfile(self): |
| 163 | return self._certfile |
| 164 | |
| 165 | @certfile.setter |
| 166 | def certfile(self, certfile): |
| 167 | if self._server_side and not certfile: |
| 168 | raise ValueError('certfile is needed for server-side') |
| 169 | if certfile and not os.access(certfile, os.R_OK): |
| 170 | raise IOError('No such certfile found: %s' % (certfile)) |
| 171 | self._certfile = certfile |
| 172 | |
| 173 | def _wrap_socket(self, sock): |
| 174 | if self._has_ssl_context: |
| 175 | if not self._custom_context: |
| 176 | self.ssl_context.verify_mode = self.cert_reqs |
| 177 | if self.certfile: |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 178 | self.ssl_context.load_cert_chain(self.certfile, |
| 179 | self.keyfile) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 180 | if self.ciphers: |
| 181 | self.ssl_context.set_ciphers(self.ciphers) |
| 182 | if self.ca_certs: |
| 183 | self.ssl_context.load_verify_locations(self.ca_certs) |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 184 | return self.ssl_context.wrap_socket( |
| 185 | sock, server_side=self._server_side, |
| 186 | server_hostname=self._server_hostname) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 187 | else: |
| 188 | ssl_opts = { |
| 189 | 'ssl_version': self._ssl_version, |
| 190 | 'server_side': self._server_side, |
| 191 | 'ca_certs': self.ca_certs, |
| 192 | 'keyfile': self.keyfile, |
| 193 | 'certfile': self.certfile, |
| 194 | 'cert_reqs': self.cert_reqs, |
| 195 | } |
| 196 | if self.ciphers: |
| 197 | if self._has_ciphers: |
| 198 | ssl_opts['ciphers'] = self.ciphers |
| 199 | else: |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 200 | logger.warning( |
| 201 | 'ciphers is specified but ignored due to old Python version') |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 202 | return ssl.wrap_socket(sock, **ssl_opts) |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 203 | |
| 204 | |
| 205 | class TSSLSocket(TSocket.TSocket, TSSLBase): |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 206 | """ |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 207 | SSL implementation of TSocket |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 208 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 209 | This class creates outbound sockets wrapped using the |
| 210 | python standard ssl module for encrypted connections. |
| 211 | """ |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 212 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 213 | # New signature |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 214 | # def __init__(self, host='localhost', port=9090, unix_socket=None, |
| 215 | # **ssl_args): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 216 | # Deprecated signature |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 217 | # def __init__(self, host='localhost', port=9090, validate=True, |
| 218 | # ca_certs=None, keyfile=None, certfile=None, |
| 219 | # unix_socket=None, ciphers=None): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 220 | def __init__(self, host='localhost', port=9090, *args, **kwargs): |
| 221 | """Positional arguments: ``host``, ``port``, ``unix_socket`` |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 222 | |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 223 | Keyword arguments: ``keyfile``, ``certfile``, ``cert_reqs``, |
| 224 | ``ssl_version``, ``ca_certs``, |
| 225 | ``ciphers`` (Python 2.7.0 or later), |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 226 | ``server_hostname`` (Python 2.7.9 or later) |
| 227 | Passed to ssl.wrap_socket. See ssl.wrap_socket documentation. |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 228 | |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 229 | Alternative keyword arguments: (Python 2.7.9 or later) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 230 | ``ssl_context``: ssl.SSLContext to be used for SSLContext.wrap_socket |
| 231 | ``server_hostname``: Passed to SSLContext.wrap_socket |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 232 | |
| 233 | Common keyword argument: |
| 234 | ``validate_callback`` (cert, hostname) -> None: |
| 235 | Called after SSL handshake. Can raise when hostname does not |
| 236 | match the cert. |
Jun | f6511c9 | 2019-02-01 12:07:58 +0800 | [diff] [blame] | 237 | ``socket_keepalive`` enable TCP keepalive, default off. |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 238 | """ |
| 239 | self.is_valid = False |
| 240 | self.peercert = None |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 241 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 242 | if args: |
| 243 | if len(args) > 6: |
| 244 | raise TypeError('Too many positional argument') |
| 245 | if not self._unix_socket_arg(host, port, args, kwargs): |
| 246 | self._deprecated_arg(args, kwargs, 0, 'validate') |
| 247 | self._deprecated_arg(args, kwargs, 1, 'ca_certs') |
| 248 | self._deprecated_arg(args, kwargs, 2, 'keyfile') |
| 249 | self._deprecated_arg(args, kwargs, 3, 'certfile') |
| 250 | self._deprecated_arg(args, kwargs, 4, 'unix_socket') |
| 251 | self._deprecated_arg(args, kwargs, 5, 'ciphers') |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 252 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 253 | validate = kwargs.pop('validate', None) |
| 254 | if validate is not None: |
| 255 | cert_reqs_name = 'CERT_REQUIRED' if validate else 'CERT_NONE' |
| 256 | warnings.warn( |
Nobuaki Sukegawa | 6a0ca7f | 2016-02-13 03:11:16 +0900 | [diff] [blame] | 257 | 'validate is deprecated. please use cert_reqs=ssl.%s instead' |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 258 | % cert_reqs_name, |
Nobuaki Sukegawa | 6a0ca7f | 2016-02-13 03:11:16 +0900 | [diff] [blame] | 259 | DeprecationWarning, stacklevel=2) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 260 | if 'cert_reqs' in kwargs: |
| 261 | raise TypeError('Cannot specify both validate and cert_reqs') |
| 262 | kwargs['cert_reqs'] = ssl.CERT_REQUIRED if validate else ssl.CERT_NONE |
| 263 | |
| 264 | unix_socket = kwargs.pop('unix_socket', None) |
Jun | f6511c9 | 2019-02-01 12:07:58 +0800 | [diff] [blame] | 265 | socket_keepalive = kwargs.pop('socket_keepalive', False) |
Nobuaki Sukegawa | f32bae7 | 2016-02-20 08:51:33 +0900 | [diff] [blame] | 266 | self._validate_callback = kwargs.pop('validate_callback', _match_hostname) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 267 | TSSLBase.__init__(self, False, host, kwargs) |
Jun | f6511c9 | 2019-02-01 12:07:58 +0800 | [diff] [blame] | 268 | TSocket.TSocket.__init__(self, host, port, unix_socket, |
| 269 | socket_keepalive=socket_keepalive) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 270 | |
James E. King III | 638c91f | 2019-01-26 10:33:04 -0500 | [diff] [blame] | 271 | def close(self): |
| 272 | try: |
| 273 | self.handle.settimeout(0.001) |
| 274 | self.handle = self.handle.unwrap() |
| 275 | except (ssl.SSLError, socket.error, OSError): |
| 276 | # could not complete shutdown in a reasonable amount of time. bail. |
| 277 | pass |
| 278 | TSocket.TSocket.close(self) |
| 279 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 280 | @property |
| 281 | def validate(self): |
Nobuaki Sukegawa | 6a0ca7f | 2016-02-13 03:11:16 +0900 | [diff] [blame] | 282 | warnings.warn('validate is deprecated. please use cert_reqs instead', |
| 283 | DeprecationWarning, stacklevel=2) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 284 | return self.cert_reqs != ssl.CERT_NONE |
| 285 | |
| 286 | @validate.setter |
| 287 | def validate(self, value): |
Nobuaki Sukegawa | 6a0ca7f | 2016-02-13 03:11:16 +0900 | [diff] [blame] | 288 | warnings.warn('validate is deprecated. please use cert_reqs instead', |
| 289 | DeprecationWarning, stacklevel=2) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 290 | self.cert_reqs = ssl.CERT_REQUIRED if value else ssl.CERT_NONE |
| 291 | |
Nobuaki Sukegawa | 1c8b5cb | 2016-02-14 22:04:38 +0900 | [diff] [blame] | 292 | def _do_open(self, family, socktype): |
| 293 | plain_sock = socket.socket(family, socktype) |
Bryan Duxbury | 2b969ad | 2011-02-22 18:20:53 +0000 | [diff] [blame] | 294 | try: |
Nobuaki Sukegawa | 1c8b5cb | 2016-02-14 22:04:38 +0900 | [diff] [blame] | 295 | return self._wrap_socket(plain_sock) |
James E. King III | 3131fe9 | 2019-07-02 14:21:05 -0400 | [diff] [blame] | 296 | except Exception as ex: |
Nobuaki Sukegawa | 1c8b5cb | 2016-02-14 22:04:38 +0900 | [diff] [blame] | 297 | plain_sock.close() |
| 298 | msg = 'failed to initialize SSL' |
| 299 | logger.exception(msg) |
James E. King III | 3131fe9 | 2019-07-02 14:21:05 -0400 | [diff] [blame] | 300 | raise TTransportException(type=TTransportException.NOT_OPEN, message=msg, inner=ex) |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 301 | |
Nobuaki Sukegawa | 1c8b5cb | 2016-02-14 22:04:38 +0900 | [diff] [blame] | 302 | def open(self): |
| 303 | super(TSSLSocket, self).open() |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 304 | if self._should_verify: |
| 305 | self.peercert = self.handle.getpeercert() |
| 306 | try: |
| 307 | self._validate_callback(self.peercert, self._server_hostname) |
| 308 | self.is_valid = True |
| 309 | except TTransportException: |
| 310 | raise |
| 311 | except Exception as ex: |
James E. King III | 3131fe9 | 2019-07-02 14:21:05 -0400 | [diff] [blame] | 312 | raise TTransportException(message=str(ex), inner=ex) |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 313 | |
Bryan Duxbury | 2b969ad | 2011-02-22 18:20:53 +0000 | [diff] [blame] | 314 | |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 315 | class TSSLServerSocket(TSocket.TServerSocket, TSSLBase): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 316 | """SSL implementation of TServerSocket |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 317 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 318 | This uses the ssl module's wrap_socket() method to provide SSL |
| 319 | negotiated encryption. |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 320 | """ |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 321 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 322 | # New signature |
| 323 | # def __init__(self, host='localhost', port=9090, unix_socket=None, **ssl_args): |
| 324 | # Deprecated signature |
| 325 | # def __init__(self, host=None, port=9090, certfile='cert.pem', unix_socket=None, ciphers=None): |
| 326 | def __init__(self, host=None, port=9090, *args, **kwargs): |
| 327 | """Positional arguments: ``host``, ``port``, ``unix_socket`` |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 328 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 329 | Keyword arguments: ``keyfile``, ``certfile``, ``cert_reqs``, ``ssl_version``, |
| 330 | ``ca_certs``, ``ciphers`` (Python 2.7.0 or later) |
| 331 | See ssl.wrap_socket documentation. |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 332 | |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 333 | Alternative keyword arguments: (Python 2.7.9 or later) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 334 | ``ssl_context``: ssl.SSLContext to be used for SSLContext.wrap_socket |
| 335 | ``server_hostname``: Passed to SSLContext.wrap_socket |
Nobuaki Sukegawa | f39f7db | 2016-02-04 15:09:41 +0900 | [diff] [blame] | 336 | |
| 337 | Common keyword argument: |
| 338 | ``validate_callback`` (cert, hostname) -> None: |
| 339 | Called after SSL handshake. Can raise when hostname does not |
| 340 | match the cert. |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 341 | """ |
| 342 | if args: |
| 343 | if len(args) > 3: |
| 344 | raise TypeError('Too many positional argument') |
| 345 | if not self._unix_socket_arg(host, port, args, kwargs): |
| 346 | self._deprecated_arg(args, kwargs, 0, 'certfile') |
| 347 | self._deprecated_arg(args, kwargs, 1, 'unix_socket') |
| 348 | self._deprecated_arg(args, kwargs, 2, 'ciphers') |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 349 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 350 | if 'ssl_context' not in kwargs: |
| 351 | # Preserve existing behaviors for default values |
| 352 | if 'cert_reqs' not in kwargs: |
| 353 | kwargs['cert_reqs'] = ssl.CERT_NONE |
| 354 | if'certfile' not in kwargs: |
| 355 | kwargs['certfile'] = 'cert.pem' |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 356 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 357 | unix_socket = kwargs.pop('unix_socket', None) |
Nobuaki Sukegawa | f39f7db | 2016-02-04 15:09:41 +0900 | [diff] [blame] | 358 | self._validate_callback = \ |
Nobuaki Sukegawa | f32bae7 | 2016-02-20 08:51:33 +0900 | [diff] [blame] | 359 | kwargs.pop('validate_callback', _match_hostname) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 360 | TSSLBase.__init__(self, True, None, kwargs) |
| 361 | TSocket.TServerSocket.__init__(self, host, port, unix_socket) |
Nobuaki Sukegawa | f32bae7 | 2016-02-20 08:51:33 +0900 | [diff] [blame] | 362 | if self._should_verify and not _match_has_ipaddress: |
Nobuaki Sukegawa | bf9fa90 | 2016-09-04 18:49:21 +0900 | [diff] [blame] | 363 | raise ValueError('Need ipaddress and backports.ssl_match_hostname ' |
Nobuaki Sukegawa | f32bae7 | 2016-02-20 08:51:33 +0900 | [diff] [blame] | 364 | 'module to verify client certificate') |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 365 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 366 | def setCertfile(self, certfile): |
Nobuaki Sukegawa | 25536ad | 2016-02-04 15:08:55 +0900 | [diff] [blame] | 367 | """Set or change the server certificate file used to wrap new |
| 368 | connections. |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 369 | |
| 370 | @param certfile: The filename of the server certificate, |
| 371 | i.e. '/etc/certs/server.pem' |
| 372 | @type certfile: str |
| 373 | |
| 374 | Raises an IOError exception if the certfile is not present or unreadable. |
| 375 | """ |
Nobuaki Sukegawa | 6a0ca7f | 2016-02-13 03:11:16 +0900 | [diff] [blame] | 376 | warnings.warn( |
| 377 | 'setCertfile is deprecated. please use certfile property instead.', |
| 378 | DeprecationWarning, stacklevel=2) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 379 | self.certfile = certfile |
| 380 | |
| 381 | def accept(self): |
| 382 | plain_client, addr = self.handle.accept() |
| 383 | try: |
| 384 | client = self._wrap_socket(plain_client) |
James E. King III | f5f430d | 2018-06-08 03:37:55 +0000 | [diff] [blame] | 385 | except (ssl.SSLError, socket.error, OSError): |
Nobuaki Sukegawa | ce1c8ab | 2016-02-11 18:21:39 +0900 | [diff] [blame] | 386 | logger.exception('Error while accepting from %s', addr) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 387 | # failed handshake/ssl wrap, close socket to client |
| 388 | plain_client.close() |
| 389 | # raise |
| 390 | # We can't raise the exception, because it kills most TServer derived |
| 391 | # serve() methods. |
| 392 | # Instead, return None, and let the TServer instance deal with it in |
| 393 | # other exception handling. (but TSimpleServer dies anyway) |
| 394 | return None |
Nobuaki Sukegawa | f39f7db | 2016-02-04 15:09:41 +0900 | [diff] [blame] | 395 | |
| 396 | if self._should_verify: |
| 397 | client.peercert = client.getpeercert() |
| 398 | try: |
| 399 | self._validate_callback(client.peercert, addr[0]) |
| 400 | client.is_valid = True |
| 401 | except Exception: |
Alexander Shadchin | 0bc2cb9 | 2024-07-21 23:27:26 +0300 | [diff] [blame] | 402 | logger.warning('Failed to validate client certificate address: %s', |
| 403 | addr[0], exc_info=True) |
Nobuaki Sukegawa | f39f7db | 2016-02-04 15:09:41 +0900 | [diff] [blame] | 404 | client.close() |
| 405 | plain_client.close() |
| 406 | return None |
| 407 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 408 | result = TSocket.TSocket() |
Nobuaki Sukegawa | 6a0ca7f | 2016-02-13 03:11:16 +0900 | [diff] [blame] | 409 | result.handle = client |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 410 | return result |