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.ws.security.action;
21  
22  import java.util.List;
23  
24  import javax.security.auth.callback.CallbackHandler;
25  
26  import org.apache.ws.security.WSConstants;
27  import org.apache.ws.security.WSEncryptionPart;
28  import org.apache.ws.security.WSPasswordCallback;
29  import org.apache.ws.security.WSSecurityException;
30  import org.apache.ws.security.handler.RequestData;
31  import org.apache.ws.security.handler.WSHandler;
32  import org.apache.ws.security.message.WSSecSignature;
33  import org.apache.ws.security.util.WSSecurityUtil;
34  
35  import org.w3c.dom.Document;
36  import org.w3c.dom.Element;
37  import org.w3c.dom.Node;
38  
39  public class SignatureAction implements Action {
40      public void execute(WSHandler handler, int actionToDo, Document doc, RequestData reqData)
41              throws WSSecurityException {
42          CallbackHandler callbackHandler = 
43              handler.getPasswordCallbackHandler(reqData);
44          WSPasswordCallback passwordCallback = 
45              handler.getPasswordCB(reqData.getSignatureUser(), actionToDo, callbackHandler, reqData);
46          WSSecSignature wsSign = new WSSecSignature(reqData.getWssConfig());
47  
48          if (reqData.getSigKeyId() != 0) {
49              wsSign.setKeyIdentifierType(reqData.getSigKeyId());
50          }
51          if (reqData.getSigAlgorithm() != null) {
52              wsSign.setSignatureAlgorithm(reqData.getSigAlgorithm());
53          }
54          if (reqData.getSigDigestAlgorithm() != null) {
55              wsSign.setDigestAlgo(reqData.getSigDigestAlgorithm());
56          }
57  
58          wsSign.setUserInfo(reqData.getSignatureUser(), passwordCallback.getPassword());
59          wsSign.setUseSingleCertificate(reqData.isUseSingleCert());
60          if (reqData.getSignatureParts().size() > 0) {
61              wsSign.setParts(reqData.getSignatureParts());
62          }
63          
64          if (passwordCallback.getKey() != null) {
65              wsSign.setSecretKey(passwordCallback.getKey());
66          }
67  
68          try {
69              wsSign.prepare(doc, reqData.getSigCrypto(), reqData.getSecHeader());
70  
71              Element siblingElementToPrepend = null;
72              for (WSEncryptionPart part : reqData.getSignatureParts()) {
73                  if ("STRTransform".equals(part.getName()) && part.getId() == null) {
74                      part.setId(wsSign.getSecurityTokenReferenceURI());
75                  } else if (reqData.isAppendSignatureAfterTimestamp()
76                          && WSConstants.WSU_NS.equals(part.getNamespace()) 
77                          && "Timestamp".equals(part.getName())) {
78                      List<Element> elements = 
79                          WSSecurityUtil.findElements(
80                              doc.getDocumentElement(), part.getName(), part.getNamespace()
81                          );
82                      if (elements != null && !elements.isEmpty()) {
83                          Element timestampElement = elements.get(0);
84                          Node child = timestampElement.getNextSibling();
85                          while (child != null && child.getNodeType() != Node.ELEMENT_NODE) {
86                              child = child.getNextSibling();
87                          }
88                          siblingElementToPrepend = (Element)child;
89                      }
90                  }
91              }
92  
93              List<javax.xml.crypto.dsig.Reference> referenceList = 
94                  wsSign.addReferencesToSign(reqData.getSignatureParts(), reqData.getSecHeader());
95  
96              if (reqData.isAppendSignatureAfterTimestamp() && siblingElementToPrepend == null) {
97                  wsSign.computeSignature(referenceList, false, null);
98              } else {
99                  wsSign.computeSignature(referenceList, true, siblingElementToPrepend);
100             }
101 
102             wsSign.prependBSTElementToHeader(reqData.getSecHeader());
103             reqData.getSignatureValues().add(wsSign.getSignatureValue());
104         } catch (WSSecurityException e) {
105             throw new WSSecurityException("Error during Signature: ", e);
106         }
107     }
108 
109 }