[runtime] Remove handler block trampoline code
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / EncryptedReference.cs
1 //
2 // EncryptedReference.cs - EncryptedReference implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-EncryptedReference
4 //
5 // Author:
6 //      Tim Coleman (tim@timcoleman.com)
7 //
8 // Copyright (C) Tim Coleman, 2004
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31
32 using System.Xml;
33
34 namespace System.Security.Cryptography.Xml {
35         public abstract class EncryptedReference {
36
37                 #region Fields
38
39                 string referenceType;
40                 string uri;
41                 TransformChain tc;
42
43                 #endregion // Fields
44
45                 #region Constructors
46
47                 protected EncryptedReference ()
48                 {
49                         TransformChain = new TransformChain ();
50                 }
51         
52                 protected EncryptedReference (string uri)
53                 {
54                         Uri = uri;
55                         TransformChain = new TransformChain ();
56                 }
57         
58                 protected EncryptedReference (string uri, TransformChain transformChain)
59                         : this ()
60                 {
61                         Uri = uri;
62                         TransformChain = transformChain;
63                 }
64         
65                 #endregion // Constructors
66
67                 #region Properties
68
69                 [MonoTODO("Always returns false")]
70                 protected internal bool CacheValid {
71                         get { return false; }
72                 }
73
74                 protected string ReferenceType {
75                         get { return referenceType; }
76                         set { referenceType = value; }
77                 }
78
79                 public TransformChain TransformChain {
80                         get { return tc; }
81                         set { tc = value; }
82                 }
83
84                 public string Uri {
85                         get { return uri; }
86                         set { uri = value; }
87                 }
88
89                 #endregion // Properties
90         
91                 #region Methods
92
93                 public void AddTransform (Transform transform)
94                 {
95                         TransformChain.Add (transform);
96                 }
97
98                 public virtual XmlElement GetXml ()
99                 {
100                         return GetXml (new XmlDocument ());
101                 }
102
103                 internal virtual XmlElement GetXml (XmlDocument document)
104                 {
105                         XmlElement xel = document.CreateElement (ReferenceType, EncryptedXml.XmlEncNamespaceUrl);
106
107                         xel.SetAttribute (XmlEncryption.AttributeNames.URI, Uri);
108
109                         if (TransformChain != null && TransformChain.Count > 0) {
110                                 XmlElement xtr = document.CreateElement (XmlEncryption.ElementNames.Transforms, EncryptedXml.XmlEncNamespaceUrl);
111                                 foreach (Transform t in TransformChain)
112                                         xtr.AppendChild (document.ImportNode (t.GetXml (), true));
113                                 xel.AppendChild (xtr);
114                         }
115
116                         return xel;
117                 }
118
119                 [MonoTODO ("Make compliant.")]
120                 public virtual void LoadXml (XmlElement value)
121                 {
122                         if (value == null)
123                                 throw new ArgumentNullException ("value");
124                         else {
125                                 Uri = null;
126                                 TransformChain = new TransformChain ();
127
128                                 foreach (XmlNode n in value.ChildNodes) {
129                                         if (n is XmlWhitespace)
130                                                 continue;
131
132                                         switch (n.LocalName) {
133                                         case XmlEncryption.ElementNames.Transforms:
134                                                 foreach (XmlNode xn in ((XmlElement) n).GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI)) {
135                                                         Transform t = null;
136                                                         switch (((XmlElement) xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value) {
137                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform:
138                                                                 t = new XmlDsigBase64Transform ();
139                                                                 break;
140                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform:
141                                                                 t = new XmlDsigC14NTransform ();
142                                                                 break;
143                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform:
144                                                                 t = new XmlDsigC14NWithCommentsTransform ();
145                                                                 break;
146                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform:
147                                                                 t = new XmlDsigEnvelopedSignatureTransform ();
148                                                                 break;
149                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigXPathTransform:
150                                                                 t = new XmlDsigXPathTransform ();
151                                                                 break;
152                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform:
153                                                                 t = new XmlDsigXsltTransform ();
154                                                                 break;
155                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NTransform:
156                                                                 t = new XmlDsigExcC14NTransform ();
157                                                                 break;
158                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NWithCommentsTransform:
159                                                                 t = new XmlDsigExcC14NWithCommentsTransform ();
160                                                                 break;
161                                                         case XmlSignature.AlgorithmNamespaces.XmlDecryptionTransform:
162                                                                 t = new XmlDecryptionTransform ();
163                                                                 break;
164                                                         default:
165                                                                 continue;
166                                                         }
167
168                                                         t.LoadInnerXml (((XmlElement) xn).ChildNodes);
169                                                         TransformChain.Add (t);
170                                                 }
171                                                 break;
172                                         }
173                                 }
174
175                                 if (value.HasAttribute (XmlEncryption.AttributeNames.URI))
176                                         Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value;
177                         }
178                 }
179
180                 #endregion // Methods
181         }
182 }
183