|
| 1 | +from nassl.ssl_client import ClientCertificateRequested |
| 2 | + |
| 3 | +from sslyze.plugins.ems_extension_plugin import ( |
| 4 | + EmsExtensionImplementation, |
| 5 | + EmsExtensionScanResult, |
| 6 | + EmsExtensionScanResultAsJson, |
| 7 | +) |
| 8 | + |
| 9 | +from sslyze.server_setting import ( |
| 10 | + ServerNetworkLocation, |
| 11 | + ServerNetworkConfiguration, |
| 12 | + ClientAuthenticationCredentials, |
| 13 | +) |
| 14 | +from tests.connectivity_utils import check_connectivity_to_server_and_return_info |
| 15 | +from tests.markers import can_only_run_on_linux_64 |
| 16 | +from tests.openssl_server import LegacyOpenSslServer, ClientAuthConfigEnum |
| 17 | +import pytest |
| 18 | + |
| 19 | + |
| 20 | +class TestFallbackScsvPlugin: |
| 21 | + def test_good(self) -> None: |
| 22 | + # Given a server that supports Extended Master Secret |
| 23 | + server_location = ServerNetworkLocation("www.google.com", 443) |
| 24 | + server_info = check_connectivity_to_server_and_return_info(server_location) |
| 25 | + |
| 26 | + # When testing for EMS support, it succeeds with the expected result |
| 27 | + result: EmsExtensionScanResult = EmsExtensionImplementation.scan_server(server_info) |
| 28 | + assert result.supports_ems_extension |
| 29 | + |
| 30 | + # And a CLI output can be generated |
| 31 | + assert EmsExtensionImplementation.cli_connector_cls.result_to_console_output(result) |
| 32 | + |
| 33 | + # And the result can be converted to JSON |
| 34 | + result_as_json = EmsExtensionScanResultAsJson.model_validate(result).model_dump_json() |
| 35 | + assert result_as_json |
| 36 | + |
| 37 | + @can_only_run_on_linux_64 |
| 38 | + def test_bad(self) -> None: |
| 39 | + # Given a server that does NOT support EMS |
| 40 | + with LegacyOpenSslServer() as server: |
| 41 | + server_location = ServerNetworkLocation( |
| 42 | + hostname=server.hostname, ip_address=server.ip_address, port=server.port |
| 43 | + ) |
| 44 | + server_info = check_connectivity_to_server_and_return_info(server_location) |
| 45 | + |
| 46 | + # When testing for EMS, it succeeds |
| 47 | + result: EmsExtensionScanResult = EmsExtensionImplementation.scan_server(server_info) |
| 48 | + |
| 49 | + # And the server is reported as NOT supporting it |
| 50 | + assert not result.supports_ems_extension |
| 51 | + |
| 52 | + @can_only_run_on_linux_64 |
| 53 | + def test_fails_when_client_auth_failed(self) -> None: |
| 54 | + # Given a server that does NOT support EMS and that requires client authentication |
| 55 | + with LegacyOpenSslServer(client_auth_config=ClientAuthConfigEnum.REQUIRED) as server: |
| 56 | + # And sslyze does NOT provide a client certificate |
| 57 | + server_location = ServerNetworkLocation( |
| 58 | + hostname=server.hostname, ip_address=server.ip_address, port=server.port |
| 59 | + ) |
| 60 | + server_info = check_connectivity_to_server_and_return_info(server_location) |
| 61 | + |
| 62 | + # When testing, it fails as a client cert was not supplied |
| 63 | + with pytest.raises(ClientCertificateRequested): |
| 64 | + EmsExtensionImplementation.scan_server(server_info) |
| 65 | + |
| 66 | + @can_only_run_on_linux_64 |
| 67 | + def test_works_when_client_auth_succeeded(self) -> None: |
| 68 | + # Given a server that does NOT support EMS and that requires client authentication |
| 69 | + with LegacyOpenSslServer(client_auth_config=ClientAuthConfigEnum.REQUIRED) as server: |
| 70 | + server_location = ServerNetworkLocation( |
| 71 | + hostname=server.hostname, ip_address=server.ip_address, port=server.port |
| 72 | + ) |
| 73 | + # And sslyze provides a client certificate |
| 74 | + network_config = ServerNetworkConfiguration( |
| 75 | + tls_server_name_indication=server.hostname, |
| 76 | + tls_client_auth_credentials=ClientAuthenticationCredentials( |
| 77 | + certificate_chain_path=server.get_client_certificate_path(), key_path=server.get_client_key_path() |
| 78 | + ), |
| 79 | + ) |
| 80 | + server_info = check_connectivity_to_server_and_return_info(server_location, network_config) |
| 81 | + |
| 82 | + # When testing for EMS, it succeeds |
| 83 | + result: EmsExtensionScanResult = EmsExtensionImplementation.scan_server(server_info) |
| 84 | + |
| 85 | + # And the server is reported as NOT supporting EMS |
| 86 | + assert not result.supports_ems_extension |
0 commit comments