2005-04-12 Dick Porter <dick@ximian.com>
[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 #if NET_2_0
32
33 using System.Xml;
34
35 namespace System.Security.Cryptography.Xml {
36         public abstract class EncryptedReference {
37
38                 #region Fields
39
40                 bool cacheValid;
41                 string referenceType;
42                 string uri;
43                 TransformChain tc;
44
45                 #endregion // Fields
46
47                 #region Constructors
48
49                 protected EncryptedReference ()
50                 {
51                         uri = null;
52                         TransformChain = new TransformChain ();
53                 }
54         
55                 protected EncryptedReference (string uri)
56                 {
57                         Uri = uri;
58                         TransformChain = new TransformChain ();
59                 }
60         
61                 protected EncryptedReference (string uri, TransformChain tc)
62                         : this ()
63                 {
64                         Uri = uri;
65                         TransformChain = tc;
66                 }
67         
68                 #endregion // Constructors
69
70                 #region Properties
71
72                 [MonoTODO()]
73                 protected internal bool CacheValid {
74                         get { return cacheValid; }
75                 }
76
77                 protected string ReferenceType {
78                         get { return referenceType; }
79                         set { referenceType = value; }
80                 }
81
82                 public TransformChain TransformChain {
83                         get { return tc; }
84                         set { tc = value; }
85                 }
86
87                 public string Uri {
88                         get { return uri; }
89                         set { uri = value; }
90                 }
91
92                 #endregion // Properties
93         
94                 #region Methods
95
96                 public void AddTransform (Transform transform)
97                 {
98                         TransformChain.Add (transform);
99                 }
100
101                 public virtual XmlElement GetXml ()
102                 {
103                         return GetXml (new XmlDocument ());
104                 }
105
106                 internal virtual XmlElement GetXml (XmlDocument document)
107                 {
108                         XmlElement xel = document.CreateElement (ReferenceType, EncryptedXml.XmlEncNamespaceUrl);
109
110                         xel.SetAttribute (XmlEncryption.AttributeNames.URI, Uri);
111
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);
117                         }
118
119                         return xel;
120                 }
121
122                 [MonoTODO ("Make compliant.")]
123                 public virtual void LoadXml (XmlElement value)
124                 {
125                         if (value == null)
126                                 throw new ArgumentNullException ("value");
127                         if ((value.LocalName != XmlEncryption.ElementNames.CipherReference) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
128                                 throw new CryptographicException ("Malformed CipherReference element.");
129                         else {
130                                 Uri = null;
131                                 TransformChain = new TransformChain ();
132
133                                 foreach (XmlNode n in value.ChildNodes) {
134                                         if (n is XmlWhitespace)
135                                                 continue;
136
137                                         switch (n.LocalName) {
138                                         case XmlEncryption.ElementNames.Transforms:
139                                                 foreach (XmlNode xn in ((XmlElement) n).GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI)) {
140                                                         Transform t = null;
141                                                         switch (((XmlElement) xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value) {
142                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform:
143                                                                 t = new XmlDsigBase64Transform ();
144                                                                 break;
145                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform:
146                                                                 t = new XmlDsigC14NTransform ();
147                                                                 break;
148                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform:
149                                                                 t = new XmlDsigC14NWithCommentsTransform ();
150                                                                 break;
151                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform:
152                                                                 t = new XmlDsigEnvelopedSignatureTransform ();
153                                                                 break;
154                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigXPathTransform:
155                                                                 t = new XmlDsigXPathTransform ();
156                                                                 break;
157                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform:
158                                                                 t = new XmlDsigXsltTransform ();
159                                                                 break;
160                                                         default:
161                                                                 continue;
162                                                         }
163
164                                                         t.LoadInnerXml (((XmlElement) xn).ChildNodes);
165                                                         TransformChain.Add (t);
166                                                 }
167                                                 break;
168                                         }
169                                 }
170
171                                 if (value.HasAttribute (XmlEncryption.AttributeNames.URI))
172                                         Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value;
173                         }
174                 }
175
176                 #endregion // Methods
177         }
178 }
179
180 #endif