churchyard / rpms / python3

Forked from rpms/python3 6 years ago
Clone

Blame 00183-cve-2013-2099-fix-ssl-match_hostname-dos.patch

22e1cc9
# HG changeset patch
22e1cc9
# User Antoine Pitrou <solipsis@pitrou.net>
22e1cc9
# Date 1368892602 -7200
22e1cc9
# Node ID c627638753e2d25a98950585b259104a025937a9
22e1cc9
# Parent  9682241dc8fcb4b1aef083bd30860efa070c3d6d
22e1cc9
Issue #17980: Fix possible abuse of ssl.match_hostname() for denial of service using certificates with many wildcards (CVE-2013-2099).
22e1cc9
22e1cc9
diff --git a/Lib/ssl.py b/Lib/ssl.py
22e1cc9
--- a/Lib/ssl.py
22e1cc9
+++ b/Lib/ssl.py
22e1cc9
@@ -129,9 +129,16 @@ class CertificateError(ValueError):
22e1cc9
     pass
22e1cc9
 
22e1cc9
 
22e1cc9
-def _dnsname_to_pat(dn):
22e1cc9
+def _dnsname_to_pat(dn, max_wildcards=1):
22e1cc9
     pats = []
22e1cc9
     for frag in dn.split(r'.'):
22e1cc9
+        if frag.count('*') > max_wildcards:
22e1cc9
+            # Issue #17980: avoid denials of service by refusing more
22e1cc9
+            # than one wildcard per fragment.  A survery of established
22e1cc9
+            # policy among SSL implementations showed it to be a
22e1cc9
+            # reasonable choice.
22e1cc9
+            raise CertificateError(
22e1cc9
+                "too many wildcards in certificate DNS name: " + repr(dn))
22e1cc9
         if frag == '*':
22e1cc9
             # When '*' is a fragment by itself, it matches a non-empty dotless
22e1cc9
             # fragment.
22e1cc9
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
22e1cc9
--- a/Lib/test/test_ssl.py
22e1cc9
+++ b/Lib/test/test_ssl.py
22e1cc9
@@ -349,6 +349,17 @@ class BasicSocketTests(unittest.TestCase
22e1cc9
         self.assertRaises(ValueError, ssl.match_hostname, None, 'example.com')
22e1cc9
         self.assertRaises(ValueError, ssl.match_hostname, {}, 'example.com')
22e1cc9
 
22e1cc9
+        # Issue #17980: avoid denials of service by refusing more than one
22e1cc9
+        # wildcard per fragment.
22e1cc9
+        cert = {'subject': ((('commonName', 'a*b.com'),),)}
22e1cc9
+        ok(cert, 'axxb.com')
22e1cc9
+        cert = {'subject': ((('commonName', 'a*b.co*'),),)}
22e1cc9
+        ok(cert, 'axxb.com')
22e1cc9
+        cert = {'subject': ((('commonName', 'a*b*.com'),),)}
22e1cc9
+        with self.assertRaises(ssl.CertificateError) as cm:
22e1cc9
+            ssl.match_hostname(cert, 'axxbxxc.com')
22e1cc9
+        self.assertIn("too many wildcards", str(cm.exception))
22e1cc9
+
22e1cc9
     def test_server_side(self):
22e1cc9
         # server_hostname doesn't work for server sockets
22e1cc9
         ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)