Class KnownHostsServerKeyVerifier
java.lang.Object
org.apache.sshd.common.util.logging.AbstractLoggingBean
org.apache.sshd.common.util.io.ModifiableFileWatcher
org.apache.sshd.client.keyverifier.KnownHostsServerKeyVerifier
- All Implemented Interfaces:
ModifiedServerKeyAcceptor,ServerKeyVerifier
- Direct Known Subclasses:
DefaultKnownHostsServerKeyVerifier
public class KnownHostsServerKeyVerifier
extends ModifiableFileWatcher
implements ServerKeyVerifier, ModifiedServerKeyAcceptor
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents an entry in the internal verifier's cache -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ServerKeyVerifierprivate final AtomicReference<Supplier<? extends Collection<KnownHostsServerKeyVerifier.HostEntryPair>>> static final StringStandard option used to indicate alternative known hosts file locationprivate ModifiedServerKeyAcceptorstatic final StringStandard option used to indicate whether to use strict host key checking or not.protected final ObjectFields inherited from class org.apache.sshd.common.util.io.ModifiableFileWatcher
options, STRICTLY_PROHIBITED_FILE_PERMISSIONFields inherited from class org.apache.sshd.common.util.logging.AbstractLoggingBean
log -
Constructor Summary
ConstructorsConstructorDescriptionKnownHostsServerKeyVerifier(ServerKeyVerifier delegate, Path file) KnownHostsServerKeyVerifier(ServerKeyVerifier delegate, Path file, LinkOption... options) -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanacceptIncompleteHostKeys(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey, Throwable reason) Called if failed to reload known hosts - by default invokesacceptUnknownHostKey(ClientSession, SocketAddress, PublicKey)protected booleanacceptKnownHostEntries(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts) booleanacceptModifiedServerKey(ClientSession clientSession, SocketAddress remoteAddress, KnownHostEntry entry, PublicKey expected, PublicKey actual) Invoked when a matching known host key was found but it does not match the presented one.protected booleanacceptUnknownHostKey(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey) Invoked if none of the known hosts matches the current one - by default invokes the delegate.protected List<KnownHostsServerKeyVerifier.HostEntryPair> findKnownHostEntries(ClientSession clientSession, SocketAddress remoteAddress, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts) protected PublicKeyEntryResolverprotected NamedFactory<Mac> getHostValueDigester(ClientSession clientSession, SocketAddress remoteAddress, SshdSocketAddress hostIdentity) Invoked byprepareKnownHostEntry(ClientSession, SocketAddress, PublicKey)in order to query whether to use a hashed value instead of a plain one for the written host name/address - default returnsnull- i.e., no hashinggetKnownHostSupplier(ClientSession clientSession, Path file) protected voidhandleKnownHostsFileUpdateFailure(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey, Path file, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts, Throwable reason) Invoked whenupdateKnownHostsFile(ClientSession, SocketAddress, PublicKey, Path, Collection)fails - by default just issues a warning.protected voidhandleModifiedServerKeyUpdateFailure(ClientSession clientSession, SocketAddress remoteAddress, KnownHostsServerKeyVerifier.HostEntryPair match, PublicKey serverKey, Path file, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts, Throwable reason) Invoked if#updateModifiedServerKey(ClientSession, SocketAddress, HostEntryPair, PublicKey, Path)throws an exception.protected KnownHostEntryprepareKnownHostEntry(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey) Invoked byupdateKnownHostsFile(ClientSession, SocketAddress, PublicKey, Path, Collection)in order to generate the host entry to be writtenprotected StringprepareModifiedServerKeyLine(ClientSession clientSession, SocketAddress remoteAddress, KnownHostEntry entry, String curLine, PublicKey expected, PublicKey actual) Invoked byupdateModifiedServerKey(ClientSession, SocketAddress, HostEntryPair, PublicKey, Path, Collection)in order to prepare the replacement - by default it replaces the key part with the new oneprotected List<KnownHostsServerKeyVerifier.HostEntryPair> reloadKnownHosts(ClientSession session, Path file) protected PublicKeyresolveHostKey(ClientSession session, KnownHostEntry entry, PublicKeyEntryResolver resolver) Recover the associated public key from a known host entryprotected Collection<SshdSocketAddress> resolveHostNetworkIdentities(ClientSession clientSession, SocketAddress remoteAddress) Retrieves the host identities to be used when matching or updating an entry for it - by default returns the reported remote address and the original connection target host name/address (if same, then only one value is returned)protected voidvoidprotected KnownHostEntryupdateKnownHostsFile(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey, Path file, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts) Invoked if a new previously unknown host key has been accepted - by default appends a new entry at the end of the currently monitored known hosts fileprotected voidupdateModifiedServerKey(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts, KnownHostsServerKeyVerifier.HostEntryPair match) protected voidupdateModifiedServerKey(ClientSession clientSession, SocketAddress remoteAddress, KnownHostsServerKeyVerifier.HostEntryPair match, PublicKey actual, Path file, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts) Invoked if a matching host entry was found, but the key did not match andacceptModifiedServerKey(ClientSession, SocketAddress, KnownHostEntry, PublicKey, PublicKey)returnedtrue.booleanverifyServerKey(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey) Verify that the server key provided is really the one of the host.Methods inherited from class org.apache.sshd.common.util.io.ModifiableFileWatcher
checkReloadRequired, exists, getPath, lastModified, resetReloadAttributes, size, toPathResource, toPathResource, toString, updateReloadAttributes, validateStrictConfigFilePermissions
-
Field Details
-
STRICT_CHECKING_OPTION
Standard option used to indicate whether to use strict host key checking or not. Values may be "yes/no", "true/false" or "on/off"- See Also:
-
KNOWN_HOSTS_FILE_OPTION
Standard option used to indicate alternative known hosts file location- See Also:
-
updateLock
-
delegate
-
keysSupplier
private final AtomicReference<Supplier<? extends Collection<KnownHostsServerKeyVerifier.HostEntryPair>>> keysSupplier -
modKeyAcceptor
-
-
Constructor Details
-
KnownHostsServerKeyVerifier
-
KnownHostsServerKeyVerifier
-
-
Method Details
-
getDelegateVerifier
-
getModifiedServerKeyAcceptor
- Returns:
- The delegate
ModifiedServerKeyAcceptorto consult if a server presents a modified key. Ifnullthen assumed to reject such a modification
-
setModifiedServerKeyAcceptor
- Parameters:
acceptor- The delegateModifiedServerKeyAcceptorto consult if a server presents a modified key. Ifnullthen assumed to reject such a modification
-
verifyServerKey
public boolean verifyServerKey(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey) Description copied from interface:ServerKeyVerifierVerify that the server key provided is really the one of the host.- Specified by:
verifyServerKeyin interfaceServerKeyVerifier- Parameters:
clientSession- the currentClientSessionremoteAddress- the host'sSocketAddressserverKey- the presented serverPublicKey- Returns:
trueif the key is accepted for the host
-
getKnownHostSupplier
protected Supplier<Collection<KnownHostsServerKeyVerifier.HostEntryPair>> getKnownHostSupplier(ClientSession clientSession, Path file) -
setLoadedHostsEntries
-
reloadKnownHosts
protected List<KnownHostsServerKeyVerifier.HostEntryPair> reloadKnownHosts(ClientSession session, Path file) throws IOException, GeneralSecurityException - Parameters:
session- TheClientSessionthat triggered this requestfile- ThePathto reload from- Returns:
- A
Listof the loadedKnownHostsServerKeyVerifier.HostEntryPairs - may benull/empty - Throws:
IOException- If failed to parse the fileGeneralSecurityException- If failed to resolve the encoded public keys
-
resolveHostKey
protected PublicKey resolveHostKey(ClientSession session, KnownHostEntry entry, PublicKeyEntryResolver resolver) throws IOException, GeneralSecurityException Recover the associated public key from a known host entry- Parameters:
session- TheClientSessionthat triggered this requestentry- TheKnownHostEntry- ignored ifnullresolver- ThePublicKeyEntryResolverto use if immediate - decoding does not work - ignored ifnull- Returns:
- The extracted
PublicKey-nullif none - Throws:
IOException- If failed to decode the keyGeneralSecurityException- If failed to generate the key- See Also:
-
getFallbackPublicKeyEntryResolver
-
acceptKnownHostEntries
protected boolean acceptKnownHostEntries(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts) -
updateModifiedServerKey
protected void updateModifiedServerKey(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts, KnownHostsServerKeyVerifier.HostEntryPair match) -
updateModifiedServerKey
protected void updateModifiedServerKey(ClientSession clientSession, SocketAddress remoteAddress, KnownHostsServerKeyVerifier.HostEntryPair match, PublicKey actual, Path file, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts) throws Exception Invoked if a matching host entry was found, but the key did not match andacceptModifiedServerKey(ClientSession, SocketAddress, KnownHostEntry, PublicKey, PublicKey)returnedtrue. By default it locates the line to be updated and updates only its key data, marking the file for reload on next verification just to be on the safe side.- Parameters:
clientSession- TheClientSessionremoteAddress- The remote host addressmatch- TheKnownHostsServerKeyVerifier.HostEntryPairwhose key does not matchactual- The presented serverPublicKeyto be updatedfile- The filePathto be updatedknownHosts- The currently loaded entries- Throws:
Exception- If failed to update the file - Note: this may mean the file is now corrupted- See Also:
-
prepareModifiedServerKeyLine
protected String prepareModifiedServerKeyLine(ClientSession clientSession, SocketAddress remoteAddress, KnownHostEntry entry, String curLine, PublicKey expected, PublicKey actual) throws Exception Invoked byupdateModifiedServerKey(ClientSession, SocketAddress, HostEntryPair, PublicKey, Path, Collection)in order to prepare the replacement - by default it replaces the key part with the new one- Parameters:
clientSession- TheClientSessionremoteAddress- The remote host addressentry- TheKnownHostEntrycurLine- The current entry line dataexpected- The expectedPublicKeyactual- The present key to be update- Returns:
- The updated line - ignored if
null/empty or same as original one - Throws:
Exception- if failed to prepare the line
-
handleModifiedServerKeyUpdateFailure
protected void handleModifiedServerKeyUpdateFailure(ClientSession clientSession, SocketAddress remoteAddress, KnownHostsServerKeyVerifier.HostEntryPair match, PublicKey serverKey, Path file, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts, Throwable reason) Invoked if#updateModifiedServerKey(ClientSession, SocketAddress, HostEntryPair, PublicKey, Path)throws an exception. This may mean the file is corrupted, but it can be recovered from the known hosts that are being provided. By default, it only logs a warning and does not attempt to recover the file- Parameters:
clientSession- TheClientSessionremoteAddress- The remote host addressmatch- TheKnownHostsServerKeyVerifier.HostEntryPairwhose key does not matchserverKey- The presented serverPublicKeyto be updatedfile- The filePathto be updatedknownHosts- The currently cached entries (may benull/empty)reason- The failure reason
-
findKnownHostEntries
protected List<KnownHostsServerKeyVerifier.HostEntryPair> findKnownHostEntries(ClientSession clientSession, SocketAddress remoteAddress, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts) -
acceptIncompleteHostKeys
protected boolean acceptIncompleteHostKeys(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey, Throwable reason) Called if failed to reload known hosts - by default invokesacceptUnknownHostKey(ClientSession, SocketAddress, PublicKey)- Parameters:
clientSession- TheClientSessionremoteAddress- The remote host addressserverKey- The presented serverPublicKeyreason- TheThrowablethat indicates the reload failure- Returns:
trueif accept the server key anyway- See Also:
-
acceptUnknownHostKey
protected boolean acceptUnknownHostKey(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey) Invoked if none of the known hosts matches the current one - by default invokes the delegate. If the delegate accepts the key, then it is appended to the currently monitored entries and the file is updated- Parameters:
clientSession- TheClientSessionremoteAddress- The remote host addressserverKey- The presented serverPublicKey- Returns:
trueif accept the server key- See Also:
-
handleKnownHostsFileUpdateFailure
protected void handleKnownHostsFileUpdateFailure(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey, Path file, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts, Throwable reason) Invoked whenupdateKnownHostsFile(ClientSession, SocketAddress, PublicKey, Path, Collection)fails - by default just issues a warning. Note: there is a chance that the file is now corrupted and cannot be re-used, so we provide a way to recover it via overriding this method and using the cached entries to re-created it.- Parameters:
clientSession- TheClientSessionremoteAddress- The remote host addressserverKey- The serverPublicKeythat was attempted to updatefile- The filePathto be updatedknownHosts- The currently known entries (may benull/emptyreason- The failure reason
-
updateKnownHostsFile
protected KnownHostEntry updateKnownHostsFile(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey, Path file, Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts) throws Exception Invoked if a new previously unknown host key has been accepted - by default appends a new entry at the end of the currently monitored known hosts file- Parameters:
clientSession- TheClientSessionremoteAddress- The remote host addressserverKey- The serverPublicKeythat to updatefile- The filePathto be updatedknownHosts- The currently cached entries (may benull/empty)- Returns:
- The generated
KnownHostEntryornullif nothing updated. If anything updated then the file will be re-loaded on next verification regardless of which server is verified - Throws:
Exception- If failed to update the file - Note: in this case the file may be corrupted sohandleKnownHostsFileUpdateFailure(ClientSession, SocketAddress, PublicKey, Path, Collection, Throwable)will be called in order to enable recovery of its data- See Also:
-
prepareKnownHostEntry
protected KnownHostEntry prepareKnownHostEntry(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey) throws Exception Invoked byupdateKnownHostsFile(ClientSession, SocketAddress, PublicKey, Path, Collection)in order to generate the host entry to be written- Parameters:
clientSession- TheClientSessionremoteAddress- The remote host addressserverKey- The serverPublicKeythat was attempted to update- Returns:
- The
KnownHostEntryto use - ifnullthen entry is not updated in the file - Throws:
Exception- If failed to generate the entry - e.g. failed to hash- See Also:
-
getHostValueDigester
protected NamedFactory<Mac> getHostValueDigester(ClientSession clientSession, SocketAddress remoteAddress, SshdSocketAddress hostIdentity) Invoked byprepareKnownHostEntry(ClientSession, SocketAddress, PublicKey)in order to query whether to use a hashed value instead of a plain one for the written host name/address - default returnsnull- i.e., no hashing- Parameters:
clientSession- TheClientSessionremoteAddress- The remote host addresshostIdentity- The entry's host name/address- Returns:
- The digester
NamedFactory-nullif no hashing is to be made
-
resolveHostNetworkIdentities
protected Collection<SshdSocketAddress> resolveHostNetworkIdentities(ClientSession clientSession, SocketAddress remoteAddress) Retrieves the host identities to be used when matching or updating an entry for it - by default returns the reported remote address and the original connection target host name/address (if same, then only one value is returned)- Parameters:
clientSession- TheClientSessionremoteAddress- The remote host address- Returns:
- A
Collectionof theInetSocketAddress-es to use - ifnull/empty then ignored (i.e., no matching is done or no entry is generated) - See Also:
-
acceptModifiedServerKey
public boolean acceptModifiedServerKey(ClientSession clientSession, SocketAddress remoteAddress, KnownHostEntry entry, PublicKey expected, PublicKey actual) throws Exception Description copied from interface:ModifiedServerKeyAcceptorInvoked when a matching known host key was found but it does not match the presented one.- Specified by:
acceptModifiedServerKeyin interfaceModifiedServerKeyAcceptor- Parameters:
clientSession- TheClientSessionremoteAddress- The remote host addressentry- Any originalKnownHostEntrywhose key did not matchexpected- Any expected serverPublicKeyactual- The presented serverPublicKey- Returns:
trueif accept the server key anyway- Throws:
Exception- if cannot process the request - equivalent tofalsereturn value
-