98def match_hostname(cert, hostname):
99 """Verify that *cert* (in decoded format as returned by
100 SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125
101 rules are followed, but IP addresses are not accepted for *hostname*.
103 CertificateError is raised on failure. On success, the function
108 "empty or no certificate, match_hostname needs a "
109 "SSL socket or SSL context with either "
110 "CERT_OPTIONAL or CERT_REQUIRED"
115 except (UnicodeError, ValueError):
121 except AttributeError:
123 if ipaddress
is None:
128 san =
cert.get(
"subjectAltName", ())
129 for key, value
in san:
134 elif key ==
"IP Address":
142 for key, value
in sub:
145 if key ==
"commonName":
149 if len(dnsnames) > 1:
152 "doesn't match either of %s" % (hostname,
", ".join(map(repr, dnsnames)))
154 elif len(dnsnames) == 1:
155 raise CertificateError(
"hostname %r doesn't match %r" % (hostname, dnsnames[0]))
158 "no appropriate commonName or subjectAltName fields were found"