This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
9 //
10
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.Xml;
34
35 namespace System.Security.Cryptography.Xml { 
36
37         public class SignedInfo : ICollection, IEnumerable {
38
39                 private ArrayList references;
40                 private string c14nMethod;
41                 private string id;
42                 private string signatureMethod;
43                 private string signatureLength;
44                 private XmlElement element;
45
46                 public SignedInfo() 
47                 {
48                         references = new ArrayList ();
49                         c14nMethod = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
50                 }
51
52                 public string CanonicalizationMethod {
53                         get { return c14nMethod; }
54                         set {
55                                 c14nMethod = value;
56                                 element = null;
57                         }
58                 }
59
60                 // documented as not supported (and throwing exception)
61                 public int Count {
62                         get { throw new NotSupportedException (); }
63                 }
64
65                 public string Id {
66                         get { return id; }
67                         set {
68                                 element = null;
69                                 id = value;
70                         }
71                 }
72
73                 // documented as not supported (and throwing exception)
74                 public bool IsReadOnly {
75                         get { throw new NotSupportedException (); }
76                 }
77
78                 // documented as not supported (and throwing exception)
79                 public bool IsSynchronized {
80                         get { throw new NotSupportedException (); }
81                 }
82
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; }
88                 }
89
90                 public string SignatureLength {
91                         get { return signatureLength; }
92                         set {
93                                 element = null;
94                                 signatureLength = value;
95                         }
96                 }
97
98                 public string SignatureMethod {
99                         get { return signatureMethod; }
100                         set {
101                                 element = null;
102                                 signatureMethod = value;
103                         }
104                 }
105
106                 // documented as not supported (and throwing exception)
107                 public object SyncRoot {
108                         get { throw new NotSupportedException (); }
109                 }
110
111                 public void AddReference (Reference reference) 
112                 {
113                         references.Add (reference);
114                 }
115
116                 // documented as not supported (and throwing exception)
117                 public void CopyTo (Array array, int index) 
118                 {
119                         throw new NotSupportedException ();
120                 }
121
122                 public IEnumerator GetEnumerator () 
123                 {
124                         return references.GetEnumerator ();
125                 }
126
127                 public XmlElement GetXml ()
128                 {
129                         if (element != null)
130                                 return element;
131
132                         if (signatureMethod == null)
133                                 throw new CryptographicException ("SignatureMethod");
134                         if (references.Count == 0)
135                                 throw new CryptographicException ("References empty");
136
137                         XmlDocument document = new XmlDocument ();
138                         XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
139                         if (id != null)
140                                 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
141
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);
146                         }
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);
154                                 }
155                                 xel.AppendChild (sm);
156                         }
157
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.");
161
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);
168                         }
169
170                         return xel;
171                 }
172
173                 private string GetAttribute (XmlElement xel, string attribute) 
174                 {
175                         XmlAttribute xa = xel.Attributes [attribute];
176                         return ((xa != null) ? xa.InnerText : null);
177                 }
178
179                 public void LoadXml (XmlElement value) 
180                 {
181                         if (value == null)
182                                 throw new ArgumentNullException ("value");
183
184                         if ((value.LocalName != XmlSignature.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignature.NamespaceURI))
185                                 throw new CryptographicException ();
186
187                         id = GetAttribute (value, XmlSignature.AttributeNames.Id);
188                         c14nMethod = XmlSignature.GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);
189
190                         XmlElement sm = XmlSignature.GetChildElement (value, XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
191                         if (sm != null) {
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;
196                                 }
197                         }
198
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);
206                                         AddReference (r);
207                                 }
208                         }
209                         element = value;
210                 }
211         }
212 }