mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 23:30:55 +03:00
(reformat)
This commit is contained in:
@@ -15,7 +15,12 @@
|
|||||||
*/
|
*/
|
||||||
package net.schmizz.sshj.transport.verification;
|
package net.schmizz.sshj.transport.verification;
|
||||||
|
|
||||||
import net.schmizz.sshj.common.*;
|
import net.schmizz.sshj.common.Base64;
|
||||||
|
import net.schmizz.sshj.common.Buffer;
|
||||||
|
import net.schmizz.sshj.common.IOUtils;
|
||||||
|
import net.schmizz.sshj.common.KeyType;
|
||||||
|
import net.schmizz.sshj.common.SSHException;
|
||||||
|
import net.schmizz.sshj.common.SecurityUtils;
|
||||||
import net.schmizz.sshj.transport.mac.HMACSHA1;
|
import net.schmizz.sshj.transport.mac.HMACSHA1;
|
||||||
import net.schmizz.sshj.transport.mac.MAC;
|
import net.schmizz.sshj.transport.mac.MAC;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -48,7 +53,8 @@ public class OpenSSHKnownHosts
|
|||||||
protected final File khFile;
|
protected final File khFile;
|
||||||
protected final List<HostEntry> entries = new ArrayList<HostEntry>();
|
protected final List<HostEntry> entries = new ArrayList<HostEntry>();
|
||||||
|
|
||||||
public OpenSSHKnownHosts(File khFile) throws IOException {
|
public OpenSSHKnownHosts(File khFile)
|
||||||
|
throws IOException {
|
||||||
this.khFile = khFile;
|
this.khFile = khFile;
|
||||||
if (khFile.exists()) {
|
if (khFile.exists()) {
|
||||||
final BufferedReader br = new BufferedReader(new FileReader(khFile));
|
final BufferedReader br = new BufferedReader(new FileReader(khFile));
|
||||||
@@ -159,7 +165,8 @@ public class OpenSSHKnownHosts
|
|||||||
*/
|
*/
|
||||||
public static class EntryFactory {
|
public static class EntryFactory {
|
||||||
|
|
||||||
public static HostEntry parseEntry(String line) throws IOException {
|
public static HostEntry parseEntry(String line)
|
||||||
|
throws IOException {
|
||||||
if (isComment(line)) {
|
if (isComment(line)) {
|
||||||
return new CommentEntry(line);
|
return new CommentEntry(line);
|
||||||
}
|
}
|
||||||
@@ -203,7 +210,8 @@ public class OpenSSHKnownHosts
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static PublicKey getKey(String sKey) throws IOException {
|
private static PublicKey getKey(String sKey)
|
||||||
|
throws IOException {
|
||||||
return new Buffer.PlainBuffer(Base64.decode(sKey)).readPublicKey();
|
return new Buffer.PlainBuffer(Base64.decode(sKey)).readPublicKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,8 +245,12 @@ public class OpenSSHKnownHosts
|
|||||||
}
|
}
|
||||||
|
|
||||||
public interface HostEntry {
|
public interface HostEntry {
|
||||||
boolean appliesTo(KeyType type, String host) throws IOException;
|
boolean appliesTo(KeyType type, String host)
|
||||||
boolean verify(PublicKey key) throws IOException;
|
throws IOException;
|
||||||
|
|
||||||
|
boolean verify(PublicKey key)
|
||||||
|
throws IOException;
|
||||||
|
|
||||||
String getLine();
|
String getLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,7 +290,8 @@ public class OpenSSHKnownHosts
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean verify(PublicKey key) throws IOException {
|
public boolean verify(PublicKey key)
|
||||||
|
throws IOException {
|
||||||
return key.equals(this.key) && marker != Marker.REVOKED;
|
return key.equals(this.key) && marker != Marker.REVOKED;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,7 +330,8 @@ public class OpenSSHKnownHosts
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean appliesTo(KeyType type, String host) throws IOException {
|
public boolean appliesTo(KeyType type, String host)
|
||||||
|
throws IOException {
|
||||||
return type == this.type && hostnames.contains(host);
|
return type == this.type && hostnames.contains(host);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -330,7 +344,8 @@ public class OpenSSHKnownHosts
|
|||||||
|
|
||||||
private final String hashedHost;
|
private final String hashedHost;
|
||||||
|
|
||||||
public HashedEntry(Marker marker, String hash, KeyType type, PublicKey key) throws SSHException {
|
public HashedEntry(Marker marker, String hash, KeyType type, PublicKey key)
|
||||||
|
throws SSHException {
|
||||||
super(marker, type, key);
|
super(marker, type, key);
|
||||||
this.hashedHost = hash;
|
this.hashedHost = hash;
|
||||||
{
|
{
|
||||||
@@ -342,16 +357,19 @@ public class OpenSSHKnownHosts
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean appliesTo(KeyType type, String host) throws IOException {
|
public boolean appliesTo(KeyType type, String host)
|
||||||
|
throws IOException {
|
||||||
return this.type == type && hashedHost.equals(hashHost(host));
|
return this.type == type && hashedHost.equals(hashHost(host));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String hashHost(String host) throws IOException {
|
private String hashHost(String host)
|
||||||
|
throws IOException {
|
||||||
sha1.init(getSaltyBytes());
|
sha1.init(getSaltyBytes());
|
||||||
return "|1|" + salt + "|" + Base64.encodeBytes(sha1.doFinal(host.getBytes(IOUtils.UTF8)));
|
return "|1|" + salt + "|" + Base64.encodeBytes(sha1.doFinal(host.getBytes(IOUtils.UTF8)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] getSaltyBytes() throws IOException {
|
private byte[] getSaltyBytes()
|
||||||
|
throws IOException {
|
||||||
if (saltyBytes == null) {
|
if (saltyBytes == null) {
|
||||||
saltyBytes = Base64.decode(salt);
|
saltyBytes = Base64.decode(salt);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user