[java-opensaml] branch master updated: OSJ-299: Enhance AlgorithmRegistry to support lookup by AlgorithmType
Brent Putman
putmanb at georgetown.edu
Fri Feb 14 18:25:21 EST 2020
This is an automated email from the git hooks/post-receive script.
putmanb pushed a commit to branch master
in repository java-opensaml.
View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=bcb885af115d0c1c6427cbfc658d23128826ba73
The following commit(s) were added to refs/heads/master by this push:
new bcb885a OSJ-299: Enhance AlgorithmRegistry to support lookup by AlgorithmType
bcb885a is described below
commit bcb885af115d0c1c6427cbfc658d23128826ba73
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Fri Feb 14 18:25:19 2020 -0500
OSJ-299: Enhance AlgorithmRegistry to support lookup by AlgorithmType
---
.../xmlsec/algorithm/AlgorithmRegistry.java | 54 +++++++++++
.../xmlsec/algorithm/AlgorithmRegistryTest.java | 102 +++++++++++++++++++++
2 files changed, 156 insertions(+)
diff --git a/opensaml-xmlsec-api/src/main/java/org/opensaml/xmlsec/algorithm/AlgorithmRegistry.java b/opensaml-xmlsec-api/src/main/java/org/opensaml/xmlsec/algorithm/AlgorithmRegistry.java
index 9a83643..ab6d96d 100644
--- a/opensaml-xmlsec-api/src/main/java/org/opensaml/xmlsec/algorithm/AlgorithmRegistry.java
+++ b/opensaml-xmlsec-api/src/main/java/org/opensaml/xmlsec/algorithm/AlgorithmRegistry.java
@@ -20,11 +20,13 @@ package org.opensaml.xmlsec.algorithm;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
+import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -32,9 +34,13 @@ import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.NoSuchPaddingException;
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotLive;
+import net.shibboleth.utilities.java.support.annotation.constraint.Unmodifiable;
import net.shibboleth.utilities.java.support.logic.Constraint;
import net.shibboleth.utilities.java.support.primitive.StringSupport;
+import org.opensaml.xmlsec.algorithm.AlgorithmDescriptor.AlgorithmType;
import org.opensaml.xmlsec.encryption.support.EncryptionConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -52,6 +58,9 @@ public class AlgorithmRegistry {
/** Map of registered algorithm descriptors. */
private Map<String, AlgorithmDescriptor> descriptors;
+ /** Index of registered AlgorithmType to algorithm URI. */
+ private Map<AlgorithmType, Set<String>> types;
+
/** Set containing algorithms which are supported by the runtime environment. */
private Set<String> runtimeSupported;
@@ -64,6 +73,7 @@ public class AlgorithmRegistry {
/** Constructor. */
public AlgorithmRegistry() {
descriptors = new HashMap<>();
+ types = new HashMap<>();
runtimeSupported = new HashSet<>();
digestAlgorithms = new HashMap<>();
signatureAlgorithms = new HashMap<>();
@@ -194,12 +204,51 @@ public class AlgorithmRegistry {
}
/**
+ * Get the set of algorithm URIs registered for the given type.
+ *
+ * @param type the algorithm type
+ *
+ * @return the set of URIs for the given type, may be empty
+ */
+ @Unmodifiable @NonnullElements @NotLive
+ public Set<String> getRegisteredURIsByType(@Nonnull final AlgorithmType type) {
+ Constraint.isNotNull(type, "AlgorithmType was null");
+ final Set<String> byType = types.get(type);
+ if (byType != null) {
+ return Set.copyOf(byType);
+ }
+ return Collections.emptySet();
+ }
+
+ /**
+ * Get the set of {@link AlgorithmDescriptor} registered for the given type.
+ *
+ * @param type the algorithm type
+ *
+ * @return the set of descriptors for the given type, may be empty
+ */
+ @Unmodifiable @NonnullElements @NotLive
+ public Set<AlgorithmDescriptor> getRegisteredByType(@Nonnull final AlgorithmType type) {
+ return getRegisteredURIsByType(type).stream()
+ .map(this::get)
+ .filter(Objects::nonNull)
+ .collect(Collectors.toUnmodifiableSet());
+ }
+
+ /**
* Add the algorithm descriptor to the indexes which support the various lookup methods
* available via the registry's API.
*
* @param descriptor the algorithm
*/
private void index(final AlgorithmDescriptor descriptor) {
+ Set<String> byType = types.get(descriptor.getType());
+ if (byType == null) {
+ byType = new HashSet<>();
+ types.put(descriptor.getType(), byType);
+ }
+ byType.add(descriptor.getURI());
+
if (checkRuntimeSupports(descriptor)) {
runtimeSupported.add(descriptor.getURI());
} else {
@@ -226,6 +275,11 @@ public class AlgorithmRegistry {
* @param descriptor the algorithm
*/
private void deindex(final AlgorithmDescriptor descriptor) {
+ final Set<String> byType = types.get(descriptor.getType());
+ if (byType != null) {
+ byType.remove(descriptor.getURI());
+ }
+
runtimeSupported.remove(descriptor.getURI());
if (descriptor instanceof DigestAlgorithm) {
diff --git a/opensaml-xmlsec-api/src/test/java/org/opensaml/xmlsec/algorithm/AlgorithmRegistryTest.java b/opensaml-xmlsec-api/src/test/java/org/opensaml/xmlsec/algorithm/AlgorithmRegistryTest.java
index 55906ca..8c6f7fa 100644
--- a/opensaml-xmlsec-api/src/test/java/org/opensaml/xmlsec/algorithm/AlgorithmRegistryTest.java
+++ b/opensaml-xmlsec-api/src/test/java/org/opensaml/xmlsec/algorithm/AlgorithmRegistryTest.java
@@ -17,11 +17,15 @@
package org.opensaml.xmlsec.algorithm;
+import java.util.Objects;
+import java.util.Set;
+
import org.opensaml.core.OpenSAMLInitBaseTestCase;
import org.opensaml.core.config.ConfigurationService;
import org.opensaml.core.config.InitializationException;
import org.opensaml.security.SecurityProviderTestSupport;
import org.opensaml.security.crypto.JCAConstants;
+import org.opensaml.xmlsec.algorithm.AlgorithmDescriptor.AlgorithmType;
import org.opensaml.xmlsec.algorithm.descriptors.DigestSHA256;
import org.opensaml.xmlsec.algorithm.descriptors.SignatureRSASHA256;
import org.opensaml.xmlsec.config.GlobalAlgorithmRegistryInitializer;
@@ -73,6 +77,28 @@ public class AlgorithmRegistryTest extends OpenSAMLInitBaseTestCase {
}
@Test
+ public void testTypeIndexing() {
+ AlgorithmRegistry registry = new AlgorithmRegistry();
+
+ Assert.assertNull(registry.get(SignatureConstants.ALGO_ID_DIGEST_SHA256));
+
+ Assert.assertTrue(registry.getRegisteredURIsByType(AlgorithmType.MessageDigest).isEmpty());
+ Assert.assertTrue(registry.getRegisteredByType(AlgorithmType.MessageDigest).isEmpty());
+
+ registry.register(new DigestSHA256());
+
+ Assert.assertEquals(registry.getRegisteredURIsByType(AlgorithmType.MessageDigest).size(), 1);
+ Assert.assertTrue(registry.getRegisteredURIsByType(AlgorithmType.MessageDigest).contains(SignatureConstants.ALGO_ID_DIGEST_SHA256));
+ Assert.assertEquals(registry.getRegisteredByType(AlgorithmType.MessageDigest).size(), 1);
+ Assert.assertTrue(DigestSHA256.class.isInstance(registry.getRegisteredByType(AlgorithmType.MessageDigest).iterator().next()));
+
+ registry.deregister(new DigestSHA256());
+
+ Assert.assertTrue(registry.getRegisteredURIsByType(AlgorithmType.MessageDigest).isEmpty());
+ Assert.assertTrue(registry.getRegisteredByType(AlgorithmType.MessageDigest).isEmpty());
+ }
+
+ @Test
public void testSignatureIndexing() {
AlgorithmRegistry registry = new AlgorithmRegistry();
Assert.assertNull(registry.get(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256));
@@ -167,6 +193,82 @@ public class AlgorithmRegistryTest extends OpenSAMLInitBaseTestCase {
}
+ @Test
+ public void testGlobalRegistryGetByType() {
+ AlgorithmRegistry registry = AlgorithmSupport.getGlobalAlgorithmRegistry();
+ Assert.assertNotNull(registry);
+
+ // Test all expected types from default auto-loaded set
+ Set<String> byType = null;
+
+ // BlockEncryption
+ byType = registry.getRegisteredURIsByType(AlgorithmType.BlockEncryption);
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128));
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128_GCM));
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES192));
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES192_GCM));
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES256));
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES256_GCM));
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_BLOCKCIPHER_TRIPLEDES));
+ Assert.assertEquals(registry.getRegisteredByType(AlgorithmType.BlockEncryption).stream().filter(Objects::nonNull).count(), byType.size());
+
+ // Digest
+ byType = registry.getRegisteredURIsByType(AlgorithmType.MessageDigest);
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_DIGEST_NOT_RECOMMENDED_MD5));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_DIGEST_RIPEMD160));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_DIGEST_SHA1));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_DIGEST_SHA224));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_DIGEST_SHA256));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_DIGEST_SHA384));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_DIGEST_SHA512));
+ Assert.assertEquals(registry.getRegisteredByType(AlgorithmType.MessageDigest).stream().filter(Objects::nonNull).count(), byType.size());
+
+ // HMAC
+ byType = registry.getRegisteredURIsByType(AlgorithmType.Mac);
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_MAC_HMAC_NOT_RECOMMENDED_MD5));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_MAC_HMAC_RIPEMD160));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_MAC_HMAC_SHA1));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_MAC_HMAC_SHA224));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_MAC_HMAC_SHA256));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_MAC_HMAC_SHA384));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_MAC_HMAC_SHA512));
+ Assert.assertEquals(registry.getRegisteredByType(AlgorithmType.Mac).stream().filter(Objects::nonNull).count(), byType.size());
+
+ // KeyTransport
+ byType = registry.getRegisteredURIsByType(AlgorithmType.KeyTransport);
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSA15));
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSAOAEP));
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSAOAEP11));
+ Assert.assertEquals(registry.getRegisteredByType(AlgorithmType.KeyTransport).stream().filter(Objects::nonNull).count(), byType.size());
+
+ // Signature
+ byType = registry.getRegisteredURIsByType(AlgorithmType.Signature);
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_DSA_SHA1));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_DSA_SHA256));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA1));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA224));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA256));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA384));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA512));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_RSA_RIPEMD160));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA224));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA384));
+ Assert.assertTrue(byType.contains(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA512));
+ Assert.assertEquals(registry.getRegisteredByType(AlgorithmType.Signature).stream().filter(Objects::nonNull).count(), byType.size());
+
+ // SymmetricKeyWrap
+ byType = registry.getRegisteredURIsByType(AlgorithmType.SymmetricKeyWrap);
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_KEYWRAP_AES128));
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_KEYWRAP_AES192));
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_KEYWRAP_AES256));
+ Assert.assertTrue(byType.contains(EncryptionConstants.ALGO_ID_KEYWRAP_TRIPLEDES));
+ Assert.assertEquals(registry.getRegisteredByType(AlgorithmType.SymmetricKeyWrap).stream().filter(Objects::nonNull).count(), byType.size());
+
+ }
+
@Test(dataProvider = "loadBCTestData")
public void testGlobalRegistryRuntimeSupported(boolean loadBC) throws InitializationException {
AlgorithmRegistry originalRegistry = AlgorithmSupport.getGlobalAlgorithmRegistry();
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list