ws-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject svn commit: r1668605 - in /webservices/wss4j/trunk: ws-security-common/src/main/java/org/apache/wss4j/common/util/ ws-security-dom/src/main/java/org/apache/wss4j/dom/message/
Date Mon, 23 Mar 2015 11:45:17 GMT
Author: coheigea
Date: Mon Mar 23 11:45:17 2015
New Revision: 1668605

URL: http://svn.apache.org/r1668605
Log:
Move KeyGenerator code to the common module

Modified:
    webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/util/KeyUtils.java
    webservices/wss4j/trunk/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/WSSecEncrypt.java
    webservices/wss4j/trunk/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/WSSecEncryptedKey.java

Modified: webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/util/KeyUtils.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/util/KeyUtils.java?rev=1668605&r1=1668604&r2=1668605&view=diff
==============================================================================
--- webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/util/KeyUtils.java
(original)
+++ webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/util/KeyUtils.java
Mon Mar 23 11:45:17 2015
@@ -19,11 +19,15 @@
 
 package org.apache.wss4j.common.util;
 
+import java.security.NoSuchAlgorithmException;
+
+import javax.crypto.KeyGenerator;
 import javax.crypto.SecretKey;
 import javax.crypto.spec.SecretKeySpec;
 
 import org.apache.wss4j.common.ext.WSSecurityException;
 import org.apache.xml.security.algorithms.JCEMapper;
+import org.apache.xml.security.encryption.XMLCipher;
 
 public final class KeyUtils {
     private static final org.slf4j.Logger LOG =
@@ -45,22 +49,22 @@ public final class KeyUtils {
     }
     
     /**
-     * Convert the raw key bytes into a SecretKey object of type symEncAlgo.
+     * Convert the raw key bytes into a SecretKey object of type algorithm.
      */
-    public static SecretKey prepareSecretKey(String symEncAlgo, byte[] rawKey) {
+    public static SecretKey prepareSecretKey(String algorithm, byte[] rawKey) {
         // Do an additional check on the keysize required by the encryption algorithm
         int size = 0;
         try {
-            size = getKeyLength(symEncAlgo);
+            size = getKeyLength(algorithm);
         } catch (Exception e) {
             // ignore - some unknown (to JCEMapper) encryption algorithm
             if (LOG.isDebugEnabled()) {
                 LOG.debug(e.getMessage());
             }
         }
-        String keyAlgorithm = JCEMapper.getJCEKeyAlgorithmFromURI(symEncAlgo);
+        String keyAlgorithm = JCEMapper.getJCEKeyAlgorithmFromURI(algorithm);
         SecretKeySpec keySpec;
-        if (size > 0 && !symEncAlgo.endsWith("gcm") && !symEncAlgo.contains("hmac-"))
{
+        if (size > 0 && !algorithm.endsWith("gcm") && !algorithm.contains("hmac-"))
{
             keySpec = 
                 new SecretKeySpec(
                     rawKey, 0, rawKey.length > size ? size : rawKey.length, keyAlgorithm
@@ -75,5 +79,33 @@ public final class KeyUtils {
             keySpec = new SecretKeySpec(rawKey, keyAlgorithm);
         }
         return keySpec;
-    }    
+    }
+    
+    public static KeyGenerator getKeyGenerator(String algorithm) throws WSSecurityException
{
+        try {
+            //
+            // Assume AES as default, so initialize it
+            //
+            String keyAlgorithm = JCEMapper.getJCEKeyAlgorithmFromURI(algorithm);
+            if (keyAlgorithm == null || "".equals(keyAlgorithm)) {
+                keyAlgorithm = JCEMapper.translateURItoJCEID(algorithm);
+            }
+            KeyGenerator keyGen = KeyGenerator.getInstance(keyAlgorithm);
+            if (algorithm.equalsIgnoreCase(XMLCipher.AES_128)
+                || algorithm.equalsIgnoreCase(XMLCipher.AES_128_GCM)) {
+                keyGen.init(128);
+            } else if (algorithm.equalsIgnoreCase(XMLCipher.AES_192)
+                || algorithm.equalsIgnoreCase(XMLCipher.AES_192_GCM)) {
+                keyGen.init(192);
+            } else if (algorithm.equalsIgnoreCase(XMLCipher.AES_256)
+                || algorithm.equalsIgnoreCase(XMLCipher.AES_256_GCM)) {
+                keyGen.init(256);
+            }
+            return keyGen;
+        } catch (NoSuchAlgorithmException e) {
+            throw new WSSecurityException(
+                WSSecurityException.ErrorCode.UNSUPPORTED_ALGORITHM, e
+            );
+        }
+    }
 }

Modified: webservices/wss4j/trunk/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/WSSecEncrypt.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/WSSecEncrypt.java?rev=1668605&r1=1668604&r2=1668605&view=diff
==============================================================================
--- webservices/wss4j/trunk/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/WSSecEncrypt.java
(original)
+++ webservices/wss4j/trunk/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/WSSecEncrypt.java
Mon Mar 23 11:45:17 2015
@@ -122,7 +122,7 @@ public class WSSecEncrypt extends WSSecE
                 symmetricKey = 
                     KeyUtils.prepareSecretKey(getSymmetricEncAlgorithm(), getEphemeralKey());
             } else {
-                KeyGenerator keyGen = getKeyGenerator();
+                KeyGenerator keyGen = KeyUtils.getKeyGenerator(getSymmetricEncAlgorithm());
                 symmetricKey = keyGen.generateKey();
             }
         }

Modified: webservices/wss4j/trunk/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/WSSecEncryptedKey.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/WSSecEncryptedKey.java?rev=1668605&r1=1668604&r2=1668605&view=diff
==============================================================================
--- webservices/wss4j/trunk/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/WSSecEncryptedKey.java
(original)
+++ webservices/wss4j/trunk/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/WSSecEncryptedKey.java
Mon Mar 23 11:45:17 2015
@@ -21,7 +21,6 @@ package org.apache.wss4j.dom.message;
 
 import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
 import java.security.cert.X509Certificate;
 import java.security.spec.MGF1ParameterSpec;
 
@@ -189,7 +188,7 @@ public class WSSecEncryptedKey extends W
             if (ephemeralKey != null) {
                 symmetricKey = KeyUtils.prepareSecretKey(symEncAlgo, ephemeralKey);
             } else {
-                KeyGenerator keyGen = getKeyGenerator();
+                KeyGenerator keyGen = KeyUtils.getKeyGenerator(symEncAlgo);
                 symmetricKey = keyGen.generateKey();
                 ephemeralKey = symmetricKey.getEncoded();
             }
@@ -437,34 +436,6 @@ public class WSSecEncryptedKey extends W
         bstToken.setID(IDGenerator.generateID(null));
     }
 
