[xmlsectool] 03/04: Refactor by exbedding Blacklist and DigestChoice.

Ian Young ian at iay.org.uk
Thu Apr 28 11:37:05 EDT 2016


This is an automated email from the git hooks/post-receive script.

iay pushed a commit to branch master
in repository xmlsectool.

commit dea1b27416470fc2e9c679e451884efb15d6f95b
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Thu Apr 28 15:54:06 2016 +0100

    Refactor by exbedding Blacklist and DigestChoice.
---
 .../net/shibboleth/tool/xmlsectool/Blacklist.java  | 135 +++++++++++
 .../shibboleth/tool/xmlsectool/DigestChoice.java   | 157 +++++++++++++
 .../net/shibboleth/tool/xmlsectool/XmlSecTool.java |   2 +-
 .../xmlsectool/XmlSecToolCommandLineArguments.java | 252 ---------------------
 .../XmlSecToolCommandLineArgumentsTest.java        |   3 -
 5 files changed, 293 insertions(+), 256 deletions(-)

diff --git a/src/main/java/net/shibboleth/tool/xmlsectool/Blacklist.java b/src/main/java/net/shibboleth/tool/xmlsectool/Blacklist.java
new file mode 100644
index 0000000..f3cd4ab
--- /dev/null
+++ b/src/main/java/net/shibboleth/tool/xmlsectool/Blacklist.java
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.tool.xmlsectool;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.opensaml.xml.signature.SignatureConstants;
+
+/**
+ * A blacklist of digest and signature algorithms we should not accept during
+ * signature verification.
+ */
+public class Blacklist {
+    
+    /**
+     * Ordered set of blacklisted digest algorithm URIs.
+     */
+    private final Set<String> digestBlacklist = new TreeSet<String>();
+    
+    /**
+     * Ordered set of blacklisted signature algorithm URIs.
+     */
+    private final Set<String> signatureBlacklist = new TreeSet<String>();
+    
+    /**
+     * Constructor.
+     *
+     * Initializes the blacklist with those algorithms that should be
+     * blacklisted by default.
+     */
+    public Blacklist() {
+        // MD5
+        addDigestAlgorithm(SignatureConstants.ALGO_ID_DIGEST_NOT_RECOMMENDED_MD5);
+        addSignatureAlgorithm(SignatureConstants.ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5);
+        
+        // SHA-1
+        addDigest(DigestChoice.SHA1);
+    }
+    
+    /**
+     * Blacklist an individual digest algorithm.
+     * 
+     * @param uri algorithm URI to blacklist
+     */
+    private void addDigestAlgorithm(String uri) {
+        digestBlacklist.add(uri);
+    }
+    
+    /**
+     * Blacklist an individual signature algorithm.
+     * 
+     * @param uri algorithm URI to blacklist
+     */
+    private void addSignatureAlgorithm(String uri) {
+        signatureBlacklist.add(uri);
+    }
+    
+    /**
+     * Blacklist the digest and signature algorithms associated with
+     * a {@link DigestChoice}.
+     * 
+     * @param digestChoice {@link DigestChoice} to add to blacklist
+     */
+    public void addDigest(DigestChoice digestChoice) {
+        addDigestAlgorithm(digestChoice.getDigestAlgorithm());
+        addSignatureAlgorithm(digestChoice.getRsaAlgorithm());
+        addSignatureAlgorithm(digestChoice.getEcdsaAlgorithm());
+    }
+    
+    /**
+     * Returns <code>true</code> if the indicated algorithm URI is blacklisted for
+     * use as a digest algorithm.
+     * 
+     * @param alg digest algorithm URI to check
+     * @return <code>true</code> if the algorithm is blacklisted
+     */
+    public boolean isBlacklistedDigest(String alg) {
+        return digestBlacklist.contains(alg);
+    }
+    
+    /**
+     * Returns <code>true</code> if the indicated algorithm URI is blacklisted for
+     * use as a signature algorithm.
+     * 
+     * @param alg signature algorithm URI to check
+     * @return <code>true</code> if the algorithm is blacklisted
+     */
+    public boolean isBlacklistedSignature(String alg) {
+        return signatureBlacklist.contains(alg);
+    }
+    
+    /**
+     * Returns an unmodifiable view on the set of blacklisted digest algorithms.
+     * 
+     * @return set of blacklisted algorithms
+     */
+    public Collection<String> getDigestBlacklist() {
+        return Collections.unmodifiableCollection(digestBlacklist);
+    }
+    
+    /**
+     * Returns an unmodifiable view on the set of blacklisted signature algorithms.
+     * 
+     * @return set of blacklisted algorithms
+     */
+    public Collection<String> getSignatureBlacklist() {
+        return Collections.unmodifiableCollection(signatureBlacklist);
+    }
+    
+    /**
+     * Empties the digest and signature blacklists.
+     */
+    public void clear() {
+        digestBlacklist.clear();
+        signatureBlacklist.clear();
+    }
+}
diff --git a/src/main/java/net/shibboleth/tool/xmlsectool/DigestChoice.java b/src/main/java/net/shibboleth/tool/xmlsectool/DigestChoice.java
new file mode 100644
index 0000000..445ab12
--- /dev/null
+++ b/src/main/java/net/shibboleth/tool/xmlsectool/DigestChoice.java
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.tool.xmlsectool;
+
+import org.opensaml.xml.signature.SignatureConstants;
+
+/**
+ * The digest method to use in the various signature algorithms.
+ */
+public enum DigestChoice {
+
+    /**
+     * SHA-1 digest.
+     */
+    SHA1("SHA-1",
+            SignatureConstants.ALGO_ID_DIGEST_SHA1,
+            SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1,
+            SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA1),
+    
+    /**
+     * SHA-256 digest.
+     */
+    SHA256("SHA-256",
+            SignatureConstants.ALGO_ID_DIGEST_SHA256,
+            SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256,
+            SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA256),
+    
+    /**
+     * SHA-384 digest.
+     */
+    SHA384("SHA-384",
+            SignatureConstants.ALGO_ID_DIGEST_SHA384,
+            SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA384,
+            SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA384),
+    
+    /**
+     * SHA-512 digest.
+     */
+    SHA512("SHA-512",
+            SignatureConstants.ALGO_ID_DIGEST_SHA512,
+            SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA512,
+            SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA512);
+    
+    /**
+     * Other name (with hyphens, etc.) used as an alternative to the enum name.
+     */
+    private final String otherName;
+    
+    /**
+     * Digest algorithm.
+     */
+    private final String digestAlgorithm;
+    
+    /**
+     * RSA signature algorithm.
+     */
+    private final String rsaAlgorithm;
+    
+    /**
+     * ECDSA signature algorithm.
+     */
+    private final String ecdsaAlgorithm;
+    
+    /**
+     * Constructor.
+     * 
+     * @param otherNameArg an alternative name for the enum.
+     * @param digestArg digest algorithm URI
+     * @param rsaArg RSA signature algorithm URI
+     * @param ecdsaArg ECDSA signature algorithm URI
+     */
+    private DigestChoice(final String otherNameArg,
+            final String digestArg, final String rsaArg, final String ecdsaArg) {
+        otherName = otherNameArg;
+        digestAlgorithm = digestArg;
+        rsaAlgorithm = rsaArg;
+        ecdsaAlgorithm = ecdsaArg;
+    }
+    
+    /**
+     * Returns the digest algorithm URI for this digest choice.
+     * 
+     * @return algorithm URI
+     */
+    public String getDigestAlgorithm() {
+        return digestAlgorithm;
+    }
+    
+    /**
+     * Returns the RSA signature algorithm URI for this digest choice.
+     * 
+     * @return algorithm URI
+     */
+    public String getRsaAlgorithm() {
+        return rsaAlgorithm;
+    }
+    
+    /**
+     * Returns the ECDSA signature algorithm URI for this digest choice.
+     * 
+     * @return algorithm URI
+     */
+    public String getEcdsaAlgorithm() {
+        return ecdsaAlgorithm;
+    }
+    
+    /**
+     * Indicates whether the enum can be called by the provided name.
+     * 
+     * The name is compared ignoring case against the enum name and
+     * against the "other" name.
+     * 
+     * @param name name to check against
+     * @return <code>true</code> if and only if the enum can be called by the provided name
+     */
+    public boolean hasName(final String name) {
+        if (name.equalsIgnoreCase(name())) {
+            return true;
+        }
+        if (name.equalsIgnoreCase(otherName)) {
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * Finds the {@link DigestChoice} for a given digest name.
+     * 
+     * @param name name of the digest to be found
+     * 
+     * @return {@link DigestChoice} represented by the name
+     */
+    public static DigestChoice find(String name) {
+        for (DigestChoice choice: values()) {
+            if (choice.hasName(name)) {
+                return choice;
+            }
+        }
+        return null;
+    }
+
+}
diff --git a/src/main/java/net/shibboleth/tool/xmlsectool/XmlSecTool.java b/src/main/java/net/shibboleth/tool/xmlsectool/XmlSecTool.java
index df72162..f4ad755 100644
--- a/src/main/java/net/shibboleth/tool/xmlsectool/XmlSecTool.java
+++ b/src/main/java/net/shibboleth/tool/xmlsectool/XmlSecTool.java
@@ -143,7 +143,7 @@ public final class XmlSecTool {
         if (cli.doClearBlacklist()) {
             cli.getBlacklist().clear();
         }
-        for (XmlSecToolCommandLineArguments.DigestChoice dig: cli.getBlacklistDigests()) {
+        for (DigestChoice dig: cli.getBlacklistDigests()) {
             cli.getBlacklist().addDigest(dig);
         }
         if (cli.doListBlacklist()) {
diff --git a/src/main/java/net/shibboleth/tool/xmlsectool/XmlSecToolCommandLineArguments.java b/src/main/java/net/shibboleth/tool/xmlsectool/XmlSecToolCommandLineArguments.java
index fbb13f2..4eb0e2a 100644
--- a/src/main/java/net/shibboleth/tool/xmlsectool/XmlSecToolCommandLineArguments.java
+++ b/src/main/java/net/shibboleth/tool/xmlsectool/XmlSecToolCommandLineArguments.java
@@ -20,12 +20,7 @@ package net.shibboleth.tool.xmlsectool;
 import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
-import java.util.Set;
-import java.util.TreeSet;
-
-import org.opensaml.xml.signature.SignatureConstants;
 
 import com.beust.jcommander.JCommander;
 import com.beust.jcommander.Parameter;
@@ -34,253 +29,6 @@ import com.beust.jcommander.ParameterException;
 /** Command line arguments for the {@link XmlSecTool} command line tool. */
 public class XmlSecToolCommandLineArguments {
 
-    /**
-     * A blacklist of digest and signature algorithms we should not accept during
-     * signature verification.
-     */
-    public class Blacklist {
-        
-        /**
-         * Ordered set of blacklisted digest algorithm URIs.
-         */
-        private final Set<String> digestBlacklist = new TreeSet<String>();
-        
-        /**
-         * Ordered set of blacklisted signature algorithm URIs.
-         */
-        private final Set<String> signatureBlacklist = new TreeSet<String>();
-        
-        /**
-         * Constructor.
-         *
-         * Initializes the blacklist with those algorithms that should be
-         * blacklisted by default.
-         */
-        public Blacklist() {
-            // MD5
-            addDigestAlgorithm(SignatureConstants.ALGO_ID_DIGEST_NOT_RECOMMENDED_MD5);
-            addSignatureAlgorithm(SignatureConstants.ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5);
-            
-            // SHA-1
-            addDigest(DigestChoice.SHA1);
-        }
-        
-        /**
-         * Blacklist an individual digest algorithm.
-         * 
-         * @param uri algorithm URI to blacklist
-         */
-        private void addDigestAlgorithm(String uri) {
-            digestBlacklist.add(uri);
-        }
-        
-        /**
-         * Blacklist an individual signature algorithm.
-         * 
-         * @param uri algorithm URI to blacklist
-         */
-        private void addSignatureAlgorithm(String uri) {
-            signatureBlacklist.add(uri);
-        }
-        
-        /**
-         * Blacklist the digest and signature algorithms associated with
-         * a {@link DigestChoice}.
-         * 
-         * @param digestChoice {@link DigestChoice} to add to blacklist
-         */
-        public void addDigest(DigestChoice digestChoice) {
-            addDigestAlgorithm(digestChoice.getDigestAlgorithm());
-            addSignatureAlgorithm(digestChoice.getRsaAlgorithm());
-            addSignatureAlgorithm(digestChoice.getEcdsaAlgorithm());
-        }
-        
-        /**
-         * Returns <code>true</code> if the indicated algorithm URI is blacklisted for
-         * use as a digest algorithm.
-         * 
-         * @param alg digest algorithm URI to check
-         * @return <code>true</code> if the algorithm is blacklisted
-         */
-        public boolean isBlacklistedDigest(String alg) {
-            return digestBlacklist.contains(alg);
-        }
-        
-        /**
-         * Returns <code>true</code> if the indicated algorithm URI is blacklisted for
-         * use as a signature algorithm.
-         * 
-         * @param alg signature algorithm URI to check
-         * @return <code>true</code> if the algorithm is blacklisted
-         */
-        public boolean isBlacklistedSignature(String alg) {
-            return signatureBlacklist.contains(alg);
-        }
-        
-        /**
-         * Returns an unmodifiable view on the set of blacklisted digest algorithms.
-         * 
-         * @return set of blacklisted algorithms
-         */
-        public Collection<String> getDigestBlacklist() {
-            return Collections.unmodifiableCollection(digestBlacklist);
-        }
-        
-        /**
-         * Returns an unmodifiable view on the set of blacklisted signature algorithms.
-         * 
-         * @return set of blacklisted algorithms
-         */
-        public Collection<String> getSignatureBlacklist() {
-            return Collections.unmodifiableCollection(signatureBlacklist);
-        }
-        
-        /**
-         * Empties the digest and signature blacklists.
-         */
-        public void clear() {
-            digestBlacklist.clear();
-            signatureBlacklist.clear();
-        }
-    }
-    
-    /**
-     * The digest method to use in the various signature algorithms.
-     */
-    public static enum DigestChoice {
-
-        /**
-         * SHA-1 digest.
-         */
-        SHA1("SHA-1",
-                SignatureConstants.ALGO_ID_DIGEST_SHA1,
-                SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1,
-                SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA1),
-        
-        /**
-         * SHA-256 digest.
-         */
-        SHA256("SHA-256",
-                SignatureConstants.ALGO_ID_DIGEST_SHA256,
-                SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256,
-                SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA256),
-        
-        /**
-         * SHA-384 digest.
-         */
-        SHA384("SHA-384",
-                SignatureConstants.ALGO_ID_DIGEST_SHA384,
-                SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA384,
-                SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA384),
-        
-        /**
-         * SHA-512 digest.
-         */
-        SHA512("SHA-512",
-                SignatureConstants.ALGO_ID_DIGEST_SHA512,
-                SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA512,
-                SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA512);
-        
-        /**
-         * Other name (with hyphens, etc.) used as an alternative to the enum name.
-         */
-        private final String otherName;
-        
-        /**
-         * Digest algorithm.
-         */
-        private final String digestAlgorithm;
-        
-        /**
-         * RSA signature algorithm.
-         */
-        private final String rsaAlgorithm;
-        
-        /**
-         * ECDSA signature algorithm.
-         */
-        private final String ecdsaAlgorithm;
-        
-        /**
-         * Constructor.
-         * 
-         * @param otherNameArg an alternative name for the enum.
-         * @param digestArg digest algorithm URI
-         * @param rsaArg RSA signature algorithm URI
-         * @param ecdsaArg ECDSA signature algorithm URI
-         */
-        private DigestChoice(final String otherNameArg,
-                final String digestArg, final String rsaArg, final String ecdsaArg) {
-            otherName = otherNameArg;
-            digestAlgorithm = digestArg;
-            rsaAlgorithm = rsaArg;
-            ecdsaAlgorithm = ecdsaArg;
-        }
-        
-        /**
-         * Returns the digest algorithm URI for this digest choice.
-         * 
-         * @return algorithm URI
-         */
-        public String getDigestAlgorithm() {
-            return digestAlgorithm;
-        }
-        
-        /**
-         * Returns the RSA signature algorithm URI for this digest choice.
-         * 
-         * @return algorithm URI
-         */
-        public String getRsaAlgorithm() {
-            return rsaAlgorithm;
-        }
-        
-        /**
-         * Returns the ECDSA signature algorithm URI for this digest choice.
-         * 
-         * @return algorithm URI
-         */
-        public String getEcdsaAlgorithm() {
-            return ecdsaAlgorithm;
-        }
-        
-        /**
-         * Indicates whether the enum can be called by the provided name.
-         * 
-         * The name is compared ignoring case against the enum name and
-         * against the "other" name.
-         * 
-         * @param name name to check against
-         * @return <code>true</code> if and only if the enum can be called by the provided name
-         */
-        public boolean hasName(final String name) {
-            if (name.equalsIgnoreCase(name())) {
-                return true;
-            }
-            if (name.equalsIgnoreCase(otherName)) {
-                return true;
-            }
-            return false;
-        }
-
-        /**
-         * Finds the {@link DigestChoice} for a given digest name.
-         * 
-         * @param name name of the digest to be found
-         * 
-         * @return {@link DigestChoice} represented by the name
-         */
-        public static DigestChoice find(String name) {
-            for (DigestChoice choice: values()) {
-                if (choice.hasName(name)) {
-                    return choice;
-                }
-            }
-            return null;
-        }
-
-    }
-    
     /*
      * Checkstyle: JavadocVariable OFF
      * Checkstyle: JavadocMethod OFF
diff --git a/src/test/java/net/shibboleth/tool/xmlsectool/XmlSecToolCommandLineArgumentsTest.java b/src/test/java/net/shibboleth/tool/xmlsectool/XmlSecToolCommandLineArgumentsTest.java
index 2c5e92e..d0d636f 100644
--- a/src/test/java/net/shibboleth/tool/xmlsectool/XmlSecToolCommandLineArgumentsTest.java
+++ b/src/test/java/net/shibboleth/tool/xmlsectool/XmlSecToolCommandLineArgumentsTest.java
@@ -4,9 +4,6 @@ import org.opensaml.xml.signature.SignatureConstants;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
-import net.shibboleth.tool.xmlsectool.XmlSecToolCommandLineArguments.Blacklist;
-import net.shibboleth.tool.xmlsectool.XmlSecToolCommandLineArguments.DigestChoice;
-
 public class XmlSecToolCommandLineArgumentsTest {
 
 	@Test

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list