2004-02-07 Tim Coleman <tim@timcoleman.com>
[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 #if NET_1_2
11
12 using System.Security.Cryptography;
13 using System.IO;
14 using System.Xml;
15
16 namespace System.Security.Cryptography.Xml {
17         public sealed class CipherData {
18
19                 #region Fields
20
21                 byte[] cipherValue;
22                 CipherReference cipherReference;
23         
24                 #endregion // Fields
25         
26                 #region Constructors
27         
28                 public CipherData ()
29                 {
30                 }
31         
32                 public CipherData (byte[] cipherValue)
33                 {
34                         CipherValue = cipherValue;
35                 }
36         
37                 public CipherData (CipherReference cipherReference)
38                 {
39                         CipherReference = cipherReference;
40                 }
41         
42                 #endregion // Constructors
43         
44                 #region Properties
45         
46                 public CipherReference CipherReference {
47                         get { return cipherReference; }
48                         set { 
49                                 if (CipherValue != null)
50                                         throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
51                                 cipherReference = value;
52                         }
53                 }
54         
55                 public byte[] CipherValue {
56                         get { return cipherValue; }
57                         set {
58                                 if (CipherReference != null)
59                                         throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
60                                 cipherValue = value;
61                         }
62                 }
63
64                 #endregion // Properties
65
66                 #region Methods
67
68                 public XmlElement GetXml ()
69                 {
70                         return GetXml (new XmlDocument ());
71                 }
72
73                 internal XmlElement GetXml (XmlDocument document)
74                 {
75                         if (CipherReference == null && CipherValue == null)
76                                 throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
77
78                         XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.CipherData, XmlEncryption.NamespaceURI);
79                         if (CipherReference != null) 
80                                 xel.AppendChild (document.ImportNode (cipherReference.GetXml (), true));
81
82                         if (CipherValue != null) {
83                                 XmlElement xcv = document.CreateElement (XmlEncryption.ElementNames.CipherValue, XmlEncryption.NamespaceURI);
84                                 StreamReader reader = new StreamReader (new CryptoStream (new MemoryStream (cipherValue), new ToBase64Transform (), CryptoStreamMode.Read));
85                                 xcv.InnerText = reader.ReadToEnd ();
86                                 reader.Close ();
87                                 xel.AppendChild (xcv);
88                         }
89                         return xel;
90                 }
91
92                 public void LoadXml (XmlElement value)
93                 {
94                         CipherReference = null;
95                         CipherValue = null;
96
97                         if (value == null)
98                                 throw new ArgumentNullException ("value");
99
100                         if ((value.LocalName != XmlEncryption.ElementNames.CipherData) || (value.NamespaceURI != XmlEncryption.NamespaceURI)) 
101                                 throw new CryptographicException ("Malformed Cipher Data element.");
102                         else {
103                                 foreach (XmlNode n in value.ChildNodes) {
104                                         if (n is XmlWhitespace)
105                                                 continue;
106
107                                         switch (n.LocalName) {
108                                         case XmlEncryption.ElementNames.CipherReference:
109                                                 cipherReference = new CipherReference ();
110                                                 cipherReference.LoadXml ((XmlElement) n);
111                                                 break;
112                                         case XmlEncryption.ElementNames.CipherValue:
113                                                 CipherValue = Convert.FromBase64String (n.InnerText);
114                                                 break;
115                                         }
116                                 }
117
118                                 if (CipherReference == null && CipherValue == null)
119                                         throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
120                         }
121                 }
122
123                 #endregion // Methods
124         }
125 }
126
127 #endif