Repository: mina-sshd
Updated Branches:
  refs/heads/master cccaa25b6 -> 458c38fa5


Using (new method) ByteArrayBuffer#getCompactClone to wrap raw data buffers


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

Branch: refs/heads/master
Commit: bced9aa56c7ee1801c2d19ba255acbce2a69f6e5
Parents: cccaa25
Author: Lyor Goldstein <[email protected]>
Authored: Mon Jan 18 07:41:26 2016 +0200
Committer: Lyor Goldstein <[email protected]>
Committed: Mon Jan 18 07:41:26 2016 +0200

----------------------------------------------------------------------
 .../client/auth/pubkey/UserAuthPublicKey.java   | 10 +++++-
 .../sshd/common/forward/TcpipClientChannel.java |  8 +++--
 .../sshd/common/session/AbstractSession.java    |  4 ++-
 .../common/util/buffer/ByteArrayBuffer.java     | 34 ++++++++++++++++++++
 .../sshd/server/forward/TcpipServerChannel.java |  4 +--
 .../common/util/buffer/BufferUtilsTest.java     | 19 +++++++++++
 .../apache/sshd/deprecated/UserAuthAgent.java   |  9 ++++--
 7 files changed, 78 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bced9aa5/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/UserAuthPublicKey.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/UserAuthPublicKey.java
 
b/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/UserAuthPublicKey.java
index 95bec60..d9aff93 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/UserAuthPublicKey.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/UserAuthPublicKey.java
@@ -142,7 +142,15 @@ public class UserAuthPublicKey extends AbstractUserAuth 
implements SignatureFact
         bs.putString(algo);
         bs.putPublicKey(key);
 
-        byte[] sig = current.sign(bs.getCompactData());
+        byte[] contents = bs.getCompactData();
+        byte[] sig = current.sign(contents);
+        if (log.isTraceEnabled()) {
+            log.trace("processAuthDataRequest({})[{}] name={}, key type={}, 
fingerprint={} - verification data={}",
+                      session, service, name, algo, 
KeyUtils.getFingerPrint(key), BufferUtils.printHex(contents));
+            log.trace("processAuthDataRequest({})[{}] name={}, key type={}, 
fingerprint={} - generated signature={}",
+                      session, service, name, algo, 
KeyUtils.getFingerPrint(key), BufferUtils.printHex(sig));
+        }
+
         bs = new ByteArrayBuffer(algo.length() + sig.length + Long.SIZE, 
false);
         bs.putString(algo);
         bs.putBytes(sig);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bced9aa5/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
 
