|
|
f63228 |
|
|
|
f63228 |
# HG changeset patch
|
|
|
f63228 |
# User Benjamin Peterson <benjamin@python.org>
|
|
|
f63228 |
# Date 1412221981 14400
|
|
|
f63228 |
# Node ID 1a36d4e8cf4edfdc4c7d59a40075b8cf00e3ad3c
|
|
|
f63228 |
# Parent 222e0faa5fa9567f657f13fc78a60069142e09ae
|
|
|
f63228 |
fix sslwrap_simple (closes #22523)
|
|
|
f63228 |
|
|
|
f63228 |
Thanks Alex Gaynor.
|
|
|
f63228 |
|
|
|
f63228 |
diff --git a/Lib/ssl.py b/Lib/ssl.py
|
|
|
f63228 |
--- a/Lib/ssl.py
|
|
|
f63228 |
+++ b/Lib/ssl.py
|
|
|
f63228 |
@@ -969,16 +969,16 @@ def get_protocol_name(protocol_code):
|
|
|
f63228 |
# a replacement for the old socket.ssl function
|
|
|
f63228 |
|
|
|
f63228 |
def sslwrap_simple(sock, keyfile=None, certfile=None):
|
|
|
f63228 |
-
|
|
|
f63228 |
"""A replacement for the old socket.ssl function. Designed
|
|
|
f63228 |
for compability with Python 2.5 and earlier. Will disappear in
|
|
|
f63228 |
Python 3.0."""
|
|
|
f63228 |
-
|
|
|
f63228 |
if hasattr(sock, "_sock"):
|
|
|
f63228 |
sock = sock._sock
|
|
|
f63228 |
|
|
|
f63228 |
- ssl_sock = _ssl.sslwrap(sock, 0, keyfile, certfile, CERT_NONE,
|
|
|
f63228 |
- PROTOCOL_SSLv23, None)
|
|
|
f63228 |
+ ctx = SSLContext(PROTOCOL_SSLv23)
|
|
|
f63228 |
+ if keyfile or certfile:
|
|
|
f63228 |
+ ctx.load_cert_chain(certfile, keyfile)
|
|
|
f63228 |
+ ssl_sock = ctx._wrap_socket(sock, server_side=False)
|
|
|
f63228 |
try:
|
|
|
f63228 |
sock.getpeername()
|
|
|
f63228 |
except socket_error:
|
|
|
f63228 |
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
|
|
|
f63228 |
--- a/Lib/test/test_ssl.py
|
|
|
f63228 |
+++ b/Lib/test/test_ssl.py
|
|
|
f63228 |
@@ -94,6 +94,8 @@ class BasicTests(unittest.TestCase):
|
|
|
f63228 |
pass
|
|
|
f63228 |
else:
|
|
|
f63228 |
raise
|
|
|
f63228 |
+
|
|
|
f63228 |
+
|
|
|
f63228 |
def can_clear_options():
|
|
|
f63228 |
# 0.9.8m or higher
|
|
|
f63228 |
return ssl._OPENSSL_API_VERSION >= (0, 9, 8, 13, 15)
|
|
|
f63228 |
@@ -2944,7 +2946,7 @@ def test_main(verbose=False):
|
|
|
f63228 |
if not os.path.exists(filename):
|
|
|
f63228 |
raise support.TestFailed("Can't read certificate file %r" % filename)
|
|
|
f63228 |
|
|
|
f63228 |
- tests = [ContextTests, BasicSocketTests, SSLErrorTests]
|
|
|
f63228 |
+ tests = [ContextTests, BasicTests, BasicSocketTests, SSLErrorTests]
|
|
|
f63228 |
|
|
|
f63228 |
if support.is_resource_enabled('network'):
|
|
|
f63228 |
tests.append(NetworkedTests)
|
|
|
f63228 |
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
|
|
|
f63228 |
--- a/Modules/_ssl.c
|
|
|
f63228 |
+++ b/Modules/_ssl.c
|
|
|
f63228 |
@@ -517,10 +517,12 @@ newPySSLSocket(PySSLContext *sslctx, PyS
|
|
|
f63228 |
self->socket_type = socket_type;
|
|
|
f63228 |
self->Socket = sock;
|
|
|
f63228 |
Py_INCREF(self->Socket);
|
|
|
f63228 |
- self->ssl_sock = PyWeakref_NewRef(ssl_sock, NULL);
|
|
|
f63228 |
- if (self->ssl_sock == NULL) {
|
|
|
f63228 |
- Py_DECREF(self);
|
|
|
f63228 |
- return NULL;
|
|
|
f63228 |
+ if (ssl_sock != Py_None) {
|
|
|
f63228 |
+ self->ssl_sock = PyWeakref_NewRef(ssl_sock, NULL);
|
|
|
f63228 |
+ if (self->ssl_sock == NULL) {
|
|
|
f63228 |
+ Py_DECREF(self);
|
|
|
f63228 |
+ return NULL;
|
|
|
f63228 |
+ }
|
|
|
f63228 |
}
|
|
|
f63228 |
return self;
|
|
|
f63228 |
}
|
|
|
f63228 |
@@ -2931,8 +2933,12 @@ static int
|
|
|
f63228 |
|
|
|
f63228 |
ssl = SSL_get_app_data(s);
|
|
|
f63228 |
assert(PySSLSocket_Check(ssl));
|
|
|
f63228 |
- ssl_socket = PyWeakref_GetObject(ssl->ssl_sock);
|
|
|
f63228 |
- Py_INCREF(ssl_socket);
|
|
|
f63228 |
+ if (ssl->ssl_sock == NULL) {
|
|
|
f63228 |
+ ssl_socket = Py_None;
|
|
|
f63228 |
+ } else {
|
|
|
f63228 |
+ ssl_socket = PyWeakref_GetObject(ssl->ssl_sock);
|
|
|
f63228 |
+ Py_INCREF(ssl_socket);
|
|
|
f63228 |
+ }
|
|
|
f63228 |
if (ssl_socket == Py_None) {
|
|
|
f63228 |
goto error;
|
|
|
f63228 |
}
|
|
|
f63228 |
|