|
| 1 | +import pytest |
| 2 | + |
| 3 | +from lib.commands import SSHCommandFailed, ssh |
| 4 | +from lib.host import Host |
| 5 | + |
| 6 | +# Exhaustive coverage of the KEX/cipher/MAC/host-key algorithms OpenSSH |
| 7 | +# supports, hardcoded from `ssh -Q kex|cipher|mac|key` on the reference |
| 8 | +# build (openssh-9.9p1-27.2.xcpng8.3). The lists are deliberately not |
| 9 | +# queried live: the point of test_*_list_matches_hardcoded is to fail the |
| 10 | +# day the compiled-in algorithm support changes, forcing a conscious |
| 11 | +# update of this file instead of the change going unnoticed. |
| 12 | +# |
| 13 | +# Requirements: |
| 14 | +# - an XCP-ng host (--hosts) >= 8.3, with the OpenSSH 9.9p1 |
| 15 | + |
| 16 | +# algorithm -> enabled by default (sshd -T) on the reference build |
| 17 | +KEX_ALGORITHMS = { |
| 18 | + # hybrid post-quantum, new in this OpenSSH update |
| 19 | + "mlkem1024nistp384-sha384": True, |
| 20 | + "mlkem768x25519-sha256": True, |
| 21 | + "mlkem768nistp256-sha256": True, |
| 22 | + "sntrup761x25519-sha512": True, |
| 23 | + "sntrup761x25519-sha512@openssh.com": True, |
| 24 | + # classical, enabled |
| 25 | + "curve25519-sha256": True, |
| 26 | + "curve25519-sha256@libssh.org": True, |
| 27 | + "ecdh-sha2-nistp521": True, |
| 28 | + "ecdh-sha2-nistp384": True, |
| 29 | + "ecdh-sha2-nistp256": True, |
| 30 | + "diffie-hellman-group16-sha512": True, |
| 31 | + "diffie-hellman-group18-sha512": True, |
| 32 | + # compiled in, but disabled |
| 33 | + "diffie-hellman-group1-sha1": False, |
| 34 | + "diffie-hellman-group14-sha1": False, |
| 35 | + "diffie-hellman-group14-sha256": False, |
| 36 | + "diffie-hellman-group-exchange-sha1": False, |
| 37 | + "diffie-hellman-group-exchange-sha256": False, |
| 38 | +} |
| 39 | + |
| 40 | +CIPHERS = { |
| 41 | + "chacha20-poly1305@openssh.com": True, |
| 42 | + "aes256-gcm@openssh.com": True, |
| 43 | + "aes128-gcm@openssh.com": True, |
| 44 | + "aes256-ctr": True, |
| 45 | + "aes128-ctr": True, |
| 46 | + "3des-cbc": False, |
| 47 | + "aes128-cbc": False, |
| 48 | + "aes192-cbc": False, |
| 49 | + "aes256-cbc": False, |
| 50 | + "aes192-ctr": False, |
| 51 | +} |
| 52 | + |
| 53 | +MACS = { |
| 54 | + "hmac-sha2-512-etm@openssh.com": True, |
| 55 | + "hmac-sha2-256-etm@openssh.com": True, |
| 56 | + "umac-128-etm@openssh.com": True, |
| 57 | + "hmac-sha2-512": True, |
| 58 | + "hmac-sha2-256": True, |
| 59 | + "umac-128@openssh.com": True, |
| 60 | + "hmac-sha1": False, |
| 61 | + "hmac-sha1-96": False, |
| 62 | + "hmac-md5": False, |
| 63 | + "hmac-md5-96": False, |
| 64 | + "umac-64@openssh.com": False, |
| 65 | + "hmac-sha1-etm@openssh.com": False, |
| 66 | + "hmac-sha1-96-etm@openssh.com": False, |
| 67 | + "hmac-md5-etm@openssh.com": False, |
| 68 | + "hmac-md5-96-etm@openssh.com": False, |
| 69 | + "umac-64-etm@openssh.com": False, |
| 70 | +} |
| 71 | + |
| 72 | +# Server host identity algorithms actually exercisable end to end: the host |
| 73 | +# only carries one host key per type (ed25519, ecdsa on nistp256, rsa), so |
| 74 | +# only algorithms with matching key material are forced here. Certificate |
| 75 | +# variants would need a CA, and the sk-* types from `ssh -Q key` are not |
| 76 | +# host identity keys at all (they're for user authentication via a |
| 77 | +# hardware security key, see test_ssh_fido.py) so they don't belong in a |
| 78 | +# HostKeyAlgorithms test. |
| 79 | +HOSTKEY_ALGORITHMS = { |
| 80 | + "ssh-ed25519": True, |
| 81 | + "ecdsa-sha2-nistp256": True, |
| 82 | + "rsa-sha2-256": True, |
| 83 | + "rsa-sha2-512": True, |
| 84 | + "ssh-rsa": False, # raw SHA-1 RSA signature, disabled by crypto-policy |
| 85 | +} |
| 86 | + |
| 87 | +# Full compiled-in key type support (`ssh -Q key`), used only to detect if |
| 88 | +# that list ever drifts (e.g. FIDO/security-key support silently regressing, |
| 89 | +# as it did between the previous and this OpenSSH build). |
| 90 | +ALL_COMPILED_KEY_TYPES = { |
| 91 | + "ssh-ed25519", |
| 92 | + "ssh-ed25519-cert-v01@openssh.com", |
| 93 | + "sk-ssh-ed25519@openssh.com", |
| 94 | + "sk-ssh-ed25519-cert-v01@openssh.com", |
| 95 | + "ecdsa-sha2-nistp256", |
| 96 | + "ecdsa-sha2-nistp256-cert-v01@openssh.com", |
| 97 | + "ecdsa-sha2-nistp384", |
| 98 | + "ecdsa-sha2-nistp384-cert-v01@openssh.com", |
| 99 | + "ecdsa-sha2-nistp521", |
| 100 | + "ecdsa-sha2-nistp521-cert-v01@openssh.com", |
| 101 | + "sk-ecdsa-sha2-nistp256@openssh.com", |
| 102 | + "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com", |
| 103 | + "ssh-rsa", |
| 104 | + "ssh-rsa-cert-v01@openssh.com", |
| 105 | +} |
| 106 | + |
| 107 | +def _assert_list_matches(host: Host, query: str, expected: set) -> None: |
| 108 | + actual = {line for line in host.ssh(f"ssh -Q {query}").splitlines() if line and ' ' not in line} |
| 109 | + assert actual == expected, ( |
| 110 | + f"`ssh -Q {query}` no longer matches the hardcoded list in this test file " |
| 111 | + f"(missing={sorted(expected - actual)}, new={sorted(actual - expected)}); " |
| 112 | + "update the hardcoded list after reviewing the change" |
| 113 | + ) |
| 114 | + |
| 115 | +def test_kex_algorithms_list_matches_hardcoded(host: Host) -> None: |
| 116 | + _assert_list_matches(host, "kex", set(KEX_ALGORITHMS)) |
| 117 | + |
| 118 | +def test_ciphers_list_matches_hardcoded(host: Host) -> None: |
| 119 | + _assert_list_matches(host, "cipher", set(CIPHERS)) |
| 120 | + |
| 121 | +def test_macs_list_matches_hardcoded(host: Host) -> None: |
| 122 | + _assert_list_matches(host, "mac", set(MACS)) |
| 123 | + |
| 124 | +def test_key_types_list_matches_hardcoded(host: Host) -> None: |
| 125 | + _assert_list_matches(host, "key", ALL_COMPILED_KEY_TYPES) |
| 126 | + |
| 127 | +def _force_algorithm(host: Host, option: str, algo: str, *, extra_options: list[str] = []) -> None: |
| 128 | + # multiplexing must be off: a shared control connection would reuse the |
| 129 | + # algorithm negotiated by whichever call created it, silently ignoring |
| 130 | + # the -o option on every later call. |
| 131 | + ssh(host.hostname_or_ip, 'true', options=['-o', f'{option}={algo}'] + extra_options, multiplexing=False) |
| 132 | + |
| 133 | +# mlkem768nistp256-sha256 and mlkem1024nistp384-sha384 aren't part of |
| 134 | +# upstream OpenSSH (see openssh-10.0-mlkem-nist.patch): they're carried by |
| 135 | +# RHEL-family builds (RHEL, CentOS, Alma, and this XCP-ng build) for FIPS |
| 136 | +# compliance, but not by other builds, including the machine running these |
| 137 | +# tests. There's no second RHEL-family host available to interoperate with |
| 138 | +# either, so unlike every other algorithm here, these two are exercised in |
| 139 | +# loopback on the host itself (which does understand its own names). That |
| 140 | +# can't reach an authenticated session (root has no key to log into |
| 141 | +# itself), so instead we assert the exact requested algorithm was the one |
| 142 | +# negotiated. |
| 143 | +SPECIFIC_KEX_NAMES = {"mlkem768nistp256-sha256", "mlkem1024nistp384-sha384"} |
| 144 | + |
| 145 | +def _negotiated_kex_in_loopback(host: Host, algo: str) -> str: |
| 146 | + # the trailing "; true" keeps the remote command's exit code at 0 |
| 147 | + # (the inner loopback ssh fails at authentication, not at key exchange) |
| 148 | + # so we can just inspect its output instead of juggling SSHCommandFailed. |
| 149 | + output = host.ssh( |
| 150 | + "ssh -v -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null " |
| 151 | + f"-o ControlMaster=no -o KexAlgorithms={algo} localhost true 2>&1; true" |
| 152 | + ) |
| 153 | + for line in output.splitlines(): |
| 154 | + if line.startswith("debug1: kex: algorithm:"): |
| 155 | + return line.rsplit(':', 1)[1].strip() |
| 156 | + pytest.fail(f"could not find the negotiated KEX algorithm in loopback ssh -v output:\n{output}") |
| 157 | + |
| 158 | +@pytest.mark.parametrize("algo,enabled", KEX_ALGORITHMS.items(), ids=list(KEX_ALGORITHMS)) |
| 159 | +def test_kex_algorithm(host: Host, algo: str, enabled: bool) -> None: |
| 160 | + if algo in SPECIFIC_KEX_NAMES: |
| 161 | + assert enabled |
| 162 | + assert _negotiated_kex_in_loopback(host, algo) == algo |
| 163 | + elif enabled: |
| 164 | + _force_algorithm(host, "KexAlgorithms", algo) |
| 165 | + else: |
| 166 | + with pytest.raises(SSHCommandFailed): |
| 167 | + _force_algorithm(host, "KexAlgorithms", algo) |
| 168 | + |
| 169 | +@pytest.mark.parametrize("algo,enabled", CIPHERS.items(), ids=list(CIPHERS)) |
| 170 | +def test_cipher_algorithm(host: Host, algo: str, enabled: bool) -> None: |
| 171 | + if enabled: |
| 172 | + _force_algorithm(host, "Ciphers", algo) |
| 173 | + else: |
| 174 | + with pytest.raises(SSHCommandFailed): |
| 175 | + _force_algorithm(host, "Ciphers", algo) |
| 176 | + |
| 177 | +@pytest.mark.parametrize("algo,enabled", MACS.items(), ids=list(MACS)) |
| 178 | +def test_mac_algorithm(host: Host, algo: str, enabled: bool) -> None: |
| 179 | + # MACs are only negotiated for non-AEAD ciphers (AEAD ciphers like the |
| 180 | + # default chacha20-poly1305/aes-gcm have their MAC built in, making the |
| 181 | + # MACs option moot), so a non-AEAD cipher is pinned to force the point. |
| 182 | + non_aead_cipher = ['-o', 'Ciphers=aes256-ctr'] |
| 183 | + if enabled: |
| 184 | + _force_algorithm(host, "MACs", algo, extra_options=non_aead_cipher) |
| 185 | + else: |
| 186 | + with pytest.raises(SSHCommandFailed): |
| 187 | + _force_algorithm(host, "MACs", algo, extra_options=non_aead_cipher) |
| 188 | + |
| 189 | +@pytest.mark.parametrize("algo,enabled", HOSTKEY_ALGORITHMS.items(), ids=list(HOSTKEY_ALGORITHMS)) |
| 190 | +def test_hostkey_algorithm(host: Host, algo: str, enabled: bool) -> None: |
| 191 | + if enabled: |
| 192 | + _force_algorithm(host, "HostKeyAlgorithms", algo) |
| 193 | + else: |
| 194 | + with pytest.raises(SSHCommandFailed): |
| 195 | + _force_algorithm(host, "HostKeyAlgorithms", algo) |
0 commit comments