2 // Signature.cs - Signature implementation for XML Signature
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
32 using System.Security.Cryptography;
35 namespace System.Security.Cryptography.Xml {
37 public class Signature {
38 static XmlNamespaceManager dsigNsmgr;
42 dsigNsmgr = new XmlNamespaceManager (new NameTable ());
43 dsigNsmgr.AddNamespace ("xd", XmlSignature.NamespaceURI);
46 private ArrayList list;
47 private SignedInfo info;
50 private byte[] signature;
51 private XmlElement element;
55 list = new ArrayList ();
66 public KeyInfo KeyInfo {
74 public IList ObjectList {
76 set { list = ArrayList.Adapter (value); }
79 public byte[] SignatureValue {
80 get { return signature; }
87 public SignedInfo SignedInfo {
95 public void AddObject (DataObject dataObject)
97 list.Add (dataObject);
100 public XmlElement GetXml ()
106 throw new CryptographicException ("SignedInfo");
107 if (signature == null)
108 throw new CryptographicException ("SignatureValue");
110 XmlDocument document = new XmlDocument ();
111 XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Signature, XmlSignature.NamespaceURI);
113 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
115 XmlNode xn = info.GetXml ();
116 XmlNode newNode = document.ImportNode (xn, true);
117 xel.AppendChild (newNode);
119 if (signature != null) {
120 XmlElement sv = document.CreateElement (XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI);
121 sv.InnerText = Convert.ToBase64String (signature);
122 xel.AppendChild (sv);
127 newNode = document.ImportNode (xn, true);
128 xel.AppendChild (newNode);
131 if (list.Count > 0) {
132 foreach (DataObject obj in list) {
134 newNode = document.ImportNode (xn, true);
135 xel.AppendChild (newNode);
142 private string GetAttribute (XmlElement xel, string attribute)
144 XmlAttribute xa = xel.Attributes [attribute];
145 return ((xa != null) ? xa.InnerText : null);
148 public void LoadXml (XmlElement value)
151 throw new ArgumentNullException ("value");
153 if ((value.LocalName == XmlSignature.ElementNames.Signature) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
154 id = GetAttribute (value, XmlSignature.AttributeNames.Id);
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);
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);
166 // signature isn't required: <element ref="ds:KeyInfo" minOccurs="0"/>
167 i = NextElementPos (value.ChildNodes, ++i, XmlSignature.ElementNames.KeyInfo, XmlSignature.NamespaceURI, false);
169 XmlElement kinfo = (XmlElement) value.ChildNodes [i];
170 key = new KeyInfo ();
174 XmlNodeList xnl = value.SelectNodes ("xd:Object", dsigNsmgr);
175 foreach (XmlElement xn in xnl) {
176 DataObject obj = new DataObject ();
182 throw new CryptographicException ("Malformed element: Signature.");
186 throw new CryptographicException ("SignedInfo");
187 if (signature == null)
188 throw new CryptographicException ("SignatureValue");
191 private int NextElementPos (XmlNodeList nl, int pos, string name, string ns, bool required)
193 while (pos < nl.Count) {
194 if (nl [pos].NodeType == XmlNodeType.Element) {
195 if (nl [pos].LocalName != name || nl [pos].NamespaceURI != ns) {
197 throw new CryptographicException ("Malformed element " + name);
208 throw new CryptographicException ("Malformed element " + name);