You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sd...@apache.org on 2016/06/29 05:44:34 UTC
commons-crypto git commit: CRYPTO-91: Drop the fallback property and
implementation
Repository: commons-crypto
Updated Branches:
refs/heads/master 6e3f56dcf -> 4a3baba53
CRYPTO-91: Drop the fallback property and implementation
Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/4a3baba5
Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/4a3baba5
Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/4a3baba5
Branch: refs/heads/master
Commit: 4a3baba5350fb6dd85d5f4f59656204cb810a039
Parents: 6e3f56d
Author: Sun Dapeng <sd...@apache.org>
Authored: Wed Jun 29 13:37:18 2016 +0800
Committer: Sun Dapeng <sd...@apache.org>
Committed: Wed Jun 29 13:40:23 2016 +0800
----------------------------------------------------------------------
.../crypto/cipher/CryptoCipherFactory.java | 8 +-
.../commons/crypto/conf/ConfigurationKeys.java | 11 --
.../crypto/random/CryptoRandomFactory.java | 39 ++++--
.../org/apache/commons/crypto/utils/Utils.java | 19 ---
.../crypto/cipher/CryptoCipherFactoryTest.java | 16 +--
.../crypto/random/CryptoRandomFactoryTest.java | 83 ++++++++++++
.../commons/crypto/random/CryptoRandomTest.java | 131 -------------------
7 files changed, 117 insertions(+), 190 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a3baba5/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java b/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java
index 8ba54c6..2c3b645 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java
@@ -79,7 +79,9 @@ public class CryptoCipherFactory {
/**
* The default value (OpensslCipher) for crypto cipher.
*/
- private static final String CIPHER_CLASSES_DEFAULT = CipherProvider.OPENSSL.getClassName();
+ private static final String CIPHER_CLASSES_DEFAULT = CipherProvider
+ .OPENSSL.getClassName().concat(",").concat(CipherProvider.JCE
+ .getClassName());
/**
* The private Constructor of {@link CryptoCipherFactory}.
@@ -118,11 +120,9 @@ public class CryptoCipherFactory {
if (cipher != null) {
return cipher;
- } else if (Utils.isFallbackEnabled(props)) {
- return new JceCipher(props,transformation);
} else {
if (errorMessage.length() == 0) {
- throw new IllegalArgumentException("No classname(s) provided, and fallback is not enabled");
+ throw new IllegalArgumentException("No classname(s) provided");
}
errorMessage.append(" is not available or transformation " +
transformation + " is not supported.");
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a3baba5/src/main/java/org/apache/commons/crypto/conf/ConfigurationKeys.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/conf/ConfigurationKeys.java b/src/main/java/org/apache/commons/crypto/conf/ConfigurationKeys.java
index c904cf1..07112bc 100644
--- a/src/main/java/org/apache/commons/crypto/conf/ConfigurationKeys.java
+++ b/src/main/java/org/apache/commons/crypto/conf/ConfigurationKeys.java
@@ -123,17 +123,6 @@ public class ConfigurationKeys {
+ "lib.tempdir";
/**
- * The configuration key of enable fallback on native failed.
- */
- public static final String ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY = CONF_PREFIX +
- "enable.fallback";
-
- /**
- * The default value of enable fallback on native failed.
- */
- public static final boolean ENABLE_FALLBACK_ON_NATIVE_FAILED_DEFAULT = true;
-
- /**
* The private constructor of {@Link ConfigurationKeys}.
*/
private ConfigurationKeys() {
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a3baba5/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java b/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java
index debc01e..b5d8c2e 100644
--- a/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java
+++ b/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java
@@ -20,9 +20,9 @@ package org.apache.commons.crypto.random;
import java.security.GeneralSecurityException;
import java.util.Properties;
+import org.apache.commons.crypto.conf.ConfigurationKeys;
import org.apache.commons.crypto.utils.ReflectionUtils;
import org.apache.commons.crypto.utils.Utils;
-import static org.apache.commons.crypto.conf.ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY;
/**
* This is the factory class used for {@link CryptoRandom}.
@@ -79,6 +79,13 @@ public class CryptoRandomFactory {
}
/**
+ * The default value (OpensslCipher) for crypto cipher.
+ */
+ private static final String SECURE_RANDOM_CLASSES_DEFAULT = RandomProvider
+ .OPENSSL.getClassName().concat(",").concat(RandomProvider.JCE
+ .getClassName());
+
+ /**
* The private constructor of {@Link CryptoRandomFactory}.
*/
private CryptoRandomFactory() {
@@ -99,14 +106,10 @@ public class CryptoRandomFactory {
*/
public static CryptoRandom getCryptoRandom(Properties props)
throws GeneralSecurityException {
- String cryptoRandomClasses = props.getProperty(SECURE_RANDOM_CLASSES_KEY);
- if (cryptoRandomClasses == null) {
- cryptoRandomClasses = System.getProperty(SECURE_RANDOM_CLASSES_KEY);
- }
-
StringBuilder errorMessage = new StringBuilder();
CryptoRandom random = null;
- for (String klassName : Utils.splitClassNames(cryptoRandomClasses, ",")) {
+ for (String klassName : Utils.splitClassNames(
+ getRandomClassString(props), ",")) {
try {
final Class<?> klass = ReflectionUtils.getClassByName(klassName);
random = (CryptoRandom) ReflectionUtils.newInstance(klass, props);
@@ -122,13 +125,29 @@ public class CryptoRandomFactory {
if (random != null) {
return random;
- } else if (Utils.isFallbackEnabled(props)) {
- return new JavaCryptoRandom(props);
} else {
if (errorMessage.length() == 0) {
- throw new IllegalArgumentException("No classname(s) provided, and fallback is not enabled");
+ throw new IllegalArgumentException("No classname(s) provided");
}
throw new GeneralSecurityException(errorMessage.toString());
}
}
+
+ /**
+ * Gets the CryptoRandom class.
+ *
+ * @param props The <code>Properties</code> class represents a set of
+ * properties.
+ * @return the CryptoRandom class based on the props.
+ */
+ private static String getRandomClassString(Properties props) {
+ final String configName = ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY;
+ String randomClassString = props.getProperty(configName) != null ? props
+ .getProperty(configName, SECURE_RANDOM_CLASSES_DEFAULT)
+ : System.getProperty(configName, SECURE_RANDOM_CLASSES_DEFAULT);
+ if (randomClassString.isEmpty()) {
+ randomClassString = SECURE_RANDOM_CLASSES_DEFAULT;
+ }
+ return randomClassString;
+ }
}
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a3baba5/src/main/java/org/apache/commons/crypto/utils/Utils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/utils/Utils.java b/src/main/java/org/apache/commons/crypto/utils/Utils.java
index 76229c1..269e882 100644
--- a/src/main/java/org/apache/commons/crypto/utils/Utils.java
+++ b/src/main/java/org/apache/commons/crypto/utils/Utils.java
@@ -178,23 +178,4 @@ public final class Utils {
return res;
}
- /**
- * Returns true if Fallback is enabled when native failed.
- * @param props The <code>Properties</code> class represents a set of
- * properties.
- * @return true if Fallback is enabled when native failed.
- */
- public static boolean isFallbackEnabled(Properties props) {
- String enableFallback = props.getProperty(ConfigurationKeys.
- ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY);
- if (enableFallback == null || enableFallback.isEmpty()) {
- enableFallback = System.getProperty(ConfigurationKeys.
- ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY);
- }
- if (enableFallback == null || enableFallback.isEmpty()) {
- return ConfigurationKeys
- .ENABLE_FALLBACK_ON_NATIVE_FAILED_DEFAULT;
- }
- return Boolean.valueOf(enableFallback);
- }
}
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a3baba5/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java b/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java
index 1776b4c..26606eb 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java
@@ -45,25 +45,11 @@ public class CryptoCipherFactoryTest {
.getClass().getName());
}
- @Test
+ @Test(expected = GeneralSecurityException.class)
public void testInvalidCipher() throws GeneralSecurityException {
Properties properties = new Properties();
properties.setProperty(ConfigurationKeys.CIPHER_CLASSES_KEY,
"InvalidCipherName");
- CryptoCipher defaultCipher = CryptoCipherFactory.getInstance(
- "AES/CBC/NoPadding", properties);
- Assert.assertEquals(JceCipher.class.getName(),
- defaultCipher.getClass().getName());
- }
-
- @Test(expected = GeneralSecurityException.class)
- public void testDisableFallback() throws GeneralSecurityException {
- Properties properties = new Properties();
- properties.setProperty(ConfigurationKeys.CIPHER_CLASSES_KEY,
- "InvalidCipherName");
- properties.setProperty(ConfigurationKeys
- .ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "false");
-
CryptoCipherFactory.getInstance("AES/CBC/NoPadding", properties);
}
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a3baba5/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java b/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java
new file mode 100644
index 0000000..b24586f
--- /dev/null
+++ b/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java
@@ -0,0 +1,83 @@
+/**
+ * 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.commons.crypto.random;
+
+import java.security.GeneralSecurityException;
+import java.util.Properties;
+
+import org.apache.commons.crypto.conf.ConfigurationKeys;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CryptoRandomFactoryTest {
+
+ @Test(expected=NullPointerException.class)
+ public void testNull() throws Exception {
+ CryptoRandomFactory.getCryptoRandom(null);
+ }
+
+ @Test
+ public void testEmpty() throws Exception {
+ final Properties props = new Properties();
+ props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, "");
+ CryptoRandomFactory.getCryptoRandom(props);
+ }
+
+
+ @Test
+ public void testDefaultRandom() throws GeneralSecurityException {
+ Properties props = new Properties();
+ CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);
+ Assert.assertEquals(OpensslCryptoRandom.class.getName(),
+ random.getClass().getName());
+ }
+
+ @Test
+ public void testGetOSRandom() throws GeneralSecurityException {
+ Properties props = new Properties();
+ props.setProperty(
+ ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY,
+ CryptoRandomFactory.RandomProvider.OS.getClassName());
+ CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);
+
+ Assert.assertEquals(OsCryptoRandom.class.getName(), random.getClass()
+ .getName());
+ }
+
+ @Test
+ public void testFullClassName() throws GeneralSecurityException {
+ Properties props = new Properties();
+ props.setProperty(
+ ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY,
+ OsCryptoRandom.class.getName());
+ CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);
+
+ Assert.assertEquals(OsCryptoRandom.class.getName(), random.getClass()
+ .getName());
+ }
+
+ @Test(expected = GeneralSecurityException.class)
+ public void testInvalidRandom() throws GeneralSecurityException {
+ Properties properties = new Properties();
+ properties.setProperty(
+ ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY,
+ "InvalidCipherName");
+ CryptoRandomFactory.getCryptoRandom(properties);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a3baba5/src/test/java/org/apache/commons/crypto/random/CryptoRandomTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/crypto/random/CryptoRandomTest.java b/src/test/java/org/apache/commons/crypto/random/CryptoRandomTest.java
deleted file mode 100644
index 9f451a7..0000000
--- a/src/test/java/org/apache/commons/crypto/random/CryptoRandomTest.java
+++ /dev/null
@@ -1,131 +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.commons.crypto.random;
-
-import static org.junit.Assert.assertTrue;
-
-import java.util.Properties;
-
-import org.apache.commons.crypto.conf.ConfigurationKeys;
-import org.junit.Test;
-
-public class CryptoRandomTest {
-
- @Test(expected=NullPointerException.class)
- public void testNull() throws Exception {
- CryptoRandomFactory.getCryptoRandom(null);
- }
-
- @Test
- public void testEmpty() throws Exception {
- final Properties props = new Properties();
- props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, "");
- CryptoRandomFactory.getCryptoRandom(props);
- }
-
- @Test
- public void testEmptyFallback() throws Exception {
- final Properties props = new Properties();
- props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, "");
- props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "");
- CryptoRandomFactory.getCryptoRandom(props);
- }
-
- @Test
- public void testTrueFallback() throws Exception {
- final Properties props = new Properties();
- props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, "");
- props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "true");
- CryptoRandomFactory.getCryptoRandom(props);
- }
-
- @Test(expected=IllegalArgumentException.class)
- public void testFalseFallbackEmpty() throws Exception {
- final Properties props = new Properties();
- props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, "");
- props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "notTrue");
- CryptoRandomFactory.getCryptoRandom(props);
- }
-
- @Test(expected=IllegalArgumentException.class)
- public void testFalseFallbackNoNames() throws Exception {
- final Properties props = new Properties();
- props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, ",,,,");
- props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "notTrue");
- CryptoRandomFactory.getCryptoRandom(props);
- }
-
- @Test
- public void testNoSuchClass() throws Exception {
- final Properties props = new Properties();
- props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, "noSuchClass");
- props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "notTrue");
- try {
- CryptoRandomFactory.getCryptoRandom(props);
- } catch (Exception e) {
- final String message = e.getMessage();
- assertTrue(message, message.contains("not found"));
- assertTrue(message, message.contains("noSuchClass"));
- }
- }
-
- @Test
- public void testWrongClass() throws Exception {
- final Properties props = new Properties();
- props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, "java.util.Properties"); // Use a class that accepts a Properties object
- props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "notTrue");
- try {
- CryptoRandomFactory.getCryptoRandom(props);
- } catch (Exception e) {
- final String message = e.getMessage();
- assertTrue(message, message.contains("java.util.Properties"));
- assertTrue(message, message.contains("not a CryptoRandom"));
- }
- }
-
- @Test
- public void testWrongClassBadCtor() throws Exception {
- final Properties props = new Properties();
- final String canonicalName = DummyRandom.class.getCanonicalName();
- props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, canonicalName);
- props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "notTrue");
- try {
- CryptoRandomFactory.getCryptoRandom(props);
- } catch (Exception e) {
- final String message = e.getMessage();
- assertTrue(message, message.contains(canonicalName));
- assertTrue(message, message.contains("NoSuchMethodException"));
- }
- }
-
- @Test
- public void testAbstractClass() throws Exception {
- final Properties props = new Properties();
- final String canonicalName = AbstractRandom.class.getCanonicalName();
- props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, canonicalName);
- props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "notTrue");
- try {
- CryptoRandomFactory.getCryptoRandom(props);
- } catch (Exception e) {
- final String message = e.getMessage();
- assertTrue(message, message.contains(canonicalName));
- assertTrue(message, message.contains("NoSuchMethodException"));
- }
- }
-
-}