codekingpro/portable-devtools
114k
1"""The match_hostname() function from Python 3.5, essential when using SSL."""2 3# Note: This file is under the PSF license as the code comes from the python4# stdlib. http://docs.python.org/3/license.html5# It is modified to remove commonName support.6 7from __future__ import annotations8 9import ipaddress10import re11import typing12from ipaddress import IPv4Address, IPv6Address13 14if typing.TYPE_CHECKING:15 from .ssl_ import _TYPE_PEER_CERT_RET_DICT16 17__version__ = "3.5.0.1"18 19 20class CertificateError(ValueError):21 pass22 23 24def _dnsname_match(25 dn: typing.Any, hostname: str, max_wildcards: int = 126) -> typing.Match[str] | None | bool:27 """Matching according to RFC 6125, section 6.4.328 29 http://tools.ietf.org/html/rfc6125#section-6.4.330 """31 pats = []32 if not dn:33 return False34 35 # Ported from python3-syntax:36 # leftmost, *remainder = dn.split(r'.')37 parts = dn.split(r".")38 leftmost = parts[0]39 remainder = parts[1:]40 41 wildcards = leftmost.count("*")42 if wildcards > max_wildcards:43 # Issue #17980: avoid denials of service by refusing more44 # than one wildcard per fragment. A survey of established45 # policy among SSL implementations showed it to be a46 # reasonable choice.47 raise CertificateError(48 "too many wildcards in certificate DNS name: " + repr(dn)49 )50 51 # speed up common case w/o wildcards52 if not wildcards:53 return bool(dn.lower() == hostname.lower())54 55 # RFC 6125, section 6.4.3, subitem 1.56 # The client SHOULD NOT attempt to match a presented identifier in which57 # the wildcard character comprises a label other than the left-most label.58 if leftmost == "*":59 # When '*' is a fragment by itself, it matches a non-empty dotless60 # fragment.61 pats.append("[^.]+")62 elif leftmost.startswith("xn--") or hostname.startswith("xn--"):63 # RFC 6125, section 6.4.3, subitem 3.64 # The client SHOULD NOT attempt to match a presented identifier65 # where the wildcard character is embedded within an A-label or66 # U-label of an internationalized domain name.67 pats.append(re.escape(leftmost))68 else:69 # Otherwise, '*' matches any dotless string, e.g. www*70 pats.append(re.escape(leftmost).replace(r"\*", "[^.]*"))71 72 # add the remaining fragments, ignore any wildcards73 for frag in remainder:74 pats.append(re.escape(frag))75 76 pat = re.compile(r"\A" + r"\.".join(pats) + r"\Z", re.IGNORECASE)77 return pat.match(hostname)78 79 80def _ipaddress_match(ipname: str, host_ip: IPv4Address | IPv6Address) -> bool:81 """Exact matching of IP addresses.82 83 RFC 9110 section 4.3.5: "A reference identity of IP-ID contains the decoded84 bytes of the IP address. An IP version 4 address is 4 octets, and an IP85 version 6 address is 16 octets. [...] A reference identity of type IP-ID86 matches if the address is identical to an iPAddress value of the87 subjectAltName extension of the certificate."88 """89 # OpenSSL may add a trailing newline to a subjectAltName's IP address90 # Divergence from upstream: ipaddress can't handle byte str91 ip = ipaddress.ip_address(ipname.rstrip())92 return bool(ip.packed == host_ip.packed)93 94 95def match_hostname(96 cert: _TYPE_PEER_CERT_RET_DICT | None,97 hostname: str,98 hostname_checks_common_name: bool = False,99) -> None:100 """Verify that *cert* (in decoded format as returned by101 SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125102 rules are followed, but IP addresses are not accepted for *hostname*.103 104 CertificateError is raised on failure. On success, the function105 returns nothing.106 """107 if not cert:108 raise ValueError(109 "empty or no certificate, match_hostname needs a "110 "SSL socket or SSL context with either "111 "CERT_OPTIONAL or CERT_REQUIRED"112 )113 114 try:115 host_ip = ipaddress.ip_address(hostname)116 except ValueError:117 # Not an IP address (common case)118 host_ip = None119 dnsnames = []120 san: tuple[tuple[str, str], ...] = cert.get("subjectAltName", ())121 key: str122 value: str123 for key, value in san:124 if key == "DNS":125 if host_ip is None and _dnsname_match(value, hostname):126 return127 dnsnames.append(value)128 elif key == "IP Address":129 if host_ip is not None and _ipaddress_match(value, host_ip):130 return131 dnsnames.append(value)132 133 # We only check 'commonName' if it's enabled and we're not verifying134 # an IP address. IP addresses aren't valid within 'commonName'.135 if hostname_checks_common_name and host_ip is None and not dnsnames:136 for sub in cert.get("subject", ()):137 for key, value in sub:138 if key == "commonName":139 if _dnsname_match(value, hostname):140 return141 dnsnames.append(142 value143 ) # Defensive: for older PyPy and OpenSSL versions144 145 if len(dnsnames) > 1:146 raise CertificateError(147 "hostname %r "148 "doesn't match either of %s" % (hostname, ", ".join(map(repr, dnsnames)))149 )150 elif len(dnsnames) == 1:151 raise CertificateError(f"hostname {hostname!r} doesn't match {dnsnames[0]!r}")152 else:153 raise CertificateError("no appropriate subjectAltName fields were found")154 