blob: 8ad4ce4002b1ad96bd3efd95d104a7c7033a3c3f [file] [log] [blame]
Nobuaki Sukegawaf32bae72016-02-20 08:51:33 +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 Sukegawad2b4f242016-09-04 18:49:23 +090020import logging
Nobuaki Sukegawad9b44252016-03-01 02:09:11 +090021import sys
22
Nobuaki Sukegawaf32bae72016-02-20 08:51:33 +090023from thrift.transport.TTransport import TTransportException
24
Nobuaki Sukegawad2b4f242016-09-04 18:49:23 +090025logger = logging.getLogger(__name__)
26
Nobuaki Sukegawaf32bae72016-02-20 08:51:33 +090027
Nobuaki Sukegawae8ba7872017-02-12 21:14:48 +090028def legacy_validate_callback(cert, hostname):
Nobuaki Sukegawaf32bae72016-02-20 08:51:33 +090029 """legacy method to validate the peer's SSL certificate, and to check
30 the commonName of the certificate to ensure it matches the hostname we
31 used to make this connection. Does not support subjectAltName records
32 in certificates.
33
34 raises TTransportException if the certificate fails validation.
35 """
36 if 'subject' not in cert:
37 raise TTransportException(
38 TTransportException.NOT_OPEN,
Nobuaki Sukegawae8ba7872017-02-12 21:14:48 +090039 'No SSL certificate found from %s' % hostname)
Nobuaki Sukegawaf32bae72016-02-20 08:51:33 +090040 fields = cert['subject']
41 for field in fields:
42 # ensure structure we get back is what we expect
43 if not isinstance(field, tuple):
44 continue
45 cert_pair = field[0]
46 if len(cert_pair) < 2:
47 continue
48 cert_key, cert_value = cert_pair[0:2]
49 if cert_key != 'commonName':
50 continue
51 certhost = cert_value
52 # this check should be performed by some sort of Access Manager
53 if certhost == hostname:
54 # success, cert commonName matches desired hostname
55 return
56 else:
57 raise TTransportException(
58 TTransportException.UNKNOWN,
59 'Hostname we connected to "%s" doesn\'t match certificate '
Nobuaki Sukegawae8ba7872017-02-12 21:14:48 +090060 'provided commonName "%s"' % (hostname, certhost))
Nobuaki Sukegawaf32bae72016-02-20 08:51:33 +090061 raise TTransportException(
62 TTransportException.UNKNOWN,
63 'Could not validate SSL certificate from host "%s". Cert=%s'
64 % (hostname, cert))
65
66
Nobuaki Sukegawad2b4f242016-09-04 18:49:23 +090067def _optional_dependencies():
68 try:
69 import ipaddress # noqa
70 logger.debug('ipaddress module is available')
71 ipaddr = True
72 except ImportError:
73 logger.warn('ipaddress module is unavailable')
74 ipaddr = False
Nobuaki Sukegawaf32bae72016-02-20 08:51:33 +090075
Nobuaki Sukegawad9b44252016-03-01 02:09:11 +090076 if sys.hexversion < 0x030500F0:
Nobuaki Sukegawad2b4f242016-09-04 18:49:23 +090077 try:
78 from backports.ssl_match_hostname import match_hostname, __version__ as ver
79 ver = list(map(int, ver.split('.')))
80 logger.debug('backports.ssl_match_hostname module is available')
81 match = match_hostname
82 if ver[0] * 10 + ver[1] >= 35:
83 return ipaddr, match
84 else:
85 logger.warn('backports.ssl_match_hostname module is too old')
86 ipaddr = False
87 except ImportError:
88 logger.warn('backports.ssl_match_hostname is unavailable')
89 ipaddr = False
Nobuaki Sukegawaf32bae72016-02-20 08:51:33 +090090 try:
91 from ssl import match_hostname
Nobuaki Sukegawad2b4f242016-09-04 18:49:23 +090092 logger.debug('ssl.match_hostname is available')
93 match = match_hostname
Nobuaki Sukegawaf32bae72016-02-20 08:51:33 +090094 except ImportError:
Nobuaki Sukegawad2b4f242016-09-04 18:49:23 +090095 logger.warn('using legacy validation callback')
96 match = legacy_validate_callback
97 return ipaddr, match
98
99_match_has_ipaddress, _match_hostname = _optional_dependencies()