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  
20  package org.apache.wss4j.dom.handler;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.wss4j.common.ext.WSSecurityException;
27  import org.apache.wss4j.common.util.SOAPUtil;
28  import org.apache.wss4j.common.util.XMLUtils;
29  import org.apache.wss4j.dom.WSConstants;
30  import org.apache.wss4j.dom.common.CustomHandler;
31  import org.apache.wss4j.dom.common.KeystoreCallbackHandler;
32  
33  import org.apache.wss4j.dom.engine.WSSConfig;
34  import org.apache.wss4j.dom.engine.WSSecurityEngine;
35  
36  import org.junit.jupiter.api.Test;
37  import org.w3c.dom.Document;
38  
39  
40  /**
41   * Some tests for WSHandlerConstants.USE_REQ_SIG_CERT - the user signature cert is used to
42   * encrypt the response.
43   */
44  public class UseReqSigCertTest {
45      private static final org.slf4j.Logger LOG =
46          org.slf4j.LoggerFactory.getLogger(UseReqSigCertTest.class);
47  
48      public UseReqSigCertTest() throws Exception {
49          WSSConfig.init();
50      }
51  
52      @Test
53      public void testIncludedCertificate() throws Exception {
54          final WSSConfig cfg = WSSConfig.getNewInstance();
55          final RequestData reqData = new RequestData();
56          reqData.setWssConfig(cfg);
57          reqData.setUsername("wss40");
58  
59          java.util.Map<String, Object> config = new java.util.TreeMap<>();
60          config.put(WSHandlerConstants.SIG_PROP_FILE, "wss40.properties");
61          config.put(WSHandlerConstants.SIG_KEY_ID, "DirectReference");
62          config.put(WSHandlerConstants.PW_CALLBACK_REF, new KeystoreCallbackHandler());
63          config.put(
64              WSHandlerConstants.SIGNATURE_PARTS, "{}{" + WSConstants.WSU_NS + "}Timestamp"
65          );
66          reqData.setMsgContext(config);
67  
68          final Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
69  
70          // Send the request
71          CustomHandler handler = new CustomHandler();
72          List<HandlerAction> actions = new ArrayList<>();
73          actions.add(new HandlerAction(WSConstants.SIGN));
74          actions.add(new HandlerAction(WSConstants.TS));
75          handler.send(
76              doc,
77              reqData,
78              actions,
79              true
80          );
81          String outputString =
82              XMLUtils.prettyDocumentToString(doc);
83          if (LOG.isDebugEnabled()) {
84              LOG.debug("Signed message:");
85              LOG.debug(outputString);
86          }
87  
88          // Process the request
89          WSHandlerResult results = processRequest(doc);
90          List<WSHandlerResult> handlerResults = new ArrayList<>();
91          handlerResults.add(0, results);
92  
93          // Send the response
94          sendResponse(handlerResults);
95      }
96  
97      @Test
98      public void testIssuerSerial() throws Exception {
99          final WSSConfig cfg = WSSConfig.getNewInstance();
100         final RequestData reqData = new RequestData();
101         reqData.setWssConfig(cfg);
102         reqData.setUsername("wss40");
103 
104         java.util.Map<String, Object> config = new java.util.TreeMap<>();
105         config.put(WSHandlerConstants.SIG_PROP_FILE, "wss40.properties");
106         config.put(WSHandlerConstants.SIG_KEY_ID, "IssuerSerial");
107         config.put(WSHandlerConstants.PW_CALLBACK_REF, new KeystoreCallbackHandler());
108         config.put(
109             WSHandlerConstants.SIGNATURE_PARTS, "{}{" + WSConstants.WSU_NS + "}Timestamp"
110         );
111         reqData.setMsgContext(config);
112 
113         final Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
114 
115         // Send the request
116         CustomHandler handler = new CustomHandler();
117         List<HandlerAction> actions = new ArrayList<>();
118         actions.add(new HandlerAction(WSConstants.SIGN));
119         actions.add(new HandlerAction(WSConstants.TS));
120         handler.send(
121             doc,
122             reqData,
123             actions,
124             true
125         );
126         String outputString =
127             XMLUtils.prettyDocumentToString(doc);
128         if (LOG.isDebugEnabled()) {
129             LOG.debug("Signed message:");
130             LOG.debug(outputString);
131         }
132 
133         // Process the request
134         WSHandlerResult results = processRequest(doc);
135         List<WSHandlerResult> handlerResults = new ArrayList<>();
136         handlerResults.add(0, results);
137 
138         // Send the response
139         sendResponse(handlerResults);
140     }
141 
142     @Test
143     public void testSKIKeyIdentifier() throws Exception {
144         final WSSConfig cfg = WSSConfig.getNewInstance();
145         final RequestData reqData = new RequestData();
146         reqData.setWssConfig(cfg);
147         reqData.setUsername("wss40");
148 
149         java.util.Map<String, Object> config = new java.util.TreeMap<>();
150         config.put(WSHandlerConstants.SIG_PROP_FILE, "wss40.properties");
151         config.put(WSHandlerConstants.SIG_KEY_ID, "SKIKeyIdentifier");
152         config.put(WSHandlerConstants.PW_CALLBACK_REF, new KeystoreCallbackHandler());
153         config.put(
154             WSHandlerConstants.SIGNATURE_PARTS, "{}{" + WSConstants.WSU_NS + "}Timestamp"
155         );
156         reqData.setMsgContext(config);
157 
158         final Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
159 
160         // Send the request
161         CustomHandler handler = new CustomHandler();
162         List<HandlerAction> actions = new ArrayList<>();
163         actions.add(new HandlerAction(WSConstants.SIGN));
164         actions.add(new HandlerAction(WSConstants.TS));
165         handler.send(
166             doc,
167             reqData,
168             actions,
169             true
170         );
171         String outputString =
172             XMLUtils.prettyDocumentToString(doc);
173         if (LOG.isDebugEnabled()) {
174             LOG.debug("Signed message:");
175             LOG.debug(outputString);
176         }
177 
178         // Process the request
179         WSHandlerResult results = processRequest(doc);
180         List<WSHandlerResult> handlerResults = new ArrayList<>();
181         handlerResults.add(0, results);
182 
183         // Send the response
184         sendResponse(handlerResults);
185     }
186 
187     private WSHandlerResult processRequest(Document doc) throws WSSecurityException {
188         final WSSConfig cfg = WSSConfig.getNewInstance();
189         final RequestData reqData = new RequestData();
190         reqData.setWssConfig(cfg);
191 
192         java.util.Map<String, Object> config = new java.util.TreeMap<>();
193         config.put(WSHandlerConstants.SIG_VER_PROP_FILE, "wss40.properties");
194         reqData.setMsgContext(config);
195 
196         CustomHandler handler = new CustomHandler();
197         List<Integer> receivedActions = new ArrayList<>();
198         receivedActions.add(WSConstants.SIGN);
199         receivedActions.add(WSConstants.TS);
200         handler.receive(receivedActions, reqData);
201 
202         WSSecurityEngine securityEngine = new WSSecurityEngine();
203         return securityEngine.processSecurityHeader(doc, reqData);
204     }
205 
206     private void sendResponse(List<WSHandlerResult> handlerResults) throws Exception {
207         final RequestData reqData = new RequestData();
208 
209         java.util.Map<String, Object> config = new java.util.TreeMap<>();
210         config.put(WSHandlerConstants.ENCRYPTION_USER, "useReqSigCert");
211         config.put(WSHandlerConstants.RECV_RESULTS, handlerResults);
212         reqData.setMsgContext(config);
213 
214         final List<Integer> actions = new ArrayList<>();
215         actions.add(WSConstants.ENCR);
216         final Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
217 
218         // Send message
219         CustomHandler handler = new CustomHandler();
220         HandlerAction action = new HandlerAction(WSConstants.ENCR);
221         handler.send(
222             doc,
223             reqData,
224             Collections.singletonList(action),
225             true
226         );
227     }
228 
229 }