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