|
15 | 15 | InvalidTag,
|
16 | 16 | UnsupportedAlgorithm,
|
17 | 17 | )
|
| 18 | +from cryptography.hazmat.primitives import hashes |
18 | 19 | from cryptography.hazmat.primitives.asymmetric import (
|
19 | 20 | dsa,
|
20 | 21 | ec,
|
|
36 | 37 | load_ssh_public_identity,
|
37 | 38 | load_ssh_public_key,
|
38 | 39 | ssh,
|
| 40 | + ssh_key_fingerprint, |
39 | 41 | )
|
40 | 42 |
|
41 | 43 | from ...doubles import DummyKeySerializationEncryption
|
@@ -1868,3 +1870,96 @@ def test_load_application(self):
|
1868 | 1870 | def test_load_application_valueerror(self):
|
1869 | 1871 | with pytest.raises(ValueError):
|
1870 | 1872 | ssh.load_application(self.ssh_str("hss:test"))
|
| 1873 | + |
| 1874 | + |
| 1875 | +class TestSSHKeyFingerprint: |
| 1876 | + @pytest.mark.supported( |
| 1877 | + only_if=lambda backend: backend.hash_supported(hashes.MD5()), |
| 1878 | + skip_message="Does not support MD5", |
| 1879 | + ) |
| 1880 | + def test_ssh_key_fingerprint_rsa_md5(self): |
| 1881 | + ssh_key = load_vectors_from_file( |
| 1882 | + os.path.join("asymmetric", "OpenSSH", "rsa-nopsw.key.pub"), |
| 1883 | + lambda f: f.read(), |
| 1884 | + mode="rb", |
| 1885 | + ) |
| 1886 | + public_key = load_ssh_public_key(ssh_key) |
| 1887 | + fingerprint = ssh_key_fingerprint(public_key, hashes.MD5()) |
| 1888 | + assert fingerprint == b"1047c26573d65149480b811f36047b52" |
| 1889 | + |
| 1890 | + def test_ssh_key_fingerprint_rsa_sha256(self): |
| 1891 | + ssh_key = load_vectors_from_file( |
| 1892 | + os.path.join("asymmetric", "OpenSSH", "rsa-nopsw.key.pub"), |
| 1893 | + lambda f: f.read(), |
| 1894 | + mode="rb", |
| 1895 | + ) |
| 1896 | + public_key = load_ssh_public_key(ssh_key) |
| 1897 | + fingerprint = ssh_key_fingerprint(public_key, hashes.SHA256()) |
| 1898 | + assert fingerprint == b"gMB1ylYk/OsEsYNdmh6hjRfEZKIzvmuk6SCSaonm6CU" |
| 1899 | + |
| 1900 | + @pytest.mark.supported( |
| 1901 | + only_if=lambda backend: backend.hash_supported(hashes.MD5()) |
| 1902 | + and backend.ed25519_supported(), |
| 1903 | + skip_message="Does not support MD5 or Ed25519", |
| 1904 | + ) |
| 1905 | + def test_ssh_key_fingerprint_ed25519_md5(self): |
| 1906 | + ssh_key = load_vectors_from_file( |
| 1907 | + os.path.join("asymmetric", "OpenSSH", "ed25519-nopsw.key.pub"), |
| 1908 | + lambda f: f.read(), |
| 1909 | + mode="rb", |
| 1910 | + ) |
| 1911 | + public_key = load_ssh_public_key(ssh_key) |
| 1912 | + fingerprint = ssh_key_fingerprint(public_key, hashes.MD5()) |
| 1913 | + assert fingerprint == b"e5523d019ea0c1e98c3f4c7cc5945785" |
| 1914 | + |
| 1915 | + @pytest.mark.supported( |
| 1916 | + only_if=lambda backend: backend.ed25519_supported(), |
| 1917 | + skip_message="Ed25519 not supported", |
| 1918 | + ) |
| 1919 | + def test_ssh_key_fingerprint_ed25519_sha256(self): |
| 1920 | + ssh_key = load_vectors_from_file( |
| 1921 | + os.path.join("asymmetric", "OpenSSH", "ed25519-nopsw.key.pub"), |
| 1922 | + lambda f: f.read(), |
| 1923 | + mode="rb", |
| 1924 | + ) |
| 1925 | + public_key = load_ssh_public_key(ssh_key) |
| 1926 | + fingerprint = ssh_key_fingerprint(public_key, hashes.SHA256()) |
| 1927 | + assert fingerprint == b"knottK/0LBWlxvM2cDgzzCJdQ0ppFlY/hzlHWlZTOLk" |
| 1928 | + |
| 1929 | + @pytest.mark.supported( |
| 1930 | + only_if=lambda backend: backend.hash_supported(hashes.MD5()), |
| 1931 | + skip_message="Does not support MD5", |
| 1932 | + ) |
| 1933 | + def test_ssh_key_fingerprint_ecdsa_md5(self): |
| 1934 | + ssh_key = load_vectors_from_file( |
| 1935 | + os.path.join("asymmetric", "OpenSSH", "ecdsa-nopsw.key.pub"), |
| 1936 | + lambda f: f.read(), |
| 1937 | + mode="rb", |
| 1938 | + ) |
| 1939 | + public_key = load_ssh_public_key(ssh_key) |
| 1940 | + fingerprint = ssh_key_fingerprint(public_key, hashes.MD5()) |
| 1941 | + assert fingerprint == b"0d65f22dfa47718c5e16b0352b061b37" |
| 1942 | + |
| 1943 | + def test_ssh_key_fingerprint_ecdsa_sha256(self): |
| 1944 | + ssh_key = load_vectors_from_file( |
| 1945 | + os.path.join("asymmetric", "OpenSSH", "ecdsa-nopsw.key.pub"), |
| 1946 | + lambda f: f.read(), |
| 1947 | + mode="rb", |
| 1948 | + ) |
| 1949 | + public_key = load_ssh_public_key(ssh_key) |
| 1950 | + fingerprint = ssh_key_fingerprint(public_key, hashes.SHA256()) |
| 1951 | + assert fingerprint == b"W6Wr6d8N5R5y1rzZl8L03NTgrxc8adxeET7GkXdJSvU" |
| 1952 | + |
| 1953 | + def test_ssh_key_fingerprint_unsupported_hash(self): |
| 1954 | + ssh_key = load_vectors_from_file( |
| 1955 | + os.path.join("asymmetric", "OpenSSH", "rsa-nopsw.key.pub"), |
| 1956 | + lambda f: f.read(), |
| 1957 | + mode="rb", |
| 1958 | + ) |
| 1959 | + public_key = load_ssh_public_key(ssh_key) |
| 1960 | + with pytest.raises(TypeError): |
| 1961 | + ssh_key_fingerprint(public_key, hashes.SHA1()) # type: ignore[arg-type] |
| 1962 | + |
| 1963 | + def test_ssh_key_fingerprint_unsupported_key(self): |
| 1964 | + with pytest.raises(ValueError): |
| 1965 | + ssh_key_fingerprint(object(), hashes.SHA256()) # type: ignore[arg-type] |
0 commit comments