2004-04-22 Martin Baulig <martin@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 #if NET_2_0
11
12 using System.Xml;
13
14 namespace System.Security.Cryptography.Xml {
15         public abstract class EncryptedReference {
16
17                 #region Fields
18
19                 bool cacheValid;
20                 string referenceType;
21                 string uri;
22                 TransformChain tc;
23
24                 #endregion // Fields
25
26                 #region Constructors
27
28                 protected EncryptedReference ()
29                 {
30                         uri = null;
31                         TransformChain = new TransformChain ();
32                 }
33         
34                 protected EncryptedReference (string uri)
35                 {
36                         Uri = uri;
37                         TransformChain = new TransformChain ();
38                 }
39         
40                 protected EncryptedReference (string uri, TransformChain tc)
41                         : this ()
42                 {
43                         Uri = uri;
44                         TransformChain = tc;
45                 }
46         
47                 #endregion // Constructors
48
49                 #region Properties
50
51                 [MonoTODO()]
52                 protected internal bool CacheValid {
53                         get { return cacheValid; }
54                 }
55
56                 protected string ReferenceType {
57                         get { return referenceType; }
58                         set { referenceType = value; }
59                 }
60
61                 public TransformChain TransformChain {
62                         get { return tc; }
63                         set { tc = value; }
64                 }
65
66                 public string Uri {
67                         get { return uri; }
68                         set { uri = value; }
69                 }
70
71                 #endregion // Properties
72         
73                 #region Methods
74
75                 public void AddTransform (Transform transform)
76                 {
77                         TransformChain.Add (transform);
78                 }
79
80                 public virtual XmlElement GetXml ()
81                 {
82                         return GetXml (new XmlDocument ());
83                 }
84
85                 internal virtual XmlElement GetXml (XmlDocument document)
86                 {
87                         XmlElement xel = document.CreateElement (ReferenceType, EncryptedXml.XmlEncNamespaceUrl);
88
89                         xel.SetAttribute (XmlEncryption.AttributeNames.URI, Uri);
90
91                         if (TransformChain != null && TransformChain.Count > 0) {
92                                 XmlElement xtr = document.CreateElement (XmlEncryption.ElementNames.Transforms, EncryptedXml.XmlEncNamespaceUrl);
93                                 foreach (Transform t in TransformChain)
94                                         xtr.AppendChild (document.ImportNode (t.GetXml (), true));
95                                 xel.AppendChild (xtr);
96                         }
97
98                         return xel;
99                 }
100
101                 [MonoTODO ("Make compliant.")]
102                 public virtual void LoadXml (XmlElement value)
103                 {
104                         if (value == null)
105                                 throw new ArgumentNullException ("value");
106                         if ((value.LocalName != XmlEncryption.ElementNames.CipherReference) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
107                                 throw new CryptographicException ("Malformed CipherReference element.");
108                         else {
109                                 Uri = null;
110                                 TransformChain = new TransformChain ();
111
112                                 foreach (XmlNode n in value.ChildNodes) {
113                                         if (n is XmlWhitespace)
114                                                 continue;
115
116                                         switch (n.LocalName) {
117                                         case XmlEncryption.ElementNames.Transforms:
118                                                 foreach (XmlNode xn in ((XmlElement) n).GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI)) {
119                                                         Transform t = null;
120                                                         switch (((XmlElement) xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value) {
121                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform:
122                                                                 t = new XmlDsigBase64Transform ();
123                                                                 break;
124                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform:
125                                                                 t = new XmlDsigC14NTransform ();
126                                                                 break;
127                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform:
128                                                                 t = new XmlDsigC14NWithCommentsTransform ();
129                                                                 break;
130                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform:
131                                                                 t = new XmlDsigEnvelopedSignatureTransform ();
132                                                                 break;
133                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigXPathTransform:
134                                                                 t = new XmlDsigXPathTransform ();
135                                                                 break;
136                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform:
137                                                                 t = new XmlDsigXsltTransform ();
138                                                                 break;
139                                                         default:
140                                                                 continue;
141                                                         }
142
143                                                         t.LoadInnerXml (((XmlElement) xn).ChildNodes);
144                                                         TransformChain.Add (t);
145                                                 }
146                                                 break;
147                                         }
148                                 }
149
150                                 if (value.HasAttribute (XmlEncryption.AttributeNames.URI))
151                                         Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value;
152                         }
153                 }
154
155                 #endregion // Methods
156         }
157 }
158
159 #endif