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/12/02 11:00:35 UTC
[3/7] mina-sshd git commit: Expose more information about the cipher
via its CipherFactory as well
Expose more information about the cipher via its CipherFactory as well
* Also exposed the "checkSupported" method as a Utils one
Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/c437270b
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/c437270b
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/c437270b
Branch: refs/heads/master
Commit: c437270b6044cca55071f5eba97819d74dc463f1
Parents: 0fcccf6
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Wed Dec 2 11:57:03 2015 +0200
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Wed Dec 2 11:57:03 2015 +0200
----------------------------------------------------------------------
.../apache/sshd/common/cipher/BaseCipher.java | 19 ++++---
.../sshd/common/cipher/BuiltinCiphers.java | 29 +++-------
.../org/apache/sshd/common/cipher/Cipher.java | 57 ++++++++++++--------
.../sshd/common/cipher/CipherFactory.java | 4 +-
.../sshd/common/cipher/CipherInformation.java | 45 ++++++++++++++++
5 files changed, 102 insertions(+), 52 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/c437270b/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 381825d..546685e 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
@@ -38,6 +38,7 @@ public class BaseCipher implements Cipher {
private final int bsize;
private final String algorithm;
private final String transformation;
+ private String s;
public BaseCipher(int ivsize, int bsize, String algorithm, String transformation) {
this.ivsize = ivsize;
@@ -102,11 +103,17 @@ public class BaseCipher implements Cipher {
@Override
public String toString() {
- return getClass().getSimpleName()
- + "[" + getAlgorithm()
- + "," + getIVSize()
- + "," + getBlockSize()
- + "," + getTransformation()
- + "]";
+ synchronized (this) {
+ if (s == null) {
+ s = getClass().getSimpleName()
+ + "[" + getAlgorithm()
+ + "," + getIVSize()
+ + "," + getBlockSize()
+ + "," + getTransformation()
+ + "]";
+ }
+ }
+
+ return s;
}
}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/c437270b/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 f2cc3ef..3b236bd 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
@@ -96,9 +96,9 @@ public enum BuiltinCiphers implements CipherFactory {
* 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
+ * change while the JVM is running
*/
- this.supported = checkSupported(this.transformation, this.keysize);
+ this.supported = Constants.NONE.equals(factoryName) || Cipher.Utils.checkSupported(this.transformation, this.keysize);
}
@Override
@@ -121,15 +121,6 @@ public enum BuiltinCiphers implements CipherFactory {
return supported;
}
- private static boolean checkSupported(String xform, int keyLength) {
- try {
- int maxKeyLength = javax.crypto.Cipher.getMaxAllowedKeyLength(xform);
- return maxKeyLength >= keyLength;
- } catch (Exception e) {
- return false;
- }
- }
-
/**
* @return The key size (in bits) for the cipher
*/
@@ -137,30 +128,22 @@ public enum BuiltinCiphers implements CipherFactory {
return keysize;
}
- /**
- * @return The size of the initialization vector
- */
+ @Override
public int getIVSize() {
return ivsize;
}
- /**
- * @return The block size for this cipher
- */
+ @Override
public int getBlockSize() {
return blocksize;
}
- /**
- * @return The algorithm for this cipher
- */
+ @Override
public String getAlgorithm() {
return algorithm;
}
- /**
- * @return The transformation for this cipher
- */
+ @Override
public String getTransformation() {
return transformation;
}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/c437270b/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 9e53cc9..20f1267 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
@@ -18,39 +18,21 @@
*/
package org.apache.sshd.common.cipher;
+import org.apache.sshd.common.util.ValidateUtils;
+
/**
* Wrapper for a cryptographic cipher, used either for encryption
* or decryption.
*
* @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
*/
-public interface Cipher {
+public interface Cipher extends CipherInformation {
enum Mode {
Encrypt, Decrypt
}
/**
- * @return The cipher's algorithm
- */
- String getAlgorithm();
-
- /**
- * @return The actual transformation used - e.g., AES/CBC/NoPadding
- */
- String getTransformation();
-
- /**
- * @return Size of the initialization vector (in bytes)
- */
- int getIVSize();
-
- /**
- * @return The block size (in bytes) for this cipher
- */
- int getBlockSize();
-
- /**
* Initialize the cipher for encryption or decryption with
* the given key and initialization vector
*
@@ -80,4 +62,37 @@ public interface Cipher {
*/
void update(byte[] input, int inputOffset, int inputLen) throws Exception;
+ /**
+ * Utility class to help using {@link Cipher}s
+ */
+ // CHECKSTYLE:OFF
+ final class Utils {
+ // CHECKSTYLE:ON
+
+ private Utils() {
+ throw new UnsupportedOperationException("No instance allowed");
+ }
+
+ /**
+ * @param xform The full cipher transformation - e.g., AES/CBC/NoPadding -
+ * never {@code null}/empty
+ * @param keyLength The required key length in bits - always positive
+ * @return {@code true} if the cipher transformation <U>and</U> required
+ * key length are supported
+ * @see {@link javax.crypto.Cipher#getMaxAllowedKeyLength(String)}
+ */
+ public static boolean checkSupported(String xform, int keyLength) {
+ ValidateUtils.checkNotNullAndNotEmpty(xform, "No transformation");
+ if (keyLength <= 0) {
+ throw new IllegalArgumentException("Bad key length (" + keyLength + ") for cipher=" + xform);
+ }
+
+ try {
+ int maxKeyLength = javax.crypto.Cipher.getMaxAllowedKeyLength(xform);
+ return maxKeyLength >= keyLength;
+ } catch (Exception e) {
+ return false;
+ }
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/c437270b/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherFactory.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherFactory.java b/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherFactory.java
index 3cafe49..36909f3 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherFactory.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherFactory.java
@@ -25,7 +25,7 @@ import org.apache.sshd.common.BuiltinFactory;
* @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
*/
// CHECKSTYLE:OFF
-public interface CipherFactory extends BuiltinFactory<Cipher> {
-
+public interface CipherFactory extends BuiltinFactory<Cipher>, CipherInformation {
+ // nothing extra
}
//CHECKSTYLE:ON
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/c437270b/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherInformation.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherInformation.java b/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherInformation.java
new file mode 100644
index 0000000..f17fd16
--- /dev/null
+++ b/sshd-core/src/main/java/org/apache/sshd/common/cipher/CipherInformation.java
@@ -0,0 +1,45 @@
+/*
+ * 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.cipher;
+
+/**
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public interface CipherInformation {
+ /**
+ * @return The cipher's algorithm
+ */
+ String getAlgorithm();
+
+ /**
+ * @return The actual transformation used - e.g., AES/CBC/NoPadding
+ */
+ String getTransformation();
+
+ /**
+ * @return Size of the initialization vector (in bytes)
+ */
+ int getIVSize();
+
+ /**
+ * @return The block size (in bytes) for this cipher
+ */
+ int getBlockSize();
+}