@@ -217,21 +217,14 @@ def test_load_default_verify_paths(self):
217
217
os .environ ["SSL_CERT_FILE" ] = certFile
218
218
if certDir is not None :
219
219
os .environ ["SSL_CERT_DIR" ] = certDir
220
-
221
- def test_verify_error (self ):
222
- hostname = 'localhost'
223
- SIGNED_CERTFILE = data_file ("signed_cert.pem" )
224
-
225
- server_context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
226
- server_context .load_cert_chain (SIGNED_CERTFILE )
227
-
228
- context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
229
220
221
+ def check_handshake (self , server_context , client_context , err = None ):
222
+ hostname = 'localhost'
230
223
c_in = ssl .MemoryBIO ()
231
224
c_out = ssl .MemoryBIO ()
232
225
s_in = ssl .MemoryBIO ()
233
226
s_out = ssl .MemoryBIO ()
234
- client = context .wrap_bio (c_in , c_out , server_hostname = hostname )
227
+ client = client_context .wrap_bio (c_in , c_out , server_hostname = hostname )
235
228
server = server_context .wrap_bio (s_in , s_out , server_side = True )
236
229
237
230
try :
@@ -248,12 +241,130 @@ def test_verify_error(self):
248
241
pass
249
242
if s_out .pending :
250
243
c_in .write (s_out .read ())
251
- except ssl .SSLCertVerificationError as e :
252
- self .assertIsNotNone (e .verify_code )
253
- self .assertIsNotNone (e .verify_message )
244
+ except Exception as e :
245
+ if err is None :
246
+ assert False
247
+ else :
248
+ assert isinstance (e , err )
254
249
else :
250
+ if err is not None :
255
251
assert False
256
-
252
+
253
+ def test_verify_mode (self ):
254
+ signed_cert = data_file ("signed_cert.pem" )
255
+ signed_cert2 = data_file ("keycertecc.pem" )
256
+ signing_ca = data_file ("signing_ca.pem" )
257
+
258
+ ########################################################################
259
+ # verify_mode - client
260
+ ########################################################################
261
+
262
+ server_context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
263
+ client_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
264
+
265
+ server_context .verify_mode = ssl .CERT_NONE
266
+
267
+ client_context .check_hostname = False
268
+
269
+ # no cert chain on server
270
+ # openssl SSLError: [SSL: NO_SHARED_CIPHER] / jdk javax.net.ssl.SSLHandshakeException: No available authentication scheme
271
+ client_context .verify_mode = ssl .CERT_NONE
272
+ self .check_handshake (server_context , client_context , ssl .SSLError )
273
+ client_context .verify_mode = ssl .CERT_REQUIRED
274
+ self .check_handshake (server_context , client_context , ssl .SSLError )
275
+ client_context .verify_mode = ssl .CERT_OPTIONAL
276
+ self .check_handshake (server_context , client_context , ssl .SSLError )
277
+
278
+ # server provides cert, but client has noverify locations
279
+ server_context .load_cert_chain (signed_cert )
280
+
281
+ client_context .verify_mode = ssl .CERT_NONE
282
+ self .check_handshake (server_context , client_context )
283
+ client_context .verify_mode = ssl .CERT_REQUIRED
284
+ self .check_handshake (server_context , client_context , ssl .SSLCertVerificationError )
285
+ client_context .verify_mode = ssl .CERT_OPTIONAL
286
+ # CERT_OPTIONAL in client mode has the same meaning as CERT_REQUIRED
287
+ self .check_handshake (server_context , client_context , ssl .SSLCertVerificationError )
288
+
289
+ client_context .check_hostname = True
290
+
291
+ with self .assertRaisesRegex (ValueError , "Cannot set verify_mode to CERT_NONE when check_hostname is enabled" ):
292
+ client_context .verify_mode = ssl .CERT_NONE
293
+
294
+ client_context .verify_mode = ssl .CERT_REQUIRED
295
+ self .check_handshake (server_context , client_context , ssl .SSLCertVerificationError )
296
+
297
+ client_context .verify_mode = ssl .CERT_OPTIONAL
298
+ # CERT_OPTIONAL in client mode has the same meaning as CERT_REQUIRED
299
+ self .check_handshake (server_context , client_context , ssl .SSLCertVerificationError )
300
+
301
+ # client provides cert, server verifies
302
+ client_context .load_verify_locations (signing_ca )
303
+
304
+ client_context .verify_mode = ssl .CERT_REQUIRED
305
+ self .check_handshake (server_context , client_context )
306
+ client_context .verify_mode = ssl .CERT_OPTIONAL
307
+ self .check_handshake (server_context , client_context )
308
+
309
+ # server provides wrong cert for CERT_OPTIONAL client
310
+ server_context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
311
+ server_context .load_cert_chain (signed_cert2 )
312
+ self .check_handshake (server_context , client_context , ssl .SSLCertVerificationError )
313
+
314
+ ########################################################################
315
+ # verify_mode - server
316
+ ########################################################################
317
+
318
+ server_context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
319
+ client_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
320
+
321
+ client_context .check_hostname = False
322
+ client_context .verify_mode = ssl .CERT_NONE
323
+
324
+ # no cert chain on server and client
325
+ # openssl SSLError: [SSL: NO_SHARED_CIPHER] / jdk javax.net.ssl.SSLHandshakeException: No available authentication scheme
326
+ server_context .verify_mode = ssl .CERT_NONE
327
+ self .check_handshake (server_context , client_context , ssl .SSLError )
328
+ server_context .verify_mode = ssl .CERT_REQUIRED
329
+ self .check_handshake (server_context , client_context , ssl .SSLError )
330
+ server_context .verify_mode = ssl .CERT_OPTIONAL
331
+ self .check_handshake (server_context , client_context , ssl .SSLError )
332
+
333
+ # no cert from client
334
+ server_context .load_cert_chain (signed_cert )
335
+
336
+ server_context .verify_mode = ssl .CERT_NONE
337
+ self .check_handshake (server_context , client_context )
338
+ server_context .verify_mode = ssl .CERT_REQUIRED
339
+ self .check_handshake (server_context , client_context , ssl .SSLError )
340
+ server_context .verify_mode = ssl .CERT_OPTIONAL
341
+ self .check_handshake (server_context , client_context )
342
+
343
+ # client provides cert, but server has nothing to verify with
344
+ client_context .load_cert_chain (signed_cert )
345
+
346
+ server_context .verify_mode = ssl .CERT_NONE
347
+ self .check_handshake (server_context , client_context )
348
+ server_context .verify_mode = ssl .CERT_REQUIRED
349
+ self .check_handshake (server_context , client_context , ssl .SSLError )
350
+ server_context .verify_mode = ssl .CERT_OPTIONAL
351
+ self .check_handshake (server_context , client_context , ssl .SSLCertVerificationError )
352
+
353
+ # client provides cert, server verifies
354
+ server_context .load_verify_locations (signing_ca )
355
+
356
+ server_context .verify_mode = ssl .CERT_NONE
357
+ self .check_handshake (server_context , client_context )
358
+ server_context .verify_mode = ssl .CERT_REQUIRED
359
+ self .check_handshake (server_context , client_context )
360
+ server_context .verify_mode = ssl .CERT_OPTIONAL
361
+ self .check_handshake (server_context , client_context )
362
+
363
+ # client provides wrong cert for CERT_OPTIONAL server
364
+ client_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
365
+ client_context .load_cert_chain (signed_cert2 )
366
+ self .check_handshake (server_context , client_context , ssl .SSLCertVerificationError )
367
+
257
368
def get_cipher_list (cipher_string ):
258
369
context = ssl .SSLContext ()
259
370
context .set_ciphers (cipher_string )
0 commit comments