Add <rename> recipe step
[zeroinstall/solver.git] / zeroinstall / support / ssl_match_hostname.py
blob55ed2f4e17bcb7256bc195142dd2c547d40d79d9
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
5 License: MIT License
6 Downloaded from: http://pypi.python.org/pypi/backports.ssl_match_hostname/"""
8 # pylint: disable=W0312
10 import re
12 class CertificateError(ValueError):
13 pass
15 def _dnsname_to_pat(dn):
16 pats = []
17 for frag in dn.split(r'.'):
18 if frag == '*':
19 # When '*' is a fragment by itself, it matches a non-empty dotless
20 # fragment.
21 pats.append('[^.]+')
22 else:
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
34 returns nothing.
35 """
36 if not cert:
37 raise ValueError("empty or no certificate")
38 dnsnames = []
39 san = cert.get('subjectAltName', ())
40 for key, value in san:
41 if key == 'DNS':
42 if _dnsname_to_pat(value).match(hostname):
43 return
44 dnsnames.append(value)
45 if not san:
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
50 # must be used.
51 if key == 'commonName':
52 if _dnsname_to_pat(value).match(hostname):
53 return
54 dnsnames.append(value)
55 if len(dnsnames) > 1:
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 "
61 "doesn't match %r"
62 % (hostname, dnsnames[0]))
63 else:
64 raise CertificateError("no appropriate commonName or "
65 "subjectAltName fields were found")