Skip to content

Commit 09f32b3

Browse files
committed
Clone CryptoWishList when filtering
To avoid overwriting the original CryptoWishList, clone it before modifying it. Additionally move the "ext-info-c" into the filtering stage so that even custom lists of algorithms have it included.
1 parent ea6a204 commit 09f32b3

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

src/main/java/com/trilead/ssh2/crypto/CryptoWishList.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,41 @@
1313
* @author Christian Plattner, plattner@trilead.com
1414
* @version $Id: CryptoWishList.java,v 1.1 2007/10/15 12:49:56 cplattne Exp $
1515
*/
16-
public class CryptoWishList
16+
public class CryptoWishList implements Cloneable
1717
{
18-
public String[] kexAlgorithms = KexManager.getDefaultKexAlgorithmList();
19-
public String[] serverHostKeyAlgorithms = KexManager.getDefaultServerHostkeyAlgorithmList();
20-
public String[] c2s_enc_algos = BlockCipherFactory.getDefaultCipherList();
21-
public String[] s2c_enc_algos = BlockCipherFactory.getDefaultCipherList();
22-
public String[] c2s_mac_algos = MACs.getMacList();
23-
public String[] s2c_mac_algos = MACs.getMacList();
24-
public String[] c2s_comp_algos = CompressionFactory.getDefaultCompressorList();
25-
public String[] s2c_comp_algos = CompressionFactory.getDefaultCompressorList();
18+
public CryptoWishList() {
19+
kexAlgorithms = KexManager.getDefaultKexAlgorithmList();
20+
serverHostKeyAlgorithms = KexManager.getDefaultServerHostkeyAlgorithmList();
21+
c2s_enc_algos = BlockCipherFactory.getDefaultCipherList();
22+
s2c_enc_algos = BlockCipherFactory.getDefaultCipherList();
23+
c2s_mac_algos = MACs.getMacList();
24+
s2c_mac_algos = MACs.getMacList();
25+
c2s_comp_algos = CompressionFactory.getDefaultCompressorList();
26+
s2c_comp_algos = CompressionFactory.getDefaultCompressorList();
27+
}
28+
29+
public CryptoWishList(CryptoWishList other) {
30+
kexAlgorithms = other.kexAlgorithms.clone();
31+
serverHostKeyAlgorithms = other.serverHostKeyAlgorithms.clone();
32+
c2s_enc_algos = other.c2s_enc_algos.clone();
33+
s2c_enc_algos = other.s2c_enc_algos.clone();
34+
c2s_mac_algos = other.c2s_mac_algos.clone();
35+
s2c_mac_algos = other.s2c_mac_algos.clone();
36+
c2s_comp_algos = other.c2s_comp_algos.clone();
37+
s2c_comp_algos = other.s2c_comp_algos.clone();
38+
}
39+
40+
public String[] kexAlgorithms;
41+
public String[] serverHostKeyAlgorithms;
42+
public String[] c2s_enc_algos;
43+
public String[] s2c_enc_algos;
44+
public String[] c2s_mac_algos;
45+
public String[] s2c_mac_algos;
46+
public String[] c2s_comp_algos;
47+
public String[] s2c_comp_algos;
48+
49+
@Override
50+
public CryptoWishList clone() {
51+
return new CryptoWishList(this);
52+
}
2653
}

src/main/java/com/trilead/ssh2/transport/KexManager.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ public class KexManager
9898
KEX_ALGS.add("diffie-hellman-group-exchange-sha1");
9999
KEX_ALGS.add("diffie-hellman-group14-sha1");
100100
KEX_ALGS.add("diffie-hellman-group1-sha1");
101-
102-
// Indicate client support for ext-info
103-
KEX_ALGS.add("ext-info-c");
104101
}
105102

103+
/** RFC 8308 Section 2 */
104+
private static final String EXT_INFO_C = "ext-info-c";
105+
106106
private KexState kxs;
107107
private int kexCount = 0;
108108
private KeyMaterial km;
@@ -279,8 +279,9 @@ private NegotiatedParameters mergeKexParameters(KexParameters client, KexParamet
279279

280280
public synchronized void initiateKEX(CryptoWishList cwl, DHGexParameters dhgex) throws IOException
281281
{
282-
nextKEXcryptoWishList = cwl;
282+
nextKEXcryptoWishList = cwl.clone();
283283
filterHostKeyTypes(nextKEXcryptoWishList);
284+
addExtraKexAlgorithms(nextKEXcryptoWishList);
284285

285286
nextKEXdhgexParameters = dhgex;
286287

@@ -295,6 +296,24 @@ public synchronized void initiateKEX(CryptoWishList cwl, DHGexParameters dhgex)
295296
}
296297
}
297298

299+
/**
300+
* Adds the pseudo-key-exchange algorithms to the crypto wishlist.
301+
*
302+
* @param cwl the crypto wishlist to which the key exchange algo
303+
* should be added.
304+
*/
305+
private static void addExtraKexAlgorithms(CryptoWishList cwl) {
306+
String[] oldKexAlgorithms = cwl.kexAlgorithms;
307+
List<String> kexAlgorithms = new ArrayList<>(oldKexAlgorithms.length + 1);
308+
for (String algo : oldKexAlgorithms)
309+
{
310+
if (!algo.equals(EXT_INFO_C))
311+
kexAlgorithms.add(algo);
312+
}
313+
kexAlgorithms.add(EXT_INFO_C);
314+
cwl.kexAlgorithms = kexAlgorithms.toArray(new String[0]);
315+
}
316+
298317
/**
299318
* If the verifier can indicate which algorithms it knows about for this host, then
300319
* filter out our crypto wish list to only include those algorithms. Otherwise we'll

0 commit comments

Comments
 (0)