]> granicus.if.org Git - python/commitdiff
fix sslwrap_simple (closes #22523)
authorBenjamin Peterson <benjamin@python.org>
Thu, 2 Oct 2014 03:53:01 +0000 (23:53 -0400)
committerBenjamin Peterson <benjamin@python.org>
Thu, 2 Oct 2014 03:53:01 +0000 (23:53 -0400)
Thanks Alex Gaynor.

Lib/ssl.py
Lib/test/test_ssl.py
Modules/_ssl.c

index 43c91a24acbd69c39821ed180f4310583633a053..5bc07a7d497b43ccaf8e3210a0655d39f126d2e5 100644 (file)
@@ -969,16 +969,16 @@ def get_protocol_name(protocol_code):
 # a replacement for the old socket.ssl function
 
 def sslwrap_simple(sock, keyfile=None, certfile=None):
-
     """A replacement for the old socket.ssl function.  Designed
     for compability with Python 2.5 and earlier.  Will disappear in
     Python 3.0."""
-
     if hasattr(sock, "_sock"):
         sock = sock._sock
 
-    ssl_sock = _ssl.sslwrap(sock, 0, keyfile, certfile, CERT_NONE,
-                            PROTOCOL_SSLv23, None)
+    ctx = SSLContext(PROTOCOL_SSLv23)
+    if keyfile or certfile:
+        ctx.load_cert_chain(certfile, keyfile)
+    ssl_sock = ctx._wrap_socket(sock, server_side=False)
     try:
         sock.getpeername()
     except socket_error:
index eac994f1230ed1f2e3dc4cd42ec486ae1b33e7c1..75bb1e0f0800d87e0296c6331b2ee814a2de32fd 100644 (file)
@@ -94,6 +94,8 @@ class BasicTests(unittest.TestCase):
                 pass
             else:
                 raise
+
+
 def can_clear_options():
     # 0.9.8m or higher
     return ssl._OPENSSL_API_VERSION >= (0, 9, 8, 13, 15)
@@ -2944,7 +2946,7 @@ def test_main(verbose=False):
         if not os.path.exists(filename):
             raise support.TestFailed("Can't read certificate file %r" % filename)
 
-    tests = [ContextTests, BasicSocketTests, SSLErrorTests]
+    tests = [ContextTests, BasicTests, BasicSocketTests, SSLErrorTests]
 
     if support.is_resource_enabled('network'):
         tests.append(NetworkedTests)
index 04d67ab1517e1140d7f5233609673bac9c4fe71b..81bb869b18ee5a0c03f4ae171d7d1b0c01d998d3 100644 (file)
@@ -517,10 +517,12 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
     self->socket_type = socket_type;
     self->Socket = sock;
     Py_INCREF(self->Socket);
-    self->ssl_sock = PyWeakref_NewRef(ssl_sock, NULL);
-    if (self->ssl_sock == NULL) {
-        Py_DECREF(self);
-        return NULL;
+    if (ssl_sock != Py_None) {
+        self->ssl_sock = PyWeakref_NewRef(ssl_sock, NULL);
+        if (self->ssl_sock == NULL) {
+            Py_DECREF(self);
+            return NULL;
+        }
     }
     return self;
 }
@@ -2931,8 +2933,12 @@ _servername_callback(SSL *s, int *al, void *args)
 
     ssl = SSL_get_app_data(s);
     assert(PySSLSocket_Check(ssl));
-    ssl_socket = PyWeakref_GetObject(ssl->ssl_sock);
-    Py_INCREF(ssl_socket);
+    if (ssl->ssl_sock == NULL) {
+        ssl_socket = Py_None;
+    } else {
+        ssl_socket = PyWeakref_GetObject(ssl->ssl_sock);
+        Py_INCREF(ssl_socket);
+    }
     if (ssl_socket == Py_None) {
         goto error;
     }