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