-    protected KeyGenerator getKeyGenerator() throws WSSecurityException {
-        try {
-            //
-            // Assume AES as default, so initialize it
-            //
-            String keyAlgorithm = JCEMapper.getJCEKeyAlgorithmFromURI(symEncAlgo);
-            if (keyAlgorithm == null || "".equals(keyAlgorithm)) {
-                keyAlgorithm = JCEMapper.translateURItoJCEID(symEncAlgo);
-            }
-            KeyGenerator keyGen = KeyGenerator.getInstance(keyAlgorithm);
-            if (symEncAlgo.equalsIgnoreCase(WSConstants.AES_128)
-                || symEncAlgo.equalsIgnoreCase(WSConstants.AES_128_GCM)) {
-                keyGen.init(128);
-            } else if (symEncAlgo.equalsIgnoreCase(WSConstants.AES_192)
-                || symEncAlgo.equalsIgnoreCase(WSConstants.AES_192_GCM)) {
-                keyGen.init(192);
-            } else if (symEncAlgo.equalsIgnoreCase(WSConstants.AES_256)
-                || symEncAlgo.equalsIgnoreCase(WSConstants.AES_256_GCM)) {
-                keyGen.init(256);
-            }
-            return keyGen;
-        } catch (NoSuchAlgorithmException e) {
-            throw new WSSecurityException(
-                WSSecurityException.ErrorCode.UNSUPPORTED_ALGORITHM, e
-            );
-        }
-    }
-
     /**
      * Create DOM subtree for <code>xenc:EncryptedKey</code>
      * 



Mime
View raw message