Skip to content

Commit

Permalink
Add markers to known_hosts parser re paramiko#771
Browse files Browse the repository at this point in the history
Not complete!

Adds basic parser support for a marker in the first field. Additional
handling of cert-authority should be discussed before being implemented.

Adds sample keys to test_hostkeys.py.
  • Loading branch information
MajorDallas committed Jul 20, 2023
1 parent 56f3c16 commit 34299ff
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
18 changes: 17 additions & 1 deletion paramiko/hostkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


from base64 import encodebytes, decodebytes
from enum import Enum
import binascii
import os
import re
Expand Down Expand Up @@ -311,6 +312,11 @@ def __init__(self, line, exc):
self.args = (line, exc)


class HostKeyMarkers(str, Enum):
CERT_AUTHORITY = "@cert-authority"
REVOKED = "@revoked"


class HostKeyEntry:
"""
Representation of a line in an OpenSSH-style "known hosts" file.
Expand Down Expand Up @@ -342,7 +348,17 @@ def from_line(cls, line, lineno=None):
msg = "Not enough fields found in known_hosts in line {} ({!r})"
log.info(msg.format(lineno, line))
return None
fields = fields[:3]

try:
marker = HostKeyMarkers(fields[0])
fields = fields[1:4]
except ValueError:
marker = None
fields = fields[:3]

if marker is HostKeyMarkers.REVOKED:
log.info("Key is marked as revoked")
return None

names, key_type, key = fields
names = names.split(",")
Expand Down
24 changes: 20 additions & 4 deletions tests/test_hostkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
curvy.example.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlz\
dHAyNTYAAABBBAa+pY7djSpbg5viAcZhPt56AO3U3Sd7h7dnlUp0EjfDgyYHYQxl2QZ4JGgfwR5iv9\
T9iRZjQzvJd5s+kBAZtpk=
@cert-authority ca.example.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA8bP1ZA7DCZD\
B9J0s50l31MBGQ3GQ/Fc7SX6gkpXkwcZryoi4kNFhHu5LvHcZPdxXV1D+uTMfGS1eyd2Yz/DoNWXNA\
l8TI0cAsW5ymME3bQ4J/k1IKxCtz/bAlAqFgKoc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+\
Pc2M=
@revoked revoked.example.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA8bP1ZA7DCZD\
B9J0s50l31MBGQ3GQ/Fc7SX6gkpXkwcZryoi4kNFhHu5LvHcZPdxXV1D+uTMfGS1eyd2Yz/DoNWXNA\
l8TI0cAsW5ymME3bQ4J/k1IKxCtz/bAlAqFgKoc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+\
Pc2M=
"""

test_hosts_file_tabs = """\
Expand All @@ -55,6 +63,14 @@
curvy.example.com\tecdsa-sha2-nistp256\tAAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbml\
zdHAyNTYAAABBBAa+pY7djSpbg5viAcZhPt56AO3U3Sd7h7dnlUp0EjfDgyYHYQxl2QZ4JGgfwR5iv\
9T9iRZjQzvJd5s+kBAZtpk=
@cert-authority\tca.example.com\tssh-rsa\tAAAAB3NzaC1yc2EAAAABIwAAAIEA8bP1ZA7DCZD\
B9J0s50l31MBGQ3GQ/Fc7SX6gkpXkwcZryoi4kNFhHu5LvHcZPdxXV1D+uTMfGS1eyd2Yz/DoNWXNA\
l8TI0cAsW5ymME3bQ4J/k1IKxCtz/bAlAqFgKoc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+\
Pc2M=
@revoked\trevoked.example.com\tssh-rsa\tAAAAB3NzaC1yc2EAAAABIwAAAIEA8bP1ZA7DCZD\
B9J0s50l31MBGQ3GQ/Fc7SX6gkpXkwcZryoi4kNFhHu5LvHcZPdxXV1D+uTMfGS1eyd2Yz/DoNWXNA\
l8TI0cAsW5ymME3bQ4J/k1IKxCtz/bAlAqFgKoc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+\
Pc2M=
"""

keyblob = b"""\
Expand Down Expand Up @@ -83,7 +99,7 @@ def tearDown(self):

def test_load(self):
hostdict = paramiko.HostKeys("hostfile.temp")
assert len(hostdict) == 4
assert len(hostdict) == 5
self.assertEqual(1, len(list(hostdict.values())[0]))
self.assertEqual(1, len(list(hostdict.values())[1]))
fp = hexlify(
Expand All @@ -96,7 +112,7 @@ def test_add(self):
hh = "|1|BMsIC6cUIP2zBuXR3t2LRcJYjzM=|hpkJMysjTk/+zzUUzxQEa2ieq6c="
key = paramiko.RSAKey(data=decodebytes(keyblob))
hostdict.add(hh, "ssh-rsa", key)
assert len(hostdict) == 5
assert len(hostdict) == 6
x = hostdict["foo.example.com"]
fp = hexlify(x["ssh-rsa"].get_fingerprint()).upper()
self.assertEqual(b"7EC91BB336CB6D810B124B1353C32396", fp)
Expand All @@ -113,7 +129,7 @@ def test_dict(self):
fp = hexlify(x["ssh-rsa"].get_fingerprint()).upper()
self.assertEqual(b"E6684DB30E109B67B70FF1DC5C7F1363", fp)
assert list(hostdict) == hostdict.keys()
assert len(list(hostdict)) == len(hostdict.keys()) == 4
assert len(list(hostdict)) == len(hostdict.keys()) == 5

def test_dict_set(self):
hostdict = paramiko.HostKeys("hostfile.temp")
Expand All @@ -123,7 +139,7 @@ def test_dict_set(self):
hostdict["fake.example.com"] = {}
hostdict["fake.example.com"]["ssh-rsa"] = key

assert len(hostdict) == 5
assert len(hostdict) == 6
self.assertEqual(2, len(list(hostdict.values())[0]))
self.assertEqual(1, len(list(hostdict.values())[1]))
self.assertEqual(1, len(list(hostdict.values())[2]))
Expand Down

0 comments on commit 34299ff

Please sign in to comment.