2004-03-30 Sebastien Pouliot <sebastien@ximian.com>
[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 using System.Collections;
11 using System.Xml;
12
13 namespace System.Security.Cryptography.Xml {
14
15         public class KeyInfo : IEnumerable {
16
17                 private ArrayList Info;
18                 private string id;
19
20                 public KeyInfo() 
21                 {
22                         Info = new ArrayList ();
23                 }
24
25                 public int Count {
26                         get { return Info.Count; }
27                 }
28                 
29                 public string Id {
30                         get { return id; }
31                         set { id = value; }
32                 }
33
34                 public void AddClause (KeyInfoClause clause) 
35                 {
36                         Info.Add (clause);
37                 }
38
39                 public IEnumerator GetEnumerator () 
40                 {
41                         return Info.GetEnumerator ();
42                 }
43
44                 public IEnumerator GetEnumerator (Type requestedObjectType)
45                 {
46                         // Build a new ArrayList...
47                         ArrayList TypeList = new ArrayList ();
48                         IEnumerator e = Info.GetEnumerator ();
49                         while (true) {
50                                 // ...with all object of specified type...
51                                 if ((e.Current).GetType().Equals (requestedObjectType))
52                                         TypeList.Add (e.Current);
53                                 if (!e.MoveNext ())
54                                         break;
55                         }
56                         // ...and return its enumerator
57                         return TypeList.GetEnumerator ();
58                 }
59
60                 public XmlElement GetXml () 
61                 {
62                         XmlDocument document = new XmlDocument ();
63                         XmlElement xel = document.CreateElement (XmlSignature.ElementNames.KeyInfo, XmlSignature.NamespaceURI);
64                         // we add References afterward so we don't end up with extraneous
65                         // xmlns="..." in each reference elements.
66                         foreach (KeyInfoClause kic in Info) {
67                                 XmlNode xn = kic.GetXml ();
68                                 XmlNode newNode = document.ImportNode (xn, true);
69                                 xel.AppendChild (newNode);
70                         }
71                         return xel;
72                 }
73
74                 public void LoadXml (XmlElement value) 
75                 {
76                         if (value == null)
77                                 throw new ArgumentNullException ("value");
78
79                         Id = value.Attributes ["Id"] != null ? value.GetAttribute ("Id") : null;
80
81                         if ((value.LocalName == XmlSignature.ElementNames.KeyInfo) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
82                                 foreach (XmlNode n in value.ChildNodes) {
83                                         if (n.NodeType != XmlNodeType.Element)
84                                                 continue;
85
86                                         KeyInfoClause kic = null;
87
88                                         switch (n.LocalName) {
89                                         case XmlSignature.ElementNames.KeyValue:
90                                                 XmlNodeList xnl = n.ChildNodes;
91                                                 if (xnl.Count > 0) {
92                                                         // we must now treat the whitespace !
93                                                         foreach (XmlNode m in xnl) {
94                                                                 switch (m.LocalName) {
95                                                                 case XmlSignature.ElementNames.DSAKeyValue:
96                                                                         kic = (KeyInfoClause) new DSAKeyValue ();
97                                                                         break;
98                                                                 case XmlSignature.ElementNames.RSAKeyValue:
99                                                                         kic = (KeyInfoClause) new RSAKeyValue ();
100                                                                         break;
101                                                                 }
102                                                         }
103                                                 }
104                                                 break;
105                                         case XmlSignature.ElementNames.KeyName:
106                                                 kic = (KeyInfoClause) new KeyInfoName ();
107                                                 break;
108                                         case XmlSignature.ElementNames.RetrievalMethod:
109                                                 kic = (KeyInfoClause) new KeyInfoRetrievalMethod ();
110                                                 break;
111                                         case XmlSignature.ElementNames.X509Data:
112                                                 kic = (KeyInfoClause) new KeyInfoX509Data ();
113                                                 break;
114                                         case XmlSignature.ElementNames.RSAKeyValue:
115                                                 kic = (KeyInfoClause) new RSAKeyValue ();
116                                                 break;
117                                         default:
118                                                 kic = (KeyInfoClause) new KeyInfoNode ();
119                                                 break;
120                                         }
121
122                                         if (kic != null) {
123                                                 kic.LoadXml ((XmlElement) n);
124                                                 AddClause (kic);
125                                         }
126                                 }
127                         }
128                         // No check is performed on MS.NET...
129                 }
130         }
131 }