2005-04-12 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / SignedInfo.cs
1 //
2 // SignedInfo.cs - SignedInfo implementation for XML Signature
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //      Tim Coleman (tim@timcoleman.com)
7 //
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) Tim Coleman, 2004
10 // (C) 2004 Novell (http://www.novell.com)
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Collections;
35 using System.Xml;
36
37 namespace System.Security.Cryptography.Xml { 
38
39         public class SignedInfo : ICollection, IEnumerable {
40
41                 private ArrayList references;
42                 private string c14nMethod;
43                 private string id;
44                 private string signatureMethod;
45                 private string signatureLength;
46                 private XmlElement element;
47
48 #if NET_2_0
49                 XmlDsigC14NTransform canonicalizationMethodObject;
50 #endif
51
52                 public SignedInfo() 
53                 {
54                         references = new ArrayList ();
55                         c14nMethod = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
56 #if NET_2_0     
57                         canonicalizationMethodObject = new XmlDsigC14NTransform ();
58 #endif
59                 }
60
61                 public string CanonicalizationMethod {
62                         get { return c14nMethod; }
63                         set {
64                                 c14nMethod = value;
65                                 element = null;
66                         }
67                 }
68
69 #if NET_2_0
70                 public Transform CanonicalizationMethodObject {
71                         get { return canonicalizationMethodObject; }
72                 }
73 #endif
74
75                 // documented as not supported (and throwing exception)
76                 public int Count {
77                         get { throw new NotSupportedException (); }
78                 }
79
80                 public string Id {
81                         get { return id; }
82                         set {
83                                 element = null;
84                                 id = value;
85                         }
86                 }
87
88                 // documented as not supported (and throwing exception)
89                 public bool IsReadOnly {
90                         get { throw new NotSupportedException (); }
91                 }
92
93                 // documented as not supported (and throwing exception)
94                 public bool IsSynchronized {
95                         get { throw new NotSupportedException (); }
96                 }
97
98                 // Manipulating this array never affects GetXml() when 
99                 // LoadXml() was used. 
100                 // (Actually, there is no way to detect modification.)
101                 public ArrayList References {
102                         get { return references; }
103                 }
104
105                 public string SignatureLength {
106                         get { return signatureLength; }
107                         set {
108                                 element = null;
109                                 signatureLength = value;
110                         }
111                 }
112
113                 public string SignatureMethod {
114                         get { return signatureMethod; }
115                         set {
116                                 element = null;
117                                 signatureMethod = value;
118                         }
119                 }
120
121                 // documented as not supported (and throwing exception)
122                 public object SyncRoot {
123                         get { throw new NotSupportedException (); }
124                 }
125
126                 public void AddReference (Reference reference) 
127                 {
128                         references.Add (reference);
129                 }
130
131                 // documented as not supported (and throwing exception)
132                 public void CopyTo (Array array, int index) 
133                 {
134                         throw new NotSupportedException ();
135                 }
136
137                 public IEnumerator GetEnumerator () 
138                 {
139                         return references.GetEnumerator ();
140                 }
141
142                 public XmlElement GetXml ()
143                 {
144                         if (element != null)
145                                 return element;
146
147                         if (signatureMethod == null)
148                                 throw new CryptographicException ("SignatureMethod");
149                         if (references.Count == 0)
150                                 throw new CryptographicException ("References empty");
151
152                         XmlDocument document = new XmlDocument ();
153                         XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
154                         if (id != null)
155                                 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
156
157                         if (c14nMethod != null) {
158                                 XmlElement c14n = document.CreateElement (XmlSignature.ElementNames.CanonicalizationMethod, XmlSignature.NamespaceURI);
159                                 c14n.SetAttribute (XmlSignature.AttributeNames.Algorithm, c14nMethod);
160                                 xel.AppendChild (c14n);
161                         }
162                         if (signatureMethod != null) {
163                                 XmlElement sm = document.CreateElement (XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
164                                 sm.SetAttribute (XmlSignature.AttributeNames.Algorithm, signatureMethod);
165                                 if (signatureLength != null) {
166                                         XmlElement hmac = document.CreateElement (XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
167                                         hmac.InnerText = signatureLength;
168                                         sm.AppendChild (hmac);
169                                 }
170                                 xel.AppendChild (sm);
171                         }
172
173                         // This check is only done when element is created here.
174                         if (references.Count == 0)
175                                 throw new CryptographicException ("At least one Reference element is required in SignedInfo.");
176
177                         // we add References afterward so we don't end up with extraneous
178                         // xmlns="..." in each reference elements.
179                         foreach (Reference r in references) {
180                                 XmlNode xn = r.GetXml ();
181                                 XmlNode newNode = document.ImportNode (xn, true);
182                                 xel.AppendChild (newNode);
183                         }
184
185                         return xel;
186                 }
187
188                 private string GetAttribute (XmlElement xel, string attribute) 
189                 {
190                         XmlAttribute xa = xel.Attributes [attribute];
191                         return ((xa != null) ? xa.InnerText : null);
192                 }
193
194                 public void LoadXml (XmlElement value) 
195                 {
196                         if (value == null)
197                                 throw new ArgumentNullException ("value");
198
199                         if ((value.LocalName != XmlSignature.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignature.NamespaceURI))
200                                 throw new CryptographicException ();
201
202                         id = GetAttribute (value, XmlSignature.AttributeNames.Id);
203                         c14nMethod = XmlSignature.GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);
204
205                         XmlElement sm = XmlSignature.GetChildElement (value, XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
206                         if (sm != null) {
207                                 signatureMethod = sm.GetAttribute (XmlSignature.AttributeNames.Algorithm);
208                                 XmlElement length = XmlSignature.GetChildElement (sm, XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
209                                 if (length != null) {
210                                         signatureLength = length.InnerText;
211                                 }
212                         }
213
214                         for (int i = 0; i < value.ChildNodes.Count; i++) {
215                                 XmlNode n = value.ChildNodes [i];
216                                 if (n.NodeType == XmlNodeType.Element &&
217                                         n.LocalName == XmlSignature.ElementNames.Reference &&
218                                         n.NamespaceURI == XmlSignature.NamespaceURI) {
219                                         Reference r = new Reference ();
220                                         r.LoadXml ((XmlElement) n);
221                                         AddReference (r);
222                                 }
223                         }
224                         element = value;
225                 }
226         }
227 }