You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by gi...@apache.org on 2013/07/30 09:54:10 UTC

svn commit: r1508334 - in /santuario/xml-security-java/trunk/src: main/java/org/apache/xml/security/stax/impl/processor/input/ main/java/org/apache/xml/security/stax/impl/processor/output/ main/java/org/apache/xml/security/stax/impl/util/ test/java/org...

Author: giger
Date: Tue Jul 30 07:54:09 2013
New Revision: 1508334

URL: http://svn.apache.org/r1508334
Log:
fix GCM issues

Added:
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/util/FullyBufferedOutputStream.java   (with props)
Modified:
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/AbstractDecryptInputProcessor.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/output/AbstractSignatureEndingOutputProcessor.java
    santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/XMLEncryption11Test.java

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/AbstractDecryptInputProcessor.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/AbstractDecryptInputProcessor.java?rev=1508334&r1=1508333&r2=1508334&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/AbstractDecryptInputProcessor.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/AbstractDecryptInputProcessor.java Tue Jul 30 07:54:09 2013
@@ -41,9 +41,7 @@ import org.apache.xml.security.stax.impl
 import org.apache.xml.security.stax.securityToken.SecurityTokenFactory;
 import org.apache.xml.security.stax.impl.util.*;
 
-import javax.crypto.Cipher;
-import javax.crypto.CipherOutputStream;
-import javax.crypto.NoSuchPaddingException;
+import javax.crypto.*;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Unmarshaller;
@@ -714,9 +712,37 @@ public abstract class AbstractDecryptInp
         public void run() {
 
             try {
+                final OutputStream outputStream;
+
+                final Cipher cipher = getSymmetricCipher();
+                if (cipher.getAlgorithm().toUpperCase().contains("GCM")) {
+                    //we have to buffer the whole data until they are authenticated.
+                    //In GCM mode the authentication tag is appended after the last cipher block...
+                    outputStream = new FullyBufferedOutputStream(pipedOutputStream);
+                } else {
+                    outputStream = pipedOutputStream;
+                }
+
+                final CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher) {
+                    //override close() to workaround a bug in oracle-jdk:
+                    //authentication failures when using AEAD ciphers are silently ignored...
+                    @Override
+                    public void close() throws IOException {
+                        super.flush();
+                        try {
+                            byte[] bytes = cipher.doFinal();
+                            outputStream.write(bytes);
+                            outputStream.close();
+                        } catch (IllegalBlockSizeException e) {
+                            throw new IOException(e);
+                        } catch (BadPaddingException e) {
+                            throw new IOException(e);
+                        }
+                    }
+                };
                 IVSplittingOutputStream ivSplittingOutputStream = new IVSplittingOutputStream(
-                        new CipherOutputStream(pipedOutputStream, getSymmetricCipher()),
-                        getSymmetricCipher(), getSecretKey(), getIvLength());
+                        cipherOutputStream,
+                        cipher, getSecretKey(), getIvLength());
                 //buffering seems not to help
                 //bufferedOutputStream = new BufferedOutputStream(new Base64OutputStream(ivSplittingOutputStream, false), 8192 * 5);
                 ReplaceableOuputStream replaceableOuputStream = new ReplaceableOuputStream(ivSplittingOutputStream);

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=1508334&r1=1508333&r2=1508334&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 Tue Jul 30 07:54:09 2013
@@ -135,6 +135,7 @@ public abstract class AbstractSignatureE
 
         String sigAlgorithm = getSecurityProperties().getSignatureAlgorithm();
         Key key = wrappingSecurityToken.getSecretKey(sigAlgorithm);
+        //todo remove and use wrappingSecurityToken.isSymmetric or so?
         if (XMLSecurityConstants.NS_XMLDSIG_HMACSHA1.equals(sigAlgorithm)) {
             key = XMLSecurityUtils.prepareSecretKey(sigAlgorithm, key.getEncoded());
         }

