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#THUMBPRINT_IDENTIFIER
90 * @see WSConstants#SKI_KEY_IDENTIFIER
91 * @see WSConstants#KEY_VALUE
92 */
93 public void setKeyIdentifierType(int keyIdType) {
94 keyIdentifierType = keyIdType;
95 }
96
97 /**
98 * Gets the value of the <code>keyIdentifierType</code>.
99 *
100 * @return The <code>keyIdentifyerType</code>.
101 * @see WSConstants#ISSUER_SERIAL
102 * @see WSConstants#BST_DIRECT_REFERENCE
103 * @see WSConstants#X509_KEY_IDENTIFIER
104 * @see WSConstants#SKI_KEY_IDENTIFIER
105 */
106 public int getKeyIdentifierType() {
107 return keyIdentifierType;
108 }
109
110 /**
111 * @param wsConfig
112 * The wsConfig to set.
113 */
114 public void setWsConfig(WSSConfig wsConfig) {
115 this.wssConfig = wsConfig;
116 }
117
118 public WSSConfig getWsConfig() {
119 if (wssConfig == null) {
120 wssConfig = WSSConfig.getNewInstance();
121 }
122 return wssConfig;
123 }
124
125
126 /**
127 * Looks up or adds a body id. <p/> First try to locate the
128 * <code>wsu:Id</code> in the SOAP body element. If one is found, the
129 * value of the <code>wsu:Id</code> attribute is returned. Otherwise the
130 * method generates a new <code>wsu:Id</code> and an appropriate value.
131 *
132 * @param doc The SOAP envelope as <code>Document</code>
133 * @return The value of the <code>wsu:Id</code> attribute of the SOAP body
134 * @throws Exception
135 */
136 protected String setBodyID(Document doc) throws Exception {
137 Element bodyElement = WSSecurityUtil.findBodyElement(doc);
138 if (bodyElement == null) {
139 throw new Exception("SOAP Body Element node not found");
140 }
141 return setWsuId(bodyElement);
142 }
143
144 protected String setWsuId(Element bodyElement) {
145 String id = bodyElement.getAttributeNS(WSConstants.WSU_NS, "Id");
146
147 String newAttrNs = WSConstants.WSU_NS;
148 String newAttrPrefix = WSConstants.WSU_PREFIX;
149
150 if ((id == null || id.length() == 0)
151 && WSConstants.ENC_NS.equals(bodyElement.getNamespaceURI())
152 && (WSConstants.ENC_DATA_LN.equals(bodyElement.getLocalName())
153 || WSConstants.ENC_KEY_LN.equals(bodyElement.getLocalName()))
154 ) {
155 // If it is an XML-Enc derived element, it may already have an ID,
156 // plus it is not schema valid to add an additional ID.
157 id = bodyElement.getAttributeNS(null, "Id");
158 newAttrPrefix = WSConstants.ENC_PREFIX;
159 newAttrNs = WSConstants.ENC_NS;
160 }
161
162 if ((id == null) || (id.length() == 0)) {
163 id = wssConfig.getIdAllocator().createId("id-", bodyElement);
164 String prefix =
165 WSSecurityUtil.setNamespace(bodyElement, newAttrNs, newAttrPrefix);
166 bodyElement.setAttributeNS(newAttrNs, prefix + ":Id", id);
167 }
168 return id;
169 }
170
171 /**
172 * Set the user and password info.
173 *
174 * Both information is used to get the user's private signing key.
175 *
176 * @param user
177 * This is the user's alias name in the keystore that identifies
178 * the private key to sign the document
179 * @param password
180 * The user's password to get the private signing key from the
181 * keystore
182 */
183 public void setUserInfo(String user, String password) {
184 this.user = user;
185 this.password = password;
186 }
187
188 }