You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2013/07/03 12:11:51 UTC

svn commit: r1499306 - in /santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax: ext/XMLSecurityUtils.java impl/processor/input/AbstractSignatureInputHandler.java impl/processor/output/AbstractSignatureEndingOutputProcessor.java

Author: coheigea
Date: Wed Jul  3 10:11:51 2013
New Revision: 1499306

URL: http://svn.apache.org/r1499306
Log:
Set the secret key up correctly for HMAC-SHA1

Modified:
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/XMLSecurityUtils.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/AbstractSignatureInputHandler.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/output/AbstractSignatureEndingOutputProcessor.java

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/XMLSecurityUtils.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/XMLSecurityUtils.java?rev=1499306&r1=1499305&r2=1499306&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/XMLSecurityUtils.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/XMLSecurityUtils.java Wed Jul  3 10:11:51 2013
@@ -19,6 +19,7 @@
 package org.apache.xml.security.stax.ext;
 
 import org.apache.commons.codec.binary.Base64;
+import org.apache.xml.security.algorithms.JCEMapper;
 import org.apache.xml.security.exceptions.XMLSecurityException;
 import org.apache.xml.security.keys.content.x509.XMLX509SKI;
 import org.apache.xml.security.stax.config.TransformerAlgorithmMapper;
@@ -31,6 +32,8 @@ import org.apache.xml.security.stax.secu
 import org.apache.xml.security.stax.securityToken.InboundSecurityToken;
 import org.apache.xml.security.stax.securityToken.SecurityTokenConstants;
 
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
 import javax.xml.bind.JAXBElement;
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamException;
@@ -373,4 +376,30 @@ public class XMLSecurityUtils {
             outputStream.write(buf, 0, read);
         }
     }
+    
+    /**
+     * Convert the raw key bytes into a SecretKey object of type symEncAlgo.
+     */
+    public static SecretKey prepareSecretKey(String symEncAlgo, byte[] rawKey) {
+        // Do an additional check on the keysize required by the encryption algorithm
+        int size = 0;
+        try {
+            size = JCEMapper.getKeyLengthFromURI(symEncAlgo) / 8;
+        } catch (Exception e) {
+            // ignore - some unknown (to JCEMapper) encryption algorithm
+            size = 0;
+        }
+        String keyAlgorithm = JCEMapper.getJCEKeyAlgorithmFromURI(symEncAlgo);
+        SecretKeySpec keySpec;
+        if (size > 0) {
+            keySpec = 
+                new SecretKeySpec(
+                    rawKey, 0, ((rawKey.length > size) ? size : rawKey.length), keyAlgorithm
+                );
+        } else {
+            keySpec = new SecretKeySpec(rawKey, keyAlgorithm);
+        }
+        return keySpec;
+    }
+
 }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/AbstractSignatureInputHandler.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/AbstractSignatureInputHandler.java?rev=1499306&r1=1499305&r2=1499306&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/AbstractSignatureInputHandler.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/AbstractSignatureInputHandler.java Wed Jul  3 10:11:51 2013
@@ -291,8 +291,9 @@ public abstract class AbstractSignatureI
             } else {
                 verifyKey = inboundSecurityToken.getSecretKey(
                         algorithmURI, XMLSecurityConstants.Sym_Sig, signatureType.getId());
+                verifyKey = XMLSecurityUtils.prepareSecretKey(algorithmURI, verifyKey.getEncoded());
             }
-
+            
             try {
                 SignatureAlgorithm signatureAlgorithm =
                         SignatureAlgorithmFactory.getInstance().getSignatureAlgorithm(

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/output/AbstractSignatureEndingOutputProcessor.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/output/AbstractSignatureEndingOutputProcessor.java?rev=1499306&r1=1499305&r2=1499306&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/output/AbstractSignatureEndingOutputProcessor.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/output/AbstractSignatureEndingOutputProcessor.java Wed Jul  3 10:11:51 2013
@@ -18,30 +18,37 @@
  */
 package org.apache.xml.security.stax.impl.processor.output;
 
+import java.io.IOException;
+import java.io.OutputStream;
+import java.security.Key;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import javax.xml.stream.XMLStreamException;
+
 import org.apache.commons.codec.binary.Base64;
 import org.apache.xml.security.exceptions.XMLSecurityException;
-import org.apache.xml.security.stax.ext.*;
-import org.apache.xml.security.stax.securityToken.SecurityTokenProvider;
+import org.apache.xml.security.stax.ext.AbstractBufferingOutputProcessor;
+import org.apache.xml.security.stax.ext.AbstractOutputProcessor;
+import org.apache.xml.security.stax.ext.OutputProcessorChain;
+import org.apache.xml.security.stax.ext.Transformer;
+import org.apache.xml.security.stax.ext.XMLSecurityConstants;
+import org.apache.xml.security.stax.ext.XMLSecurityUtils;
 import org.apache.xml.security.stax.ext.stax.XMLSecAttribute;
 import org.apache.xml.security.stax.ext.stax.XMLSecEvent;
 import org.apache.xml.security.stax.ext.stax.XMLSecStartElement;
 import org.apache.xml.security.stax.impl.SignaturePartDef;
 import org.apache.xml.security.stax.impl.algorithms.SignatureAlgorithm;
 import org.apache.xml.security.stax.impl.algorithms.SignatureAlgorithmFactory;
-import org.apache.xml.security.stax.securityToken.OutboundSecurityToken;
 import org.apache.xml.security.stax.impl.util.IDGenerator;
 import org.apache.xml.security.stax.impl.util.SignerOutputStream;
 import org.apache.xml.security.stax.impl.util.UnsynchronizedBufferedOutputStream;
-
-import javax.xml.stream.XMLStreamException;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.security.NoSuchAlgorithmException;
-import java.security.NoSuchProviderException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
+import org.apache.xml.security.stax.securityToken.OutboundSecurityToken;
+import org.apache.xml.security.stax.securityToken.SecurityTokenProvider;
 
 /**
  * @author $Author$
@@ -126,7 +133,12 @@ public abstract class AbstractSignatureE
             throw new XMLSecurityException("stax.keyNotFound");
         }
 
-        signatureAlgorithm.engineInitSign(wrappingSecurityToken.getSecretKey(getSecurityProperties().getSignatureAlgorithm()));
+        String sigAlgorithm = getSecurityProperties().getSignatureAlgorithm();
+        Key key = wrappingSecurityToken.getSecretKey(sigAlgorithm);
+        if (XMLSecurityConstants.NS_XMLDSIG_HMACSHA1.equals(sigAlgorithm)) {
+            key = XMLSecurityUtils.prepareSecretKey(sigAlgorithm, key.getEncoded());
+        }
+        signatureAlgorithm.engineInitSign(key);
 
         SignedInfoProcessor signedInfoProcessor = newSignedInfoProcessor(signatureAlgorithm, signatureElement, subOutputProcessorChain);
         createStartElementAndOutputAsEvent(subOutputProcessorChain, XMLSecurityConstants.TAG_dsig_SignedInfo, false, null);
@@ -204,7 +216,7 @@ public abstract class AbstractSignatureE
             OutputProcessorChain outputProcessorChain,
             OutboundSecurityToken securityToken,
             boolean useSingleCertificate) throws XMLStreamException, XMLSecurityException;
-
+    
 
     public class SignedInfoProcessor extends AbstractOutputProcessor {