Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 215E5200B3C for ; Wed, 29 Jun 2016 07:44:37 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 1FB3A160A6C; Wed, 29 Jun 2016 05:44:37 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id E62AD160A56 for ; Wed, 29 Jun 2016 07:44:35 +0200 (CEST) Received: (qmail 62152 invoked by uid 500); 29 Jun 2016 05:44:34 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 62143 invoked by uid 99); 29 Jun 2016 05:44:34 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Jun 2016 05:44:34 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A6DA3E08FE; Wed, 29 Jun 2016 05:44:34 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sdp@apache.org To: commits@commons.apache.org Message-Id: <5b13b12543174e458745f548de5e0fc4@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: commons-crypto git commit: CRYPTO-91: Drop the fallback property and implementation Date: Wed, 29 Jun 2016 05:44:34 +0000 (UTC) archived-at: Wed, 29 Jun 2016 05:44:37 -0000 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 Authored: Wed Jun 29 13:37:18 2016 +0800 Committer: Sun Dapeng 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 Properties 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 Properties 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")); - } - } - -}