2 // SignedInfo.cs - SignedInfo implementation for XML Signature
5 // Sebastien Pouliot <sebastien@ximian.com>
6 // Tim Coleman (tim@timcoleman.com)
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) Tim Coleman, 2004
10 // Copyright (C) 2004-2005 Novell, Inc (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;
33 using System.Runtime.InteropServices;
36 namespace System.Security.Cryptography.Xml {
38 public class SignedInfo : ICollection, IEnumerable {
40 private ArrayList references;
41 private string c14nMethod;
43 private string signatureMethod;
44 private string signatureLength;
45 private XmlElement element;
49 references = new ArrayList ();
50 c14nMethod = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
53 public string CanonicalizationMethod {
54 get { return c14nMethod; }
63 public Transform CanonicalizationMethodObject {
64 get { throw new NotImplementedException (); }
67 // documented as not supported (and throwing exception)
69 get { throw new NotSupportedException (); }
80 // documented as not supported (and throwing exception)
81 public bool IsReadOnly {
82 get { throw new NotSupportedException (); }
85 // documented as not supported (and throwing exception)
86 public bool IsSynchronized {
87 get { throw new NotSupportedException (); }
90 // Manipulating this array never affects GetXml() when
91 // LoadXml() was used.
92 // (Actually, there is no way to detect modification.)
93 public ArrayList References {
94 get { return references; }
97 public string SignatureLength {
98 get { return signatureLength; }
101 signatureLength = value;
105 public string SignatureMethod {
106 get { return signatureMethod; }
109 signatureMethod = value;
113 // documented as not supported (and throwing exception)
114 public object SyncRoot {
115 get { throw new NotSupportedException (); }
118 public void AddReference (Reference reference)
120 references.Add (reference);
123 // documented as not supported (and throwing exception)
124 public void CopyTo (Array array, int index)
126 throw new NotSupportedException ();
129 public IEnumerator GetEnumerator ()
131 return references.GetEnumerator ();
134 public XmlElement GetXml ()
139 if (signatureMethod == null)
140 throw new CryptographicException ("SignatureMethod");
141 if (references.Count == 0)
142 throw new CryptographicException ("References empty");
144 XmlDocument document = new XmlDocument ();
145 XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
147 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
149 if (c14nMethod != null) {
150 XmlElement c14n = document.CreateElement (XmlSignature.ElementNames.CanonicalizationMethod, XmlSignature.NamespaceURI);
151 c14n.SetAttribute (XmlSignature.AttributeNames.Algorithm, c14nMethod);
152 xel.AppendChild (c14n);
154 if (signatureMethod != null) {
155 XmlElement sm = document.CreateElement (XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
156 sm.SetAttribute (XmlSignature.AttributeNames.Algorithm, signatureMethod);
157 if (signatureLength != null) {
158 XmlElement hmac = document.CreateElement (XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
159 hmac.InnerText = signatureLength;
160 sm.AppendChild (hmac);
162 xel.AppendChild (sm);
165 // This check is only done when element is created here.
166 if (references.Count == 0)
167 throw new CryptographicException ("At least one Reference element is required in SignedInfo.");
169 // we add References afterward so we don't end up with extraneous
170 // xmlns="..." in each reference elements.
171 foreach (Reference r in references) {
172 XmlNode xn = r.GetXml ();
173 XmlNode newNode = document.ImportNode (xn, true);
174 xel.AppendChild (newNode);
180 private string GetAttribute (XmlElement xel, string attribute)
182 XmlAttribute xa = xel.Attributes [attribute];
183 return ((xa != null) ? xa.InnerText : null);
186 public void LoadXml (XmlElement value)
189 throw new ArgumentNullException ("value");
191 if ((value.LocalName != XmlSignature.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignature.NamespaceURI))
192 throw new CryptographicException ();
194 id = GetAttribute (value, XmlSignature.AttributeNames.Id);
195 c14nMethod = XmlSignature.GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);
197 XmlElement sm = XmlSignature.GetChildElement (value, XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
199 signatureMethod = sm.GetAttribute (XmlSignature.AttributeNames.Algorithm);
200 XmlElement length = XmlSignature.GetChildElement (sm, XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
201 if (length != null) {
202 signatureLength = length.InnerText;
206 for (int i = 0; i < value.ChildNodes.Count; i++) {
207 XmlNode n = value.ChildNodes [i];
208 if (n.NodeType == XmlNodeType.Element &&
209 n.LocalName == XmlSignature.ElementNames.Reference &&
210 n.NamespaceURI == XmlSignature.NamespaceURI) {
211 Reference r = new Reference ();
212 r.LoadXml ((XmlElement) n);