Top Description Inners Fields Constructors Methods
org.eclipse.jgit.internal.transport.sshd

public Class OpenSshServerKeyDatabase

extends Object
implements ServerKeyDatabase
Class Inheritance
All Implemented Interfaces
org.eclipse.jgit.transport.sshd.ServerKeyDatabase
Static Imports
java.nio.charset.StandardCharsets.UTF_8, java.text.MessageFormat.format

A sever host key verifier that honors the StrictHostKeyChecking and UserKnownHostsFile values from the ssh configuration.

The verifier can be given default known_hosts files in the constructor, which will be used if the ssh config does not specify a UserKnownHostsFile. If the ssh config does set UserKnownHostsFile, the verifier uses the given files in the order given. Non-existing or unreadable files are ignored.

StrictHostKeyChecking accepts the following values:

ask
Ask the user whether new or changed keys shall be accepted and be added to the known_hosts file.
yes/true
Accept only keys listed in the known_hosts file.
no/false
Silently accept all new or changed keys, add new keys to the known_hosts file.
accept-new
Silently accept keys for new hosts and add them to the known_hosts file.

If StrictHostKeyChecking is not set, or set to any other value, the default value ask is active.

This implementation relies on the ClientSession being a JGitClientSession. By default Apache MINA sshd does not forward the config file host entry to the session, so it would be unknown here which entry it was and what setting of StrictHostKeyChecking should be used. If used with some other session type, the implementation assumes "ask".

Asking the user is done via a CredentialsProvider obtained from the session. If none is set, the implementation falls back to strict host key checking ("yes").

Note that adding a key to the known hosts file may create the file. You can specify in the constructor whether the user shall be asked about that, too. If the user declines updating the file, but the key was otherwise accepted (user confirmed for "ask", or "no" or "accept-new" are active), the key is accepted for this session only.

