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