1 """This is a temporary hack to fix an SSL bug in Python versions before 3.2.
2 It is used automatically when you download an HTTPS URL using a Fetcher.
4 Copyright: Python Software Foundation
6 Downloaded from: http://pypi.python.org/pypi/backports.ssl_match_hostname/"""
8 # pylint: disable=W0312
12 class CertificateError(ValueError):
15 def _dnsname_to_pat(dn
):
17 for frag
in dn
.split(r
'.'):
19 # When '*' is a fragment by itself, it matches a non-empty dotless
23 # Otherwise, '*' matches any dotless fragment.
24 frag
= re
.escape(frag
)
25 pats
.append(frag
.replace(r
'\*', '[^.]*'))
26 return re
.compile(r
'\A' + r
'\.'.join(pats
) + r
'\Z', re
.IGNORECASE
)
28 def match_hostname(cert
, hostname
):
29 """Verify that *cert* (in decoded format as returned by
30 SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 rules
31 are mostly followed, but IP addresses are not accepted for *hostname*.
33 CertificateError is raised on failure. On success, the function
37 raise ValueError("empty or no certificate")
39 san
= cert
.get('subjectAltName', ())
40 for key
, value
in san
:
42 if _dnsname_to_pat(value
).match(hostname
):
44 dnsnames
.append(value
)
46 # The subject is only checked when subjectAltName is empty
47 for sub
in cert
.get('subject', ()):
48 for key
, value
in sub
:
49 # XXX according to RFC 2818, the most specific Common Name
51 if key
== 'commonName':
52 if _dnsname_to_pat(value
).match(hostname
):
54 dnsnames
.append(value
)
56 raise CertificateError("hostname %r "
57 "doesn't match either of %s"
58 % (hostname
, ', '.join(map(repr, dnsnames
))))
59 elif len(dnsnames
) == 1:
60 raise CertificateError("hostname %r "
62 % (hostname
, dnsnames
[0]))
64 raise CertificateError("no appropriate commonName or "
65 "subjectAltName fields were found")