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.ArrayList;
23  import java.util.List;
24  
25  import javax.security.auth.callback.CallbackHandler;
26  
27  import org.apache.ws.security.SOAPConstants;
28  import org.apache.ws.security.WSConstants;
29  import org.apache.ws.security.WSEncryptionPart;
30  import org.apache.ws.security.WSPasswordCallback;
31  import org.apache.ws.security.WSSecurityException;
32  import org.apache.ws.security.handler.RequestData;
33  import org.apache.ws.security.handler.WSHandler;
34  import org.apache.ws.security.message.WSSecUsernameToken;
35  import org.apache.ws.security.message.WSSecSignature;
36  import org.apache.ws.security.util.WSSecurityUtil;
37  import org.w3c.dom.Document;
38  
39  /**
40   * Sign a request using a secret key derived from UsernameToken data.
41   * 
42   * Enhanced by Alberto Coletti to support digest password type for 
43   * username token signature
44   * 
45   * @author Werner Dittmann (Werner.Dittmann@t-online.de)
46   */
47  
48  public class UsernameTokenSignedAction implements Action {
49      public void execute(WSHandler handler, int actionToDo, Document doc, RequestData reqData)
50              throws WSSecurityException {
51          CallbackHandler callbackHandler = 
52              handler.getPasswordCallbackHandler(reqData);
53          WSPasswordCallback passwordCallback = 
54              handler.getPasswordCB(reqData.getUsername(), actionToDo, callbackHandler, reqData);
55  
56          WSSecUsernameToken builder = new WSSecUsernameToken(reqData.getWssConfig());
57          
58          if (reqData.isUseDerivedKey()) {
59              int iterations = reqData.getDerivedKeyIterations();
60              boolean useMac = reqData.isUseDerivedKeyForMAC();
61              builder.addDerivedKey(useMac, null, iterations);
62          } else {
63              builder.setPasswordType(reqData.getPwType());  // enhancement by Alberto Coletti
64              builder.setSecretKeyLength(reqData.getSecretKeyLength());
65          }
66          
67          builder.setUserInfo(reqData.getUsername(), passwordCallback.getPassword());
68          builder.addCreated();
69          builder.addNonce();
70          builder.prepare(doc);
71  
72          // Now prepare to sign.
73          // First step:  Get a WS Signature object and set config parameters
74          // second step: set user data and algorithm parameters. This
75          //              _must_ be done before we "prepare"
76          // third step:  Call "prepare". This creates the internal WS Signature
77          //              data structures, XML element, fills in the algorithms
78          //              and other data.
79          // fourth step: Get the references. These references identify the parts
80          //              of the document that will be included into the 
81          //              signature. If no references are given sign the message
82          //              body by default.
83          // fifth step:  compute the signature
84          //
85          // after "prepare" the Signature XML element is ready and may prepend
86          // this to the security header.
87          
88          WSSecSignature sign = new WSSecSignature(reqData.getWssConfig());
89          sign.setCustomTokenValueType(WSConstants.USERNAMETOKEN_NS + "#UsernameToken");
90          sign.setCustomTokenId(builder.getId());
91          sign.setSecretKey(builder.getSecretKey());
92          sign.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING);
93          if (reqData.getSigDigestAlgorithm() != null) {
94              sign.setDigestAlgo(reqData.getSigDigestAlgorithm());
95          }
96          
97          if (reqData.getSigAlgorithm() != null) {
98              sign.setSignatureAlgorithm(reqData.getSigAlgorithm());
99          } else {
100             sign.setSignatureAlgorithm(WSConstants.HMAC_SHA1);
101         }
102 
103         sign.prepare(doc, null, reqData.getSecHeader());
104 
105         // prepend in this order: first the Signature Element and then the
106         // UsernameToken Element. This way the server gets the UsernameToken
107         // first, can check it and are prepared to compute the Signature key.  
108         // sign.prependToHeader(reqData.getSecHeader());
109         // builder.prependToHeader(reqData.getSecHeader());
110 
111         List<WSEncryptionPart> parts = null;
112         if (reqData.getSignatureParts().size() > 0) {
113             parts = reqData.getSignatureParts();
114         } else {
115             SOAPConstants soapConstants = reqData.getSoapConstants();
116             if (soapConstants == null) {
117                 soapConstants = WSSecurityUtil.getSOAPConstants(doc.getDocumentElement());
118             }
119             parts = new ArrayList<WSEncryptionPart>();
120             WSEncryptionPart encP = 
121                 new WSEncryptionPart(WSConstants.ELEM_BODY, soapConstants.getEnvelopeURI(), "Content");
122             parts.add(encP);
123         }
124         List<javax.xml.crypto.dsig.Reference> referenceList = 
125             sign.addReferencesToSign(parts, reqData.getSecHeader());
126 
127         try {
128             sign.computeSignature(referenceList);
129             reqData.getSignatureValues().add(sign.getSignatureValue());
130         } catch (WSSecurityException e) {
131             throw new WSSecurityException(
132                 "WSHandler: Error during UsernameTokenSignature", e
133             );
134         }
135         builder.prependToHeader(reqData.getSecHeader());
136     }
137 }