Merge pull request #2084 from joelmartinez/mdoc-deletetestfix
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / KeyInfo.cs
1 //
2 // KeyInfo.cs - Xml Signature KeyInfo implementation
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
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 using System.Collections;
32 using System.Xml;
33
34 namespace System.Security.Cryptography.Xml {
35
36         public class KeyInfo : IEnumerable {
37
38                 private ArrayList Info;
39                 private string id;
40
41                 public KeyInfo() 
42                 {
43                         Info = new ArrayList ();
44                 }
45
46                 public int Count {
47                         get { return Info.Count; }
48                 }
49                 
50                 public string Id {
51                         get { return id; }
52                         set { id = value; }
53                 }
54
55                 public void AddClause (KeyInfoClause clause) 
56                 {
57                         Info.Add (clause);
58                 }
59
60                 public IEnumerator GetEnumerator () 
61                 {
62                         return Info.GetEnumerator ();
63                 }
64
65                 public IEnumerator GetEnumerator (Type requestedObjectType)
66                 {
67                         // Build a new ArrayList...
68                         ArrayList TypeList = new ArrayList ();
69                         IEnumerator e = Info.GetEnumerator ();
70                         while (true) {
71                                 // ...with all object of specified type...
72                                 if ((e.Current).GetType().Equals (requestedObjectType))
73                                         TypeList.Add (e.Current);
74                                 if (!e.MoveNext ())
75                                         break;
76                         }
77                         // ...and return its enumerator
78                         return TypeList.GetEnumerator ();
79                 }
80
81                 public XmlElement GetXml () 
82                 {
83                         XmlDocument document = new XmlDocument ();
84                         XmlElement xel = document.CreateElement (XmlSignature.ElementNames.KeyInfo, XmlSignature.NamespaceURI);
85                         // we add References afterward so we don't end up with extraneous
86                         // xmlns="..." in each reference elements.
87                         foreach (KeyInfoClause kic in Info) {
88                                 XmlNode xn = kic.GetXml ();
89                                 XmlNode newNode = document.ImportNode (xn, true);
90                                 xel.AppendChild (newNode);
91                         }
92                         return xel;
93                 }
94
95                 public void LoadXml (XmlElement value) 
96                 {
97                         if (value == null)
98                                 throw new ArgumentNullException ("value");
99
100                         Id = value.Attributes ["Id"] != null ? value.GetAttribute ("Id") : null;
101
102                         if ((value.LocalName == XmlSignature.ElementNames.KeyInfo) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
103                                 foreach (XmlNode n in value.ChildNodes) {
104                                         if (n.NodeType != XmlNodeType.Element)
105                                                 continue;
106
107                                         KeyInfoClause kic = null;
108
109                                         switch (n.LocalName) {
110                                         case XmlSignature.ElementNames.KeyValue:
111                                                 XmlNodeList xnl = n.ChildNodes;
112                                                 if (xnl.Count > 0) {
113                                                         // we must now treat the whitespace !
114                                                         foreach (XmlNode m in xnl) {
115                                                                 switch (m.LocalName) {
116                                                                 case XmlSignature.ElementNames.DSAKeyValue:
117                                                                         kic = (KeyInfoClause) new DSAKeyValue ();
118                                                                         break;
119                                                                 case XmlSignature.ElementNames.RSAKeyValue:
120                                                                         kic = (KeyInfoClause) new RSAKeyValue ();
121                                                                         break;
122                                                                 }
123                                                         }
124                                                 }
125                                                 break;
126                                         case XmlSignature.ElementNames.KeyName:
127                                                 kic = (KeyInfoClause) new KeyInfoName ();
128                                                 break;
129                                         case XmlSignature.ElementNames.RetrievalMethod:
130                                                 kic = (KeyInfoClause) new KeyInfoRetrievalMethod ();
131                                                 break;
132                                         case XmlSignature.ElementNames.X509Data:
133                                                 kic = (KeyInfoClause) new KeyInfoX509Data ();
134                                                 break;
135                                         case XmlSignature.ElementNames.RSAKeyValue:
136                                                 kic = (KeyInfoClause) new RSAKeyValue ();
137                                                 break;
138                                         case XmlSignature.ElementNames.EncryptedKey:
139                                                 kic = (KeyInfoClause) new KeyInfoEncryptedKey ();
140                                                 break;
141                                         default:
142                                                 kic = (KeyInfoClause) new KeyInfoNode ();
143                                                 break;
144                                         }
145
146                                         if (kic != null) {
147                                                 kic.LoadXml ((XmlElement) n);
148                                                 AddClause (kic);
149                                         }
150                                 }
151                         }
152                         // No check is performed on MS.NET...
153                 }
154         }
155 }