ssl_match_hostname.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. """The match_hostname() function from Python 3.5, essential when using SSL."""
  2. # Note: This file is under the PSF license as the code comes from the python
  3. # stdlib. http://docs.python.org/3/license.html
  4. # It is modified to remove commonName support.
  5. from __future__ import annotations
  6. import ipaddress
  7. import re
  8. import typing
  9. from ipaddress import IPv4Address, IPv6Address
  10. if typing.TYPE_CHECKING:
  11. from .ssl_ import _TYPE_PEER_CERT_RET_DICT
  12. __version__ = "3.5.0.1"
  13. class CertificateError(ValueError):
  14. pass
  15. def _dnsname_match(
  16. dn: typing.Any, hostname: str, max_wildcards: int = 1
  17. ) -> typing.Match[str] | None | bool:
  18. """Matching according to RFC 6125, section 6.4.3
  19. http://tools.ietf.org/html/rfc6125#section-6.4.3
  20. """
  21. pats = []
  22. if not dn:
  23. return False
  24. # Ported from python3-syntax:
  25. # leftmost, *remainder = dn.split(r'.')
  26. parts = dn.split(r".")
  27. leftmost = parts[0]
  28. remainder = parts[1:]
  29. wildcards = leftmost.count("*")
  30. if wildcards > max_wildcards:
  31. # Issue #17980: avoid denials of service by refusing more
  32. # than one wildcard per fragment. A survey of established
  33. # policy among SSL implementations showed it to be a
  34. # reasonable choice.
  35. raise CertificateError(
  36. "too many wildcards in certificate DNS name: " + repr(dn)
  37. )
  38. # speed up common case w/o wildcards
  39. if not wildcards:
  40. return bool(dn.lower() == hostname.lower())
  41. # RFC 6125, section 6.4.3, subitem 1.
  42. # The client SHOULD NOT attempt to match a presented identifier in which
  43. # the wildcard character comprises a label other than the left-most label.
  44. if leftmost == "*":
  45. # When '*' is a fragment by itself, it matches a non-empty dotless
  46. # fragment.
  47. pats.append("[^.]+")
  48. elif leftmost.startswith("xn--") or hostname.startswith("xn--"):
  49. # RFC 6125, section 6.4.3, subitem 3.
  50. # The client SHOULD NOT attempt to match a presented identifier
  51. # where the wildcard character is embedded within an A-label or
  52. # U-label of an internationalized domain name.
  53. pats.append(re.escape(leftmost))
  54. else:
  55. # Otherwise, '*' matches any dotless string, e.g. www*
  56. pats.append(re.escape(leftmost).replace(r"\*", "[^.]*"))
  57. # add the remaining fragments, ignore any wildcards
  58. for frag in remainder:
  59. pats.append(re.escape(frag))
  60. pat = re.compile(r"\A" + r"\.".join(pats) + r"\Z", re.IGNORECASE)
  61. return pat.match(hostname)
  62. def _ipaddress_match(ipname: str, host_ip: IPv4Address | IPv6Address) -> bool:
  63. """Exact matching of IP addresses.
  64. RFC 9110 section 4.3.5: "A reference identity of IP-ID contains the decoded
  65. bytes of the IP address. An IP version 4 address is 4 octets, and an IP
  66. version 6 address is 16 octets. [...] A reference identity of type IP-ID
  67. matches if the address is identical to an iPAddress value of the
  68. subjectAltName extension of the certificate."
  69. """
  70. # OpenSSL may add a trailing newline to a subjectAltName's IP address
  71. # Divergence from upstream: ipaddress can't handle byte str
  72. ip = ipaddress.ip_address(ipname.rstrip())
  73. return bool(ip.packed == host_ip.packed)
  74. def match_hostname(
  75. cert: _TYPE_PEER_CERT_RET_DICT | None,
  76. hostname: str,
  77. hostname_checks_common_name: bool = False,
  78. ) -> None:
  79. """Verify that *cert* (in decoded format as returned by
  80. SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125
  81. rules are followed, but IP addresses are not accepted for *hostname*.
  82. CertificateError is raised on failure. On success, the function
  83. returns nothing.
  84. """
  85. if not cert:
  86. raise ValueError(
  87. "empty or no certificate, match_hostname needs a "
  88. "SSL socket or SSL context with either "
  89. "CERT_OPTIONAL or CERT_REQUIRED"
  90. )
  91. try:
  92. # Divergence from upstream: ipaddress can't handle byte str
  93. #
  94. # The ipaddress module shipped with Python < 3.9 does not support
  95. # scoped IPv6 addresses so we unconditionally strip the Zone IDs for
  96. # now. Once we drop support for Python 3.9 we can remove this branch.
  97. if "%" in hostname:
  98. host_ip = ipaddress.ip_address(hostname[: hostname.rfind("%")])
  99. else:
  100. host_ip = ipaddress.ip_address(hostname)
  101. except ValueError:
  102. # Not an IP address (common case)
  103. host_ip = None
  104. dnsnames = []
  105. san: tuple[tuple[str, str], ...] = cert.get("subjectAltName", ())
  106. key: str
  107. value: str
  108. for key, value in san:
  109. if key == "DNS":
  110. if host_ip is None and _dnsname_match(value, hostname):
  111. return
  112. dnsnames.append(value)
  113. elif key == "IP Address":
  114. if host_ip is not None and _ipaddress_match(value, host_ip):
  115. return
  116. dnsnames.append(value)
  117. # We only check 'commonName' if it's enabled and we're not verifying
  118. # an IP address. IP addresses aren't valid within 'commonName'.
  119. if hostname_checks_common_name and host_ip is None and not dnsnames:
  120. for sub in cert.get("subject", ()):
  121. for key, value in sub:
  122. if key == "commonName":
  123. if _dnsname_match(value, hostname):
  124. return
  125. dnsnames.append(value)
  126. if len(dnsnames) > 1:
  127. raise CertificateError(
  128. "hostname %r "
  129. "doesn't match either of %s" % (hostname, ", ".join(map(repr, dnsnames)))
  130. )
  131. elif len(dnsnames) == 1:
  132. raise CertificateError(f"hostname {hostname!r} doesn't match {dnsnames[0]!r}")
  133. else:
  134. raise CertificateError("no appropriate subjectAltName fields were found")