Top Inners Fields Constructors Methods
org.apache.sshd.client.keyverifier

public Class KnownHostsServerKeyVerifier

extends ModifiableFileWatcher
implements ServerKeyVerifier, ModifiedServerKeyAcceptor
Class Inheritance
All Implemented Interfaces
org.apache.sshd.client.keyverifier.ModifiedServerKeyAcceptor, org.apache.sshd.client.keyverifier.ServerKeyVerifier
Known Direct Subclasses
org.apache.sshd.client.keyverifier.DefaultKnownHostsServerKeyVerifier
Imports
java.io.BufferedReader, .IOException, .OutputStream, .Writer, java.net.SocketAddress, java.nio.charset.StandardCharsets, java.nio.file.Files, .LinkOption, .Path, .StandardOpenOption, java.security.GeneralSecurityException, .PublicKey, java.util.ArrayList, .Collection, .Collections, .List, .Objects, .Optional, .TreeSet, java.util.concurrent.atomic.AtomicReference, java.util.function.Supplier, java.util.stream.Collectors, org.apache.sshd.client.config.hosts.KnownHostEntry, .KnownHostHashValue, org.apache.sshd.client.session.ClientSession, org.apache.sshd.common.Factory, .FactoryManager, .NamedFactory, org.apache.sshd.common.config.ConfigFileReaderSupport, org.apache.sshd.common.config.keys.AuthorizedKeyEntry, .KeyUtils, .PublicKeyEntry, .PublicKeyEntryResolver, .UnsupportedSshPublicKey, org.apache.sshd.common.mac.Mac, org.apache.sshd.common.random.Random, org.apache.sshd.common.session.SessionContext, org.apache.sshd.common.util.GenericUtils, .ValidateUtils, org.apache.sshd.common.util.io.IoUtils, .ModifiableFileWatcher, org.apache.sshd.common.util.net.SshdSocketAddress

Author
Apache MINA SSHD Project

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
KnownHostsServerKeyVerifier.HostEntryPair

Represents an entry in the internal verifier's cache

Field Summary

Modifier and TypeField and Description
private final ServerKeyVerifier
private final AtomicReference<Supplier<? extends Collection<KnownHostsServerKeyVerifier.HostEntryPair>>>
public static final String
KNOWN_HOSTS_FILE_OPTION

Standard option used to indicate alternative known hosts file location

private ModifiedServerKeyAcceptor
public static final String
STRICT_CHECKING_OPTION

Standard option used to indicate whether to use strict host key checking or not.

protected final Object
Inherited from org.apache.sshd.common.util.io.ModifiableFileWatcher:
optionsSTRICTLY_PROHIBITED_FILE_PERMISSION

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Modifier and TypeMethod and Description
protected boolean

Returns:

true if accept the server key anyway
acceptIncompleteHostKeys
(ClientSession clientSession, SocketAddress
The remote host address
remoteAddress
,
PublicKey
The presented server PublicKey
serverKey
,
Throwable
The Throwable that indicates the reload failure
reason
)

Called if failed to reload known hosts - by default invokes acceptUnknownHostKey(ClientSession, SocketAddress, PublicKey)

protected boolean
public boolean
acceptModifiedServerKey(ClientSession clientSession, SocketAddress
The remote host address
remoteAddress
,
KnownHostEntry
Any original KnownHostEntry whose key did not match
entry
,
PublicKey
Any expected server PublicKey
expected
,
PublicKey
The presented server PublicKey
actual
)

Implements org.apache.sshd.client.keyverifier.ModifiedServerKeyAcceptor.acceptModifiedServerKey.

Invoked when a matching known host key was found but it does not match the presented one.

protected boolean

Returns:

true if accept the server key
acceptUnknownHostKey
(ClientSession clientSession, SocketAddress
The remote host address
remoteAddress
,
PublicKey
The presented server PublicKey
serverKey
)

Invoked if none of the known hosts matches the current one - by default invokes the delegate.

protected List<KnownHostsServerKeyVerifier.HostEntryPair>
public ServerKeyVerifier
protected PublicKeyEntryResolver
protected NamedFactory<Mac>

Returns:

The digester NamedFactory - null if no hashing is to be made
getHostValueDigester
(ClientSession clientSession, SocketAddress
The remote host address
remoteAddress
,
SshdSocketAddress
The entry's host name/address
hostIdentity
)

