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)
10 using System.Collections;
11 using System.Security.Cryptography;
14 namespace System.Security.Cryptography.Xml {
16 public class Signature {
18 private ArrayList list;
19 private SignedInfo info;
22 private byte[] signature;
26 list = new ArrayList ();
34 public KeyInfo KeyInfo {
39 public IList ObjectList {
41 set { list = ArrayList.Adapter (value); }
44 public byte[] SignatureValue {
45 get { return signature; }
46 set { signature = value; }
49 public SignedInfo SignedInfo {
54 public void AddObject (DataObject dataObject)
56 list.Add (dataObject);
59 public XmlElement GetXml ()
62 throw new CryptographicException ("SignedInfo");
63 if (signature == null)
64 throw new CryptographicException ("SignatureValue");
66 XmlDocument document = new XmlDocument ();
67 XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Signature, XmlSignature.NamespaceURI);
69 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
71 XmlNode xn = info.GetXml ();
72 XmlNode newNode = document.ImportNode (xn, true);
73 xel.AppendChild (newNode);
75 if (signature != null) {
76 XmlElement sv = document.CreateElement (XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI);
77 sv.InnerText = Convert.ToBase64String (signature);
83 newNode = document.ImportNode (xn, true);
84 xel.AppendChild (newNode);
88 foreach (DataObject obj in list) {
90 newNode = document.ImportNode (xn, true);
91 xel.AppendChild (newNode);
98 private string GetAttribute (XmlElement xel, string attribute)
100 XmlAttribute xa = xel.Attributes [attribute];
101 return ((xa != null) ? xa.InnerText : null);
104 public void LoadXml (XmlElement value)
107 throw new ArgumentNullException ("value");
109 if ((value.LocalName == XmlSignature.ElementNames.Signature) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
110 id = GetAttribute (value, XmlSignature.AttributeNames.Id);
112 XmlNodeList xnl = value.GetElementsByTagName (XmlSignature.ElementNames.SignedInfo);
113 if ((xnl != null) && (xnl.Count == 1)) {
114 info = new SignedInfo ();
115 info.LoadXml ((XmlElement) xnl[0]);
118 xnl = value.GetElementsByTagName (XmlSignature.ElementNames.SignatureValue);
119 if ((xnl != null) && (xnl.Count == 1)) {
120 signature = Convert.FromBase64String (xnl[0].InnerText);
123 xnl = value.GetElementsByTagName (XmlSignature.ElementNames.KeyInfo);
124 if ((xnl != null) && (xnl.Count == 1)) {
125 key = new KeyInfo ();
126 key.LoadXml ((XmlElement) xnl[0]);
129 xnl = value.GetElementsByTagName (XmlSignature.ElementNames.Object);
130 if ((xnl != null) && (xnl.Count > 0)) {
131 foreach (XmlNode xn in xnl) {
132 DataObject obj = new DataObject ();
133 obj.LoadXml ((XmlElement) xn);
141 throw new CryptographicException ("SignedInfo");
142 if (signature == null)
143 throw new CryptographicException ("SignatureValue");