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.handler;
21  
22  import java.io.IOException;
23  import java.util.List;
24  
25  import javax.security.auth.callback.Callback;
26  import javax.security.auth.callback.CallbackHandler;
27  import javax.security.auth.callback.UnsupportedCallbackException;
28  
29  import org.apache.ws.security.WSConstants;
30  import org.apache.ws.security.WSPasswordCallback;
31  import org.apache.ws.security.WSSConfig;
32  import org.apache.ws.security.WSSecurityEngine;
33  import org.apache.ws.security.WSSecurityEngineResult;
34  import org.apache.ws.security.common.CustomHandler;
35  import org.apache.ws.security.common.SOAPUtil;
36  import org.apache.ws.security.components.crypto.CryptoFactory;
37  import org.w3c.dom.Document;
38  
39  
40  /**
41   * This is a test for WSS-194 - "Support overriding KeyStore alias for signature so that it can
42   * be different than user name used for UsernameToken".
43   */
44  public class SignatureUTAliasTest extends org.junit.Assert implements CallbackHandler {
45      private static final org.apache.commons.logging.Log LOG = 
46          org.apache.commons.logging.LogFactory.getLog(SignatureUTAliasTest.class);
47      private WSSecurityEngine secEngine = new WSSecurityEngine();
48  
49      /**
50       * Test involving adding a Username Token to a SOAP message and signing it, where the
51       * private key for signature is extracted from the KeyStore using a different username/alias
52       * to the UsernameToken. 
53       */
54      @org.junit.Test
55      public void 
56      testUsernameTokenSignatureHandler() throws Exception {
57          final WSSConfig cfg = WSSConfig.getNewInstance();
58          final RequestData reqData = new RequestData();
59          reqData.setWssConfig(cfg);
60          reqData.setUsername("alice");
61          reqData.setPwType(WSConstants.PASSWORD_TEXT);
62          java.util.Map<String, Object> messageContext = new java.util.TreeMap<String, Object>();
63          messageContext.put(
64              WSHandlerConstants.PW_CALLBACK_REF, 
65              this
66          );
67          messageContext.put(WSHandlerConstants.SIGNATURE_USER, "wss40");
68          messageContext.put(WSHandlerConstants.SIG_PROP_FILE, "wss40.properties");
69          messageContext.put(
70              WSHandlerConstants.SIGNATURE_PARTS, 
71              "{}{" + WSConstants.WSSE_NS + "}" + "UsernameToken"
72          );
73          messageContext.put(WSHandlerConstants.SIG_KEY_ID, "DirectReference");
74          reqData.setMsgContext(messageContext);
75          
76          final java.util.List<Integer> actions = new java.util.ArrayList<Integer>();
77          actions.add(Integer.valueOf(WSConstants.UT));
78          actions.add(Integer.valueOf(WSConstants.SIGN));
79          final Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
80          CustomHandler handler = new CustomHandler();
81          handler.send(
82              WSConstants.UT | WSConstants.SIGN, 
83              doc, 
84              reqData, 
85              actions,
86              true
87          );
88          
89          if (LOG.isDebugEnabled()) {
90              LOG.debug("After Signing....");
91              String outputString = 
92                  org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
93              LOG.debug(outputString);
94          }
95          
96          verify(doc);
97          
98      }
99      
100 
101     /**
102      * Verifies the soap envelope
103      * <p/>
104      * 
105      * @param doc 
106      * @throws Exception Thrown when there is a problem in verification
107      */
108     private List<WSSecurityEngineResult> verify(Document doc) throws Exception {
109         List<WSSecurityEngineResult> results = 
110             secEngine.processSecurityHeader(
111                 doc, null, this, CryptoFactory.getInstance("wss40CA.properties")
112             );
113         if (LOG.isDebugEnabled()) {
114             LOG.debug("Verfied and decrypted message:");
115             String outputString = 
116                 org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
117             LOG.debug(outputString);
118         }
119         return results;
120     }
121 
122     
123     public void handle(Callback[] callbacks)
124         throws IOException, UnsupportedCallbackException {
125         for (int i = 0; i < callbacks.length; i++) {
126             if (callbacks[i] instanceof WSPasswordCallback) {
127                 WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
128                 if (pc.getUsage() == WSPasswordCallback.USERNAME_TOKEN
129                         && "alice".equals(pc.getIdentifier())) {
130                     pc.setPassword("verySecret");
131                 } else if (pc.getUsage() == WSPasswordCallback.SIGNATURE
132                         && "wss40".equals(pc.getIdentifier())) {
133                     pc.setPassword("security");
134                 } else {
135                     throw new IOException("Authentication failed");
136                 }
137             } else {
138                 throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
139             }
140         }
141     }
142 
143     
144 }