2004-08-02 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Policy / HashMembershipCondition.cs
1 //
2 // System.Security.Policy.HashMembershipCondition
3 //
4 // Authors:
5 //      Jackson Harper (Jackson@LatitudeGeo.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2002 Jackson Harper, All rights reserved
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.Globalization;
32 using System.Reflection;
33 using System.Runtime.Serialization;
34 using System.Security.Cryptography;
35 using System.Text;
36
37 namespace System.Security.Policy {
38
39         [Serializable]
40 #if NET_2_0
41         public sealed class HashMembershipCondition : IMembershipCondition, ISecurityEncodable, 
42                 ISecurityPolicyEncodable, IDeserializationCallback, ISerializable {
43 #else
44         public sealed class HashMembershipCondition : IMembershipCondition, ISecurityEncodable, 
45                 ISecurityPolicyEncodable {
46 #endif
47                 private static readonly string XmlTag = "IMembershipCondition";
48
49                 private HashAlgorithm hash_algorithm;
50                 private byte[] hash_value;
51
52                 // so System.Activator.CreateInstance can create an instance...
53                 internal HashMembershipCondition ()
54                 {
55                 }
56
57                 public HashMembershipCondition (HashAlgorithm hash_algorithm, byte[] hash_value)
58                 {
59                         if (hash_algorithm == null)
60                                 throw new ArgumentNullException ("hash_algorithm");
61                         if (hash_value == null)
62                                 throw new ArgumentNullException ("hash_value");
63                                 
64                         this.hash_algorithm = hash_algorithm;
65                         this.hash_value = hash_value;
66                 }
67
68                 //
69                 // Public Properties
70                 //
71                 
72                 public HashAlgorithm HashAlgorithm {
73                         get { return hash_algorithm; }
74                         set { 
75                                 if (value == null)
76                                         throw new ArgumentNullException ();
77                                 hash_algorithm = value; 
78                         }
79                 }
80
81                 public byte[] HashValue {
82                         get { return hash_value; }
83                         set { 
84                                 if (value == null)
85                                         throw new ArgumentNullException ();
86                                 hash_value = value; 
87                         } 
88                 }
89
90                 //
91                 // Public Methods
92                 //
93
94                 public bool Check (Evidence evidence)
95                 {
96                         if (evidence == null)
97                                 throw new ArgumentNullException ("evidence");
98
99                         // Loop through evidence finding the first Hash object
100                         foreach (object obj in evidence) {
101                                 Hash hash = obj as Hash;
102                                 if (hash == null)
103                                         continue;
104                                 if (EqualsHashValue (hash.GenerateHash (hash_algorithm)))
105                                         return true;
106                                 break;
107                         }
108                         return false;
109                 }
110
111                 public IMembershipCondition Copy ()
112                 {
113                         return new HashMembershipCondition (hash_algorithm, hash_value);
114                 }
115
116                 public override bool Equals (object o)
117                 {
118                         HashMembershipCondition other;
119                         if (!(o is HashMembershipCondition))
120                                 return false;
121
122                         other = (HashMembershipCondition)o;
123                         
124                         return (other.HashAlgorithm == hash_algorithm &&
125                                 other.HashValue == hash_value);
126                 }
127                 
128                 public SecurityElement ToXml()
129                 {
130                         return ToXml (null);
131                 }
132
133                 public SecurityElement ToXml (PolicyLevel level)
134                 {
135                         SecurityElement se = new SecurityElement (XmlTag);
136                         Type type = this.GetType ();
137                         string classString = type.FullName + ", " + type.Assembly;
138                         se.AddAttribute ("class", classString);
139                         se.AddAttribute ("version", "1");
140                         se.AddAttribute ("HashValue", Encoding.Default.GetString (hash_value));
141                         se.AddAttribute ("HashAlgorithm", hash_algorithm.GetType ().FullName);
142                         return se;
143                 }
144
145                 public void FromXml (SecurityElement element)
146                 {
147                         FromXml (element, null);
148                 }
149                 
150                 public void FromXml (SecurityElement e, PolicyLevel level)
151                 {
152                         if (e == null)
153                                 throw new ArgumentNullException ("e");
154                         if (e.Tag != XmlTag) {
155                                 throw new ArgumentException ("e", Locale.GetText (
156                                         "The Tag of SecurityElement must be " + XmlTag));
157                         }
158                         
159                         string value = (string)e.Attributes ["HashValue"];
160                         string algorithm = (string)e.Attributes ["HashAlgorithm"];
161
162                         if (value == null || algorithm == null ) {
163                                 throw new ArgumentException ("e", Locale.GetText (
164                                         "Missing either HashValue or HashAlgorithm"));
165                         }
166                         
167                         hash_value = Encoding.Default.GetBytes (value);
168                         hash_algorithm = (HashAlgorithm)Assembly.GetExecutingAssembly ().CreateInstance (algorithm);
169                         
170                 }
171
172                 public override int GetHashCode ()
173                 {
174                         return hash_value.GetHashCode ();
175                 }
176                 
177                 public override string ToString ()
178                 {
179                         StringBuilder builder = new StringBuilder ();
180                         Type alg_type = hash_algorithm.GetType ();
181
182                         builder.Append ("Hash -");
183                         builder.AppendFormat ("{0} {1}", alg_type.FullName, 
184                                 alg_type.Assembly);
185                         builder.AppendFormat (" = ",  Encoding.Default.GetString (hash_value));
186
187                         return builder.ToString ();
188                 }
189
190                 //
191                 // Private Methods
192                 //
193
194                 private bool EqualsHashValue (byte[] value)
195                 {
196                         int len;
197
198                         if (value.Length != hash_value.Length)
199                                 return false;
200                         
201                         len = value.Length;
202                         for (int i=0; i<len; i++ ) {
203                                 if (value[i] != hash_value[i])
204                                         return false;
205                         }
206
207                         return true;
208                 }
209
210 #if NET_2_0
211                 [MonoTODO]
212                 void IDeserializationCallback.OnDeserialization (object sender)
213                 {
214                 }
215
216                 [MonoTODO]
217                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context) 
218                 {
219                 }
220 #endif
221         }
222 }
223