**** Merged from MCS ****
[mono-project.git] / mcs / class / Microsoft.Web.Services / Microsoft.Web.Services.Security / Reference.cs
blobb7ad7d745835f5599c742da6f07e24646e327f88
1 //
2 // Reference.cs - Reference 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.IO;
12 using System.Security.Cryptography;
13 using System.Xml;
15 #if (WSE1 || WSE2)
16 using System.Security.Cryptography.Xml;
18 namespace Microsoft.Web.Services.Security {
19 #else
20 namespace System.Security.Cryptography.Xml {
21 #endif
22 // http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/Overview.html#sec-Reference
23 public class Reference {
25 private TransformChain chain;
26 private string digestMethod;
27 private byte[] digestValue;
28 private string id;
29 private string uri;
30 private string type;
31 private HashAlgorithm hash;
33 public Reference ()
35 chain = new TransformChain ();
36 digestMethod = XmlSignature.NamespaceURI + "sha1";
39 [MonoTODO()]
40 public Reference (Stream stream) : this ()
44 public Reference (string uri) : this ()
46 this.uri = uri;
49 // default to SHA1
50 public string DigestMethod {
51 get { return digestMethod; }
52 set { digestMethod = value; }
55 public byte[] DigestValue {
56 get { return digestValue; }
57 set { digestValue = value; }
60 public string Id {
61 get { return id; }
62 set { id = value; }
65 public TransformChain TransformChain {
66 get { return chain; }
69 public string Type {
70 get { return type; }
71 set { type = value; }
74 public string Uri {
75 get { return uri; }
76 set { uri = value; }
79 public void AddTransform (Transform transform)
81 chain.Add (transform);
84 public XmlElement GetXml ()
86 if (digestMethod == null)
87 throw new CryptographicException ("DigestMethod");
88 if (digestValue == null)
89 throw new NullReferenceException ("DigestValue");
91 XmlDocument document = new XmlDocument ();
92 XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Reference, XmlSignature.NamespaceURI);
93 if (id != null)
94 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
95 if (uri != null)
96 xel.SetAttribute (XmlSignature.AttributeNames.URI, uri);
97 if (type != null)
98 xel.SetAttribute (XmlSignature.AttributeNames.Type, type);
100 if (chain.Count > 0) {
101 XmlElement ts = document.CreateElement (XmlSignature.ElementNames.Transforms, XmlSignature.NamespaceURI);
102 foreach (Transform t in chain) {
103 XmlNode xn = t.GetXml ();
104 XmlNode newNode = document.ImportNode (xn, true);
105 ts.AppendChild (newNode);
107 xel.AppendChild (ts);
110 XmlElement dm = document.CreateElement (XmlSignature.ElementNames.DigestMethod, XmlSignature.NamespaceURI);
111 dm.SetAttribute (XmlSignature.AttributeNames.Algorithm, digestMethod);
112 xel.AppendChild (dm);
114 XmlElement dv = document.CreateElement (XmlSignature.ElementNames.DigestValue, XmlSignature.NamespaceURI);
115 dv.InnerText = Convert.ToBase64String (digestValue);
116 xel.AppendChild (dv);
118 return xel;
121 private string GetAttributeFromElement (XmlElement xel, string attribute, string element)
123 string result = null;
124 XmlNodeList xnl = xel.GetElementsByTagName (element);
125 if ((xnl != null) && (xnl.Count > 0)) {
126 XmlAttribute xa = xnl[0].Attributes [attribute];
127 if (xa != null)
128 result = xa.InnerText;
130 return result;
133 // note: we do NOT return null -on purpose- if attribute isn't found
134 private string GetAttribute (XmlElement xel, string attribute)
136 XmlAttribute xa = xel.Attributes [attribute];
137 return ((xa != null) ? xa.InnerText : null);
140 public void LoadXml (XmlElement value)
142 if (value == null)
143 throw new ArgumentNullException ("value");
145 if ((value.LocalName != XmlSignature.ElementNames.Reference) || (value.NamespaceURI != XmlSignature.NamespaceURI))
146 throw new CryptographicException ();
148 id = GetAttribute (value, XmlSignature.AttributeNames.Id);
149 uri = GetAttribute (value, XmlSignature.AttributeNames.URI);
150 type = GetAttribute (value, XmlSignature.AttributeNames.Type);
151 // Note: order is important for validations
152 XmlNodeList xnl = value.GetElementsByTagName (XmlSignature.ElementNames.Transform);
153 if ((xnl != null) && (xnl.Count > 0)) {
154 Transform t = null;
155 foreach (XmlNode xn in xnl) {
156 string a = GetAttribute ((XmlElement)xn, XmlSignature.AttributeNames.Algorithm);
157 switch (a) {
158 case "http://www.w3.org/2000/09/xmldsig#base64":
159 t = new XmlDsigBase64Transform ();
160 break;
161 case "http://www.w3.org/TR/2001/REC-xml-c14n-20010315":
162 t = new XmlDsigC14NTransform ();
163 break;
164 case "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments":
165 t = new XmlDsigC14NWithCommentsTransform ();
166 break;
167 case "http://www.w3.org/2000/09/xmldsig#enveloped-signature":
168 t = new XmlDsigEnvelopedSignatureTransform ();
169 break;
170 case "http://www.w3.org/TR/1999/REC-xpath-19991116":
171 t = new XmlDsigXPathTransform ();
172 break;
173 case "http://www.w3.org/TR/1999/REC-xslt-19991116":
174 t = new XmlDsigXsltTransform ();
175 break;
176 default:
177 throw new NotSupportedException ();
179 AddTransform (t);
182 // get DigestMethod
183 DigestMethod = GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.DigestMethod);
184 // get DigestValue
185 xnl = value.GetElementsByTagName (XmlSignature.ElementNames.DigestValue);
186 if ((xnl != null) && (xnl.Count > 0)) {
187 DigestValue = Convert.FromBase64String (xnl[0].InnerText);