Skip to content
Permalink
Browse files

[JENKINS-42959] Return host algorithms if they're in known hosts and …

…we support them
  • Loading branch information
mc1arke committed Mar 27, 2017
1 parent 3aaec83 commit 63d9a981eec88c46b8b73ce54f8980f40c6b1927
Showing with 59 additions and 4 deletions.
  1. +4 −4 src/com/trilead/ssh2/KnownHosts.java
  2. +55 −0 test/com/trilead/ssh2/KnownHostsTest.java
@@ -508,12 +508,12 @@ private boolean pseudoRegex(char[] pattern, int i, char[] match, int j)
if (preferredAlgo != null) {
/* If we find different key types, then return null */

if (preferredAlgo.compareTo(thisAlgo) != 0)
if (!preferredAlgo.equals(thisAlgo)) {
return null;
}

/* OK, we found the same algo again, optimize */

continue;
} else {
preferredAlgo = thisAlgo;
}
}

@@ -0,0 +1,55 @@
package com.trilead.ssh2;

import com.trilead.ssh2.signature.DSAPublicKey;
import com.trilead.ssh2.signature.DSASHA1Verify;
import com.trilead.ssh2.signature.RSAPublicKey;
import com.trilead.ssh2.signature.RSASHA1Verify;
import org.junit.Test;

import java.io.IOException;
import java.math.BigInteger;

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

/**
* @author Michael Clarke
*/
public class KnownHostsTest {

@Test
public void testKnownHostsPreferredAlgorithmsSshDssOnly() throws IOException {
KnownHosts testCase = new KnownHosts();
testCase.addHostkey(new String[]{"localhost"}, "ssh-dss", DSASHA1Verify.encodeSSHDSAPublicKey(new DSAPublicKey(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE)));
assertArrayEquals(new String[]{"ssh-dss", "ssh-rsa"}, testCase.getPreferredServerHostkeyAlgorithmOrder("localhost"));
}

@Test
public void testKnownHostsPreferredAlgorithmsSshRsaOnly() throws IOException {
KnownHosts testCase = new KnownHosts();
testCase.addHostkey(new String[]{"localhost"}, "ssh-rsa", RSASHA1Verify.encodeSSHRSAPublicKey(new RSAPublicKey(BigInteger.ONE, BigInteger.ONE)));
assertArrayEquals(new String[]{"ssh-rsa", "ssh-dss"}, testCase.getPreferredServerHostkeyAlgorithmOrder("localhost"));
}

@Test
public void testKnownHostsPreferredAlgorithmsNoKnownHosts() throws IOException {
KnownHosts testCase = new KnownHosts();
assertNull(testCase.getPreferredServerHostkeyAlgorithmOrder("localhost"));
}


/**
* The Known Hosts implementation currently expects multiple known hosts entries for the same hosts to result in
* a null value being returned for the preferred algorithms, rather than a list of all those known algorithms. This
* seems an odd choice, but I'll protect that feature for now.
* @throws IOException if failing to add the test keys
*/
@Test
public void testKnownHostsPreferredAlgorithmsRsaAndDssHosts() throws IOException {
KnownHosts testCase = new KnownHosts();
testCase.addHostkey(new String[]{"localhost"}, "ssh-dss", DSASHA1Verify.encodeSSHDSAPublicKey(new DSAPublicKey(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE)));
testCase.addHostkey(new String[]{"localhost"}, "ssh-rsa", RSASHA1Verify.encodeSSHRSAPublicKey(new RSAPublicKey(BigInteger.ONE, BigInteger.ONE)));
assertNull(testCase.getPreferredServerHostkeyAlgorithmOrder("localhost"));
}

}

0 comments on commit 63d9a98

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