xref: /openbsd-src/regress/lib/libssl/tlsfuzzer/tlsfuzzer.py (revision 3cab2bb3f667058bece8e38b12449a63a9d73c4b)
1#   $OpenBSD: tlsfuzzer.py,v 1.13 2020/08/08 10:21:22 tb Exp $
2#
3# Copyright (c) 2020 Theo Buehler <tb@openbsd.org>
4#
5# Permission to use, copy, modify, and distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17import getopt
18import os
19import subprocess
20import sys
21from timeit import default_timer as timer
22
23tlsfuzzer_scriptdir = "/usr/local/share/tlsfuzzer/scripts/"
24
25class Test:
26    """
27    Represents a tlsfuzzer test script.
28        name:           the script's name
29        args:           arguments to feed to the script
30        tls12_args:     override args for a TLSv1.2 server
31        tls13_args:     override args for a TLSv1.3 server
32
33    XXX Add client cert support.
34    """
35    def __init__(self, name="", args=[], tls12_args=[], tls13_args=[]):
36        self.name = name
37        self.tls12_args = args
38        self.tls13_args = args
39        if tls12_args:
40            self.tls12_args = tls12_args
41        if tls13_args:
42            self.tls13_args = tls13_args
43
44    def args(self, has_tls1_3: True):
45        if has_tls1_3:
46            return self.tls13_args
47        else:
48            return self.tls12_args
49
50    def __repr__(self):
51        return "<Test: %s tls12_args: %s tls13_args: %s>" % (
52                self.name, self.tls12_args, tls13_args
53            )
54
55class TestGroup:
56    """ A group of Test objects to be run by TestRunner."""
57    def __init__(self, title="Tests", tests=[]):
58        self.title = title
59        self.tests = tests
60
61    def __iter__(self):
62        return iter(self.tests)
63
64# argument to pass to several tests
65tls13_unsupported_ciphers = [
66    "-e", "TLS 1.3 with ffdhe2048",
67    "-e", "TLS 1.3 with ffdhe3072",
68    "-e", "TLS 1.3 with x448",
69]
70
71tls13_tests = TestGroup("TLSv1.3 tests", [
72    Test("test-tls13-ccs.py"),
73    Test("test-tls13-conversation.py"),
74    Test("test-tls13-count-tickets.py"),
75    Test("test-tls13-empty-alert.py"),
76    Test("test-tls13-finished-plaintext.py"),
77    Test("test-tls13-hrr.py"),
78    Test("test-tls13-keyshare-omitted.py"),
79    Test("test-tls13-legacy-version.py"),
80    Test("test-tls13-nociphers.py"),
81    Test("test-tls13-record-padding.py"),
82    Test("test-tls13-shuffled-extentions.py"),
83
84    # The skipped tests fail due to a bug in BIO_gets() which masks the retry
85    # signalled from an SSL_read() failure. Testing with httpd(8) shows we're
86    # handling these corner cases correctly since tls13_record_layer.c -r1.47.
87    Test("test-tls13-zero-length-data.py", [
88        "-e", "zero-length app data",
89        "-e", "zero-length app data with large padding",
90        "-e", "zero-length app data with padding",
91    ]),
92])
93
94# Tests that take a lot of time (> ~30s on an x280)
95tls13_slow_tests = TestGroup("slow TLSv1.3 tests", [
96    # XXX: Investigate the occasional message
97    # "Got shared secret with 1 most significant bytes equal to zero."
98    Test("test-tls13-dhe-shared-secret-padding.py", tls13_unsupported_ciphers),
99
100    Test("test-tls13-invalid-ciphers.py"),
101    Test("test-tls13-serverhello-random.py", tls13_unsupported_ciphers),
102
103    # Mark two tests cases as xfail for now. The tests expect an arguably
104    # correct decode_error while we send a decrypt_error (like fizz/boring).
105    Test("test-tls13-record-layer-limits.py", [
106        "-x", "max size payload (2**14) of Finished msg, with 16348 bytes of left padding, cipher TLS_AES_128_GCM_SHA256",
107        "-x", "max size payload (2**14) of Finished msg, with 16348 bytes of left padding, cipher TLS_CHACHA20_POLY1305_SHA256",
108    ]),
109])
110
111tls13_extra_cert_tests = TestGroup("TLSv1.3 certificate tests", [
112    # need to set up client certs to run these
113    Test("test-tls13-certificate-request.py"),
114    Test("test-tls13-certificate-verify.py"),
115    Test("test-tls13-ecdsa-in-certificate-verify.py"),
116
117    # Test expects the server to have installed three certificates:
118    # with P-256, P-384 and P-521 curve. Also SHA1+ECDSA is verified
119    # to not work.
120    Test("test-tls13-ecdsa-support.py"),
121])
122
123tls13_failing_tests = TestGroup("failing TLSv1.3 tests", [
124    # Some tests fail because we fail later than the scripts expect us to.
125    # With X25519, we accept weak peer public keys and fail when we actually
126    # compute the keyshare.  Other tests seem to indicate that we could be
127    # stricter about what keyshares we accept.
128    Test("test-tls13-crfg-curves.py"),
129    Test("test-tls13-ecdhe-curves.py"),
130
131    # https://github.com/openssl/openssl/issues/8369
132    Test("test-tls13-obsolete-curves.py"),
133
134    # 3 failing rsa_pss_pss tests
135    Test("test-tls13-rsa-signatures.py"),
136
137    # AssertionError: Unexpected message from peer: ChangeCipherSpec()
138    # Most failing tests expect the CCS right before finished.
139    # What's up with that?
140    Test("test-tls13-version-negotiation.py"),
141])
142
143tls13_slow_failing_tests = TestGroup("slow, failing TLSv1.3 tests", [
144    # Other test failures bugs in keyshare/tlsext negotiation?
145    Test("test-tls13-unrecognised-groups.py"),    # unexpected closure
146
147    # 5 failures:
148    #   'app data split, conversation with KeyUpdate msg'
149    #   'fragmented keyupdate msg'
150    #   'multiple KeyUpdate messages'
151    #   'post-handshake KeyUpdate msg with update_not_request'
152    #   'post-handshake KeyUpdate msg with update_request'
153    Test("test-tls13-keyupdate.py"),
154
155    Test("test-tls13-symetric-ciphers.py"),       # unexpected message from peer
156
157    # 70 fail and 644 pass. For some reason the tests expect a decode_error
158    # but we send a decrypt_error after the CBS_mem_equal() fails in
159    # tls13_server_finished_recv() (which is correct).
160    Test("test-tls13-finished.py"),               # decrypt_error -> decode_error?
161
162    # The following two tests fail Test (skip empty extensions for the first one):
163    # 'empty unassigned extensions, ids in range from 2 to 4118'
164    # 'unassigned extensions with random payload, ids in range from 2 to 1046'
165    Test("test-tls13-large-number-of-extensions.py"), # 2 fail: empty/random payload
166
167    # 6 tests fail: 'rsa_pkcs1_{md5,sha{1,224,256,384,512}} signature'
168    # We send server hello, but the test expects handshake_failure
169    Test("test-tls13-pkcs-signature.py"),
170    # 8 tests fail: 'tls13 signature rsa_pss_{pss,rsae}_sha{256,384,512}
171    Test("test-tls13-rsapss-signatures.py"),
172])
173
174tls13_unsupported_tests = TestGroup("TLSv1.3 tests for unsupported features", [
175    # Tests for features we don't support
176    Test("test-tls13-0rtt-garbage.py"),
177    Test("test-tls13-ffdhe-groups.py"),
178    Test("test-tls13-ffdhe-sanity.py"),
179    Test("test-tls13-psk_dhe_ke.py"),
180    Test("test-tls13-psk_ke.py"),
181
182    # need server to react to HTTP GET for /keyupdate
183    Test("test-tls13-keyupdate-from-server.py"),
184
185    # Weird test: tests servers that don't support 1.3
186    Test("test-tls13-non-support.py"),
187
188    # broken test script
189    # UnboundLocalError: local variable 'cert' referenced before assignment
190    Test("test-tls13-post-handshake-auth.py"),
191
192    # ExpectNewSessionTicket
193    Test("test-tls13-session-resumption.py"),
194
195    # Server must be configured to support only rsa_pss_rsae_sha512
196    Test("test-tls13-signature-algorithms.py"),
197])
198
199tls12_exclude_legacy_protocols = [
200    # all these have BIO_read timeouts against TLSv1.3
201    "-e", "Protocol (3, 0)",
202    "-e", "Protocol (3, 0) in SSLv2 compatible ClientHello",
203    # the following only fail with TLSv1.3
204    "-e", "Protocol (3, 1) in SSLv2 compatible ClientHello",
205    "-e", "Protocol (3, 2) in SSLv2 compatible ClientHello",
206    "-e", "Protocol (3, 3) in SSLv2 compatible ClientHello",
207    "-e", "Protocol (3, 1) with x448 group",
208    "-e", "Protocol (3, 2) with x448 group",
209    "-e", "Protocol (3, 3) with x448 group",
210]
211
212tls12_tests = TestGroup("TLSv1.2 tests", [
213    # Tests that pass as they are.
214    Test("test-TLSv1_2-rejected-without-TLSv1_2.py"),
215    Test("test-aes-gcm-nonces.py"),
216    Test("test-chacha20.py"),
217    Test("test-conversation.py"),
218    Test("test-cve-2016-2107.py"),
219    Test("test-dhe-rsa-key-exchange.py"),
220    Test("test-dhe-rsa-key-exchange-with-bad-messages.py"),
221    Test("test-early-application-data.py"),
222    Test("test-empty-extensions.py"),
223    Test("test-fuzzed-MAC.py"),
224    Test("test-fuzzed-ciphertext.py"),
225    Test("test-fuzzed-finished.py"),
226    Test("test-fuzzed-padding.py"),
227    Test("test-hello-request-by-client.py"),
228    Test("test-invalid-cipher-suites.py"),
229    Test("test-invalid-content-type.py"),
230    Test("test-invalid-session-id.py"),
231    Test("test-invalid-version.py"),
232    Test("test-lucky13.py"),
233    Test("test-message-skipping.py"),
234    Test("test-no-heartbeat.py"),
235    Test("test-sessionID-resumption.py"),
236    Test("test-sslv2-connection.py"),
237    Test("test-truncating-of-finished.py"),
238    Test("test-truncating-of-kRSA-client-key-exchange.py"),
239    Test("test-unsupported-curve-fallback.py"),
240    Test("test-version-numbers.py"),
241    Test("test-zero-length-data.py"),
242
243    # Tests that need tweaking for unsupported features and ciphers.
244    Test(
245        "test-atypical-padding.py", [
246            "-e", "sanity - encrypt then MAC",
247            "-e", "2^14 bytes of AppData with 256 bytes of padding (SHA1 + Encrypt then MAC)",
248        ]
249    ),
250    Test(
251        "test-dhe-rsa-key-exchange-signatures.py", [
252            "-e", "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA sha224 signature",
253            "-e", "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 sha224 signature",
254            "-e", "TLS_DHE_RSA_WITH_AES_128_CBC_SHA sha224 signature",
255            "-e", "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 sha224 signature",
256            "-e", "TLS_DHE_RSA_WITH_AES_256_CBC_SHA sha224 signature",
257        ]
258    ),
259    Test("test-dhe-key-share-random.py", tls12_exclude_legacy_protocols),
260    Test("test-export-ciphers-rejected.py", ["--min-ver", "TLSv1.0"]),
261    Test(
262        "test-downgrade-protection.py",
263        tls12_args = ["--server-max-protocol", "TLSv1.2"],
264        tls13_args = ["--server-max-protocol", "TLSv1.3"],
265    ),
266    Test("test-fallback-scsv.py", tls13_args = ["--tls-1.3"] ),
267    Test("test-serverhello-random.py", args = tls12_exclude_legacy_protocols),
268])
269
270tls12_slow_tests = TestGroup("slow TLSv1.2 tests", [
271    Test("test-cve-2016-7054.py"),
272    Test("test-dhe-no-shared-secret-padding.py", tls12_exclude_legacy_protocols),
273    Test("test-ecdhe-padded-shared-secret.py", tls12_exclude_legacy_protocols),
274    Test("test-ecdhe-rsa-key-share-random.py", tls12_exclude_legacy_protocols),
275    # This test has some failures once in a while.
276    Test("test-fuzzed-plaintext.py"),
277])
278
279tls12_failing_tests = TestGroup("failing TLSv1.2 tests", [
280    # no shared cipher
281    Test("test-aesccm.py"),
282    # need server to set up alpn
283    Test("test-alpn-negotiation.py"),
284    # many tests fail due to unexpected server_name extension
285    Test("test-bleichenbacher-workaround.py"),
286
287    # need client key and cert plus extra server setup
288    Test("test-certificate-malformed.py"),
289    Test("test-certificate-request.py"),
290    Test("test-certificate-verify-malformed-sig.py"),
291    Test("test-certificate-verify-malformed.py"),
292    Test("test-certificate-verify.py"),
293    Test("test-ecdsa-in-certificate-verify.py"),
294    Test("test-renegotiation-disabled-client-cert.py"),
295    Test("test-rsa-pss-sigs-on-certificate-verify.py"),
296    Test("test-rsa-sigs-on-certificate-verify.py"),
297
298    # test doesn't expect session ticket
299    Test("test-client-compatibility.py"),
300    # abrupt closure
301    Test("test-client-hello-max-size.py"),
302    # unknown signature algorithms
303    Test("test-clienthello-md5.py"),
304    # abrupt closure
305    Test("test-cve-2016-6309.py"),
306
307    # Tests expect an illegal_parameter alert
308    Test("test-ecdhe-rsa-key-exchange-with-bad-messages.py"),
309
310    # We send a handshake_failure while the test expects to succeed
311    Test("test-ecdhe-rsa-key-exchange.py"),
312
313    # unsupported?
314    Test("test-extended-master-secret-extension-with-client-cert.py"),
315
316    # no shared cipher
317    Test("test-ecdsa-sig-flexibility.py"),
318
319    # unsupported
320    Test("test-encrypt-then-mac-renegotiation.py"),
321    Test("test-encrypt-then-mac.py"),
322    Test("test-extended-master-secret-extension.py"),
323    Test("test-ffdhe-expected-params.py"),
324    Test("test-ffdhe-negotiation.py"),
325    # unsupported. Expects the server to send the heartbeat extension
326    Test("test-heartbeat.py"),
327
328    # 29 succeed, 263 fail:
329    #   'n extensions', 'n extensions last empty' n in 4086, 4096, 8192, 16383
330    #   'fuzz ext length to n' n in [0..255] with the exception of 41...
331    Test("test-extensions.py"),
332
333    # Tests expect SH but we send unexpected_message or handshake_failure
334    #   'Application data inside Client Hello'
335    #   'Application data inside Client Key Exchange'
336    #   'Application data inside Finished'
337    Test("test-interleaved-application-data-and-fragmented-handshakes-in-renegotiation.py"),
338    # Tests expect SH but we send handshake_failure
339    #   'Application data before Change Cipher Spec'
340    #   'Application data before Client Key Exchange'
341    #   'Application data before Finished'
342    Test("test-interleaved-application-data-in-renegotiation.py"),
343
344    # broken test script
345    # TypeError: '<' not supported between instances of 'int' and 'NoneType'
346    Test("test-invalid-client-hello-w-record-overflow.py"),
347
348    # Lots of failures. abrupt closure
349    Test("test-invalid-client-hello.py"),
350
351    # Test expects illegal_parameter, we send decode_error in ssl_srvr.c:1016
352    # Need to check that this is correct.
353    Test("test-invalid-compression-methods.py"),
354
355    # abrupt closure
356    # 'encrypted premaster set to all zero (n)' n in 256 384 512
357    Test("test-invalid-rsa-key-exchange-messages.py"),
358
359    # test expects illegal_parameter, we send unrecognized_name (which seems
360    # correct according to rfc 6066?)
361    Test("test-invalid-server-name-extension-resumption.py"),
362    # let through some server names without sending an alert
363    # again illegal_parameter vs unrecognized_name
364    Test("test-invalid-server-name-extension.py"),
365
366    Test("test-large-hello.py"),
367
368    # 14 pass
369    # 7 fail
370    # 'n extensions', n in 4095, 4096, 4097, 8191, 8192, 8193, 16383,
371    Test("test-large-number-of-extensions.py"),
372
373    # 4 failures:
374    #   'insecure (legacy) renegotiation with GET after 2nd handshake'
375    #   'insecure (legacy) renegotiation with incomplete GET'
376    #   'secure renegotiation with GET after 2nd handshake'
377    #   'secure renegotiation with incomplete GET'
378    Test("test-legacy-renegotiation.py"),
379
380    # 1 failure (timeout): we don't send the unexpected_message alert
381    # 'duplicate change cipher spec after Finished'
382    Test("test-message-duplication.py"),
383
384    # server should send status_request
385    Test("test-ocsp-stapling.py"),
386
387    # unexpected closure
388    Test("test-openssl-3712.py"),
389
390    # 3 failures:
391    # 'big, needs fragmentation: max fragment - 16336B extension'
392    # 'big, needs fragmentation: max fragment - 32768B extension'
393    # 'maximum size: max fragment - 65531B extension'
394    Test("test-record-layer-fragmentation.py"),
395
396    # wants --reply-AD-size
397    Test("test-record-size-limit.py"),
398
399    # failed: 3 (expect an alert, we send AD)
400    # 'try insecure (legacy) renegotiation with incomplete GET'
401    # 'try secure renegotiation with GET after 2nd CH'
402    # 'try secure renegotiation with incomplete GET'
403    Test("test-renegotiation-disabled.py"),
404
405    # 'resumption of safe session with NULL cipher'
406    # 'resumption with cipher from old CH but not selected by server'
407    Test("test-resumption-with-wrong-ciphers.py"),
408
409    # 5 failures:
410    #   'empty sigalgs'
411    #   'only undefined sigalgs'
412    #   'rsa_pss_pss_sha256 only'
413    #   'rsa_pss_pss_sha384 only'
414    #   'rsa_pss_pss_sha512 only'
415    Test("test-sig-algs.py"),
416
417    # 13 failures:
418    #   'duplicated n non-rsa schemes' for n in 202 2342 8119 23741 32744
419    #   'empty list of signature methods'
420    #   'tolerance n RSA or ECDSA methods' for n in 215 2355 8132 23754
421    #   'tolerance 32758 methods with sig_alg_cert'
422    #   'tolerance max 32744 number of methods with sig_alg_cert'
423    #   'tolerance max (32760) number of methods'
424    Test("test-signature-algorithms.py"),
425
426    # times out
427    Test("test-ssl-death-alert.py"),
428
429    # 17 pass, 13 fail. padding and truncation
430    Test("test-truncating-of-client-hello.py"),
431
432    # x448 tests need disabling plus x25519 corner cases need sorting out
433    Test("test-x25519.py"),
434])
435
436tls12_unsupported_tests = TestGroup("TLSv1.2 for unsupported features", [
437    # protocol_version
438    Test("test-SSLv3-padding.py"),
439    # we don't do RSA key exchanges
440    Test("test-bleichenbacher-timing.py"),
441])
442
443# These tests take a ton of time to fail against an 1.3 server,
444# so don't run them against 1.3 pending further investigation.
445legacy_tests = TestGroup("Legacy protocol tests", [
446    Test("test-sslv2-force-cipher-3des.py"),
447    Test("test-sslv2-force-cipher-non3des.py"),
448    Test("test-sslv2-force-cipher.py"),
449    Test("test-sslv2-force-export-cipher.py"),
450    Test("test-sslv2hello-protocol.py"),
451])
452
453all_groups = [
454    tls13_tests,
455    tls13_slow_tests,
456    tls13_extra_cert_tests,
457    tls13_failing_tests,
458    tls13_slow_failing_tests,
459    tls13_unsupported_tests,
460    tls12_tests,
461    tls12_slow_tests,
462    tls12_failing_tests,
463    tls12_unsupported_tests,
464    legacy_tests,
465]
466
467failing_groups = [
468    tls13_failing_tests,
469    tls13_slow_failing_tests,
470    tls12_failing_tests,
471]
472
473class TestRunner:
474    """ Runs the given tests troups against a server and displays stats. """
475
476    def __init__(
477        self, timing=False, verbose=False, port=4433, use_tls1_3=True,
478        dry_run=False, tests=[], scriptdir=tlsfuzzer_scriptdir,
479    ):
480        self.tests = []
481
482        self.dryrun = dry_run
483        self.use_tls1_3 = use_tls1_3
484        self.port = str(port)
485        self.scriptdir = scriptdir
486
487        self.stats = []
488        self.failed = []
489
490        self.timing = timing
491        self.verbose = verbose
492
493    def add(self, title="tests", tests=[]):
494        # tests.sort(key=lambda test: test.name)
495        self.tests.append(TestGroup(title, tests))
496
497    def add_group(self, group):
498        self.tests.append(group)
499
500    def run_script(self, test):
501        script = test.name
502        args = ["-p"] + [self.port] + test.args(self.use_tls1_3)
503
504        if self.dryrun:
505            if not self.verbose:
506                args = []
507            print(script , end=' ' if args else '')
508            print(' '.join([f"\"{arg}\"" for arg in args]))
509            return
510
511        if self.verbose:
512            print(script)
513        else:
514            print(f"{script[:68]:<72}", end=" ", flush=True)
515        start = timer()
516        test = subprocess.run(
517            ["python3", os.path.join(self.scriptdir, script)] + args,
518            capture_output=not self.verbose,
519            text=True,
520        )
521        end = timer()
522        self.stats.append((script, end - start))
523        if test.returncode == 0:
524            print("OK")
525            return
526        print("FAILED")
527        self.failed.append(script)
528
529        if self.verbose:
530            return
531
532        print('\n'.join(test.stdout.split("Test end\n", 1)[1:]), end="")
533
534    def run(self):
535        for group in self:
536            print(f"Running {group.title} ...")
537            for test in group:
538                self.run_script(test)
539        return not self.failed
540
541    def __iter__(self):
542        return iter(self.tests)
543
544    def __del__(self):
545        if self.timing and self.stats:
546            total = 0.0
547            for (script, time) in self.stats:
548                print(f"{round(time, 2):6.2f} {script}")
549                total += time
550            print(f"{round(total, 2):6.2f} total")
551
552        if self.failed:
553            print("Failed tests:")
554            print('\n'.join(self.failed))
555
556class TlsServer:
557    """ Spawns an s_server listening on localhost:port if necessary. """
558
559    def __init__(self, port=4433):
560        self.spawn = True
561        # Check whether a server is already listening on localhost:port
562        self.spawn = subprocess.run(
563            ["nc", "-c", "-z", "-T", "noverify", "localhost", str(port)],
564            stderr=subprocess.DEVNULL,
565        ).returncode != 0
566
567        if self.spawn:
568            self.server = subprocess.Popen(
569                [
570                    "openssl",
571                    "s_server",
572                    "-accept",
573                    str(port),
574                    "-groups",
575                    "X25519:P-256:P-521:P-384",
576                    "-key",
577                    "localhost.key",
578                    "-cert",
579                    "localhost.crt",
580                    "-www",
581                ],
582                stdout=subprocess.DEVNULL,
583                stderr=subprocess.PIPE,
584                text=True,
585            )
586
587        # Check whether the server talks TLSv1.3
588        self.has_tls1_3 = True or subprocess.run(
589            [
590                "nc",
591                "-c",
592                "-z",
593                "-T",
594                "noverify",
595                "-T",
596                "protocols=TLSv1.3",
597                "localhost",
598                str(port),
599            ],
600            stderr=subprocess.DEVNULL,
601        ).returncode == 0
602
603        self.check()
604
605    def check(self):
606        if self.spawn and self.server.poll() is not None:
607            print(self.server.stderr.read())
608            raise RuntimeError(
609                f"openssl s_server died. Return code: {self.server.returncode}."
610            )
611        if self.spawn:
612            self.server.stderr.detach()
613
614    def __del__(self):
615        if self.spawn:
616            self.server.terminate()
617
618# Extract the arguments we pass to script
619def defaultargs(script, has_tls1_3):
620    return next(
621        (test for group in all_groups for test in group if test.name == script),
622        Test()
623    ).args(has_tls1_3)
624
625def list_or_missing(missing=True):
626    tests = [test.name for group in all_groups for test in group]
627
628    if missing:
629        scripts = {
630            f for f in os.listdir(tlsfuzzer_scriptdir) if f != "__pycache__"
631        }
632        missing = scripts - set(tests)
633        if missing:
634            print('\n'.join(sorted(missing)))
635        exit(0)
636
637    tests.sort()
638    print('\n'.join(tests))
639    exit(0)
640
641def usage():
642    print("Usage: python3 tlsfuzzer.py [-lmnstv] [-p port] [script [test...]]")
643    print(" --help      help")
644    print(" -f          run failing tests")
645    print(" -l          list tests")
646    print(" -m          list new tests after package update")
647    print(" -n          do not run tests, but list the ones that would be run")
648    print(" -p port     connect to this port - defaults to 4433")
649    print(" -s          run slow tests")
650    print(" -t          show timing stats at end")
651    print(" -v          verbose output")
652    exit(0)
653
654def main():
655    failing = False
656    list = False
657    missing = False
658    dryrun = False
659    port = 4433
660    slow = False
661    timing = False
662    verbose = False
663
664    argv = sys.argv[1:]
665    opts, args = getopt.getopt(argv, "flmnp:stv", ["help"])
666    for opt, arg in opts:
667        if opt == '--help':
668            usage()
669        elif opt == '-f':
670            failing = True
671        elif opt == '-l':
672            list = True
673        elif opt == '-m':
674            missing = True
675        elif opt == '-n':
676            dryrun = True
677        elif opt == '-p':
678            port = int(arg)
679        elif opt == '-s':
680            slow = True
681        elif opt == '-t':
682            timing = True
683        elif opt == '-v':
684            verbose = True
685        else:
686            raise ValueError(f"Unknown option: {opt}")
687
688    if not os.path.exists(tlsfuzzer_scriptdir):
689        print("package py3-tlsfuzzer is required for this regress")
690        exit(1)
691
692    if list and failing:
693        failing = [test.name for group in failing_groups for test in group]
694        failing.sort()
695        print('\n'.join(failing))
696        exit(0)
697
698    if list or missing:
699        list_or_missing(missing)
700
701    tls_server = TlsServer(port)
702
703    tests = TestRunner(timing, verbose, port, tls_server.has_tls1_3, dryrun)
704
705    if args:
706        (dir, script) = os.path.split(args[0])
707        if dir and not dir == '.':
708            tests.scriptdir = dir
709
710        testargs = defaultargs(script, tls_server.has_tls1_3)
711
712        tests.verbose = True
713        tests.add("test from command line", [Test(script, testargs + args[1:])])
714
715        exit(not tests.run())
716
717    if failing:
718        if tls_server.has_tls1_3:
719            tests.add_group(tls13_failing_tests)
720            if slow:
721                tests.add_group(tls13_slow_failing_tests)
722        tests.add_group(tls12_failing_tests)
723
724    if tls_server.has_tls1_3:
725        tests.add_group(tls13_tests)
726        if slow:
727            tests.add_group(tls13_slow_tests)
728    else:
729        tests.add_group(legacy_tests)
730
731    tests.add_group(tls12_tests)
732    if slow:
733        tests.add_group(tls12_slow_tests)
734
735    success = tests.run()
736    del tests
737
738    if not success:
739        print("FAILED")
740        exit(1)
741
742if __name__ == "__main__":
743    main()
744