Nobuaki Sukegawa | f32bae7 | 2016-02-20 08:51:33 +0900 | [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 | # |
| 19 | |
Nobuaki Sukegawa | d2b4f24 | 2016-09-04 18:49:23 +0900 | [diff] [blame] | 20 | import logging |
Nobuaki Sukegawa | d9b4425 | 2016-03-01 02:09:11 +0900 | [diff] [blame] | 21 | import sys |
| 22 | |
Nobuaki Sukegawa | f32bae7 | 2016-02-20 08:51:33 +0900 | [diff] [blame] | 23 | from thrift.transport.TTransport import TTransportException |
| 24 | |
Nobuaki Sukegawa | d2b4f24 | 2016-09-04 18:49:23 +0900 | [diff] [blame] | 25 | logger = logging.getLogger(__name__) |
| 26 | |
Nobuaki Sukegawa | f32bae7 | 2016-02-20 08:51:33 +0900 | [diff] [blame] | 27 | |
Nobuaki Sukegawa | e8ba787 | 2017-02-12 21:14:48 +0900 | [diff] [blame^] | 28 | def legacy_validate_callback(cert, hostname): |
Nobuaki Sukegawa | f32bae7 | 2016-02-20 08:51:33 +0900 | [diff] [blame] | 29 | """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 Sukegawa | e8ba787 | 2017-02-12 21:14:48 +0900 | [diff] [blame^] | 39 | 'No SSL certificate found from %s' % hostname) |
Nobuaki Sukegawa | f32bae7 | 2016-02-20 08:51:33 +0900 | [diff] [blame] | 40 | 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 Sukegawa | e8ba787 | 2017-02-12 21:14:48 +0900 | [diff] [blame^] | 60 | 'provided commonName "%s"' % (hostname, certhost)) |
Nobuaki Sukegawa | f32bae7 | 2016-02-20 08:51:33 +0900 | [diff] [blame] | 61 | raise TTransportException( |
| 62 | TTransportException.UNKNOWN, |
| 63 | 'Could not validate SSL certificate from host "%s". Cert=%s' |
| 64 | % (hostname, cert)) |
| 65 | |
| 66 | |
Nobuaki Sukegawa | d2b4f24 | 2016-09-04 18:49:23 +0900 | [diff] [blame] | 67 | def _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 Sukegawa | f32bae7 | 2016-02-20 08:51:33 +0900 | [diff] [blame] | 75 | |
Nobuaki Sukegawa | d9b4425 | 2016-03-01 02:09:11 +0900 | [diff] [blame] | 76 | if sys.hexversion < 0x030500F0: |
Nobuaki Sukegawa | d2b4f24 | 2016-09-04 18:49:23 +0900 | [diff] [blame] | 77 | 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 Sukegawa | f32bae7 | 2016-02-20 08:51:33 +0900 | [diff] [blame] | 90 | try: |
| 91 | from ssl import match_hostname |
Nobuaki Sukegawa | d2b4f24 | 2016-09-04 18:49:23 +0900 | [diff] [blame] | 92 | logger.debug('ssl.match_hostname is available') |
| 93 | match = match_hostname |
Nobuaki Sukegawa | f32bae7 | 2016-02-20 08:51:33 +0900 | [diff] [blame] | 94 | except ImportError: |
Nobuaki Sukegawa | d2b4f24 | 2016-09-04 18:49:23 +0900 | [diff] [blame] | 95 | logger.warn('using legacy validation callback') |
| 96 | match = legacy_validate_callback |
| 97 | return ipaddr, match |
| 98 | |
| 99 | _match_has_ipaddress, _match_hostname = _optional_dependencies() |