Add full-scratch expression parser implementation.
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / CipherData.cs
1 //
2 // CipherData.cs - CipherData implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-CipherData
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.Security.Cryptography;
34 using System.IO;
35 using System.Xml;
36
37 namespace System.Security.Cryptography.Xml {
38         public sealed class CipherData {
39
40                 #region Fields
41
42                 byte[] cipherValue;
43                 CipherReference cipherReference;
44         
45                 #endregion // Fields
46         
47                 #region Constructors
48         
49                 public CipherData ()
50                 {
51                 }
52         
53                 public CipherData (byte[] cipherValue)
54                 {
55                         CipherValue = cipherValue;
56                 }
57         
58                 public CipherData (CipherReference cipherReference)
59                 {
60                         CipherReference = cipherReference;
61                 }
62         
63                 #endregion // Constructors
64         
65                 #region Properties
66         
67                 public CipherReference CipherReference {
68                         get { return cipherReference; }
69                         set { 
70                                 if (CipherValue != null)
71                                         throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
72                                 cipherReference = value;
73                         }
74                 }
75         
76                 public byte[] CipherValue {
77                         get { return cipherValue; }
78                         set {
79                                 if (CipherReference != null)
80                                         throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
81                                 cipherValue = value;
82                         }
83                 }
84
85                 #endregion // Properties
86
87                 #region Methods
88
89                 public XmlElement GetXml ()
90                 {
91                         return GetXml (new XmlDocument ());
92                 }
93
94                 internal XmlElement GetXml (XmlDocument document)
95                 {
96                         if (CipherReference == null && CipherValue == null)
97                                 throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
98
99                         XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.CipherData, EncryptedXml.XmlEncNamespaceUrl);
100                         if (CipherReference != null) 
101                                 xel.AppendChild (document.ImportNode (cipherReference.GetXml (), true));
102
103                         if (CipherValue != null) {
104                                 XmlElement xcv = document.CreateElement (XmlEncryption.ElementNames.CipherValue, EncryptedXml.XmlEncNamespaceUrl);
105                                 StreamReader reader = new StreamReader (new CryptoStream (new MemoryStream (cipherValue), new ToBase64Transform (), CryptoStreamMode.Read));
106                                 xcv.InnerText = reader.ReadToEnd ();
107                                 reader.Close ();
108                                 xel.AppendChild (xcv);
109                         }
110                         return xel;
111                 }
112
113                 public void LoadXml (XmlElement value)
114                 {
115                         CipherReference = null;
116                         CipherValue = null;
117
118                         if (value == null)
119                                 throw new ArgumentNullException ("value");
120
121                         if ((value.LocalName != XmlEncryption.ElementNames.CipherData) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl)) 
122                                 throw new CryptographicException ("Malformed Cipher Data element.");
123                         else {
124                                 foreach (XmlNode n in value.ChildNodes) {
125                                         if (n is XmlWhitespace)
126                                                 continue;
127
128                                         switch (n.LocalName) {
129                                         case XmlEncryption.ElementNames.CipherReference:
130                                                 cipherReference = new CipherReference ();
131                                                 cipherReference.LoadXml ((XmlElement) n);
132                                                 break;
133                                         case XmlEncryption.ElementNames.CipherValue:
134                                                 CipherValue = Convert.FromBase64String (n.InnerText);
135                                                 break;
136                                         }
137                                 }
138
139                                 if (CipherReference == null && CipherValue == null)
140                                         throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
141                         }
142                 }
143
144                 #endregion // Methods
145         }
146 }
147
148 #endif