ae2451
ae2451
# HG changeset patch
ae2451
# User Benjamin Peterson <benjamin@python.org>
ae2451
# Date 1416798122 21600
ae2451
# Node ID fb83916c3ea12899569e88a7505469a90ab1f674
ae2451
# Parent  c84f36a5f556867c2ec50453dc879a500032d377
ae2451
pep 476: verify certificates by default (#22417)
ae2451
ae2451
diff --git a/Doc/library/httplib.rst b/Doc/library/httplib.rst
ae2451
--- a/Doc/library/httplib.rst
ae2451
+++ b/Doc/library/httplib.rst
ae2451
@@ -90,9 +90,6 @@ The module provides the following classe
ae2451
       server's certificate.  If you want to change that behaviour, you can
ae2451
       explicitly set *check_hostname* to False.
ae2451
 
ae2451
-   .. warning::
ae2451
-      This does not do any verification of the server's certificate.
ae2451
-
ae2451
    .. versionadded:: 2.0
ae2451
 
ae2451
    .. versionchanged:: 2.6
ae2451
@@ -104,6 +101,11 @@ The module provides the following classe
ae2451
    .. versionchanged:: 2.7.9
ae2451
       *context* and *check_hostname* was added.
ae2451
 
ae2451
+      This class now performs all the necessary certificate and hostname checks
ae2451
+      by default. To revert to the previous, unverified, behavior
ae2451
+      :func:`ssl._create_unverified_context` can be passed to the *context*
ae2451
+      parameter.
ae2451
+
ae2451
 
ae2451
 .. class:: HTTPResponse(sock, debuglevel=0, strict=0)
ae2451
 
ae2451
diff --git a/Lib/httplib.py b/Lib/httplib.py
ae2451
--- a/Lib/httplib.py
ae2451
+++ b/Lib/httplib.py
ae2451
@@ -1193,7 +1193,7 @@ else:
ae2451
             self.key_file = key_file
ae2451
             self.cert_file = cert_file
ae2451
             if context is None:
ae2451
-                context = ssl.create_default_context()
ae2451
+                context = ssl._create_default_https_context()
ae2451
             will_verify = context.verify_mode != ssl.CERT_NONE
ae2451
             if check_hostname is None:
ae2451
                 check_hostname = will_verify
ae2451
diff --git a/Lib/ssl.py b/Lib/ssl.py
ae2451
--- a/Lib/ssl.py
ae2451
+++ b/Lib/ssl.py
ae2451
@@ -427,8 +427,7 @@ def create_default_context(purpose=Purpo
ae2451
         context.load_default_certs(purpose)
ae2451
     return context
ae2451
 
ae2451
-
ae2451
-def _create_stdlib_context(protocol=PROTOCOL_SSLv23, cert_reqs=None,
ae2451
+def _create_unverified_context(protocol=PROTOCOL_SSLv23, cert_reqs=None,
ae2451
                            check_hostname=False, purpose=Purpose.SERVER_AUTH,
ae2451
                            certfile=None, keyfile=None,
ae2451
                            cafile=None, capath=None, cadata=None):
ae2451
@@ -469,6 +468,14 @@ def _create_stdlib_context(protocol=PROT
ae2451
 
ae2451
     return context
ae2451
 
ae2451
+# Used by http.client if no context is explicitly passed.
ae2451
+_create_default_https_context = create_default_context
ae2451
+
ae2451
+
ae2451
+# Backwards compatibility alias, even though it's not a public name.
ae2451
+_create_stdlib_context = _create_unverified_context
ae2451
+
ae2451
+
ae2451
 class SSLSocket(socket):
ae2451
     """This class implements a subtype of socket.socket that wraps
ae2451
     the underlying OS socket in an SSL context when necessary, and
ae2451
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
ae2451
--- a/Lib/test/test_httplib.py
ae2451
+++ b/Lib/test/test_httplib.py
ae2451
@@ -1,10 +1,9 @@
ae2451
 import httplib
ae2451
 import array
ae2451
-import httplib
ae2451
-import os
ae2451
 import StringIO
ae2451
 import socket
ae2451
 import errno
ae2451
+import os
ae2451
 
ae2451
 import unittest
ae2451
 TestCase = unittest.TestCase
ae2451
diff --git a/Lib/test/test_urllib2_localnet.py b/Lib/test/test_urllib2_localnet.py
ae2451
--- a/Lib/test/test_urllib2_localnet.py
ae2451
+++ b/Lib/test/test_urllib2_localnet.py
ae2451
@@ -5,6 +5,7 @@ import urllib2
ae2451
 import BaseHTTPServer
ae2451
 import unittest
ae2451
 import hashlib
ae2451
+import ssl
ae2451
 
ae2451
 from test import test_support
ae2451
 
ae2451
@@ -562,15 +563,37 @@ class TestUrlopen(BaseTestCase):
ae2451
                             cafile=CERT_localhost)
ae2451
         self.assertEqual(data, b"we care a bit")
ae2451
         # Bad cert
ae2451
-        with self.assertRaises(urllib2.URLError) as cm:
ae2451
+        with self.assertRaises(urllib2.URLError):
ae2451
             self.urlopen("https://localhost:%s/bizarre" % handler.port,
ae2451
                          cafile=CERT_fakehostname)
ae2451
         # Good cert, but mismatching hostname
ae2451
         handler = self.start_https_server(certfile=CERT_fakehostname)
ae2451
-        with self.assertRaises(ssl.CertificateError) as cm:
ae2451
+        with self.assertRaises(ssl.CertificateError):
ae2451
             self.urlopen("https://localhost:%s/bizarre" % handler.port,
ae2451
                          cafile=CERT_fakehostname)
ae2451
 
ae2451
+    def test_https_with_cadefault(self):
ae2451
+        handler = self.start_https_server(certfile=CERT_localhost)
ae2451
+        # Self-signed cert should fail verification with system certificate store
ae2451
+        with self.assertRaises(urllib2.URLError):
ae2451
+            self.urlopen("https://localhost:%s/bizarre" % handler.port,
ae2451
+                         cadefault=True)
ae2451
+
ae2451
+    def test_https_sni(self):
ae2451
+        if ssl is None:
ae2451
+            self.skipTest("ssl module required")
ae2451
+        if not ssl.HAS_SNI:
ae2451
+            self.skipTest("SNI support required in OpenSSL")
ae2451
+        sni_name = [None]
ae2451
+        def cb_sni(ssl_sock, server_name, initial_context):
ae2451
+            sni_name[0] = server_name
ae2451
+        context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
ae2451
+        context.set_servername_callback(cb_sni)
ae2451
+        handler = self.start_https_server(context=context, certfile=CERT_localhost)
ae2451
+        context = ssl.create_default_context(cafile=CERT_localhost)
ae2451
+        self.urlopen("https://localhost:%s" % handler.port, context=context)
ae2451
+        self.assertEqual(sni_name[0], "localhost")
ae2451
+
ae2451
     def test_sending_headers(self):
ae2451
         handler = self.start_server([(200, [], "we don't care")])
ae2451
 
ae2451
diff -up Python-2.7.5/Doc/library/xmlrpclib.rst.ver Python-2.7.5/Doc/library/xmlrpclib.rst
ae2451
--- Python-2.7.5/Doc/library/xmlrpclib.rst.ver	2015-03-30 13:59:29.243493601 +0200
ae2451
+++ Python-2.7.5/Doc/library/xmlrpclib.rst	2015-03-30 14:03:40.509532180 +0200
ae2451
@@ -34,6 +34,10 @@ between conformable Python objects and X
ae2451
    constructed data.  If you need to parse untrusted or unauthenticated data see
ae2451
    :ref:`xml-vulnerabilities`.
ae2451
 
ae2451
+.. versionchanged:: 2.7.9
ae2451
+
ae2451
+   For https URIs, :mod:`xmlrpclib` now performs all the necessary certificate
ae2451
+   and hostname checks by default
ae2451
 
ae2451
 .. class:: ServerProxy(uri[, transport[, encoding[, verbose[,  allow_none[, use_datetime]]]]])
ae2451