/** * Authenticate ourselves against the server. * * @return identity of the server represented as a public key. */ public PublicKey authenticate(Iterable<KeyPair> privateKeys) throws IOException, GeneralSecurityException { Pipe c2s = Pipe.createLocalToRemote(); Pipe s2c = Pipe.createRemoteToLocal(); entryPoint.authenticate("ssh", c2s, s2c); Connection c = new Connection(s2c.getIn(), c2s.getOut()); try { byte[] sharedSecret = c.diffieHellman(false).generateSecret(); PublicKey serverIdentity = c.verifyIdentity(sharedSecret); // try all the public keys for (KeyPair key : privateKeys) { c.proveIdentity(sharedSecret, key); if (c.readBoolean()) return serverIdentity; // succeeded } if (privateKeys.iterator().hasNext()) throw new GeneralSecurityException("Authentication failed. No private key accepted."); else throw new GeneralSecurityException("No private key is available for use in authentication"); } finally { c.close(); } }