(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Microsoft.Web.Services / Microsoft.Web.Services.Security / SignedInfo.cs
blobb3e21d8e6decb971a8896fdcfe71977a166b22a2
1 //
2 // SignedInfo.cs - SignedInfo implementation for XML Signature
3 //
4 // Author:
5 // Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
10 using System;
11 using System.Collections;
12 using System.Xml;
14 #if (WSE1 || WSE2)
15 using System.Security.Cryptography;
16 using System.Security.Cryptography.Xml;
18 namespace Microsoft.Web.Services.Security {
19 #else
20 namespace System.Security.Cryptography.Xml {
21 #endif
22 public class SignedInfo : ICollection, IEnumerable {
24 private ArrayList references;
25 private string c14nMethod;
26 private string id;
27 private string signatureMethod;
28 private string signatureLength;
30 public SignedInfo()
32 references = new ArrayList ();
33 c14nMethod = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
36 public string CanonicalizationMethod {
37 get { return c14nMethod; }
38 set { c14nMethod = value; }
41 // documented as not supported (and throwing exception)
42 public int Count {
43 get { throw new NotSupportedException (); }
46 public string Id {
47 get { return id; }
48 set { id = value; }
51 // documented as not supported (and throwing exception)
52 public bool IsReadOnly {
53 get { throw new NotSupportedException (); }
56 // documented as not supported (and throwing exception)
57 public bool IsSynchronized {
58 get { throw new NotSupportedException (); }
61 public ArrayList References {
62 get { return references; }
65 public string SignatureLength {
66 get { return signatureLength; }
67 set { signatureLength = value; }
70 public string SignatureMethod {
71 get { return signatureMethod; }
72 set { signatureMethod = value; }
75 // documented as not supported (and throwing exception)
76 public object SyncRoot {
77 get { throw new NotSupportedException (); }
80 public void AddReference (Reference reference)
82 references.Add (reference);
85 // documented as not supported (and throwing exception)
86 public void CopyTo (Array array, int index)
88 throw new NotSupportedException ();
91 public IEnumerator GetEnumerator ()
93 return references.GetEnumerator ();
96 public XmlElement GetXml()
98 if (signatureMethod == null)
99 throw new CryptographicException ("SignatureMethod");
100 if (references.Count == 0)
101 throw new CryptographicException ("References empty");
103 XmlDocument document = new XmlDocument ();
104 XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
105 if (id != null)
106 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
108 if (c14nMethod != null) {
109 XmlElement c14n = document.CreateElement (XmlSignature.ElementNames.CanonicalizationMethod, XmlSignature.NamespaceURI);
110 c14n.SetAttribute (XmlSignature.AttributeNames.Algorithm, c14nMethod);
111 xel.AppendChild (c14n);
113 if (signatureMethod != null) {
114 XmlElement sm = document.CreateElement (XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
115 sm.SetAttribute (XmlSignature.AttributeNames.Algorithm, signatureMethod);
116 if (signatureLength != null) {
117 XmlElement hmac = document.CreateElement (XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
118 hmac.InnerText = signatureLength;
119 sm.AppendChild (hmac);
121 xel.AppendChild (sm);
124 // we add References afterward so we don't end up with extraneous
125 // xmlns="..." in each reference elements.
126 foreach (Reference r in references) {
127 XmlNode xn = r.GetXml ();
128 XmlNode newNode = document.ImportNode (xn, true);
129 xel.AppendChild (newNode);
132 return xel;
135 private string GetAttributeFromElement (XmlElement xel, string attribute, string element)
137 string result = null;
138 XmlNodeList xnl = xel.GetElementsByTagName (element);
139 if ((xnl != null) && (xnl.Count > 0)) {
140 XmlAttribute xa = xnl[0].Attributes [attribute];
141 if (xa != null)
142 result = xa.InnerText;
144 return result;
147 private string GetAttribute (XmlElement xel, string attribute)
149 XmlAttribute xa = xel.Attributes [attribute];
150 return ((xa != null) ? xa.InnerText : null);
153 [MonoTODO("signatureLength for HMAC")]
154 public void LoadXml (XmlElement value)
156 if (value == null)
157 throw new ArgumentNullException ("value");
159 if ((value.LocalName != XmlSignature.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignature.NamespaceURI))
160 throw new CryptographicException ();
162 id = GetAttribute (value, XmlSignature.AttributeNames.Id);
163 c14nMethod = GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);
164 signatureMethod = GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.SignatureMethod);
165 // TODO signatureLength for HMAC
166 XmlNodeList xnl = value.GetElementsByTagName (XmlSignature.ElementNames.Reference);
167 foreach (XmlNode xn in xnl) {
168 Reference r = new Reference ();
169 r.LoadXml ((XmlElement) xn);
170 AddReference (r);