b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
index a5b4281..06c7f88 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
@@ -126,9 +126,13 @@ public class TcpipClientChannel extends 
AbstractClientChannel {
     @Override
     protected synchronized void doWriteData(byte[] data, int off, int len) 
throws IOException {
         // Make sure we copy the data as the incoming buffer may be reused
-        Buffer buf = new ByteArrayBuffer(data, off, len);
-        buf = new ByteArrayBuffer(buf.getCompactData());
+        Buffer buf = ByteArrayBuffer.getCompactClone(data, off, len);
         localWindow.consumeAndCheck(len);
         serverSession.write(buf);
     }
+
+    @Override
+    protected void doWriteExtendedData(byte[] data, int off, int len) throws 
IOException {
+        throw new UnsupportedOperationException(type + "Tcpip channel does not 
support extended data");
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bced9aa5/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java 
b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
index e5eaa54..88447ae 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
@@ -1714,8 +1714,10 @@ public abstract class AbstractSession extends 
AbstractKexFactoryManager implemen
     }
 
     protected void requestSuccess(Buffer buffer) throws Exception {
+        // use a copy of the original data in case it is re-used on return
+        Buffer resultBuf = ByteArrayBuffer.getCompactClone(buffer.array(), 
buffer.rpos(), buffer.available());
         synchronized (requestResult) {
-            requestResult.set(new ByteArrayBuffer(buffer.getCompactData()));
+            requestResult.set(resultBuf);
             resetIdleTimeout();
             requestResult.notify();
         }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bced9aa5/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/ByteArrayBuffer.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/ByteArrayBuffer.java
 
b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/ByteArrayBuffer.java
index 650af18..f2ee567 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/ByteArrayBuffer.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/ByteArrayBuffer.java
@@ -21,7 +21,9 @@ package org.apache.sshd.common.util.buffer;
 
 import java.nio.charset.Charset;
 
+import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.common.util.Int2IntFunction;
+import org.apache.sshd.common.util.NumberUtils;
 import org.apache.sshd.common.util.Readable;
 import org.apache.sshd.common.util.ValidateUtils;
 
@@ -195,4 +197,36 @@ public final class ByteArrayBuffer extends Buffer {
     protected int size() {
         return data.length;
     }
+
+    /**
+     * Creates a compact buffer (i.e., one that starts at offset zero) 
containing a <U>copy</U>
+     * of the original data
+     *
+     * @param data   The original data buffer
+     * @return A {@link ByteArrayBuffer} containing a <U>copy</U> of the 
original data
+     * starting at zero read position
+     * @see #getCompactClone(byte[], int, int)
+     */
+    public static ByteArrayBuffer getCompactClone(byte[] data) {
+        return getCompactClone(data, 0, NumberUtils.length(data));
+    }
+
+    /**
+     * Creates a compact buffer (i.e., one that starts at offset zero) 
containing a <U>copy</U>
+     * of the original data
+     *
+     * @param data   The original data buffer
+     * @param offset The offset of the valid data in the buffer
+     * @param len    The size (in bytes) of of the valid data in the buffer
+     * @return A {@link ByteArrayBuffer} containing a <U>copy</U> of the 
original data
+     * starting at zero read position
+     */
+    public static ByteArrayBuffer getCompactClone(byte[] data, int offset, int 
len) {
+        byte[] clone = (len > 0) ? new byte[len] : 
GenericUtils.EMPTY_BYTE_ARRAY;
+        if (len > 0) {
+            System.arraycopy(data, offset, clone, 0, len);
+        }
+
+        return new ByteArrayBuffer(clone, true);
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bced9aa5/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java
 
b/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java
index 6519555..bf97181 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java
@@ -295,8 +295,7 @@ public class TcpipServerChannel extends 
AbstractServerChannel {
     @Override
     protected void doWriteData(byte[] data, int off, final int len) throws 
IOException {
         // Make sure we copy the data as the incoming buffer may be reused
-        Buffer buf = new ByteArrayBuffer(data, off, len);
-        buf = new ByteArrayBuffer(buf.getCompactData());
+        Buffer buf = ByteArrayBuffer.getCompactClone(data, off, len);
         ioSession.write(buf).addListener(new 
SshFutureListener<IoWriteFuture>() {
             @SuppressWarnings("synthetic-access")
             @Override
@@ -315,5 +314,4 @@ public class TcpipServerChannel extends 
AbstractServerChannel {
     protected void doWriteExtendedData(byte[] data, int off, int len) throws 
IOException {
         throw new UnsupportedOperationException(type + "Tcpip channel does not 
support extended data");
     }
-
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bced9aa5/sshd-core/src/test/java/org/apache/sshd/common/util/buffer/BufferUtilsTest.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/test/java/org/apache/sshd/common/util/buffer/BufferUtilsTest.java
 
b/sshd-core/src/test/java/org/apache/sshd/common/util/buffer/BufferUtilsTest.java
index 23a6599..30eb2a0 100644
--- 
a/sshd-core/src/test/java/org/apache/sshd/common/util/buffer/BufferUtilsTest.java
+++ 
b/sshd-core/src/test/java/org/apache/sshd/common/util/buffer/BufferUtilsTest.java
@@ -20,6 +20,7 @@
 package org.apache.sshd.common.util.buffer;
 
 import java.nio.charset.StandardCharsets;
+import java.util.Random;
 
 import org.apache.sshd.util.test.BaseTestSupport;
 import org.junit.FixMethodOrder;
@@ -48,4 +49,22 @@ public class BufferUtilsTest extends BaseTestSupport {
             assertArrayEquals("Mismatched result for sep='" + sepName + "'", 
expData, actData);
         }
     }
+
+    @Test
+    public void testGetCompactClone() {
+        byte[] expected = 
getCurrentTestName().getBytes(StandardCharsets.UTF_8);
+        final int OFFSET = Byte.SIZE / 2;
+        byte[] data = new byte[expected.length + 2 * OFFSET];
+        Random rnd = new Random(System.nanoTime());
+        rnd.nextBytes(data);
+        System.arraycopy(expected, 0, data, OFFSET, expected.length);
+
+        Buffer buf = ByteArrayBuffer.getCompactClone(data, OFFSET, 
expected.length);
+        assertEquals("Mismatched cloned buffer read position", 0, buf.rpos());
+        assertEquals("Mismatched cloned buffer available size", 
expected.length, buf.available());
+
+        byte[] actual = buf.array();
+        assertNotSame("Original data not cloned", data, actual);
+        assertArrayEquals("Mismatched cloned contents", expected, actual);
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bced9aa5/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthAgent.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthAgent.java 
b/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthAgent.java
index ba58e64..5d26ab0 100644
--- a/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthAgent.java
+++ b/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthAgent.java
@@ -72,9 +72,12 @@ public class UserAuthAgent extends AbstractUserAuth {
             bs.putString(KeyUtils.getKeyType(key));
             bs.putPublicKey(key);
 
-            Buffer bs2 = new ByteArrayBuffer();
-            bs2.putString(KeyUtils.getKeyType(key));
-            bs2.putBytes(agent.sign(key, bs.getCompactData()));
+            String keyType = KeyUtils.getKeyType(key);
+            byte[] contents = bs.getCompactData();
+            byte[] signature = agent.sign(key, contents);
+            Buffer bs2 = new ByteArrayBuffer(keyType.length() + 
signature.length + Long.SIZE, false);
+            bs2.putString(keyType);
+            bs2.putBytes(signature);
             buffer.putBytes(bs2.array(), bs2.rpos(), bs2.available());
 
             session.writePacket(buffer);

Reply via email to