Matej Stuchlik 54afb02
--- a/Doc/library/ssl.rst
Matej Stuchlik 54afb02
+++ b/Doc/library/ssl.rst
Matej Stuchlik 54afb02
@@ -283,10 +283,10 @@ Certificate handling
Matej Stuchlik 54afb02
    Verify that *cert* (in decoded format as returned by
Matej Stuchlik 54afb02
    :meth:`SSLSocket.getpeercert`) matches the given *hostname*.  The rules
Matej Stuchlik 54afb02
    applied are those for checking the identity of HTTPS servers as outlined
Matej Stuchlik 54afb02
-   in :rfc:`2818`, except that IP addresses are not currently supported.
Matej Stuchlik 54afb02
-   In addition to HTTPS, this function should be suitable for checking the
Matej Stuchlik 54afb02
-   identity of servers in various SSL-based protocols such as FTPS, IMAPS,
Matej Stuchlik 54afb02
-   POPS and others.
Matej Stuchlik 54afb02
+   in :rfc:`2818` and :rfc:`6125`, except that IP addresses are not currently
Matej Stuchlik 54afb02
+   supported. In addition to HTTPS, this function should be suitable for
Matej Stuchlik 54afb02
+   checking the identity of servers in various SSL-based protocols such as
Matej Stuchlik 54afb02
+   FTPS, IMAPS, POPS and others.
Matej Stuchlik 54afb02
 
Matej Stuchlik 54afb02
    :exc:`CertificateError` is raised on failure. On success, the function
Matej Stuchlik 54afb02
    returns nothing::
Matej Stuchlik 54afb02
@@ -301,6 +301,13 @@ Certificate handling
Matej Stuchlik 54afb02
 
Matej Stuchlik 54afb02
    .. versionadded:: 3.2
Matej Stuchlik 54afb02
 
Matej Stuchlik 54afb02
+   .. versionchanged:: 3.3.3
Matej Stuchlik 54afb02
+      The function now follows :rfc:`6125`, section 6.4.3 and does neither
Matej Stuchlik 54afb02
+      match multiple wildcards (e.g. ``*.*.com`` or ``*a*.example.org``) nor
Matej Stuchlik 54afb02
+      a wildcard inside an internationalized domain names (IDN) fragment.
Matej Stuchlik 54afb02
+      IDN A-labels such as ``www*.xn--pthon-kva.org`` are still supported,
Matej Stuchlik 54afb02
+      but ``x*.python.org`` no longer matches ``xn--tda.python.org``.
Matej Stuchlik 54afb02
+
Matej Stuchlik 54afb02
 .. function:: cert_time_to_seconds(timestring)
Matej Stuchlik 54afb02
 
Matej Stuchlik 54afb02
    Returns a floating-point value containing a normal seconds-after-the-epoch
Matej Stuchlik 54afb02
unchanged:
Matej Stuchlik 54afb02
--- a/Lib/ssl.py
Matej Stuchlik 54afb02
+++ b/Lib/ssl.py
Matej Stuchlik 54afb02
@@ -129,25 +129,53 @@ class CertificateError(ValueError):
Matej Stuchlik 54afb02
     pass
Matej Stuchlik 54afb02
 
Matej Stuchlik 54afb02
 
Matej Stuchlik 54afb02
-def _dnsname_to_pat(dn, max_wildcards=1):
Matej Stuchlik 54afb02
+def _dnsname_match(dn, hostname, max_wildcards=1):
Matej Stuchlik 54afb02
+    """Matching according to RFC 6125, section 6.4.3
Matej Stuchlik 54afb02
+
Matej Stuchlik 54afb02
+    http://tools.ietf.org/html/rfc6125#section-6.4.3
Matej Stuchlik 54afb02
+    """
Matej Stuchlik 54afb02
     pats = []