If several known hosts files are specified, a new key is always added to the first file (even if it doesn't exist yet; see the note about file creation above).

See Also
man ssh-config

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private static class

Field Summary

Modifier and TypeField and Description
private final boolean
private final List<OpenSshServerKeyDatabase.HostKeyFile>
private final Map<Path, OpenSshServerKeyDatabase.HostKeyFile>
private static final Logger
private static final String
MARKER_CA

Marks CA keys used for SSH certificates.

private static final String
MARKER_REVOKED

Can be used to mark revoked known host lines.

private Random

Constructor Summary

AccessConstructor and Description
public
OpenSshServerKeyDatabase(boolean
whether to ask the user, if possible, about creating a new non-existing known_hosts file
askAboutNewFile
,
List<Path>
typically ~/.ssh/known_hosts and ~/.ssh/known_hosts2. May be empty or null, in which case no default files are installed. The files need not exist.
defaultFiles
)

Creates a new OpenSshServerKeyDatabase.

Method Summary

Modifier and TypeMethod and Description
public boolean
accept(String
IP address the session tried to connect to
connectAddress
,
InetSocketAddress
IP address as reported for the remote end point
remoteAddress
,
PublicKey
received from the remote end
serverKey
,
ServerKeyDatabase.Configuration
giving access to potentially interesting configuration settings
config
,
CredentialsProvider
for interacting with the user, if required; may be null
provider
)

Implements org.eclipse.jgit.transport.sshd.ServerKeyDatabase.accept.

Determines whether to accept a received server host key.

private void
private List<OpenSshServerKeyDatabase.HostKeyFile>
private String
private boolean
private Collection<SshdSocketAddress>
getCandidates(String connectAddress, InetSocketAddress remoteAddress)

private List<OpenSshServerKeyDatabase.HostKeyFile>
private static boolean
isBetterMatch(String keyType, String thisType, String modifiedType)

private static boolean
private static boolean
public List<PublicKey>
lookup(String
IP address the session tried to connect to
connectAddress
,
InetSocketAddress
IP address as reported for the remote end point
remoteAddress
,
ServerKeyDatabase.Configuration
giving access to potentially interesting configuration settings
config
)

Implements org.eclipse.jgit.transport.sshd.ServerKeyDatabase.lookup.

Retrieves all known and not revoked host keys for the given addresses.

private int
private SshdSocketAddress
private String
private void
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

askAboutNewFileback to summary
private final boolean askAboutNewFile
defaultFilesback to summary
private final List<OpenSshServerKeyDatabase.HostKeyFile> defaultFiles
knownHostsFilesback to summary
private final Map<Path, OpenSshServerKeyDatabase.HostKeyFile> knownHostsFiles
LOGback to summary
private static final Logger LOG
MARKER_CAback to summary
private static final String MARKER_CA

Marks CA keys used for SSH certificates.

MARKER_REVOKEDback to summary
private static final String MARKER_REVOKED

Can be used to mark revoked known host lines.

prngback to summary
private Random prng

Constructor Detail

OpenSshServerKeyDatabaseback to summary
public OpenSshServerKeyDatabase(boolean askAboutNewFile, List<Path> defaultFiles)

Creates a new OpenSshServerKeyDatabase.

Parameters
askAboutNewFile:boolean

whether to ask the user, if possible, about creating a new non-existing known_hosts file

defaultFiles:List<Path>

typically ~/.ssh/known_hosts and ~/.ssh/known_hosts2. May be empty or null, in which case no default files are installed. The files need not exist.

Method Detail

acceptback to summary
public boolean accept(String connectAddress, InetSocketAddress remoteAddress, PublicKey serverKey, ServerKeyDatabase.Configuration config, CredentialsProvider provider)

Implements org.eclipse.jgit.transport.sshd.ServerKeyDatabase.accept.

Doc from org.eclipse.jgit.transport.sshd.ServerKeyDatabase.accept.

Determines whether to accept a received server host key.

Parameters
connectAddress:String

IP address the session tried to connect to

remoteAddress:InetSocketAddress

IP address as reported for the remote end point

serverKey:PublicKey

received from the remote end

config:ServerKeyDatabase.Configuration

giving access to potentially interesting configuration settings

provider:CredentialsProvider

for interacting with the user, if required; may be null

Returns:boolean

true if the serverKey is accepted, false otherwise

Annotations
@Override
addKeyToFileback to summary
private void addKeyToFile(OpenSshServerKeyDatabase.HostKeyFile file, Collection<SshdSocketAddress> candidates, PublicKey serverKey, OpenSshServerKeyDatabase.AskUser ask, ServerKeyDatabase.Configuration config)
addUserHostKeyFilesback to summary
private List<OpenSshServerKeyDatabase.HostKeyFile> addUserHostKeyFiles(List<String> fileNames)
createHostKeyLineback to summary
private String createHostKeyLine(Collection<SshdSocketAddress> patterns, PublicKey key, ServerKeyDatabase.Configuration config) throws Exception
findback to summary
private boolean find(Collection<SshdSocketAddress> candidates, PublicKey serverKey, List<KnownHostsServerKeyVerifier.HostEntryPair> entries, KnownHostsServerKeyVerifier.HostEntryPair[] modified) throws RevokedKeyException
getCandidatesback to summary
private Collection<SshdSocketAddress> getCandidates(String connectAddress, InetSocketAddress remoteAddress)
getFilesToUseback to summary
private List<OpenSshServerKeyDatabase.HostKeyFile> getFilesToUse(ServerKeyDatabase.Configuration config)
isBetterMatchback to summary
private static boolean isBetterMatch(String keyType, String thisType, String modifiedType)
isCertificateAuthorityback to summary
private static boolean isCertificateAuthority(KnownHostEntry entry)
isRevokedback to summary
private static boolean isRevoked(KnownHostEntry entry)
lookupback to summary
public List<PublicKey> lookup(String connectAddress, InetSocketAddress remoteAddress, ServerKeyDatabase.Configuration config)

Implements org.eclipse.jgit.transport.sshd.ServerKeyDatabase.lookup.

Doc from org.eclipse.jgit.transport.sshd.ServerKeyDatabase.lookup.

Retrieves all known and not revoked host keys for the given addresses.

Parameters
connectAddress:String

IP address the session tried to connect to

remoteAddress:InetSocketAddress

IP address as reported for the remote end point

config:ServerKeyDatabase.Configuration

giving access to potentially interesting configuration settings

Returns:List<PublicKey>

the list of known and not revoked keys for the given addresses

Annotations
@Override
parsePortback to summary
private int parsePort(String s)
toSshdSocketAddressback to summary
private SshdSocketAddress toSshdSocketAddress(String address)
updateHostKeyLineback to summary
private String updateHostKeyLine(String line, PublicKey newKey) throws IOException
updateKnownHostsFileback to summary
private void updateKnownHostsFile(Collection<SshdSocketAddress> candidates, PublicKey serverKey, Path path, ServerKeyDatabase.Configuration config) throws Exception
updateModifiedServerKeyback to summary
private void updateModifiedServerKey(PublicKey serverKey, KnownHostsServerKeyVerifier.HostEntryPair entry, Path path) throws IOException
org.eclipse.jgit.internal.transport.sshd back to summary

private Class OpenSshServerKeyDatabase.AskUser

extends Object
Class Inheritance
  • java.lang.Object
  • org.eclipse.jgit.internal.transport.sshd.OpenSshServerKeyDatabase.AskUser

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum
public static enum

Field Summary

Modifier and TypeField and Description
private final ServerKeyDatabase.Configuration
private final CredentialsProvider

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public OpenSshServerKeyDatabase.AskUser.ModifiedKeyHandling
acceptModifiedServerKey(InetSocketAddress remoteAddress, PublicKey expected, PublicKey actual, Path path)

public boolean
acceptUnknownKey(SocketAddress remoteAddress, PublicKey serverKey)

private static boolean
askUser(CredentialsProvider provider, URIish uri, String prompt, String... messages)

private OpenSshServerKeyDatabase.AskUser.Check
checkMode(SocketAddress remoteAddress, boolean changed)

public boolean
public void
revokedKey(SocketAddress remoteAddress, PublicKey serverKey, Path path)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

configback to summary
private final ServerKeyDatabase.Configuration config
Annotations
@NonNull
providerback to summary
private final CredentialsProvider provider

Constructor Detail

AskUserback to summary
public AskUser(ServerKeyDatabase.Configuration config, CredentialsProvider provider)

Method Detail

acceptModifiedServerKeyback to summary
public OpenSshServerKeyDatabase.AskUser.ModifiedKeyHandling acceptModifiedServerKey(InetSocketAddress remoteAddress, PublicKey expected, PublicKey actual, Path path)
acceptUnknownKeyback to summary
public boolean acceptUnknownKey(SocketAddress remoteAddress, PublicKey serverKey)
askUserback to summary
private static boolean askUser(CredentialsProvider provider, URIish uri, String prompt, String... messages)
checkModeback to summary
private OpenSshServerKeyDatabase.AskUser.Check checkMode(SocketAddress remoteAddress, boolean changed)
createNewFileback to summary
public boolean createNewFile(Path path)
revokedKeyback to summary
public void revokedKey(SocketAddress remoteAddress, PublicKey serverKey, Path path)
org.eclipse.jgit.internal.transport.sshd back to summary

private final Enum OpenSshServerKeyDatabase.AskUser.Check

extends Enum<OpenSshServerKeyDatabase.AskUser.Check>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final OpenSshServerKeyDatabase.AskUser.Check
public static final OpenSshServerKeyDatabase.AskUser.Check
public static final OpenSshServerKeyDatabase.AskUser.Check

Constructor Summary

AccessConstructor and Description
private

Field Detail

ALLOWback to summary
public static final OpenSshServerKeyDatabase.AskUser.Check ALLOW
ASKback to summary
public static final OpenSshServerKeyDatabase.AskUser.Check ASK
DENYback to summary
public static final OpenSshServerKeyDatabase.AskUser.Check DENY

Constructor Detail

Checkback to summary
private Check()

Method Detail

valueOfback to summary
public static OpenSshServerKeyDatabase.AskUser.Check valueOf(String name)
valuesback to summary
public static OpenSshServerKeyDatabase.AskUser.Check[] values()
org.eclipse.jgit.internal.transport.sshd back to summary

public final Enum OpenSshServerKeyDatabase.AskUser.ModifiedKeyHandling

extends Enum<OpenSshServerKeyDatabase.AskUser.ModifiedKeyHandling>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final OpenSshServerKeyDatabase.AskUser.ModifiedKeyHandling
public static final OpenSshServerKeyDatabase.AskUser.ModifiedKeyHandling
public static final OpenSshServerKeyDatabase.AskUser.ModifiedKeyHandling

Constructor Summary

AccessConstructor and Description
private

Field Detail

ALLOWback to summary
public static final OpenSshServerKeyDatabase.AskUser.ModifiedKeyHandling ALLOW
ALLOW_AND_STOREback to summary
public static final OpenSshServerKeyDatabase.AskUser.ModifiedKeyHandling ALLOW_AND_STORE
DENYback to summary
public static final OpenSshServerKeyDatabase.AskUser.ModifiedKeyHandling DENY

Constructor Detail

ModifiedKeyHandlingback to summary
private ModifiedKeyHandling()
org.eclipse.jgit.internal.transport.sshd back to summary

private Class OpenSshServerKeyDatabase.HostKeyFile

extends ModifiableFileWatcher
implements Supplier<List<KnownHostsServerKeyVerifier.HostEntryPair>>
Class Inheritance
All Implemented Interfaces
java.util.function.Supplier

Field Summary

Modifier and TypeField and Description
private List<KnownHostsServerKeyVerifier.HostEntryPair>
Inherited from org.apache.sshd.common.util.io.ModifiableFileWatcher:
optionsSTRICTLY_PROHIBITED_FILE_PERMISSION

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public List<KnownHostsServerKeyVerifier.HostEntryPair>
get()

Implements java.util.function.Supplier.get.

Gets a result.

private List<KnownHostsServerKeyVerifier.HostEntryPair>
reload(Path path)

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

Field Detail

entriesback to summary
private List<KnownHostsServerKeyVerifier.HostEntryPair> entries

Constructor Detail

HostKeyFileback to summary
public HostKeyFile(Path path)

Method Detail

getback to summary
public List<KnownHostsServerKeyVerifier.HostEntryPair> get()

Implements java.util.function.Supplier.get.

Doc from java.util.function.Supplier.get.

Gets a result.

Returns:List<KnownHostsServerKeyVerifier.HostEntryPair>

a result

Annotations
@Override
reloadback to summary
private List<KnownHostsServerKeyVerifier.HostEntryPair> reload(Path path) throws IOException
org.eclipse.jgit.internal.transport.sshd back to summary

private Class OpenSshServerKeyDatabase.RevokedKeyException

extends Exception
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
private

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.lang.Exception.serialVersionUID.

Constructor Detail

RevokedKeyExceptionback to summary
private RevokedKeyException()