Class KnownHostsKeyVerification

java.lang.Object
com.sshtools.common.knownhosts.KnownHostsKeyVerification
All Implemented Interfaces:
HostKeyUpdater, HostKeyVerification
Direct Known Subclasses:
KnownHostsFile

public class KnownHostsKeyVerification extends Object implements HostKeyVerification, HostKeyUpdater

An abstract HostKeyVerification class implementation providing validation against the known_hosts format.

Author:
Lee David Painter
  • Constructor Details

  • Method Details

    • clear

      public void clear()
    • load

      public void load(InputStream in) throws SshException, IOException
      Throws:
      SshException
      IOException
    • onHostKeyAdded

      protected void onHostKeyAdded(Set<String> names, SshPublicKey key)
    • setComment

      public void setComment(KnownHostsKeyVerification.KeyEntry entry, String comment)
    • setHashHosts

      public void setHashHosts(boolean hashHosts)
    • onInvalidHostEntry

      protected void onInvalidHostEntry(String entry) throws SshException
      Throws:
      SshException
    • onHostKeyMismatch

      protected void onHostKeyMismatch(String host, List<SshPublicKey> allowedHostKey, SshPublicKey actualHostKey) throws SshException

      Called by the verifyHost method when the host key supplied by the host does not match the current key recording in the known hosts file.

      Parameters:
      host - the name of the host
      allowedHostKey - the current key recorded in the known_hosts file.
      actualHostKey - the actual key supplied by the user
      Throws:
      SshException - if an error occurs
      Since:
      0.2.0
    • onUnknownHost

      protected void onUnknownHost(String host, SshPublicKey key) throws SshException

      Called by the verifyHost method when the host key supplied is not recorded in the known_hosts file.

      Parameters:
      host - the name of the host
      key - the public key supplied by the host
      Throws:
      SshException - if an error occurs
      Since:
      0.2.0
    • onRevokedKey

      protected void onRevokedKey(String host, SshPublicKey key)
      Called by the verifyHost method when the host key supplied is listed as a revoked key. This is informational, any changes made to the current entries will still result in a failed host verification.
      Parameters:
      host -
      key -
      Throws:
      SshException
    • removeEntries

      public void removeEntries(String host) throws SshException

      Removes an allowed host.

      Parameters:
      host - the host to remove
      Throws:
      SshException
      Since:
      0.2.0
    • removeEntries

      public void removeEntries(String... hosts) throws SshException
      Throws:
      SshException
    • removeEntries

      public void removeEntries(SshPublicKey key)
    • removeEntry

      public void removeEntry(KnownHostsKeyVerification.KeyEntry... keys)
    • onHostKeyRemoved

      protected void onHostKeyRemoved(Set<String> names, SshPublicKey key)
    • isHostFileWriteable

      public boolean isHostFileWriteable()
    • allowHost

      public void allowHost(String host, SshPublicKey key, boolean always) throws SshException
      Throws:
      SshException
    • addEntry

      public void addEntry(SshPublicKey key, String comment, String... names) throws SshException
      Throws:
      SshException
    • addEntry

      public void addEntry(SshPublicKey key, String comment, boolean always, String... names) throws SshException
      Throws:
      SshException
    • verifyHost

      public boolean verifyHost(String host, SshPublicKey pk) throws SshException

      Verifies a host key against the list of known_hosts.

      If the host unknown or the key does not match the currently allowed host key the abstract onUnknownHost or onHostKeyMismatch methods are called so that the caller may identify and allow the host.

      Specified by:
      verifyHost in interface HostKeyVerification
      Parameters:
      host - the name of the host
      pk - the host key supplied
      Returns:
      true if the host is accepted, otherwise false
      Throws:
      SshException - if an error occurs
      Since:
      0.2.0
    • verifyHost

      protected boolean verifyHost(String host, SshPublicKey pk, boolean validateUnknown) throws SshException
      Throws:
      SshException
    • resolveNames

      protected Set<String> resolveNames(String host)
    • useCanonicalHostname

      public boolean useCanonicalHostname()
    • useReverseDNS

      public boolean useReverseDNS()
    • useHashHosts

      public boolean useHashHosts()
    • toString

      public String toString()

      Outputs the allowed hosts in the known_hosts file format.

      The format consists of any number of lines each representing one key for a single host.

      titan,192.168.1.12 ssh-dss AAAAB3NzaC1kc3MAAACBAP1/U4Ed..... titan,192.168.1.12 ssh-rsa AAAAB3NzaC1kc3MAAACBAP1/U4Ed..... einstein,192.168.1.40 ssh-dss AAAAB3NzaC1kc3MAAACBAP1/U4Ed.....
      Overrides:
      toString in class Object
      Returns:
      String
      Since:
      0.2.0
    • setUseCanonicalHostnames

      public void setUseCanonicalHostnames(boolean value)
    • setUseReverseDNS

      public void setUseReverseDNS(boolean value)
    • getKeyEntries

      public Set<KnownHostsKeyVerification.KeyEntry> getKeyEntries()
    • isKnownHost

      public boolean isKnownHost(String host, SshPublicKey key) throws SshException
      Specified by:
      isKnownHost in interface HostKeyUpdater
      Throws:
      SshException
    • updateHostKey

      public void updateHostKey(String host, SshPublicKey key) throws SshException
      Specified by:
      updateHostKey in interface HostKeyUpdater
      Throws:
      SshException
    • onHostKeyUpdated

      protected void onHostKeyUpdated(Set<String> names, SshPublicKey key)