Matej Stuchlik 54afb02
-    for frag in dn.split(r'.'):
Matej Stuchlik 54afb02
-        if frag.count('*') > max_wildcards:
Matej Stuchlik 54afb02
-            # Issue #17980: avoid denials of service by refusing more
Matej Stuchlik 54afb02
-            # than one wildcard per fragment.  A survery of established
Matej Stuchlik 54afb02
-            # policy among SSL implementations showed it to be a
Matej Stuchlik 54afb02
-            # reasonable choice.
Matej Stuchlik 54afb02
-            raise CertificateError(
Matej Stuchlik 54afb02
-                "too many wildcards in certificate DNS name: " + repr(dn))
Matej Stuchlik 54afb02
-        if frag == '*':
Matej Stuchlik 54afb02
-            # When '*' is a fragment by itself, it matches a non-empty dotless
Matej Stuchlik 54afb02
-            # fragment.
Matej Stuchlik 54afb02
-            pats.append('[^.]+')
Matej Stuchlik 54afb02
-        else:
Matej Stuchlik 54afb02
-            # Otherwise, '*' matches any dotless fragment.
Matej Stuchlik 54afb02
-            frag = re.escape(frag)
Matej Stuchlik 54afb02
-            pats.append(frag.replace(r'\*', '[^.]*'))
Matej Stuchlik 54afb02
-    return re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE)
Matej Stuchlik 54afb02
+    if not dn:
Matej Stuchlik 54afb02
+        return False
Matej Stuchlik 54afb02
+
Matej Stuchlik 54afb02
+    leftmost, *remainder = dn.split(r'.')
Matej Stuchlik 54afb02
+
Matej Stuchlik 54afb02
+    wildcards = leftmost.count('*')
Matej Stuchlik 54afb02
+    if wildcards > max_wildcards:
Matej Stuchlik 54afb02
+        # Issue #17980: avoid denials of service by refusing more
Matej Stuchlik 54afb02
+        # than one wildcard per fragment.  A survery of established
Matej Stuchlik 54afb02
+        # policy among SSL implementations showed it to be a
Matej Stuchlik 54afb02
+        # reasonable choice.
Matej Stuchlik 54afb02
+        raise CertificateError(
Matej Stuchlik 54afb02
+            "too many wildcards in certificate DNS name: " + repr(dn))
Matej Stuchlik 54afb02
+
Matej Stuchlik 54afb02
+    # speed up common case w/o wildcards
Matej Stuchlik 54afb02
+    if not wildcards:
Matej Stuchlik 54afb02
+        return dn.lower() == hostname.lower()
Matej Stuchlik 54afb02
+
Matej Stuchlik 54afb02
+    # RFC 6125, section 6.4.3, subitem 1.
Matej Stuchlik 54afb02
+    # The client SHOULD NOT attempt to match a presented identifier in which
Matej Stuchlik 54afb02
+    # the wildcard character comprises a label other than the left-most label.
Matej Stuchlik 54afb02
+    if leftmost == '*':
Matej Stuchlik 54afb02
+        # When '*' is a fragment by itself, it matches a non-empty dotless
Matej Stuchlik 54afb02
+        # fragment.
Matej Stuchlik 54afb02
+        pats.append('[^.]+')
Matej Stuchlik 54afb02
+    elif leftmost.startswith('xn--') or hostname.startswith('xn--'):
Matej Stuchlik 54afb02
+        # RFC 6125, section 6.4.3, subitem 3.
Matej Stuchlik 54afb02
+        # The client SHOULD NOT attempt to match a presented identifier
Matej Stuchlik 54afb02
+        # where the wildcard character is embedded within an A-label or
Matej Stuchlik 54afb02
+        # U-label of an internationalized domain name.
Matej Stuchlik 54afb02
+        pats.append(re.escape(leftmost))
Matej Stuchlik 54afb02
+    else:
Matej Stuchlik 54afb02
+        # Otherwise, '*' matches any dotless string, e.g. www*
Matej Stuchlik 54afb02
+        pats.append(re.escape(leftmost).replace(r'\*', '[^.]*'))
Matej Stuchlik 54afb02
+
Matej Stuchlik 54afb02
+    # add the remaining fragments, ignore any wildcards
Matej Stuchlik 54afb02
+    for frag in remainder:
Matej Stuchlik 54afb02
+        pats.append(re.escape(frag))
Matej Stuchlik 54afb02
+
Matej Stuchlik 54afb02
+    pat = re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE)
Matej Stuchlik 54afb02
+    return pat.match(hostname)
Matej Stuchlik 54afb02
 
