1414
1515from __future__ import print_function
1616
17- import sys, os, re, time, multiprocessing, subprocess
17+ import sys, os, re, time, subprocess
18+ from scapy.modules.six.moves.queue import Queue
19+ import threading
1820
1921from ast import literal_eval
2022import os
@@ -58,7 +60,7 @@ def check_output_for_data(out, err, expected_data):
5860    else:
5961        return (False, None)
6062
61- def run_tls_test_server(expected_data, q, curve=None, cookie=False):
63+ def run_tls_test_server(expected_data, q, curve=None, cookie=False, client_auth=False ):
6264    correct = False
6365    print("Server started !")
6466    with captured_output() as (out, err):
@@ -76,6 +78,7 @@ def run_tls_test_server(expected_data, q, curve=None, cookie=False):
7678                               mykey=mykey,
7779                               curve=curve,
7880                               cookie=cookie,
81+                                client_auth=client_auth,
7982                               debug=5)
8083        # Sync threads
8184        q.put(True)
@@ -86,11 +89,12 @@ def run_tls_test_server(expected_data, q, curve=None, cookie=False):
8689    # Return data
8790    q.put(res)
8891
89- def test_tls_server(suite="", version=""):
92+ def test_tls_server(suite="", version="", tls13=False, client_auth=False ):
9093    msg = ("TestS_%s_data" % suite).encode()
9194    # Run server
92-     q_ = multiprocessing.Manager().Queue()
93-     th_ = multiprocessing.Process(target=run_tls_test_server, args=(msg, q_))
95+     q_ = Queue()
96+     th_ = threading.Thread(target=run_tls_test_server, args=(msg, q_, None, False, client_auth))
97+     th_.setDaemon(True)
9498    th_.start()
9599    # Synchronise threads
96100    q_.get()
@@ -100,14 +104,13 @@ def test_tls_server(suite="", version=""):
100104    filename = os.getenv("SCAPY_ROOT_DIR")+filename if not os.path.exists(filename) else filename
101105    CA_f = os.path.abspath(filename)
102106    p = subprocess.Popen(
103-         ["openssl", "s_client", "-connect", "127.0.0.1:4433", "-debug", "-cipher", suite, version, "-CAfile", CA_f],
107+         ["openssl", "s_client", "-connect", "127.0.0.1:4433", "-debug", "-ciphersuites" if tls13 else "- cipher", suite, version, "-CAfile", CA_f],
104108        stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
105109    )
106110    msg += b"\nstop_server\n"
107111    out = p.communicate(input=msg)[0]
108112    print(out.decode())
109113    if p.returncode != 0:
110-         th_.terminate()
111114        raise RuntimeError("OpenSSL returned with error code")
112115    else:
113116        p = re.compile(br'verify return:(\d+)')
@@ -120,38 +123,46 @@ def test_tls_server(suite="", version=""):
120123            else:
121124                _one_success = True
122125        if _failed or not _one_success:
123-             th_.terminate()
124126            raise RuntimeError("OpenSSL returned unexpected values")
125127    # Wait for server
126128    th_.join(5)
127129    if th_.is_alive():
128-         th_.terminate()
129130        raise RuntimeError("Test timed out")
130131    # Analyse values
131132    if q_.empty():
132133        raise RuntimeError("Missing return values")
133134    print(q_.get())
134135
135136
136- #= Testing TLS server with TLS 1.0 and TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
137- #~ open_ssl_client
137+ = Testing TLS server with TLS 1.0 and TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
138+ ~ open_ssl_client
139+ 
140+ test_tls_server("ECDHE-RSA-AES128-SHA", "-tls1")
141+ 
142+ = Testing TLS server with TLS 1.1 and TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
143+ ~ open_ssl_client
138144
139- # test_tls_server("ECDHE-RSA-AES128-SHA", "-tls1 ")
145+ test_tls_server("ECDHE-RSA-AES128-SHA", "-tls1_1 ")
140146
141- # = Testing TLS server with TLS 1.1  and TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA 
142- # ~ open_ssl_client
147+ = Testing TLS server with TLS 1.2  and TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 
148+ ~ open_ssl_client
143149
144- # test_tls_server("ECDHE -RSA-AES128-SHA ", "-tls1_1 ")
150+ test_tls_server("DHE -RSA-AES128-SHA256 ", "-tls1_2 ")
145151
146- # = Testing TLS server with TLS 1.2 and TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 
147- # ~ open_ssl_client
152+ = Testing TLS server with TLS 1.2 and TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 
153+ ~ open_ssl_client
148154
149- # test_tls_server("DHE -RSA-AES128-SHA256 ", "-tls1_2")
155+ test_tls_server("ECDHE -RSA-AES256-GCM-SHA384 ", "-tls1_2")
150156
151- # = Testing TLS server with TLS 1.2  and TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 
152- # ~ open_ssl_client
157+ = Testing TLS server with TLS 1.3  and TLS_AES_256_GCM_SHA384 
158+ ~ open_ssl_client
153159
154- #test_tls_server("ECDHE-RSA-AES256-GCM-SHA384", "-tls1_2")
160+ test_tls_server("TLS_AES_256_GCM_SHA384", "-tls1_3", tls13=True)
161+ 
162+ = Testing TLS server with TLS 1.3 and TLS_AES_256_GCM_SHA384 and client auth
163+ ~ open_ssl_client
164+ 
165+ test_tls_server("TLS_AES_256_GCM_SHA384", "-tls1_3", tls13=True, client_auth=True)
155166
156167+ TLS client automaton tests
157168
@@ -179,12 +190,12 @@ def run_tls_test_client(send_data=None, cipher_suite_code=None, version=None):
179190    print("Running client...")
180191    t.run()
181192
182- def test_tls_client(suite, version):
193+ def test_tls_client(suite, version, curve=None, cookie=False, client_auth=False ):
183194    msg = ("TestC_%s_data" % suite).encode()
184195    # Run server
185196    q_ = Queue()
186197    print("Starting server...")
187-     th_ = threading.Thread(target=run_tls_test_server, args=(msg, q_))
198+     th_ = threading.Thread(target=run_tls_test_server, args=(msg, q_, curve, cookie, client_auth ))
188199    th_.setDaemon(True)
189200    th_.start()
190201    # Synchronise threads
@@ -204,42 +215,6 @@ def test_tls_client(suite, version):
204215        raise RuntimeError("Missing return value")
205216    return q_.get(timeout=5)
206217
207- 
208- def test_tls13_client(suite, retry=False):
209-     msg = ("TestC_%s_data" % suite).encode()
210-     # Run server
211-     q_ = Queue()
212-     print("Starting server...")
213-     if retry:
214-         # Run a server that support only secp256r1 and use cookie mechanism.
215-         # It will send a HelloRetryRequest in response to a ClientHello
216-         # with x25519 as default group.
217-         curve = "secp256r1"
218-         cookie = True
219-         th_ = threading.Thread(target=run_tls_test_server, args=(msg, q_, curve, cookie))
220-     else:
221-         th_ = threading.Thread(target=run_tls_test_server, args=(msg, q_))
222-     th_.setDaemon(True)
223-     th_.start()
224-     # Synchronise threads
225-     print("Syncrhonising...")
226-     assert q_.get(timeout=5) is True
227-     time.sleep(1)
228-     print("Thread synchronised")
229-     # Run client
230-     run_tls_test_client(msg, suite, "0304")
231-     # Wait for server
232-     print("Client running, waiting...")
233-     th_.join(5)
234-     if th_.is_alive():
235-         raise RuntimeError("Test timed out")
236-     # Return values
237-     if q_.empty():
238-         raise RuntimeError("Missing return value")
239-     return q_.get(timeout=5)
240- 
241- 
242- 
243218= Testing TLS server and client with SSLv2 and SSL_CK_DES_192_EDE3_CBC_WITH_MD5
244219
245220test_tls_client("0700c0", "0002")
@@ -281,4 +256,9 @@ test_tls_client("1305", "0304")
281256= Testing TLS server and client with TLS 1.3 and a retry
282257~ crypto_advanced
283258
284- test_tls13_client("1302", retry=True)
259+ test_tls_client("1302", "0304", curve="secp256r1", cookie=True)
260+ 
261+ = Testing TLS server and client with TLS 1.3 and TLS_AES_128_CCM_8_SHA256 and client auth
262+ ~ crypto_advanced
263+ 
264+ test_tls_client("1305", "0304", client_auth=True)
0 commit comments