Merge branch 'master' of git://github.com/mono/mono
[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 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System.Collections;
33 using System.Runtime.InteropServices;
34 using System.Xml;
35
36 namespace System.Security.Cryptography.Xml { 
37
38         public class SignedInfo : ICollection, IEnumerable {
39
40                 private ArrayList references;
41                 private string c14nMethod;
42                 private string id;
43                 private string signatureMethod;
44                 private string signatureLength;
45                 private XmlElement element;
46
47                 public SignedInfo() 
48                 {
49                         references = new ArrayList ();
50                         c14nMethod = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
51                 }
52
53                 public string CanonicalizationMethod {
54                         get { return c14nMethod; }
55                         set {
56                                 c14nMethod = value;
57                                 element = null;
58                         }
59                 }
60
61 #if NET_2_0
62                 [ComVisible (false)]
63                 [MonoTODO]
64                 public Transform CanonicalizationMethodObject {
65                         get { throw new NotImplementedException (); }
66                 }
67 #endif
68
69                 // documented as not supported (and throwing exception)
70                 public int Count {
71                         get { throw new NotSupportedException (); }
72                 }
73
74                 public string Id {
75                         get { return id; }
76                         set {
77                                 element = null;
78                                 id = value;
79                         }
80                 }
81
82                 // documented as not supported (and throwing exception)
83                 public bool IsReadOnly {
84                         get { throw new NotSupportedException (); }
85                 }
86
87                 // documented as not supported (and throwing exception)
88                 public bool IsSynchronized {
89                         get { throw new NotSupportedException (); }
90                 }
91
92                 // Manipulating this array never affects GetXml() when 
93                 // LoadXml() was used. 
94                 // (Actually, there is no way to detect modification.)
95                 public ArrayList References {
96                         get { return references; }
97                 }
98
99                 public string SignatureLength {
100                         get { return signatureLength; }
101                         set {
102                                 element = null;
103                                 signatureLength = value;
104                         }
105                 }
106
107                 public string SignatureMethod {
108                         get { return signatureMethod; }
109                         set {
110                                 element = null;
111                                 signatureMethod = value;
112                         }
113                 }
114
115                 // documented as not supported (and throwing exception)
116                 public object SyncRoot {
117                         get { throw new NotSupportedException (); }
118                 }
119
120                 public void AddReference (Reference reference) 
121                 {
122                         references.Add (reference);
123                 }
124
125                 // documented as not supported (and throwing exception)
126                 public void CopyTo (Array array, int index) 
127                 {
128                         throw new NotSupportedException ();
129                 }
130
131                 public IEnumerator GetEnumerator () 
132                 {
133                         return references.GetEnumerator ();
134                 }
135
136                 public XmlElement GetXml ()
137                 {
138                         if (element != null)
139                                 return element;
140
141                         if (signatureMethod == null)
142                                 throw new CryptographicException ("SignatureMethod");
143                         if (references.Count == 0)
144                                 throw new CryptographicException ("References empty");
145
146                         XmlDocument document = new XmlDocument ();
147                         XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
148                         if (id != null)
149                                 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
150
151                         if (c14nMethod != null) {
152                                 XmlElement c14n = document.CreateElement (XmlSignature.ElementNames.CanonicalizationMethod, XmlSignature.NamespaceURI);
153                                 c14n.SetAttribute (XmlSignature.AttributeNames.Algorithm, c14nMethod);
154                                 xel.AppendChild (c14n);
155                         }
156                         if (signatureMethod != null) {
157                                 XmlElement sm = document.CreateElement (XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
158                                 sm.SetAttribute (XmlSignature.AttributeNames.Algorithm, signatureMethod);
159                                 if (signatureLength != null) {
160                                         XmlElement hmac = document.CreateElement (XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
161                                         hmac.InnerText = signatureLength;
162                                         sm.AppendChild (hmac);
163                                 }
164                                 xel.AppendChild (sm);
165                         }
166
167                         // This check is only done when element is created here.
168                         if (references.Count == 0)
169                                 throw new CryptographicException ("At least one Reference element is required in SignedInfo.");
170
171                         // we add References afterward so we don't end up with extraneous
172                         // xmlns="..." in each reference elements.
173                         foreach (Reference r in references) {
174                                 XmlNode xn = r.GetXml ();
175                                 XmlNode newNode = document.ImportNode (xn, true);
176                                 xel.AppendChild (newNode);
177                         }
178
179                         return xel;
180                 }
181
182                 private string GetAttribute (XmlElement xel, string attribute) 
183                 {
184                         XmlAttribute xa = xel.Attributes [attribute];
185                         return ((xa != null) ? xa.InnerText : null);
186                 }
187
188                 public void LoadXml (XmlElement value) 
189                 {
190                         if (value == null)
191                                 throw new ArgumentNullException ("value");
192
193                         if ((value.LocalName != XmlSignature.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignature.NamespaceURI))
194                                 throw new CryptographicException ();
195
196                         id = GetAttribute (value, XmlSignature.AttributeNames.Id);
197                         c14nMethod = XmlSignature.GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);
198
199                         XmlElement sm = XmlSignature.GetChildElement (value, XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
200                         if (sm != null) {
201                                 signatureMethod = sm.GetAttribute (XmlSignature.AttributeNames.Algorithm);
202                                 XmlElement length = XmlSignature.GetChildElement (sm, XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
203                                 if (length != null) {
204                                         signatureLength = length.InnerText;
205                                 }
206                         }
207
208                         for (int i = 0; i < value.ChildNodes.Count; i++) {
209                                 XmlNode n = value.ChildNodes [i];
210                                 if (n.NodeType == XmlNodeType.Element &&
211                                         n.LocalName == XmlSignature.ElementNames.Reference &&
212                                         n.NamespaceURI == XmlSignature.NamespaceURI) {
213                                         Reference r = new Reference ();
214                                         r.LoadXml ((XmlElement) n);
215                                         AddReference (r);
216                                 }
217                         }
218                         element = value;
219                 }
220         }
221 }