(Merge 3.1) Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional

OpenSSL is now compiled with OPENSSL_NO_SSL2 defined (without the SSLv2
protocol) on Debian: fix the ssl module on Debian Testing and Debian Sid.

Optimize also ssl.get_protocol_name(): speed does matter!
This commit is contained in:
Victor Stinner 2011-05-10 01:52:03 +02:00
parent 2c585f607e
commit b1241f9619
5 changed files with 52 additions and 34 deletions

View File

@ -237,6 +237,9 @@ Functions, Constants, and Exceptions
Selects SSL version 2 as the channel encryption protocol. Selects SSL version 2 as the channel encryption protocol.
This protocol is not available if OpenSSL is compiled with OPENSSL_NO_SSL2
flag.
.. warning:: .. warning::
SSL version 2 is insecure. Its use is highly discouraged. SSL version 2 is insecure. Its use is highly discouraged.

View File

@ -62,7 +62,6 @@ import _ssl # if we can't import it, let the error propagate
from _ssl import OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_INFO, OPENSSL_VERSION from _ssl import OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_INFO, OPENSSL_VERSION
from _ssl import SSLError from _ssl import SSLError
from _ssl import CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED from _ssl import CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED
from _ssl import PROTOCOL_SSLv2, PROTOCOL_SSLv3, PROTOCOL_SSLv23, PROTOCOL_TLSv1
from _ssl import RAND_status, RAND_egd, RAND_add from _ssl import RAND_status, RAND_egd, RAND_add
from _ssl import \ from _ssl import \
SSL_ERROR_ZERO_RETURN, \ SSL_ERROR_ZERO_RETURN, \
@ -74,6 +73,18 @@ from _ssl import \
SSL_ERROR_WANT_CONNECT, \ SSL_ERROR_WANT_CONNECT, \
SSL_ERROR_EOF, \ SSL_ERROR_EOF, \
SSL_ERROR_INVALID_ERROR_CODE SSL_ERROR_INVALID_ERROR_CODE
from _ssl import PROTOCOL_SSLv3, PROTOCOL_SSLv23, PROTOCOL_TLSv1
_PROTOCOL_NAMES = {
PROTOCOL_TLSv1: "TLSv1",
PROTOCOL_SSLv23: "SSLv23",
PROTOCOL_SSLv3: "SSLv3",
}
try:
from _ssl import PROTOCOL_SSLv2
except ImportError:
pass
else:
_PROTOCOL_NAMES[PROTOCOL_SSLv2] = "SSLv2"
from socket import socket, _fileobject, _delegate_methods, error as socket_error from socket import socket, _fileobject, _delegate_methods, error as socket_error
from socket import getnameinfo as _getnameinfo from socket import getnameinfo as _getnameinfo
@ -425,16 +436,7 @@ def get_server_certificate(addr, ssl_version=PROTOCOL_SSLv3, ca_certs=None):
return DER_cert_to_PEM_cert(dercert) return DER_cert_to_PEM_cert(dercert)
def get_protocol_name(protocol_code): def get_protocol_name(protocol_code):
if protocol_code == PROTOCOL_TLSv1: return _PROTOCOL_NAMES.get(protocol_code, '<unknown>')
return "TLSv1"
elif protocol_code == PROTOCOL_SSLv23:
return "SSLv23"
elif protocol_code == PROTOCOL_SSLv2:
return "SSLv2"
elif protocol_code == PROTOCOL_SSLv3:
return "SSLv3"
else:
return "<unknown>"
# a replacement for the old socket.ssl function # a replacement for the old socket.ssl function

View File

