View Javadoc

1   /*
2    * Copyright 2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.security.wss4j;
18  
19  import java.io.IOException;
20  import java.security.cert.X509Certificate;
21  import java.util.Properties;
22  import java.util.Vector;
23  import javax.security.auth.callback.Callback;
24  import javax.security.auth.callback.CallbackHandler;
25  import javax.security.auth.callback.UnsupportedCallbackException;
26  
27  import org.apache.ws.security.WSConstants;
28  import org.apache.ws.security.WSPasswordCallback;
29  import org.apache.ws.security.WSSecurityException;
30  import org.apache.ws.security.components.crypto.Crypto;
31  import org.apache.ws.security.handler.RequestData;
32  import org.apache.ws.security.handler.WSHandler;
33  import org.apache.ws.security.handler.WSHandlerConstants;
34  import org.apache.ws.security.message.token.Timestamp;
35  import org.w3c.dom.Document;
36  
37  import org.springframework.ws.context.MessageContext;
38  
39  /**
40   * @author Tareq Abed Rabbo
41   * @author Arjen Poutsma
42   * @since 1.5.0
43   */
44  class Wss4jHandler extends WSHandler {
45  
46      /** Keys are constants from {@link WSHandlerConstants}; values are strings. */
47      private Properties options = new Properties();
48  
49      private CallbackHandler securementCallbackHandler;
50  
51      private String securementPassword;
52  
53      private Crypto securementEncryptionCrypto;
54  
55      private Crypto securementSignatureCrypto;
56  
57      Wss4jHandler() {
58          // set up default handler properties
59          options.setProperty(WSHandlerConstants.MUST_UNDERSTAND, Boolean.toString(true));
60          options.setProperty(WSHandlerConstants.ENABLE_SIGNATURE_CONFIRMATION, Boolean.toString(true));
61      }
62  
63      protected boolean checkReceiverResults(Vector wsResult, Vector actions) {
64          return super.checkReceiverResults(wsResult, actions);
65      }
66  
67      protected boolean checkReceiverResultsAnyOrder(Vector wsResult, Vector actions) {
68          return super.checkReceiverResultsAnyOrder(wsResult, actions);
69      }
70  
71      void setOption(String key, String value) {
72          options.setProperty(key, value);
73      }
74  
75      void setOption(String key, boolean value) {
76          options.setProperty(key, Boolean.toString(value));
77      }
78  
79      public Object getOption(String key) {
80          return options.getProperty(key);
81      }
82  
83      void setSecurementCallbackHandler(CallbackHandler securementCallbackHandler) {
84          this.securementCallbackHandler = securementCallbackHandler;
85      }
86  
87      void setSecurementPassword(String securementPassword) {
88          this.securementPassword = securementPassword;
89      }
90  
91      void setSecurementEncryptionCrypto(Crypto securementEncryptionCrypto) {
92          this.securementEncryptionCrypto = securementEncryptionCrypto;
93      }
94  
95      void setSecurementSignatureCrypto(Crypto securementSignatureCrypto) {
96          this.securementSignatureCrypto = securementSignatureCrypto;
97      }
98  
99      /** Gets the password first from securementCallbackHandler, then from securementPassword if not found. */
100     public WSPasswordCallback getPassword(String username,
101                                           int doAction,
102                                           String clsProp,
103                                           String refProp,
104                                           RequestData reqData) {
105         WSPasswordCallback callback;
106         if (securementCallbackHandler != null) {
107             int reason = 0;
108 
109             switch (doAction) {
110                 case WSConstants.UT:
111                 case WSConstants.UT_SIGN:
112                     reason = WSPasswordCallback.USERNAME_TOKEN;
113                     break;
114                 case WSConstants.SIGN:
115                     reason = WSPasswordCallback.SIGNATURE;
116                     break;
117                 case WSConstants.ENCR:
118                     reason = WSPasswordCallback.KEY_NAME;
119                     break;
120             }
121             callback = new WSPasswordCallback(username, reason);
122             Callback[] callbacks = new Callback[]{callback};
123             try {
124                 securementCallbackHandler.handle(callbacks);
125             }
126             catch (UnsupportedCallbackException ex) {
127                 throw new Wss4jSecuritySecurementException(ex.getMessage(), ex);
128             }
129             catch (IOException ex) {
130                 throw new Wss4jSecuritySecurementException(ex.getMessage(), ex);
131             }
132         }
133         else {
134             callback = new WSPasswordCallback(username, WSPasswordCallback.UNKNOWN);
135             callback.setPassword(securementPassword);
136         }
137         return callback;
138     }
139 
140     public String getPassword(Object msgContext) {
141         return securementPassword;
142     }
143 
144     public Object getProperty(Object msgContext, String key) {
145         return ((MessageContext) msgContext).getProperty(key);
146     }
147 
148     protected Crypto loadEncryptionCrypto(RequestData reqData) throws WSSecurityException {
149         return securementEncryptionCrypto;
150     }
151 
152     public Crypto loadSignatureCrypto(RequestData reqData) throws WSSecurityException {
153         return securementSignatureCrypto;
154     }
155 
156     public void setPassword(Object msgContext, String password) {
157         securementPassword = password;
158     }
159 
160     public void setProperty(Object msgContext, String key, Object value) {
161         ((MessageContext) msgContext).setProperty(key, value);
162     }
163 
164     protected void doSenderAction(int doAction, Document doc, RequestData reqData, Vector actions, boolean isRequest)
165             throws WSSecurityException {
166         super.doSenderAction(doAction, doc, reqData, actions, isRequest);
167     }
168 
169     protected boolean verifyTimestamp(Timestamp timestamp, int timeToLive) throws WSSecurityException {
170         return super.verifyTimestamp(timestamp, timeToLive);
171     }
172 
173     protected boolean verifyTrust(X509Certificate cert, RequestData reqData) throws WSSecurityException {
174         return super.verifyTrust(cert, reqData);
175     }
176 }