You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2015/05/26 07:57:34 UTC

mina-sshd git commit: [SSHD-474] Null pointer exception when connecting with latest Dropbear's dbclient

Repository: mina-sshd
Updated Branches:
  refs/heads/master b6166cc8d -> 8009a89f9


[SSHD-474] Null pointer exception when connecting with latest Dropbear's dbclient

Added stricter validations in order to have more information about the reason for the failure


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/8009a89f
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/8009a89f
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/8009a89f

Branch: refs/heads/master
Commit: 8009a89f94fccba7e3236488dd6b390588ffa013
Parents: b6166cc
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Tue May 26 08:57:25 2015 +0300
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Tue May 26 08:57:25 2015 +0300

----------------------------------------------------------------------
 .../java/org/apache/sshd/common/kex/ECDH.java   | 11 ++++-
 .../apache/sshd/common/util/GenericUtils.java   |  8 ++++
 .../sshd/common/util/buffer/BufferUtils.java    | 45 +++++++++++++-------
 .../org/apache/sshd/client/kex/KexTest.java     |  8 +++-
 4 files changed, 54 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8009a89f/sshd-core/src/main/java/org/apache/sshd/common/kex/ECDH.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/kex/ECDH.java b/sshd-core/src/main/java/org/apache/sshd/common/kex/ECDH.java
index fbb7090..08dc695 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/kex/ECDH.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/kex/ECDH.java
@@ -31,7 +31,10 @@ import javax.crypto.KeyAgreement;
 
 import org.apache.sshd.common.Digest;
 import org.apache.sshd.common.cipher.ECCurves;
+import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.common.util.SecurityUtils;
+import org.apache.sshd.common.util.ValidateUtils;
+import org.apache.sshd.common.util.buffer.BufferUtils;
 
 /**
  * Elliptic Curve Diffie-Hellman key agreement.
@@ -60,6 +63,7 @@ public class ECDH extends AbstractDH {
     @Override
     public byte[] getE() throws Exception {
         if (e == null) {
+            ValidateUtils.checkNotNull(params, "No ECParameterSpec(s)", GenericUtils.EMPTY_OBJECT_ARRAY);
             myKpairGen.initialize(params);
             KeyPair myKpair = myKpairGen.generateKeyPair();
             myKeyAgree.init(myKpair.getPrivate());
@@ -71,6 +75,7 @@ public class ECDH extends AbstractDH {
 
     @Override
     protected byte[] calculateK() throws Exception {
+        ValidateUtils.checkNotNull(params, "No ECParameterSpec(s)", GenericUtils.EMPTY_OBJECT_ARRAY);
         KeyFactory myKeyFac = SecurityUtils.getKeyFactory("EC");
         ECPublicKeySpec keySpec = new ECPublicKeySpec(f, params);
         PublicKey yourPubKey = myKeyFac.generatePublic(keySpec);
@@ -84,11 +89,15 @@ public class ECDH extends AbstractDH {
 
     @Override
     public void setF(byte[] f) {
-        this.f = ECCurves.decodeECPoint(f, params.getCurve());
+        ValidateUtils.checkNotNull(params, "No ECParameterSpec(s)", GenericUtils.EMPTY_OBJECT_ARRAY);
+        if ((this.f = ECCurves.decodeECPoint(f, params.getCurve())) == null) {
+            throw new IllegalArgumentException("No EC point decoded for F=" + BufferUtils.printHex(':', f));
+        }
     }
 
     @Override
     public Digest getHash() throws Exception {
+        ValidateUtils.checkNotNull(params, "No ECParameterSpec(s)", GenericUtils.EMPTY_OBJECT_ARRAY);
         return ECCurves.getDigestForParams(params);
     }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8009a89f/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java
index a047157..c75235b 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java
@@ -181,6 +181,14 @@ public class GenericUtils {
         }
     }
 
+    public static final int length(byte ... a) {
+        if (a == null) {
+            return 0;
+        } else {
+            return a.length;
+        }
+    }
+
     @SafeVarargs
     public static final <T> int length(T ... a) {
         if (a == null) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8009a89f/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java
index 36bed35..07b8617 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java
@@ -18,6 +18,8 @@
  */
 package org.apache.sshd.common.util.buffer;
 
