Forgive redundant spaces in OpenSSHv2 public keys and known_hosts (#524)

* Forgive redundant spaces in OpenSSHv2 public keys and known_hosts

Sometimes users copy-pastes private and public keys in text editors. It leads to redundant spaces
and newlines. OpenSSH can easily read such keys, so users expect from SSHJ the same.

* Fixed bugs in OpenSSH key file and known_hosts parsers

* OpenSSHKnownHosts should not throw errors while parsing corrupted records
This commit is contained in:
Vladimir Lagunov
2019-09-18 20:14:45 +07:00
committed by Jeroen van Erp
parent d5c045defd
commit 327a4c4c5b
4 changed files with 116 additions and 8 deletions

View File

@@ -199,14 +199,21 @@ public class OpenSSHKnownHosts
return new CommentEntry(line);
}
final String[] split = line.split(" ");
final String[] split = line.split("\\s+");
if(split.length < 3) {
log.error("Error reading entry `{}`", line);
return new BadHostEntry(line);
}
int i = 0;
if (split[i].isEmpty()) {
i++;
}
final Marker marker = Marker.fromString(split[i]);
if (marker != null) {
i++;
}
if(split.length < 3) {
if(split.length < i + 3) {
log.error("Error reading entry `{}`", line);
return new BadHostEntry(line);
}

View File

@@ -93,13 +93,21 @@ public class OpenSSHKeyFile
private void initPubKey(Reader publicKey) throws IOException {
final BufferedReader br = new BufferedReader(publicKey);
try {
final String keydata = br.readLine();
if (keydata != null) {
String[] parts = keydata.trim().split(" ");
assert parts.length >= 2;
type = KeyType.fromString(parts[0]);
pubKey = new Buffer.PlainBuffer(Base64.decode(parts[1])).readPublicKey();
String keydata;
while ((keydata = br.readLine()) != null) {
keydata = keydata.trim();
if (!keydata.isEmpty()) {
String[] parts = keydata.trim().split("\\s+");
if (parts.length >= 2) {
type = KeyType.fromString(parts[0]);
pubKey = new Buffer.PlainBuffer(Base64.decode(parts[1])).readPublicKey();
} else {
throw new IOException("Got line with only one column");
}
return;
}
}
throw new IOException("Public key file is blank");
} finally {
br.close();
}