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/06/09 13:04:48 UTC

mina-sshd git commit: [SSHD-443] Improve supported cipher detection

Repository: mina-sshd
Updated Branches:
  refs/heads/master 6380cc8d8 -> ff6a4b04f


[SSHD-443] Improve supported cipher detection


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

Branch: refs/heads/master
Commit: ff6a4b04f08bfc4a7d7aa2dd6a0814423939c7ac
Parents: 6380cc8
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Tue Jun 9 14:04:40 2015 +0300
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Tue Jun 9 14:04:40 2015 +0300

----------------------------------------------------------------------
 .../apache/sshd/common/cipher/BaseCipher.java   |  6 ++
 .../sshd/common/cipher/BuiltinCiphers.java      | 45 ++++++----
 .../org/apache/sshd/common/cipher/Cipher.java   |  7 ++
 .../apache/sshd/common/cipher/CipherNone.java   | 14 +++-
 .../test/java/org/apache/sshd/AgentTest.java    |  4 +-
 .../sshd/common/cipher/BuiltinCiphersTest.java  | 87 ++++++++++++++++++--
 6 files changed, 135 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ff6a4b04/sshd-core/src/main/java/org/apache/sshd/common/cipher/BaseCipher.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/cipher/BaseCipher.java b/sshd-core/src/main/java/org/apache/sshd/common/cipher/BaseCipher.java
index 8b21afa..de22140 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/cipher/BaseCipher.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/cipher/BaseCipher.java
@@ -22,6 +22,7 @@ import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
 
 import org.apache.sshd.common.SshException;
+import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.common.util.SecurityUtils;
 
 /**
@@ -71,6 +72,11 @@ public class BaseCipher implements Cipher {
     }
 
     @Override
+    public void update(byte[] input) throws Exception {
+        update(input, 0, GenericUtils.length(input));
+    }
+
+    @Override
     public void update(byte[] input, int inputOffset, int inputLen) throws Exception {
         cipher.update(input, inputOffset, inputLen, input, inputOffset);
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ff6a4b04/sshd-core/src/main/java/org/apache/sshd/common/cipher/BuiltinCiphers.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/cipher/BuiltinCiphers.java b/sshd-core/src/main/java/org/apache/sshd/common/cipher/BuiltinCiphers.java
index b16a22b..ddfe4ce 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/cipher/BuiltinCiphers.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/cipher/BuiltinCiphers.java
@@ -72,8 +72,10 @@ public enum BuiltinCiphers implements CipherFactory {
     private final String factoryName;
     private final int ivsize;
     private final int blocksize;
+    private final int keysize;
     private final String algorithm;
     private final String transformation;
+    private final boolean supported;
 
     @Override
     public final String getName() {
@@ -89,9 +91,16 @@ public enum BuiltinCiphers implements CipherFactory {
         this.factoryName = factoryName;
         this.ivsize = ivsize;
         this.blocksize = blocksize;
+        this.keysize = blocksize * Byte.SIZE;
         this.algorithm = algorithm;
         this.transformation = transformation;
-
+        /*
+         * This can be done once since in order to change the support the JVM
+         * needs to be stopped, some unlimited-strength files need be installed
+         * and then the JVM re-started. Therefore, the answer is not going to
+         * change while the JVM is running 
+         */
+        this.supported = checkSupported(this.transformation, this.keysize);
     }
 
     /**
@@ -101,45 +110,51 @@ public enum BuiltinCiphers implements CipherFactory {
      */
     @Override
     public boolean isSupported() {
+        return supported;
+    }
+
+    private static boolean checkSupported(String xform, int keyLength) {
         try {
-            int maxKeyLength = javax.crypto.Cipher.getMaxAllowedKeyLength(getAlgorithm());
-            return maxKeyLength >= (1l << getBlockSize());
+            int maxKeyLength = javax.crypto.Cipher.getMaxAllowedKeyLength(xform);
+            if (maxKeyLength >= keyLength) {
+                return true;
+            } else {
+                return false;   // debug breakpoint
+            }
         } catch (Exception e) {
             return false;
         }
     }
