@@ -1003,6 +1003,7 @@ def test_create_default_context(self):
10031003 ctx = ssl .create_default_context ()
10041004 self .assertEqual (ctx .protocol , ssl .PROTOCOL_TLSv1 )
10051005 self .assertEqual (ctx .verify_mode , ssl .CERT_REQUIRED )
1006+ self .assertTrue (ctx .check_hostname )
10061007 self .assertEqual (ctx .options & ssl .OP_NO_SSLv2 , ssl .OP_NO_SSLv2 )
10071008
10081009 with open (SIGNING_CA ) as f :
@@ -1022,6 +1023,7 @@ def test__create_stdlib_context(self):
10221023 ctx = ssl ._create_stdlib_context ()
10231024 self .assertEqual (ctx .protocol , ssl .PROTOCOL_SSLv23 )
10241025 self .assertEqual (ctx .verify_mode , ssl .CERT_NONE )
1026+ self .assertFalse (ctx .check_hostname )
10251027 self .assertEqual (ctx .options & ssl .OP_NO_SSLv2 , ssl .OP_NO_SSLv2 )
10261028
10271029 ctx = ssl ._create_stdlib_context (ssl .PROTOCOL_TLSv1 )
@@ -1040,6 +1042,28 @@ def test__create_stdlib_context(self):
10401042 self .assertEqual (ctx .verify_mode , ssl .CERT_NONE )
10411043 self .assertEqual (ctx .options & ssl .OP_NO_SSLv2 , ssl .OP_NO_SSLv2 )
10421044
1045+ def test_check_hostname (self ):
1046+ ctx = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
1047+ self .assertFalse (ctx .check_hostname )
1048+
1049+ # Requires CERT_REQUIRED or CERT_OPTIONAL
1050+ with self .assertRaises (ValueError ):
1051+ ctx .check_hostname = True
1052+ ctx .verify_mode = ssl .CERT_REQUIRED
1053+ self .assertFalse (ctx .check_hostname )
1054+ ctx .check_hostname = True
1055+ self .assertTrue (ctx .check_hostname )
1056+
1057+ ctx .verify_mode = ssl .CERT_OPTIONAL
1058+ ctx .check_hostname = True
1059+ self .assertTrue (ctx .check_hostname )
1060+
1061+ # Cannot set CERT_NONE with check_hostname enabled
1062+ with self .assertRaises (ValueError ):
1063+ ctx .verify_mode = ssl .CERT_NONE
1064+ ctx .check_hostname = False
1065+ self .assertFalse (ctx .check_hostname )
1066+
10431067
10441068class SSLErrorTests (unittest .TestCase ):
10451069
@@ -1930,6 +1954,44 @@ def test_crl_check(self):
19301954 cert = s .getpeercert ()
19311955 self .assertTrue (cert , "Can't get peer certificate." )
19321956
1957+ def test_check_hostname (self ):
1958+ if support .verbose :
1959+ sys .stdout .write ("\n " )
1960+
1961+ server_context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
1962+ server_context .load_cert_chain (SIGNED_CERTFILE )
1963+
1964+ context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
1965+ context .verify_mode = ssl .CERT_REQUIRED
1966+ context .check_hostname = True
1967+ context .load_verify_locations (SIGNING_CA )
1968+
1969+ # correct hostname should verify
1970+ server = ThreadedEchoServer (context = server_context , chatty = True )
1971+ with server :
1972+ with context .wrap_socket (socket .socket (),
1973+ server_hostname = "localhost" ) as s :
1974+ s .connect ((HOST , server .port ))
1975+ cert = s .getpeercert ()
1976+ self .assertTrue (cert , "Can't get peer certificate." )
1977+
1978+ # incorrect hostname should raise an exception
1979+ server = ThreadedEchoServer (context = server_context , chatty = True )
1980+ with server :
1981+ with context .wrap_socket (socket .socket (),
1982+ server_hostname = "invalid" ) as s :
1983+ with self .assertRaisesRegex (ssl .CertificateError ,
1984+ "hostname 'invalid' doesn't match 'localhost'" ):
1985+ s .connect ((HOST , server .port ))
1986+
1987+ # missing server_hostname arg should cause an exception, too
1988+ server = ThreadedEchoServer (context = server_context , chatty = True )
1989+ with server :
1990+ with socket .socket () as s :
1991+ with self .assertRaisesRegex (ValueError ,
1992+ "check_hostname requires server_hostname" ):
1993+ context .wrap_socket (s )
1994+
19331995 def test_empty_cert (self ):
19341996 """Connecting with an empty cert file"""
19351997 bad_cert_test (os .path .join (os .path .dirname (__file__ ) or os .curdir ,
0 commit comments