[java-opensaml COMMIT] in /trunk: opensaml-xmlsec-api/src/main/java/org/opensaml/xmlsec/algorithm/AlgorithmSupport.ja...

noreply at shibboleth.net noreply at shibboleth.net
Wed Apr 9 21:18:05 EDT 2014


Author: putmanb
Date: Wed Apr  9 21:18:05 2014
New Revision: 3775

URL: http://svn.shibboleth.net/view/java-opensaml?rev=3775&view=rev
Log:
Add some methods to AlgorithmSupport which evaluate AlgorithmDescriptors for various purposes.
Leverage AlgorithmRegistry support in encryption and signing parameters resovlers.

Modified:
    trunk/opensaml-xmlsec-api/src/main/java/org/opensaml/xmlsec/algorithm/AlgorithmSupport.java
    trunk/opensaml-xmlsec-api/src/test/java/org/opensaml/xmlsec/algorithm/AlgorithmSupportTest.java
    trunk/opensaml-xmlsec-impl/src/main/java/org/opensaml/xmlsec/impl/BasicEncryptionParametersResolver.java
    trunk/opensaml-xmlsec-impl/src/main/java/org/opensaml/xmlsec/impl/BasicSignatureSigningParametersResolver.java

Modified: trunk/opensaml-xmlsec-api/src/main/java/org/opensaml/xmlsec/algorithm/AlgorithmSupport.java
URL: http://svn.shibboleth.net/view/java-opensaml/trunk/opensaml-xmlsec-api/src/main/java/org/opensaml/xmlsec/algorithm/AlgorithmSupport.java?rev=3775&r1=3774&r2=3775&view=diff
==============================================================================
--- trunk/opensaml-xmlsec-api/src/main/java/org/opensaml/xmlsec/algorithm/AlgorithmSupport.java (original)
+++ trunk/opensaml-xmlsec-api/src/main/java/org/opensaml/xmlsec/algorithm/AlgorithmSupport.java Wed Apr  9 21:18:05 2014
@@ -17,10 +17,13 @@
 
 package org.opensaml.xmlsec.algorithm;
 
+import java.security.Key;
 import java.security.KeyException;
 import java.security.KeyPair;
 import java.security.NoSuchAlgorithmException;
 import java.security.NoSuchProviderException;
+import java.security.PrivateKey;
+import java.security.PublicKey;
 import java.util.Collection;
 
 import javax.annotation.Nonnull;
@@ -31,6 +34,7 @@
 import org.opensaml.core.config.ConfigurationService;
 import org.opensaml.security.credential.BasicCredential;
 import org.opensaml.security.credential.Credential;
+import org.opensaml.security.credential.CredentialSupport;
 import org.opensaml.security.crypto.KeySupport;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -57,6 +61,161 @@
      */
     @Nullable public static AlgorithmRegistry getGlobalAlgorithmRegistry() {
         return ConfigurationService.get(AlgorithmRegistry.class);
+    }
+    
+    /**
+     * Check whether the supplied descriptor represents an algorithm that my be used for
+     * key encryption, i.e. a key transport or symmetric key wrap algorithm.
+     * 
+     * @param algorithm the algorithm descriptor to evaluate
+     * @return true if the algorithm may be used for key encryption, false otherwise
+     */
+    public static boolean isKeyEncryptionAlgorithm(@Nullable final AlgorithmDescriptor algorithm) {    
+        if (algorithm == null) {
+            return false;
+        }
+        
+        switch(algorithm.getType()) {
+            case KeyTransport:
+            case SymmetricKeyWrap:
+                return true;
+            default:
+                return false;
+        }
+    }
+    
+    /**
+     * Check whether the supplied descriptor represents an algorithm that my be used for
+     * data encryption, i.e. a block encryption algorithm.
+     * 
+     * @param algorithm the algorithm descriptor to evaluate
+     * @return true if the algorithm may be used for key encryption, false otherwise
+     */
+    public static boolean isDataEncryptionAlgorithm(@Nullable AlgorithmDescriptor algorithm) {
+        if (algorithm == null) {
+            return false;
+        }
+        
+        switch(algorithm.getType()) {
+            case BlockEncryption:
+                return true;
+            default:
+                return false;
+        }
+    }
+    
+    /**
+     * Check whether the supplied credential may be used with the supplied algorithm for the purpose of
+     * signing.  
+     * 
+     * <p>
+     * This checks the consistency of the type of credential signing key and the algorithm type, as well
+     * as the key algorithm and length where applicable.
+     * </p>
+     * 
+     * @param credential the candidate signing credential to evaluate
+     * @param algorithm the candidate signing algorithm to evaluate
+     * @return true if the credential may be used with the algorithm for signing, false otherwise
+     */
+    public static boolean credentialSupportsAlgorithmForSigning(@Nullable final Credential credential, 
+            @Nullable final AlgorithmDescriptor algorithm) {
+        if (credential == null || algorithm == null) {
+            return false;
+        }
+        
+        Key key = CredentialSupport.extractSigningKey(credential);
+        if (key == null) {
+            return false;
+        }
+        
+        switch(algorithm.getType()) {
+            case Signature:
+                if (!(key instanceof PrivateKey)) {
+                    return false;
+                }
+                break;
+            case Mac:
+                if (!(key instanceof SecretKey)) {
+                    return false;
+                }
+                break;
+            default:
+                return false;
+        }
+        

[... 527 lines stripped ...]


More information about the commits mailing list