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 )
@@ -1204,6 +1210,8 @@ def connector():
12041210 t .join ()
12051211
12061212 @skip_if_broken_ubuntu_ssl
1213+ @unittest .skipUnless (hasattr (ssl , 'PROTOCOL_SSLv2' ),
1214+ "OpenSSL is compiled without SSLv2 support" )
12071215 def test_protocol_sslv2 (self ):
12081216 """Connecting to an SSLv2 server with various client options"""
12091217 if support .verbose :
@@ -1229,14 +1237,15 @@ def test_protocol_sslv23(self):
12291237 """Connecting to an SSLv23 server with various client options"""
12301238 if support .verbose :
12311239 sys .stdout .write ("\n " )
1232- try :
1233- try_protocol_combo (ssl .PROTOCOL_SSLv23 , ssl .PROTOCOL_SSLv2 , True )
1234- except (ssl .SSLError , socket .error ) as x :
1235- # this fails on some older versions of OpenSSL (0.9.7l, for instance)
1236- if support .verbose :
1237- sys .stdout .write (
1238- " SSL2 client to SSL23 server test unexpectedly failed:\n %s\n "
1239- % str (x ))
1240+ if hasattr (ssl , 'PROTOCOL_SSLv2' ):
1241+ try :
1242+ try_protocol_combo (ssl .PROTOCOL_SSLv23 , ssl .PROTOCOL_SSLv2 , True )
1243+ except (ssl .SSLError , socket .error ) as x :
1244+ # this fails on some older versions of OpenSSL (0.9.7l, for instance)
1245+ if support .verbose :
1246+ sys .stdout .write (
1247+ " SSL2 client to SSL23 server test unexpectedly failed:\n %s\n "
1248+ % str (x ))
12401249 try_protocol_combo (ssl .PROTOCOL_SSLv23 , ssl .PROTOCOL_SSLv3 , True )
12411250 try_protocol_combo (ssl .PROTOCOL_SSLv23 , ssl .PROTOCOL_SSLv23 , True )
12421251 try_protocol_combo (ssl .PROTOCOL_SSLv23 , ssl .PROTOCOL_TLSv1 , True )
@@ -1267,7 +1276,8 @@ def test_protocol_sslv3(self):
12671276 try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_SSLv3 , True )
12681277 try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_SSLv3 , True , ssl .CERT_OPTIONAL )
12691278 try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_SSLv3 , True , ssl .CERT_REQUIRED )
1270- try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_SSLv2 , False )
1279+ if hasattr (ssl , 'PROTOCOL_SSLv2' ):
1280+ try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_SSLv2 , False )
12711281 try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_SSLv23 , False )
12721282 try_protocol_combo (ssl .PROTOCOL_SSLv3 , ssl .PROTOCOL_TLSv1 , False )
12731283 if no_sslv2_implies_sslv3_hello ():
@@ -1283,7 +1293,8 @@ def test_protocol_tlsv1(self):
12831293 try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_TLSv1 , True )
12841294 try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_TLSv1 , True , ssl .CERT_OPTIONAL )
12851295 try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_TLSv1 , True , ssl .CERT_REQUIRED )
1286- try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_SSLv2 , False )
1296+ if hasattr (ssl , 'PROTOCOL_SSLv2' ):
1297+ try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_SSLv2 , False )
12871298 try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_SSLv3 , False )
12881299 try_protocol_combo (ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_SSLv23 , False )
12891300
0 commit comments