2004-02-07 Tim Coleman <tim@timcoleman.com>
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / EncryptedData.cs
1 //
2 // EncryptedData.cs - EncryptedData implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-EncryptedData
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 EncryptedData : EncryptedType {
16
17                 #region Constructors
18
19                 public EncryptedData ()
20                         : base ()
21                 {
22                 }
23
24                 #endregion // Constructors
25
26                 #region Methods
27
28                 public override XmlElement GetXml ()
29                 {
30                         return GetXml (new XmlDocument ());
31                 }
32
33                 internal XmlElement GetXml (XmlDocument document)
34                 {
35                         if (CipherData == null)
36                                 throw new CryptographicException ("Cipher data is not specified.");
37
38                         XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedData, XmlEncryption.NamespaceURI);
39
40                         if (EncryptionMethod != null)
41                                 xel.AppendChild (EncryptionMethod.GetXml (document));
42                         if (KeyInfo != null) 
43                                 xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
44                         if (CipherData != null)
45                                 xel.AppendChild (CipherData.GetXml (document));
46
47                         if (EncryptionProperties.Count > 0) {
48                                 XmlElement xep = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperties, XmlEncryption.NamespaceURI);
49                                 foreach (EncryptionProperty p in EncryptionProperties)
50                                         xep.AppendChild (p.GetXml (document));
51                                 xel.AppendChild (xep);
52                         }
53
54                         if (Id != null)
55                                 xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
56                         if (Type != null)
57                                 xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
58                         if (MimeType != null)
59                                 xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
60                         if (Encoding != null)
61                                 xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
62                         return xel;
63                 }
64
65                 public override void LoadXml (XmlElement value)
66                 {
67                         if (value == null)
68                                 throw new ArgumentNullException ("value");
69
70                         if ((value.LocalName != XmlEncryption.ElementNames.EncryptedData) || (value.NamespaceURI != XmlEncryption.NamespaceURI))
71                                 throw new CryptographicException ("Malformed EncryptedData element.");
72                         else {
73                                 EncryptionMethod = null;
74                                 KeyInfo keyInfo = null;
75                                 CipherData cipherData = null;
76                                 EncryptionMethod = null;
77                                 EncryptionProperties = new EncryptionProperties ();
78                                 Id = null;
79                                 Type = null;
80                                 MimeType = null;
81                                 Encoding = null;
82
83                                 foreach (XmlNode n in value.ChildNodes) {
84                                         if (n is XmlWhitespace)
85                                                 continue;
86
87                                         switch (n.LocalName) {
88                                         case XmlEncryption.ElementNames.EncryptionMethod:
89                                                 EncryptionMethod = new EncryptionMethod ();
90                                                 EncryptionMethod.LoadXml ((XmlElement) n);
91                                                 break;
92                                         case XmlSignature.ElementNames.KeyInfo:
93                                                 KeyInfo = new KeyInfo ();
94                                                 KeyInfo.LoadXml ((XmlElement) n);
95                                                 break;
96                                         case XmlEncryption.ElementNames.CipherData:
97                                                 CipherData = new CipherData ();
98                                                 CipherData.LoadXml ((XmlElement) n);
99                                                 break;
100                                         case XmlEncryption.ElementNames.EncryptionProperties:
101                                                 foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, XmlEncryption.NamespaceURI))
102                                                         EncryptionProperties.Add (new EncryptionProperty (element));
103                                                 break;
104                                         }
105                                 }
106
107                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
108                                         Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
109                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
110                                         Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
111                                 if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
112                                         MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
113                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
114                                         Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
115                         }
116                 }
117
118                 #endregion // Methods
119         }
120 }
121
122 #endif