Blame SOURCES/setuptools-ssl-match_hostname-wildcard.patch

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