2004-03-16 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / CipherReference.cs
1 //
2 // CipherReference.cs - CipherReference implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-CipherReference
4 //
5 // Author:
6 //      Tim Coleman (tim@timcoleman.com)
7 //
8 // Copyright (C) Tim Coleman, 2004
9
10 #if NET_1_2
11
12 using System.Xml;
13
14 namespace System.Security.Cryptography.Xml {
15         public sealed class CipherReference : EncryptedReference {
16
17                 #region Constructors
18         
19                 public CipherReference ()
20                         : base ()
21                 {
22                 }
23         
24                 public CipherReference (string uri)
25                         : base (uri)
26                 {
27                 }
28         
29                 public CipherReference (string uri, TransformChain tc)
30                         : base (uri, tc)
31                 {
32                 }
33         
34                 #endregion // Constructors
35         
36                 #region Methods
37
38                 public override XmlElement GetXml ()
39                 {
40                         return GetXml (new XmlDocument ());
41                 }
42
43                 internal override XmlElement GetXml (XmlDocument document)
44                 {
45                         XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.CipherReference, EncryptedXml.XmlEncNamespaceUrl);
46
47                         xel.SetAttribute (XmlEncryption.AttributeNames.URI, Uri);
48
49                         if (TransformChain != null && TransformChain.Count > 0) {
50                                 XmlElement xtr = document.CreateElement (XmlEncryption.ElementNames.Transforms, EncryptedXml.XmlEncNamespaceUrl);
51                                 foreach (Transform t in TransformChain) 
52                                         xtr.AppendChild (document.ImportNode (t.GetXml (), true));
53                                 xel.AppendChild (xtr);
54                         }
55
56                         return xel;
57                 }
58
59                 public override void LoadXml (XmlElement value)
60                 {
61                         if (value == null)
62                                 throw new ArgumentNullException ("value");
63                         if ((value.LocalName != XmlEncryption.ElementNames.CipherReference) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
64                                 throw new CryptographicException ("Malformed CipherReference element.");
65                         else {
66                                 Uri = null;
67                                 TransformChain = new TransformChain ();
68
69                                 foreach (XmlNode n in value.ChildNodes) {
70                                         if (n is XmlWhitespace)
71                                                 continue;
72
73                                         switch (n.LocalName) {
74                                         case XmlEncryption.ElementNames.Transforms:
75                                                 foreach (XmlNode xn in ((XmlElement) n).GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI)) {
76                                                         Transform t = null;
77                                                         switch (((XmlElement) xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value) {
78                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform:
79                                                                 t = new XmlDsigBase64Transform ();
80                                                                 break;
81                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform:
82                                                                 t = new XmlDsigC14NTransform ();
83                                                                 break;
84                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform:
85                                                                 t = new XmlDsigC14NWithCommentsTransform ();
86                                                                 break;
87                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform:
88                                                                 t = new XmlDsigEnvelopedSignatureTransform ();
89                                                                 break;
90                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigXPathTransform:
91                                                                 t = new XmlDsigXPathTransform ();
92                                                                 break;
93                                                         case XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform:
94                                                                 t = new XmlDsigXsltTransform ();
95                                                                 break;
96                                                         default:
97                                                                 continue;
98                                                         }
99
100                                                         t.LoadInnerXml (((XmlElement) xn).ChildNodes);
101                                                         TransformChain.Add (t);
102                                                 }
103                                                 break;
104                                         }
105                                 }
106                                                 
107                                 if (value.HasAttribute (XmlEncryption.AttributeNames.URI))
108                                         Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value;
109                         }
110                 }
111
112                 #endregion // Methods
113         }
114 }
115
116 #endif