2 // EncryptedReference.cs - EncryptedReference implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-EncryptedReference
6 // Tim Coleman (tim@timcoleman.com)
8 // Copyright (C) Tim Coleman, 2004
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 namespace System.Security.Cryptography.Xml {
36 public abstract class EncryptedReference {
49 protected EncryptedReference ()
52 TransformChain = new TransformChain ();
55 protected EncryptedReference (string uri)
58 TransformChain = new TransformChain ();
61 protected EncryptedReference (string uri, TransformChain tc)
68 #endregion // Constructors
73 protected internal bool CacheValid {
74 get { return cacheValid; }
77 protected string ReferenceType {
78 get { return referenceType; }
79 set { referenceType = value; }
82 public TransformChain TransformChain {
92 #endregion // Properties
96 public void AddTransform (Transform transform)
98 TransformChain.Add (transform);
101 public virtual XmlElement GetXml ()
103 return GetXml (new XmlDocument ());
106 internal virtual XmlElement GetXml (XmlDocument document)
108 XmlElement xel = document.CreateElement (ReferenceType, EncryptedXml.XmlEncNamespaceUrl);
110 xel.SetAttribute (XmlEncryption.AttributeNames.URI, Uri);
112 if (TransformChain != null && TransformChain.Count > 0) {
113 XmlElement xtr = document.CreateElement (XmlEncryption.ElementNames.Transforms, EncryptedXml.XmlEncNamespaceUrl);
114 foreach (Transform t in TransformChain)
115 xtr.AppendChild (document.ImportNode (t.GetXml (), true));
116 xel.AppendChild (xtr);
122 [MonoTODO ("Make compliant.")]
123 public virtual void LoadXml (XmlElement value)
126 throw new ArgumentNullException ("value");
127 if ((value.LocalName != XmlEncryption.ElementNames.CipherReference) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
128 throw new CryptographicException ("Malformed CipherReference element.");
131 TransformChain = new TransformChain ();
133 foreach (XmlNode n in value.ChildNodes) {
134 if (n is XmlWhitespace)
137 switch (n.LocalName) {
138 case XmlEncryption.ElementNames.Transforms:
139 foreach (XmlNode xn in ((XmlElement) n).GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI)) {
141 switch (((XmlElement) xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value) {
142 case XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform:
143 t = new XmlDsigBase64Transform ();
145 case XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform:
146 t = new XmlDsigC14NTransform ();
148 case XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform:
149 t = new XmlDsigC14NWithCommentsTransform ();
151 case XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform:
152 t = new XmlDsigEnvelopedSignatureTransform ();
154 case XmlSignature.AlgorithmNamespaces.XmlDsigXPathTransform:
155 t = new XmlDsigXPathTransform ();
157 case XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform:
158 t = new XmlDsigXsltTransform ();
164 t.LoadInnerXml (((XmlElement) xn).ChildNodes);
165 TransformChain.Add (t);
171 if (value.HasAttribute (XmlEncryption.AttributeNames.URI))
172 Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value;
176 #endregion // Methods