+import org.apache.sshd.common.util.GenericUtils;
+
 /**
  * TODO Add javadoc
  *
@@ -25,38 +27,55 @@ package org.apache.sshd.common.util.buffer;
  */
 public class BufferUtils {
 
-    public static String printHex(byte[] array) {
-        return printHex(array, 0, array.length);
+    public static String printHex(byte ... array) {
+        return printHex(array, 0, GenericUtils.length(array));
+    }
+
+    public static String printHex(char sep, byte ... array) {
+        return printHex(array, 0, GenericUtils.length(array), sep);
     }
 
     public static String printHex(byte[] array, int offset, int len) {
         return printHex(array, offset, len, ' ');
     }
 
+    public static final String  HEX_DIGITS="0123456789abcdef";
+
     public static String printHex(byte[] array, int offset, int len, char sep) {
-        StringBuilder sb = new StringBuilder();
-        for (int i = 0; i < len; i++) {
-            byte b = array[offset + i];
+        if (len <= 0) {
+            return "";
+        }
+
+        StringBuilder sb = new StringBuilder(len * 3 /* 2 HEX + sep */);
+        for (int curOffset = offset, maxOffset = offset + len; curOffset < maxOffset; curOffset++) {
+            byte b = array[curOffset];
             if (sb.length() > 0) {
                 sb.append(sep);
             }
-            sb.append(digits[(b >> 4) & 0x0F]);
-            sb.append(digits[b & 0x0F]);
+            sb.append(HEX_DIGITS.charAt((b >> 4) & 0x0F));
+            sb.append(HEX_DIGITS.charAt(b & 0x0F));
         }
+
         return sb.toString();
     }
 
     public static boolean equals(byte[] a1, byte[] a2) {
-        if (a1.length != a2.length) {
+        int len1 = GenericUtils.length(a1);
+        int len2 = GenericUtils.length(a2);
+        if (len1 != len2) {
             return false;
+        } else {
+            return equals(a1, 0, a2, 0, len1);
         }
-        return equals(a1, 0, a2, 0, a1.length);
     }
 
     public static boolean equals(byte[] a1, int a1Offset, byte[] a2, int a2Offset, int length) {
-        if (a1.length < a1Offset + length || a2.length < a2Offset + length) {
+        int len1 = GenericUtils.length(a1);
+        int len2 = GenericUtils.length(a2);
+        if ((len1 < (a1Offset + length)) || (len2 < (a2Offset + length))) {
             return false;
         }
+
         while (length-- > 0) {
             if (a1[a1Offset++] != a2[a2Offset++]) {
                 return false;
@@ -65,12 +84,6 @@ public class BufferUtils {
         return true;
     }
 
-    final static char[] digits = {
-	    '0' , '1' , '2' , '3' , '4' , '5' ,
-	    '6' , '7' , '8' , '9' , 'a' , 'b' ,
-	    'c' , 'd' , 'e' , 'f'
-    };
-
     public static final int getNextPowerOf2(int i) {
         int j = 1;
         while (j < i) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8009a89f/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java b/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java
index c166805..1edc936 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java
@@ -53,6 +53,10 @@ public class KexTest extends BaseTestSupport {
     private SshServer sshd;
     private int port;
 
+    public KexTest() {
+        super();
+    }
+
     @Before
     public void setUp() throws Exception {
         sshd = SshServer.setUpDefaultServer();
@@ -65,7 +69,9 @@ public class KexTest extends BaseTestSupport {
 
     @After
     public void tearDown() throws Exception {
-        sshd.stop(true);
+        if (sshd != null) {
+            sshd.stop(true);
+        }
     }
 
     @Test