ssl_match_hostname.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. """The match_hostname() function from Python 3.3.3, 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. import re
  5. import sys
  6. # ipaddress has been backported to 2.6+ in pypi. If it is installed on the
  7. # system, use it to handle IPAddress ServerAltnames (this was added in
  8. # python-3.5) otherwise only do DNS matching. This allows
  9. # util.ssl_match_hostname to continue to be used in Python 2.7.
  10. try:
  11. import ipaddress
  12. except ImportError:
  13. ipaddress = None
  14. __version__ = "3.5.0.1"
  15. class CertificateError(ValueError):
  16. pass
  17. def _dnsname_match(dn, hostname, max_wildcards=1):
  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 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 _to_unicode(obj):
  63. if isinstance(obj, str) and sys.version_info < (3,):
  64. # ignored flake8 # F821 to support python 2.7 function
  65. obj = unicode(obj, encoding="ascii", errors="strict") # noqa: F821
  66. return obj
  67. def _ipaddress_match(ipname, host_ip):
  68. """Exact matching of IP addresses.
  69. RFC 6125 explicitly doesn't define an algorithm for this
  70. (section 1.7.2 - "Out of Scope").
  71. """
  72. # OpenSSL may add a trailing newline to a subjectAltName's IP address
  73. # Divergence from upstream: ipaddress can't handle byte str
  74. ip = ipaddress.ip_address(_to_unicode(ipname).rstrip())
  75. return ip == host_ip
  76. def match_hostname(cert, hostname):
  77. """Verify that *cert* (in decoded format as returned by
  78. SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125
  79. rules are followed, but IP addresses are not accepted for *hostname*.
  80. CertificateError is raised on failure. On success, the function
  81. returns nothing.
  82. """
  83. if not cert:
  84. raise ValueError(
  85. "empty or no certificate, match_hostname needs a "
  86. "SSL socket or SSL context with either "
  87. "CERT_OPTIONAL or CERT_REQUIRED"
  88. )
  89. try:
  90. # Divergence from upstream: ipaddress can't handle byte str
  91. host_ip = ipaddress.ip_address(_to_unicode(hostname))
  92. except ValueError:
  93. # Not an IP address (common case)
  94. host_ip = None
  95. except UnicodeError:
  96. # Divergence from upstream: Have to deal with ipaddress not taking
  97. # byte strings. addresses should be all ascii, so we consider it not
  98. # an ipaddress in this case
  99. host_ip = None
  100. except AttributeError:
  101. # Divergence from upstream: Make ipaddress library optional
  102. if ipaddress is None:
  103. host_ip = None
  104. else:
  105. raise
  106. dnsnames = []
  107. san = cert.get("subjectAltName", ())
  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. if not dnsnames:
  118. # The subject is only checked when there is no dNSName entry
  119. # in subjectAltName
  120. for sub in cert.get("subject", ()):
  121. for key, value in sub:
  122. # XXX according to RFC 2818, the most specific Common Name
  123. # must be used.
  124. if key == "commonName":
  125. if _dnsname_match(value, hostname):
  126. return
  127. dnsnames.append(value)
  128. if len(dnsnames) > 1:
  129. raise CertificateError(
  130. "hostname %r "
  131. "doesn't match either of %s" % (hostname, ", ".join(map(repr, dnsnames)))
  132. )
  133. elif len(dnsnames) == 1:
  134. raise CertificateError("hostname %r doesn't match %r" % (hostname, dnsnames[0]))
  135. else:
  136. raise CertificateError(
  137. "no appropriate commonName or subjectAltName fields were found"
  138. )