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

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