public class KnownHostsKeyVerification extends Object implements HostKeyVerification, HostKeyUpdater
An abstract HostKeyVerification class implementation providing validation against the known_hosts format.
Modifier and Type | Class and Description |
---|---|
class |
KnownHostsKeyVerification.BlankEntry |
class |
KnownHostsKeyVerification.CertAuthorityEntry |
class |
KnownHostsKeyVerification.CommentEntry |
class |
KnownHostsKeyVerification.HostFileEntry |
class |
KnownHostsKeyVerification.InvalidEntry |
class |
KnownHostsKeyVerification.KeyEntry |
class |
KnownHostsKeyVerification.RevokedEntry |
class |
KnownHostsKeyVerification.Ssh2KeyEntry |
Constructor and Description |
---|
KnownHostsKeyVerification() |
KnownHostsKeyVerification(InputStream in) |
KnownHostsKeyVerification(String knownhosts) |
Modifier and Type | Method and Description |
---|---|
void |
addEntry(SshPublicKey key,
String comment,
String... names) |
void |
addListener(HostKeyVerificationListener listener) |
void |
allowHost(String host,
SshPublicKey key,
boolean always) |
void |
clear() |
Set<String> |
getAlgorithmsForHost(String host) |
Set<KnownHostsKeyVerification.KeyEntry> |
getKeyEntries() |
boolean |
isHostFileWriteable() |
boolean |
isKnownHost(String host,
SshPublicKey key) |
void |
load(InputStream in) |
protected void |
onHostKeyAdded(Set<String> names,
SshPublicKey key) |
protected void |
onHostKeyMismatch(String host,
List<SshPublicKey> allowedHostKey,
SshPublicKey actualHostKey)
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. |
protected void |
onHostKeyRemoved(Set<String> names,
SshPublicKey key) |
protected void |
onHostKeyUpdated(Set<String> names,
SshPublicKey key) |
protected void |
onInvalidHostEntry(String entry) |
protected void |
onRevokedKey(String host,
SshPublicKey key)
Called by the
verifyHost method when the host key supplied
is listed as a revoked key. |
protected void |
onUnknownHost(String host,
SshPublicKey key)
Called by the
verifyHost method when the host key supplied
is not recorded in the known_hosts file. |
void |
removeEntries(SshPublicKey key) |
void |
removeEntries(String... hosts) |
void |
removeEntries(String host)
Removes an allowed host.
|
void |
removeEntries(String host,
SshPublicKey key) |
void |
removeEntry(KnownHostsKeyVerification.KeyEntry... keys) |
protected Set<String> |
resolveNames(String host) |
void |
setComment(KnownHostsKeyVerification.KeyEntry entry,
String comment) |
void |
setHashHosts(boolean hashHosts) |
void |
setUseCanonicalHostnames(boolean value) |
void |
setUseReverseDNS(boolean value) |
String |
toString()
Outputs the allowed hosts in the known_hosts file format.
|
void |
updateHostKey(String host,
SshPublicKey key) |
boolean |
useCanonicalHostname() |
boolean |
useHashHosts() |
boolean |
useReverseDNS() |
boolean |
verifyHost(String host,
SshPublicKey pk)
Verifies a host key against the list of known_hosts.
|
public KnownHostsKeyVerification(InputStream in) throws SshException, IOException
SshException
IOException
public KnownHostsKeyVerification(String knownhosts) throws SshException, IOException
SshException
IOException
public KnownHostsKeyVerification()
public void addListener(HostKeyVerificationListener listener)
public void clear()
public void load(InputStream in) throws SshException, IOException
SshException
IOException
protected void onHostKeyAdded(Set<String> names, SshPublicKey key)
public void setComment(KnownHostsKeyVerification.KeyEntry entry, String comment)
public void setHashHosts(boolean hashHosts)
protected void onInvalidHostEntry(String entry) throws SshException
SshException
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.
host
- the name of the hostallowedHostKey
- the current key recorded in the known_hosts file.actualHostKey
- the actual key supplied by the userSshException
- if an error occursprotected 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.
host
- the name of the hostkey
- the public key supplied by the hostSshException
- if an error occursprotected void onRevokedKey(String host, SshPublicKey key)
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.host
- key
- SshException
public void removeEntries(String host) throws SshException
Removes an allowed host.
host
- the host to removeSshException
public void removeEntries(String host, SshPublicKey key) throws SshException
SshException
public void removeEntries(String... hosts) throws SshException
SshException
public void removeEntries(SshPublicKey key)
public void removeEntry(KnownHostsKeyVerification.KeyEntry... keys)
protected void onHostKeyRemoved(Set<String> names, SshPublicKey key)
public boolean isHostFileWriteable()
public void allowHost(String host, SshPublicKey key, boolean always) throws SshException
SshException
public void addEntry(SshPublicKey key, String comment, String... names) throws SshException
SshException
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.
verifyHost
in interface HostKeyVerification
host
- the name of the hostpk
- the host key suppliedSshException
- if an error occurspublic Set<String> getAlgorithmsForHost(String host) throws SshException
SshException
public boolean useCanonicalHostname()
public boolean useReverseDNS()
public boolean useHashHosts()
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.....
public void setUseCanonicalHostnames(boolean value)
public void setUseReverseDNS(boolean value)
public Set<KnownHostsKeyVerification.KeyEntry> getKeyEntries()
public boolean isKnownHost(String host, SshPublicKey key) throws SshException
isKnownHost
in interface HostKeyUpdater
SshException
public void updateHostKey(String host, SshPublicKey key) throws SshException
updateHostKey
in interface HostKeyUpdater
SshException
protected void onHostKeyUpdated(Set<String> names, SshPublicKey key)
Copyright © 2024. All rights reserved.