Skip to content
Permalink
Browse files

Merge pull request #21 from mc1arke/jenkins-42959-key-format-mismatch

[JENKINS-42959] Correctly check host key algorithm
  • Loading branch information
mc1arke committed May 4, 2017
2 parents 63b2b85 + 25a98a6 commit 2f01ab94bfc72edab8c06554b7139e733c2cd540
Showing with 15 additions and 1 deletion.
  1. +1 −1 src/com/trilead/ssh2/KnownHosts.java
  2. +14 −0 test/com/trilead/ssh2/KnownHostsTest.java
@@ -599,7 +599,7 @@ public int verifyHostkey(String hostname, String serverHostKeyAlgorithm, byte[]

private PublicKey decodeHostKey(String hostKeyAlgorithm, byte[] encodedHostKey) throws IOException {
for (KeyAlgorithm<PublicKey, PrivateKey> algorithm : KeyAlgorithmManager.getSupportedAlgorithms()) {
if (algorithm.getKeyFormat().equals(encodedHostKey)) {
if (algorithm.getKeyFormat().equals(hostKeyAlgorithm)) {
return algorithm.decodePublicKey(encodedHostKey);
}
}
@@ -14,6 +14,7 @@
import java.security.interfaces.RSAPublicKey;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
@@ -69,4 +70,17 @@ public void testKnownHostsPreferredAlgorithmsRsaAndDssHosts() throws IOException
assertNull(testCase.getPreferredServerHostkeyAlgorithmOrder("localhost"));
}


@Test
public void testVerifyKnownHostKey() throws IOException, NoSuchAlgorithmException {
KnownHosts testCase = new KnownHosts();
KeyPairGenerator rsaGenerator = KeyPairGenerator.getInstance("RSA");
byte[] encodedPublicKey = new RSAKeyAlgorithm().encodePublicKey((RSAPublicKey) rsaGenerator.generateKeyPair().getPublic());
byte[] encodedPublicKey2 = new RSAKeyAlgorithm().encodePublicKey((RSAPublicKey) rsaGenerator.generateKeyPair().getPublic());
testCase.addHostkey(new String[]{"testhost"}, "ssh-rsa", encodedPublicKey);
assertEquals(KnownHosts.HOSTKEY_IS_NEW, testCase.verifyHostkey("testhost2", "ssh-rsa", encodedPublicKey));
assertEquals(KnownHosts.HOSTKEY_HAS_CHANGED, testCase.verifyHostkey("testhost", "ssh-rsa", encodedPublicKey2));
assertEquals(KnownHosts.HOSTKEY_IS_OK, testCase.verifyHostkey("testhost", "ssh-rsa", encodedPublicKey));
}

}

0 comments on commit 2f01ab9

Please sign in to comment.
You can’t perform that action at this time.