@@ -380,6 +380,29 @@ def test_errors(self):
380380 certfile = NONEXISTINGCERT , keyfile = NONEXISTINGCERT )
381381 self .assertEqual (cm .exception .errno , errno .ENOENT )
382382
383+ def bad_cert_test (self , certfile ):
384+ """Check that trying to use the given client certificate fails"""
385+ certfile = os .path .join (os .path .dirname (__file__ ) or os .curdir ,
386+ certfile )
387+ sock = socket .socket ()
388+ self .addCleanup (sock .close )
389+ with self .assertRaises (ssl .SSLError ):
390+ ssl .wrap_socket (sock ,
391+ certfile = certfile ,
392+ ssl_version = ssl .PROTOCOL_TLSv1 )
393+
394+ def test_empty_cert (self ):
395+ """Wrapping with an empty cert file"""
396+ self .bad_cert_test ("nullcert.pem" )
397+
398+ def test_malformed_cert (self ):
399+ """Wrapping with a badly formatted certificate (syntax error)"""
400+ self .bad_cert_test ("badcert.pem" )
401+
402+ def test_malformed_key (self ):
403+ """Wrapping with a badly formatted key (syntax error)"""
404+ self .bad_cert_test ("badkey.pem" )
405+
383406 def test_match_hostname (self ):
384407 def ok (cert , hostname ):
385408 ssl .match_hostname (cert , hostname )
@@ -2092,31 +2115,6 @@ def stop(self):
20922115 self .active = False
20932116 self .server .close ()
20942117
2095- def bad_cert_test (certfile ):
2096- """
2097- Launch a server with CERT_REQUIRED, and check that trying to
2098- connect to it with the given client certificate fails.
2099- """
2100- server = ThreadedEchoServer (CERTFILE ,
2101- certreqs = ssl .CERT_REQUIRED ,
2102- cacerts = CERTFILE , chatty = False ,
2103- connectionchatty = False )
2104- with server :
2105- try :
2106- with socket .socket () as sock :
2107- s = ssl .wrap_socket (sock ,
2108- certfile = certfile ,
2109- ssl_version = ssl .PROTOCOL_TLSv1 )
2110- s .connect ((HOST , server .port ))
2111- except ssl .SSLError as x :
2112- if support .verbose :
2113- sys .stdout .write ("\n SSLError is %s\n " % x .args [1 ])
2114- except OSError as x :
2115- if support .verbose :
2116- sys .stdout .write ("\n OSError is %s\n " % x .args [1 ])
2117- else :
2118- raise AssertionError ("Use of invalid cert should have failed!" )
2119-
21202118 def server_params_test (client_context , server_context , indata = b"FOO\n " ,
21212119 chatty = True , connectionchatty = False , sni_name = None ):
21222120 """
@@ -2357,22 +2355,38 @@ def test_check_hostname(self):
23572355 "check_hostname requires server_hostname" ):
23582356 context .wrap_socket (s )
23592357
2360- def test_empty_cert (self ):
2361- """Connecting with an empty cert file"""
2362- bad_cert_test (os .path .join (os .path .dirname (__file__ ) or os .curdir ,
2363- "nullcert.pem" ))
2364- def test_malformed_cert (self ):
2365- """Connecting with a badly formatted certificate (syntax error)"""
2366- bad_cert_test (os .path .join (os .path .dirname (__file__ ) or os .curdir ,
2367- "badcert.pem" ))
23682358 def test_wrong_cert (self ):
2369- """Connecting with a cert file not matching the server"""
2370- bad_cert_test (os .path .join (os .path .dirname (__file__ ) or os .curdir ,
2371- "wrongcert.pem" ))
2372- def test_malformed_key (self ):
2373- """Connecting with a badly formatted key (syntax error)"""
2374- bad_cert_test (os .path .join (os .path .dirname (__file__ ) or os .curdir ,
2375- "badkey.pem" ))
2359+ """Connecting when the server rejects the client's certificate
2360+
2361+ Launch a server with CERT_REQUIRED, and check that trying to
2362+ connect to it with a wrong client certificate fails.
2363+ """
2364+ certfile = os .path .join (os .path .dirname (__file__ ) or os .curdir ,
2365+ "wrongcert.pem" )
2366+ server = ThreadedEchoServer (CERTFILE ,
2367+ certreqs = ssl .CERT_REQUIRED ,
2368+ cacerts = CERTFILE , chatty = False ,
2369+ connectionchatty = False )
2370+ with server , \
2371+ socket .socket () as sock , \
2372+ ssl .wrap_socket (sock ,
2373+ certfile = certfile ,
2374+ ssl_version = ssl .PROTOCOL_TLSv1 ) as s :
2375+ try :
2376+ # Expect either an SSL error about the server rejecting
2377+ # the connection, or a low-level connection reset (which
2378+ # sometimes happens on Windows)
2379+ s .connect ((HOST , server .port ))
2380+ except ssl .SSLError as e :
2381+ if support .verbose :
2382+ sys .stdout .write ("\n SSLError is %r\n " % e )
2383+ except OSError as e :
2384+ if e .errno != errno .ECONNRESET :
2385+ raise
2386+ if support .verbose :
2387+ sys .stdout .write ("\n socket.error is %r\n " % e )
2388+ else :
2389+ self .fail ("Use of invalid cert should have failed!" )
23762390
23772391 def test_rude_shutdown (self ):
23782392 """A brutal shutdown of an SSL server should raise an OSError
0 commit comments