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.action;
21  
22  import org.apache.wss4j.dom.WSConstants;
23  import org.apache.wss4j.dom.engine.WSSecurityEngineResult;
24  import org.apache.wss4j.common.SecurityActionToken;
25  import org.apache.wss4j.common.SignatureActionToken;
26  import org.apache.wss4j.common.WSEncryptionPart;
27  import org.apache.wss4j.common.ext.WSSecurityException;
28  import org.apache.wss4j.dom.handler.RequestData;
29  import org.apache.wss4j.dom.handler.WSHandler;
30  import org.apache.wss4j.dom.handler.WSHandlerConstants;
31  import org.apache.wss4j.dom.handler.WSHandlerResult;
32  import org.apache.wss4j.dom.message.WSSecSignatureConfirmation;
33  
34  import java.util.List;
35  
36  public class SignatureConfirmationAction implements Action {
37      private static final org.slf4j.Logger LOG =
38          org.slf4j.LoggerFactory.getLogger(SignatureConfirmationAction.class);
39  
40      @SuppressWarnings("unchecked")
41      public void execute(WSHandler handler, SecurityActionToken actionToken, RequestData reqData)
42              throws WSSecurityException {
43          LOG.debug("Perform Signature confirmation");
44  
45          List<WSHandlerResult> results =
46              (List<WSHandlerResult>) handler.getProperty(
47                  reqData.getMsgContext(), WSHandlerConstants.RECV_RESULTS
48              );
49          if (results == null || results.isEmpty()) {
50              return;
51          }
52  
53          //
54          // prepare a SignatureConfirmation token
55          //
56          WSSecSignatureConfirmation wsc = new WSSecSignatureConfirmation(reqData.getSecHeader());
57          wsc.setIdAllocator(reqData.getWssConfig().getIdAllocator());
58          wsc.setWsDocInfo(reqData.getWsDocInfo());
59          wsc.setExpandXopInclude(reqData.isExpandXopInclude());
60          SignatureActionToken signatureToken = (SignatureActionToken)actionToken;
61          if (signatureToken == null) {
62              signatureToken = reqData.getSignatureToken();
63          }
64          List<WSEncryptionPart> signatureParts = signatureToken.getParts();
65  
66          //
67          // Loop over all the (signature) results gathered by all the processors
68          //
69          boolean signatureAdded = false;
70          for (WSHandlerResult wshResult : results) {
71              List<WSSecurityEngineResult> resultList = wshResult.getResults();
72  
73              for (WSSecurityEngineResult result : resultList) {
74                  Integer resultAction = (Integer) result.get(WSSecurityEngineResult.TAG_ACTION);
75  
76                  // See if it's a signature action
77                  if (resultAction != null
78                      && (WSConstants.SIGN == resultAction.intValue()
79                          || WSConstants.ST_SIGNED == resultAction.intValue()
80                          || WSConstants.UT_SIGN == resultAction.intValue())) {
81                      byte[] sigVal = (byte[]) result.get(WSSecurityEngineResult.TAG_SIGNATURE_VALUE);
82                      wsc.build(sigVal);
83                      signatureParts.add(new WSEncryptionPart(wsc.getId()));
84                      signatureAdded = true;
85                  }
86              }
87          }
88  
89          if (!signatureAdded) {
90              wsc.build(null);
91              signatureParts.add(new WSEncryptionPart(wsc.getId()));
92          }
93          handler.setProperty(
94              reqData.getMsgContext(), WSHandlerConstants.SIG_CONF_DONE, ""
95          );
96      }
97  
98  }