|
30 | 30 | import java.security.PublicKey; |
31 | 31 | import java.security.cert.X509Certificate; |
32 | 32 | import java.util.ArrayList; |
| 33 | +import java.util.Collections; |
33 | 34 | import java.util.List; |
34 | 35 |
|
35 | 36 | import javax.crypto.KeyGenerator; |
@@ -1696,6 +1697,88 @@ private SecretKey generateDESSecretKey() throws Exception { |
1696 | 1697 | return keyFactory.generateSecret(keySpec); |
1697 | 1698 | } |
1698 | 1699 |
|
| 1700 | + @Test |
| 1701 | + public void testEncryptionIdToEncrypt() throws Exception { |
| 1702 | + SecurePart securePart = new SecurePart(SecurePart.Modifier.Element); |
| 1703 | + securePart.setIdToSecure("abc"); |
| 1704 | + testEncryptionIdToEncrypt(securePart); |
| 1705 | + } |
| 1706 | + |
| 1707 | + @Test |
| 1708 | + public void testEncryptionIdToSign() throws Exception { |
| 1709 | + SecurePart securePart = new SecurePart(SecurePart.Modifier.Element); |
| 1710 | + securePart.setIdToSign("abc"); |
| 1711 | + testEncryptionIdToEncrypt(securePart); |
| 1712 | + } |
| 1713 | + |
| 1714 | + private void testEncryptionIdToEncrypt(SecurePart securePart) throws Exception { |
| 1715 | + String xml = "<?xml version='1.0'?>\n" + |
| 1716 | + "<Root>\n" + |
| 1717 | + " <Branch attr1='abc'/>\n" + |
| 1718 | + "</Root>\n"; |
| 1719 | + XMLSecurityProperties properties = new XMLSecurityProperties(); |
| 1720 | + properties.setIdAttributeNS(new QName("attr1")); |
| 1721 | + properties.setActions(Collections.singletonList(XMLSecurityConstants.ENCRYPT)); |
| 1722 | + properties.addEncryptionPart(securePart); |
| 1723 | + byte[] bits192 = "abcdefghijklmnopqrstuvwx".getBytes(StandardCharsets.US_ASCII); |
| 1724 | + SecretKey transportKey = new SecretKeySpec(bits192, "AES"); |
| 1725 | + properties.setEncryptionKeyTransportAlgorithm("http://www.w3.org/2001/04/xmlenc#kw-aes192"); |
| 1726 | + properties.setEncryptionTransportKey(transportKey); |
| 1727 | + properties.setEncryptionSymAlgorithm("http://www.w3.org/2001/04/xmlenc#aes128-cbc"); |
| 1728 | + OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties); |
| 1729 | + ByteArrayOutputStream encryptedOut = new ByteArrayOutputStream(); |
| 1730 | + XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(encryptedOut, StandardCharsets.UTF_8.name()); |
| 1731 | + InputStream sourceDocument = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)); |
| 1732 | + XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument); |
| 1733 | + XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter); |
| 1734 | + xmlStreamWriter.close(); |
| 1735 | + byte[] encryptedData = encryptedOut.toByteArray(); |
| 1736 | +// System.out.println(new String(encryptedOut.toByteArray(), StandardCharsets.UTF_8)); |
| 1737 | + Document document = XMLUtils.read(new ByteArrayInputStream(encryptedData), false); |
| 1738 | + NodeList encryptedElements = document.getElementsByTagNameNS( |
| 1739 | + XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| 1740 | + XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| 1741 | + ); |
| 1742 | + assertEquals(encryptedElements.getLength(), 1); |
| 1743 | + } |
| 1744 | + |
| 1745 | + @Test |
| 1746 | + public void testEncryptionIdToSecureSupersedesName() throws Exception { |
| 1747 | + String xml = "<?xml version='1.0'?>\n" + |
| 1748 | + "<Root>\n" + |
| 1749 | + " <Branch1 attr1='abc'/>\n" + |
| 1750 | + " <Branch2 attr1='def'/>\n" + |
| 1751 | + "</Root>\n"; |
| 1752 | + XMLSecurityProperties properties = new XMLSecurityProperties(); |
| 1753 | + properties.setIdAttributeNS(new QName("attr1")); |
| 1754 | + properties.setActions(Collections.singletonList(XMLSecurityConstants.ENCRYPT)); |
| 1755 | + SecurePart securePart = new SecurePart(new QName("Branch1"), SecurePart.Modifier.Element); |
| 1756 | + securePart.setIdToSecure("def"); |
| 1757 | + properties.addEncryptionPart(securePart); |
| 1758 | + byte[] bits192 = "abcdefghijklmnopqrstuvwx".getBytes(StandardCharsets.US_ASCII); |
| 1759 | + SecretKey transportKey = new SecretKeySpec(bits192, "AES"); |
| 1760 | + properties.setEncryptionKeyTransportAlgorithm("http://www.w3.org/2001/04/xmlenc#kw-aes192"); |
| 1761 | + properties.setEncryptionTransportKey(transportKey); |
| 1762 | + properties.setEncryptionSymAlgorithm("http://www.w3.org/2001/04/xmlenc#aes128-cbc"); |
| 1763 | + OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties); |
| 1764 | + ByteArrayOutputStream encryptedOut = new ByteArrayOutputStream(); |
| 1765 | + XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(encryptedOut, StandardCharsets.UTF_8.name()); |
| 1766 | + InputStream sourceDocument = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)); |
| 1767 | + XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument); |
| 1768 | + XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter); |
| 1769 | + xmlStreamWriter.close(); |
| 1770 | + byte[] encryptedData = encryptedOut.toByteArray(); |
| 1771 | +// System.out.println(new String(encryptedOut.toByteArray(), StandardCharsets.UTF_8)); |
| 1772 | + Document document = XMLUtils.read(new ByteArrayInputStream(encryptedData), false); |
| 1773 | + NodeList encryptedElements = document.getElementsByTagNameNS( |
| 1774 | + XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), |
| 1775 | + XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart() |
| 1776 | + ); |
| 1777 | + assertEquals(1, encryptedElements.getLength()); |
| 1778 | + assertEquals(1, document.getElementsByTagName("Branch1").getLength()); |
| 1779 | + assertEquals(0, document.getElementsByTagName("Branch2").getLength()); |
| 1780 | + } |
| 1781 | + |
1699 | 1782 | /** |
1700 | 1783 | * Decrypt the document using DOM API and run some tests on the decrypted Document. |
1701 | 1784 | */ |
|
0 commit comments