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.token;
21
22 import java.util.Arrays;
23
24 import org.apache.ws.security.WSConstants;
25 import org.apache.ws.security.WSSecurityException;
26 import org.apache.ws.security.util.DOM2Writer;
27 import org.apache.ws.security.util.WSSecurityUtil;
28 import org.apache.ws.security.util.Base64;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31 import org.w3c.dom.Node;
32
33
34 /**
35 * Signature Confirmation element.
36 *
37 *
38 * @author Werner Dittmann (Werner.Dittmann@t-online.de)
39 */
40 public class SignatureConfirmation {
41
42 public static final String SC_VALUE_ATTR = "Value";
43 protected Element element = null;
44 private byte[] signatureValue = null;
45
46 /**
47 * Constructs a <code>SignatureConfirmation</code> object and parses the
48 * <code>wsse11:SignatureConfirmation</code> element to initialize it.
49 *
50 * @param elem the <code>wsse11:SignatureCOnfirmation</code> element that
51 * contains the confirmation data
52 */
53 public SignatureConfirmation(Element elem) throws WSSecurityException {
54 element = elem;
55 String sv = element.getAttribute(SC_VALUE_ATTR);
56 if (sv != null) {
57 signatureValue = Base64.decode(sv);
58 }
59 }
60
61 /**
62 * Constructs a <code>SignatureConfirmation</code> object according
63 * to the defined parameters.
64 *
65 * @param doc the SOAP envelope as <code>Document</code>
66 * @param signVal the Signature value as byte[] of <code>null</code>
67 * if no value available.
68 */
69 public SignatureConfirmation(Document doc, byte[] signVal) {
70 element =
71 doc.createElementNS(
72 WSConstants.WSSE11_NS,
73 WSConstants.WSSE11_PREFIX + ":" + WSConstants.SIGNATURE_CONFIRMATION_LN
74 );
75 WSSecurityUtil.setNamespace(element, WSConstants.WSSE11_NS, WSConstants.WSSE11_PREFIX);
76 if (signVal != null) {
77 String sv = Base64.encode(signVal);
78 element.setAttributeNS(null, SC_VALUE_ATTR, sv);
79 }
80 }
81
82 /**
83 * Add the WSU Namespace to this SC. The namespace is not added by default for
84 * efficiency purposes.
85 */
86 public void addWSUNamespace() {
87 WSSecurityUtil.setNamespace(element, WSConstants.WSU_NS, WSConstants.WSU_PREFIX);
88 }
89
90 /**
91 * Returns the dom element of this <code>SignatureConfirmation</code> object.
92 *
93 * @return the <code>wsse11:SignatureConfirmation</code> element
94 */
95 public Element getElement() {
96 return element;
97 }
98
99 /**
100 * Returns the string representation of the token.
101 *
102 * @return a XML string representation
103 */
104 public String toString() {
105 return DOM2Writer.nodeToString((Node)element);
106 }
107
108 /**
109 * Set wsu:Id attribute of this SignatureConfirmation element.
110 * @param id
111 */
112 public void setID(String id) {
113 element.setAttributeNS(WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":Id", id);
114 }
115
116 /**
117 * Returns the value of the wsu:Id attribute
118 * @return the WSU ID
119 */
120 public String getID() {
121 return element.getAttributeNS(WSConstants.WSU_NS, "Id");
122 }
123
124 /**
125 * @return Returns the signatureValue.
126 */
127 public byte[] getSignatureValue() {
128 return signatureValue;
129 }
130
131 @Override
132 public int hashCode() {
133 int result = 17;
134 if (signatureValue != null) {
135 result = 31 * result + Arrays.hashCode(signatureValue);
136 }
137 return result;
138 }
139
140 @Override
141 public boolean equals(Object object) {
142 if (!(object instanceof SignatureConfirmation)) {
143 return false;
144 }
145 SignatureConfirmation signatureConfirmation = (SignatureConfirmation)object;
146 byte[] sigValue = signatureConfirmation.getSignatureValue();
147 if (!Arrays.equals(sigValue, getSignatureValue())) {
148 return false;
149 }
150 return true;
151 }
152
153 }