2 // SignedInfo.cs - SignedInfo 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;
13 namespace System.Security.Cryptography.Xml {
15 public class SignedInfo : ICollection, IEnumerable {
17 private ArrayList references;
18 private string c14nMethod;
20 private string signatureMethod;
21 private string signatureLength;
25 references = new ArrayList ();
26 c14nMethod = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
29 public string CanonicalizationMethod {
30 get { return c14nMethod; }
31 set { c14nMethod = value; }
34 // documented as not supported (and throwing exception)
36 get { throw new NotSupportedException (); }
44 // documented as not supported (and throwing exception)
45 public bool IsReadOnly {
46 get { throw new NotSupportedException (); }
49 // documented as not supported (and throwing exception)
50 public bool IsSynchronized {
51 get { throw new NotSupportedException (); }
54 public ArrayList References {
55 get { return references; }
58 public string SignatureLength {
59 get { return signatureLength; }
60 set { signatureLength = value; }
63 public string SignatureMethod {
64 get { return signatureMethod; }
65 set { signatureMethod = value; }
68 // documented as not supported (and throwing exception)
69 public object SyncRoot {
70 get { throw new NotSupportedException (); }
73 public void AddReference (Reference reference)
75 references.Add (reference);
78 // documented as not supported (and throwing exception)
79 public void CopyTo (Array array, int index)
81 throw new NotSupportedException ();
84 public IEnumerator GetEnumerator ()
86 return references.GetEnumerator ();
89 public XmlElement GetXml()
91 if (signatureMethod == null)
92 throw new CryptographicException ("SignatureMethod");
93 if (references.Count == 0)
94 throw new CryptographicException ("References empty");
96 XmlDocument document = new XmlDocument ();
97 XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
99 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
101 if (c14nMethod != null) {
102 XmlElement c14n = document.CreateElement (XmlSignature.ElementNames.CanonicalizationMethod, XmlSignature.NamespaceURI);
103 c14n.SetAttribute (XmlSignature.AttributeNames.Algorithm, c14nMethod);
104 xel.AppendChild (c14n);
106 if (signatureMethod != null) {
107 XmlElement sm = document.CreateElement (XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
108 sm.SetAttribute (XmlSignature.AttributeNames.Algorithm, signatureMethod);
109 if (signatureLength != null) {
110 XmlElement hmac = document.CreateElement (XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
111 hmac.InnerText = signatureLength;
112 sm.AppendChild (hmac);
114 xel.AppendChild (sm);
117 // we add References afterward so we don't end up with extraneous
118 // xmlns="..." in each reference elements.
119 foreach (Reference r in references) {
120 XmlNode xn = r.GetXml ();
121 XmlNode newNode = document.ImportNode (xn, true);
122 xel.AppendChild (newNode);
128 private string GetAttributeFromElement (XmlElement xel, string attribute, string element)
130 string result = null;
131 XmlNodeList xnl = xel.GetElementsByTagName (element);
132 if ((xnl != null) && (xnl.Count > 0)) {
133 XmlAttribute xa = xnl[0].Attributes [attribute];
135 result = xa.InnerText;
140 private string GetAttribute (XmlElement xel, string attribute)
142 XmlAttribute xa = xel.Attributes [attribute];
143 return ((xa != null) ? xa.InnerText : null);
146 [MonoTODO("signatureLength for HMAC")]
147 public void LoadXml (XmlElement value)
150 throw new ArgumentNullException ("value");
152 if ((value.LocalName != XmlSignature.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignature.NamespaceURI))
153 throw new CryptographicException ();
155 id = GetAttribute (value, XmlSignature.AttributeNames.Id);
156 c14nMethod = GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);
157 signatureMethod = GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.SignatureMethod);
158 // TODO signatureLength for HMAC
159 XmlNodeList xnl = value.GetElementsByTagName (XmlSignature.ElementNames.Reference);
160 foreach (XmlNode xn in xnl) {
161 Reference r = new Reference ();
162 r.LoadXml ((XmlElement) xn);