1# $OpenBSD: tlsfuzzer.py,v 1.26 2021/03/27 18:11:53 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 71# test-tls13-finished.py has 70 failing tests that expect a "decode_error" 72# instead of the "decrypt_error" sent by tls13_server_finished_recv(). 73# Both alerts appear to be reasonable in this context, so work around this 74# in the test instead of the library. 75def generate_test_tls13_finished_args(): 76 assertion = "Expected alert description \"decode_error\"" 77 assertion += " does not match received \"decrypt_error\"" 78 paddings = [ 79 ("TLS_AES_128_GCM_SHA256", 0, 1), 80 ("TLS_AES_128_GCM_SHA256", 0, 2), 81 ("TLS_AES_128_GCM_SHA256", 0, 4), 82 ("TLS_AES_128_GCM_SHA256", 0, 8), 83 ("TLS_AES_128_GCM_SHA256", 0, 16), 84 ("TLS_AES_128_GCM_SHA256", 0, 32), 85 ("TLS_AES_128_GCM_SHA256", 0, 48), 86 ("TLS_AES_128_GCM_SHA256", 0, 2**14-4-32), 87 ("TLS_AES_128_GCM_SHA256", 0, 0x20000), 88 ("TLS_AES_128_GCM_SHA256", 0, 0x30000), 89 ("TLS_AES_128_GCM_SHA256", 1, 0), 90 ("TLS_AES_128_GCM_SHA256", 2, 0), 91 ("TLS_AES_128_GCM_SHA256", 4, 0), 92 ("TLS_AES_128_GCM_SHA256", 8, 0), 93 ("TLS_AES_128_GCM_SHA256", 16, 0), 94 ("TLS_AES_128_GCM_SHA256", 32, 0), 95 ("TLS_AES_128_GCM_SHA256", 48, 0), 96 ("TLS_AES_128_GCM_SHA256", 2**14-4-32, 0), 97 ("TLS_AES_128_GCM_SHA256", 12, 0), 98 ("TLS_AES_128_GCM_SHA256", 1, 1), 99 ("TLS_AES_128_GCM_SHA256", 8, 8), 100 ("TLS_AES_256_GCM_SHA384", 0, 1), 101 ("TLS_AES_256_GCM_SHA384", 0, 2), 102 ("TLS_AES_256_GCM_SHA384", 0, 4), 103 ("TLS_AES_256_GCM_SHA384", 0, 8), 104 ("TLS_AES_256_GCM_SHA384", 0, 16), 105 ("TLS_AES_256_GCM_SHA384", 0, 32), 106 ("TLS_AES_256_GCM_SHA384", 0, 48), 107 ("TLS_AES_256_GCM_SHA384", 0, 2**14-4-48), 108 ("TLS_AES_256_GCM_SHA384", 0, 0x20000), 109 ("TLS_AES_256_GCM_SHA384", 0, 0x30000), 110 ("TLS_AES_256_GCM_SHA384", 0, 12), 111 ("TLS_AES_256_GCM_SHA384", 1, 0), 112 ("TLS_AES_256_GCM_SHA384", 2, 0), 113 ("TLS_AES_256_GCM_SHA384", 4, 0), 114 ("TLS_AES_256_GCM_SHA384", 8, 0), 115 ("TLS_AES_256_GCM_SHA384", 16, 0), 116 ("TLS_AES_256_GCM_SHA384", 32, 0), 117 ("TLS_AES_256_GCM_SHA384", 48, 0), 118 ("TLS_AES_256_GCM_SHA384", 2**14-4-48, 0), 119 ("TLS_AES_256_GCM_SHA384", 1, 1), 120 ("TLS_AES_256_GCM_SHA384", 8, 8), 121 ] 122 truncations = [ 123 ("TLS_AES_128_GCM_SHA256", 0, -1), 124 ("TLS_AES_128_GCM_SHA256", 0, -2), 125 ("TLS_AES_128_GCM_SHA256", 0, -4), 126 ("TLS_AES_128_GCM_SHA256", 0, -8), 127 ("TLS_AES_128_GCM_SHA256", 0, -16), 128 ("TLS_AES_128_GCM_SHA256", 0, -32), 129 ("TLS_AES_128_GCM_SHA256", 0, 12), 130 ("TLS_AES_128_GCM_SHA256", 1, None), 131 ("TLS_AES_128_GCM_SHA256", 2, None), 132 ("TLS_AES_128_GCM_SHA256", 4, None), 133 ("TLS_AES_128_GCM_SHA256", 8, None), 134 ("TLS_AES_128_GCM_SHA256", 16, None), 135 ("TLS_AES_128_GCM_SHA256", 32, None), 136 ("TLS_AES_256_GCM_SHA384", 0, -1), 137 ("TLS_AES_256_GCM_SHA384", 0, -2), 138 ("TLS_AES_256_GCM_SHA384", 0, -4), 139 ("TLS_AES_256_GCM_SHA384", 0, -8), 140 ("TLS_AES_256_GCM_SHA384", 0, -16), 141 ("TLS_AES_256_GCM_SHA384", 0, -32), 142 ("TLS_AES_256_GCM_SHA384", 0, 12), 143 ("TLS_AES_256_GCM_SHA384", 1, None), 144 ("TLS_AES_256_GCM_SHA384", 2, None), 145 ("TLS_AES_256_GCM_SHA384", 4, None), 146 ("TLS_AES_256_GCM_SHA384", 8, None), 147 ("TLS_AES_256_GCM_SHA384", 16, None), 148 ("TLS_AES_256_GCM_SHA384", 32, None), 149 ] 150 151 args = [ 152 "-x", "empty - cipher TLS_AES_128_GCM_SHA256", "-X", assertion, 153 "-x", "empty - cipher TLS_AES_256_GCM_SHA384", "-X", assertion, 154 ] 155 padding_fmt = "padding - cipher %s, pad_byte 0, pad_left %d, pad_right %d" 156 for padding in paddings: 157 args += ["-x", padding_fmt % padding, "-X", assertion] 158 truncation_fmt = "truncation - cipher %s, start %d, end %s" 159 for truncation in truncations: 160 args += ["-x", truncation_fmt % truncation, "-X", assertion] 161 return args 162 163tls13_tests = TestGroup("TLSv1.3 tests", [ 164 Test("test-tls13-ccs.py"), 165 Test("test-tls13-conversation.py"), 166 Test("test-tls13-count-tickets.py"), 167 Test("test-tls13-empty-alert.py"), 168 Test("test-tls13-finished.py", generate_test_tls13_finished_args()), 169 Test("test-tls13-finished-plaintext.py"), 170 Test("test-tls13-hrr.py"), 171 Test("test-tls13-keyshare-omitted.py"), 172 Test("test-tls13-legacy-version.py"), 173 Test("test-tls13-nociphers.py"), 174 Test("test-tls13-record-padding.py"), 175 Test("test-tls13-shuffled-extentions.py"), 176 Test("test-tls13-zero-content-type.py"), 177 178 # The skipped tests fail due to a bug in BIO_gets() which masks the retry 179 # signalled from an SSL_read() failure. Testing with httpd(8) shows we're 180 # handling these corner cases correctly since tls13_record_layer.c -r1.47. 181 Test("test-tls13-zero-length-data.py", [ 182 "-e", "zero-length app data", 183 "-e", "zero-length app data with large padding", 184 "-e", "zero-length app data with padding", 185 ]), 186]) 187 188# Tests that take a lot of time (> ~30s on an x280) 189tls13_slow_tests = TestGroup("slow TLSv1.3 tests", [ 190 # XXX: Investigate the occasional message 191 # "Got shared secret with 1 most significant bytes equal to zero." 192 Test("test-tls13-dhe-shared-secret-padding.py", tls13_unsupported_ciphers), 193 194 Test("test-tls13-invalid-ciphers.py"), 195 Test("test-tls13-serverhello-random.py", tls13_unsupported_ciphers), 196 197 # Mark two tests cases as xfail for now. The tests expect an arguably 198 # correct decode_error while we send a decrypt_error (like fizz/boring). 199 Test("test-tls13-record-layer-limits.py", [ 200 "-x", "max size payload (2**14) of Finished msg, with 16348 bytes of left padding, cipher TLS_AES_128_GCM_SHA256", 201 "-x", "max size payload (2**14) of Finished msg, with 16348 bytes of left padding, cipher TLS_CHACHA20_POLY1305_SHA256", 202 ]), 203 # We don't accept an empty ECPF extension since it must advertise the 204 # uncompressed point format. Exclude this extension type from the test. 205 Test( 206 "test-tls13-large-number-of-extensions.py", 207 tls13_args = ["--exc", "11"], 208 ), 209]) 210 211tls13_extra_cert_tests = TestGroup("TLSv1.3 certificate tests", [ 212 # need to set up client certs to run these 213 Test("test-tls13-certificate-request.py"), 214 Test("test-tls13-certificate-verify.py"), 215 Test("test-tls13-ecdsa-in-certificate-verify.py"), 216 217 # Test expects the server to have installed three certificates: 218 # with P-256, P-384 and P-521 curve. Also SHA1+ECDSA is verified 219 # to not work. 220 Test("test-tls13-ecdsa-support.py"), 221]) 222 223tls13_failing_tests = TestGroup("failing TLSv1.3 tests", [ 224 # Some tests fail because we fail later than the scripts expect us to. 225 # With X25519, we accept weak peer public keys and fail when we actually 226 # compute the keyshare. Other tests seem to indicate that we could be 227 # stricter about what keyshares we accept. 228 Test("test-tls13-crfg-curves.py", [ 229 '-e', 'all zero x448 key share', 230 '-e', 'empty x448 key share', 231 '-e', 'sanity x448 with compression ansiX962_compressed_char2', 232 '-e', 'sanity x448 with compression ansiX962_compressed_prime', 233 '-e', 'sanity x448 with compression uncompressed', 234 '-e', 'too big x448 key share', 235 '-e', 'too small x448 key share', 236 '-e', 'x448 key share of "1"', 237 ]), 238 Test("test-tls13-ecdhe-curves.py", [ 239 '-e', 'sanity - x448', 240 '-e', 'x448 - key share from other curve', 241 '-e', 'x448 - point at infinity', 242 '-e', 'x448 - right 0-padded key_share', 243 '-e', 'x448 - right-truncated key_share', 244 ]), 245 246 # The test sends records with protocol version 0x0300 instead of 0x0303 247 # and currently fails with OpenSSL and LibreSSL for this reason. 248 # We have the logic corresponding to NSS's fix for CVE-2020-25648 249 # https://hg.mozilla.org/projects/nss/rev/57bbefa793232586d27cee83e74411171e128361 250 # so should not be affected by this issue. 251 Test("test-tls13-multiple-ccs-messages.py"), 252 253 # https://github.com/openssl/openssl/issues/8369 254 Test("test-tls13-obsolete-curves.py"), 255 256 # 3 failing rsa_pss_pss tests 257 Test("test-tls13-rsa-signatures.py"), 258 259 # AssertionError: Unexpected message from peer: ChangeCipherSpec() 260 # Most failing tests expect the CCS right before finished. 261 # What's up with that? 262 Test("test-tls13-version-negotiation.py"), 263]) 264 265tls13_slow_failing_tests = TestGroup("slow, failing TLSv1.3 tests", [ 266 # Other test failures bugs in keyshare/tlsext negotiation? 267 Test("test-tls13-unrecognised-groups.py"), # unexpected closure 268 269 # 5 failures: 270 # 'app data split, conversation with KeyUpdate msg' 271 # 'fragmented keyupdate msg' 272 # 'multiple KeyUpdate messages' 273 # 'post-handshake KeyUpdate msg with update_not_request' 274 # 'post-handshake KeyUpdate msg with update_request' 275 Test("test-tls13-keyupdate.py"), 276 277 Test("test-tls13-symetric-ciphers.py"), # unexpected message from peer 278 279 # 6 tests fail: 'rsa_pkcs1_{md5,sha{1,224,256,384,512}} signature' 280 # We send server hello, but the test expects handshake_failure 281 Test("test-tls13-pkcs-signature.py"), 282 # 8 tests fail: 'tls13 signature rsa_pss_{pss,rsae}_sha{256,384,512} 283 Test("test-tls13-rsapss-signatures.py"), 284]) 285 286tls13_unsupported_tests = TestGroup("TLSv1.3 tests for unsupported features", [ 287 # Tests for features we don't support 288 Test("test-tls13-0rtt-garbage.py"), 289 Test("test-tls13-ffdhe-groups.py"), 290 Test("test-tls13-ffdhe-sanity.py"), 291 Test("test-tls13-psk_dhe_ke.py"), 292 Test("test-tls13-psk_ke.py"), 293 294 # need server to react to HTTP GET for /keyupdate 295 Test("test-tls13-keyupdate-from-server.py"), 296 297 # Weird test: tests servers that don't support 1.3 298 Test("test-tls13-non-support.py"), 299 300 # broken test script 301 # UnboundLocalError: local variable 'cert' referenced before assignment 302 Test("test-tls13-post-handshake-auth.py"), 303 304 # ExpectNewSessionTicket 305 Test("test-tls13-session-resumption.py"), 306 307 # Server must be configured to support only rsa_pss_rsae_sha512 308 Test("test-tls13-signature-algorithms.py"), 309]) 310 311tls12_exclude_legacy_protocols = [ 312 # all these have BIO_read timeouts against TLSv1.3 313 "-e", "Protocol (3, 0)", 314 "-e", "Protocol (3, 0) in SSLv2 compatible ClientHello", 315 # the following only fail with TLSv1.3 316 "-e", "Protocol (3, 1) in SSLv2 compatible ClientHello", 317 "-e", "Protocol (3, 2) in SSLv2 compatible ClientHello", 318 "-e", "Protocol (3, 3) in SSLv2 compatible ClientHello", 319 "-e", "Protocol (3, 1) with x448 group", 320 "-e", "Protocol (3, 2) with x448 group", 321 "-e", "Protocol (3, 3) with x448 group", 322] 323 324tls12_tests = TestGroup("TLSv1.2 tests", [ 325 # Tests that pass as they are. 326 Test("test-TLSv1_2-rejected-without-TLSv1_2.py"), 327 Test("test-aes-gcm-nonces.py"), 328 Test("test-chacha20.py"), 329 Test("test-conversation.py"), 330 Test("test-cve-2016-2107.py"), 331 Test("test-dhe-rsa-key-exchange.py"), 332 Test("test-dhe-rsa-key-exchange-with-bad-messages.py"), 333 Test("test-early-application-data.py"), 334 Test("test-empty-extensions.py"), 335 Test("test-fuzzed-MAC.py"), 336 Test("test-fuzzed-ciphertext.py"), 337 Test("test-fuzzed-finished.py"), 338 Test("test-fuzzed-padding.py"), 339 Test("test-hello-request-by-client.py"), 340 Test("test-invalid-cipher-suites.py"), 341 Test("test-invalid-content-type.py"), 342 Test("test-invalid-session-id.py"), 343 Test("test-invalid-version.py"), 344 Test("test-lucky13.py"), 345 Test("test-message-skipping.py"), 346 Test("test-no-heartbeat.py"), 347 Test("test-sessionID-resumption.py"), 348 Test("test-sslv2-connection.py"), 349 Test("test-truncating-of-finished.py"), 350 Test("test-truncating-of-kRSA-client-key-exchange.py"), 351 Test("test-unsupported-curve-fallback.py"), 352 Test("test-version-numbers.py"), 353 Test("test-zero-length-data.py"), 354 355 # Tests that need tweaking for unsupported features and ciphers. 356 Test( 357 "test-atypical-padding.py", [ 358 "-e", "sanity - encrypt then MAC", 359 "-e", "2^14 bytes of AppData with 256 bytes of padding (SHA1 + Encrypt then MAC)", 360 ] 361 ), 362 Test( 363 "test-dhe-rsa-key-exchange-signatures.py", [ 364 "-e", "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA sha224 signature", 365 "-e", "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 sha224 signature", 366 "-e", "TLS_DHE_RSA_WITH_AES_128_CBC_SHA sha224 signature", 367 "-e", "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 sha224 signature", 368 "-e", "TLS_DHE_RSA_WITH_AES_256_CBC_SHA sha224 signature", 369 ] 370 ), 371 Test("test-dhe-key-share-random.py", tls12_exclude_legacy_protocols), 372 Test("test-export-ciphers-rejected.py", ["--min-ver", "TLSv1.0"]), 373 Test( 374 "test-downgrade-protection.py", 375 tls12_args = ["--server-max-protocol", "TLSv1.2"], 376 tls13_args = ["--server-max-protocol", "TLSv1.3"], 377 ), 378 Test("test-fallback-scsv.py", tls13_args = ["--tls-1.3"] ), 379 380 # Without --sig-algs-drop-ok, two tests fail since we do not currently 381 # implement the signature_algorithms_cert extension (although we MUST). 382 Test("test-sig-algs-renegotiation-resumption.py", ["--sig-algs-drop-ok"]), 383 384 Test("test-serverhello-random.py", args = tls12_exclude_legacy_protocols), 385]) 386 387tls12_slow_tests = TestGroup("slow TLSv1.2 tests", [ 388 Test("test-cve-2016-7054.py"), 389 Test("test-dhe-no-shared-secret-padding.py", tls12_exclude_legacy_protocols), 390 Test("test-ecdhe-padded-shared-secret.py", tls12_exclude_legacy_protocols), 391 Test("test-ecdhe-rsa-key-share-random.py", tls12_exclude_legacy_protocols), 392 # This test has some failures once in a while. 393 Test("test-fuzzed-plaintext.py"), 394]) 395 396tls12_failing_tests = TestGroup("failing TLSv1.2 tests", [ 397 # no shared cipher 398 Test("test-aesccm.py"), 399 # need server to set up alpn 400 Test("test-alpn-negotiation.py"), 401 # many tests fail due to unexpected server_name extension 402 Test("test-bleichenbacher-workaround.py"), 403 404 # need client key and cert plus extra server setup 405 Test("test-certificate-malformed.py"), 406 Test("test-certificate-request.py"), 407 Test("test-certificate-verify-malformed-sig.py"), 408 Test("test-certificate-verify-malformed.py"), 409 Test("test-certificate-verify.py"), 410 Test("test-ecdsa-in-certificate-verify.py"), 411 Test("test-renegotiation-disabled-client-cert.py"), 412 Test("test-rsa-pss-sigs-on-certificate-verify.py"), 413 Test("test-rsa-sigs-on-certificate-verify.py"), 414 415 # test doesn't expect session ticket 416 Test("test-client-compatibility.py"), 417 # abrupt closure 418 Test("test-client-hello-max-size.py"), 419 # unknown signature algorithms 420 Test("test-clienthello-md5.py"), 421 # abrupt closure 422 Test("test-cve-2016-6309.py"), 423 424 # Tests expect an illegal_parameter alert 425 Test("test-ecdhe-rsa-key-exchange-with-bad-messages.py"), 426 427 # We send a handshake_failure while the test expects to succeed 428 Test("test-ecdhe-rsa-key-exchange.py"), 429 430 # unsupported? 431 Test("test-extended-master-secret-extension-with-client-cert.py"), 432 433 # no shared cipher 434 Test("test-ecdsa-sig-flexibility.py"), 435 436 # unsupported 437 Test("test-encrypt-then-mac-renegotiation.py"), 438 Test("test-encrypt-then-mac.py"), 439 Test("test-extended-master-secret-extension.py"), 440 Test("test-ffdhe-expected-params.py"), 441 Test("test-ffdhe-negotiation.py"), 442 # unsupported. Expects the server to send the heartbeat extension 443 Test("test-heartbeat.py"), 444 445 # 29 succeed, 263 fail: 446 # 'n extensions', 'n extensions last empty' n in 4086, 4096, 8192, 16383 447 # 'fuzz ext length to n' n in [0..255] with the exception of 41... 448 Test("test-extensions.py"), 449 450 # Tests expect SH but we send unexpected_message or handshake_failure 451 # 'Application data inside Client Hello' 452 # 'Application data inside Client Key Exchange' 453 # 'Application data inside Finished' 454 Test("test-interleaved-application-data-and-fragmented-handshakes-in-renegotiation.py"), 455 # Tests expect SH but we send handshake_failure 456 # 'Application data before Change Cipher Spec' 457 # 'Application data before Client Key Exchange' 458 # 'Application data before Finished' 459 Test("test-interleaved-application-data-in-renegotiation.py"), 460 461 # broken test script 462 # TypeError: '<' not supported between instances of 'int' and 'NoneType' 463 Test("test-invalid-client-hello-w-record-overflow.py"), 464 465 # Lots of failures. abrupt closure 466 Test("test-invalid-client-hello.py"), 467 468 # Test expects illegal_parameter, we send decode_error in ssl_srvr.c:1016 469 # Need to check that this is correct. 470 Test("test-invalid-compression-methods.py"), 471 472 # abrupt closure 473 # 'encrypted premaster set to all zero (n)' n in 256 384 512 474 Test("test-invalid-rsa-key-exchange-messages.py"), 475 476 # test expects illegal_parameter, we send unrecognized_name (which seems 477 # correct according to rfc 6066?) 478 Test("test-invalid-server-name-extension-resumption.py"), 479 # let through some server names without sending an alert 480 # again illegal_parameter vs unrecognized_name 481 Test("test-invalid-server-name-extension.py"), 482 483 Test("test-large-hello.py"), 484 485 # 14 pass 486 # 7 fail 487 # 'n extensions', n in 4095, 4096, 4097, 8191, 8192, 8193, 16383, 488 Test("test-large-number-of-extensions.py"), 489 490 # 4 failures: 491 # 'insecure (legacy) renegotiation with GET after 2nd handshake' 492 # 'insecure (legacy) renegotiation with incomplete GET' 493 # 'secure renegotiation with GET after 2nd handshake' 494 # 'secure renegotiation with incomplete GET' 495 Test("test-legacy-renegotiation.py"), 496 497 # 1 failure (timeout): we don't send the unexpected_message alert 498 # 'duplicate change cipher spec after Finished' 499 Test("test-message-duplication.py"), 500 501 # server should send status_request 502 Test("test-ocsp-stapling.py"), 503 504 # unexpected closure 505 Test("test-openssl-3712.py"), 506 507 # 3 failures: 508 # 'big, needs fragmentation: max fragment - 16336B extension' 509 # 'big, needs fragmentation: max fragment - 32768B extension' 510 # 'maximum size: max fragment - 65531B extension' 511 Test("test-record-layer-fragmentation.py"), 512 513 # wants --reply-AD-size 514 Test("test-record-size-limit.py"), 515 516 # failed: 3 (expect an alert, we send AD) 517 # 'try insecure (legacy) renegotiation with incomplete GET' 518 # 'try secure renegotiation with GET after 2nd CH' 519 # 'try secure renegotiation with incomplete GET' 520 Test("test-renegotiation-disabled.py"), 521 522 # 'resumption of safe session with NULL cipher' 523 # 'resumption with cipher from old CH but not selected by server' 524 Test("test-resumption-with-wrong-ciphers.py"), 525 526 # 5 failures: 527 # 'empty sigalgs' 528 # 'only undefined sigalgs' 529 # 'rsa_pss_pss_sha256 only' 530 # 'rsa_pss_pss_sha384 only' 531 # 'rsa_pss_pss_sha512 only' 532 Test("test-sig-algs.py"), 533 534 # 13 failures: 535 # 'duplicated n non-rsa schemes' for n in 202 2342 8119 23741 32744 536 # 'empty list of signature methods' 537 # 'tolerance n RSA or ECDSA methods' for n in 215 2355 8132 23754 538 # 'tolerance 32758 methods with sig_alg_cert' 539 # 'tolerance max 32744 number of methods with sig_alg_cert' 540 # 'tolerance max (32760) number of methods' 541 Test("test-signature-algorithms.py"), 542 543 # times out 544 Test("test-ssl-death-alert.py"), 545 546 # 17 pass, 13 fail. padding and truncation 547 Test("test-truncating-of-client-hello.py"), 548 549 # x448 tests need disabling plus x25519 corner cases need sorting out 550 Test("test-x25519.py"), 551]) 552 553tls12_unsupported_tests = TestGroup("TLSv1.2 for unsupported features", [ 554 # protocol_version 555 Test("test-SSLv3-padding.py"), 556 # we don't do RSA key exchanges 557 Test("test-bleichenbacher-timing.py"), 558]) 559 560# These tests take a ton of time to fail against an 1.3 server, 561# so don't run them against 1.3 pending further investigation. 562legacy_tests = TestGroup("Legacy protocol tests", [ 563 Test("test-sslv2-force-cipher-3des.py"), 564 Test("test-sslv2-force-cipher-non3des.py"), 565 Test("test-sslv2-force-cipher.py"), 566 Test("test-sslv2-force-export-cipher.py"), 567 Test("test-sslv2hello-protocol.py"), 568]) 569 570all_groups = [ 571 tls13_tests, 572 tls13_slow_tests, 573 tls13_extra_cert_tests, 574 tls13_failing_tests, 575 tls13_slow_failing_tests, 576 tls13_unsupported_tests, 577 tls12_tests, 578 tls12_slow_tests, 579 tls12_failing_tests, 580 tls12_unsupported_tests, 581 legacy_tests, 582] 583 584failing_groups = [ 585 tls13_failing_tests, 586 tls13_slow_failing_tests, 587 tls12_failing_tests, 588] 589 590class TestRunner: 591 """ Runs the given tests troups against a server and displays stats. """ 592 593 def __init__( 594 self, timing=False, verbose=False, port=4433, use_tls1_3=True, 595 dry_run=False, tests=[], scriptdir=tlsfuzzer_scriptdir, 596 ): 597 self.tests = [] 598 599 self.dryrun = dry_run 600 self.use_tls1_3 = use_tls1_3 601 self.port = str(port) 602 self.scriptdir = scriptdir 603 604 self.stats = [] 605 self.failed = [] 606 self.missing = [] 607 608 self.timing = timing 609 self.verbose = verbose 610 611 def add(self, title="tests", tests=[]): 612 # tests.sort(key=lambda test: test.name) 613 self.tests.append(TestGroup(title, tests)) 614 615 def add_group(self, group): 616 self.tests.append(group) 617 618 def run_script(self, test): 619 script = test.name 620 args = ["-p"] + [self.port] + test.args(self.use_tls1_3) 621 622 if self.dryrun: 623 if not self.verbose: 624 args = [] 625 print(script , end=' ' if args else '') 626 print(' '.join([f"\"{arg}\"" for arg in args])) 627 return 628 629 if self.verbose: 630 print(script) 631 else: 632 print(f"{script[:68]:<72}", end=" ", flush=True) 633 start = timer() 634 scriptpath = os.path.join(self.scriptdir, script) 635 if not os.path.exists(scriptpath): 636 self.missing.append(script) 637 print("MISSING") 638 return 639 test = subprocess.run( 640 ["python3", scriptpath] + args, 641 capture_output=not self.verbose, 642 text=True, 643 ) 644 end = timer() 645 self.stats.append((script, end - start)) 646 if test.returncode == 0: 647 print("OK") 648 return 649 print("FAILED") 650 self.failed.append(script) 651 652 if self.verbose: 653 return 654 655 print('\n'.join(test.stdout.split("Test end\n", 1)[1:]), end="") 656 657 def run(self): 658 for group in self: 659 print(f"Running {group.title} ...") 660 for test in group: 661 self.run_script(test) 662 return not self.failed 663 664 def __iter__(self): 665 return iter(self.tests) 666 667 def __del__(self): 668 if self.timing and self.stats: 669 total = 0.0 670 for (script, time) in self.stats: 671 print(f"{round(time, 2):6.2f} {script}") 672 total += time 673 print(f"{round(total, 2):6.2f} total") 674 675 if self.failed: 676 print("Failed tests:") 677 print('\n'.join(self.failed)) 678 679 if self.missing: 680 print("Missing tests (outdated package?):") 681 print('\n'.join(self.missing)) 682 683class TlsServer: 684 """ Spawns an s_server listening on localhost:port if necessary. """ 685 686 def __init__(self, port=4433): 687 self.spawn = True 688 # Check whether a server is already listening on localhost:port 689 self.spawn = subprocess.run( 690 ["nc", "-c", "-z", "-T", "noverify", "localhost", str(port)], 691 stderr=subprocess.DEVNULL, 692 ).returncode != 0 693 694 if self.spawn: 695 self.server = subprocess.Popen( 696 [ 697 "openssl", 698 "s_server", 699 "-accept", 700 str(port), 701 "-groups", 702 "X25519:P-256:P-521:P-384", 703 "-key", 704 "localhost.key", 705 "-cert", 706 "localhost.crt", 707 "-www", 708 ], 709 stdout=subprocess.DEVNULL, 710 stderr=subprocess.PIPE, 711 text=True, 712 ) 713 714 # Check whether the server talks TLSv1.3 715 self.has_tls1_3 = True or subprocess.run( 716 [ 717 "nc", 718 "-c", 719 "-z", 720 "-T", 721 "noverify", 722 "-T", 723 "protocols=TLSv1.3", 724 "localhost", 725 str(port), 726 ], 727 stderr=subprocess.DEVNULL, 728 ).returncode == 0 729 730 self.check() 731 732 def check(self): 733 if self.spawn and self.server.poll() is not None: 734 print(self.server.stderr.read()) 735 raise RuntimeError( 736 f"openssl s_server died. Return code: {self.server.returncode}." 737 ) 738 if self.spawn: 739 self.server.stderr.detach() 740 741 def __del__(self): 742 if self.spawn: 743 self.server.terminate() 744 745# Extract the arguments we pass to script 746def defaultargs(script, has_tls1_3): 747 return next( 748 (test for group in all_groups for test in group if test.name == script), 749 Test() 750 ).args(has_tls1_3) 751 752def list_or_missing(missing=True): 753 tests = [test.name for group in all_groups for test in group] 754 755 if missing: 756 scripts = { 757 f for f in os.listdir(tlsfuzzer_scriptdir) if f != "__pycache__" 758 } 759 missing = scripts - set(tests) 760 if missing: 761 print('\n'.join(sorted(missing))) 762 exit(0) 763 764 tests.sort() 765 print('\n'.join(tests)) 766 exit(0) 767 768def usage(): 769 print("Usage: python3 tlsfuzzer.py [-lmnstv] [-p port] [script [test...]]") 770 print(" --help help") 771 print(" -f run failing tests") 772 print(" -l list tests") 773 print(" -m list new tests after package update") 774 print(" -n do not run tests, but list the ones that would be run") 775 print(" -p port connect to this port - defaults to 4433") 776 print(" -s run slow tests") 777 print(" -t show timing stats at end") 778 print(" -v verbose output") 779 exit(0) 780 781def main(): 782 failing = False 783 list = False 784 missing = False 785 dryrun = False 786 port = 4433 787 slow = False 788 timing = False 789 verbose = False 790 791 argv = sys.argv[1:] 792 opts, args = getopt.getopt(argv, "flmnp:stv", ["help"]) 793 for opt, arg in opts: 794 if opt == '--help': 795 usage() 796 elif opt == '-f': 797 failing = True 798 elif opt == '-l': 799 list = True 800 elif opt == '-m': 801 missing = True 802 elif opt == '-n': 803 dryrun = True 804 elif opt == '-p': 805 port = int(arg) 806 elif opt == '-s': 807 slow = True 808 elif opt == '-t': 809 timing = True 810 elif opt == '-v': 811 verbose = True 812 else: 813 raise ValueError(f"Unknown option: {opt}") 814 815 if not os.path.exists(tlsfuzzer_scriptdir): 816 print("package py3-tlsfuzzer is required for this regress") 817 exit(1) 818 819 if list and failing: 820 failing = [test.name for group in failing_groups for test in group] 821 failing.sort() 822 print('\n'.join(failing)) 823 exit(0) 824 825 if list or missing: 826 list_or_missing(missing) 827 828 tls_server = TlsServer(port) 829 830 tests = TestRunner(timing, verbose, port, tls_server.has_tls1_3, dryrun) 831 832 if args: 833 (dir, script) = os.path.split(args[0]) 834 if dir and not dir == '.': 835 tests.scriptdir = dir 836 837 testargs = defaultargs(script, tls_server.has_tls1_3) 838 839 tests.verbose = True 840 tests.add("test from command line", [Test(script, testargs + args[1:])]) 841 842 exit(not tests.run()) 843 844 if failing: 845 if tls_server.has_tls1_3: 846 tests.add_group(tls13_failing_tests) 847 if slow: 848 tests.add_group(tls13_slow_failing_tests) 849 tests.add_group(tls12_failing_tests) 850 851 if tls_server.has_tls1_3: 852 tests.add_group(tls13_tests) 853 if slow: 854 tests.add_group(tls13_slow_tests) 855 else: 856 tests.add_group(legacy_tests) 857 858 tests.add_group(tls12_tests) 859 if slow: 860 tests.add_group(tls12_slow_tests) 861 862 success = tests.run() 863 del tests 864 865 if not success: 866 print("FAILED") 867 exit(1) 868 869if __name__ == "__main__": 870 main() 871