@ -58,32 +58,35 @@ class BasicTests(unittest.TestCase):
# Issue #9415: Ubuntu hijacks their OpenSSL and forcefully disables SSLv2 # Issue #9415: Ubuntu hijacks their OpenSSL and forcefully disables SSLv2
def skip_if_broken_ubuntu_ssl(func): def skip_if_broken_ubuntu_ssl(func):
# We need to access the lower-level wrapper in order to create an if hasattr(ssl, 'PROTOCOL_SSLv2'):
# implicit SSL context without trying to connect or listen. # We need to access the lower-level wrapper in order to create an
try: # implicit SSL context without trying to connect or listen.
import _ssl
except ImportError:
# The returned function won't get executed, just ignore the error
pass
@functools.wraps(func)
def f(*args, **kwargs):
try: try:
s = socket.socket(socket.AF_INET) import _ssl
_ssl.sslwrap(s._sock, 0, None, None, except ImportError:
ssl.CERT_NONE, ssl.PROTOCOL_SSLv2, None, None) # The returned function won't get executed, just ignore the error
except ssl.SSLError as e: pass
if (ssl.OPENSSL_VERSION_INFO == (0, 9, 8, 15, 15) and @functools.wraps(func)
platform.linux_distribution() == ('debian', 'squeeze/sid', '') def f(*args, **kwargs):
and 'Invalid SSL protocol variant specified' in str(e)): try:
raise unittest.SkipTest("Patched Ubuntu OpenSSL breaks behaviour") s = socket.socket(socket.AF_INET)
return func(*args, **kwargs) _ssl.sslwrap(s._sock, 0, None, None,
return f ssl.CERT_NONE, ssl.PROTOCOL_SSLv2, None, None)
except ssl.SSLError as e:
if (ssl.OPENSSL_VERSION_INFO == (0, 9, 8, 15, 15) and
platform.linux_distribution() == ('debian', 'squeeze/sid', '')
and 'Invalid SSL protocol variant specified' in str(e)):
raise unittest.SkipTest("Patched Ubuntu OpenSSL breaks behaviour")
return func(*args, **kwargs)
return f
else:
return func
class BasicSocketTests(unittest.TestCase): class BasicSocketTests(unittest.TestCase):
def test_constants(self): def test_constants(self):
ssl.PROTOCOL_SSLv2 #ssl.PROTOCOL_SSLv2
ssl.PROTOCOL_SSLv23 ssl.PROTOCOL_SSLv23
ssl.PROTOCOL_SSLv3 ssl.PROTOCOL_SSLv3
ssl.PROTOCOL_TLSv1 ssl.PROTOCOL_TLSv1
@ -1008,7 +1011,8 @@ else:
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True) try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True)
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True, ssl.CERT_OPTIONAL) try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True, ssl.CERT_OPTIONAL)
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True, ssl.CERT_REQUIRED) try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True, ssl.CERT_REQUIRED)
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv2, False) if hasattr(ssl, 'PROTOCOL_SSLv2'):
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv2, False)
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv23, False) try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv23, False)
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_TLSv1, False) try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_TLSv1, False)
@ -1020,7 +1024,8 @@ else:
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True) try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True)
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True, ssl.CERT_OPTIONAL) try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True, ssl.CERT_OPTIONAL)
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True, ssl.CERT_REQUIRED) try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True, ssl.CERT_REQUIRED)
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv2, False) if hasattr(ssl, 'PROTOCOL_SSLv2'):
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv2, False)
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv3, False) try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv3, False)
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv23, False) try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv23, False)

View File

@ -80,6 +80,8 @@ Core and Builtins
Library Library
------- -------
- Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional.
- Issue #11164: Remove obsolete allnodes test from minidom test. - Issue #11164: Remove obsolete allnodes test from minidom test.
- Issue #11927: SMTP_SSL now uses port 465 by default as documented. Patch - Issue #11927: SMTP_SSL now uses port 465 by default as documented. Patch

View File

@ -62,8 +62,10 @@ enum py_ssl_cert_requirements {
}; };
enum py_ssl_version { enum py_ssl_version {
#ifndef OPENSSL_NO_SSL2
PY_SSL_VERSION_SSL2, PY_SSL_VERSION_SSL2,
PY_SSL_VERSION_SSL3, #endif
PY_SSL_VERSION_SSL3=1,
PY_SSL_VERSION_SSL23, PY_SSL_VERSION_SSL23,
PY_SSL_VERSION_TLS1 PY_SSL_VERSION_TLS1
}; };
@ -302,8 +304,10 @@ newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file,
self->ctx = SSL_CTX_new(TLSv1_method()); /* Set up context */ self->ctx = SSL_CTX_new(TLSv1_method()); /* Set up context */
else if (proto_version == PY_SSL_VERSION_SSL3) else if (proto_version == PY_SSL_VERSION_SSL3)
self->ctx = SSL_CTX_new(SSLv3_method()); /* Set up context */ self->ctx = SSL_CTX_new(SSLv3_method()); /* Set up context */
#ifndef OPENSSL_NO_SSL2
else if (proto_version == PY_SSL_VERSION_SSL2) else if (proto_version == PY_SSL_VERSION_SSL2)
self->ctx = SSL_CTX_new(SSLv2_method()); /* Set up context */ self->ctx = SSL_CTX_new(SSLv2_method()); /* Set up context */
#endif
else if (proto_version == PY_SSL_VERSION_SSL23) else if (proto_version == PY_SSL_VERSION_SSL23)
self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */ self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS
@ -1708,8 +1712,10 @@ init_ssl(void)
PY_SSL_CERT_REQUIRED); PY_SSL_CERT_REQUIRED);
/* protocol versions */ /* protocol versions */
#ifndef OPENSSL_NO_SSL2
PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", PyModule_AddIntConstant(m, "PROTOCOL_SSLv2",
PY_SSL_VERSION_SSL2); PY_SSL_VERSION_SSL2);
#endif
PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", PyModule_AddIntConstant(m, "PROTOCOL_SSLv3",
PY_SSL_VERSION_SSL3); PY_SSL_VERSION_SSL3);
PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", PyModule_AddIntConstant(m, "PROTOCOL_SSLv23",