Skip to content

bpo-31346: Use PROTOCOL_TLS_CLIENT/SERVER #3058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/asyncio/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def dummy_ssl_context():
if ssl is None:
return None
else:
return ssl.SSLContext(ssl.PROTOCOL_SSLv23)
return ssl.SSLContext(ssl.PROTOCOL_TLS)


def run_briefly(loop):
Expand Down
4 changes: 2 additions & 2 deletions Lib/ftplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ class FTP_TLS(FTP):
'221 Goodbye.'
>>>
'''
ssl_version = ssl.PROTOCOL_SSLv23
ssl_version = ssl.PROTOCOL_TLS_CLIENT

def __init__(self, host='', user='', passwd='', acct='', keyfile=None,
certfile=None, context=None,
Expand Down Expand Up @@ -753,7 +753,7 @@ def auth(self):
'''Set up secure control connection by using TLS/SSL.'''
if isinstance(self.sock, ssl.SSLSocket):
raise ValueError("Already using TLS")
if self.ssl_version >= ssl.PROTOCOL_SSLv23:
if self.ssl_version >= ssl.PROTOCOL_TLS:
resp = self.voidcmd('AUTH TLS')
else:
resp = self.voidcmd('AUTH SSL')
Expand Down
7 changes: 5 additions & 2 deletions Lib/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def create_default_context(purpose=Purpose.SERVER_AUTH, *, cafile=None,
context.load_default_certs(purpose)
return context

def _create_unverified_context(protocol=PROTOCOL_TLS, *, cert_reqs=None,
def _create_unverified_context(protocol=PROTOCOL_TLS, *, cert_reqs=CERT_NONE,
check_hostname=False, purpose=Purpose.SERVER_AUTH,
certfile=None, keyfile=None,
cafile=None, capath=None, cadata=None):
Expand All @@ -536,9 +536,12 @@ def _create_unverified_context(protocol=PROTOCOL_TLS, *, cert_reqs=None,
# by default.
context = SSLContext(protocol)

if not check_hostname:
context.check_hostname = False
if cert_reqs is not None:
context.verify_mode = cert_reqs
context.check_hostname = check_hostname
if check_hostname:
context.check_hostname = True

if keyfile and not certfile:
raise ValueError("certfile must be specified")
Expand Down
16 changes: 8 additions & 8 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,13 +824,13 @@ def test_ssl_connect_accepted_socket(self):
'SSL not supported with proactor event loops before Python 3.5'
)

server_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
server_context.load_cert_chain(ONLYCERT, ONLYKEY)
if hasattr(server_context, 'check_hostname'):
server_context.check_hostname = False
server_context.verify_mode = ssl.CERT_NONE

client_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
client_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
if hasattr(server_context, 'check_hostname'):
client_context.check_hostname = False
client_context.verify_mode = ssl.CERT_NONE
Expand Down Expand Up @@ -985,7 +985,7 @@ def test_create_unix_server_path_socket_error(self):
self.loop.run_until_complete(f)

def _create_ssl_context(self, certfile, keyfile=None):
sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
sslcontext.options |= ssl.OP_NO_SSLv2
sslcontext.load_cert_chain(certfile, keyfile)
return sslcontext
Expand Down Expand Up @@ -1082,7 +1082,7 @@ def test_create_server_ssl_verify_failed(self):
server, host, port = self._make_ssl_server(
lambda: proto, SIGNED_CERTFILE)

sslcontext_client = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
sslcontext_client = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
sslcontext_client.options |= ssl.OP_NO_SSLv2
sslcontext_client.verify_mode = ssl.CERT_REQUIRED
if hasattr(sslcontext_client, 'check_hostname'):
Expand Down Expand Up @@ -1116,7 +1116,7 @@ def test_create_unix_server_ssl_verify_failed(self):
server, path = self._make_ssl_unix_server(
lambda: proto, SIGNED_CERTFILE)

sslcontext_client = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
sslcontext_client = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
sslcontext_client.options |= ssl.OP_NO_SSLv2
sslcontext_client.verify_mode = ssl.CERT_REQUIRED
if hasattr(sslcontext_client, 'check_hostname'):
Expand Down Expand Up @@ -1150,7 +1150,7 @@ def test_create_server_ssl_match_failed(self):
server, host, port = self._make_ssl_server(
lambda: proto, SIGNED_CERTFILE)

sslcontext_client = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
sslcontext_client = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
sslcontext_client.options |= ssl.OP_NO_SSLv2
sslcontext_client.verify_mode = ssl.CERT_REQUIRED
sslcontext_client.load_verify_locations(
Expand Down Expand Up @@ -1183,7 +1183,7 @@ def test_create_unix_server_ssl_verified(self):
server, path = self._make_ssl_unix_server(
lambda: proto, SIGNED_CERTFILE)

sslcontext_client = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
sslcontext_client = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
sslcontext_client.options |= ssl.OP_NO_SSLv2
sslcontext_client.verify_mode = ssl.CERT_REQUIRED
sslcontext_client.load_verify_locations(cafile=SIGNING_CA)
Expand Down Expand Up @@ -1212,7 +1212,7 @@ def test_create_server_ssl_verified(self):
server, host, port = self._make_ssl_server(
lambda: proto, SIGNED_CERTFILE)

sslcontext_client = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
sslcontext_client = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
sslcontext_client.options |= ssl.OP_NO_SSLv2
sslcontext_client.verify_mode = ssl.CERT_REQUIRED
sslcontext_client.load_verify_locations(cafile=SIGNING_CA)
Expand Down
18 changes: 6 additions & 12 deletions Lib/test/test_ftplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,17 +896,11 @@ def test_auth_issued_twice(self):
self.client.auth()
self.assertRaises(ValueError, self.client.auth)

def test_auth_ssl(self):
try:
self.client.ssl_version = ssl.PROTOCOL_SSLv23
self.client.auth()
self.assertRaises(ValueError, self.client.auth)
finally:
self.client.ssl_version = ssl.PROTOCOL_TLSv1

def test_context(self):
self.client.quit()
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
self.assertRaises(ValueError, ftplib.FTP_TLS, keyfile=CERTFILE,
context=ctx)
self.assertRaises(ValueError, ftplib.FTP_TLS, certfile=CERTFILE,
Expand Down Expand Up @@ -935,9 +929,9 @@ def test_ccc(self):

def test_check_hostname(self):
self.client.quit()
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
ctx.verify_mode = ssl.CERT_REQUIRED
ctx.check_hostname = True
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED)
self.assertEqual(ctx.check_hostname, True)
ctx.load_verify_locations(CAFILE)
self.client = ftplib.FTP_TLS(context=ctx, timeout=TIMEOUT)

Expand Down
15 changes: 6 additions & 9 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,8 +1583,9 @@ def test_networked_good_cert(self):
import ssl
support.requires('network')
with support.transient_internet('self-signed.pythontest.net'):
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_REQUIRED
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
self.assertEqual(context.verify_mode, ssl.CERT_REQUIRED)
self.assertEqual(context.check_hostname, True)
context.load_verify_locations(CERT_selfsigned_pythontestdotnet)
h = client.HTTPSConnection('self-signed.pythontest.net', 443, context=context)
h.request('GET', '/')
Expand All @@ -1599,8 +1600,7 @@ def test_networked_bad_cert(self):
import ssl
support.requires('network')
with support.transient_internet('self-signed.pythontest.net'):
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_REQUIRED
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.load_verify_locations(CERT_localhost)
h = client.HTTPSConnection('self-signed.pythontest.net', 443, context=context)
with self.assertRaises(ssl.SSLError) as exc_info:
Expand All @@ -1620,8 +1620,7 @@ def test_local_good_hostname(self):
# The (valid) cert validates the HTTP hostname
import ssl
server = self.make_server(CERT_localhost)
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_REQUIRED
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.load_verify_locations(CERT_localhost)
h = client.HTTPSConnection('localhost', server.port, context=context)
self.addCleanup(h.close)
Expand All @@ -1634,9 +1633,7 @@ def test_local_bad_hostname(self):
# The (valid) cert doesn't validate the HTTP hostname
import ssl
server = self.make_server(CERT_fakehostname)
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.load_verify_locations(CERT_fakehostname)
h = client.HTTPSConnection('localhost', server.port, context=context)
with self.assertRaises(ssl.CertificateError):
Expand Down
18 changes: 8 additions & 10 deletions Lib/test/test_imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,9 @@ class NewIMAPSSLTests(NewIMAPTestsMixin, unittest.TestCase):
server_class = SecureTCPServer

def test_ssl_raises(self):
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ssl_context.verify_mode = ssl.CERT_REQUIRED
ssl_context.check_hostname = True
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
self.assertEqual(ssl_context.verify_mode, ssl.CERT_REQUIRED)
self.assertEqual(ssl_context.check_hostname, True)
ssl_context.load_verify_locations(CAFILE)

with self.assertRaisesRegex(ssl.CertificateError,
Expand All @@ -490,9 +490,7 @@ def test_ssl_raises(self):
client.shutdown()

def test_ssl_verified(self):
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ssl_context.verify_mode = ssl.CERT_REQUIRED
ssl_context.check_hostname = True
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ssl_context.load_verify_locations(CAFILE)

_, server = self._setup(SimpleIMAPHandler)
Expand Down Expand Up @@ -869,9 +867,7 @@ class ThreadedNetworkedTestsSSL(ThreadedNetworkedTests):

@reap_threads
def test_ssl_verified(self):
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ssl_context.verify_mode = ssl.CERT_REQUIRED
ssl_context.check_hostname = True
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ssl_context.load_verify_locations(CAFILE)

with self.assertRaisesRegex(
Expand Down Expand Up @@ -951,7 +947,9 @@ def tearDown(self):
pass

def create_ssl_context(self):
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
ssl_context.load_cert_chain(CERTFILE)
return ssl_context

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -1804,7 +1804,7 @@ def test_output(self):
else:
here = os.path.dirname(__file__)
localhost_cert = os.path.join(here, "keycert.pem")
sslctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
sslctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
sslctx.load_cert_chain(localhost_cert)

context = ssl.create_default_context(cafile=localhost_cert)
Expand Down
10 changes: 6 additions & 4 deletions Lib/test/test_poplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,10 @@ def test_stls(self):
@requires_ssl
def test_stls_context(self):
expected = b'+OK Begin TLS negotiation'
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.load_verify_locations(CAFILE)
ctx.verify_mode = ssl.CERT_REQUIRED
ctx.check_hostname = True
self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED)
self.assertEqual(ctx.check_hostname, True)
with self.assertRaises(ssl.CertificateError):
resp = self.client.stls(context=ctx)
self.client = poplib.POP3("localhost", self.server.port, timeout=3)
Expand Down Expand Up @@ -390,7 +390,9 @@ def test__all__(self):
self.assertIn('POP3_SSL', poplib.__all__)

def test_context(self):
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
self.assertRaises(ValueError, poplib.POP3_SSL, self.server.host,
self.server.port, keyfile=CERTFILE, context=ctx)
self.assertRaises(ValueError, poplib.POP3_SSL, self.server.host,
Expand Down
8 changes: 6 additions & 2 deletions Lib/test/test_smtpnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class SmtpTest(unittest.TestCase):

def test_connect_starttls(self):
support.get_attribute(smtplib, 'SMTP_SSL')
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
with support.transient_internet(self.testServer):
server = smtplib.SMTP(self.testServer, self.remotePort)
try:
Expand Down Expand Up @@ -58,7 +60,9 @@ def test_connect_default_port(self):
server.quit()

def test_connect_using_sslcontext(self):
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
support.get_attribute(smtplib, 'SMTP_SSL')
with support.transient_internet(self.testServer):
server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=context)
Expand Down
Loading