Class KnownHosts

java.lang.Object
com.trilead.ssh2.KnownHosts

public class KnownHosts extends Object
The KnownHosts class is a handy tool to verify received server hostkeys based on the information in known_hosts files (the ones used by OpenSSH).

It offers basically an in-memory database for known_hosts entries, as well as some helper functions. Entries from a known_hosts file can be loaded at construction time. It is also possible to add more keys later (e.g., one can parse different known_hosts files).

It is a thread safe implementation, therefore, you need only to instantiate one KnownHosts for your whole application.

Version:
$Id: KnownHosts.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $
  • Field Details

  • Constructor Details

  • Method Details

    • addHostkey

      public void addHostkey(String[] hostnames, String serverHostKeyAlgorithm, byte[] serverHostKey) throws IOException
      Adds a single public key entry to the database. Note: this will NOT add the public key to any physical file (e.g., "~/.ssh/known_hosts") - use addHostkeyToFile() for that purpose. This method is designed to be used in a ServerHostKeyVerifier.
      Parameters:
      hostnames - a list of hostname patterns - at least one most be specified. Check out the OpenSSH sshd man page for a description of the pattern matching algorithm.
      serverHostKeyAlgorithm - as passed to the ServerHostKeyVerifier.
      serverHostKey - as passed to the ServerHostKeyVerifier.
      Throws:
      IOException - on failure trying to convert the host key to a saveable format
    • addHostkeys

      public void addHostkeys(char[] knownHostsData) throws IOException
      Parses the given known_hosts data and adds entries to the database.
      Parameters:
      knownHostsData - the known hosts data to parse
      Throws:
      IOException - on failure reading the parsing the known hosts data
    • addHostkeys

      public void addHostkeys(File knownHosts) throws IOException
      Parses the given known_hosts file and adds entries to the database.
      Parameters:
      knownHosts - the file to read the existing known hosts entries feom, add to add any new entries to
      Throws:
      IOException - on failure reading the existing known hosts file
    • createHashedHostname

      public static String createHashedHostname(String hostname)
      Generate the hashed representation of the given hostname. Useful for adding entries with hashed hostnames to a known_hosts file. (see -H option of OpenSSH key-gen).
      Parameters:
      hostname - the hostname to hash
      Returns:
      the hashed representation, e.g., "|1|cDhrv7zwEUV3k71CEPHnhHZezhA=|Xo+2y6rUXo2OIWRAYhBOIijbJMA="
    • hmacSha1Hash

      private static byte[] hmacSha1Hash(byte[] salt, String hostname)
    • checkHashed

      private boolean checkHashed(String entry, String hostname)
    • checkKey

      private int checkKey(String remoteHostname, PublicKey remoteKey)
    • getAllKnownHostEntries

      private Vector<KnownHosts.KnownHostsEntry> getAllKnownHostEntries(String hostname)
    • getPreferredServerHostkeyAlgorithmOrder

      public String[] getPreferredServerHostkeyAlgorithmOrder(String hostname)
      Try to find the preferred order of hostkey algorithms for the given hostname. Based on the type of hostkey that is present in the internal database. an ordered list of hostkey algorithms is returned which can be passed to Connection.setServerHostKeyAlgorithms.
      Parameters:
      hostname - the hostname (or hostname pattern) to search for
      Returns:
      null if no key for the given hostname is present or there are keys of multiple types present for the given hostname. Otherwise, an array with hostkey algorithms is returned.
    • hostnameMatches

      private boolean hostnameMatches(String[] hostpatterns, String hostname)
    • initialize

      private void initialize(char[] knownHostsData) throws IOException
      Throws:
      IOException
    • initialize

      private void initialize(File knownHosts) throws IOException
      Throws:
      IOException
    • matchKeys

      private boolean matchKeys(PublicKey key1, PublicKey key2)
    • pseudoRegex

      private boolean pseudoRegex(char[] pattern, int i, char[] match, int j)
    • recommendHostkeyAlgorithms

      private String[] recommendHostkeyAlgorithms(String hostname)
    • verifyHostkey

      public int verifyHostkey(String hostname, String serverHostKeyAlgorithm, byte[] serverHostKey) throws IOException
      Checks the internal hostkey database for the given hostkey. If no matching key can be found, then the hostname is resolved to an IP address and the search is repeated using that IP address.
      Parameters:
      hostname - the server's hostname, will be matched with all hostname patterns
      serverHostKeyAlgorithm - type of hostkey being verified
      serverHostKey - the key blob
      Returns:
      • HOSTKEY_IS_OK: the given hostkey matches an entry for the given hostname
      • HOSTKEY_IS_NEW: no entries found for this hostname and this type of hostkey
      • HOSTKEY_HAS_CHANGED: hostname is known, but with another key of the same type (man-in-the-middle attack?)
      Throws:
      IOException - if the supplied key blob cannot be parsed or does not match the given hostkey type.
    • decodeHostKey

      private PublicKey decodeHostKey(String hostKeyAlgorithm, byte[] encodedHostKey) throws IOException
      Throws:
      IOException
    • addHostkeyToFile

      public static void addHostkeyToFile(File knownHosts, String[] hostnames, String serverHostKeyAlgorithm, byte[] serverHostKey) throws IOException
      Adds a single public key entry to the a known_hosts file. This method is designed to be used in a ServerHostKeyVerifier.
      Parameters:
      knownHosts - the file where the publickey entry will be appended.
      hostnames - a list of hostname patterns - at least one most be specified. Check out the OpenSSH sshd man page for a description of the pattern matching algorithm.
      serverHostKeyAlgorithm - as passed to the ServerHostKeyVerifier.
      serverHostKey - as passed to the ServerHostKeyVerifier.
      Throws:
      IOException - on failure parsing the key or writing to file
    • rawFingerPrint

      private static byte[] rawFingerPrint(String type, String keyType, byte[] hostkey)
      Generates a "raw" fingerprint of a hostkey.
      Parameters:
      type - either "md5" or "sha1"
      keyType - the type of key being fingerprinted
      hostkey - the hostkey
      Returns:
      the raw fingerprint
    • rawToHexFingerprint

      private static String rawToHexFingerprint(byte[] fingerprint)
      Convert a raw fingerprint to hex representation (XX:YY:ZZ...).
      Parameters:
      fingerprint - raw fingerprint
      Returns:
      the hex representation
    • rawToBubblebabbleFingerprint

      private static String rawToBubblebabbleFingerprint(byte[] raw)
      Convert a raw fingerprint to bubblebabble representation.
      Parameters:
      raw - raw fingerprint
      Returns:
      the bubblebabble representation
    • createHexFingerprint

      public static String createHexFingerprint(String keytype, byte[] publickey)
      Convert a ssh2 key-blob into a human readable hex fingerprint. Generated fingerprints are identical to those generated by OpenSSH.

      Example fingerprint: d0:cb:76:19:99:5a:03:fc:73:10:70:93:f2:44:63:47.

      Parameters:
      keytype - the type of key being fingerprinted
      publickey - key blob
      Returns:
      Hex fingerprint
    • createBubblebabbleFingerprint

      public static String createBubblebabbleFingerprint(String keytype, byte[] publickey)
      Convert a ssh2 key-blob into a human readable bubblebabble fingerprint. The used bubblebabble algorithm (taken from OpenSSH) generates fingerprints that are easier to remember for humans.

      Example fingerprint: xofoc-bubuz-cazin-zufyl-pivuk-biduk-tacib-pybur-gonar-hotat-lyxux.

      Parameters:
      keytype - the type of key being fingerprinted
      publickey - key data
      Returns:
      Bubblebabble fingerprint