New test.
[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-2005 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.Collections;
32 using System.Globalization;
33 using System.Reflection;
34 using System.Runtime.InteropServices;
35 using System.Runtime.Serialization;
36 using System.Security.Cryptography;
37
38 using Mono.Security.Cryptography;
39
40 namespace System.Security.Policy {
41
42         [Serializable]
43 #if NET_2_0
44         [ComVisible (true)]
45         public sealed class HashMembershipCondition : IMembershipCondition, IDeserializationCallback, ISerializable {
46 #else
47         public sealed class HashMembershipCondition : IMembershipCondition {
48 #endif
49                 private readonly int version = 1;
50
51                 private HashAlgorithm hash_algorithm;
52                 private byte[] hash_value;
53
54                 // so System.Activator.CreateInstance can create an instance...
55                 internal HashMembershipCondition ()
56                 {
57                 }
58
59                 public HashMembershipCondition (HashAlgorithm hash_algorithm, byte[] hash_value)
60                 {
61                         if (hash_algorithm == null)
62                                 throw new ArgumentNullException ("hash_algorithm");
63                         if (hash_value == null)
64                                 throw new ArgumentNullException ("hash_value");
65                                 
66                         this.hash_algorithm = hash_algorithm;
67                         this.hash_value = (byte[]) hash_value.Clone ();
68                 }
69
70                 //
71                 // Public Properties
72                 //
73                 
74                 public HashAlgorithm HashAlgorithm {
75                         get {
76                                 if (hash_algorithm == null)
77                                         hash_algorithm = new SHA1Managed ();
78                                 return hash_algorithm;
79                         }
80                         set { 
81                                 if (value == null)
82                                         throw new ArgumentNullException ("HashAlgorithm");
83                                 hash_algorithm = value; 
84                         }
85                 }
86
87                 public byte[] HashValue {
88                         get {
89                                 if (hash_value == null)
90                                         throw new ArgumentException (Locale.GetText ("No HashValue available."));
91                                 return (byte[]) hash_value.Clone ();
92                         }
93                         set { 
94                                 if (value == null)
95                                         throw new ArgumentNullException ("HashValue");
96                                 hash_value = (byte[]) value.Clone ();
97                         } 
98                 }
99
100                 //
101                 // Public Methods
102                 //
103
104                 public bool Check (Evidence evidence)
105                 {
106                         if (evidence == null)
107                                 return false;
108
109                         IEnumerator e = evidence.GetHostEnumerator ();
110                         while (e.MoveNext ()) {
111                                 Hash hash = (e.Current as Hash);
112                                 if (hash == null)
113                                         continue;
114                                 if (Compare (hash_value, hash.GenerateHash (hash_algorithm)))
115                                         return true;
116                                 break;
117                         }
118                         return false;
119                 }
120
121                 public IMembershipCondition Copy ()
122                 {
123                         return new HashMembershipCondition (hash_algorithm, hash_value);
124                 }
125
126                 public override bool Equals (object o)
127                 {
128                         HashMembershipCondition other = (o as HashMembershipCondition);
129                         if (other == null)
130                                 return false;
131
132                         return ((other.HashAlgorithm == hash_algorithm) &&
133                                 Compare (hash_value, other.hash_value));
134                 }
135                 
136                 public SecurityElement ToXml ()
137                 {
138                         return ToXml (null);
139                 }
140
141                 public SecurityElement ToXml (PolicyLevel level)
142                 {
143                         SecurityElement se = MembershipConditionHelper.Element (typeof (HashMembershipCondition), version);
144                         se.AddAttribute ("HashValue", CryptoConvert.ToHex (HashValue));
145                         se.AddAttribute ("HashAlgorithm", hash_algorithm.GetType ().FullName);
146                         return se;
147                 }
148
149                 public void FromXml (SecurityElement element)
150                 {
151                         FromXml (element, null);
152                 }
153                 
154                 public void FromXml (SecurityElement e, PolicyLevel level)
155                 {
156                         MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
157                         
158                         hash_value = CryptoConvert.FromHex (e.Attribute ("HashValue"));
159
160                         string algorithm = e.Attribute ("HashAlgorithm");
161                         hash_algorithm = (algorithm == null) ? null : HashAlgorithm.Create (algorithm);
162                 }
163
164                 public override int GetHashCode ()
165                 {
166                         // note: a Copy must have the same hash code
167                         int code = hash_algorithm.GetType ().GetHashCode ();
168                         if (hash_value != null) {
169                                 foreach (byte b in hash_value) {
170                                         code ^= b;
171                                 }
172                         }
173                         return code;
174                 }
175                 
176                 public override string ToString ()
177                 {
178                         Type alg_type = this.HashAlgorithm.GetType ();
179                         return String.Format ("Hash - {0} {1} = {2}", alg_type.FullName, 
180                                 alg_type.Assembly, CryptoConvert.ToHex (HashValue));
181                 }
182
183                 //
184                 // Private Methods
185                 //
186
187                 private bool Compare (byte[] expected, byte[] actual)
188                 {
189                         if (expected.Length != actual.Length)
190                                 return false;
191                         
192                         int len = expected.Length;
193                         for (int i = 0; i < len; i++) {
194                                 if (expected [i] != actual [i])
195                                         return false;
196                         }
197                         return true;
198                 }
199
200 #if NET_2_0
201                 [MonoTODO ("fx 2.0")]
202                 void IDeserializationCallback.OnDeserialization (object sender)
203                 {
204                 }
205
206                 [MonoTODO ("fx 2.0")]
207                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context) 
208                 {
209                 }
210 #endif
211         }
212 }
213