Added: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/util/FullyBufferedOutputStream.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/util/FullyBufferedOutputStream.java?rev=1508334&view=auto
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/util/FullyBufferedOutputStream.java (added)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/util/FullyBufferedOutputStream.java Tue Jul 30 07:54:09 2013
@@ -0,0 +1,58 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.xml.security.stax.impl.util;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class FullyBufferedOutputStream extends FilterOutputStream {
+
+    private UnsynchronizedByteArrayOutputStream buf = new UnsynchronizedByteArrayOutputStream();
+
+    public FullyBufferedOutputStream(OutputStream out) {
+        super(out);
+    }
+
+    @Override
+    public void write(int b) throws IOException {
+        buf.write(b);
+    }
+
+    @Override
+    public void write(byte[] b) throws IOException {
+        buf.write(b);
+    }
+
+    @Override
+    public void write(byte[] b, int off, int len) throws IOException {
+        buf.write(b, off, len);
+    }
+
+    @Override
+    public void close() throws IOException {
+        buf.writeTo(out);
+        out.close();
+    }
+
+    @Override
+    public void flush() throws IOException {
+        //nothing to do here
+    }
+}

Propchange: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/util/FullyBufferedOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/XMLEncryption11Test.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/XMLEncryption11Test.java?rev=1508334&r1=1508333&r2=1508334&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/XMLEncryption11Test.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/XMLEncryption11Test.java Tue Jul 30 07:54:09 2013
@@ -20,6 +20,7 @@ package org.apache.xml.security.test.sta
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.security.Key;
 import java.security.KeyStore;
@@ -31,12 +32,14 @@ import java.security.cert.X509Certificat
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.crypto.BadPaddingException;
 import javax.crypto.KeyGenerator;
 import javax.crypto.SecretKey;
 import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.dom.DOMSource;
@@ -395,6 +398,58 @@ public class XMLEncryption11Test extends
     }
 
     /**
+     * rsa-oaep, Digest:SHA512, MGF:SHA512, PSource: Specified 8 bytes
+     */
+    @org.junit.Test
+    public void testAESGCMAuthentication() throws Exception {
+
+        String keystore = "org/w3c/www/interop/xmlenc-core-11/RSA-4096_SHA256WithRSA.jks";
+
+        KeyStore keyStore = KeyStore.getInstance("jks");
+        keyStore.load(this.getClass().getClassLoader().getResourceAsStream(keystore), "passwd".toCharArray());
+
+        Certificate cert = keyStore.getCertificate("importkey");
+
+        KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
+                keyStore.getEntry("importkey", new KeyStore.PasswordProtection("passwd".toCharArray()));
+        PrivateKey rsaKey = pkEntry.getPrivateKey();
+        X509Certificate x509Certificate = (X509Certificate) pkEntry.getCertificate();
+
+        // Perform encryption
+        String filename = "org/w3c/www/interop/xmlenc-core-11/plaintext.xml";
+
+        KeyGenerator keygen = KeyGenerator.getInstance("AES");
+        keygen.init(256);
+        SecretKey sessionKey = keygen.generateKey();
+
+        SecurePart securePart =
+                new SecurePart(new QName("urn:example:po", "PurchaseOrder"), SecurePart.Modifier.Element);
+
+        Document ed = encryptDocument(filename, securePart,
+                x509Certificate.getPublicKey(), "http://www.w3.org/2009/xmlenc11#rsa-oaep",
+                "http://www.w3.org/2001/04/xmlenc#sha512",
+                "http://www.w3.org/2009/xmlenc11#mgf1sha512",
+                sessionKey, "http://www.w3.org/2009/xmlenc11#aes256-gcm",
+                Base64.decode("ZHVtbXkxMjM=".getBytes("UTF-8")));
+        // XMLUtils.outputDOM(ed.getFirstChild(), System.out);
+
+        NodeList nl = ed.getElementsByTagNameNS("http://www.w3.org/2001/04/xmlenc#", "CipherValue");
+        Element cipherValue = (Element) nl.item(1);
+        String elementText = cipherValue.getTextContent();
+        elementText = elementText.substring(0, 100) + 0 + elementText.substring(100);
+        cipherValue.setTextContent(elementText);
+
+        // Perform decryption
+        try {
+            Document dd = decryptElementStAX(ed, rsaKey, (X509Certificate) cert);
+        } catch (XMLStreamException e) {
+            Assert.assertTrue(e.getCause() instanceof IOException);
+            Assert.assertTrue(e.getCause().getCause() instanceof BadPaddingException);
+            Assert.assertEquals("mac check in GCM failed", e.getCause().getCause().getMessage());
+        }
+    }
+
+    /**
      * Method decryptElement
      * <p/>
      * Take a key, encryption type and a file, find an encrypted element