2 // SignedInfo.cs - SignedInfo implementation for XML Signature
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
35 namespace System.Security.Cryptography.Xml {
37 public class SignedInfo : ICollection, IEnumerable {
39 private ArrayList references;
40 private string c14nMethod;
42 private string signatureMethod;
43 private string signatureLength;
44 private XmlElement element;
48 references = new ArrayList ();
49 c14nMethod = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
52 public string CanonicalizationMethod {
53 get { return c14nMethod; }
60 // documented as not supported (and throwing exception)
62 get { throw new NotSupportedException (); }
73 // documented as not supported (and throwing exception)
74 public bool IsReadOnly {
75 get { throw new NotSupportedException (); }
78 // documented as not supported (and throwing exception)
79 public bool IsSynchronized {
80 get { throw new NotSupportedException (); }
83 // Manipulating this array never affects GetXml() when
84 // LoadXml() was used.
85 // (Actually, there is no way to detect modification.)
86 public ArrayList References {
87 get { return references; }
90 public string SignatureLength {
91 get { return signatureLength; }
94 signatureLength = value;
98 public string SignatureMethod {
99 get { return signatureMethod; }
102 signatureMethod = value;
106 // documented as not supported (and throwing exception)
107 public object SyncRoot {
108 get { throw new NotSupportedException (); }
111 public void AddReference (Reference reference)
113 references.Add (reference);
116 // documented as not supported (and throwing exception)
117 public void CopyTo (Array array, int index)
119 throw new NotSupportedException ();
122 public IEnumerator GetEnumerator ()
124 return references.GetEnumerator ();
127 public XmlElement GetXml ()
132 if (signatureMethod == null)
133 throw new CryptographicException ("SignatureMethod");
134 if (references.Count == 0)
135 throw new CryptographicException ("References empty");
137 XmlDocument document = new XmlDocument ();
138 XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
140 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
142 if (c14nMethod != null) {
143 XmlElement c14n = document.CreateElement (XmlSignature.ElementNames.CanonicalizationMethod, XmlSignature.NamespaceURI);
144 c14n.SetAttribute (XmlSignature.AttributeNames.Algorithm, c14nMethod);
145 xel.AppendChild (c14n);
147 if (signatureMethod != null) {
148 XmlElement sm = document.CreateElement (XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
149 sm.SetAttribute (XmlSignature.AttributeNames.Algorithm, signatureMethod);
150 if (signatureLength != null) {
151 XmlElement hmac = document.CreateElement (XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
152 hmac.InnerText = signatureLength;
153 sm.AppendChild (hmac);
155 xel.AppendChild (sm);
158 // This check is only done when element is created here.
159 if (references.Count == 0)
160 throw new CryptographicException ("At least one Reference element is required in SignedInfo.");
162 // we add References afterward so we don't end up with extraneous
163 // xmlns="..." in each reference elements.
164 foreach (Reference r in references) {
165 XmlNode xn = r.GetXml ();
166 XmlNode newNode = document.ImportNode (xn, true);
167 xel.AppendChild (newNode);
173 private string GetAttribute (XmlElement xel, string attribute)
175 XmlAttribute xa = xel.Attributes [attribute];
176 return ((xa != null) ? xa.InnerText : null);
179 public void LoadXml (XmlElement value)
182 throw new ArgumentNullException ("value");
184 if ((value.LocalName != XmlSignature.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignature.NamespaceURI))
185 throw new CryptographicException ();
187 id = GetAttribute (value, XmlSignature.AttributeNames.Id);
188 c14nMethod = XmlSignature.GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);
190 XmlElement sm = XmlSignature.GetChildElement (value, XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
192 signatureMethod = sm.GetAttribute (XmlSignature.AttributeNames.Algorithm);
193 XmlElement length = XmlSignature.GetChildElement (sm, XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
194 if (length != null) {
195 signatureLength = length.InnerText;
199 for (int i = 0; i < value.ChildNodes.Count; i++) {
200 XmlNode n = value.ChildNodes [i];
201 if (n.NodeType == XmlNodeType.Element &&
202 n.LocalName == XmlSignature.ElementNames.Reference &&
203 n.NamespaceURI == XmlSignature.NamespaceURI) {
204 Reference r = new Reference ();
205 r.LoadXml ((XmlElement) n);