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