@@ -48,6 +48,9 @@ def data_file(*name):
4848CAFILE_CACERT = data_file ("capath" , "5ed36f99.0" )
4949
5050
51+ # empty CRL
52+ CRLFILE = data_file ("revocation.crl" )
53+
5154# Two keys and certs signed by the same CA (for SNI tests)
5255SIGNED_CERTFILE = data_file ("keycert3.pem" )
5356SIGNED_CERTFILE2 = data_file ("keycert4.pem" )
@@ -631,7 +634,7 @@ def test_options(self):
631634 with self .assertRaises (ValueError ):
632635 ctx .options = 0
633636
634- def test_verify (self ):
637+ def test_verify_mode (self ):
635638 ctx = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
636639 # Default value
637640 self .assertEqual (ctx .verify_mode , ssl .CERT_NONE )
@@ -646,6 +649,23 @@ def test_verify(self):
646649 with self .assertRaises (ValueError ):
647650 ctx .verify_mode = 42
648651
652+ def test_verify_flags (self ):
653+ ctx = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
654+ # default value by OpenSSL
655+ self .assertEqual (ctx .verify_flags , ssl .VERIFY_DEFAULT )
656+ ctx .verify_flags = ssl .VERIFY_CRL_CHECK_LEAF
657+ self .assertEqual (ctx .verify_flags , ssl .VERIFY_CRL_CHECK_LEAF )
658+ ctx .verify_flags = ssl .VERIFY_CRL_CHECK_CHAIN
659+ self .assertEqual (ctx .verify_flags , ssl .VERIFY_CRL_CHECK_CHAIN )
660+ ctx .verify_flags = ssl .VERIFY_DEFAULT
661+ self .assertEqual (ctx .verify_flags , ssl .VERIFY_DEFAULT )
662+ # supports any value
663+ ctx .verify_flags = ssl .VERIFY_CRL_CHECK_LEAF | ssl .VERIFY_X509_STRICT
664+ self .assertEqual (ctx .verify_flags ,
665+ ssl .VERIFY_CRL_CHECK_LEAF | ssl .VERIFY_X509_STRICT )
666+ with self .assertRaises (TypeError ):
667+ ctx .verify_flags = None
668+
649669 def test_load_cert_chain (self ):
650670 ctx = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
651671 # Combined key and cert in a single file
@@ -1771,6 +1791,47 @@ def test_getpeercert(self):
17711791 self .assertLess (before , after )
17721792 s .close ()
17731793
1794+ def test_crl_check (self ):
1795+ if support .verbose :
1796+ sys .stdout .write ("\n " )
1797+
1798+ server_context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
1799+ server_context .load_cert_chain (SIGNED_CERTFILE )
1800+
1801+ context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
1802+ context .verify_mode = ssl .CERT_REQUIRED
1803+ context .load_verify_locations (SIGNING_CA )
1804+ context .verify_mode = ssl .CERT_REQUIRED
1805+ context .verify_flags = ssl .VERIFY_DEFAULT
1806+
1807+ # VERIFY_DEFAULT should pass
1808+ server = ThreadedEchoServer (context = server_context , chatty = True )
1809+ with server :
1810+ with context .wrap_socket (socket .socket ()) as s :
1811+ s .connect ((HOST , server .port ))
1812+ cert = s .getpeercert ()
1813+ self .assertTrue (cert , "Can't get peer certificate." )
1814+
1815+ # VERIFY_CRL_CHECK_LEAF without a loaded CRL file fails
1816+ context .verify_flags = ssl .VERIFY_CRL_CHECK_LEAF
1817+
1818+ server = ThreadedEchoServer (context = server_context , chatty = True )
1819+ with server :
1820+ with context .wrap_socket (socket .socket ()) as s :
1821+ with self .assertRaisesRegex (ssl .SSLError ,
1822+ "certificate verify failed" ):
1823+ s .connect ((HOST , server .port ))
1824+
1825+ # now load a CRL file. The CRL file is signed by the CA.
1826+ context .load_verify_locations (CRLFILE )
1827+
1828+ server = ThreadedEchoServer (context = server_context , chatty = True )
1829+ with server :
1830+ with context .wrap_socket (socket .socket ()) as s :
1831+ s .connect ((HOST , server .port ))
1832+ cert = s .getpeercert ()
1833+ self .assertTrue (cert , "Can't get peer certificate." )
1834+
17741835 def test_empty_cert (self ):
17751836 """Connecting with an empty cert file"""
17761837 bad_cert_test (os .path .join (os .path .dirname (__file__ ) or os .curdir ,
0 commit comments