2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Security / System.Security.Cryptography.Xml / Signature.cs
blob49e9d3ad9913e176b17bafb62e3bfdd244dbc0df
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
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.
33 using System.Collections;
34 using System.Security.Cryptography;
35 using System.Xml;
37 namespace System.Security.Cryptography.Xml {
39 public class Signature {
40 static XmlNamespaceManager dsigNsmgr;
42 static Signature ()
44 dsigNsmgr = new XmlNamespaceManager (new NameTable ());
45 dsigNsmgr.AddNamespace ("xd", XmlSignature.NamespaceURI);
48 private ArrayList list;
49 private SignedInfo info;
50 private KeyInfo key;
51 private string id;
52 private byte[] signature;
53 private XmlElement element;
55 public Signature ()
57 list = new ArrayList ();
60 public string Id {
61 get { return id; }
62 set {
63 element = null;
64 id = value;
68 public KeyInfo KeyInfo {
69 get { return key; }
70 set {
71 element = null;
72 key = value;
76 public IList ObjectList {
77 get { return list; }
78 set { list = ArrayList.Adapter (value); }
81 public byte[] SignatureValue {
82 get { return signature; }
83 set {
84 element = null;
85 signature = value;
89 public SignedInfo SignedInfo {
90 get { return info; }
91 set {
92 element = null;
93 info = value;
97 public void AddObject (DataObject dataObject)
99 list.Add (dataObject);
102 public XmlElement GetXml ()
104 return GetXml (null);
107 internal XmlElement GetXml (XmlDocument document)
109 if (element != null)
110 return element;
112 if (info == null)
113 throw new CryptographicException ("SignedInfo");
114 if (signature == null)
115 throw new CryptographicException ("SignatureValue");
117 if (document == null)
118 document = new XmlDocument ();
120 XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Signature, XmlSignature.NamespaceURI);
121 if (id != null)
122 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
124 XmlNode xn = info.GetXml ();
125 XmlNode newNode = document.ImportNode (xn, true);
126 xel.AppendChild (newNode);
128 if (signature != null) {
129 XmlElement sv = document.CreateElement (XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI);
130 sv.InnerText = Convert.ToBase64String (signature);
131 xel.AppendChild (sv);
134 if (key != null) {
135 xn = key.GetXml ();
136 newNode = document.ImportNode (xn, true);
137 xel.AppendChild (newNode);
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);
148 return xel;
151 private string GetAttribute (XmlElement xel, string attribute)
153 XmlAttribute xa = xel.Attributes [attribute];
154 return ((xa != null) ? xa.InnerText : null);
157 public void LoadXml (XmlElement value)
159 if (value == null)
160 throw new ArgumentNullException ("value");
162 if ((value.LocalName == XmlSignature.ElementNames.Signature) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
163 id = GetAttribute (value, XmlSignature.AttributeNames.Id);
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);
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);
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);
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);
190 else
191 throw new CryptographicException ("Malformed element: Signature.");
193 // if invalid
194 if (info == null)
195 throw new CryptographicException ("SignedInfo");
196 if (signature == null)
197 throw new CryptographicException ("SignatureValue");
200 private int NextElementPos (XmlNodeList nl, int pos, string name, string ns, bool required)
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;
210 else
211 return pos;
213 else
214 pos++;
216 if (required)
217 throw new CryptographicException ("Malformed element " + name);
218 return -1;