aboutsummaryrefslogtreecommitdiffstats
path: root/sshlib/src/test/java/com
diff options
context:
space:
mode:
Diffstat (limited to 'sshlib/src/test/java/com')
-rw-r--r--sshlib/src/test/java/com/trilead/ssh2/crypto/SimpleDERReaderTest.java187
-rw-r--r--sshlib/src/test/java/com/trilead/ssh2/signature/ECDSASHA2VerifyTest.java66
2 files changed, 253 insertions, 0 deletions
diff --git a/sshlib/src/test/java/com/trilead/ssh2/crypto/SimpleDERReaderTest.java b/sshlib/src/test/java/com/trilead/ssh2/crypto/SimpleDERReaderTest.java
new file mode 100644
index 0000000..6a97218
--- /dev/null
+++ b/sshlib/src/test/java/com/trilead/ssh2/crypto/SimpleDERReaderTest.java
@@ -0,0 +1,187 @@
+package com.trilead.ssh2.crypto;
+
+import org.junit.Test;
+
+import java.io.IOException;
+import java.math.BigInteger;
+
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
+
+/**
+ * Created by kenny on 12/25/15.
+ */
+public class SimpleDERReaderTest {
+ @Test
+ public void readLength_Extended_OverlyLongLength() throws Exception {
+ byte[] vector = new byte[] {
+ (byte) 0x85, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ assertEquals(-1, reader.readLength());
+ }
+
+ @Test
+ public void readLength_Extended_TooLongForInt() throws Exception {
+ byte[] vector = new byte[] {
+ (byte) 0x84, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ assertEquals(-1, reader.readLength());
+ }
+
+ @Test
+ public void readLength_Extended_Zero() throws Exception {
+ byte[] vector = new byte[] {
+ (byte) 0x80, (byte) 0x01
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ assertEquals(-1, reader.readLength());
+ }
+
+ @Test
+ public void readLength_Extended_Valid() throws Exception {
+ byte[] vector = new byte[] {
+ (byte) 0x82, (byte) 0x05, (byte) 0xFF
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ assertEquals(0x5FF, reader.readLength());
+ }
+
+ @Test
+ public void readLength_Short_Zero() throws Exception {
+ byte[] vector = new byte[] {
+ (byte) 0x00
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ assertEquals(0, reader.readLength());
+ }
+
+ @Test
+ public void readLength_Short_Regular() throws Exception {
+ byte[] vector = new byte[] {
+ (byte) 0x09
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ assertEquals(9, reader.readLength());
+ }
+
+ @Test
+ public void readInt_MaxInt() throws Exception {
+ byte[] vector = new byte[] {
+ (byte) 0x02, (byte) 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ assertEquals(BigInteger.valueOf(0xFFFFFFFF), reader.readInt());
+ }
+
+ @Test
+ public void readInt_NotReallyInteger() throws Exception {
+ byte[] vector = new byte[] {
+ (byte) 0x01, (byte) 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ try {
+ reader.readInt();
+ } catch (IOException expected) {
+ assertThat(expected.getMessage(), containsString("Expected DER Integer"));
+ }
+ }
+
+ @Test
+ public void readInt_InvalidLength() throws Exception {
+ byte[] vector = new byte[] {
+ (byte) 0x02, (byte) 0x80, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ try {
+ reader.readInt();
+ } catch (IOException expected) {
+ assertThat(expected.getMessage(), containsString("Illegal len"));
+ }
+ }
+
+ @Test
+ public void readInt_ShortArray() throws Exception {
+ byte[] vector = new byte[] {
+ (byte) 0x02, (byte) 0x02, (byte) 0xFF
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ try {
+ reader.readInt();
+ } catch (IOException expected) {
+ }
+ }
+
+ @Test
+ public void readOid_InvalidLength() throws Exception {
+ byte[] vector = new byte[]{
+ (byte) 0x02, (byte) 0x80, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ try {
+ reader.readOid();
+ } catch (IOException expected) {
+ }
+ }
+
+ @Test
+ public void readOid_TooShort() throws Exception {
+ byte[] vector = new byte[]{
+ (byte) 0x02, (byte) 0x00
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ try {
+ reader.readOid();
+ } catch (IOException expected) {
+ }
+ }
+
+ @Test
+ public void readOid_NotOidValue() throws Exception {
+ byte[] vector = new byte[]{
+ (byte) 0x02, (byte) 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ try {
+ reader.readOid();
+ } catch (IOException expected) {
+ }
+ }
+
+ @Test
+ public void readOid_Valid1() throws Exception {
+ byte[] vector = new byte[]{
+ (byte) 0x06, (byte) 0x01, (byte) 0x28
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ assertEquals("1.0", reader.readOid());
+ }
+
+ @Test
+ public void readOid_Valid1Prefix() throws Exception {
+ byte[] vector = new byte[]{
+ (byte) 0x06, (byte) 0x09, (byte) 0x2a, (byte) 0x86, (byte) 0x48, (byte) 0x86, (byte) 0xf7, (byte) 0x0d, (byte) 0x01, (byte) 0x01, (byte) 0x0b
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ assertEquals("1.2.840.113549.1.1.11", reader.readOid());
+ }
+
+ @Test
+ public void readOid_Valid0Prefix() throws Exception {
+ byte[] vector = new byte[]{
+ (byte) 0x06, (byte) 0x0A, (byte) 0x09, (byte) 0x92, (byte) 0x26, (byte) 0x89, (byte) 0x93, (byte) 0xF2, (byte) 0x2C, (byte) 0x64, (byte) 0x04, (byte) 0x0D
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ assertEquals("0.9.2342.19200300.100.4.13", reader.readOid());
+ }
+
+ @Test
+ public void readOid_Valid2Prefix() throws Exception {
+ byte[] vector = new byte[]{
+ (byte) 0x06, (byte) 0x03, (byte) 0x55, (byte) 0x1D, (byte) 0x0E
+ };
+ SimpleDERReader reader = new SimpleDERReader(vector);
+ assertEquals("2.5.29.14", reader.readOid());
+ }
+} \ No newline at end of file
diff --git a/sshlib/src/test/java/com/trilead/ssh2/signature/ECDSASHA2VerifyTest.java b/sshlib/src/test/java/com/trilead/ssh2/signature/ECDSASHA2VerifyTest.java
new file mode 100644
index 0000000..d7b9316
--- /dev/null
+++ b/sshlib/src/test/java/com/trilead/ssh2/signature/ECDSASHA2VerifyTest.java
@@ -0,0 +1,66 @@
+package com.trilead.ssh2.signature;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Created by kenny on 12/25/15.
+ */
+public class ECDSASHA2VerifyTest {
+ private static final byte[] DER_ENCODED_P521_SIG = new byte[]{
+ (byte) 0x30, (byte) 0x81, (byte) 0x88, (byte) 0x02, (byte) 0x42, (byte) 0x00, (byte) 0xFB, (byte) 0x41, (byte) 0xFD, (byte) 0xBD, (byte) 0x61, (byte) 0x5D,
+ (byte) 0xFE, (byte) 0x3F, (byte) 0x0C, (byte) 0xA1, (byte) 0xF0, (byte) 0x73, (byte) 0xF1, (byte) 0x18, (byte) 0xFB, (byte) 0x25, (byte) 0x57, (byte) 0xF4,
+ (byte) 0xDE, (byte) 0xF5, (byte) 0xC1, (byte) 0xAA, (byte) 0xB2, (byte) 0xA7, (byte) 0x2B, (byte) 0x9F, (byte) 0x81, (byte) 0xD1, (byte) 0x21, (byte) 0x9D,
+ (byte) 0x48, (byte) 0xC8, (byte) 0xC9, (byte) 0x31, (byte) 0xB9, (byte) 0x9B, (byte) 0xE5, (byte) 0x97, (byte) 0x94, (byte) 0x2F, (byte) 0xD5, (byte) 0x7E,
+ (byte) 0x0C, (byte) 0x32, (byte) 0x2D, (byte) 0xF9, (byte) 0x76, (byte) 0xC6, (byte) 0x33, (byte) 0x2C, (byte) 0x49, (byte) 0x1D, (byte) 0xDF, (byte) 0x51,
+ (byte) 0xA2, (byte) 0xD2, (byte) 0xB0, (byte) 0x72, (byte) 0x9B, (byte) 0x26, (byte) 0xC4, (byte) 0xB2, (byte) 0xA0, (byte) 0xF0, (byte) 0x7E, (byte) 0x02,
+ (byte) 0x42, (byte) 0x01, (byte) 0x56, (byte) 0x94, (byte) 0x9B, (byte) 0xAB, (byte) 0x00, (byte) 0x6D, (byte) 0x3C, (byte) 0x28, (byte) 0x34, (byte) 0x1B,
+ (byte) 0x00, (byte) 0xF3, (byte) 0xDF, (byte) 0xF7, (byte) 0x42, (byte) 0xAD, (byte) 0x8B, (byte) 0x20, (byte) 0x55, (byte) 0x2E, (byte) 0x80, (byte) 0x4F,
+ (byte) 0xDE, (byte) 0x0F, (byte) 0xBC, (byte) 0xE7, (byte) 0xE2, (byte) 0x7C, (byte) 0xF3, (byte) 0x3B, (byte) 0xFD, (byte) 0x95, (byte) 0xB0, (byte) 0xF7,
+ (byte) 0xD4, (byte) 0xE0, (byte) 0x63, (byte) 0xA9, (byte) 0x86, (byte) 0xA6, (byte) 0x49, (byte) 0xF4, (byte) 0x69, (byte) 0x66, (byte) 0x10, (byte) 0xD5,
+ (byte) 0x3F, (byte) 0xB6, (byte) 0x30, (byte) 0xDC, (byte) 0x01, (byte) 0x0E, (byte) 0xBE, (byte) 0xD1, (byte) 0x62, (byte) 0x86, (byte) 0x2B, (byte) 0xE4,
+ (byte) 0xF2, (byte) 0xF3, (byte) 0x6D, (byte) 0x4C, (byte) 0xE1, (byte) 0xD0, (byte) 0x5C
+ };
+
+ private static final byte[] SSH_ENCODED_P521_SIG = new byte[] {
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x13,
+ (byte) 0x65, (byte) 0x63, (byte) 0x64, (byte) 0x73, (byte) 0x61, (byte) 0x2D, (byte) 0x73, (byte) 0x68,
+ (byte) 0x61, (byte) 0x32, (byte) 0x2D, (byte) 0x6E, (byte) 0x69, (byte) 0x73, (byte) 0x74, (byte) 0x70,
+ (byte) 0x35, (byte) 0x32, (byte) 0x31,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x8C,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x42,
+ (byte) 0x00, (byte) 0xFB, (byte) 0x41, (byte) 0xFD, (byte) 0xBD, (byte) 0x61, (byte) 0x5D,
+ (byte) 0xFE, (byte) 0x3F, (byte) 0x0C, (byte) 0xA1, (byte) 0xF0, (byte) 0x73, (byte) 0xF1, (byte) 0x18,
+ (byte) 0xFB, (byte) 0x25, (byte) 0x57, (byte) 0xF4, (byte) 0xDE, (byte) 0xF5, (byte) 0xC1, (byte) 0xAA,
+ (byte) 0xB2, (byte) 0xA7, (byte) 0x2B, (byte) 0x9F, (byte) 0x81, (byte) 0xD1, (byte) 0x21, (byte) 0x9D,
+ (byte) 0x48, (byte) 0xC8, (byte) 0xC9, (byte) 0x31, (byte) 0xB9, (byte) 0x9B, (byte) 0xE5, (byte) 0x97,
+ (byte) 0x94, (byte) 0x2F, (byte) 0xD5, (byte) 0x7E, (byte) 0x0C, (byte) 0x32, (byte) 0x2D, (byte) 0xF9,
+ (byte) 0x76, (byte) 0xC6, (byte) 0x33, (byte) 0x2C, (byte) 0x49, (byte) 0x1D, (byte) 0xDF, (byte) 0x51,
+ (byte) 0xA2, (byte) 0xD2, (byte) 0xB0, (byte) 0x72, (byte) 0x9B, (byte) 0x26, (byte) 0xC4, (byte) 0xB2,
+ (byte) 0xA0, (byte) 0xF0, (byte) 0x7E,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x42,
+ (byte) 0x01, (byte) 0x56, (byte) 0x94, (byte) 0x9B, (byte) 0xAB, (byte) 0x00, (byte) 0x6D, (byte) 0x3C,
+ (byte) 0x28, (byte) 0x34, (byte) 0x1B, (byte) 0x00, (byte) 0xF3, (byte) 0xDF, (byte) 0xF7, (byte) 0x42,
+ (byte) 0xAD, (byte) 0x8B, (byte) 0x20, (byte) 0x55, (byte) 0x2E, (byte) 0x80, (byte) 0x4F, (byte) 0xDE,
+ (byte) 0x0F, (byte) 0xBC, (byte) 0xE7, (byte) 0xE2, (byte) 0x7C, (byte) 0xF3, (byte) 0x3B, (byte) 0xFD,
+ (byte) 0x95, (byte) 0xB0, (byte) 0xF7, (byte) 0xD4, (byte) 0xE0, (byte) 0x63, (byte) 0xA9, (byte) 0x86,
+ (byte) 0xA6, (byte) 0x49, (byte) 0xF4, (byte) 0x69, (byte) 0x66, (byte) 0x10, (byte) 0xD5, (byte) 0x3F,
+ (byte) 0xB6, (byte) 0x30, (byte) 0xDC, (byte) 0x01, (byte) 0x0E, (byte) 0xBE, (byte) 0xD1, (byte) 0x62,
+ (byte) 0x86, (byte) 0x2B, (byte) 0xE4, (byte) 0xF2, (byte) 0xF3, (byte) 0x6D, (byte) 0x4C, (byte) 0xE1,
+ (byte) 0xD0, (byte) 0x5C
+ };
+
+ @Test
+ public void encodeSSHECDSASignature() throws Exception {
+ byte[] encoded = ECDSASHA2Verify.encodeSSHECDSASignature(DER_ENCODED_P521_SIG,
+ ECDSASHA2Verify.getCurveForSize(521));
+ assertArrayEquals(SSH_ENCODED_P521_SIG, encoded);
+ }
+
+ @Test
+ public void decodeSSHECDSASignature() throws Exception {
+ byte[] encoded = ECDSASHA2Verify.decodeSSHECDSASignature(SSH_ENCODED_P521_SIG);
+ assertArrayEquals(DER_ENCODED_P521_SIG, encoded);
+ }
+} \ No newline at end of file