• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3  * Please refer to the LICENSE.txt for licensing details.
4  */
5 import ch.ethz.ssh2.KnownHosts;
6 import ch.ethz.ssh2.ServerHostKeyVerifier;
7 
8 class SimpleVerifier implements ServerHostKeyVerifier
9 {
10 	KnownHosts database;
11 
12 	/*
13 	 * This class is being used by the UsingKnownHosts.java example.
14 	 */
15 
SimpleVerifier(KnownHosts database)16 	public SimpleVerifier(KnownHosts database)
17 	{
18 		if (database == null)
19 			throw new IllegalArgumentException();
20 
21 		this.database = database;
22 	}
23 
verifyServerHostKey(String hostname, int port, String serverHostKeyAlgorithm, byte[] serverHostKey)24 	public boolean verifyServerHostKey(String hostname, int port, String serverHostKeyAlgorithm, byte[] serverHostKey)
25 			throws Exception
26 	{
27 		int result = database.verifyHostkey(hostname, serverHostKeyAlgorithm, serverHostKey);
28 
29 		switch (result)
30 		{
31 		case KnownHosts.HOSTKEY_IS_OK:
32 
33 			return true; // We are happy
34 
35 		case KnownHosts.HOSTKEY_IS_NEW:
36 
37 			// Unknown host? Blindly accept the key and put it into the cache.
38 			// Well, you definitely can do better (e.g., ask the user).
39 
40 			// The following call will ONLY put the key into the memory cache!
41 			// To save it in a known hosts file, also call "KnownHosts.addHostkeyToFile(...)"
42 			database.addHostkey(new String[] { hostname }, serverHostKeyAlgorithm, serverHostKey);
43 
44 			return true;
45 
46 		case KnownHosts.HOSTKEY_HAS_CHANGED:
47 
48 			// Close the connection if the hostkey has changed.
49 			// Better: ask user and add new key to database.
50 			return false;
51 
52 		default:
53 			throw new IllegalStateException();
54 		}
55 	}
56 }