View Javadoc

1   /*
2    * Copyright  2003-2004 The Apache Software Foundation.
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  
18  package org.apache.ws.security;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.ws.security.transform.STRTransform;
23  import org.apache.ws.security.util.Loader;
24  import org.apache.xml.security.transforms.Transform;
25  
26  /***
27   * WSSConfig
28   * <p/>
29   * Carries configuration data so the WSS4J spec compliance can be modified in
30   * runtime. Configure an instance of this object only if you need WSS4J to
31   * emulate certain industry clients or previous OASIS specifications for
32   * WS-Security interoperability testing purposes.
33   * <p/>
34   * The default settings follow the latest OASIS and changing anything might
35   * violate the OASIS specs.
36   * <p/>
37   * <b>WARNING: changing the default settings will break the compliance with the
38   * latest specs.  Do this only if you know what you are doing.</b>
39   * <p/>
40   *
41   * @author Rami Jaamour (rjaamour@parasoft.com)
42   */
43  public class WSSConfig {
44      private static Log log = LogFactory.getLog(WSSConfig.class.getName());
45      protected static WSSConfig defaultConfig = getNewInstance();
46      protected String wsse_ns = WSConstants.WSSE_NS_OASIS_1_0;
47      protected String wsu_ns = WSConstants.WSU_NS_OASIS_1_0;
48      protected boolean qualifyBSTAttributes = false;
49      protected boolean prefixBSTValues = false;
50      protected boolean targetIdQualified = true;
51      protected boolean wsiBSPCompliant = false;
52      protected boolean processNonCompliantMessages = true;
53      public static final int TIMESTAMP_IN_SECURITY_ELEMENT = 1;
54      public static final int TIMESTAMP_IN_HEADER_ELEMENT = 2;
55      protected int timestampLocation = TIMESTAMP_IN_SECURITY_ELEMENT;
56  
57      /***
58       * Set the timestamp precision mode.
59       * If set to <code>true</code> then use timestamps with milliseconds,
60       * otherwise omit the millisconds. As per XML Date/Time specification
61       * the defualt is to include the milliseconds.
62       */
63      protected boolean precisionInMilliSeconds = true;
64  
65      protected WSSConfig() {
66          org.apache.xml.security.Init.init();
67          try {
68              Class c = Loader.loadClass("org.bouncycastle.jce.provider.BouncyCastleProvider");
69              String Id = "BC";
70              if (java.security.Security.getProvider(Id) == null) {
71                  log.debug("The provider " + Id
72                          + " had to be added to the java.security.Security");
73                  java.security.Security.addProvider((java.security.Provider)c.newInstance());
74              }
75          } catch (Throwable t) {
76          }
77          Transform.init();
78          try {
79              Transform.register(STRTransform.implementedTransformURI,
80                      "org.apache.ws.security.transform.STRTransform");
81          } catch (Exception ex) {
82          }
83      }
84  
85      /***
86       * @return a new WSSConfig instance configured with the default values
87       *         (values identical to {@link #getDefaultWSConfig getDefaultWSConfig()})
88       */
89      public static WSSConfig getNewInstance() {
90          WSSConfig config = new WSSConfig();
91          return config;
92      }
93  
94      /***
95       * returns a static WSConfig instance that is configured with the latest
96       * OASIS WS-Seurity settings.
97       */
98      public static WSSConfig getDefaultWSConfig() {
99          return defaultConfig;
100     }
101 
102     /***
103      * default value is {@link WSConstants.WSSE_NS_OASIS_1_0}
104      * <p/>
105      * The WS-Security namespace
106      */
107     public String getWsseNS() {
108         return wsse_ns;
109     }
110 
111     /***
112      * Valid values:
113      * <ul>
114      * <li> {@link WSConstants#WSSE_NS_OASIS_2002_07} </li>
115      * <li> {@link WSConstants#WSSE_NS_OASIS_2002_12} </li>
116      * <li> {@link WSConstants#WSSE_NS_OASIS_2003_06} </li>
117      * <li> {@link WSConstants#WSSE_NS_OASIS_1_0} OASIS WS-Security v1.0 (March 2004). This is the default and recommended setting</li>
118      * </ul>
119      */
120     public void setWsseNS(String wsseNamespace) {
121         wsse_ns = wsseNamespace;
122     }
123 
124     /***
125      * default value is {@link WSConstants.WSU_NS_OASIS_1_0}
126      * <p/>
127      * The WS-Security utility namespace
128      */
129     public String getWsuNS() {
130         return wsu_ns;
131     }
132 
133     /***
134      * Valid values:
135      * <ul>
136      * <li> {@link WSConstants#WSU_NS_OASIS_2002_07} </li>
137      * <li> {@link WSConstants#WSU_NS_OASIS_2002_12} </li>
138      * <li> {@link WSConstants#WSU_NS_OASIS_2003_06} </li>
139      * <li> {@link WSConstants#WSU_NS_OASIS_1_0} OASIS WS-Security v1.0 (March 2004). This is the default and recommended setting</li>
140      * </ul>
141      */
142     public void setWsuNS(String wsuNamespace) {
143         wsu_ns = wsuNamespace;
144     }
145 
146     /***
147      * default value is false.
148      * <p/>
149      * returns true if the BinarySecurityToken EncodingType and ValueType
150      * attributes should be namespace qualified.
151      */
152     public boolean isBSTAttributesQualified() {
153         return qualifyBSTAttributes;
154     }
155 
156     /***
157      * specify if the BinarySecurityToken EncodingType and ValueType
158      * attributes should be namespace qualified. The default value is false.
159      */
160     public void setBSTAttributesQualified(boolean qualifyBSTAttributes) {
161         this.qualifyBSTAttributes = qualifyBSTAttributes;
162     }
163 
164     /***
165      * default value is false.
166      * <p/>
167      * returns true if the BinarySecurityToken EncodingType and ValueType
168      * attribute values should be prefixed with "wsse" or otherwise qualified
169      * with the wsse namespace (false).
170      */
171     public boolean isBSTValuesPrefixed() {
172         return prefixBSTValues;
173     }
174 
175     /***
176      * sets and option whether the BinarySecurityToken EncodingType and ValueType
177      * attribute values should be prefixed with "wsse" or otherwise qualified
178      * with the wsse namespace (false).
179      */
180     public void setBSTValuesPrefixed(boolean prefixBSTAttributeValues) {
181         prefixBSTValues = prefixBSTAttributeValues;
182     }
183 
184     /***
185      * default value is true.
186      * <p/>
187      * returns true if the Id attribute placed in the signature target element is
188      * qualified with the wsu namespace.
189      */
190     public boolean isTargetIdQualified() {
191         return targetIdQualified;
192     }
193 
194     /***
195      * Sets an option whether the Id attribute placed in the signature target should be
196      * qualified with the wsu namespace.
197      */
198     public void setTargetIdQualified(boolean qualifyTargetIdAttribute) {
199         targetIdQualified = qualifyTargetIdAttribute;
200     }
201 
202     /***
203      * default value is TIMESTAMP_IN_SECURITY_ELEMENT (following OASIS 2003 and 2004 specs).
204      * <p/>
205      * returns TIMESTAMP_IN_SECURITY_ELEMENT if the wsu:Timestamp element is placed inside
206      * the wsse:Secutriy element. TIMESTAMP_IN_HEADER_ELEMENT if it is placed under the Header directly, outside
207      * the wsse:Secutriy element.
208      */
209     public int getTimestampLocation() {
210         return timestampLocation;
211     }
212 
213     /***
214      * Sets an option whether the Iwsu:Timestamp element is placed inside
215      * the wsse:Secutriy element. set it to false foe placement in the Header,
216      * outside the wsse:Secutriy element.
217      */
218     public void setTimestampLocation(int timestampElementLocation) {
219         timestampLocation = timestampElementLocation;
220     }
221 
222     /***
223      * default value is true.
224      * <p/>
225      * returns true if WSS4J attempts to process non-compliant WS-Security
226      * messages, such as WS-Security headers with older OASIS spec namespaces.
227      */
228     public boolean getProcessNonCompliantMessages() {
229         return processNonCompliantMessages;
230     }
231 
232     /***
233      * Sets an option whether WSS4J should attempt to process non-compliant
234      * WS-Security messages, such as WS-Security headers with older OASIS spec
235      * namespaces.
236      */
237     public void setProcessNonCompliantMessages(boolean attemptProcess) {
238         processNonCompliantMessages = attemptProcess;
239     }
240 
241     /***
242      * Checks if we are in WS-I Basic Security Profile compliance mode
243      *
244      * @return
245      */
246     public boolean isWsiBSPCompliant() {
247         return wsiBSPCompliant;
248     }
249 
250     /***
251      * Set the WS-I Basic Security Profile compliance mode. The default is
252      * false (dues to .Net interop problems).
253      *
254      * @param wsiBSPCompliant
255      */
256     public void setWsiBSPCompliant(boolean wsiBSPCompliant) {
257         this.wsiBSPCompliant = wsiBSPCompliant;
258     }
259 
260     /***
261      * Checks if we need to use milliseconds in timestamps
262      *
263      * @return
264      */
265     public boolean isPrecisionInMilliSeconds() {
266         return precisionInMilliSeconds;
267     }
268 
269     /***
270      * Set the precision in milliseconds
271      *
272      * @param wsiBSPCompliant
273      */
274     public void setPrecisionInMilliSeconds(boolean precisionInMilliSeconds) {
275         this.precisionInMilliSeconds = precisionInMilliSeconds;
276     }
277 }