You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gn...@apache.org on 2017/03/28 11:31:08 UTC

[1/3] mina-sshd git commit: Revert "[SSHD-727] Upgrade used EdDSA artifact version to 1.1"

Repository: mina-sshd
Updated Branches:
  refs/heads/master be51bdc8a -> a1cb02b41


Revert "[SSHD-727] Upgrade used EdDSA artifact version to 1.1"

This reverts commit cbb92d2a89600ee8a34abdf7d84cdc08291dcbf5.


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

Branch: refs/heads/master
Commit: 3148667309bf37b98714fc87ec20cc089bb4a30a
Parents: be51bdc
Author: Guillaume Nodet <gn...@apache.org>
Authored: Tue Mar 28 11:59:07 2017 +0200
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Tue Mar 28 11:59:07 2017 +0200

----------------------------------------------------------------------
 pom.xml                                         |   6 +-
 .../common/util/security/SecurityUtils.java     |  31 ++-
 .../security/eddsa/EdDSASecurityProvider.java   | 205 +++++++++++++++++++
 .../eddsa/EdDSASecurityProviderRegistrar.java   |   2 +-
 .../eddsa/EdDSASecurityProviderUtils.java       | 197 ------------------
 .../OpenSSHEd25519PrivateKeyEntryDecoder.java   |   2 +-
 .../common/signature/SignaturesDevelopment.java |   6 +-
 .../util/security/eddsa/Ed25519VectorsTest.java |  12 +-
 .../EdDSASecurityProviderRegistrarTest.java     |   2 -
 9 files changed, 234 insertions(+), 229 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/31486673/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 06ec711..806f7fb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -104,7 +104,7 @@
         <gmaven.plugin.version>2.0</gmaven.plugin.version>
         <groovy.major.version>2</groovy.major.version>
         <groovy.minor.version>4</groovy.minor.version>
-        <groovy.release.version>9</groovy.release.version>
+        <groovy.release.version>8</groovy.release.version>
         <groovy.compliance.level>2.0</groovy.compliance.level>
         <groovy.version>${groovy.major.version}.${groovy.minor.version}.${groovy.release.version}</groovy.version>        
 
@@ -202,7 +202,7 @@
             <dependency>
                 <groupId>net.i2p.crypto</groupId>
                 <artifactId>eddsa</artifactId>
-                <version>0.1.1</version>
+                <version>0.1.0</version>
             </dependency>
 
             <dependency>
@@ -594,7 +594,7 @@
                         <dependency>
                             <groupId>com.puppycrawl.tools</groupId>
                             <artifactId>checkstyle</artifactId>
-                            <version>7.6</version>
+                            <version>7.5.1</version>
                             <exclusions>
                                 <!-- MCHECKSTYLE-156 -->
                                 <exclusion>

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/31486673/sshd-core/src/main/java/org/apache/sshd/common/util/security/SecurityUtils.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/security/SecurityUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/security/SecurityUtils.java
index b86bcc3..6aff088 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/security/SecurityUtils.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/security/SecurityUtils.java
@@ -73,7 +73,7 @@ import org.apache.sshd.common.util.buffer.Buffer;
 import org.apache.sshd.common.util.security.bouncycastle.BouncyCastleGeneratorHostKeyProvider;
 import org.apache.sshd.common.util.security.bouncycastle.BouncyCastleKeyPairResourceParser;
 import org.apache.sshd.common.util.security.bouncycastle.BouncyCastleRandomFactory;
-import org.apache.sshd.common.util.security.eddsa.EdDSASecurityProviderUtils;
+import org.apache.sshd.common.util.security.eddsa.EdDSASecurityProvider;
 import org.apache.sshd.common.util.threads.ThreadUtils;
 import org.apache.sshd.server.keyprovider.AbstractGeneratorHostKeyProvider;
 import org.slf4j.Logger;
