2 // System.Security.Policy.HashMembershipCondition
5 // Jackson Harper (Jackson@LatitudeGeo.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2002 Jackson Harper, All rights reserved
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
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;
38 using Mono.Security.Cryptography;
40 namespace System.Security.Policy {
45 public sealed class HashMembershipCondition : IMembershipCondition, IDeserializationCallback, ISerializable {
47 public sealed class HashMembershipCondition : IMembershipCondition {
49 private readonly int version = 1;
51 private HashAlgorithm hash_algorithm;
52 private byte[] hash_value;
54 // so System.Activator.CreateInstance can create an instance...
55 internal HashMembershipCondition ()
59 public HashMembershipCondition (HashAlgorithm hash_algorithm, byte[] hash_value)
61 if (hash_algorithm == null)
62 throw new ArgumentNullException ("hash_algorithm");
63 if (hash_value == null)
64 throw new ArgumentNullException ("hash_value");
66 this.hash_algorithm = hash_algorithm;
67 this.hash_value = (byte[]) hash_value.Clone ();
74 public HashAlgorithm HashAlgorithm {
76 if (hash_algorithm == null)
77 hash_algorithm = new SHA1Managed ();
78 return hash_algorithm;
82 throw new ArgumentNullException ("HashAlgorithm");
83 hash_algorithm = value;
87 public byte[] HashValue {
89 if (hash_value == null)
90 throw new ArgumentException (Locale.GetText ("No HashValue available."));
91 return (byte[]) hash_value.Clone ();
95 throw new ArgumentNullException ("HashValue");
96 hash_value = (byte[]) value.Clone ();
104 public bool Check (Evidence evidence)
106 if (evidence == null)
109 IEnumerator e = evidence.GetHostEnumerator ();
110 while (e.MoveNext ()) {
111 Hash hash = (e.Current as Hash);
114 if (Compare (hash_value, hash.GenerateHash (hash_algorithm)))
121 public IMembershipCondition Copy ()
123 return new HashMembershipCondition (hash_algorithm, hash_value);
126 public override bool Equals (object o)
128 HashMembershipCondition other = (o as HashMembershipCondition);
132 return ((other.HashAlgorithm == hash_algorithm) &&
133 Compare (hash_value, other.hash_value));
136 public SecurityElement ToXml ()
141 public SecurityElement ToXml (PolicyLevel level)
143 SecurityElement se = MembershipConditionHelper.Element (typeof (HashMembershipCondition), version);
144 se.AddAttribute ("HashValue", CryptoConvert.ToHex (HashValue));
145 se.AddAttribute ("HashAlgorithm", hash_algorithm.GetType ().FullName);
149 public void FromXml (SecurityElement element)
151 FromXml (element, null);
154 public void FromXml (SecurityElement e, PolicyLevel level)
156 MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
158 hash_value = CryptoConvert.FromHex (e.Attribute ("HashValue"));
160 string algorithm = e.Attribute ("HashAlgorithm");
161 hash_algorithm = (algorithm == null) ? null : HashAlgorithm.Create (algorithm);
164 public override int GetHashCode ()
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) {
176 public override string ToString ()
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));
187 private bool Compare (byte[] expected, byte[] actual)
189 if (expected.Length != actual.Length)
192 int len = expected.Length;
193 for (int i = 0; i < len; i++) {
194 if (expected [i] != actual [i])
201 [MonoTODO ("fx 2.0")]
202 void IDeserializationCallback.OnDeserialization (object sender)
206 [MonoTODO ("fx 2.0")]
207 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)