File: SimpleVerifier.java

package info (click to toggle)
trilead-ssh2 6401%2Bsvn158-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,044 kB
  • sloc: java: 13,191; makefile: 13
file content (55 lines) | stat: -rw-r--r-- 1,557 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import com.trilead.ssh2.KnownHosts;
import com.trilead.ssh2.ServerHostKeyVerifier;

/**
 * This example hostkey verifier is used by the
 * UsingKnownHosts.java example.
 *  
 * @author Christian Plattner, plattner@trilead.com
 * @version $Id: SimpleVerifier.java,v 1.4 2007/10/15 12:49:57 cplattne Exp $
 */
class SimpleVerifier implements ServerHostKeyVerifier
{
	KnownHosts database;

	public SimpleVerifier(KnownHosts database)
	{
		if (database == null)
			throw new IllegalArgumentException();

		this.database = database;
	}

	public boolean verifyServerHostKey(String hostname, int port, String serverHostKeyAlgorithm, byte[] serverHostKey)
			throws Exception
	{
		int result = database.verifyHostkey(hostname, serverHostKeyAlgorithm, serverHostKey);

		switch (result)
		{
		case KnownHosts.HOSTKEY_IS_OK:

			return true; // We are happy

		case KnownHosts.HOSTKEY_IS_NEW:

			// Unknown host? Blindly accept the key and put it into the cache.
			// Well, you definitely can do better (e.g., ask the user).

			// The following call will ONLY put the key into the memory cache!
			// To save it in a known hosts file, also call "KnownHosts.addHostkeyToFile(...)"
			database.addHostkey(new String[] { hostname }, serverHostKeyAlgorithm, serverHostKey);

			return true;

		case KnownHosts.HOSTKEY_HAS_CHANGED:

			// Close the connection if the hostkey has changed.
			// Better: ask user and add new key to database.
			return false;

		default:
			throw new IllegalStateException();
		}
	}
}