View Javadoc

1   /*
2    * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
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  package org.opensaml.xml.signature.impl;
18  
19  import java.util.Collections;
20  import java.util.LinkedList;
21  import java.util.List;
22  
23  import org.apache.xml.security.signature.XMLSignature;
24  import org.opensaml.xml.AbstractXMLObject;
25  import org.opensaml.xml.XMLObject;
26  import org.opensaml.xml.security.credential.Credential;
27  import org.opensaml.xml.signature.ContentReference;
28  import org.opensaml.xml.signature.KeyInfo;
29  import org.opensaml.xml.signature.Signature;
30  
31  /**
32   * XMLObject representing an enveloped or detached XML Digital Signature, version 20020212, Signature element.
33   */
34  public class SignatureImpl extends AbstractXMLObject implements Signature {
35  
36      /** Canonicalization algorithm used in signature. */
37      private String canonicalizationAlgorithm;
38  
39      /** Algorithm used to generate the signature. */
40      private String signatureAlgorithm;
41  
42      /** Optional HMAC output length parameter to the signature algorithm. */
43      private Integer hmacOutputLength;
44  
45      /** Key used to sign the signature. */
46      private Credential signingCredential;
47  
48      /** Public key information to embed in the signature. */
49      private KeyInfo keyInfo;
50  
51      /** References to content to be signed. */
52      private List<ContentReference> contentReferences;
53  
54      /** Constructed Apache XML Security signature object. */
55      private XMLSignature xmlSignature;
56  
57      /**
58       * Constructor.
59       * 
60       * @param namespaceURI the namespace the element is in
61       * @param elementLocalName the local name of the XML element this Object represents
62       * @param namespacePrefix the prefix for the given namespace
63       */
64      protected SignatureImpl(String namespaceURI, String elementLocalName, String namespacePrefix) {
65          super(namespaceURI, elementLocalName, namespacePrefix);
66          contentReferences = new LinkedList<ContentReference>();
67      }
68  
69      /** {@inheritDoc} */
70      public String getCanonicalizationAlgorithm() {
71          return canonicalizationAlgorithm;
72      }
73  
74      /** {@inheritDoc} */
75      public void setCanonicalizationAlgorithm(String newAlgorithm) {
76          canonicalizationAlgorithm = prepareForAssignment(canonicalizationAlgorithm, newAlgorithm);
77      }
78  
79      /** {@inheritDoc} */
80      public String getSignatureAlgorithm() {
81          return signatureAlgorithm;
82      }
83  
84      /** {@inheritDoc} */
85      public void setSignatureAlgorithm(String newAlgorithm) {
86          signatureAlgorithm = prepareForAssignment(signatureAlgorithm, newAlgorithm);
87      }
88  
89      /** {@inheritDoc} */
90      public Integer getHMACOutputLength() {
91          return hmacOutputLength;
92      }
93  
94      /** {@inheritDoc} */
95      public void setHMACOutputLength(Integer length) {
96          hmacOutputLength = prepareForAssignment(hmacOutputLength, length);
97      }
98  
99      /** {@inheritDoc} */
100     public Credential getSigningCredential() {
101         return signingCredential;
102     }
103 
104     /** {@inheritDoc} */
105     public void setSigningCredential(Credential newCredential) {
106         signingCredential = prepareForAssignment(signingCredential, newCredential);
107     }
108 
109     /** {@inheritDoc} */
110     public KeyInfo getKeyInfo() {
111         return keyInfo;
112     }
113 
114     /** {@inheritDoc} */
115     public void setKeyInfo(KeyInfo newKeyInfo) {
116         keyInfo = prepareForAssignment(keyInfo, newKeyInfo);
117     }
118 
119     /** {@inheritDoc} */
120     public List<ContentReference> getContentReferences() {
121         // TODO worry about detecting changes and releasing this object's and parent's DOM?
122         // would need something like an Observable list/collection impl or something similar
123         return contentReferences;
124     }
125 
126     /** {@inheritDoc} */
127     public List<XMLObject> getOrderedChildren() {
128         return Collections.EMPTY_LIST;
129     }
130 
131     /** {@inheritDoc} */
132     public void releaseDOM() {
133         super.releaseDOM();
134         xmlSignature = null;
135         
136         // Signature's does not treat its children as other XMLObjects do
137         // they are more tightly bound to the Signature and can not exist
138         // without it.  So when Signature releases its DOM it whacks the 
139         // DOM for its children too
140         if (keyInfo != null) {
141             keyInfo.releaseChildrenDOM(true);
142             keyInfo.releaseDOM();
143         }
144     }
145 
146     /**
147      * Get the Apache XML Security signature instance held by this object.
148      * 
149      * @return an Apache XML Security signature object
150      */
151     public XMLSignature getXMLSignature() {
152         return xmlSignature;
153     }
154 
155     /**
156      * Set the Apache XML Security signature instance held by this object.
157      * 
158      * @param signature an Apache XML Security signature object
159      */
160     public void setXMLSignature(XMLSignature signature) {
161         xmlSignature = prepareForAssignment(xmlSignature, signature);
162     }
163 }