92e8e94
Index: setuptools-1.1.7/setuptools/ssl_support.py
92e8e94
===================================================================
92e8e94
--- setuptools-1.1.7.orig/setuptools/ssl_support.py
92e8e94
+++ setuptools-1.1.7/setuptools/ssl_support.py
92e8e94
@@ -85,28 +85,69 @@ except ImportError:
6dfb759
 try:
6dfb759
     from ssl import CertificateError, match_hostname
6dfb759
 except ImportError:
6dfb759
+    try:
6dfb759
+        from backports.ssl_match_hostname import CertificateError
6dfb759
+        from backports.ssl_match_hostname import match_hostname
6dfb759
+    except ImportError:
6dfb759
+        CertificateError = None
6dfb759
+        match_hostname = None
6dfb759
+
6dfb759
+if not CertificateError:
6dfb759
     class CertificateError(ValueError):
6dfb759
         pass
6dfb759
 
92e8e94
-    def _dnsname_to_pat(dn, max_wildcards=1):
6dfb759
+if not match_hostname:
6dfb759
+    def _dnsname_match(dn, hostname, max_wildcards=1):
6dfb759
+        """Matching according to RFC 6125, section 6.4.3
6dfb759
+
6dfb759
+        http://tools.ietf.org/html/rfc6125#section-6.4.3
6dfb759
+        """
6dfb759
         pats = []
6dfb759
-        for frag in dn.split(r'.'):
92e8e94
-            if frag.count('*') > max_wildcards:
92e8e94
-                # Issue #17980: avoid denials of service by refusing more
92e8e94
-                # than one wildcard per fragment.  A survery of established
92e8e94
-                # policy among SSL implementations showed it to be a
92e8e94
-                # reasonable choice.
92e8e94
-                raise CertificateError(
92e8e94
-                    "too many wildcards in certificate DNS name: " + repr(dn))
6dfb759
-            if frag == '*':
6dfb759
-                # When '*' is a fragment by itself, it matches a non-empty dotless
6dfb759
-                # fragment.
6dfb759
-                pats.append('[^.]+')
6dfb759
-            else:
6dfb759
-                # Otherwise, '*' matches any dotless fragment.
6dfb759
-                frag = re.escape(frag)
6dfb759
-                pats.append(frag.replace(r'\*', '[^.]*'))
6dfb759
-        return re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE)
6dfb759
+        if not dn:
6dfb759
+            return False
6dfb759
+
6dfb759
+        # Ported from python3-syntax:
6dfb759
+        # leftmost, *remainder = dn.split(r'.')
6dfb759
+        parts = dn.split(r'.')
6dfb759
+        leftmost = parts[0]
6dfb759
+        remainder = parts[1:]
6dfb759
+
6dfb759
+        wildcards = leftmost.count('*')
6dfb759
+        if wildcards > max_wildcards:
6dfb759
+            # Issue #17980: avoid denials of service by refusing more
6dfb759
+            # than one wildcard per fragment.  A survey of established
6dfb759
+            # policy among SSL implementations showed it to be a
6dfb759
+            # reasonable choice.
6dfb759
+            raise CertificateError(
6dfb759
+                "too many wildcards in certificate DNS name: " + repr(dn))
6dfb759
+
6dfb759
+        # speed up common case w/o wildcards
6dfb759
+        if not wildcards:
6dfb759
+            return dn.lower() == hostname.lower()
6dfb759
+
6dfb759
+        # RFC 6125, section 6.4.3, subitem 1.
6dfb759
+        # The client SHOULD NOT attempt to match a presented identifier in which
6dfb759
+        # the wildcard character comprises a label other than the left-most label.
6dfb759
+        if leftmost == '*':
6dfb759
+            # When '*' is a fragment by itself, it matches a non-empty dotless
6dfb759
+            # fragment.
6dfb759
+            pats.append('[^.]+')
6dfb759
+        elif leftmost.startswith('xn--') or hostname.startswith('xn--'):
6dfb759
+            # RFC 6125, section 6.4.3, subitem 3.
6dfb759
+            # The client SHOULD NOT attempt to match a presented identifier
6dfb759
+            # where the wildcard character is embedded within an A-label or
6dfb759
+            # U-label of an internationalized domain name.
6dfb759
+            pats.append(re.escape(leftmost))
6dfb759
+        else:
6dfb759
+            # Otherwise, '*' matches any dotless string, e.g. www*
6dfb759
+            pats.append(re.escape(leftmost).replace(r'\*', '[^.]*'))
6dfb759
+
6dfb759
+        # add the remaining fragments, ignore any wildcards
6dfb759
+        for frag in remainder:
6dfb759
+            pats.append(re.escape(frag))
6dfb759
+
6dfb759
+        pat = re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE)
6dfb759
+        return pat.match(hostname)
6dfb759
 
6dfb759
     def match_hostname(cert, hostname):
6dfb759
         """Verify that *cert* (in decoded format as returned by
92e8e94
@@ -122,7 +163,7 @@ except ImportError:
6dfb759
         san = cert.get('subjectAltName', ())
6dfb759
         for key, value in san:
6dfb759
             if key == 'DNS':
6dfb759
-                if _dnsname_to_pat(value).match(hostname):
6dfb759
+                if _dnsname_match(value, hostname):
6dfb759
                     return
6dfb759
                 dnsnames.append(value)
6dfb759
         if not dnsnames:
92e8e94
@@ -133,7 +174,7 @@ except ImportError:
6dfb759
                     # XXX according to RFC 2818, the most specific Common Name
6dfb759
                     # must be used.
6dfb759
                     if key == 'commonName':
6dfb759
-                        if _dnsname_to_pat(value).match(hostname):
6dfb759
+                        if _dnsname_match(value, hostname):
6dfb759
                             return
6dfb759
                         dnsnames.append(value)
6dfb759
         if len(dnsnames) > 1: