@@ -77,12 +77,14 @@ def check_handshake(server_context, client_context, err = None):
77
77
except Exception as e :
78
78
if err is None :
79
79
assert False
80
- else :
80
+ else :
81
81
assert isinstance (e , err )
82
82
else :
83
83
if err is not None :
84
84
assert False
85
-
85
+ return server , client
86
+
87
+
86
88
class CertTests (unittest .TestCase ):
87
89
88
90
ctx = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
@@ -110,28 +112,28 @@ def check_load_verify_locations_error(self, cafile=None, capath=None, cadata=Non
110
112
cafile = data_file (cafile )
111
113
if cadata is not None :
112
114
cadata = open (data_file (cadata )).read ()
113
- self .ctx .load_verify_locations (cafile , capath , cadata )
115
+ self .ctx .load_verify_locations (cafile , capath , cadata )
114
116
except err as e :
115
117
if errno != - 1 :
116
118
self .assertEqual (e .errno , errno )
117
- if strerror is not None :
119
+ if strerror is not None :
118
120
if isinstance (ssl .SSLError , err ):
119
121
self .assertIn (strerror , e .strerror )
120
122
else :
121
123
self .assertIn (strerror , str (e ))
122
124
self .assertIsInstance (type (e ), type (err ))
123
125
else :
124
126
assert False
125
-
127
+
126
128
def check_load_verify_locations_cadata_bytes_error (self , cadata , errno = - 1 , strerror = None , err = ssl .SSLError ):
127
- try :
129
+ try :
128
130
cadata = open (data_file (cadata )).read ()
129
131
cadata .replace ("" )
130
- self .ctx .load_verify_locations (cafile , capath , cadata )
132
+ self .ctx .load_verify_locations (cafile , capath , cadata )
131
133
except err as e :
132
134
if errno != - 1 :
133
135
self .assertEqual (e .errno , errno )
134
- if strerror is not None :
136
+ if strerror is not None :
135
137
if isinstance (ssl .SSLError , err ):
136
138
self .assertIn (strerror , e .strerror )
137
139
else :
@@ -176,14 +178,14 @@ def test_load_cert_chain(self):
176
178
self .check_load_cert_chain_error (certfile = "cert_rsa.pem" , keyfile = "broken_pk_no_begin.pem" , errno = 9 , strerror = "[SSL] PEM lib" )
177
179
self .check_load_cert_chain_error (certfile = "cert_rsa.pem" , keyfile = "broken_pk_no_end.pem" , errno = 9 , strerror = "[SSL] PEM lib" )
178
180
179
- self .check_load_cert_chain_error (certfile = "cert_rsa2.pem" , keyfile = "pk_rsa.pem" , errno = 116 , strerror = "[X509: KEY_VALUES_MISMATCH] key values mismatch" )
180
- self .check_load_cert_chain_error (certfile = "cert_rsa2.pem" , keyfile = "pk_ecc.pem" )
181
+ self .check_load_cert_chain_error (certfile = "cert_rsa2.pem" , keyfile = "pk_rsa.pem" , errno = 116 , strerror = "[X509: KEY_VALUES_MISMATCH] key values mismatch" )
182
+ self .check_load_cert_chain_error (certfile = "cert_rsa2.pem" , keyfile = "pk_ecc.pem" )
181
183
182
184
def test_load_verify_locations (self ):
183
185
self .ctx .load_verify_locations (data_file ("cert_rsa.pem" ))
184
186
self .ctx .load_verify_locations (capath = data_file ("cert_rsa.pem" ))
185
187
cad = open (data_file ("cert_rsa.pem" )).read ()
186
- self .ctx .load_verify_locations (cadata = cad )
188
+ self .ctx .load_verify_locations (cadata = cad )
187
189
cad = ssl .PEM_cert_to_DER_cert (cad )
188
190
self .ctx .load_verify_locations (cadata = cad )
189
191
self .ctx .load_verify_locations (data_file ("cert_rsa.pem" ), 'does_not_exit' )
@@ -224,12 +226,12 @@ def test_load_verify_locations(self):
224
226
self .check_load_verify_locations_error (cadata = "broken_cert_no_end.pem" )
225
227
self .check_load_verify_locations_error (cadata = "broken_cert_data.pem" , errno = 100 , strerror = "[PEM: BAD_BASE64_DECODE]" )
226
228
self .check_load_verify_locations_error (cadata = "broken_cert_data_at_begin.pem" , errno = 100 , strerror = "[PEM: BAD_BASE64_DECODE]" )
227
- self .check_load_verify_locations_error (cadata = "broken_cert_data_at_end.pem" , errno = 100 , strerror = "[PEM: BAD_BASE64_DECODE]" )
229
+ self .check_load_verify_locations_error (cadata = "broken_cert_data_at_end.pem" , errno = 100 , strerror = "[PEM: BAD_BASE64_DECODE]" )
228
230
229
231
def test_load_default_verify_paths (self ):
230
232
env = os .environ
231
233
certFile = env ["SSL_CERT_FILE" ] if "SSL_CERT_FILE" in env else None
232
- certDir = env ["SSL_CERT_DIR" ] if "SSL_CERT_DIR" in env else None
234
+ certDir = env ["SSL_CERT_DIR" ] if "SSL_CERT_DIR" in env else None
233
235
try :
234
236
env ["SSL_CERT_DIR" ] = "does_not_exit"
235
237
env ["SSL_CERT_FILE" ] = "does_not_exit"
@@ -243,18 +245,18 @@ def test_load_default_verify_paths(self):
243
245
except Exception :
244
246
# load_default_certs reports no errors
245
247
assert False
246
- finally :
248
+ finally :
247
249
if certFile is not None :
248
250
env ["SSL_CERT_FILE" ] = certFile
249
251
else :
250
252
del env ["SSL_CERT_FILE" ]
251
253
if certDir is not None :
252
254
env ["SSL_CERT_DIR" ] = certDir
253
- else :
255
+ else :
254
256
del env ["SSL_CERT_DIR" ]
255
257
256
258
@unittest .skipIf (sys .implementation .name == 'cpython' , "graalpython specific" )
257
- def test_load_default_verify_keystore (self ):
259
+ def test_load_default_verify_keystore (self ):
258
260
# execute with javax.net.ssl.trustStore=tests/ssldata/signing_keystore.jks
259
261
# the JKS keystore:
260
262
# - contains one trusted certificate, the same as in tests/ssldata/signing_ca.pem
@@ -285,7 +287,7 @@ def test_verify_mode(self):
285
287
server_context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
286
288
client_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
287
289
288
- server_context .verify_mode = ssl .CERT_NONE
290
+ server_context .verify_mode = ssl .CERT_NONE
289
291
290
292
client_context .check_hostname = False
291
293
@@ -323,15 +325,15 @@ def test_verify_mode(self):
323
325
324
326
# client provides cert, server verifies
325
327
client_context .load_verify_locations (signing_ca )
326
-
328
+
327
329
client_context .verify_mode = ssl .CERT_REQUIRED
328
330
check_handshake (server_context , client_context )
329
331
client_context .verify_mode = ssl .CERT_OPTIONAL
330
332
check_handshake (server_context , client_context )
331
333
332
334
# server provides wrong cert for CERT_OPTIONAL client
333
335
server_context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
334
- server_context .load_cert_chain (signed_cert2 )
336
+ server_context .load_cert_chain (signed_cert2 )
335
337
check_handshake (server_context , client_context , ssl .SSLCertVerificationError )
336
338
337
339
########################################################################
@@ -352,7 +354,7 @@ def test_verify_mode(self):
352
354
check_handshake (server_context , client_context , ssl .SSLError )
353
355
server_context .verify_mode = ssl .CERT_OPTIONAL
354
356
check_handshake (server_context , client_context , ssl .SSLError )
355
-
357
+
356
358
# no cert from client
357
359
server_context .load_cert_chain (signed_cert )
358
360
@@ -388,6 +390,27 @@ def test_verify_mode(self):
388
390
client_context .load_cert_chain (signed_cert2 )
389
391
check_handshake (server_context , client_context , ssl .SSLCertVerificationError )
390
392
393
+ def test_alpn (self ):
394
+ signed_cert = data_file ("signed_cert.pem" )
395
+ server_context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
396
+ server_context .load_cert_chain (signed_cert )
397
+ server_context .verify_mode = ssl .CERT_NONE
398
+ client_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
399
+ client_context .check_hostname = False
400
+ client_context .verify_mode = ssl .CERT_NONE
401
+ server , client = check_handshake (server_context , client_context )
402
+ self .assertIsNone (client .selected_alpn_protocol ())
403
+
404
+ server_context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
405
+ server_context .load_cert_chain (signed_cert )
406
+ server_context .set_alpn_protocols (["http/1.1" ])
407
+ client_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
408
+ client_context .check_hostname = False
409
+ client_context .verify_mode = ssl .CERT_NONE
410
+ client_context .set_alpn_protocols (["http/1.1" ])
411
+ server , client = check_handshake (server_context , client_context )
412
+ self .assertEqual (client .selected_alpn_protocol (), "http/1.1" )
413
+
391
414
def get_cipher_list (cipher_string ):
392
415
context = ssl .SSLContext ()
393
416
context .set_ciphers (cipher_string )
0 commit comments