knownhosts.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. # -*- test-case-name: twisted.conch.test.test_knownhosts -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. An implementation of the OpenSSH known_hosts database.
  6. @since: 8.2
  7. """
  8. from __future__ import annotations
  9. import hmac
  10. import sys
  11. from binascii import Error as DecodeError, a2b_base64, b2a_base64
  12. from contextlib import closing
  13. from hashlib import sha1
  14. from typing import IO, Callable, Literal
  15. from zope.interface import implementer
  16. from twisted.conch.error import HostKeyChanged, InvalidEntry, UserRejectedKey
  17. from twisted.conch.interfaces import IKnownHostEntry
  18. from twisted.conch.ssh.keys import BadKeyError, FingerprintFormats, Key
  19. from twisted.internet import defer
  20. from twisted.internet.defer import Deferred
  21. from twisted.logger import Logger
  22. from twisted.python.compat import nativeString
  23. from twisted.python.filepath import FilePath
  24. from twisted.python.randbytes import secureRandom
  25. from twisted.python.util import FancyEqMixin
  26. log = Logger()
  27. def _b64encode(s):
  28. """
  29. Encode a binary string as base64 with no trailing newline.
  30. @param s: The string to encode.
  31. @type s: L{bytes}
  32. @return: The base64-encoded string.
  33. @rtype: L{bytes}
  34. """
  35. return b2a_base64(s).strip()
  36. def _extractCommon(string):
  37. """
  38. Extract common elements of base64 keys from an entry in a hosts file.
  39. @param string: A known hosts file entry (a single line).
  40. @type string: L{bytes}
  41. @return: a 4-tuple of hostname data (L{bytes}), ssh key type (L{bytes}), key
  42. (L{Key}), and comment (L{bytes} or L{None}). The hostname data is
  43. simply the beginning of the line up to the first occurrence of
  44. whitespace.
  45. @rtype: L{tuple}
  46. """
  47. elements = string.split(None, 2)
  48. if len(elements) != 3:
  49. raise InvalidEntry()
  50. hostnames, keyType, keyAndComment = elements
  51. splitkey = keyAndComment.split(None, 1)
  52. if len(splitkey) == 2:
  53. keyString, comment = splitkey
  54. comment = comment.rstrip(b"\n")
  55. else:
  56. keyString = splitkey[0]
  57. comment = None
  58. key = Key.fromString(a2b_base64(keyString))
  59. return hostnames, keyType, key, comment
  60. class _BaseEntry:
  61. """
  62. Abstract base of both hashed and non-hashed entry objects, since they
  63. represent keys and key types the same way.
  64. @ivar keyType: The type of the key; either ssh-dss or ssh-rsa.
  65. @type keyType: L{bytes}
  66. @ivar publicKey: The server public key indicated by this line.
  67. @type publicKey: L{twisted.conch.ssh.keys.Key}
  68. @ivar comment: Trailing garbage after the key line.
  69. @type comment: L{bytes}
  70. """
  71. def __init__(self, keyType, publicKey, comment):
  72. self.keyType = keyType
  73. self.publicKey = publicKey
  74. self.comment = comment
  75. def matchesKey(self, keyObject):
  76. """
  77. Check to see if this entry matches a given key object.
  78. @param keyObject: A public key object to check.
  79. @type keyObject: L{Key}
  80. @return: C{True} if this entry's key matches C{keyObject}, C{False}
  81. otherwise.
  82. @rtype: L{bool}
  83. """
  84. return self.publicKey == keyObject
  85. @implementer(IKnownHostEntry)
  86. class PlainEntry(_BaseEntry):
  87. """
  88. A L{PlainEntry} is a representation of a plain-text entry in a known_hosts
  89. file.
  90. @ivar _hostnames: the list of all host-names associated with this entry.
  91. """
  92. def __init__(
  93. self, hostnames: list[bytes], keyType: bytes, publicKey: Key, comment: bytes
  94. ):
  95. self._hostnames: list[bytes] = hostnames
  96. super().__init__(keyType, publicKey, comment)
  97. @classmethod
  98. def fromString(cls, string: bytes) -> PlainEntry:
  99. """
  100. Parse a plain-text entry in a known_hosts file, and return a
  101. corresponding L{PlainEntry}.
  102. @param string: a space-separated string formatted like "hostname
  103. key-type base64-key-data comment".
  104. @raise DecodeError: if the key is not valid encoded as valid base64.
  105. @raise InvalidEntry: if the entry does not have the right number of
  106. elements and is therefore invalid.
  107. @raise BadKeyError: if the key, once decoded from base64, is not
  108. actually an SSH key.
  109. @return: an IKnownHostEntry representing the hostname and key in the
  110. input line.
  111. @rtype: L{PlainEntry}
  112. """
  113. hostnames, keyType, key, comment = _extractCommon(string)
  114. self = cls(hostnames.split(b","), keyType, key, comment)
  115. return self
  116. def matchesHost(self, hostname: bytes | str) -> bool:
  117. """
  118. Check to see if this entry matches a given hostname.
  119. @param hostname: A hostname or IP address literal to check against this
  120. entry.
  121. @return: C{True} if this entry is for the given hostname or IP address,
  122. C{False} otherwise.
  123. """
  124. if isinstance(hostname, str):
  125. hostname = hostname.encode("utf-8")
  126. return hostname in self._hostnames
  127. def toString(self) -> bytes:
  128. """
  129. Implement L{IKnownHostEntry.toString} by recording the comma-separated
  130. hostnames, key type, and base-64 encoded key.
  131. @return: The string representation of this entry, with unhashed hostname
  132. information.
  133. """
  134. fields = [
  135. b",".join(self._hostnames),
  136. self.keyType,
  137. _b64encode(self.publicKey.blob()),
  138. ]
  139. if self.comment is not None:
  140. fields.append(self.comment)
  141. return b" ".join(fields)
  142. @implementer(IKnownHostEntry)
  143. class UnparsedEntry:
  144. """
  145. L{UnparsedEntry} is an entry in a L{KnownHostsFile} which can't actually be
  146. parsed; therefore it matches no keys and no hosts.
  147. """
  148. def __init__(self, string):
  149. """
  150. Create an unparsed entry from a line in a known_hosts file which cannot
  151. otherwise be parsed.
  152. """
  153. self._string = string
  154. def matchesHost(self, hostname):
  155. """
  156. Always returns False.
  157. """
  158. return False
  159. def matchesKey(self, key):
  160. """
  161. Always returns False.
  162. """
  163. return False
  164. def toString(self):
  165. """
  166. Returns the input line, without its newline if one was given.
  167. @return: The string representation of this entry, almost exactly as was
  168. used to initialize this entry but without a trailing newline.
  169. @rtype: L{bytes}
  170. """
  171. return self._string.rstrip(b"\n")
  172. def _hmacedString(key, string):
  173. """
  174. Return the SHA-1 HMAC hash of the given key and string.
  175. @param key: The HMAC key.
  176. @type key: L{bytes}
  177. @param string: The string to be hashed.
  178. @type string: L{bytes}
  179. @return: The keyed hash value.
  180. @rtype: L{bytes}
  181. """
  182. hash = hmac.HMAC(key, digestmod=sha1)
  183. if isinstance(string, str):
  184. string = string.encode("utf-8")
  185. hash.update(string)
  186. return hash.digest()
  187. @implementer(IKnownHostEntry)
  188. class HashedEntry(_BaseEntry, FancyEqMixin):
  189. """
  190. A L{HashedEntry} is a representation of an entry in a known_hosts file
  191. where the hostname has been hashed and salted.
  192. @ivar _hostSalt: the salt to combine with a hostname for hashing.
  193. @ivar _hostHash: the hashed representation of the hostname.
  194. @cvar MAGIC: the 'hash magic' string used to identify a hashed line in a
  195. known_hosts file as opposed to a plaintext one.
  196. """
  197. MAGIC = b"|1|"
  198. compareAttributes = ("_hostSalt", "_hostHash", "keyType", "publicKey", "comment")
  199. def __init__(
  200. self,
  201. hostSalt: bytes,
  202. hostHash: bytes,
  203. keyType: bytes,
  204. publicKey: Key,
  205. comment: bytes | None,
  206. ) -> None:
  207. self._hostSalt = hostSalt
  208. self._hostHash = hostHash
  209. super().__init__(keyType, publicKey, comment)
  210. @classmethod
  211. def fromString(cls, string: bytes) -> HashedEntry:
  212. """
  213. Load a hashed entry from a string representing a line in a known_hosts
  214. file.
  215. @param string: A complete single line from a I{known_hosts} file,
  216. formatted as defined by OpenSSH.
  217. @raise DecodeError: if the key, the hostname, or the is not valid
  218. encoded as valid base64
  219. @raise InvalidEntry: if the entry does not have the right number of
  220. elements and is therefore invalid, or the host/hash portion
  221. contains more items than just the host and hash.
  222. @raise BadKeyError: if the key, once decoded from base64, is not
  223. actually an SSH key.
  224. @return: The newly created L{HashedEntry} instance, initialized with
  225. the information from C{string}.
  226. """
  227. stuff, keyType, key, comment = _extractCommon(string)
  228. saltAndHash = stuff[len(cls.MAGIC) :].split(b"|")
  229. if len(saltAndHash) != 2:
  230. raise InvalidEntry()
  231. hostSalt, hostHash = saltAndHash
  232. self = cls(a2b_base64(hostSalt), a2b_base64(hostHash), keyType, key, comment)
  233. return self
  234. def matchesHost(self, hostname):
  235. """
  236. Implement L{IKnownHostEntry.matchesHost} to compare the hash of the
  237. input to the stored hash.
  238. @param hostname: A hostname or IP address literal to check against this
  239. entry.
  240. @type hostname: L{bytes}
  241. @return: C{True} if this entry is for the given hostname or IP address,
  242. C{False} otherwise.
  243. @rtype: L{bool}
  244. """
  245. return hmac.compare_digest(
  246. _hmacedString(self._hostSalt, hostname), self._hostHash
  247. )
  248. def toString(self):
  249. """
  250. Implement L{IKnownHostEntry.toString} by base64-encoding the salt, host
  251. hash, and key.
  252. @return: The string representation of this entry, with the hostname part
  253. hashed.
  254. @rtype: L{bytes}
  255. """
  256. fields = [
  257. self.MAGIC
  258. + b"|".join([_b64encode(self._hostSalt), _b64encode(self._hostHash)]),
  259. self.keyType,
  260. _b64encode(self.publicKey.blob()),
  261. ]
  262. if self.comment is not None:
  263. fields.append(self.comment)
  264. return b" ".join(fields)
  265. class KnownHostsFile:
  266. """
  267. A structured representation of an OpenSSH-format ~/.ssh/known_hosts file.
  268. @ivar _added: A list of L{IKnownHostEntry} providers which have been added
  269. to this instance in memory but not yet saved.
  270. @ivar _clobber: A flag indicating whether the current contents of the save
  271. path will be disregarded and potentially overwritten or not. If
  272. C{True}, this will be done. If C{False}, entries in the save path will
  273. be read and new entries will be saved by appending rather than
  274. overwriting.
  275. @type _clobber: L{bool}
  276. @ivar _savePath: See C{savePath} parameter of L{__init__}.
  277. """
  278. def __init__(self, savePath: FilePath[str]) -> None:
  279. """
  280. Create a new, empty KnownHostsFile.
  281. Unless you want to erase the current contents of C{savePath}, you want
  282. to use L{KnownHostsFile.fromPath} instead.
  283. @param savePath: The L{FilePath} to which to save new entries.
  284. @type savePath: L{FilePath}
  285. """
  286. self._added: list[IKnownHostEntry] = []
  287. self._savePath = savePath
  288. self._clobber = True
  289. @property
  290. def savePath(self) -> FilePath[str]:
  291. """
  292. @see: C{savePath} parameter of L{__init__}
  293. """
  294. return self._savePath
  295. def iterentries(self):
  296. """
  297. Iterate over the host entries in this file.
  298. @return: An iterable the elements of which provide L{IKnownHostEntry}.
  299. There is an element for each entry in the file as well as an element
  300. for each added but not yet saved entry.
  301. @rtype: iterable of L{IKnownHostEntry} providers
  302. """
  303. for entry in self._added:
  304. yield entry
  305. if self._clobber:
  306. return
  307. try:
  308. fp = self._savePath.open()
  309. except OSError:
  310. return
  311. with fp:
  312. for line in fp:
  313. try:
  314. if line.startswith(HashedEntry.MAGIC):
  315. entry = HashedEntry.fromString(line)
  316. else:
  317. entry = PlainEntry.fromString(line)
  318. except (DecodeError, InvalidEntry, BadKeyError):
  319. entry = UnparsedEntry(line)
  320. yield entry
  321. def hasHostKey(self, hostname, key):
  322. """
  323. Check for an entry with matching hostname and key.
  324. @param hostname: A hostname or IP address literal to check for.
  325. @type hostname: L{bytes}
  326. @param key: The public key to check for.
  327. @type key: L{Key}
  328. @return: C{True} if the given hostname and key are present in this file,
  329. C{False} if they are not.
  330. @rtype: L{bool}
  331. @raise HostKeyChanged: if the host key found for the given hostname
  332. does not match the given key.
  333. """
  334. for lineidx, entry in enumerate(self.iterentries(), -len(self._added)):
  335. if entry.matchesHost(hostname) and entry.keyType == key.sshType():
  336. if entry.matchesKey(key):
  337. return True
  338. else:
  339. # Notice that lineidx is 0-based but HostKeyChanged.lineno
  340. # is 1-based.
  341. if lineidx < 0:
  342. line = None
  343. path = None
  344. else:
  345. line = lineidx + 1
  346. path = self._savePath
  347. raise HostKeyChanged(entry, path, line)
  348. return False
  349. def verifyHostKey(
  350. self, ui: ConsoleUI, hostname: bytes, ip: bytes, key: Key
  351. ) -> Deferred[bool]:
  352. """
  353. Verify the given host key for the given IP and host, asking for
  354. confirmation from, and notifying, the given UI about changes to this
  355. file.
  356. @param ui: The user interface to request an IP address from.
  357. @param hostname: The hostname that the user requested to connect to.
  358. @param ip: The string representation of the IP address that is actually
  359. being connected to.
  360. @param key: The public key of the server.
  361. @return: a L{Deferred} that fires with True when the key has been
  362. verified, or fires with an errback when the key either cannot be
  363. verified or has changed.
  364. @rtype: L{Deferred}
  365. """
  366. hhk = defer.execute(self.hasHostKey, hostname, key)
  367. def gotHasKey(result: bool) -> bool | Deferred[bool]:
  368. if result:
  369. if not self.hasHostKey(ip, key):
  370. addMessage = (
  371. f"Warning: Permanently added the {key.type()} host key"
  372. f" for IP address '{ip.decode()}' to the list of known"
  373. " hosts.\n"
  374. )
  375. ui.warn(addMessage.encode("utf-8"))
  376. self.addHostKey(ip, key)
  377. self.save()
  378. return result
  379. else:
  380. def promptResponse(response: bool) -> bool:
  381. if response:
  382. self.addHostKey(hostname, key)
  383. self.addHostKey(ip, key)
  384. self.save()
  385. return response
  386. else:
  387. raise UserRejectedKey()
  388. keytype: str = key.type()
  389. if keytype == "EC":
  390. keytype = "ECDSA"
  391. prompt = (
  392. "The authenticity of host '%s (%s)' "
  393. "can't be established.\n"
  394. "%s key fingerprint is SHA256:%s.\n"
  395. "Are you sure you want to continue connecting (yes/no)? "
  396. % (
  397. nativeString(hostname),
  398. nativeString(ip),
  399. keytype,
  400. key.fingerprint(format=FingerprintFormats.SHA256_BASE64),
  401. )
  402. )
  403. proceed = ui.prompt(prompt.encode(sys.getdefaultencoding()))
  404. return proceed.addCallback(promptResponse)
  405. return hhk.addCallback(gotHasKey)
  406. def addHostKey(self, hostname: bytes, key: Key) -> HashedEntry:
  407. """
  408. Add a new L{HashedEntry} to the key database.
  409. Note that you still need to call L{KnownHostsFile.save} if you wish
  410. these changes to be persisted.
  411. @param hostname: A hostname or IP address literal to associate with the
  412. new entry.
  413. @type hostname: L{bytes}
  414. @param key: The public key to associate with the new entry.
  415. @type key: L{Key}
  416. @return: The L{HashedEntry} that was added.
  417. @rtype: L{HashedEntry}
  418. """
  419. salt = secureRandom(20)
  420. keyType = key.sshType()
  421. entry = HashedEntry(salt, _hmacedString(salt, hostname), keyType, key, None)
  422. self._added.append(entry)
  423. return entry
  424. def save(self) -> None:
  425. """
  426. Save this L{KnownHostsFile} to the path it was loaded from.
  427. """
  428. p = self._savePath.parent()
  429. if not p.isdir():
  430. p.makedirs()
  431. mode: Literal["a", "w"] = "w" if self._clobber else "a"
  432. with self._savePath.open(mode) as hostsFileObj:
  433. if self._added:
  434. hostsFileObj.write(
  435. b"\n".join([entry.toString() for entry in self._added]) + b"\n"
  436. )
  437. self._added = []
  438. self._clobber = False
  439. @classmethod
  440. def fromPath(cls, path: FilePath[str]) -> KnownHostsFile:
  441. """
  442. Create a new L{KnownHostsFile}, potentially reading existing known
  443. hosts information from the given file.
  444. @param path: A path object to use for both reading contents from and
  445. later saving to. If no file exists at this path, it is not an
  446. error; a L{KnownHostsFile} with no entries is returned.
  447. @return: A L{KnownHostsFile} initialized with entries from C{path}.
  448. """
  449. knownHosts = cls(path)
  450. knownHosts._clobber = False
  451. return knownHosts
  452. class ConsoleUI:
  453. """
  454. A UI object that can ask true/false questions and post notifications on the
  455. console, to be used during key verification.
  456. """
  457. def __init__(self, opener: Callable[[], IO[bytes]]):
  458. """
  459. @param opener: A no-argument callable which should open a console
  460. binary-mode file-like object to be used for reading and writing.
  461. This initializes the C{opener} attribute.
  462. @type opener: callable taking no arguments and returning a read/write
  463. file-like object
  464. """
  465. self.opener = opener
  466. def prompt(self, text: bytes) -> Deferred[bool]:
  467. """
  468. Write the given text as a prompt to the console output, then read a
  469. result from the console input.
  470. @param text: Something to present to a user to solicit a yes or no
  471. response.
  472. @type text: L{bytes}
  473. @return: a L{Deferred} which fires with L{True} when the user answers
  474. 'yes' and L{False} when the user answers 'no'. It may errback if
  475. there were any I/O errors.
  476. """
  477. d = defer.succeed(None)
  478. def body(ignored):
  479. with closing(self.opener()) as f:
  480. f.write(text)
  481. while True:
  482. answer = f.readline().strip().lower()
  483. if answer == b"yes":
  484. return True
  485. elif answer in {b"no", b""}:
  486. return False
  487. else:
  488. f.write(b"Please type 'yes' or 'no': ")
  489. return d.addCallback(body)
  490. def warn(self, text: bytes) -> None:
  491. """
  492. Notify the user (non-interactively) of the provided text, by writing it
  493. to the console.
  494. @param text: Some information the user is to be made aware of.
  495. """
  496. try:
  497. with closing(self.opener()) as f:
  498. f.write(text)
  499. except Exception:
  500. log.failure("Failed to write to console")