Debian Codegraph @

python/tests/proton_tests/ssl.py

raw
1 #
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
17 # under the License.
18 #
19
20 from __future__ import absolute_import
21
22 import os
23
24 from proton import *
25
26 from . import common
27 from .common import Skipped, pump
28
29
30 def _testpath(file):
31 """ Set the full path to the certificate,keyfile, etc. for the test.
32 """
33 if os.name == "nt":
34 if file.find("private-key") != -1:
35 # The private key is not in a separate store
36 return None
37 # Substitute pkcs#12 equivalent for the CA/key store
38 if file.endswith(".pem"):
39 file = file[:-4] + ".p12"
40 return os.path.join(os.path.dirname(__file__),
41 "ssl_db/%s" % file)
42
43
44 class SslTest(common.Test):
45
46 def __init__(self, *args):
47 common.Test.__init__(self, *args)
48 self._testpath = _testpath
49
50 def setUp(self):
51 if not common.isSSLPresent():
52 raise Skipped("No SSL libraries found.")
53 self.server_domain = SSLDomain(SSLDomain.MODE_SERVER)
54 self.client_domain = SSLDomain(SSLDomain.MODE_CLIENT)
55
56 def tearDown(self):
57 self.server_domain = None
58 self.client_domain = None
59
60 class SslTestConnection(object):
61 """ Represents a single SSL connection.
62 """
63
64 def __init__(self, domain=None, mode=Transport.CLIENT,
65 session_details=None, conn_hostname=None,
66 ssl_peername=None):
67 if not common.isSSLPresent():
68 raise Skipped("No SSL libraries found.")
69
70 self.ssl = None
71 self.domain = domain
72 self.transport = Transport(mode)
73 self.connection = Connection()
74 if conn_hostname:
75 self.connection.hostname = conn_hostname
76 if domain:
77 self.ssl = SSL(self.transport, self.domain, session_details)
78 if ssl_peername:
79 self.ssl.peer_hostname = ssl_peername
80 # bind last, after all configuration complete:
81 self.transport.bind(self.connection)
82
83 def _pump(self, ssl_client, ssl_server, buffer_size=1024):
84 pump(ssl_client.transport, ssl_server.transport, buffer_size)
85
86 def _do_handshake(self, client, server):
87 """ Attempt to connect client to server. Will throw a TransportException if the SSL
88 handshake fails.
89 """
90 client.connection.open()
91 server.connection.open()
92 self._pump(client, server)
93 if client.transport.closed:
94 return
95 assert client.ssl.protocol_name() is not None
96 client.connection.close()
97 server.connection.close()
98 self._pump(client, server)
99
100 def test_anonymous_cipher(self):
101 if os.name == "nt":
102 raise Skipped("Windows SChannel lacks anonymous cipher support.")
103 """ With no configuration at all, the client default
104 VERIFY_PEER_NAME should preclude anonymous cipher TLS negotiation.
105 """
106 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
107 client = SslTest.SslTestConnection(self.client_domain)
108
109 # check that no SSL connection exists
110 assert not server.ssl.cipher_name()
111 assert not client.ssl.protocol_name()
112
113 # client.transport.trace(Transport.TRACE_DRV)
114 # server.transport.trace(Transport.TRACE_DRV)
115
116 client.connection.open()
117 server.connection.open()
118 self._pump(client, server)
119
120 assert client.transport.closed
121 assert server.transport.closed
122 assert client.connection.state & Endpoint.REMOTE_UNINIT
123 assert server.connection.state & Endpoint.REMOTE_UNINIT
124
125 def test_simple_anonymous(self):
126 if os.name == "nt":
127 raise Skipped("Windows SChannel lacks anonymous cipher support.")
128 """ The simplest SSL configuration using anonymous
129 ciphers.
130 """
131 self.client_domain.set_peer_authentication(SSLDomain.ANONYMOUS_PEER)
132 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
133 client = SslTest.SslTestConnection(self.client_domain)
134
135 # check that no SSL connection exists
136 assert not server.ssl.cipher_name()
137 assert not client.ssl.protocol_name()
138
139 # client.transport.trace(Transport.TRACE_DRV)
140 # server.transport.trace(Transport.TRACE_DRV)
141
142 client.connection.open()
143 server.connection.open()
144 self._pump(client, server)
145
146 # now SSL should be active
147 assert server.ssl.cipher_name() is not None
148 assert client.ssl.protocol_name() is not None
149
150 client.connection.close()
151 server.connection.close()
152 self._pump(client, server)
153
154 def test_ssl_with_small_buffer(self):
155 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
156 self._testpath("server-private-key.pem"),
157 "server-password")
158 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
159 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER)
160
161 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
162 client = SslTest.SslTestConnection(self.client_domain)
163
164 client.connection.open()
165 server.connection.open()
166
167 small_buffer_size = 1
168 self._pump(client, server, small_buffer_size)
169
170 assert client.ssl.protocol_name() is not None
171 client.connection.close()
172 server.connection.close()
173 self._pump(client, server)
174
175 def test_server_certificate_fail(self):
176 """ Test that default configured clients cannot connect to a server that has
177 a certificate configured.
178 """
179 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
180 self._testpath("server-private-key.pem"),
181 "server-password")
182 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
183 client = SslTest.SslTestConnection(self.client_domain)
184
185 client.connection.open()
186 server.connection.open()
187 self._pump(client, server)
188
189 assert client.transport.closed
190 assert server.transport.closed
191 assert client.connection.state & Endpoint.REMOTE_UNINIT
192 assert server.connection.state & Endpoint.REMOTE_UNINIT
193
194 def test_server_certificate_no_verify(self):
195 """ Test that anonymous clients can still connect to a server that has
196 a certificate configured.
197 """
198 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
199 self._testpath("server-private-key.pem"),
200 "server-password")
201 self.client_domain.set_peer_authentication(SSLDomain.ANONYMOUS_PEER)
202 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
203 client = SslTest.SslTestConnection(self.client_domain)
204
205 client.connection.open()
206 server.connection.open()
207 self._pump(client, server)
208 assert client.ssl.protocol_name() is not None
209 client.connection.close()
210 server.connection.close()
211 self._pump(client, server)
212
213 def test_server_authentication(self):
214 """ Simple SSL connection with authentication of the server
215 """
216 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
217 self._testpath("server-private-key.pem"),
218 "server-password")
219
220 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
221 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER)
222
223 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
224 client = SslTest.SslTestConnection(self.client_domain)
225
226 client.connection.open()
227 server.connection.open()
228 self._pump(client, server)
229 assert client.ssl.protocol_name() is not None
230 client.connection.close()
231 server.connection.close()
232 self._pump(client, server)
233
234 def test_certificate_fingerprint_and_subfields(self):
235 if os.name == "nt":
236 raise Skipped("Windows support for certificate fingerprint and subfield not implemented yet")
237
238 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
239 self._testpath("server-private-key.pem"),
240 "server-password")
241 self.server_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
242 self.server_domain.set_peer_authentication(SSLDomain.VERIFY_PEER,
243 self._testpath("ca-certificate.pem"))
244 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
245
246 # give the client a certificate, but let's not require server authentication
247 self.client_domain.set_credentials(self._testpath("client-certificate1.pem"),
248 self._testpath("client-private-key1.pem"),
249 "client-password")
250 self.client_domain.set_peer_authentication(SSLDomain.ANONYMOUS_PEER)
251 client = SslTest.SslTestConnection(self.client_domain)
252
253 client.connection.open()
254 server.connection.open()
255 self._pump(client, server)
256
257 # Test the subject subfields
258 self.assertEqual("Client", server.ssl.get_cert_organization())
259 self.assertEqual("Dev", server.ssl.get_cert_organization_unit())
260 self.assertEqual("ST", server.ssl.get_cert_state_or_province())
261 self.assertEqual("US", server.ssl.get_cert_country())
262 self.assertEqual("City", server.ssl.get_cert_locality_or_city())
263 self.assertEqual("O=Server,CN=A1.Good.Server.domain.com", client.ssl.get_cert_subject())
264 self.assertEqual("O=Client,CN=127.0.0.1,C=US,ST=ST,L=City,OU=Dev", server.ssl.get_cert_subject())
265
266 self.assertEqual("1762688fb3c1c7908690efa158e89924a8e739b6", server.ssl.get_cert_fingerprint_sha1())
267 self.assertEqual("f5890052d5ad3f38f68eae9027b30fc4c3f09758d59d8f19e3f192e29e41708d",
268 server.ssl.get_cert_fingerprint_sha256())
269 self.assertEqual("5d2b1bff39df99d101040348be015970e3da1d0ad610902dc78bba24555aae16395fd1342e26b89b422c304c32b48913000b485933720773d033aaffc561b3e9",
270 server.ssl.get_cert_fingerprint_sha512())
271 self.assertEqual("246ea13c8a549e8c0b0cc6490d81b34b", server.ssl.get_cert_fingerprint_md5())
272
273 # Test the various fingerprint algorithms
274 self.assertEqual("d28c0ae17c370c269bd680ea3bcc523ea5da544e", client.ssl.get_cert_fingerprint_sha1())
275 self.assertEqual("c460a601f77b77ec59480955574a227c309805dd36dbf866f42d2dce3fd4757c",
276 client.ssl.get_cert_fingerprint_sha256())
277 self.assertEqual("37c28f451105b1979e2ea62d4a38c86e158ad345894b5016662bdd1913f48764bd71deb4b4de4ce22559828634357dcaea1832dd58327dfe5b0bc368ecbeee4c",
278 client.ssl.get_cert_fingerprint_sha512())
279 self.assertEqual("16b075688b82c40ce5b03c984f20286b", client.ssl.get_cert_fingerprint_md5())
280
281 self.assertEqual(None, client.ssl.get_cert_fingerprint(21, SSL.SHA1)) # Should be at least 41
282 self.assertEqual(None, client.ssl.get_cert_fingerprint(50, SSL.SHA256)) # Should be at least 65
283 self.assertEqual(None, client.ssl.get_cert_fingerprint(128, SSL.SHA512)) # Should be at least 129
284 self.assertEqual(None, client.ssl.get_cert_fingerprint(10, SSL.MD5)) # Should be at least 33
285 self.assertEqual(None, client.ssl._get_cert_subject_unknown_subfield())
286
287 self.assertNotEqual(None, client.ssl.get_cert_fingerprint(50, SSL.SHA1)) # Should be at least 41
288 self.assertNotEqual(None, client.ssl.get_cert_fingerprint(70, SSL.SHA256)) # Should be at least 65
289 self.assertNotEqual(None, client.ssl.get_cert_fingerprint(130, SSL.SHA512)) # Should be at least 129
290 self.assertNotEqual(None, client.ssl.get_cert_fingerprint(35, SSL.MD5)) # Should be at least 33
291 self.assertEqual(None, client.ssl._get_cert_fingerprint_unknown_hash_alg())
292
293 def test_client_authentication(self):
294 """ Force the client to authenticate.
295 """
296 # note: when requesting client auth, the server _must_ send its
297 # certificate, so make sure we configure one!
298 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
299 self._testpath("server-private-key.pem"),
300 "server-password")
301 self.server_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
302 self.server_domain.set_peer_authentication(SSLDomain.VERIFY_PEER,
303 self._testpath("ca-certificate.pem"))
304 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
305
306 # give the client a certificate, but let's not require server authentication
307 self.client_domain.set_credentials(self._testpath("client-certificate.pem"),
308 self._testpath("client-private-key.pem"),
309 "client-password")
310 self.client_domain.set_peer_authentication(SSLDomain.ANONYMOUS_PEER)
311 client = SslTest.SslTestConnection(self.client_domain)
312
313 client.connection.open()
314 server.connection.open()
315 self._pump(client, server)
316
317 assert client.ssl.protocol_name() is not None
318 client.connection.close()
319 server.connection.close()
320 self._pump(client, server)
321
322 def test_client_authentication_fail_bad_cert(self):
323 """ Ensure that the server can detect a bad client certificate.
324 """
325 # note: when requesting client auth, the server _must_ send its
326 # certificate, so make sure we configure one!
327 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
328 self._testpath("server-private-key.pem"),
329 "server-password")
330 self.server_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
331 self.server_domain.set_peer_authentication(SSLDomain.VERIFY_PEER,
332 self._testpath("ca-certificate.pem"))
333 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
334
335 self.client_domain.set_credentials(self._testpath("bad-server-certificate.pem"),
336 self._testpath("bad-server-private-key.pem"),
337 "server-password")
338 self.client_domain.set_peer_authentication(SSLDomain.ANONYMOUS_PEER)
339 client = SslTest.SslTestConnection(self.client_domain)
340
341 client.connection.open()
342 server.connection.open()
343 self._pump(client, server)
344 assert client.transport.closed
345 assert server.transport.closed
346 assert client.connection.state & Endpoint.REMOTE_UNINIT
347 assert server.connection.state & Endpoint.REMOTE_UNINIT
348
349 def test_client_authentication_fail_no_cert(self):
350 """ Ensure that the server will fail a client that does not provide a
351 certificate.
352 """
353 # note: when requesting client auth, the server _must_ send its
354 # certificate, so make sure we configure one!
355 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
356 self._testpath("server-private-key.pem"),
357 "server-password")
358 self.server_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
359 self.server_domain.set_peer_authentication(SSLDomain.VERIFY_PEER,
360 self._testpath("ca-certificate.pem"))
361 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
362
363 self.client_domain.set_peer_authentication(SSLDomain.ANONYMOUS_PEER)
364 client = SslTest.SslTestConnection(self.client_domain)
365
366 client.connection.open()
367 server.connection.open()
368 self._pump(client, server)
369 assert client.transport.closed
370 assert server.transport.closed
371 assert client.connection.state & Endpoint.REMOTE_UNINIT
372 assert server.connection.state & Endpoint.REMOTE_UNINIT
373
374 def test_client_server_authentication(self):
375 """ Require both client and server to mutually identify themselves.
376 """
377 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
378 self._testpath("server-private-key.pem"),
379 "server-password")
380 self.server_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
381 self.server_domain.set_peer_authentication(SSLDomain.VERIFY_PEER,
382 self._testpath("ca-certificate.pem"))
383
384 self.client_domain.set_credentials(self._testpath("client-certificate.pem"),
385 self._testpath("client-private-key.pem"),
386 "client-password")
387 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
388 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER)
389
390 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
391 client = SslTest.SslTestConnection(self.client_domain)
392
393 client.connection.open()
394 server.connection.open()
395 self._pump(client, server)
396 assert client.ssl.protocol_name() is not None
397 client.connection.close()
398 server.connection.close()
399 self._pump(client, server)
400
401 def test_server_only_authentication(self):
402 """ Client verifies server, but server does not verify client.
403 """
404 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
405 self._testpath("server-private-key.pem"),
406 "server-password")
407 self.server_domain.set_peer_authentication(SSLDomain.ANONYMOUS_PEER)
408
409 self.client_domain.set_credentials(self._testpath("client-certificate.pem"),
410 self._testpath("client-private-key.pem"),
411 "client-password")
412 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
413 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER)
414
415 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
416 client = SslTest.SslTestConnection(self.client_domain)
417
418 client.connection.open()
419 server.connection.open()
420 self._pump(client, server)
421 assert client.ssl.protocol_name() is not None
422 client.connection.close()
423 server.connection.close()
424 self._pump(client, server)
425
426 def test_bad_server_certificate(self):
427 """ A server with a self-signed certificate that is not trusted by the
428 client. The client should reject the server.
429 """
430 self.server_domain.set_credentials(self._testpath("bad-server-certificate.pem"),
431 self._testpath("bad-server-private-key.pem"),
432 "server-password")
433 self.server_domain.set_peer_authentication(SSLDomain.ANONYMOUS_PEER)
434
435 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
436 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER)
437
438 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
439 client = SslTest.SslTestConnection(self.client_domain)
440
441 client.connection.open()
442 server.connection.open()
443 self._pump(client, server)
444 assert client.transport.closed
445 assert server.transport.closed
446 assert client.connection.state & Endpoint.REMOTE_UNINIT
447 assert server.connection.state & Endpoint.REMOTE_UNINIT
448
449 del server
450 del client
451
452 # now re-try with a client that does not require peer verification
453 self.client_domain.set_peer_authentication(SSLDomain.ANONYMOUS_PEER)
454
455 client = SslTest.SslTestConnection(self.client_domain)
456 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
457
458 client.connection.open()
459 server.connection.open()
460 self._pump(client, server)
461 assert client.ssl.protocol_name() is not None
462 client.connection.close()
463 server.connection.close()
464 self._pump(client, server)
465
466 def test_allow_unsecured_client_which_connects_unsecured(self):
467 """ Server allows an unsecured client to connect if configured.
468 """
469 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
470 self._testpath("server-private-key.pem"),
471 "server-password")
472 self.server_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
473 self.server_domain.set_peer_authentication(SSLDomain.VERIFY_PEER,
474 self._testpath("ca-certificate.pem"))
475 # allow unsecured clients on this connection
476 self.server_domain.allow_unsecured_client()
477 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
478
479 # non-ssl connection
480 client = SslTest.SslTestConnection()
481
482 client.connection.open()
483 server.connection.open()
484 self._pump(client, server)
485 assert server.ssl.protocol_name() is None
486 client.connection.close()
487 server.connection.close()
488 self._pump(client, server)
489
490 def test_allow_unsecured_client_which_connects_secured(self):
491 """ As per test_allow_unsecured_client_which_connects_unsecured
492 but client actually uses SSL
493 """
494 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
495 self._testpath("server-private-key.pem"),
496 "server-password")
497 self.server_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
498 self.server_domain.set_peer_authentication(SSLDomain.VERIFY_PEER,
499 self._testpath("ca-certificate.pem"))
500
501 self.client_domain.set_credentials(self._testpath("client-certificate.pem"),
502 self._testpath("client-private-key.pem"),
503 "client-password")
504 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
505 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER)
506
507 # allow unsecured clients on this connection
508 # self.server_domain.allow_unsecured_client()
509
510 # client uses ssl. Server should detect this.
511 client = SslTest.SslTestConnection(self.client_domain)
512 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
513
514 client.connection.open()
515 server.connection.open()
516 self._pump(client, server)
517 assert server.ssl.protocol_name() is not None
518 client.connection.close()
519 server.connection.close()
520 self._pump(client, server)
521
522 def test_disallow_unsecured_client(self):
523 """ Non-SSL Client is disallowed from connecting to server.
524 """
525 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
526 self._testpath("server-private-key.pem"),
527 "server-password")
528 self.server_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
529 self.server_domain.set_peer_authentication(SSLDomain.ANONYMOUS_PEER)
530 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
531
532 # non-ssl connection
533 client = SslTest.SslTestConnection()
534
535 client.connection.open()
536 server.connection.open()
537 self._pump(client, server)
538 assert client.transport.closed
539 assert server.transport.closed
540 assert client.connection.state & Endpoint.REMOTE_UNINIT
541 assert server.connection.state & Endpoint.REMOTE_UNINIT
542
543 def test_session_resume(self):
544 """ Test resume of client session.
545 """
546 if os.name == "nt":
547 raise Skipped("Windows SChannel session resume not yet implemented.")
548
549 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
550 self._testpath("server-private-key.pem"),
551 "server-password")
552 self.server_domain.set_peer_authentication(SSLDomain.ANONYMOUS_PEER)
553
554 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
555 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER)
556
557 # details will be used in initial and subsequent connections to allow session to be resumed
558 initial_session_details = SSLSessionDetails("my-session-id")
559
560 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
561 client = SslTest.SslTestConnection(self.client_domain, session_details=initial_session_details)
562
563 # bring up the connection and store its state
564 client.connection.open()
565 server.connection.open()
566 self._pump(client, server)
567 assert client.ssl.protocol_name() is not None
568
569 # cleanly shutdown the connection
570 client.connection.close()
571 server.connection.close()
572 self._pump(client, server)
573
574 # destroy the existing clients
575 del client
576 del server
577
578 # now create a new set of connections, use last session id
579 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
580 # provide the details of the last session, allowing it to be resumed
581 client = SslTest.SslTestConnection(self.client_domain, session_details=initial_session_details)
582
583 # client.transport.trace(Transport.TRACE_DRV)
584 # server.transport.trace(Transport.TRACE_DRV)
585
586 client.connection.open()
587 server.connection.open()
588 self._pump(client, server)
589 assert server.ssl.protocol_name() is not None
590 if(API_LANGUAGE == "C"):
591 assert client.ssl.resume_status() == SSL.RESUME_REUSED
592 else:
593 # Java gives no way to check whether a previous session has been resumed
594 pass
595
596 client.connection.close()
597 server.connection.close()
598 self._pump(client, server)
599
600 # now try to resume using an unknown session-id, expect resume to fail
601 # and a new session is negotiated
602
603 del client
604 del server
605
606 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
607 client = SslTest.SslTestConnection(
608 self.client_domain, session_details=SSLSessionDetails("some-other-session-id"))
609
610 client.connection.open()
611 server.connection.open()
612 self._pump(client, server)
613 assert server.ssl.protocol_name() is not None
614 if(API_LANGUAGE == "C"):
615 assert client.ssl.resume_status() == SSL.RESUME_NEW
616
617 client.connection.close()
618 server.connection.close()
619 self._pump(client, server)
620
621 def test_multiple_sessions(self):
622 """ Test multiple simultaneous active SSL sessions with bi-directional
623 certificate verification, shared across two domains.
624 """
625 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
626 self._testpath("server-private-key.pem"),
627 "server-password")
628 self.server_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
629 self.server_domain.set_peer_authentication(SSLDomain.VERIFY_PEER,
630 self._testpath("ca-certificate.pem"))
631
632 self.client_domain.set_credentials(self._testpath("client-certificate.pem"),
633 self._testpath("client-private-key.pem"),
634 "client-password")
635 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
636 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER)
637
638 max_count = 100
639 sessions = [(SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER),
640 SslTest.SslTestConnection(self.client_domain)) for x in
641 range(max_count)]
642 for s in sessions:
643 s[0].connection.open()
644 self._pump(s[0], s[1])
645
646 for s in sessions:
647 s[1].connection.open()
648 self._pump(s[1], s[0])
649 assert s[0].ssl.cipher_name() is not None
650 assert s[1].ssl.cipher_name() == s[0].ssl.cipher_name()
651
652 for s in sessions:
653 s[1].connection.close()
654 self._pump(s[0], s[1])
655
656 for s in sessions:
657 s[0].connection.close()
658 self._pump(s[1], s[0])
659
660 def test_server_hostname_authentication(self):
661 """ Test authentication of the names held in the server's certificate
662 against various configured hostnames.
663 """
664 if os.name == "nt":
665 raise Skipped("PROTON-1057: disable temporarily on Windows.")
666
667 # Check the CommonName matches (case insensitive).
668 # Assumes certificate contains "CN=A1.Good.Server.domain.com"
669 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
670 self._testpath("server-private-key.pem"),
671 "server-password")
672 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
673 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME)
674
675 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
676 client = SslTest.SslTestConnection(self.client_domain)
677
678 client.ssl.peer_hostname = "a1.good.server.domain.com"
679 assert client.ssl.peer_hostname == "a1.good.server.domain.com"
680 self._do_handshake(client, server)
681 del server
682 del client
683 self.tearDown()
684
685 # Should fail on CN name mismatch:
686 self.setUp()
687 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
688 self._testpath("server-private-key.pem"),
689 "server-password")
690 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
691 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME)
692
693 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
694 client = SslTest.SslTestConnection(self.client_domain)
695
696 client.ssl.peer_hostname = "A1.Good.Server.domain.comX"
697 self._do_handshake(client, server)
698 assert client.transport.closed
699 assert server.transport.closed
700 assert client.connection.state & Endpoint.REMOTE_UNINIT
701 assert server.connection.state & Endpoint.REMOTE_UNINIT
702 del server
703 del client
704 self.tearDown()
705
706 # Wildcarded Certificate
707 # Assumes:
708 # 1) certificate contains Server Alternate Names:
709 # "alternate.name.one.com" and "another.name.com"
710 # 2) certificate has wildcarded CommonName "*.prefix*.domain.com"
711 #
712
713 # Pass: match an alternate
714 self.setUp()
715 self.server_domain.set_credentials(self._testpath("server-wc-certificate.pem"),
716 self._testpath("server-wc-private-key.pem"),
717 "server-password")
718 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
719 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME)
720
721 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
722 client = SslTest.SslTestConnection(self.client_domain)
723
724 client.ssl.peer_hostname = "alternate.Name.one.com"
725 self._do_handshake(client, server)
726 del client
727 del server
728 self.tearDown()
729
730 # Pass: match an alternate
731 self.setUp()
732 self.server_domain.set_credentials(self._testpath("server-wc-certificate.pem"),
733 self._testpath("server-wc-private-key.pem"),
734 "server-password")
735 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
736 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME)
737
738 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
739 client = SslTest.SslTestConnection(self.client_domain)
740
741 client.ssl.peer_hostname = "ANOTHER.NAME.COM"
742 self._do_handshake(client, server)
743 del client
744 del server
745 self.tearDown()
746
747 # Pass: match the pattern
748 self.setUp()
749 self.server_domain.set_credentials(self._testpath("server-wc-certificate.pem"),
750 self._testpath("server-wc-private-key.pem"),
751 "server-password")
752 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
753 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME)
754
755 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
756 client = SslTest.SslTestConnection(self.client_domain)
757
758 client.ssl.peer_hostname = "SOME.PREfix.domain.COM"
759 self._do_handshake(client, server)
760 del client
761 del server
762 self.tearDown()
763
764 # Pass: match the pattern
765 self.setUp()
766 self.server_domain.set_credentials(self._testpath("server-wc-certificate.pem"),
767 self._testpath("server-wc-private-key.pem"),
768 "server-password")
769 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
770 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME)
771
772 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
773 client = SslTest.SslTestConnection(self.client_domain)
774
775 client.ssl.peer_hostname = "FOO.PREfixZZZ.domain.com"
776 self._do_handshake(client, server)
777 del client
778 del server
779 self.tearDown()
780
781 # Fail: must match prefix on wildcard
782 self.setUp()
783 self.server_domain.set_credentials(self._testpath("server-wc-certificate.pem"),
784 self._testpath("server-wc-private-key.pem"),
785 "server-password")
786 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
787 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME)
788
789 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
790 client = SslTest.SslTestConnection(self.client_domain)
791
792 client.ssl.peer_hostname = "FOO.PREfi.domain.com"
793 self._do_handshake(client, server)
794 assert client.transport.closed
795 assert server.transport.closed
796 assert client.connection.state & Endpoint.REMOTE_UNINIT
797 assert server.connection.state & Endpoint.REMOTE_UNINIT
798 del server
799 del client
800 self.tearDown()
801
802 # Fail: leading wildcards are not optional
803 self.setUp()
804 self.server_domain.set_credentials(self._testpath("server-wc-certificate.pem"),
805 self._testpath("server-wc-private-key.pem"),
806 "server-password")
807 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
808 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME)
809
810 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
811 client = SslTest.SslTestConnection(self.client_domain)
812
813 client.ssl.peer_hostname = "PREfix.domain.COM"
814 self._do_handshake(client, server)
815 assert client.transport.closed
816 assert server.transport.closed
817 assert client.connection.state & Endpoint.REMOTE_UNINIT
818 assert server.connection.state & Endpoint.REMOTE_UNINIT
819 self.tearDown()
820
821 # Pass: ensure that the user can give an alternate name that overrides
822 # the connection's configured hostname
823 self.setUp()
824 self.server_domain.set_credentials(self._testpath("server-wc-certificate.pem"),
825 self._testpath("server-wc-private-key.pem"),
826 "server-password")
827 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
828 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME)
829
830 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
831 client = SslTest.SslTestConnection(self.client_domain,
832 conn_hostname="This.Name.Does.not.Match",
833 ssl_peername="alternate.name.one.com")
834 self._do_handshake(client, server)
835 del client
836 del server
837 self.tearDown()
838
839 # Pass: ensure that the hostname supplied by the connection is used if
840 # none has been specified for the SSL instance
841 self.setUp()
842 self.server_domain.set_credentials(self._testpath("server-certificate.pem"),
843 self._testpath("server-private-key.pem"),
844 "server-password")
845 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
846 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME)
847
848 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
849 client = SslTest.SslTestConnection(self.client_domain,
850 conn_hostname="a1.good.server.domain.com")
851 self._do_handshake(client, server)
852 del client
853 del server
854 self.tearDown()
855
856 def test_server_hostname_authentication_2(self):
857 """Initially separated from test_server_hostname_authentication
858 above to force Windows checking and sidestep PROTON-1057 exclusion.
859 """
860
861 # Fail for a null peer name.
862 self.server_domain.set_credentials(self._testpath("server-wc-certificate.pem"),
863 self._testpath("server-wc-private-key.pem"),
864 "server-password")
865 self.client_domain.set_trusted_ca_db(self._testpath("ca-certificate.pem"))
866 self.client_domain.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME)
867
868 server = SslTest.SslTestConnection(self.server_domain, mode=Transport.SERVER)
869 client = SslTest.SslTestConnection(self.client_domain)
870
871 # Next line results in an eventual pn_ssl_set_peer_hostname(client.ssl._ssl, None)
872 client.ssl.peer_hostname = None
873 self._do_handshake(client, server)
874 assert client.transport.closed
875 assert server.transport.closed
876 assert client.connection.state & Endpoint.REMOTE_UNINIT
877 assert server.connection.state & Endpoint.REMOTE_UNINIT
878 self.tearDown()
879
880 def test_defaults_messenger_app(self):
881 """ Test an SSL connection using the Messenger apps (no certificates)
882 """
883 if os.name == "nt":
884 raise Skipped("Windows SChannel lacks anonymous cipher support.")
885 port = common.free_tcp_ports()[0]
886
887 receiver = common.MessengerReceiverC()
888 receiver.subscriptions = ["amqps://~0.0.0.0:%s" % port]
889 receiver.receive_count = 1
890 receiver.timeout = self.timeout
891 receiver.start()
892
893 sender = common.MessengerSenderC()
894 sender.targets = ["amqps://0.0.0.0:%s/X" % port]
895 sender.send_count = 1
896 sender.timeout = self.timeout
897 sender.start()
898 sender.wait()
899 assert sender.status() == 0, "Command '%s' failed" % str(sender.cmdline())
900
901 receiver.wait()
902 assert receiver.status() == 0, "Command '%s' failed" % str(receiver.cmdline())
903
904 def test_server_authentication_messenger_app(self):
905 """ Test an SSL authentication using the Messenger apps.
906 """
907 port = common.free_tcp_ports()[0]
908
909 receiver = common.MessengerReceiverC()
910 receiver.subscriptions = ["amqps://~0.0.0.0:%s" % port]
911 receiver.receive_count = 1
912 receiver.timeout = self.timeout
913 # Note hack - by default we use the client-certificate for the
914 # _server_ because the client-certificate's common name field
915 # is "127.0.0.1", which will match the target address used by
916 # the sender.
917 receiver.certificate = self._testpath("client-certificate.pem")
918 receiver.privatekey = self._testpath("client-private-key.pem")
919 receiver.password = "client-password"
920 receiver.start()
921
922 sender = common.MessengerSenderC()
923 sender.targets = ["amqps://127.0.0.1:%s/X" % port]
924 sender.send_count = 1
925 sender.timeout = self.timeout
926 sender.ca_db = self._testpath("ca-certificate.pem")
927 sender.start()
928 sender.wait()
929 assert sender.status() == 0, "Command '%s' failed" % str(sender.cmdline())
930
931 receiver.wait()
932 assert receiver.status() == 0, "Command '%s' failed" % str(receiver.cmdline())
933
934 def test_singleton(self):
935 """Verify that only a single instance of SSL can exist per Transport"""
936 transport = Transport()
937 ssl1 = SSL(transport, self.client_domain)
938 ssl2 = transport.ssl(self.client_domain)
939 ssl3 = transport.ssl(self.client_domain)
940 assert ssl1 is ssl2
941 assert ssl1 is ssl3
942 transport = Transport()
943 ssl1 = transport.ssl(self.client_domain)
944 ssl2 = SSL(transport, self.client_domain)
945 assert ssl1 is ssl2
946 # catch attempt to re-configure existing SSL
947 try:
948 ssl3 = SSL(transport, self.server_domain)
949 assert False, "Expected error did not occur!"
950 except SSLException:
951 pass