mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-07 15:50:57 +03:00
Add support for verifying multiple host entries (Fixes #405)
This commit is contained in:
@@ -460,10 +460,13 @@ public class Buffer<T extends Buffer<T>> {
|
||||
|
||||
public PublicKey readPublicKey()
|
||||
throws BufferException {
|
||||
KeyType keyType = KeyType.fromString(readString());
|
||||
try {
|
||||
return KeyType.fromString(readString()).readPubKeyFromBuffer(this);
|
||||
return keyType.readPubKeyFromBuffer(this);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new SSHRuntimeException(e);
|
||||
} catch (UnsupportedOperationException uoe) {
|
||||
throw new BufferException("Could not decode keytype " + keyType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ public class ConsoleKnownHostsVerifier
|
||||
protected boolean hostKeyUnverifiableAction(String hostname, PublicKey key) {
|
||||
final KeyType type = KeyType.fromKey(key);
|
||||
console.printf("The authenticity of host '%s' can't be established.\n" +
|
||||
"%s key fingerprint is %s.\n", hostname, type, SecurityUtils.getFingerprint(key));
|
||||
"%s key fingerprint is %s.\n", hostname, type, SecurityUtils.getFingerprint(key));
|
||||
String response = console.readLine("Are you sure you want to continue connecting (yes/no)? ");
|
||||
while (!(response.equalsIgnoreCase(YES) || response.equalsIgnoreCase(NO))) {
|
||||
response = console.readLine("Please explicitly enter yes/no: ");
|
||||
@@ -60,7 +60,7 @@ public class ConsoleKnownHostsVerifier
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean hostKeyChangedAction(KnownHostEntry entry, String hostname, PublicKey key) {
|
||||
protected boolean hostKeyChangedAction(String hostname, PublicKey key) {
|
||||
final KeyType type = KeyType.fromKey(key);
|
||||
final String fp = SecurityUtils.getFingerprint(key);
|
||||
final String path = getFile().getAbsolutePath();
|
||||
|
||||
@@ -87,14 +87,23 @@ public class OpenSSHKnownHosts
|
||||
|
||||
final String adjustedHostname = (port != 22) ? "[" + hostname + "]:" + port : hostname;
|
||||
|
||||
boolean foundApplicableHostEntry = false;
|
||||
for (KnownHostEntry e : entries) {
|
||||
try {
|
||||
if (e.appliesTo(type, adjustedHostname))
|
||||
return e.verify(key) || hostKeyChangedAction(e, adjustedHostname, key);
|
||||
if (e.appliesTo(type, adjustedHostname)) {
|
||||
foundApplicableHostEntry = true;
|
||||
if (e.verify(key)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
log.error("Error with {}: {}", e, ioe);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
if (foundApplicableHostEntry) {
|
||||
return hostKeyChangedAction(adjustedHostname, key);
|
||||
}
|
||||
|
||||
return hostKeyUnverifiableAction(adjustedHostname, key);
|
||||
@@ -104,7 +113,7 @@ public class OpenSSHKnownHosts
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean hostKeyChangedAction(KnownHostEntry entry, String hostname, PublicKey key) {
|
||||
protected boolean hostKeyChangedAction(String hostname, PublicKey key) {
|
||||
log.warn("Host key for `{}` has changed!", hostname);
|
||||
return false;
|
||||
}
|
||||
@@ -199,7 +208,7 @@ public class OpenSSHKnownHosts
|
||||
}
|
||||
if(split.length < 3) {
|
||||
log.error("Error reading entry `{}`", line);
|
||||
return null;
|
||||
return new BadHostEntry(line);
|
||||
}
|
||||
final String hostnames = split[i++];
|
||||
final String sType = split[i++];
|
||||
@@ -209,7 +218,13 @@ public class OpenSSHKnownHosts
|
||||
|
||||
if (type != KeyType.UNKNOWN) {
|
||||
final String sKey = split[i++];
|
||||
key = new Buffer.PlainBuffer(Base64.decode(sKey)).readPublicKey();
|
||||
try {
|
||||
byte[] keyBytes = Base64.decode(sKey);
|
||||
key = new Buffer.PlainBuffer(keyBytes).readPublicKey();
|
||||
} catch (IOException ioe) {
|
||||
log.warn("Error decoding Base64 key bytes", ioe);
|
||||
return new BadHostEntry(line);
|
||||
}
|
||||
} else if (isBits(sType)) {
|
||||
type = KeyType.RSA;
|
||||
// int bits = Integer.valueOf(sType);
|
||||
@@ -220,11 +235,11 @@ public class OpenSSHKnownHosts
|
||||
key = keyFactory.generatePublic(new RSAPublicKeySpec(n, e));
|
||||
} catch (Exception ex) {
|
||||
log.error("Error reading entry `{}`, could not create key", line, ex);
|
||||
return null;
|
||||
return new BadHostEntry(line);
|
||||
}
|
||||
} else {
|
||||
log.error("Error reading entry `{}`, could not determine type", line);
|
||||
return null;
|
||||
return new BadHostEntry(line);
|
||||
}
|
||||
|
||||
return new HostEntry(marker, hostnames, type, key);
|
||||
@@ -364,6 +379,44 @@ public class OpenSSHKnownHosts
|
||||
}
|
||||
}
|
||||
|
||||
public static class BadHostEntry implements KnownHostEntry {
|
||||
private String line;
|
||||
|
||||
public BadHostEntry(String line) {
|
||||
this.line = line;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyType getType() {
|
||||
return KeyType.UNKNOWN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFingerprint() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean appliesTo(String host) throws IOException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean appliesTo(KeyType type, String host) throws IOException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean verify(PublicKey key) throws IOException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLine() {
|
||||
return line;
|
||||
}
|
||||
}
|
||||
|
||||
public enum Marker {
|
||||
CA_CERT("@cert-authority"),
|
||||
REVOKED("@revoked");
|
||||
|
||||
Reference in New Issue
Block a user