2121ssl = support .import_module ("ssl" )
2222
2323PROTOCOLS = [
24- ssl .PROTOCOL_SSLv2 , ssl . PROTOCOL_SSLv3 ,
24+ ssl .PROTOCOL_SSLv3 ,
2525 ssl .PROTOCOL_SSLv23 , ssl .PROTOCOL_TLSv1
2626]
27+ if hasattr (ssl , 'PROTOCOL_SSLv2' ):
28+ PROTOCOLS .append (ssl .PROTOCOL_SSLv2 )
2729
2830HOST = support .HOST
2931
@@ -67,22 +69,25 @@ def no_sslv2_implies_sslv3_hello():
6769
6870# Issue #9415: Ubuntu hijacks their OpenSSL and forcefully disables SSLv2
6971def skip_if_broken_ubuntu_ssl (func ):
70- @functools .wraps (func )
71- def f (* args , ** kwargs ):
72- try :
73- ssl .SSLContext (ssl .PROTOCOL_SSLv2 )
74- except ssl .SSLError :
75- if (ssl .OPENSSL_VERSION_INFO == (0 , 9 , 8 , 15 , 15 ) and
76- platform .linux_distribution () == ('debian' , 'squeeze/sid' , '' )):
77- raise unittest .SkipTest ("Patched Ubuntu OpenSSL breaks behaviour" )
78- return func (* args , ** kwargs )
79- return f
72+ if hasattr (ssl , 'PROTOCOL_SSLv2' ):
73+ @functools .wraps (func )
74+ def f (* args , ** kwargs ):
75+ try :
76+ ssl .SSLContext (ssl .PROTOCOL_SSLv2 )
77+ except ssl .SSLError :
78+ if (ssl .OPENSSL_VERSION_INFO == (0 , 9 , 8 , 15 , 15 ) and
79+ platform .linux_distribution () == ('debian' , 'squeeze/sid' , '' )):
80+ raise unittest .SkipTest ("Patched Ubuntu OpenSSL breaks behaviour" )
81+ return func (* args , ** kwargs )
82+ return f
83+ else :
84+ return func
8085
8186
8287class BasicSocketTests (unittest .TestCase ):
8388
8489 def test_constants (self ):
85- ssl .PROTOCOL_SSLv2
90+ # ssl.PROTOCOL_SSLv2
8691 ssl .PROTOCOL_SSLv23
8792 ssl .PROTOCOL_SSLv3
8893 ssl .PROTOCOL_TLSv1
@@ -310,7 +315,8 @@ class ContextTests(unittest.TestCase):
310315
311316 @skip_if_broken_ubuntu_ssl
312317 def test_constructor (self ):
313- ctx = ssl .SSLContext (ssl .PROTOCOL_SSLv2 )
318+ if hasattr (ssl , 'PROTOCOL_SSLv2' ):
319+ ctx = ssl .SSLContext (ssl .PROTOCOL_SSLv2 )
314320 ctx = ssl .SSLContext (ssl .PROTOCOL_SSLv23 )
315321 ctx = ssl .SSLContext (ssl .PROTOCOL_SSLv3 )
316322 ctx = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
@@ -1199,6 +1205,7 @@ def connector():
11991205 t .join ()
12001206
12011207 @skip_if_broken_ubuntu_ssl
1208+ @unittest .skipUnless (hasattr (ssl , 'PROTOCOL_SSLv2' ), "need SSLv2" )
12021209 def test_protocol_sslv2 (self ):
12031210 """Connecting to an SSLv2 server with various client options"""
12041211 if support .verbose :
@@ -1224,14 +1231,15 @@ def test_protocol_sslv23(self):
12241231 """Connecting to an SSLv23 server with various client options"""
12251232 if support .verbose :
12261233 sys .stdout .write ("\n " )
1227- try :
1228- try_protocol_combo (ssl .PROTOCOL_SSLv23 , ssl .PROTOCOL_SSLv2 , True )
1229- except (ssl .SSLError , socket .error ) as x :
1230- # this fails on some older versions of OpenSSL (0.9.7l, for instance)
1231- if support .verbose :
1232- sys .stdout .write (
1233- " SSL2 client to SSL23 server test unexpectedly failed:\n %s\n "
1234- % str (x ))
1234+ if hasattr (ssl , 'PROTOCOL_SSLv2' ):
1235+ try :
1236+ try_protocol_combo (ssl .PROTOCOL_SSLv23 , ssl .PROTOCOL_SSLv2 , True )
1237+ except (ssl .SSLError , socket .error ) as x :
1238+ # this fails on some older versions of OpenSSL (0.9.7l, for instance)
1239+ if support .verbose :
1240+ sys .stdout .write (
1241+ " SSL2 client to SSL23 server test unexpectedly failed:\n %s\n "
1242+ % str (x ))
12351243 try_protocol_combo (ssl .PROTOCOL_SSLv23 , ssl .PROTOCOL_SSLv3 , True )
12361244 try_protocol_combo (ssl .PROTOCOL_SSLv23 , ssl .PROTOCOL_SSLv23 , True )
12371245 try_protocol_combo (ssl .PROTOCOL_SSLv23 , ssl .PROTOCOL_TLSv1 , True )
@@ -1262,7 +1270,8 @@ def test_protocol_sslv3(self):
12621270 try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_SSLv3 , True )
12631271 try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_SSLv3 , True , ssl .CERT_OPTIONAL )
12641272 try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_SSLv3 , True , ssl .CERT_REQUIRED )
1265- try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_SSLv2 , False )
1273+ if hasattr (ssl , 'PROTOCOL_SSLv2' ):
1274+ try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_SSLv2 , False )
12661275 try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_SSLv23 , False )
12671276 try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_TLSv1 , False )
12681277 if no_sslv2_implies_sslv3_hello ():
@@ -1278,7 +1287,8 @@ def test_protocol_tlsv1(self):
12781287 try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_TLSv1 , True )
12791288 try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_TLSv1 , True , ssl .CERT_OPTIONAL )
12801289 try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_TLSv1 , True , ssl .CERT_REQUIRED )
1281- try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_SSLv2 , False )
1290+ if hasattr (ssl , 'PROTOCOL_SSLv2' ):
1291+ try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_SSLv2 , False )
12821292 try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_SSLv3 , False )
12831293 try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_SSLv23 , False )
12841294
0 commit comments