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.message;
21  
22  import org.apache.ws.security.WSConstants;
23  import org.apache.ws.security.WSEncryptionPart;
24  import org.apache.ws.security.WSSConfig;
25  import org.apache.ws.security.util.WSSecurityUtil;
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Element;
28  
29  import java.util.List;
30  
31  /**
32   * This is the base class for WS Security messages. It provides common functions
33   * and fields used by the specific message classes such as sign, encrypt, and
34   * username token.
35   * 
36   * @author Werner Dittmann (Werner.Dittmann@apache.org)
37   */
38  public class WSSecBase {
39      protected String user = null;
40  
41      protected String password = null;
42  
43      protected int keyIdentifierType = WSConstants.ISSUER_SERIAL;
44  
45      protected List<WSEncryptionPart> parts = null;
46  
47      protected boolean doDebug = false;
48      
49      protected CallbackLookup callbackLookup;
50  
51      private WSSConfig wssConfig;
52      
53      public WSSecBase() {
54      }
55      public WSSecBase(WSSConfig config) {
56          wssConfig = config;
57      }
58  
59      /**
60       * @param callbackLookup The CallbackLookup object to retrieve elements
61       */
62      public void setCallbackLookup(CallbackLookup callbackLookup) {
63          this.callbackLookup = callbackLookup;
64      }
65      
66      /**
67       * Set which parts of the message to encrypt/sign. <p/>
68       * 
69       * @param parts The list containing the WSEncryptionPart objects
70       */
71      public void setParts(List<WSEncryptionPart> parts) {
72          this.parts = parts;
73      }
74  
75      /**
76       * Sets which key identifier to use. 
77       * 
78       * <p/> 
79       * 
80       * Defines the key identifier type to
81       * use in the {@link WSSecSignature#prepare(Document, Crypto, WSSecHeader) method} or
82       * the {@link WSSecEncrypt#prepare(Document, Crypto) method} function to
83       * set up the key identification elements.
84       * 
85       * @param keyIdType
86       * @see WSConstants#ISSUER_SERIAL
87       * @see WSConstants#BST_DIRECT_REFERENCE
88       * @see WSConstants#X509_KEY_IDENTIFIER
89       * @see WSConstants#SKI_KEY_IDENTIFIER
90       */
91      public void setKeyIdentifierType(int keyIdType) {
92          keyIdentifierType = keyIdType;
93      }
94  
95      /**
96       * Gets the value of the <code>keyIdentifierType</code>.
97       * 
98       * @return The <code>keyIdentifyerType</code>.
99       * @see WSConstants#ISSUER_SERIAL
100      * @see WSConstants#BST_DIRECT_REFERENCE
101      * @see WSConstants#X509_KEY_IDENTIFIER
102      * @see WSConstants#SKI_KEY_IDENTIFIER
103      */
104     public int getKeyIdentifierType() {
105         return keyIdentifierType;
106     }
107 
108     /**
109      * @param wsConfig
110      *            The wsConfig to set.
111      */
112     public void setWsConfig(WSSConfig wsConfig) {
113         this.wssConfig = wsConfig;
114     }
115     
116     public WSSConfig getWsConfig() {
117         if (wssConfig == null) {
118             wssConfig = WSSConfig.getNewInstance();
119         }
120         return wssConfig;
121     }
122      
123 
124     /**
125      * Looks up or adds a body id. <p/> First try to locate the
126      * <code>wsu:Id</code> in the SOAP body element. If one is found, the
127      * value of the <code>wsu:Id</code> attribute is returned. Otherwise the
128      * method generates a new <code>wsu:Id</code> and an appropriate value.
129      * 
130      * @param doc The SOAP envelope as <code>Document</code>
131      * @return The value of the <code>wsu:Id</code> attribute of the SOAP body
132      * @throws Exception
133      */
134     protected String setBodyID(Document doc) throws Exception {
135         Element bodyElement = WSSecurityUtil.findBodyElement(doc);
136         if (bodyElement == null) {
137             throw new Exception("SOAP Body Element node not found");
138         }
139         return setWsuId(bodyElement);
140     }
141 
142     protected String setWsuId(Element bodyElement) {
143         String id = bodyElement.getAttributeNS(WSConstants.WSU_NS, "Id");
144 
145         String newAttrNs = WSConstants.WSU_NS;
146         String newAttrPrefix = WSConstants.WSU_PREFIX;
147 
148         if ((id == null || id.length() == 0)
149             && WSConstants.ENC_NS.equals(bodyElement.getNamespaceURI())
150             && (WSConstants.ENC_DATA_LN.equals(bodyElement.getLocalName())
151                 || WSConstants.ENC_KEY_LN.equals(bodyElement.getLocalName()))
152         ) {
153             // If it is an XML-Enc derived element, it may already have an ID,
154             // plus it is not schema valid to add an additional ID.
155             id = bodyElement.getAttribute("Id");
156             newAttrPrefix = WSConstants.ENC_PREFIX;
157             newAttrNs = WSConstants.ENC_NS;
158         }
159         
160         if ((id == null) || (id.length() == 0)) {
161             id = wssConfig.getIdAllocator().createId("id-", bodyElement);
162             String prefix = 
163                 WSSecurityUtil.setNamespace(bodyElement, newAttrNs, newAttrPrefix);
164             bodyElement.setAttributeNS(newAttrNs, prefix + ":Id", id);
165         }
166         return id;
167     }
168     
169     /**
170      * Set the user and password info. 
171      * 
172      * Both information is used to get the user's private signing key.
173      * 
174      * @param user
175      *            This is the user's alias name in the keystore that identifies
176      *            the private key to sign the document
177      * @param password
178      *            The user's password to get the private signing key from the
179      *            keystore
180      */
181     public void setUserInfo(String user, String password) {
182         this.user = user;
183         this.password = password;
184     }
185     
186 }