| /** |
| * 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.test.stax.encryption; |
| |
| import java.io.ByteArrayInputStream; |
| import java.io.ByteArrayOutputStream; |
| import java.io.InputStream; |
| import java.lang.reflect.Constructor; |
| import java.nio.charset.StandardCharsets; |
| import java.security.Key; |
| import java.security.Provider; |
| import java.security.Security; |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import javax.crypto.KeyGenerator; |
| import javax.crypto.SecretKey; |
| import javax.xml.namespace.QName; |
| import javax.xml.stream.XMLInputFactory; |
| import javax.xml.stream.XMLStreamReader; |
| import javax.xml.stream.XMLStreamWriter; |
| |
| import org.apache.xml.security.encryption.XMLCipher; |
| import org.apache.xml.security.stax.ext.OutboundXMLSec; |
| import org.apache.xml.security.stax.ext.SecurePart; |
| import org.apache.xml.security.stax.ext.XMLSec; |
| import org.apache.xml.security.stax.ext.XMLSecurityConstants; |
| import org.apache.xml.security.stax.ext.XMLSecurityProperties; |
| import org.apache.xml.security.test.stax.utils.XMLSecEventAllocator; |
| import org.apache.xml.security.test.stax.utils.XmlReaderToWriter; |
| import org.apache.xml.security.utils.XMLUtils; |
| import org.junit.Assert; |
| import org.junit.BeforeClass; |
| import org.junit.Test; |
| import org.w3c.dom.Document; |
| import org.w3c.dom.Element; |
| import org.w3c.dom.NodeList; |
| |
| /** |
| * A test to make sure that the various Symmetric Encryption algorithms are working |
| */ |
| public class SymmetricEncryptionCreationTest extends Assert { |
| |
| private static boolean bcInstalled; |
| private XMLInputFactory xmlInputFactory; |
| |
| @BeforeClass |
| public static void setup() throws Exception { |
| // |
| // If the BouncyCastle provider is not installed, then try to load it |
| // via reflection. |
| // |
| if (Security.getProvider("BC") == null) { |
| Constructor<?> cons = null; |
| try { |
| Class<?> c = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider"); |
| cons = c.getConstructor(new Class[] {}); |
| } catch (Exception e) { |
| //ignore |
| } |
| if (cons != null) { |
| Provider provider = (Provider)cons.newInstance(); |
| Security.insertProviderAt(provider, 2); |
| bcInstalled = true; |
| } |
| } |
| } |
| |
| public SymmetricEncryptionCreationTest() throws Exception { |
| org.apache.xml.security.Init.init(); |
| |
| xmlInputFactory = XMLInputFactory.newInstance(); |
| xmlInputFactory.setEventAllocator(new XMLSecEventAllocator()); |
| } |
| |
| @org.junit.AfterClass |
| public static void cleanup() throws Exception { |
| Security.removeProvider("BC"); |
| } |
| |
| @Test |
| public void testAES128() throws Exception { |
| // Set up the Configuration |
| XMLSecurityProperties properties = new XMLSecurityProperties(); |
| List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>(); |
| actions.add(XMLSecurityConstants.ENCRYPT); |
| properties.setActions(actions); |
| |
| // Set the key up |
| KeyGenerator keygen = KeyGenerator.getInstance("AES"); |
| keygen.init(128); |
| SecretKey key = keygen.generateKey(); |
| properties.setEncryptionKey(key); |
| |
| String algorithm = "http://www.w3.org/2001/04/xmlenc#aes128-cbc"; |
| properties.setEncryptionSymAlgorithm(algorithm); |
| |
| SecurePart securePart = |
| new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element); |
| properties.addEncryptionPart(securePart); |
| |
| OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties); |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, StandardCharsets.UTF_8.name()); |
| |
| InputStream sourceDocument = |
| this.getClass().getClassLoader().getResourceAsStream( |
| "ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml"); |
| XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument); |
| |
| XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter); |
| xmlStreamWriter.close(); |
| |
| // System.out.println("Got:\n" + new String(baos.toByteArray(), StandardCharsets.UTF_8.name())); |
| |
| Document document = null; |
| try (InputStream is = new ByteArrayInputStream(baos.toByteArray())) { |
| document = XMLUtils.createDocumentBuilder(false).parse(is); |
| } |
| |
| NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| // Check the CreditCard encrypted ok |
| nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| nodeList = document.getElementsByTagNameNS( |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| ); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| |
| // Decrypt using DOM API |
| Document doc = decryptUsingDOM(algorithm, key, null, document); |
| |
| // Check the CreditCard decrypted ok |
| nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| } |
| |
| @Test |
| public void testAES128_GCM() throws Exception { |
| // Set up the Configuration |
| XMLSecurityProperties properties = new XMLSecurityProperties(); |
| List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>(); |
| actions.add(XMLSecurityConstants.ENCRYPT); |
| properties.setActions(actions); |
| |
| // Set the key up |
| KeyGenerator keygen = KeyGenerator.getInstance("AES"); |
| keygen.init(128); |
| SecretKey key = keygen.generateKey(); |
| properties.setEncryptionKey(key); |
| |
| String algorithm = "http://www.w3.org/2009/xmlenc11#aes128-gcm"; |
| properties.setEncryptionSymAlgorithm(algorithm); |
| |
| SecurePart securePart = |
| new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element); |
| properties.addEncryptionPart(securePart); |
| |
| OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties); |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, StandardCharsets.UTF_8.name()); |
| |
| InputStream sourceDocument = |
| this.getClass().getClassLoader().getResourceAsStream( |
| "ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml"); |
| XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument); |
| |
| XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter); |
| xmlStreamWriter.close(); |
| |
| // System.out.println("Got:\n" + new String(baos.toByteArray(), StandardCharsets.UTF_8.name())); |
| |
| Document document = null; |
| try (InputStream is = new ByteArrayInputStream(baos.toByteArray())) { |
| document = XMLUtils.createDocumentBuilder(false).parse(is); |
| } |
| |
| NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| // Check the CreditCard encrypted ok |
| nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| nodeList = document.getElementsByTagNameNS( |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| ); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| |
| // Decrypt using DOM API |
| Document doc = decryptUsingDOM(algorithm, key, null, document); |
| |
| // Check the CreditCard decrypted ok |
| nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| } |
| |
| @Test |
| public void testAES192() throws Exception { |
| // Set up the Configuration |
| XMLSecurityProperties properties = new XMLSecurityProperties(); |
| List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>(); |
| actions.add(XMLSecurityConstants.ENCRYPT); |
| properties.setActions(actions); |
| |
| // Set the key up |
| KeyGenerator keygen = KeyGenerator.getInstance("AES"); |
| keygen.init(192); |
| SecretKey key = keygen.generateKey(); |
| properties.setEncryptionKey(key); |
| |
| String algorithm = "http://www.w3.org/2001/04/xmlenc#aes192-cbc"; |
| properties.setEncryptionSymAlgorithm(algorithm); |
| |
| SecurePart securePart = |
| new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element); |
| properties.addEncryptionPart(securePart); |
| |
| OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties); |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, StandardCharsets.UTF_8.name()); |
| |
| InputStream sourceDocument = |
| this.getClass().getClassLoader().getResourceAsStream( |
| "ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml"); |
| XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument); |
| |
| XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter); |
| xmlStreamWriter.close(); |
| |
| // System.out.println("Got:\n" + new String(baos.toByteArray(), StandardCharsets.UTF_8.name())); |
| |
| Document document = null; |
| try (InputStream is = new ByteArrayInputStream(baos.toByteArray())) { |
| document = XMLUtils.createDocumentBuilder(false).parse(is); |
| } |
| |
| NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| // Check the CreditCard encrypted ok |
| nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| nodeList = document.getElementsByTagNameNS( |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| ); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| |
| // Decrypt using DOM API |
| Document doc = decryptUsingDOM(algorithm, key, null, document); |
| |
| // Check the CreditCard decrypted ok |
| nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| } |
| |
| @Test |
| public void testAES192_GCM() throws Exception { |
| // Set up the Configuration |
| XMLSecurityProperties properties = new XMLSecurityProperties(); |
| List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>(); |
| actions.add(XMLSecurityConstants.ENCRYPT); |
| properties.setActions(actions); |
| |
| // Set the key up |
| KeyGenerator keygen = KeyGenerator.getInstance("AES"); |
| keygen.init(192); |
| SecretKey key = keygen.generateKey(); |
| properties.setEncryptionKey(key); |
| |
| String algorithm = "http://www.w3.org/2009/xmlenc11#aes192-gcm"; |
| properties.setEncryptionSymAlgorithm(algorithm); |
| |
| SecurePart securePart = |
| new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element); |
| properties.addEncryptionPart(securePart); |
| |
| OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties); |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, StandardCharsets.UTF_8.name()); |
| |
| InputStream sourceDocument = |
| this.getClass().getClassLoader().getResourceAsStream( |
| "ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml"); |
| XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument); |
| |
| XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter); |
| xmlStreamWriter.close(); |
| |
| // System.out.println("Got:\n" + new String(baos.toByteArray(), StandardCharsets.UTF_8.name())); |
| |
| Document document = null; |
| try (InputStream is = new ByteArrayInputStream(baos.toByteArray())) { |
| document = XMLUtils.createDocumentBuilder(false).parse(is); |
| } |
| |
| NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| // Check the CreditCard encrypted ok |
| nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| nodeList = document.getElementsByTagNameNS( |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| ); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| |
| // Decrypt using DOM API |
| Document doc = decryptUsingDOM(algorithm, key, null, document); |
| |
| // Check the CreditCard decrypted ok |
| nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| } |
| |
| @Test |
| public void testAES256() throws Exception { |
| // Set up the Configuration |
| XMLSecurityProperties properties = new XMLSecurityProperties(); |
| List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>(); |
| actions.add(XMLSecurityConstants.ENCRYPT); |
| properties.setActions(actions); |
| |
| // Set the key up |
| KeyGenerator keygen = KeyGenerator.getInstance("AES"); |
| keygen.init(256); |
| SecretKey key = keygen.generateKey(); |
| properties.setEncryptionKey(key); |
| |
| String algorithm = "http://www.w3.org/2001/04/xmlenc#aes256-cbc"; |
| properties.setEncryptionSymAlgorithm(algorithm); |
| |
| SecurePart securePart = |
| new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element); |
| properties.addEncryptionPart(securePart); |
| |
| OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties); |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, StandardCharsets.UTF_8.name()); |
| |
| InputStream sourceDocument = |
| this.getClass().getClassLoader().getResourceAsStream( |
| "ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml"); |
| XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument); |
| |
| XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter); |
| xmlStreamWriter.close(); |
| |
| // System.out.println("Got:\n" + new String(baos.toByteArray(), StandardCharsets.UTF_8.name())); |
| |
| Document document = null; |
| try (InputStream is = new ByteArrayInputStream(baos.toByteArray())) { |
| document = XMLUtils.createDocumentBuilder(false).parse(is); |
| } |
| |
| NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| // Check the CreditCard encrypted ok |
| nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| nodeList = document.getElementsByTagNameNS( |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| ); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| |
| // Decrypt using DOM API |
| Document doc = decryptUsingDOM(algorithm, key, null, document); |
| |
| // Check the CreditCard decrypted ok |
| nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| } |
| |
| @Test |
| public void testAES256_GCM() throws Exception { |
| // Set up the Configuration |
| XMLSecurityProperties properties = new XMLSecurityProperties(); |
| List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>(); |
| actions.add(XMLSecurityConstants.ENCRYPT); |
| properties.setActions(actions); |
| |
| // Set the key up |
| KeyGenerator keygen = KeyGenerator.getInstance("AES"); |
| keygen.init(256); |
| SecretKey key = keygen.generateKey(); |
| properties.setEncryptionKey(key); |
| |
| String algorithm = "http://www.w3.org/2009/xmlenc11#aes256-gcm"; |
| properties.setEncryptionSymAlgorithm(algorithm); |
| |
| SecurePart securePart = |
| new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element); |
| properties.addEncryptionPart(securePart); |
| |
| OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties); |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, StandardCharsets.UTF_8.name()); |
| |
| InputStream sourceDocument = |
| this.getClass().getClassLoader().getResourceAsStream( |
| "ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml"); |
| XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument); |
| |
| XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter); |
| xmlStreamWriter.close(); |
| |
| // System.out.println("Got:\n" + new String(baos.toByteArray(), StandardCharsets.UTF_8.name())); |
| |
| Document document = null; |
| try (InputStream is = new ByteArrayInputStream(baos.toByteArray())) { |
| document = XMLUtils.createDocumentBuilder(false).parse(is); |
| } |
| |
| NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| // Check the CreditCard encrypted ok |
| nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| nodeList = document.getElementsByTagNameNS( |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| ); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| |
| // Decrypt using DOM API |
| Document doc = decryptUsingDOM(algorithm, key, null, document); |
| |
| // Check the CreditCard decrypted ok |
| nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| } |
| |
| @Test |
| public void testTRIPLE_DES() throws Exception { |
| // Set up the Configuration |
| XMLSecurityProperties properties = new XMLSecurityProperties(); |
| List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>(); |
| actions.add(XMLSecurityConstants.ENCRYPT); |
| properties.setActions(actions); |
| |
| // Set the key up |
| KeyGenerator keygen = KeyGenerator.getInstance("DESede"); |
| SecretKey key = keygen.generateKey(); |
| properties.setEncryptionKey(key); |
| |
| String algorithm = "http://www.w3.org/2001/04/xmlenc#tripledes-cbc"; |
| properties.setEncryptionSymAlgorithm(algorithm); |
| |
| SecurePart securePart = |
| new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element); |
| properties.addEncryptionPart(securePart); |
| |
| OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties); |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, StandardCharsets.UTF_8.name()); |
| |
| InputStream sourceDocument = |
| this.getClass().getClassLoader().getResourceAsStream( |
| "ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml"); |
| XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument); |
| |
| XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter); |
| xmlStreamWriter.close(); |
| |
| // System.out.println("Got:\n" + new String(baos.toByteArray(), StandardCharsets.UTF_8.name())); |
| |
| Document document = null; |
| try (InputStream is = new ByteArrayInputStream(baos.toByteArray())) { |
| document = XMLUtils.createDocumentBuilder(false).parse(is); |
| } |
| |
| NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| // Check the CreditCard encrypted ok |
| nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| nodeList = document.getElementsByTagNameNS( |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| ); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| |
| // Decrypt using DOM API |
| Document doc = decryptUsingDOM(algorithm, key, null, document); |
| |
| // Check the CreditCard decrypted ok |
| nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| } |
| |
| @Test |
| public void testSEED_128() throws Exception { |
| org.junit.Assume.assumeTrue(bcInstalled); |
| |
| // Set up the Configuration |
| XMLSecurityProperties properties = new XMLSecurityProperties(); |
| List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>(); |
| actions.add(XMLSecurityConstants.ENCRYPT); |
| properties.setActions(actions); |
| |
| // Set the key up |
| KeyGenerator keygen = KeyGenerator.getInstance("SEED"); |
| keygen.init(128); |
| SecretKey key = keygen.generateKey(); |
| properties.setEncryptionKey(key); |
| |
| String algorithm = "http://www.w3.org/2007/05/xmldsig-more#seed128-cbc"; |
| properties.setEncryptionSymAlgorithm(algorithm); |
| |
| SecurePart securePart = |
| new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element); |
| properties.addEncryptionPart(securePart); |
| |
| OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties); |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, StandardCharsets.UTF_8.name()); |
| |
| InputStream sourceDocument = |
| this.getClass().getClassLoader().getResourceAsStream( |
| "ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml"); |
| XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument); |
| |
| XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter); |
| xmlStreamWriter.close(); |
| |
| // System.out.println("Got:\n" + new String(baos.toByteArray(), StandardCharsets.UTF_8.name())); |
| |
| Document document = null; |
| try (InputStream is = new ByteArrayInputStream(baos.toByteArray())) { |
| document = XMLUtils.createDocumentBuilder(false).parse(is); |
| } |
| |
| NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| // Check the CreditCard encrypted ok |
| nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| nodeList = document.getElementsByTagNameNS( |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| ); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| |
| // Decrypt using DOM API |
| Document doc = decryptUsingDOM(algorithm, key, null, document); |
| |
| // Check the CreditCard decrypted ok |
| nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| } |
| |
| @Test |
| public void testCAMELLIA_128() throws Exception { |
| org.junit.Assume.assumeTrue(bcInstalled); |
| |
| // Set up the Configuration |
| XMLSecurityProperties properties = new XMLSecurityProperties(); |
| List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>(); |
| actions.add(XMLSecurityConstants.ENCRYPT); |
| properties.setActions(actions); |
| |
| // Set the key up |
| KeyGenerator keygen = KeyGenerator.getInstance("Camellia"); |
| keygen.init(128); |
| SecretKey key = keygen.generateKey(); |
| properties.setEncryptionKey(key); |
| |
| String algorithm = "http://www.w3.org/2001/04/xmldsig-more#camellia128-cbc"; |
| properties.setEncryptionSymAlgorithm(algorithm); |
| |
| SecurePart securePart = |
| new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element); |
| properties.addEncryptionPart(securePart); |
| |
| OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties); |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, StandardCharsets.UTF_8.name()); |
| |
| InputStream sourceDocument = |
| this.getClass().getClassLoader().getResourceAsStream( |
| "ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml"); |
| XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument); |
| |
| XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter); |
| xmlStreamWriter.close(); |
| |
| // System.out.println("Got:\n" + new String(baos.toByteArray(), StandardCharsets.UTF_8.name())); |
| |
| Document document = null; |
| try (InputStream is = new ByteArrayInputStream(baos.toByteArray())) { |
| document = XMLUtils.createDocumentBuilder(false).parse(is); |
| } |
| |
| NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| // Check the CreditCard encrypted ok |
| nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| nodeList = document.getElementsByTagNameNS( |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| ); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| |
| // Decrypt using DOM API |
| Document doc = decryptUsingDOM(algorithm, key, null, document); |
| |
| // Check the CreditCard decrypted ok |
| nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| } |
| |
| @Test |
| public void testCAMELLIA_192() throws Exception { |
| org.junit.Assume.assumeTrue(bcInstalled); |
| |
| // Set up the Configuration |
| XMLSecurityProperties properties = new XMLSecurityProperties(); |
| List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>(); |
| actions.add(XMLSecurityConstants.ENCRYPT); |
| properties.setActions(actions); |
| |
| // Set the key up |
| KeyGenerator keygen = KeyGenerator.getInstance("Camellia"); |
| keygen.init(192); |
| SecretKey key = keygen.generateKey(); |
| properties.setEncryptionKey(key); |
| |
| String algorithm = "http://www.w3.org/2001/04/xmldsig-more#camellia192-cbc"; |
| properties.setEncryptionSymAlgorithm(algorithm); |
| |
| SecurePart securePart = |
| new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element); |
| properties.addEncryptionPart(securePart); |
| |
| OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties); |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, StandardCharsets.UTF_8.name()); |
| |
| InputStream sourceDocument = |
| this.getClass().getClassLoader().getResourceAsStream( |
| "ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml"); |
| XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument); |
| |
| XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter); |
| xmlStreamWriter.close(); |
| |
| // System.out.println("Got:\n" + new String(baos.toByteArray(), StandardCharsets.UTF_8.name())); |
| |
| Document document = null; |
| try (InputStream is = new ByteArrayInputStream(baos.toByteArray())) { |
| document = XMLUtils.createDocumentBuilder(false).parse(is); |
| } |
| |
| NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| // Check the CreditCard encrypted ok |
| nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| nodeList = document.getElementsByTagNameNS( |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| ); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| |
| // Decrypt using DOM API |
| Document doc = decryptUsingDOM(algorithm, key, null, document); |
| |
| // Check the CreditCard decrypted ok |
| nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| } |
| |
| @Test |
| public void testCAMELLIA_256() throws Exception { |
| org.junit.Assume.assumeTrue(bcInstalled); |
| |
| // Set up the Configuration |
| XMLSecurityProperties properties = new XMLSecurityProperties(); |
| List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>(); |
| actions.add(XMLSecurityConstants.ENCRYPT); |
| properties.setActions(actions); |
| |
| // Set the key up |
| KeyGenerator keygen = KeyGenerator.getInstance("Camellia"); |
| keygen.init(256); |
| SecretKey key = keygen.generateKey(); |
| properties.setEncryptionKey(key); |
| |
| String algorithm = "http://www.w3.org/2001/04/xmldsig-more#camellia256-cbc"; |
| properties.setEncryptionSymAlgorithm(algorithm); |
| |
| SecurePart securePart = |
| new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element); |
| properties.addEncryptionPart(securePart); |
| |
| OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties); |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, StandardCharsets.UTF_8.name()); |
| |
| InputStream sourceDocument = |
| this.getClass().getClassLoader().getResourceAsStream( |
| "ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml"); |
| XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument); |
| |
| XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter); |
| xmlStreamWriter.close(); |
| |
| // System.out.println("Got:\n" + new String(baos.toByteArray(), StandardCharsets.UTF_8.name())); |
| |
| Document document = null; |
| try (InputStream is = new ByteArrayInputStream(baos.toByteArray())) { |
| document = XMLUtils.createDocumentBuilder(false).parse(is); |
| } |
| |
| NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| // Check the CreditCard encrypted ok |
| nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| Assert.assertEquals(nodeList.getLength(), 0); |
| |
| nodeList = document.getElementsByTagNameNS( |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| ); |
| Assert.assertEquals(nodeList.getLength(), 1); |
| |
| // Decrypt using DOM API |
| Document doc = decryptUsingDOM(algorithm, key, null, document); |
| |
| // Check the CreditCard decrypted ok |
| nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard"); |
| } |
| |
| /** |
| * Decrypt the document using DOM API and run some tests on the decrypted Document. |
| */ |
| private Document decryptUsingDOM( |
| String algorithm, |
| SecretKey secretKey, |
| Key wrappingKey, |
| Document document |
| ) throws Exception { |
| XMLCipher cipher = XMLCipher.getInstance(algorithm); |
| cipher.init(XMLCipher.DECRYPT_MODE, secretKey); |
| if (wrappingKey != null) { |
| cipher.setKEK(wrappingKey); |
| } |
| |
| NodeList nodeList = document.getElementsByTagNameNS( |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| ); |
| Element ee = (Element)nodeList.item(0); |
| return cipher.doFinal(document, ee); |
| } |
| |
| } |