View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.wss4j.stax.test;
20  
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.nio.charset.StandardCharsets;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import javax.xml.stream.XMLStreamReader;
29  import javax.xml.stream.XMLStreamWriter;
30  
31  import org.apache.wss4j.stax.ext.WSSConstants;
32  import org.apache.wss4j.stax.ext.WSSSecurityProperties;
33  import org.apache.wss4j.stax.setup.OutboundWSSec;
34  import org.apache.wss4j.stax.setup.WSSec;
35  import org.apache.wss4j.stax.test.utils.XmlReaderToWriter;
36  import org.apache.xml.security.stax.securityEvent.SecurityEvent;
37  import org.junit.jupiter.api.Test;
38  import org.w3c.dom.Document;
39  import org.w3c.dom.Node;
40  import org.w3c.dom.NodeList;
41  
42  import static org.junit.jupiter.api.Assertions.assertEquals;
43  import static org.junit.jupiter.api.Assertions.assertNotNull;
44  import static org.junit.jupiter.api.Assertions.fail;
45  
46  public class OutputChainTest extends AbstractTestBase {
47  
48      @Test
49      public void testEncryptionAction() throws Exception {
50          WSSSecurityProperties securityProperties = new WSSSecurityProperties();
51          List<WSSConstants.Action> actions = new ArrayList<>();
52          actions.add(WSSConstants.ENCRYPTION);
53          securityProperties.setActions(actions);
54          securityProperties.loadEncryptionKeystore(this.getClass().getClassLoader().getResource("receiver.jks"), "default".toCharArray());
55          securityProperties.setEncryptionUser("receiver");
56  
57          OutboundWSSec wsSecOut = WSSec.getOutboundWSSec(securityProperties);
58          ByteArrayOutputStream baos = new ByteArrayOutputStream();
59          XMLStreamWriter xmlStreamWriter = wsSecOut.processOutMessage(baos, StandardCharsets.UTF_8.name(), new ArrayList<SecurityEvent>());
60          XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(this.getClass().getClassLoader().getResourceAsStream("testdata/plain-soap-1.1.xml"));
61          XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
62          xmlStreamWriter.close();
63  
64          Document document = documentBuilderFactory.newDocumentBuilder().parse(new ByteArrayInputStream(baos.toByteArray()));
65          NodeList nodeList = document.getElementsByTagNameNS(WSSConstants.TAG_xenc_EncryptedKey.getNamespaceURI(), WSSConstants.TAG_xenc_EncryptedKey.getLocalPart());
66          assertEquals(nodeList.item(0).getParentNode().getLocalName(), WSSConstants.TAG_WSSE_SECURITY.getLocalPart());
67  
68          nodeList = document.getElementsByTagNameNS(WSSConstants.TAG_xenc_EncryptedData.getNamespaceURI(), WSSConstants.TAG_xenc_EncryptedData.getLocalPart());
69          assertEquals(nodeList.getLength(), 1);
70  
71          assertEquals(nodeList.item(0).getParentNode().getLocalName(), "Body");
72          NodeList childNodes = nodeList.item(0).getParentNode().getChildNodes();
73          for (int i = 0; i < childNodes.getLength(); i++) {
74              Node child = childNodes.item(i);
75              if (child.getNodeType() == Node.TEXT_NODE) {
76                  assertEquals(child.getTextContent().trim(), "");
77              } else if (child.getNodeType() == Node.ELEMENT_NODE) {
78                  assertEquals(child, nodeList.item(0));
79              } else {
80                  fail("Unexpected Node encountered");
81              }
82          }
83  
84          nodeList = document.getElementsByTagNameNS(WSSConstants.TAG_dsig_Signature.getNamespaceURI(), WSSConstants.TAG_dsig_Signature.getLocalPart());
85          assertEquals(nodeList.getLength(), 0);
86  
87          nodeList = document.getElementsByTagNameNS(WSSConstants.TAG_WSU_TIMESTAMP.getNamespaceURI(), WSSConstants.TAG_WSU_TIMESTAMP.getLocalPart());
88          assertEquals(nodeList.getLength(), 0);
89      }
90  
91      @Test
92      public void testSignatureAction() throws Exception {
93          WSSSecurityProperties securityProperties = new WSSSecurityProperties();
94          List<WSSConstants.Action> actions = new ArrayList<>();
95          actions.add(WSSConstants.SIGNATURE);
96          securityProperties.setActions(actions);
97          securityProperties.loadSignatureKeyStore(this.getClass().getClassLoader().getResource("receiver.jks"), "default".toCharArray());
98          securityProperties.setSignatureUser("receiver");
99          securityProperties.setCallbackHandler(new CallbackHandlerImpl());
100 
101         OutboundWSSec wsSecOut = WSSec.getOutboundWSSec(securityProperties);
102         ByteArrayOutputStream baos = new ByteArrayOutputStream();
103         XMLStreamWriter xmlStreamWriter = wsSecOut.processOutMessage(baos, StandardCharsets.UTF_8.name(), new ArrayList<SecurityEvent>());
104         XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(this.getClass().getClassLoader().getResourceAsStream("testdata/plain-soap-1.1.xml"));
105         XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
106         xmlStreamWriter.close();
107 
108         Document document = documentBuilderFactory.newDocumentBuilder().parse(new ByteArrayInputStream(baos.toByteArray()));
109         NodeList nodeList = document.getElementsByTagNameNS(WSSConstants.TAG_dsig_Signature.getNamespaceURI(), WSSConstants.TAG_dsig_Signature.getLocalPart());
110         assertEquals(nodeList.getLength(), 1);
111 
112         assertEquals(nodeList.item(0).getParentNode().getLocalName(), WSSConstants.TAG_WSSE_SECURITY.getLocalPart());
113 
114         nodeList = document.getElementsByTagNameNS(WSSConstants.NS_SOAP11, WSSConstants.TAG_SOAP_BODY_LN);
115         assertEquals(nodeList.getLength(), 1);
116 
117         Node attr = nodeList.item(0).getAttributes().getNamedItemNS(WSSConstants.ATT_WSU_ID.getNamespaceURI(), WSSConstants.ATT_WSU_ID.getLocalPart());
118         assertNotNull(attr);
119 
120         nodeList = document.getElementsByTagNameNS(WSSConstants.TAG_xenc_EncryptedData.getNamespaceURI(), WSSConstants.TAG_xenc_EncryptedData.getLocalPart());
121         assertEquals(nodeList.getLength(), 0);
122 
123         nodeList = document.getElementsByTagNameNS(WSSConstants.TAG_WSU_TIMESTAMP.getNamespaceURI(), WSSConstants.TAG_WSU_TIMESTAMP.getLocalPart());
124         assertEquals(nodeList.getLength(), 0);
125     }
126 
127     @Test
128     public void testTimeStampAction() throws Exception {
129         WSSSecurityProperties securityProperties = new WSSSecurityProperties();
130         List<WSSConstants.Action> actions = new ArrayList<>();
131         actions.add(WSSConstants.TIMESTAMP);
132         securityProperties.setActions(actions);
133 
134         OutboundWSSec wsSecOut = WSSec.getOutboundWSSec(securityProperties);
135         ByteArrayOutputStream baos = new ByteArrayOutputStream();
136         XMLStreamWriter xmlStreamWriter = wsSecOut.processOutMessage(baos, StandardCharsets.UTF_8.name(), new ArrayList<SecurityEvent>());
137         XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(this.getClass().getClassLoader().getResourceAsStream("testdata/plain-soap-1.1.xml"));
138         XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
139         xmlStreamWriter.close();
140 
141         Document document = documentBuilderFactory.newDocumentBuilder().parse(new ByteArrayInputStream(baos.toByteArray()));
142 
143         NodeList nodeList = document.getElementsByTagNameNS(WSSConstants.TAG_WSU_TIMESTAMP.getNamespaceURI(), WSSConstants.TAG_WSU_TIMESTAMP.getLocalPart());
144         assertEquals(nodeList.getLength(), 1);
145 
146         assertEquals(nodeList.item(0).getParentNode().getLocalName(), WSSConstants.TAG_WSSE_SECURITY.getLocalPart());
147 
148         nodeList = document.getElementsByTagNameNS(WSSConstants.TAG_xenc_EncryptedData.getNamespaceURI(), WSSConstants.TAG_xenc_EncryptedData.getLocalPart());
149         assertEquals(nodeList.getLength(), 0);
150 
151         nodeList = document.getElementsByTagNameNS(WSSConstants.TAG_dsig_Signature.getNamespaceURI(), WSSConstants.TAG_dsig_Signature.getLocalPart());
152         assertEquals(nodeList.getLength(), 0);
153     }
154 }