@Test public void testNewKey() throws NoSuchAlgorithmException, InvalidKeySpecException { SocketAddress address = LOCALHOST; PublicKey validServerKey = createPubKey(); KnownHostsManager knowHostsManager = EasyMock.createMock(KnownHostsManager.class); EasyMock.expect(knowHostsManager.getKnownKey(address, ALGORITHM)).andReturn(null); knowHostsManager.storeKeyForHost(address, validServerKey); EasyMock.expectLastCall(); EasyMock.replay(knowHostsManager); ServerKeyVerifierImpl verifier = new ServerKeyVerifierImpl(knowHostsManager, true); boolean verified = verifier.verifyServerKey(null, address, validServerKey); Assert.assertTrue("Key should be verified as the key is new", verified); }
@Override public boolean verifyServerKey(ClientSession sshClientSession, SocketAddress remoteAddress, PublicKey serverKey) { PublicKey knownKey; try { knownKey = knownHostsManager.getKnownKey(remoteAddress, serverKey.getAlgorithm()); } catch (InvalidKeySpecException e) { System.err.println("Invalid key stored for host " + remoteAddress + ". Terminating session."); return false; } if (knownKey == null) { boolean confirm; if (!quiet) { System.out.println("Connecting to unknown server. Add this server to known hosts ? (y/n)"); confirm = getConfirmation(); } else { System.out.println("Connecting to unknown server. Automatically adding to known hosts."); confirm = true; } if (confirm) { knownHostsManager.storeKeyForHost(remoteAddress, serverKey); System.out.println("Storing the server key in known_hosts."); } else { System.out.println("Aborting connection"); } return confirm; } boolean verifed = (knownKey.equals(serverKey)); if (!verifed) { System.err.println("Server key for host " + remoteAddress + " does not match the stored key !! Terminating session."); System.err.println(keyChangedMessage); } return verifed; }
ServerKeyVerifierImpl implements ServerKeyVerifier { @Override public boolean verifyServerKey(ClientSession sshClientSession, SocketAddress remoteAddress, PublicKey serverKey) { PublicKey knownKey; try { knownKey = knownHostsManager.getKnownKey(remoteAddress, serverKey.getAlgorithm()); } catch (InvalidKeySpecException e) { System.err.println("Invalid key stored for host " + remoteAddress + ". Terminating session."); return false; } if (knownKey == null) { boolean confirm; if (!quiet) { System.out.println("Connecting to unknown server. Add this server to known hosts ? (y/n)"); confirm = getConfirmation(); } else { System.out.println("Connecting to unknown server. Automatically adding to known hosts."); confirm = true; } if (confirm) { knownHostsManager.storeKeyForHost(remoteAddress, serverKey); System.out.println("Storing the server key in known_hosts."); } else { System.out.println("Aborting connection"); } return confirm; } boolean verifed = (knownKey.equals(serverKey)); if (!verifed) { System.err.println("Server key for host " + remoteAddress + " does not match the stored key !! Terminating session."); System.err.println(keyChangedMessage); } return verifed; } }
ServerKeyVerifierImpl implements ServerKeyVerifier { @Override public boolean verifyServerKey(ClientSession sshClientSession, SocketAddress remoteAddress, PublicKey serverKey) { PublicKey knownKey; try { knownKey = knownHostsManager.getKnownKey(remoteAddress, serverKey.getAlgorithm()); } catch (InvalidKeySpecException e) { System.err.println("Invalid key stored for host " + remoteAddress + ". Terminating session."); return false; } if (knownKey == null) { boolean confirm; if (!quiet) { System.out.println("Connecting to unknown server. Add this server to known hosts ? (y/n)"); confirm = getConfirmation(); } else { System.out.println("Connecting to unknown server. Automatically adding to known hosts."); confirm = true; } if (confirm) { knownHostsManager.storeKeyForHost(remoteAddress, serverKey); System.out.println("Storing the server key in known_hosts."); } else { System.out.println("Aborting connection"); } return confirm; } boolean verifed = (knownKey.equals(serverKey)); if (!verifed) { System.err.println("Server key for host " + remoteAddress + " does not match the stored key !! Terminating session."); System.err.println(keyChangedMessage); } return verifed; } ServerKeyVerifierImpl(KnownHostsManager knownHostsManager, boolean quiet); }
ServerKeyVerifierImpl implements ServerKeyVerifier { @Override public boolean verifyServerKey(ClientSession sshClientSession, SocketAddress remoteAddress, PublicKey serverKey) { PublicKey knownKey; try { knownKey = knownHostsManager.getKnownKey(remoteAddress, serverKey.getAlgorithm()); } catch (InvalidKeySpecException e) { System.err.println("Invalid key stored for host " + remoteAddress + ". Terminating session."); return false; } if (knownKey == null) { boolean confirm; if (!quiet) { System.out.println("Connecting to unknown server. Add this server to known hosts ? (y/n)"); confirm = getConfirmation(); } else { System.out.println("Connecting to unknown server. Automatically adding to known hosts."); confirm = true; } if (confirm) { knownHostsManager.storeKeyForHost(remoteAddress, serverKey); System.out.println("Storing the server key in known_hosts."); } else { System.out.println("Aborting connection"); } return confirm; } boolean verifed = (knownKey.equals(serverKey)); if (!verifed) { System.err.println("Server key for host " + remoteAddress + " does not match the stored key !! Terminating session."); System.err.println(keyChangedMessage); } return verifed; } ServerKeyVerifierImpl(KnownHostsManager knownHostsManager, boolean quiet); @Override boolean verifyServerKey(ClientSession sshClientSession, SocketAddress remoteAddress,
                                   PublicKey serverKey); }
ServerKeyVerifierImpl implements ServerKeyVerifier { @Override public boolean verifyServerKey(ClientSession sshClientSession, SocketAddress remoteAddress, PublicKey serverKey) { PublicKey knownKey; try { knownKey = knownHostsManager.getKnownKey(remoteAddress, serverKey.getAlgorithm()); } catch (InvalidKeySpecException e) { System.err.println("Invalid key stored for host " + remoteAddress + ". Terminating session."); return false; } if (knownKey == null) { boolean confirm; if (!quiet) { System.out.println("Connecting to unknown server. Add this server to known hosts ? (y/n)"); confirm = getConfirmation(); } else { System.out.println("Connecting to unknown server. Automatically adding to known hosts."); confirm = true; } if (confirm) { knownHostsManager.storeKeyForHost(remoteAddress, serverKey); System.out.println("Storing the server key in known_hosts."); } else { System.out.println("Aborting connection"); } return confirm; } boolean verifed = (knownKey.equals(serverKey)); if (!verifed) { System.err.println("Server key for host " + remoteAddress + " does not match the stored key !! Terminating session."); System.err.println(keyChangedMessage); } return verifed; } ServerKeyVerifierImpl(KnownHostsManager knownHostsManager, boolean quiet); @Override boolean verifyServerKey(ClientSession sshClientSession, SocketAddress remoteAddress,
                                   PublicKey serverKey);  }