Skip to content

Commit

Permalink
Trilead ssh2 fix big integer removes leading zero (#178)
Browse files Browse the repository at this point in the history
* Update TimeoutService.java

interrupt hanging timeoutservice threads

* Update TimeoutService.java

* Update TimeoutService.java

* Update Connection.java

* Update Connection.java

* Update TimeoutService.java

* Update TimeoutService.java

* Update Connection.java

* fix BigInteger

---------

Co-authored-by: mikael petterson <mikael.petterson@ericsson.com>
  • Loading branch information
mpet and eraonel authored Feb 19, 2024
1 parent 2a05e6b commit 56de4d4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ target/
.metadata
.loadpath
bin/

#Netbeans files
nbproject/private/
build/
nbbuild/
dist/
nbdist/
nbactions.xml
nb-configuration.xml
10 changes: 5 additions & 5 deletions src/com/trilead/ssh2/packets/PacketKexDHReply.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;

import java.math.BigInteger;

/**
* PacketKexDHReply.
Expand All @@ -15,7 +14,7 @@ public class PacketKexDHReply
byte[] payload;

byte[] hostKey;
BigInteger f;
byte [] publicKey;
byte[] signature;

public PacketKexDHReply(byte payload[], int off, int len) throws IOException
Expand All @@ -32,15 +31,16 @@ public PacketKexDHReply(byte payload[], int off, int len) throws IOException
+ packet_type + ")");

hostKey = tr.readByteString();
f = tr.readMPINT();
publicKey = tr.readByteString();
signature = tr.readByteString();

if (tr.remain() != 0) throw new IOException("PADDING IN SSH_MSG_KEXDH_REPLY!");
}

public BigInteger getF()
public byte[] getF()
{
return f;
return publicKey;

}

public byte[] getHostKey()
Expand Down
2 changes: 1 addition & 1 deletion src/com/trilead/ssh2/transport/KexManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ public synchronized void handleMessage(byte[] msg, int msglen) throws IOExceptio
throw new IOException("The server hostkey was not accepted by the verifier callback");
}

kxs.dhx.setF(dhr.getF().toByteArray());
kxs.dhx.setF(dhr.getF());

try
{
Expand Down
66 changes: 66 additions & 0 deletions test/com/trilead/ssh2/crypto/dh/Curve25519ExchangeTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.trilead.ssh2.crypto.dh;

import com.google.crypto.tink.subtle.X25519;
import com.trilead.ssh2.packets.PacketKexDHReply;
import java.io.IOException;

import org.junit.Test;

import java.math.BigInteger;
import java.security.InvalidKeyException;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

/**
* Created by Kenny Root on 1/23/16.
Expand Down Expand Up @@ -76,4 +81,65 @@ public void knownValues_Bob() throws Exception {
ex.setF(ALICE_PUBLIC);
assertEquals(KNOWN_SHARED_SECRET_BI, ex.sharedSecret);
}

@Test
public void testBigIntegerRemovesLeadingZero() throws Exception {

BigInteger bigIntegerWithoutLeadingZero = new BigInteger(1, toByteArray("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"));

BigInteger bigIntegerWithLeadingZero = new BigInteger(1, toByteArray("015d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"));
//The key with same length does not become the same when using BigInteger when a key has leading zeros. This is because BigInteger has stripLeadingZeroBytes method.
assertNotEquals(Integer.valueOf(bigIntegerWithoutLeadingZero.bitLength()),Integer.valueOf(bigIntegerWithLeadingZero.bitLength()));



}


@Test
public void testKeyWithLeadingZeros() throws InvalidKeyException{

//When the message contains leading 0 then the BigInteger class
//will remove the leading zero since it has a function to do so internally.
Curve25519Exchange curve25519Exchange = new Curve25519Exchange(ALICE_PRIVATE);

//public Diffie-Hellman key and other parameters in message. This one has
//leading zero.0, 0, 0, 2, 2, 2, 2, 2
byte[] msg = new byte[]{
31,

0, 0, 0, 32,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,

0, 0, 0, 32,
0, 0, 0, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2,

0, 0, 0, 32,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
PacketKexDHReply dhr = null;

try {
dhr = new PacketKexDHReply(msg, 0, msg.length);
} catch (IOException ioex) {
fail("We should not get exception when creating the PacketKexDHReply "+ioex.getMessage());
}
try {
curve25519Exchange.setF(dhr.getF());
} catch (IOException ioex) {
fail("We should not get exception while setting curve key "+ioex.getMessage());

}
}

}

0 comments on commit 56de4d4

Please sign in to comment.