2004-03-20 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / EncryptedKey.cs
1 //
2 // EncryptedKey.cs - EncryptedKey implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-EncryptedKey
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 EncryptedKey : EncryptedType {
16
17                 #region Fields
18
19                 string carriedKeyName;
20                 string recipient;
21                 ReferenceList referenceList;
22
23                 #endregion // Fields
24
25                 #region Constructors
26
27                 public EncryptedKey ()
28                         : base ()
29                 {
30                         CarriedKeyName = null;
31                         Recipient = null;
32                         ReferenceList = new ReferenceList ();
33                 }
34
35                 #endregion // Constructors
36
37                 #region Properties
38
39                 public string CarriedKeyName {
40                         get { return carriedKeyName; }
41                         set { carriedKeyName = value; }
42                 }
43
44                 public string Recipient {
45                         get { return recipient; }
46                         set { recipient = value; }
47                 }
48
49                 public ReferenceList ReferenceList {
50                         get { return referenceList; }
51                         set { referenceList = value; }
52                 }
53
54                 #endregion // Properties
55
56                 #region Methods
57
58                 public void AddReference (DataReference dataReference)
59                 {
60                         ReferenceList.Add (dataReference);
61                 }
62
63                 public void AddReference (KeyReference keyReference)
64                 {
65                         ReferenceList.Add (keyReference);
66                 }
67
68                 public override XmlElement GetXml ()
69                 {
70                         return GetXml (new XmlDocument ());
71                 }
72
73                 internal XmlElement GetXml (XmlDocument document)
74                 {
75                         if (CipherData == null)
76                                 throw new CryptographicException ("Cipher data is not specified.");
77
78                         XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedData, EncryptedXml.XmlEncNamespaceUrl);
79
80                         if (EncryptionMethod != null)
81                                 xel.AppendChild (EncryptionMethod.GetXml (document));
82                         if (KeyInfo != null) 
83                                 xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
84                         if (CipherData != null)
85                                 xel.AppendChild (CipherData.GetXml (document));
86
87                         if (EncryptionProperties.Count > 0) {
88                                 XmlElement xep = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperties, EncryptedXml.XmlEncNamespaceUrl);
89                                 foreach (EncryptionProperty p in EncryptionProperties)
90                                         xep.AppendChild (p.GetXml (document));
91                                 xel.AppendChild (xep);
92                         }
93
94                         if (ReferenceList.Count > 0) {
95                                 XmlElement xrl = document.CreateElement (XmlEncryption.ElementNames.ReferenceList, EncryptedXml.XmlEncNamespaceUrl);
96                                 foreach (EncryptedReference er in ReferenceList) 
97                                         xrl.AppendChild (er.GetXml (document));
98                                 xel.AppendChild (xrl);
99                         }
100
101                         if (CarriedKeyName != null) {
102                                 XmlElement xck = document.CreateElement (XmlEncryption.ElementNames.CarriedKeyName, EncryptedXml.XmlEncNamespaceUrl);
103                                 xck.InnerText = CarriedKeyName;
104                                 xel.AppendChild (xck);
105                         }
106
107                         if (Id != null)
108                                 xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
109                         if (Type != null)
110                                 xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
111                         if (MimeType != null)
112                                 xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
113                         if (Encoding != null)
114                                 xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
115                         if (Recipient != null)
116                                 xel.SetAttribute (XmlEncryption.AttributeNames.Recipient, Recipient);
117                         return xel;
118                 }
119
120                 public override void LoadXml (XmlElement value)
121                 {
122                         if (value == null)
123                                 throw new ArgumentNullException ("value");
124
125                         if ((value.LocalName != XmlEncryption.ElementNames.EncryptedKey) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
126                                 throw new CryptographicException ("Malformed EncryptedKey element.");
127                         else {
128                                 EncryptionMethod = null;
129                                 KeyInfo keyInfo = null;
130                                 CipherData cipherData = null;
131                                 EncryptionMethod = null;
132                                 EncryptionProperties = new EncryptionProperties ();
133                                 ReferenceList = new ReferenceList ();
134                                 CarriedKeyName = null;
135                                 Id = null;
136                                 Type = null;
137                                 MimeType = null;
138                                 Encoding = null;
139                                 Recipient = null;
140
141                                 foreach (XmlNode n in value.ChildNodes) {
142                                         if (n is XmlWhitespace)
143                                                 continue;
144
145                                         switch (n.LocalName) {
146                                         case XmlEncryption.ElementNames.EncryptionMethod:
147                                                 EncryptionMethod = new EncryptionMethod ();
148                                                 EncryptionMethod.LoadXml ((XmlElement) n);
149                                                 break;
150                                         case XmlSignature.ElementNames.KeyInfo:
151                                                 KeyInfo = new KeyInfo ();
152                                                 KeyInfo.LoadXml ((XmlElement) n);
153                                                 break;
154                                         case XmlEncryption.ElementNames.CipherData:
155                                                 CipherData = new CipherData ();
156                                                 CipherData.LoadXml ((XmlElement) n);
157                                                 break;
158                                         case XmlEncryption.ElementNames.EncryptionProperties:
159                                                 foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
160                                                         EncryptionProperties.Add (new EncryptionProperty (element));
161                                                 break;
162                                         case XmlEncryption.ElementNames.ReferenceList:
163                                                 foreach (XmlNode r in ((XmlElement) n).ChildNodes) {
164                                                         if (r is XmlWhitespace) 
165                                                                 continue;
166
167                                                         switch (r.LocalName) {
168                                                         case XmlEncryption.ElementNames.DataReference:
169                                                                 DataReference dr = new DataReference ();
170                                                                 dr.LoadXml ((XmlElement) r);
171                                                                 AddReference (dr);
172                                                                 break;
173                                                         case XmlEncryption.ElementNames.KeyReference:
174                                                                 KeyReference kr = new KeyReference ();
175                                                                 kr.LoadXml ((XmlElement) r);
176                                                                 AddReference (kr);
177                                                                 break;
178                                                         }
179                                                 }
180                                                 break;
181                                         case XmlEncryption.ElementNames.CarriedKeyName:
182                                                 CarriedKeyName = ((XmlElement) n).InnerText;
183                                                 break;
184                                         }
185                                 }
186
187                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
188                                         Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
189                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
190                                         Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
191                                 if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
192                                         MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
193                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
194                                         Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
195                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Recipient))
196                                         Encoding = value.Attributes [XmlEncryption.AttributeNames.Recipient].Value;
197                         }
198                 }
199
200                 #endregion // Methods
201         }
202 }
203
204 #endif