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 | |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 20 | import os |
| 21 | import socket |
| 22 | import ssl |
Bryan Duxbury | 2b969ad | 2011-02-22 18:20:53 +0000 | [diff] [blame] | 23 | |
| 24 | from thrift.transport import TSocket |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 25 | from thrift.transport.TTransport import TTransportException |
Bryan Duxbury | 2b969ad | 2011-02-22 18:20:53 +0000 | [diff] [blame] | 26 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 27 | |
Bryan Duxbury | 2b969ad | 2011-02-22 18:20:53 +0000 | [diff] [blame] | 28 | class TSSLSocket(TSocket.TSocket): |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 29 | """ |
| 30 | SSL implementation of client-side TSocket |
| 31 | |
| 32 | This class creates outbound sockets wrapped using the |
| 33 | python standard ssl module for encrypted connections. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 34 | |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 35 | The protocol used is set using the class variable |
| 36 | SSL_VERSION, which must be one of ssl.PROTOCOL_* and |
| 37 | defaults to ssl.PROTOCOL_TLSv1 for greatest security. |
| 38 | """ |
| 39 | SSL_VERSION = ssl.PROTOCOL_TLSv1 |
| 40 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 41 | def __init__(self, |
| 42 | host='localhost', |
| 43 | port=9090, |
| 44 | validate=True, |
| 45 | ca_certs=None, |
Jake Farrell | 877125c | 2013-06-07 23:47:22 -0400 | [diff] [blame^] | 46 | keyfile=None, |
| 47 | certfile=None, |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 48 | unix_socket=None): |
| 49 | """Create SSL TSocket |
| 50 | |
| 51 | @param validate: Set to False to disable SSL certificate validation |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 52 | @type validate: bool |
| 53 | @param ca_certs: Filename to the Certificate Authority pem file, possibly a |
| 54 | file downloaded from: http://curl.haxx.se/ca/cacert.pem This is passed to |
| 55 | the ssl_wrap function as the 'ca_certs' parameter. |
| 56 | @type ca_certs: str |
Jake Farrell | 877125c | 2013-06-07 23:47:22 -0400 | [diff] [blame^] | 57 | @param keyfile: The private key |
| 58 | @type keyfile: str |
| 59 | @param certfile: The cert file |
| 60 | @type certfile: str |
| 61 | |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 62 | Raises an IOError exception if validate is True and the ca_certs file is |
| 63 | None, not present or unreadable. |
| 64 | """ |
| 65 | self.validate = validate |
| 66 | self.is_valid = False |
| 67 | self.peercert = None |
| 68 | if not validate: |
| 69 | self.cert_reqs = ssl.CERT_NONE |
| 70 | else: |
| 71 | self.cert_reqs = ssl.CERT_REQUIRED |
| 72 | self.ca_certs = ca_certs |
Jake Farrell | 877125c | 2013-06-07 23:47:22 -0400 | [diff] [blame^] | 73 | self.keyfile = keyfile |
| 74 | self.certfile = certfile |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 75 | if validate: |
| 76 | if ca_certs is None or not os.access(ca_certs, os.R_OK): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 77 | raise IOError('Certificate Authority ca_certs file "%s" ' |
| 78 | 'is not readable, cannot validate SSL ' |
| 79 | 'certificates.' % (ca_certs)) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 80 | TSocket.TSocket.__init__(self, host, port, unix_socket) |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 81 | |
| 82 | def open(self): |
| 83 | try: |
| 84 | res0 = self._resolveAddr() |
| 85 | for res in res0: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 86 | sock_family, sock_type = res[0:2] |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 87 | ip_port = res[4] |
| 88 | plain_sock = socket.socket(sock_family, sock_type) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 89 | self.handle = ssl.wrap_socket(plain_sock, |
| 90 | ssl_version=self.SSL_VERSION, |
| 91 | do_handshake_on_connect=True, |
| 92 | ca_certs=self.ca_certs, |
Jake Farrell | 877125c | 2013-06-07 23:47:22 -0400 | [diff] [blame^] | 93 | keyfile=self.keyfile, |
| 94 | certfile=self.certfile, |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 95 | cert_reqs=self.cert_reqs) |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 96 | self.handle.settimeout(self._timeout) |
Bryan Duxbury | 2b969ad | 2011-02-22 18:20:53 +0000 | [diff] [blame] | 97 | try: |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 98 | self.handle.connect(ip_port) |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 99 | except socket.error, e: |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 100 | if res is not res0[-1]: |
| 101 | continue |
Bryan Duxbury | 2b969ad | 2011-02-22 18:20:53 +0000 | [diff] [blame] | 102 | else: |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 103 | raise e |
| 104 | break |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 105 | except socket.error, e: |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 106 | if self._unix_socket: |
Roger Meier | 52820d0 | 2012-11-08 23:11:14 +0000 | [diff] [blame] | 107 | message = 'Could not connect to secure socket %s: %s' \ |
| 108 | % (self._unix_socket, e) |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 109 | else: |
Roger Meier | 52820d0 | 2012-11-08 23:11:14 +0000 | [diff] [blame] | 110 | message = 'Could not connect to %s:%d: %s' % (self.host, self.port, e) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 111 | raise TTransportException(type=TTransportException.NOT_OPEN, |
| 112 | message=message) |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 113 | if self.validate: |
| 114 | self._validate_cert() |
| 115 | |
| 116 | def _validate_cert(self): |
| 117 | """internal method to validate the peer's SSL certificate, and to check the |
| 118 | commonName of the certificate to ensure it matches the hostname we |
| 119 | used to make this connection. Does not support subjectAltName records |
| 120 | in certificates. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 121 | |
| 122 | raises TTransportException if the certificate fails validation. |
| 123 | """ |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 124 | cert = self.handle.getpeercert() |
| 125 | self.peercert = cert |
| 126 | if 'subject' not in cert: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 127 | raise TTransportException( |
| 128 | type=TTransportException.NOT_OPEN, |
| 129 | message='No SSL certificate found from %s:%s' % (self.host, self.port)) |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 130 | fields = cert['subject'] |
| 131 | for field in fields: |
| 132 | # ensure structure we get back is what we expect |
| 133 | if not isinstance(field, tuple): |
| 134 | continue |
| 135 | cert_pair = field[0] |
| 136 | if len(cert_pair) < 2: |
| 137 | continue |
| 138 | cert_key, cert_value = cert_pair[0:2] |
| 139 | if cert_key != 'commonName': |
| 140 | continue |
| 141 | certhost = cert_value |
Jake Farrell | 877125c | 2013-06-07 23:47:22 -0400 | [diff] [blame^] | 142 | # this check should be performed by some sort of Access Manager |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 143 | if certhost == self.host: |
| 144 | # success, cert commonName matches desired hostname |
| 145 | self.is_valid = True |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 146 | return |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 147 | else: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 148 | raise TTransportException( |
| 149 | type=TTransportException.UNKNOWN, |
| 150 | message='Hostname we connected to "%s" doesn\'t match certificate ' |
| 151 | 'provided commonName "%s"' % (self.host, certhost)) |
| 152 | raise TTransportException( |
| 153 | type=TTransportException.UNKNOWN, |
| 154 | message='Could not validate SSL certificate from ' |
| 155 | 'host "%s". Cert=%s' % (self.host, cert)) |
| 156 | |
Bryan Duxbury | 2b969ad | 2011-02-22 18:20:53 +0000 | [diff] [blame] | 157 | |
| 158 | class TSSLServerSocket(TSocket.TServerSocket): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 159 | """SSL implementation of TServerSocket |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 160 | |
| 161 | This uses the ssl module's wrap_socket() method to provide SSL |
| 162 | negotiated encryption. |
| 163 | """ |
| 164 | SSL_VERSION = ssl.PROTOCOL_TLSv1 |
| 165 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 166 | def __init__(self, |
| 167 | host=None, |
| 168 | port=9090, |
| 169 | certfile='cert.pem', |
| 170 | unix_socket=None): |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 171 | """Initialize a TSSLServerSocket |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 172 | |
| 173 | @param certfile: filename of the server certificate, defaults to cert.pem |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 174 | @type certfile: str |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 175 | @param host: The hostname or IP to bind the listen socket to, |
| 176 | i.e. 'localhost' for only allowing local network connections. |
| 177 | Pass None to bind to all interfaces. |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 178 | @type host: str |
| 179 | @param port: The port to listen on for inbound connections. |
| 180 | @type port: int |
| 181 | """ |
| 182 | self.setCertfile(certfile) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 183 | TSocket.TServerSocket.__init__(self, host, port) |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 184 | |
| 185 | def setCertfile(self, certfile): |
| 186 | """Set or change the server certificate file used to wrap new connections. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 187 | |
| 188 | @param certfile: The filename of the server certificate, |
| 189 | i.e. '/etc/certs/server.pem' |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 190 | @type certfile: str |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 191 | |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 192 | Raises an IOError exception if the certfile is not present or unreadable. |
| 193 | """ |
| 194 | if not os.access(certfile, os.R_OK): |
| 195 | raise IOError('No such certfile found: %s' % (certfile)) |
| 196 | self.certfile = certfile |
| 197 | |
| 198 | def accept(self): |
| 199 | plain_client, addr = self.handle.accept() |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 200 | try: |
| 201 | client = ssl.wrap_socket(plain_client, certfile=self.certfile, |
| 202 | server_side=True, ssl_version=self.SSL_VERSION) |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 203 | except ssl.SSLError, ssl_exc: |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 204 | # failed handshake/ssl wrap, close socket to client |
| 205 | plain_client.close() |
| 206 | # raise ssl_exc |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 207 | # We can't raise the exception, because it kills most TServer derived |
| 208 | # serve() methods. |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 209 | # Instead, return None, and let the TServer instance deal with it in |
| 210 | # other exception handling. (but TSimpleServer dies anyway) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 211 | return None |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 212 | result = TSocket.TSocket() |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 213 | result.setHandle(client) |
| 214 | return result |