+    /**
+     * @return The key size (in bits) for the cipher
+     */
+    public int getKeySize() {
+        return keysize;
+    }
 
     /**
-     * Retrieves the size of the initialization vector
-     *
-     * @return
+     * @return The size of the initialization vector
      */
     public int getIVSize() {
         return ivsize;
     }
 
     /**
-     * Retrieves the block size for this cipher
-     *
-     * @return
+     * @return The block size for this cipher
      */
     public int getBlockSize() {
         return blocksize;
     }
 
     /**
-     * Retrieves the algorithm for this cipher
-     *
-     * @return
+     * @return The algorithm for this cipher
      */
     public String getAlgorithm() {
         return algorithm;
     }
 
     /**
-     * Retrieves the algorithm for this cipher
-     *
-     * @return
+     * @return The transformation for this cipher
      */
     public String getTransformation() {
         return transformation;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ff6a4b04/sshd-core/src/main/java/org/apache/sshd/common/cipher/Cipher.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/cipher/Cipher.java b/sshd-core/src/main/java/org/apache/sshd/common/cipher/Cipher.java
index 1770e2d..e50ce14 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/cipher/Cipher.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/cipher/Cipher.java
@@ -57,6 +57,13 @@ public interface Cipher  {
 
     /**
      * Performs in-place encryption or decryption on the given data.
+     * @param input The input/output bytes
+     * @throws Exception
+     */
+    void update(byte[] input) throws Exception;
+
+    /**
+     * Performs in-place encryption or decryption on the given data.
      * 
      * @param input
      * @param inputOffset

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ff6a4b04/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherNone.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherNone.java b/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherNone.java
index 63e791c..e454d03 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherNone.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherNone.java
@@ -18,6 +18,8 @@
  */
 package org.apache.sshd.common.cipher;
 
+import org.apache.sshd.common.util.GenericUtils;
+
 
 /**
  * Represents a no-op cipher.
@@ -28,15 +30,18 @@ package org.apache.sshd.common.cipher;
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
 public class CipherNone implements Cipher {
+    public CipherNone() {
+        super();
+    }
 
     @Override
     public int getIVSize() {
-        return 8;
+        return 8;   // dummy
     }
 
     @Override
     public int getBlockSize() {
-        return 16;
+        return 16;  // dummy
     }
 
     @Override
@@ -45,6 +50,11 @@ public class CipherNone implements Cipher {
     }
 
     @Override
+    public void update(byte[] input) throws Exception {
+        update(input, 0, GenericUtils.length(input));
+    }
+
+    @Override
     public void update(byte[] input, int inputOffset, int inputLen) throws Exception {
         // ignored - always succeeds
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ff6a4b04/sshd-core/src/test/java/org/apache/sshd/AgentTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/AgentTest.java b/sshd-core/src/test/java/org/apache/sshd/AgentTest.java
index 1890fb3..abdd37e 100644
--- a/sshd-core/src/test/java/org/apache/sshd/AgentTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/AgentTest.java
@@ -131,7 +131,7 @@ public class AgentTest extends BaseTestSupport {
                     client1.start();
                     
                     try(ClientSession session1 = client1.connect(username, "localhost", port1).await().getSession()) {
-                        session1.auth().verify(10L, TimeUnit.SECONDS);
+                        session1.auth().verify(15L, TimeUnit.SECONDS);
 
                         try(ChannelShell channel1 = session1.createShellChannel();
                             ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -156,7 +156,7 @@ public class AgentTest extends BaseTestSupport {
                                     client2.start();
                                     
                                     try(ClientSession session2 = client2.connect(username, "localhost", port2).await().getSession()) {
-                                        session2.auth().verify(5L, TimeUnit.SECONDS);
+                                        session2.auth().verify(15L, TimeUnit.SECONDS);
 
                                         try(ChannelShell channel2 = session2.createShellChannel()) {
                                             channel2.setIn(shellFactory.shell.getIn());

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ff6a4b04/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java b/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java
index bab5f31..4161b8b 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java
@@ -22,16 +22,20 @@ package org.apache.sshd.common.cipher;
 import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.EnumSet;
 import java.util.List;
 import java.util.Random;
 import java.util.Set;
 
+import org.apache.sshd.client.SshClient;
+import org.apache.sshd.common.FactoryManager;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.NamedResource;
 import org.apache.sshd.common.cipher.BuiltinCiphers.ParseResult;
 import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.server.SshServer;
 import org.apache.sshd.util.BaseTestSupport;
 import org.junit.FixMethodOrder;
 import org.junit.Test;
@@ -103,18 +107,83 @@ public class BuiltinCiphersTest extends BaseTestSupport {
         assertEquals("Incomplete coverage", BuiltinCiphers.VALUES, avail);
     }
 
-    @Test
+    @Test   // make sure that if a cipher is reported as supported we can indeed use it
     public void testSupportedCipher() throws Exception {
-        for (BuiltinCiphers expected : BuiltinCiphers.VALUES) {
-            if (!expected.isSupported()) {
-                System.out.append("Skip unsupported cipher: ").println(expected);
-                continue;
+        Exception err = null;
+        Random  rnd = new Random(System.nanoTime());
+        for (BuiltinCiphers c : BuiltinCiphers.VALUES) {
+            if (c.isSupported()) {
+                try {
+                    testCipherEncryption(rnd, c.create());
+                } catch(Exception e) {
+                    System.err.println("Failed (" + e.getClass().getSimpleName() + ") to encrypt using " + c + ": " + e.getMessage());
+                    err = e;
+                }
+            } else {
+                System.out.append("Skip unsupported cipher: ").println(c);
+            }
+        }
+        
+        if (err != null) {
+            throw err;
+        }
+    }
+
+    @Test   // make sure that the reported support matches reality by trying to encrypt something
+    public void testCipherSupportDetection() throws Exception {
+        Random  rnd = new Random(System.nanoTime());
+        for (BuiltinCiphers c : BuiltinCiphers.VALUES) {
+            try {
+                testCipherEncryption(rnd, c.create());
+                assertTrue("Mismatched support report for " + c, c.isSupported());
+            } catch(Exception e) {
+                assertFalse("Mismatched support report for " + c, c.isSupported());
+            }
+        }
+    }
+
+    @Test
+    public void testSshClientSupportedCiphersConfiguration() throws Exception {
+        try(SshClient client = SshClient.setUpDefaultClient()) {
+            testSupportedCiphersConfiguration(client);
+        }
+    }
+
+    @Test
+    public void testSshSercerSupportedCiphersConfiguration() throws Exception {
+        try(SshServer server = SshServer.setUpDefaultServer()) {
+            testSupportedCiphersConfiguration(server);
+        }
+    }
+
+    private static <M extends FactoryManager> M testSupportedCiphersConfiguration(M manager) {
+        Collection<? extends NamedResource> factories = manager.getCipherFactories();
+        List<String> names = NamedResource.Utils.getNameList(factories);
+        for (BuiltinCiphers c : BuiltinCiphers.VALUES) {
+            if (BuiltinCiphers.none.equals(c)) {
+                continue;   // not always included by default + it is a dummy cipher
+            }
+
+            // for now, all key sizes below 128 are supported in JVM(s)
+            if (c.getKeySize() <= 128) {
+                assertTrue("Supported cipher not configured by default: " + c, names.contains(c.getName()));
             }
-            Cipher cipher = expected.create();
-            byte[] key = new byte[cipher.getBlockSize()];
-            byte[] iv = new byte[cipher.getIVSize()];
-            cipher.init(Cipher.Mode.Encrypt, key, iv);
         }
+        
+        return manager;
+    }
+
+    private static void testCipherEncryption(Random rnd, Cipher cipher) throws Exception {
+        byte[] key=new byte[cipher.getBlockSize()];
+        rnd.nextBytes(key);
+        byte[] iv=new byte[cipher.getIVSize()];
+        rnd.nextBytes(iv);
+        cipher.init(Cipher.Mode.Encrypt, key, iv);
+
+        byte[] data=new byte[cipher.getBlockSize()];
+        rnd.nextBytes(data);
+        
+        cipher.update(data);
     }
 
     @Test