Matej Stuchlik 54afb02
 
Matej Stuchlik 54afb02
 def match_hostname(cert, hostname):
Matej Stuchlik 54afb02
unchanged:
Matej Stuchlik 54afb02
--- a/Lib/test/test_ssl.py
Matej Stuchlik 54afb02
+++ b/Lib/test/test_ssl.py
Matej Stuchlik 54afb02
@@ -304,11 +304,7 @@ class BasicSocketTests(unittest.TestCase
Matej Stuchlik 54afb02
         fail(cert, 'Xa.com')
Matej Stuchlik 54afb02
         fail(cert, '.a.com')
Matej Stuchlik 54afb02
 
Matej Stuchlik 54afb02
-        cert = {'subject': ((('commonName', 'a.*.com'),),)}
Matej Stuchlik 54afb02
-        ok(cert, 'a.foo.com')
Matej Stuchlik 54afb02
-        fail(cert, 'a..com')
Matej Stuchlik 54afb02
-        fail(cert, 'a.com')
Matej Stuchlik 54afb02
-
Matej Stuchlik 54afb02
+        # only match one left-most wildcard
Matej Stuchlik 54afb02
         cert = {'subject': ((('commonName', 'f*.com'),),)}
Matej Stuchlik 54afb02
         ok(cert, 'foo.com')
Matej Stuchlik 54afb02
         ok(cert, 'f.com')
Matej Stuchlik 54afb02
@@ -323,6 +319,36 @@ class BasicSocketTests(unittest.TestCase
Matej Stuchlik 54afb02
         fail(cert, 'example.org')
Matej Stuchlik 54afb02
         fail(cert, 'null.python.org')
Matej Stuchlik 54afb02
 
Matej Stuchlik 54afb02
+        # error cases with wildcards
Matej Stuchlik 54afb02
+        cert = {'subject': ((('commonName', '*.*.a.com'),),)}
Matej Stuchlik 54afb02
+        fail(cert, 'bar.foo.a.com')
Matej Stuchlik 54afb02
+        fail(cert, 'a.com')
Matej Stuchlik 54afb02
+        fail(cert, 'Xa.com')
Matej Stuchlik 54afb02
+        fail(cert, '.a.com')
Matej Stuchlik 54afb02
+
Matej Stuchlik 54afb02
+        cert = {'subject': ((('commonName', 'a.*.com'),),)}
Matej Stuchlik 54afb02
+        fail(cert, 'a.foo.com')
Matej Stuchlik 54afb02
+        fail(cert, 'a..com')
Matej Stuchlik 54afb02
+        fail(cert, 'a.com')
Matej Stuchlik 54afb02
+
Matej Stuchlik 54afb02
+        # wildcard doesn't match IDNA prefix 'xn--'
Matej Stuchlik 54afb02
+        idna = 'püthon.python.org'.encode("idna").decode("ascii")
Matej Stuchlik 54afb02
+        cert = {'subject': ((('commonName', idna),),)}
Matej Stuchlik 54afb02
+        ok(cert, idna)
Matej Stuchlik 54afb02
+        cert = {'subject': ((('commonName', 'x*.python.org'),),)}
Matej Stuchlik 54afb02
+        fail(cert, idna)
Matej Stuchlik 54afb02
+        cert = {'subject': ((('commonName', 'xn--p*.python.org'),),)}
Matej Stuchlik 54afb02
+        fail(cert, idna)
Matej Stuchlik 54afb02
+
Matej Stuchlik 54afb02
+        # wildcard in first fragment and  IDNA A-labels in sequent fragments
Matej Stuchlik 54afb02
+        # are supported.
Matej Stuchlik 54afb02
+        idna = 'www*.pythön.org'.encode("idna").decode("ascii")
Matej Stuchlik 54afb02
+        cert = {'subject': ((('commonName', idna),),)}
Matej Stuchlik 54afb02
+        ok(cert, 'www.pythön.org'.encode("idna").decode("ascii"))
Matej Stuchlik 54afb02
+        ok(cert, 'www1.pythön.org'.encode("idna").decode("ascii"))
Matej Stuchlik 54afb02
+        fail(cert, 'ftp.pythön.org'.encode("idna").decode("ascii"))
Matej Stuchlik 54afb02
+        fail(cert, 'pythön.org'.encode("idna").decode("ascii"))
Matej Stuchlik 54afb02
+
Matej Stuchlik 54afb02
         # Slightly fake real-world example
Matej Stuchlik 54afb02
         cert = {'notAfter': 'Jun 26 21:41:46 2011 GMT',
Matej Stuchlik 54afb02
                 'subject': ((('commonName', 'linuxfrz.org'),),),
Matej Stuchlik 54afb02
@@ -383,7 +409,7 @@ class BasicSocketTests(unittest.TestCase
Matej Stuchlik 54afb02
         cert = {'subject': ((('commonName', 'a*b.com'),),)}
Matej Stuchlik 54afb02
         ok(cert, 'axxb.com')
Matej Stuchlik 54afb02
         cert = {'subject': ((('commonName', 'a*b.co*'),),)}
Matej Stuchlik 54afb02
-        ok(cert, 'axxb.com')
Matej Stuchlik 54afb02
+        fail(cert, 'axxb.com')
Matej Stuchlik 54afb02
         cert = {'subject': ((('commonName', 'a*b*.com'),),)}
Matej Stuchlik 54afb02
         with self.assertRaises(ssl.CertificateError) as cm:
Matej Stuchlik 54afb02
             ssl.match_hostname(cert, 'axxbxxc.com')
Matej Stuchlik 54afb02
--- a/Lib/ssl.py
Matej Stuchlik 54afb02
+++ b/Lib/ssl.py
Matej Stuchlik 54afb02
@@ -192,7 +192,7 @@ def match_hostname(cert, hostname):
Matej Stuchlik 54afb02
     san = cert.get('subjectAltName', ())
Matej Stuchlik 54afb02
     for key, value in san:
Matej Stuchlik 54afb02
         if key == 'DNS':
Matej Stuchlik 54afb02
-            if _dnsname_to_pat(value).match(hostname):
Matej Stuchlik 54afb02
+            if _dnsname_match(value, hostname):
Matej Stuchlik 54afb02
                 return
Matej Stuchlik 54afb02
             dnsnames.append(value)
Matej Stuchlik 54afb02
     if not dnsnames:
Matej Stuchlik 54afb02
@@ -203,7 +203,7 @@ def match_hostname(cert, hostname):
Matej Stuchlik 54afb02
                 # XXX according to RFC 2818, the most specific Common Name
Matej Stuchlik 54afb02
                 # must be used.
Matej Stuchlik 54afb02
                 if key == 'commonName':
Matej Stuchlik 54afb02
-                    if _dnsname_to_pat(value).match(hostname):
Matej Stuchlik 54afb02
+                    if _dnsname_match(value, hostname):
Matej Stuchlik 54afb02
                         return
Matej Stuchlik 54afb02
                     dnsnames.append(value)
Matej Stuchlik 54afb02
     if len(dnsnames) > 1: