* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / Signature.cs
1 //
2 // Signature.cs - Signature implementation for XML Signature
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.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 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Collections;
34 using System.Security.Cryptography;
35 using System.Xml;
36
37 namespace System.Security.Cryptography.Xml {
38
39         public class Signature {
40                 static XmlNamespaceManager dsigNsmgr;
41                 
42                 static Signature ()
43                 {
44                         dsigNsmgr = new XmlNamespaceManager (new NameTable ());
45                         dsigNsmgr.AddNamespace ("xd", XmlSignature.NamespaceURI);
46                 }
47
48                 private ArrayList list;
49                 private SignedInfo info;
50                 private KeyInfo key;
51                 private string id;
52                 private byte[] signature;
53                 private XmlElement element;
54
55                 public Signature () 
56                 {
57                         list = new ArrayList ();
58                 }
59
60                 public string Id {
61                         get { return id; }
62                         set {
63                                 element = null;
64                                 id = value;
65                         }
66                 }
67
68                 public KeyInfo KeyInfo {
69                         get { return key; }
70                         set {
71                                 element = null;
72                                 key = value;
73                         }
74                 }
75
76                 public IList ObjectList {
77                         get { return list; }
78                         set { list = ArrayList.Adapter (value); }
79                 }
80
81                 public byte[] SignatureValue {
82                         get { return signature; }
83                         set {
84                                 element = null;
85                                 signature = value;
86                         }
87                 }
88
89                 public SignedInfo SignedInfo {
90                         get { return info; }
91                         set {
92                                 element = null;
93                                 info = value;
94                         }
95                 }
96
97                 public void AddObject (DataObject dataObject) 
98                 {
99                         list.Add (dataObject);
100                 }
101
102                 public XmlElement GetXml () 
103                 {
104                         return GetXml (null);
105                 }
106
107                 internal XmlElement GetXml (XmlDocument document)
108                 {
109                         if (element != null)
110                                 return element;
111
112                         if (info == null)
113                                 throw new CryptographicException ("SignedInfo");
114                         if (signature == null)
115                                 throw new CryptographicException ("SignatureValue");
116
117                         if (document == null)
118                                 document = new XmlDocument ();
119
120                         XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Signature, XmlSignature.NamespaceURI);
121                         if (id != null)
122                                 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
123
124                         XmlNode xn = info.GetXml ();
125                         XmlNode newNode = document.ImportNode (xn, true);
126                         xel.AppendChild (newNode);
127
128                         if (signature != null) {
129                                 XmlElement sv = document.CreateElement (XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI);
130                                 sv.InnerText = Convert.ToBase64String (signature);
131                                 xel.AppendChild (sv);
132                         }
133
134                         if (key != null) {
135                                 xn = key.GetXml ();
136                                 newNode = document.ImportNode (xn, true);
137                                 xel.AppendChild (newNode);
138                         }
139
140                         if (list.Count > 0) {
141                                 foreach (DataObject obj in list) {
142                                         xn = obj.GetXml ();
143                                         newNode = document.ImportNode (xn, true);
144                                         xel.AppendChild (newNode);
145                                 }
146                         }
147
148                         return xel;
149                 }
150
151                 private string GetAttribute (XmlElement xel, string attribute) 
152                 {
153                         XmlAttribute xa = xel.Attributes [attribute];
154                         return ((xa != null) ? xa.InnerText : null);
155                 }
156
157                 public void LoadXml (XmlElement value) 
158                 {
159                         if (value == null)
160                                 throw new ArgumentNullException ("value");
161
162                         if ((value.LocalName == XmlSignature.ElementNames.Signature) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
163                                 id = GetAttribute (value, XmlSignature.AttributeNames.Id);
164
165                                 // LAMESPEC: This library is totally useless against eXtensibly Marked-up document.
166                                 int i = NextElementPos (value.ChildNodes, 0, XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI, true);
167                                 XmlElement sinfo = (XmlElement) value.ChildNodes [i];
168                                 info = new SignedInfo ();
169                                 info.LoadXml (sinfo);
170
171                                 i = NextElementPos (value.ChildNodes, ++i, XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI, true);
172                                 XmlElement sigValue = (XmlElement) value.ChildNodes [i];
173                                 signature = Convert.FromBase64String (sigValue.InnerText);
174
175                                 // signature isn't required: <element ref="ds:KeyInfo" minOccurs="0"/> 
176                                 i = NextElementPos (value.ChildNodes, ++i, XmlSignature.ElementNames.KeyInfo, XmlSignature.NamespaceURI, false);
177                                 if (i > 0) {
178                                         XmlElement kinfo = (XmlElement) value.ChildNodes [i];
179                                         key = new KeyInfo ();
180                                         key.LoadXml (kinfo);
181                                 }
182
183                                 XmlNodeList xnl = value.SelectNodes ("xd:Object", dsigNsmgr);
184                                 foreach (XmlElement xn in xnl) {
185                                         DataObject obj = new DataObject ();
186                                         obj.LoadXml (xn);
187                                         AddObject (obj);
188                                 }
189                         }
190                         else
191                                 throw new CryptographicException ("Malformed element: Signature.");
192
193                         // if invalid
194                         if (info == null)
195                                 throw new CryptographicException ("SignedInfo");
196                         if (signature == null)
197                                 throw new CryptographicException ("SignatureValue");
198                 }
199
200                 private int NextElementPos (XmlNodeList nl, int pos, string name, string ns, bool required)
201                 {
202                         while (pos < nl.Count) {
203                                 if (nl [pos].NodeType == XmlNodeType.Element) {
204                                         if (nl [pos].LocalName != name || nl [pos].NamespaceURI != ns) {
205                                                 if (required)
206                                                         throw new CryptographicException ("Malformed element " + name);
207                                                 else
208                                                         return -2;
209                                         }
210                                         else
211                                                 return pos;
212                                 }
213                                 else
214                                         pos++;
215                         }
216                         if (required)
217                                 throw new CryptographicException ("Malformed element " + name);
218                         return -1;
219                 }
220         }
221 }