This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch 3_0_x-fixes
in repository https://gitbox.apache.org/repos/asf/ws-wss4j.git
The following commit(s) were added to refs/heads/3_0_x-fixes by this push:
new bc3969e15 Enforce the required symmetric algorithm on encrypted
attachments (StAX) (#773)
bc3969e15 is described below
commit bc3969e1517b2478130ddf2750171bd8d92e4741
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Fri Nov 6 07:45:35 2026 +0000
Enforce the required symmetric algorithm on encrypted attachments (StAX)
(#773)
---
.../processor/input/DecryptInputProcessor.java | 36 +++++++--
.../org/apache/wss4j/stax/test/AttachmentTest.java | 85 ++++++++++++++++++++++
2 files changed, 113 insertions(+), 8 deletions(-)
diff --git
a/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/input/DecryptInputProcessor.java
b/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/input/DecryptInputProcessor.java
index c41267a3e..7cc1a532d 100644
---
a/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/input/DecryptInputProcessor.java
+++
b/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/input/DecryptInputProcessor.java
@@ -179,6 +179,10 @@ public class DecryptInputProcessor extends
AbstractDecryptInputProcessor {
protected void handleCipherReference(InputProcessorChain
inputProcessorChain, EncryptedDataType encryptedDataType,
Cipher cipher, InboundSecurityToken
inboundSecurityToken) throws XMLSecurityException {
+ // An attachment is decrypted from here rather than through
newDecryptedEventReaderInputProcessor,
+ // so the required algorithm must be checked here too
+ checkEncryptionAlgorithm(encryptedDataType);
+
String typeStr = encryptedDataType.getType();
if (typeStr != null
&&
(WSSConstants.SWA_ATTACHMENT_ENCRYPTED_DATA_TYPE_CONTENT_ONLY.equals(typeStr)
@@ -209,6 +213,8 @@ public class DecryptInputProcessor extends
AbstractDecryptInputProcessor {
@Override
protected InputStream handleXOPInclude(InputProcessorChain
inputProcessorChain, EncryptedDataType encryptedDataType, String href,
Cipher cipher, InboundSecurityToken
inboundSecurityToken) throws XMLSecurityException {
+ checkEncryptionAlgorithm(encryptedDataType);
+
if (href == null || href.length() < 5) {
throw new
WSSecurityException(WSSecurityException.ErrorCode.FAILED_CHECK);
}
@@ -257,15 +263,8 @@ public class DecryptInputProcessor extends
AbstractDecryptInputProcessor {
boolean encryptedHeader, XMLSecStartElement xmlSecStartElement,
EncryptedDataType encryptedDataType,
InboundSecurityToken inboundSecurityToken, InboundSecurityContext
inboundSecurityContext) throws XMLSecurityException {
- // Check encryption algorithm against the required algorithm, if
defined
+ checkEncryptionAlgorithm(encryptedDataType);
String encryptionAlgorithm =
encryptedDataType.getEncryptionMethod().getAlgorithm();
- if (this.getSecurityProperties().getEncryptionSymAlgorithm() != null
- &&
!this.getSecurityProperties().getEncryptionSymAlgorithm().equals(encryptionAlgorithm))
{
- LOG.warn(
- "The Key encryption method does not match the requirement"
- );
- throw new
WSSecurityException(WSSecurityException.ErrorCode.INVALID_SECURITY);
- }
if (!WSSConstants.NS_XENC_TRIPLE_DES.equals(encryptionAlgorithm)
&& !WSSConstants.NS_XENC_AES128.equals(encryptionAlgorithm)
@@ -280,6 +279,27 @@ public class DecryptInputProcessor extends
AbstractDecryptInputProcessor {
encryptedHeader, xmlSecStartElement, encryptedDataType, this,
inboundSecurityToken);
}
+ /**
+ * Check the encryption algorithm of the EncryptedData against the
required algorithm, if one is
+ * defined. This must be done on every path that decrypts: the algorithm
is stated by the message
+ * itself, so where it is not checked the sender chooses it, e.g.
relabelling an aes128-gcm
+ * EncryptedData as aes128-cbc, which takes the same key.
+ */
+ private void checkEncryptionAlgorithm(EncryptedDataType encryptedDataType)
throws XMLSecurityException {
+ String requiredAlgorithm =
this.getSecurityProperties().getEncryptionSymAlgorithm();
+ if (requiredAlgorithm == null) {
+ return;
+ }
+ String encryptionAlgorithm = encryptedDataType.getEncryptionMethod()
!= null
+ ? encryptedDataType.getEncryptionMethod().getAlgorithm() : null;
+ if (!requiredAlgorithm.equals(encryptionAlgorithm)) {
+ LOG.warn(
+ "The Key encryption method does not match the requirement"
+ );
+ throw new
WSSecurityException(WSSecurityException.ErrorCode.INVALID_SECURITY);
+ }
+ }
+
@Override
protected void handleSecurityToken(InboundSecurityToken
inboundSecurityToken, InboundSecurityContext inboundSecurityContext,
EncryptedDataType encryptedDataType)
throws XMLSecurityException {
diff --git
a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/AttachmentTest.java
b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/AttachmentTest.java
index 889a9ae47..2c9fc51f8 100644
---
a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/AttachmentTest.java
+++
b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/AttachmentTest.java
@@ -559,6 +559,91 @@ public class AttachmentTest extends AbstractTestBase {
assertEquals(6, attHeaders.size());
}
+ /**
+ * The required symmetric algorithm must be enforced for an encrypted
attachment as well as for the
+ * SOAP Body. Here the attachment's EncryptedData is relabelled from
aes128-gcm to aes128-cbc, which
+ * takes the same 16-byte key: decrypting it would expose the attachment
to a CBC padding oracle.
+ */
+ @Test
+ public void testXMLAttachmentContentEncryptionAlgorithmRelabelled() throws
Exception {
+ final String attachmentId = UUID.randomUUID().toString();
+ final Attachment attachment = new Attachment();
+ attachment.setMimeType("text/xml");
+ attachment.addHeaders(getHeaders(attachmentId));
+ attachment.setId(attachmentId);
+ attachment.setSourceStream(new
ByteArrayInputStream(SOAPUtil.SAMPLE_SOAP_MSG.getBytes(StandardCharsets.UTF_8)));
+ AttachmentCallbackHandler attachmentCallbackHandler =
+ new
AttachmentCallbackHandler(Collections.singletonList(attachment));
+ List<Attachment> encryptedAttachments =
attachmentCallbackHandler.getResponseAttachments();
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ {
+ WSSSecurityProperties securityProperties = new
WSSSecurityProperties();
+ List<WSSConstants.Action> actions = new ArrayList<>();
+ actions.add(WSSConstants.ENCRYPTION);
+ securityProperties.setActions(actions);
+
securityProperties.loadEncryptionKeystore(this.getClass().getClassLoader().getResource("transmitter.jks"),
"default".toCharArray());
+ securityProperties.setEncryptionUser("receiver");
+ securityProperties.addEncryptionPart(new SecurePart(new
QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"),
SecurePart.Modifier.Content));
+ securityProperties.addEncryptionPart(new
SecurePart("cid:Attachments", SecurePart.Modifier.Content));
+
securityProperties.setAttachmentCallbackHandler(attachmentCallbackHandler);
+
securityProperties.setEncryptionSymAlgorithm(WSSConstants.NS_XENC11_AES128_GCM);
+
+ OutboundWSSec wsSecOut =
WSSec.getOutboundWSSec(securityProperties);
+ XMLStreamWriter xmlStreamWriter = wsSecOut.processOutMessage(baos,
StandardCharsets.UTF_8.name(), new ArrayList<SecurityEvent>());
+ XMLStreamReader xmlStreamReader =
xmlInputFactory.createXMLStreamReader(this.getClass().getClassLoader().getResourceAsStream("testdata/plain-soap-1.1.xml"));
+ XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
+ xmlStreamWriter.close();
+ }
+
+ // Relabel only the attachment's EncryptedData, so that the Body still
meets the requirement
+ Document securedDoc =
documentBuilderFactory.newDocumentBuilder().parse(new
ByteArrayInputStream(baos.toByteArray()));
+ NodeList encDatas =
securedDoc.getElementsByTagNameNS(WSConstants.ENC_NS, "EncryptedData");
+ Element attachmentEncData = null;
+ for (int i = 0; i < encDatas.getLength(); i++) {
+ Element encData = (Element) encDatas.item(i);
+ if
(WSConstants.SWA_ATTACHMENT_ENCRYPTED_DATA_TYPE_CONTENT_ONLY.equals(encData.getAttributeNS(null,
"Type"))) {
+ attachmentEncData = encData;
+ break;
+ }
+ }
+ assertNotNull(attachmentEncData);
+ Element encryptionMethod =
+ (Element)
attachmentEncData.getElementsByTagNameNS(WSConstants.ENC_NS,
"EncryptionMethod").item(0);
+ assertNotNull(encryptionMethod);
+ assertEquals(WSSConstants.NS_XENC11_AES128_GCM,
encryptionMethod.getAttributeNS(null, "Algorithm"));
+ encryptionMethod.setAttributeNS(null, "Algorithm",
WSSConstants.NS_XENC_AES128);
+
+ ByteArrayOutputStream tamperedBaos = new ByteArrayOutputStream();
+ TRANSFORMER_FACTORY.newTransformer().transform(new
DOMSource(securedDoc), new StreamResult(tamperedBaos));
+
+ attachmentCallbackHandler = new
AttachmentCallbackHandler(encryptedAttachments);
+ {
+ WSSSecurityProperties securityProperties = new
WSSSecurityProperties();
+
securityProperties.loadDecryptionKeystore(this.getClass().getClassLoader().getResource("receiver.jks"),
"default".toCharArray());
+ securityProperties.setCallbackHandler(new CallbackHandlerImpl());
+
securityProperties.setAttachmentCallbackHandler(attachmentCallbackHandler);
+
securityProperties.setEncryptionSymAlgorithm(WSSConstants.NS_XENC11_AES128_GCM);
+
+ // Return the underlying error rather than the masked
SECURITY_ERROR, so that the test
+ // shows the message was rejected by the algorithm check, and not
by some later failure
+ // (e.g. CBC padding) when decrypting the relabelled attachment
+ InboundWSSec wsSecIn = WSSec.getInboundWSSec(securityProperties,
false, true);
+ XMLStreamReader xmlStreamReader =
wsSecIn.processInMessage(xmlInputFactory.createXMLStreamReader(new
ByteArrayInputStream(tamperedBaos.toByteArray())));
+ try {
+ StAX2DOM.readDoc(documentBuilderFactory.newDocumentBuilder(),
xmlStreamReader);
+ fail("Exception expected on an attachment encrypted with an
algorithm other than the required one");
+ } catch (XMLStreamException e) {
+ assertTrue(e.getCause() instanceof WSSecurityException);
+ assertEquals(WSSecurityException.ErrorCode.INVALID_SECURITY,
+ ((WSSecurityException)
e.getCause()).getErrorCode());
+ }
+ }
+
+ // The attachment must not have been decrypted under the relabelled
algorithm
+
assertTrue(attachmentCallbackHandler.getResponseAttachments().isEmpty());
+ }
+
@Test
public void testInvalidXMLAttachmentContentEncryption() throws Exception {
final String attachmentId = UUID.randomUUID().toString();