Invoked by prepareKnownHostEntry(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 returns null - i.e., no hashing

protected Supplier<Collection<KnownHostsServerKeyVerifier.HostEntryPair>>
public ModifiedServerKeyAcceptor

Returns:

The delegate ModifiedServerKeyAcceptor to consult if a server presents a modified key. If null then assumed to reject such a modification
getModifiedServerKeyAcceptor
()

protected void
handleKnownHostsFileUpdateFailure(ClientSession clientSession, SocketAddress
The remote host address
remoteAddress
,
PublicKey
The server PublicKey that was attempted to update
serverKey
,
Path
The file Path to be updated
file
,
Collection<KnownHostsServerKeyVerifier.HostEntryPair>
The currently known entries (may be null/empty
knownHosts
,
Throwable
The failure reason
reason
)

Invoked when updateKnownHostsFile(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.

protected void
handleModifiedServerKeyUpdateFailure(ClientSession clientSession, SocketAddress
The remote host address
remoteAddress
,
KnownHostsServerKeyVerifier.HostEntryPair
The HostEntryPair whose key does not match
match
,
PublicKey
The presented server PublicKey to be updated
serverKey
,
Path
The file Path to be updated
file
,
Collection<KnownHostsServerKeyVerifier.HostEntryPair>
The currently cached entries (may be null/empty)
knownHosts
,
Throwable
The failure reason
reason
)

Invoked if #updateModifiedServerKey(ClientSession, SocketAddress, HostEntryPair, PublicKey, Path) throws an exception.

protected void
handleRevokedKey(ClientSession clientSession, SocketAddress
The remote host address
remoteAddress
,
PublicKey
The presented server PublicKey
serverKey
)

Invoked if any matching host entry has a 'revoked' marker

protected KnownHostEntry

Returns:

The KnownHostEntry to use - if null then entry is not updated in the file
prepareKnownHostEntry
(ClientSession clientSession, SocketAddress
The remote host address
remoteAddress
,
PublicKey
The server PublicKey that was attempted to update
serverKey
)

Invoked by updateKnownHostsFile(ClientSession, SocketAddress, PublicKey, Path, Collection) in order to generate the host entry to be written

protected String

Returns:

The updated line - ignored if null/empty or same as original one
prepareModifiedServerKeyLine
(ClientSession clientSession, SocketAddress
The remote host address
remoteAddress
,
KnownHostEntry entry, String
The current entry line data
curLine
,
PublicKey
The expected PublicKey
expected
,
PublicKey
The present key to be update
actual
)

Invoked by updateModifiedServerKey(ClientSession, SocketAddress, HostEntryPair, PublicKey, Path, Collection) in order to prepare the replacement - by default it replaces the key part with the new one

protected List<KnownHostsServerKeyVerifier.HostEntryPair>

Returns:

A List of the loaded HostEntryPairs - may be null/empty
reloadKnownHosts
(ClientSession
The ClientSession that triggered this request
session
,
Path
The Path to reload from
file
)

protected PublicKey

Returns:

The extracted PublicKey - null if none
resolveHostKey
(ClientSession
The ClientSession that triggered this request
session
,
KnownHostEntry
The KnownHostEntry - ignored if null
entry
,
PublicKeyEntryResolver
The PublicKeyEntryResolver to use if immediate - decoding does not work - ignored if null
resolver
)

Recover the associated public key from a known host entry

protected Collection<SshdSocketAddress>

Returns:

A Collection of the InetSocketAddress-es to use - if null/empty then ignored (i.e., no matching is done or no entry is generated)
resolveHostNetworkIdentities
(ClientSession clientSession, SocketAddress
The remote host address
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 void
public void
setModifiedServerKeyAcceptor(ModifiedServerKeyAcceptor
The delegate ModifiedServerKeyAcceptor to consult if a server presents a modified key. If null then assumed to reject such a modification
acceptor
)

protected KnownHostEntry

Returns:

The generated KnownHostEntry or null if nothing updated. If anything updated then the file will be re-loaded on next verification regardless of which server is verified
updateKnownHostsFile
(ClientSession clientSession, SocketAddress
The remote host address
remoteAddress
,
PublicKey
The server PublicKey that to update
serverKey
,
Path
The file Path to be updated
file
,
Collection<KnownHostsServerKeyVerifier.HostEntryPair>
The currently cached entries (may be null/empty)
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 file

protected void
protected void
updateModifiedServerKey(ClientSession clientSession, SocketAddress
The remote host address
remoteAddress
,
KnownHostsServerKeyVerifier.HostEntryPair
The HostEntryPair whose key does not match
match
,
PublicKey
The presented server PublicKey to be updated
actual
,
Path
The file Path to be updated
file
,
Collection<KnownHostsServerKeyVerifier.HostEntryPair>
The currently loaded entries
knownHosts
)

Invoked if a matching host entry was found, but the key did not match and acceptModifiedServerKey(ClientSession, SocketAddress, KnownHostEntry, PublicKey, PublicKey) returned true.

public boolean
verifyServerKey(ClientSession
the current ClientSession
clientSession
,
SocketAddress
the host's SocketAddress
remoteAddress
,
PublicKey
the presented server PublicKey
serverKey
)

Implements org.apache.sshd.client.keyverifier.ServerKeyVerifier.verifyServerKey.

Verify that the server key provided is really the one of the host.

Inherited from org.apache.sshd.common.util.io.ModifiableFileWatcher:
checkReloadRequiredexistsgetPathlastModifiedresetReloadAttributessizetoPathResourcetoPathResourcetoStringupdateReloadAttributesvalidateStrictConfigFilePermissions