@@ -91,13 +91,12 @@ public final class SecurityUtils {
     public static final String BOUNCY_CASTLE = "BC";
 
     /**
-     * EDDSA support - should match {@code EdDSAKey.KEY_ALGORITHM}
+     * EDDSA support
      */
     public static final String EDDSA = "EdDSA";
 
     // A copy-paste from the original, but we don't want to drag the classes into the classpath
-    // See EdDSANamedCurveTable.CURVE_ED25519_SHA512
-    public static final String CURVE_ED25519_SHA512 = "SHA512withEd25519";
+    public static final String CURVE_ED25519_SHA512 = "ed25519-sha-512";
 
     /**
      * System property used to configure the value for the maximum supported Diffie-Hellman
@@ -539,7 +538,7 @@ public final class SecurityUtils {
             throw new UnsupportedOperationException(EDDSA + " provider N/A");
         }
 
-        return EdDSASecurityProviderUtils.getEDDSAPublicKeyEntryDecoder();
+        return EdDSASecurityProvider.getEDDSAPublicKeyEntryDecoder();
     }
 
     public static PrivateKeyEntryDecoder<? extends PublicKey, ? extends PrivateKey> getOpenSSHEDDSAPrivateKeyEntryDecoder() {
@@ -547,35 +546,35 @@ public final class SecurityUtils {
             throw new UnsupportedOperationException(EDDSA + " provider N/A");
         }
 
-        return EdDSASecurityProviderUtils.getOpenSSHEDDSAPrivateKeyEntryDecoder();
+        return EdDSASecurityProvider.getOpenSSHEDDSAPrivateKeyEntryDecoder();
     }
 
     public static org.apache.sshd.common.signature.Signature getEDDSASigner() {
         if (isEDDSACurveSupported()) {
-            return EdDSASecurityProviderUtils.getEDDSASignature();
+            return EdDSASecurityProvider.getEDDSASignature();
         }
 
         throw new UnsupportedOperationException(EDDSA + " Signer not available");
     }
 
     public static int getEDDSAKeySize(Key key) {
-        return EdDSASecurityProviderUtils.getEDDSAKeySize(key);
+        return EdDSASecurityProvider.getEDDSAKeySize(key);
     }
 
     public static Class<? extends PublicKey> getEDDSAPublicKeyType() {
-        return isEDDSACurveSupported() ? EdDSASecurityProviderUtils.getEDDSAPublicKeyType() : PublicKey.class;
+        return isEDDSACurveSupported() ? EdDSASecurityProvider.getEDDSAPublicKeyType() : PublicKey.class;
     }
 
     public static Class<? extends PrivateKey> getEDDSAPrivateKeyType() {
-        return isEDDSACurveSupported() ? EdDSASecurityProviderUtils.getEDDSAPrivateKeyType() : PrivateKey.class;
+        return isEDDSACurveSupported() ? EdDSASecurityProvider.getEDDSAPrivateKeyType() : PrivateKey.class;
     }
 
     public static boolean compareEDDSAPPublicKeys(PublicKey k1, PublicKey k2) {
-        return isEDDSACurveSupported() ? EdDSASecurityProviderUtils.compareEDDSAPPublicKeys(k1, k2) : false;
+        return isEDDSACurveSupported() ? EdDSASecurityProvider.compareEDDSAPPublicKeys(k1, k2) : false;
     }
 
     public static boolean compareEDDSAPrivateKeys(PrivateKey k1, PrivateKey k2) {
-        return isEDDSACurveSupported() ? EdDSASecurityProviderUtils.compareEDDSAPrivateKeys(k1, k2) : false;
+        return isEDDSACurveSupported() ? EdDSASecurityProvider.compareEDDSAPrivateKeys(k1, k2) : false;
     }
 
     public static PublicKey recoverEDDSAPublicKey(PrivateKey key) throws GeneralSecurityException {
@@ -583,7 +582,7 @@ public final class SecurityUtils {
             throw new NoSuchAlgorithmException(EDDSA + " provider not supported");
         }
 
-        return EdDSASecurityProviderUtils.recoverEDDSAPublicKey(key);
+        return EdDSASecurityProvider.recoverEDDSAPublicKey(key);
     }
 
     public static PublicKey generateEDDSAPublicKey(String keyType, byte[] seed) throws GeneralSecurityException {
@@ -595,7 +594,7 @@ public final class SecurityUtils {
             throw new NoSuchAlgorithmException(EDDSA + " provider not supported");
         }
 
-        return EdDSASecurityProviderUtils.generateEDDSAPublicKey(seed);
+        return EdDSASecurityProvider.generateEDDSAPublicKey(seed);
     }
 
     public static <B extends Buffer> B putRawEDDSAPublicKey(B buffer, PublicKey key) {
@@ -603,7 +602,7 @@ public final class SecurityUtils {
             throw new UnsupportedOperationException(EDDSA + " provider not supported");
         }
 
-        return EdDSASecurityProviderUtils.putRawEDDSAPublicKey(buffer, key);
+        return EdDSASecurityProvider.putRawEDDSAPublicKey(buffer, key);
     }
 
     public static <B extends Buffer> B putEDDSAKeyPair(B buffer, KeyPair kp) {
@@ -615,7 +614,7 @@ public final class SecurityUtils {
             throw new UnsupportedOperationException(EDDSA + " provider not supported");
         }
 
-        return EdDSASecurityProviderUtils.putEDDSAKeyPair(buffer, pubKey, prvKey);
+        return EdDSASecurityProvider.putEDDSAKeyPair(buffer, pubKey, prvKey);
     }
 
     public static KeyPair extractEDDSAKeyPair(Buffer buffer, String keyType) throws GeneralSecurityException {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/31486673/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProvider.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProvider.java b/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProvider.java
new file mode 100644
index 0000000..e039547
--- /dev/null
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProvider.java
@@ -0,0 +1,205 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sshd.common.util.security.eddsa;
+
+import java.security.GeneralSecurityException;
+import java.security.InvalidKeyException;
+import java.security.Key;
+import java.security.KeyFactory;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.Provider;
+import java.security.PublicKey;
+import java.util.Arrays;
+import java.util.Objects;
+
+import net.i2p.crypto.eddsa.EdDSAKey;
+import net.i2p.crypto.eddsa.EdDSAPrivateKey;
+import net.i2p.crypto.eddsa.EdDSAPublicKey;
+import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;
+import net.i2p.crypto.eddsa.spec.EdDSAParameterSpec;
+import net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec;
+import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec;
+
+import org.apache.sshd.common.config.keys.PrivateKeyEntryDecoder;
+import org.apache.sshd.common.config.keys.PublicKeyEntryDecoder;
+import org.apache.sshd.common.keyprovider.KeyPairProvider;
+import org.apache.sshd.common.util.ValidateUtils;
+import org.apache.sshd.common.util.buffer.Buffer;
+import org.apache.sshd.common.util.security.SecurityUtils;
+
+/**
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public class EdDSASecurityProvider extends Provider {
+    private static final long serialVersionUID = -6183277432144104981L;
+
+    public EdDSASecurityProvider() {
+        super(SecurityUtils.EDDSA, 0.1, "net.i2p security provider wrapper");
+
+        // see https://docs.oracle.com/javase/8/docs/technotes/guides/security/crypto/HowToImplAProvider.html
+        put("KeyPairGenerator." + SecurityUtils.EDDSA, "net.i2p.crypto.eddsa.KeyPairGenerator");
+        put("KeyFactory." + SecurityUtils.EDDSA, "net.i2p.crypto.eddsa.KeyFactory");
+        put("Signature." + EdDSANamedCurveTable.CURVE_ED25519_SHA512, "net.i2p.crypto.eddsa.EdDSAEngine");
+    }
+
+    public static Class<? extends PublicKey> getEDDSAPublicKeyType() {
+        return EdDSAPublicKey.class;
+    }
+
+    public static Class<? extends PrivateKey> getEDDSAPrivateKeyType() {
+        return EdDSAPrivateKey.class;
+    }
+
+    public static int getEDDSAKeySize(Key key) {
+        return (SecurityUtils.isEDDSACurveSupported() && (key instanceof EdDSAKey)) ? 256 : -1;
+    }
+
+    public static boolean compareEDDSAPPublicKeys(PublicKey k1, PublicKey k2) {
+        if (!SecurityUtils.isEDDSACurveSupported()) {
+            return false;
+        }
+
+        if ((k1 instanceof EdDSAPublicKey) && (k2 instanceof EdDSAPublicKey)) {
+            if (Objects.equals(k1, k2)) {
+                return true;
+            } else if (k1 == null || k2 == null) {
+                return false;   // both null is covered by Objects#equals
+            }
+
+            EdDSAPublicKey ed1 = (EdDSAPublicKey) k1;
+            EdDSAPublicKey ed2 = (EdDSAPublicKey) k2;
+            return Arrays.equals(ed1.getAbyte(), ed2.getAbyte())
+                && compareEDDSAKeyParams(ed1.getParams(), ed2.getParams());
+        }
+
+        return false;
+    }
+
+    public static boolean isEDDSASignatureAlgorithm(String algorithm) {
+        return EdDSANamedCurveTable.CURVE_ED25519_SHA512.equalsIgnoreCase(algorithm);
+    }
+
+    public static EdDSAPublicKey recoverEDDSAPublicKey(PrivateKey key) throws GeneralSecurityException {
+        ValidateUtils.checkTrue(SecurityUtils.isEDDSACurveSupported(), SecurityUtils.EDDSA + " not supported");
+        if (!(key instanceof EdDSAPrivateKey)) {
+            throw new InvalidKeyException("Private key is not " + SecurityUtils.EDDSA);
+        }
+
+        EdDSAPrivateKey prvKey = (EdDSAPrivateKey) key;
+        EdDSAPublicKeySpec keySpec = new EdDSAPublicKeySpec(prvKey.getSeed(), prvKey.getParams());
+        KeyFactory factory = SecurityUtils.getKeyFactory(SecurityUtils.EDDSA);
+        return EdDSAPublicKey.class.cast(factory.generatePublic(keySpec));
+    }
+
+    public static org.apache.sshd.common.signature.Signature getEDDSASignature() {
+        ValidateUtils.checkTrue(SecurityUtils.isEDDSACurveSupported(), SecurityUtils.EDDSA + " not supported");
+        return new SignatureEd25519();
+    }
+
+    public static boolean isEDDSAKeyFactoryAlgorithm(String algorithm) {
+        return SecurityUtils.EDDSA.equalsIgnoreCase(algorithm);
+    }
+
+    public static boolean isEDDSAKeyPairGeneratorAlgorithm(String algorithm) {
+        return SecurityUtils.EDDSA.equalsIgnoreCase(algorithm);
+    }
+
+    public static PublicKeyEntryDecoder<? extends PublicKey, ? extends PrivateKey> getEDDSAPublicKeyEntryDecoder() {
+        ValidateUtils.checkTrue(SecurityUtils.isEDDSACurveSupported(), SecurityUtils.EDDSA + " not supported");
+        return Ed25519PublicKeyDecoder.INSTANCE;
+    }
+
+    public static PrivateKeyEntryDecoder<? extends PublicKey, ? extends PrivateKey> getOpenSSHEDDSAPrivateKeyEntryDecoder() {
+        ValidateUtils.checkTrue(SecurityUtils.isEDDSACurveSupported(), SecurityUtils.EDDSA + " not supported");
+        return OpenSSHEd25519PrivateKeyEntryDecoder.INSTANCE;
+    }
+
+    public static boolean compareEDDSAPrivateKeys(PrivateKey k1, PrivateKey k2) {
+        if (!SecurityUtils.isEDDSACurveSupported()) {
+            return false;
+        }
+
+        if ((k1 instanceof EdDSAPrivateKey) && (k2 instanceof EdDSAPrivateKey)) {
+            if (Objects.equals(k1, k2)) {
+                return true;
+            } else if (k1 == null || k2 == null) {
+                return false;   // both null is covered by Objects#equals
+            }
+
+            EdDSAPrivateKey ed1 = (EdDSAPrivateKey) k1;
+            EdDSAPrivateKey ed2 = (EdDSAPrivateKey) k2;
+            return Arrays.equals(ed1.getSeed(), ed2.getSeed())
+                && compareEDDSAKeyParams(ed1.getParams(), ed2.getParams());
+        }
+
+        return false;
+    }
+
+    public static boolean compareEDDSAKeyParams(EdDSAParameterSpec s1, EdDSAParameterSpec s2) {
+        if (Objects.equals(s1, s2)) {
+            return true;
+        } else if (s1 == null || s2 == null) {
+            return false;   // both null is covered by Objects#equals
+        } else {
+            return Objects.equals(s1.getHashAlgorithm(), s2.getHashAlgorithm())
+                && Objects.equals(s1.getCurve(), s2.getCurve())
+                && Objects.equals(s1.getB(), s2.getB());
+        }
+    }
+
+    public static PublicKey generateEDDSAPublicKey(byte[] seed) throws GeneralSecurityException {
+        if (!SecurityUtils.isEDDSACurveSupported()) {
+            throw new NoSuchAlgorithmException(SecurityUtils.EDDSA + " not supported");
+        }
+
+        EdDSAParameterSpec params = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512);
+        EdDSAPublicKeySpec keySpec = new EdDSAPublicKeySpec(seed, params);
+        KeyFactory factory = SecurityUtils.getKeyFactory(SecurityUtils.EDDSA);
+        return factory.generatePublic(keySpec);
+    }
+
+    public static PrivateKey generateEDDSAPrivateKey(byte[] seed) throws GeneralSecurityException {
+        if (!SecurityUtils.isEDDSACurveSupported()) {
+            throw new NoSuchAlgorithmException(SecurityUtils.EDDSA + " not supported");
+        }
+
+        EdDSAParameterSpec params = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512);
+        EdDSAPrivateKeySpec keySpec = new EdDSAPrivateKeySpec(seed, params);
+        KeyFactory factory = SecurityUtils.getKeyFactory(SecurityUtils.EDDSA);
+        return factory.generatePrivate(keySpec);
+    }
+
+    public static <B extends Buffer> B putRawEDDSAPublicKey(B buffer, PublicKey key) {
+        ValidateUtils.checkTrue(SecurityUtils.isEDDSACurveSupported(), SecurityUtils.EDDSA + " not supported");
+        EdDSAPublicKey edKey = ValidateUtils.checkInstanceOf(key, EdDSAPublicKey.class, "Not an EDDSA public key: %s", key);
+        byte[] seed = Ed25519PublicKeyDecoder.getSeedValue(edKey);
+        ValidateUtils.checkNotNull(seed, "No seed extracted from key: %s", edKey.getA());
+        buffer.putString(KeyPairProvider.SSH_ED25519);
+        buffer.putBytes(seed);
+        return buffer;
+    }
+
+    public static <B extends Buffer> B putEDDSAKeyPair(B buffer, PublicKey pubKey, PrivateKey prvKey) {
+        ValidateUtils.checkTrue(SecurityUtils.isEDDSACurveSupported(), SecurityUtils.EDDSA + " not supported");
+        ValidateUtils.checkInstanceOf(pubKey, EdDSAPublicKey.class, "Not an EDDSA public key: %s", pubKey);
+        ValidateUtils.checkInstanceOf(prvKey, EdDSAPrivateKey.class, "Not an EDDSA private key: %s", prvKey);
+        throw new UnsupportedOperationException("Full SSHD-440 implementation N/A");
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/31486673/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderRegistrar.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderRegistrar.java b/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderRegistrar.java
index 2bb3f95..b463d27 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderRegistrar.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderRegistrar.java
@@ -36,7 +36,7 @@ import org.apache.sshd.common.util.threads.ThreadUtils;
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
 public class EdDSASecurityProviderRegistrar extends AbstractSecurityProviderRegistrar {
-    public static final String PROVIDER_CLASS = "net.i2p.crypto.eddsa.EdDSASecurityProvider";
+    public static final String PROVIDER_CLASS = "org.apache.sshd.common.util.security.eddsa.EdDSASecurityProvider";
     // Do not define a static registrar instance to minimize class loading issues
     private final AtomicReference<Boolean> supportHolder = new AtomicReference<>(null);
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/31486673/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderUtils.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderUtils.java
deleted file mode 100644
index b5d79f0..0000000
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderUtils.java
+++ /dev/null
@@ -1,197 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.sshd.common.util.security.eddsa;
-
-import java.security.GeneralSecurityException;
-import java.security.InvalidKeyException;
-import java.security.Key;
-import java.security.KeyFactory;
-import java.security.NoSuchAlgorithmException;
-import java.security.PrivateKey;
-import java.security.PublicKey;
-import java.util.Arrays;
-import java.util.Objects;
-
-import net.i2p.crypto.eddsa.EdDSAKey;
-import net.i2p.crypto.eddsa.EdDSAPrivateKey;
-import net.i2p.crypto.eddsa.EdDSAPublicKey;
-import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;
-import net.i2p.crypto.eddsa.spec.EdDSAParameterSpec;
-import net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec;
-import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec;
-
-import org.apache.sshd.common.config.keys.PrivateKeyEntryDecoder;
-import org.apache.sshd.common.config.keys.PublicKeyEntryDecoder;
-import org.apache.sshd.common.keyprovider.KeyPairProvider;
-import org.apache.sshd.common.util.ValidateUtils;
-import org.apache.sshd.common.util.buffer.Buffer;
-import org.apache.sshd.common.util.security.SecurityUtils;
-
-/**
- * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
- */
-public final class EdDSASecurityProviderUtils {
-    private EdDSASecurityProviderUtils() {
-        throw new UnsupportedOperationException("No instance");
-    }
-
-    public static Class<? extends PublicKey> getEDDSAPublicKeyType() {
-        return EdDSAPublicKey.class;
-    }
-
-    public static Class<? extends PrivateKey> getEDDSAPrivateKeyType() {
-        return EdDSAPrivateKey.class;
-    }
-
-    public static int getEDDSAKeySize(Key key) {
-        return (SecurityUtils.isEDDSACurveSupported() && (key instanceof EdDSAKey)) ? 256 : -1;
-    }
-
-    public static boolean compareEDDSAPPublicKeys(PublicKey k1, PublicKey k2) {
-        if (!SecurityUtils.isEDDSACurveSupported()) {
-            return false;
-        }
-
-        if ((k1 instanceof EdDSAPublicKey) && (k2 instanceof EdDSAPublicKey)) {
-            if (Objects.equals(k1, k2)) {
-                return true;
-            } else if (k1 == null || k2 == null) {
-                return false;   // both null is covered by Objects#equals
-            }
-
-            EdDSAPublicKey ed1 = (EdDSAPublicKey) k1;
-            EdDSAPublicKey ed2 = (EdDSAPublicKey) k2;
-            return Arrays.equals(ed1.getAbyte(), ed2.getAbyte())
-                && compareEDDSAKeyParams(ed1.getParams(), ed2.getParams());
-        }
-
-        return false;
-    }
-
-    public static boolean isEDDSASignatureAlgorithm(String algorithm) {
-        return EdDSANamedCurveTable.CURVE_ED25519_SHA512.equalsIgnoreCase(algorithm);
-    }
-
-    public static EdDSAPublicKey recoverEDDSAPublicKey(PrivateKey key) throws GeneralSecurityException {
-        ValidateUtils.checkTrue(SecurityUtils.isEDDSACurveSupported(), SecurityUtils.EDDSA + " not supported");
-        if (!(key instanceof EdDSAPrivateKey)) {
-            throw new InvalidKeyException("Private key is not " + SecurityUtils.EDDSA);
-        }
-
-        EdDSAPrivateKey prvKey = (EdDSAPrivateKey) key;
-        EdDSAPublicKeySpec keySpec = new EdDSAPublicKeySpec(prvKey.getSeed(), prvKey.getParams());
-        KeyFactory factory = SecurityUtils.getKeyFactory(SecurityUtils.EDDSA);
-        return EdDSAPublicKey.class.cast(factory.generatePublic(keySpec));
-    }
-
-    public static org.apache.sshd.common.signature.Signature getEDDSASignature() {
-        ValidateUtils.checkTrue(SecurityUtils.isEDDSACurveSupported(), SecurityUtils.EDDSA + " not supported");
-        return new SignatureEd25519();
-    }
-
-    public static boolean isEDDSAKeyFactoryAlgorithm(String algorithm) {
-        return SecurityUtils.EDDSA.equalsIgnoreCase(algorithm);
-    }
-
-    public static boolean isEDDSAKeyPairGeneratorAlgorithm(String algorithm) {
-        return SecurityUtils.EDDSA.equalsIgnoreCase(algorithm);
-    }
-
-    public static PublicKeyEntryDecoder<? extends PublicKey, ? extends PrivateKey> getEDDSAPublicKeyEntryDecoder() {
-        ValidateUtils.checkTrue(SecurityUtils.isEDDSACurveSupported(), SecurityUtils.EDDSA + " not supported");
-        return Ed25519PublicKeyDecoder.INSTANCE;
-    }
-
-    public static PrivateKeyEntryDecoder<? extends PublicKey, ? extends PrivateKey> getOpenSSHEDDSAPrivateKeyEntryDecoder() {
-        ValidateUtils.checkTrue(SecurityUtils.isEDDSACurveSupported(), SecurityUtils.EDDSA + " not supported");
-        return OpenSSHEd25519PrivateKeyEntryDecoder.INSTANCE;
-    }
-
-    public static boolean compareEDDSAPrivateKeys(PrivateKey k1, PrivateKey k2) {
-        if (!SecurityUtils.isEDDSACurveSupported()) {
-            return false;
-        }
-
-        if ((k1 instanceof EdDSAPrivateKey) && (k2 instanceof EdDSAPrivateKey)) {
-            if (Objects.equals(k1, k2)) {
-                return true;
-            } else if (k1 == null || k2 == null) {
-                return false;   // both null is covered by Objects#equals
-            }
-
-            EdDSAPrivateKey ed1 = (EdDSAPrivateKey) k1;
-            EdDSAPrivateKey ed2 = (EdDSAPrivateKey) k2;
-            return Arrays.equals(ed1.getSeed(), ed2.getSeed())
-                && compareEDDSAKeyParams(ed1.getParams(), ed2.getParams());
-        }
-
-        return false;
-    }
-
-    public static boolean compareEDDSAKeyParams(EdDSAParameterSpec s1, EdDSAParameterSpec s2) {
-        if (Objects.equals(s1, s2)) {
-            return true;
-        } else if (s1 == null || s2 == null) {
-            return false;   // both null is covered by Objects#equals
-        } else {
-            return Objects.equals(s1.getHashAlgorithm(), s2.getHashAlgorithm())
-                && Objects.equals(s1.getCurve(), s2.getCurve())
-                && Objects.equals(s1.getB(), s2.getB());
-        }
-    }
-
-    public static PublicKey generateEDDSAPublicKey(byte[] seed) throws GeneralSecurityException {
-        if (!SecurityUtils.isEDDSACurveSupported()) {
-            throw new NoSuchAlgorithmException(SecurityUtils.EDDSA + " not supported");
-        }
-
-        EdDSAParameterSpec params = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512);
-        EdDSAPublicKeySpec keySpec = new EdDSAPublicKeySpec(seed, params);
-        KeyFactory factory = SecurityUtils.getKeyFactory(SecurityUtils.EDDSA);
-        return factory.generatePublic(keySpec);
-    }
-
-    public static PrivateKey generateEDDSAPrivateKey(byte[] seed) throws GeneralSecurityException {
-        if (!SecurityUtils.isEDDSACurveSupported()) {
-            throw new NoSuchAlgorithmException(SecurityUtils.EDDSA + " not supported");
-        }
-
-        EdDSAParameterSpec params = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512);
-        EdDSAPrivateKeySpec keySpec = new EdDSAPrivateKeySpec(seed, params);
-        KeyFactory factory = SecurityUtils.getKeyFactory(SecurityUtils.EDDSA);
-        return factory.generatePrivate(keySpec);
-    }
-
-    public static <B extends Buffer> B putRawEDDSAPublicKey(B buffer, PublicKey key) {
-        ValidateUtils.checkTrue(SecurityUtils.isEDDSACurveSupported(), SecurityUtils.EDDSA + " not supported");
-        EdDSAPublicKey edKey = ValidateUtils.checkInstanceOf(key, EdDSAPublicKey.class, "Not an EDDSA public key: %s", key);
-        byte[] seed = Ed25519PublicKeyDecoder.getSeedValue(edKey);
-        ValidateUtils.checkNotNull(seed, "No seed extracted from key: %s", edKey.getA());
-        buffer.putString(KeyPairProvider.SSH_ED25519);
-        buffer.putBytes(seed);
-        return buffer;
-    }
-
-    public static <B extends Buffer> B putEDDSAKeyPair(B buffer, PublicKey pubKey, PrivateKey prvKey) {
-        ValidateUtils.checkTrue(SecurityUtils.isEDDSACurveSupported(), SecurityUtils.EDDSA + " not supported");
-        ValidateUtils.checkInstanceOf(pubKey, EdDSAPublicKey.class, "Not an EDDSA public key: %s", pubKey);
-        ValidateUtils.checkInstanceOf(prvKey, EdDSAPrivateKey.class, "Not an EDDSA private key: %s", prvKey);
-        throw new UnsupportedOperationException("Full SSHD-440 implementation N/A");
-    }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/31486673/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/OpenSSHEd25519PrivateKeyEntryDecoder.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/OpenSSHEd25519PrivateKeyEntryDecoder.java b/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/OpenSSHEd25519PrivateKeyEntryDecoder.java
index e5ae0ac..b05a4cc 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/OpenSSHEd25519PrivateKeyEntryDecoder.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/security/eddsa/OpenSSHEd25519PrivateKeyEntryDecoder.java
@@ -98,7 +98,7 @@ public class OpenSSHEd25519PrivateKeyEntryDecoder extends AbstractPrivateKeyEntr
 
     @Override
     public EdDSAPublicKey recoverPublicKey(EdDSAPrivateKey prvKey) throws GeneralSecurityException {
-        return EdDSASecurityProviderUtils.recoverEDDSAPublicKey(prvKey);
+        return EdDSASecurityProvider.recoverEDDSAPublicKey(prvKey);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/31486673/sshd-core/src/test/java/org/apache/sshd/common/signature/SignaturesDevelopment.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/signature/SignaturesDevelopment.java b/sshd-core/src/test/java/org/apache/sshd/common/signature/SignaturesDevelopment.java
index 180e997..6c4929a 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/signature/SignaturesDevelopment.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/signature/SignaturesDevelopment.java
@@ -25,7 +25,7 @@ import java.security.PublicKey;
 
 import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.common.util.buffer.BufferUtils;
-import org.apache.sshd.common.util.security.eddsa.EdDSASecurityProviderUtils;
+import org.apache.sshd.common.util.security.eddsa.EdDSASecurityProvider;
 import org.apache.sshd.util.test.BaseTestSupport;
 
 /**
@@ -64,9 +64,9 @@ public class SignaturesDevelopment extends BaseTestSupport {
         SignatureFactory factory = BuiltinSignatures.resolveFactory(args[0]);
         // TODO recover public/private keys according to factory name
         byte[] publicKey = BufferUtils.decodeHex(':', args[1]);
-        PublicKey pubKey = EdDSASecurityProviderUtils.generateEDDSAPublicKey(publicKey);
+        PublicKey pubKey = EdDSASecurityProvider.generateEDDSAPublicKey(publicKey);
         byte[] privateKey = BufferUtils.decodeHex(':', args[2]);
-        PrivateKey prvKey = EdDSASecurityProviderUtils.generateEDDSAPrivateKey(privateKey);
+        PrivateKey prvKey = EdDSASecurityProvider.generateEDDSAPrivateKey(privateKey);
         String op = args[3];
         byte[] data = BufferUtils.decodeHex(':', args[4]);
         byte[] signature = GenericUtils.EMPTY_BYTE_ARRAY;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/31486673/sshd-core/src/test/java/org/apache/sshd/common/util/security/eddsa/Ed25519VectorsTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/util/security/eddsa/Ed25519VectorsTest.java b/sshd-core/src/test/java/org/apache/sshd/common/util/security/eddsa/Ed25519VectorsTest.java
index b58aad3..fab3294 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/util/security/eddsa/Ed25519VectorsTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/util/security/eddsa/Ed25519VectorsTest.java
@@ -60,9 +60,9 @@ public class Ed25519VectorsTest extends BaseTestSupport {
     public Ed25519VectorsTest(String name, String prvKey, String pubKey, String msg, String signature)
             throws GeneralSecurityException {
         prvBytes = BufferUtils.decodeHex(BufferUtils.EMPTY_HEX_SEPARATOR, prvKey);
-        privateKey = EdDSASecurityProviderUtils.generateEDDSAPrivateKey(prvBytes.clone());
+        privateKey = EdDSASecurityProvider.generateEDDSAPrivateKey(prvBytes.clone());
         pubBytes = BufferUtils.decodeHex(BufferUtils.EMPTY_HEX_SEPARATOR, pubKey);
-        publicKey = EdDSASecurityProviderUtils.generateEDDSAPublicKey(pubBytes.clone());
+        publicKey = EdDSASecurityProvider.generateEDDSAPublicKey(pubBytes.clone());
         msgBytes = BufferUtils.decodeHex(BufferUtils.EMPTY_HEX_SEPARATOR, msg);
         expSignature = BufferUtils.decodeHex(BufferUtils.EMPTY_HEX_SEPARATOR, signature);
     }
@@ -200,14 +200,14 @@ public class Ed25519VectorsTest extends BaseTestSupport {
 
     @Test
     public void testSignature() throws Exception {
-        Signature signer = EdDSASecurityProviderUtils.getEDDSASignature();
+        Signature signer = EdDSASecurityProvider.getEDDSASignature();
         signer.initSigner(privateKey);
         signer.update(msgBytes.clone());
 
         byte[] actSignature = signer.sign();
         assertArrayEquals("Mismatched signature", expSignature, actSignature);
 
-        Signature verifier = EdDSASecurityProviderUtils.getEDDSASignature();
+        Signature verifier = EdDSASecurityProvider.getEDDSASignature();
         verifier.initVerifier(publicKey);
         verifier.update(msgBytes.clone());
         assertTrue("Verification failed", verifier.verify(expSignature));
@@ -222,14 +222,14 @@ public class Ed25519VectorsTest extends BaseTestSupport {
         System.arraycopy(msgBytes, 0, dataBuf, offset, msgBytes.length);
         System.arraycopy(extraData, offset, dataBuf, offset + msgBytes.length, extraData.length - offset);
 
-        Signature signer = EdDSASecurityProviderUtils.getEDDSASignature();
+        Signature signer = EdDSASecurityProvider.getEDDSASignature();
         signer.initSigner(privateKey);
         signer.update(dataBuf.clone(), offset, msgBytes.length);
 
         byte[] actSignature = signer.sign();
         assertArrayEquals("Mismatched signature", expSignature, actSignature);
 
-        Signature verifier = EdDSASecurityProviderUtils.getEDDSASignature();
+        Signature verifier = EdDSASecurityProvider.getEDDSASignature();
         verifier.initVerifier(publicKey);
         verifier.update(dataBuf.clone(), offset, msgBytes.length);
         assertTrue("Verification failed", verifier.verify(expSignature));

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/31486673/sshd-core/src/test/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderRegistrarTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderRegistrarTest.java b/sshd-core/src/test/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderRegistrarTest.java
index f2c1f3f..9e12fab 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderRegistrarTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderRegistrarTest.java
@@ -26,8 +26,6 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashSet;
 
-import net.i2p.crypto.eddsa.EdDSASecurityProvider;
-
 import org.apache.sshd.common.util.security.SecurityProviderRegistrar;
 import org.apache.sshd.common.util.security.SecurityProviderRegistrarTestSupport;
 import org.apache.sshd.common.util.security.SecurityUtils;


[2/3] mina-sshd git commit: [SSHD-732] Fixed BuiltinClientIdentitiesWatcherTest in view

Posted by gn...@apache.org.
[SSHD-732] Fixed BuiltinClientIdentitiesWatcherTest in view


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

Branch: refs/heads/master
Commit: 6ecd949a9d855feee2f1d2231b7c002d1df7f4cb
Parents: 3148667
Author: Lyor Goldstein <ly...@gmail.com>
Authored: Tue Mar 21 06:49:16 2017 +0200
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Tue Mar 28 11:59:15 2017 +0200

----------------------------------------------------------------------
 .../client/config/keys/ClientIdentitiesWatcher.java     | 12 +++++++++---
 .../config/keys/BuiltinClientIdentitiesWatcherTest.java |  7 ++++++-
 2 files changed, 15 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6ecd949a/sshd-core/src/main/java/org/apache/sshd/client/config/keys/ClientIdentitiesWatcher.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/config/keys/ClientIdentitiesWatcher.java b/sshd-core/src/main/java/org/apache/sshd/client/config/keys/ClientIdentitiesWatcher.java
index 6c2bb0b..094766f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/config/keys/ClientIdentitiesWatcher.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/config/keys/ClientIdentitiesWatcher.java
@@ -76,7 +76,7 @@ public class ClientIdentitiesWatcher extends AbstractKeyPairProvider implements
         return loadKeys(null);
     }
 
-    protected Iterable<KeyPair> loadKeys(Predicate<KeyPair> filter) {
+    protected Iterable<KeyPair> loadKeys(Predicate<? super KeyPair> filter) {
         return () -> {
             Stream<KeyPair> stream = safeMap(GenericUtils.stream(providers), this::doGetKeyPair);
             if (filter != null) {
@@ -89,14 +89,20 @@ public class ClientIdentitiesWatcher extends AbstractKeyPairProvider implements
     /**
      * Performs a mapping operation on the stream, discarding any null values
      * returned by the mapper.
+     *
+     * @param <U> Original type
+     * @param <V> Mapped type
+     * @param stream Original values stream
+     * @param mapper Mapper to target type
+     * @return Mapped stream
      */
-    private <U, V> Stream<V> safeMap(Stream<U> stream, Function<U, V> mapper) {
+    protected <U, V> Stream<V> safeMap(Stream<U> stream, Function<? super U, ? extends V> mapper) {
         return stream.map(u -> Optional.ofNullable(mapper.apply(u)))
                 .filter(Optional::isPresent)
                 .map(Optional::get);
     }
 
-    private KeyPair doGetKeyPair(ClientIdentityProvider p) {
+    protected KeyPair doGetKeyPair(ClientIdentityProvider p) {
         try {
             KeyPair kp = p.getClientIdentity();
             if (kp == null) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6ecd949a/sshd-core/src/test/java/org/apache/sshd/client/config/keys/BuiltinClientIdentitiesWatcherTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/config/keys/BuiltinClientIdentitiesWatcherTest.java b/sshd-core/src/test/java/org/apache/sshd/client/config/keys/BuiltinClientIdentitiesWatcherTest.java
index 9599684..70cc197 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/config/keys/BuiltinClientIdentitiesWatcherTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/config/keys/BuiltinClientIdentitiesWatcherTest.java
@@ -28,6 +28,7 @@ import java.nio.file.Path;
 import java.nio.file.StandardOpenOption;
 import java.security.GeneralSecurityException;
 import java.security.KeyPair;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
 import java.util.EnumMap;
@@ -141,7 +142,11 @@ public class BuiltinClientIdentitiesWatcherTest extends BaseTestSupport {
     }
 
     private static void testMultipleFilesWatch(String phase, KeyIdentityProvider watcher, Collection<? extends KeyPair> expected) {
-        Collection<? extends KeyPair> actual = (Collection<? extends KeyPair>) watcher.loadKeys();
+        Iterable<KeyPair> keys = watcher.loadKeys();
+        Collection<KeyPair> actual = new ArrayList<>();
+        for (KeyPair kp : keys) {
+            actual.add(kp);
+        }
         assertEquals(phase + ": mismatched sizes", GenericUtils.size(expected), GenericUtils.size(actual));
 
         if (!GenericUtils.isEmpty(expected)) {


[3/3] mina-sshd git commit: [SSHD-734] When ClientSessionImpl construction fails, AbstractSessionIoHandler#exceptionCaught may throw NPE

Posted by gn...@apache.org.
[SSHD-734] When ClientSessionImpl construction fails, AbstractSessionIoHandler#exceptionCaught may throw NPE

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

Branch: refs/heads/master
Commit: a1cb02b41355c176693ead60c0bce80eb858eb5b
Parents: 6ecd949
Author: Guillaume Nodet <gn...@apache.org>
Authored: Tue Mar 28 11:16:25 2017 +0200
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Tue Mar 28 11:59:28 2017 +0200

----------------------------------------------------------------------
 .../common/session/helpers/AbstractSession.java |  2 +
 .../helpers/AbstractSessionIoHandler.java       |  3 +-
 .../apache/sshd/client/ClientDeadlockTest.java  | 86 ++++++++++++++++++++
 3 files changed, 89 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a1cb02b4/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
index 18c84bc..49ce76b 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
@@ -255,6 +255,8 @@ public abstract class AbstractSession extends AbstractKexFactoryManager implemen
         this.ioSession = ioSession;
         this.decoderBuffer = new SessionWorkBuffer(this);
 
+        attachSession(ioSession, this);
+
         Factory<Random> factory = ValidateUtils.checkNotNull(factoryManager.getRandomFactory(), "No random factory for %s", ioSession);
         random = ValidateUtils.checkNotNull(factory.create(), "No randomizer instance for %s", ioSession);
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a1cb02b4/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSessionIoHandler.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSessionIoHandler.java b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSessionIoHandler.java
index 34f4465..1610030 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSessionIoHandler.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSessionIoHandler.java
@@ -37,9 +37,8 @@ public abstract class AbstractSessionIoHandler extends AbstractLoggingBean imple
 
     @Override
     public void sessionCreated(IoSession ioSession) throws Exception {
-        AbstractSession session = ValidateUtils.checkNotNull(
+        ValidateUtils.checkNotNull(
                 createSession(ioSession), "No session created for %s", ioSession);
-        AbstractSession.attachSession(ioSession, session);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a1cb02b4/sshd-core/src/test/java/org/apache/sshd/client/ClientDeadlockTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/ClientDeadlockTest.java b/sshd-core/src/test/java/org/apache/sshd/client/ClientDeadlockTest.java
new file mode 100644
index 0000000..bff2420
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/client/ClientDeadlockTest.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sshd.client;
+
+import java.io.IOException;
+import java.util.EnumSet;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.sshd.client.future.ConnectFuture;
+import org.apache.sshd.client.session.ClientSession;
+import org.apache.sshd.common.io.IoSession;
+import org.apache.sshd.server.SshServer;
+import org.apache.sshd.server.session.ServerSessionImpl;
+import org.apache.sshd.server.session.SessionFactory;
+import org.apache.sshd.util.test.BaseTestSupport;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * TODO Add javadoc
+ *
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public class ClientDeadlockTest extends BaseTestSupport {
+
+    private SshServer sshd;
+    private SshClient client;
+    private int port;
+
+    public ClientDeadlockTest() {
+        super();
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        sshd = setupTestServer();
+        sshd.setSessionFactory(new SessionFactory(sshd) {
+            @Override
+            protected ServerSessionImpl doCreateSession(IoSession ioSession) throws Exception {
+                throw new IOException("Closing");
+            }
+        });
+        sshd.start();
+        port = sshd.getPort();
+
+        client = setupTestClient();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        if (sshd != null) {
+            sshd.stop(true);
+        }
+        if (client != null) {
+            client.stop();
+        }
+    }
+
+    @Test
+    public void testSimpleClient() throws Exception {
+        client.start();
+
+        ConnectFuture future = client.connect(getCurrentTestName(), TEST_LOCALHOST, port);
+        ClientSession session = future.verify(5, TimeUnit.SECONDS).getSession();
+        session.waitFor(EnumSet.of(ClientSession.ClientSessionEvent.CLOSED), TimeUnit.SECONDS.toMillis(3L));
+        assertTrue(session.isClosed());
+    }
+
+}