Skip to content
This repository was archived by the owner on May 26, 2020. It is now read-only.

Commit 6662c17

Browse files
committed
SANTUARIO-536 - Deprecated get/setIdToSign in favor of new and more appropriate get/setIdToSecure. Thanks to Peter De Maeyer for the patch. This closes #25.
git-svn-id: https://svn.apache.org/repos/asf/santuario/xml-security-java/trunk@1876596 13f79535-47bb-0310-9956-ffa450edef68
1 parent 36ed2db commit 6662c17

4 files changed

Lines changed: 119 additions & 13 deletions

File tree

src/main/java/org/apache/xml/security/stax/ext/OutboundXMLSec.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,16 @@ private XMLStreamWriter processOutMessage(
116116
configureSignatureKeys(outboundSecurityContext);
117117
List<SecurePart> signatureParts = securityProperties.getSignatureSecureParts();
118118
for (SecurePart securePart : signatureParts) {
119-
if (securePart.getIdToSign() == null && securePart.getName() != null) {
119+
if (securePart.getIdToSecure() == null && securePart.getName() != null) {
120120
outputProcessorChain.getSecurityContext().putAsMap(
121121
XMLSecurityConstants.SIGNATURE_PARTS,
122122
securePart.getName(),
123123
securePart
124124
);
125-
} else if (securePart.getIdToSign() != null) {
125+
} else if (securePart.getIdToSecure() != null) {
126126
outputProcessorChain.getSecurityContext().putAsMap(
127127
XMLSecurityConstants.SIGNATURE_PARTS,
128-
securePart.getIdToSign(),
128+
securePart.getIdToSecure(),
129129
securePart
130130
);
131131
} else if (securePart.getExternalReference() != null) {
@@ -146,16 +146,16 @@ private XMLStreamWriter processOutMessage(
146146
configureEncryptionKeys(outboundSecurityContext);
147147
List<SecurePart> encryptionParts = securityProperties.getEncryptionSecureParts();
148148
for (SecurePart securePart : encryptionParts) {
149-
if (securePart.getIdToSign() == null && securePart.getName() != null) {
149+
if (securePart.getIdToSecure() == null && securePart.getName() != null) {
150150
outputProcessorChain.getSecurityContext().putAsMap(
151151
XMLSecurityConstants.ENCRYPTION_PARTS,
152152
securePart.getName(),
153153
securePart
154154
);
155-
} else if (securePart.getIdToSign() != null) {
155+
} else if (securePart.getIdToSecure() != null) {
156156
outputProcessorChain.getSecurityContext().putAsMap(
157157
XMLSecurityConstants.ENCRYPTION_PARTS,
158-
securePart.getIdToSign(),
158+
securePart.getIdToSecure(),
159159
securePart
160160
);
161161
} else if (securePart.isSecureEntireRequest()) {

src/main/java/org/apache/xml/security/stax/ext/SecurePart.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@ public static Modifier getModifier(String modifier) {
5959
private QName name;
6060
private boolean generateXPointer;
6161
private Modifier modifier;
62-
private String idToSign;
62+
private String idToSecure;
6363
private String externalReference;
6464
private String[] transforms;
6565
private String digestMethod;
6666
private boolean required = true;
6767
private boolean secureEntireRequest;
6868

69+
public SecurePart(Modifier modifier) {
70+
this(null, false, modifier);
71+
}
72+
6973
public SecurePart(QName name, Modifier modifier) {
7074
this(name, false, modifier);
7175
}
@@ -125,16 +129,35 @@ public void setModifier(Modifier modifier) {
125129
}
126130

127131
/**
128-
* The id of the Element
132+
* The ID of the element to secure (encrypt or sign), possibly {@code null}.
133+
* This matches the attribute value of an element that has an attribute with a name given by
134+
* {@link XMLSecurityProperties#getIdAttributeNS()}.
129135
*
130-
* @return The id
136+
* @return The ID of the element to secure, possibly {@code null}.
137+
*/
138+
public String getIdToSecure() {
139+
return idToSecure;
140+
}
141+
142+
public void setIdToSecure(String idToSecure) {
143+
this.idToSecure = idToSecure;
144+
}
145+
146+
/**
147+
* Use {@link #getIdToSecure()} instead.
131148
*/
149+
@Deprecated
132150
public String getIdToSign() {
133-
return idToSign;
151+
return getIdToSecure();
134152
}
135153

154+
/**
155+
* Use {@link #setIdToSecure(String)} instead.
156+
* @param idToSign
157+
*/
158+
@Deprecated
136159
public void setIdToSign(String idToSign) {
137-
this.idToSign = idToSign;
160+
setIdToSecure(idToSign);
138161
}
139162

140163
public boolean isGenerateXPointer() {

src/main/java/org/apache/xml/security/stax/impl/processor/output/XMLSignatureOutputProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void processNextEvent(XMLSecEvent xmlSecEvent, OutputProcessorChain outpu
8383
}
8484

8585
if (securityProperties.isSignatureGenerateIds()) {
86-
if (securePart.getIdToSign() == null) {
86+
if (securePart.getIdToSecure() == null) {
8787
signaturePartDef.setGenerateXPointer(securePart.isGenerateXPointer());
8888
signaturePartDef.setSigRefId(IDGenerator.generateID(null));
8989

@@ -96,7 +96,7 @@ public void processNextEvent(XMLSecEvent xmlSecEvent, OutputProcessorChain outpu
9696
xmlSecEvent = addAttributes(xmlSecStartElement, attributeList);
9797
}
9898
} else {
99-
signaturePartDef.setSigRefId(securePart.getIdToSign());
99+
signaturePartDef.setSigRefId(securePart.getIdToSecure());
100100
}
101101
}
102102

src/test/java/org/apache/xml/security/test/stax/encryption/EncryptionCreationTest.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.security.PublicKey;
3131
import java.security.cert.X509Certificate;
3232
import java.util.ArrayList;
33+
import java.util.Collections;
3334
import java.util.List;
3435

3536
import javax.crypto.KeyGenerator;
@@ -1696,6 +1697,88 @@ private SecretKey generateDESSecretKey() throws Exception {
16961697
return keyFactory.generateSecret(keySpec);
16971698
}
16981699

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+
16991782
/**
17001783
* Decrypt the document using DOM API and run some tests on the decrypted Document.
17011784
*/

0 commit comments

Comments
 (0)