| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- //
- // Signature.cs - Signature implementation for XML Signature
- //
- // Author:
- // Sebastien Pouliot ([email protected])
- //
- // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
- //
- using System.Collections;
- using System.Security.Cryptography;
- using System.Text;
- using System.Xml;
- namespace System.Security.Cryptography.Xml {
- public class Signature {
- private ArrayList list;
- private SignedInfo info;
- private KeyInfo key;
- private string id;
- private byte[] signature;
- static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
- public Signature()
- {
- list = new ArrayList ();
- }
- public string Id {
- get { return id; }
- set { id = value; }
- }
- public KeyInfo KeyInfo {
- get { return key; }
- set { key = value; }
- }
- public IList ObjectList {
- get { return list; }
- set { list = ArrayList.Adapter (value); }
- }
- public byte[] SignatureValue {
- get { return signature; }
- set { signature = value; }
- }
- public SignedInfo SignedInfo {
- get { return info; }
- set { info = value; }
- }
- public void AddObject (DataObject dataObject)
- {
- list.Add (dataObject);
- }
- public XmlElement GetXml ()
- {
- if (info == null)
- throw new CryptographicException ("SignedInfo");
- if (signature == null)
- throw new CryptographicException ("SignatureValue");
- StringBuilder sb = new StringBuilder ();
- sb.Append ("<Signature");
- if (id != null) {
- sb.Append (" Id = \"");
- sb.Append (id);
- sb.Append ("\"");
- }
- sb.Append (" xmlns=\"");
- sb.Append (xmldsig);
- /* if (info != null) {
- sb.Append ("\">");
- sb.Append (info.GetXml ().OuterXml);
- sb.Append ("</Signature>");
- }
- else*/
- sb.Append ("\" />");
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml (sb.ToString ());
- XmlNode xn = null;
- XmlNode newNode = null;
- if (info != null) {
- // this adds the xmlns=xmldsig
- xn = info.GetXml ();
- newNode = doc.ImportNode (xn, true);
- doc.DocumentElement.AppendChild (newNode);
- }
- if (signature != null) {
- XmlElement sv = doc.CreateElement ("SignatureValue", xmldsig);
- sv.InnerText = Convert.ToBase64String (signature);
- doc.DocumentElement.AppendChild (sv);
- }
- if (key != null) {
- xn = key.GetXml ();
- newNode = doc.ImportNode (xn, true);
- doc.DocumentElement.AppendChild (newNode);
- }
- if (list.Count > 0) {
- foreach (DataObject obj in list) {
- xn = obj.GetXml ();
- newNode = doc.ImportNode (xn, true);
- doc.DocumentElement.AppendChild (newNode);
- }
- }
- return doc.DocumentElement;
- }
- private string GetAttribute (XmlElement xel, string attribute)
- {
- XmlAttribute xa = xel.Attributes [attribute];
- return ((xa != null) ? xa.InnerText : null);
- }
- public void LoadXml (XmlElement value)
- {
- if (value == null)
- throw new ArgumentNullException ("value");
- if ((value.LocalName == "Signature") && (value.NamespaceURI == xmldsig)) {
- id = GetAttribute (value, "Id");
- XmlNodeList xnl = value.GetElementsByTagName ("SignedInfo");
- if ((xnl != null) && (xnl.Count == 1)) {
- info = new SignedInfo ();
- info.LoadXml ((XmlElement) xnl[0]);
- }
- xnl = value.GetElementsByTagName ("SignatureValue");
- if ((xnl != null) && (xnl.Count == 1)) {
- signature = Convert.FromBase64String (xnl[0].InnerText);
- }
- xnl = value.GetElementsByTagName ("KeyInfo");
- if ((xnl != null) && (xnl.Count == 1)) {
- key = new KeyInfo ();
- key.LoadXml ((XmlElement) xnl[0]);
- }
- xnl = value.GetElementsByTagName ("Object");
- if ((xnl != null) && (xnl.Count > 0)) {
- foreach (XmlNode xn in xnl) {
- DataObject obj = new DataObject ();
- obj.LoadXml ((XmlElement) xn);
- AddObject (obj);
- }
- }
- }
- // if invalid
- if (info == null)
- throw new CryptographicException ("SignedInfo");
- if (signature == null)
- throw new CryptographicException ("SignatureValue");
- }
- }
- }
|