2 // System.Security.Policy.HashMembershipCondition
5 // Jackson Harper (Jackson@LatitudeGeo.com)
7 // (C) 2002 Jackson Harper, All rights reserved
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Reflection;
35 using System.Security.Cryptography;
37 namespace System.Security.Policy {
40 public sealed class HashMembershipCondition : IMembershipCondition,
41 ISecurityEncodable, ISecurityPolicyEncodable {
43 private static readonly string XmlTag = "IMembershipCondition";
45 private HashAlgorithm hash_algorithm;
46 private byte[] hash_value;
48 public HashMembershipCondition (HashAlgorithm hash_algorithm,
51 if (hash_algorithm == null || hash_value == null)
52 throw new ArgumentNullException ();
54 this.hash_algorithm = hash_algorithm;
55 this.hash_value = hash_value;
62 public HashAlgorithm HashAlgorithm {
63 get { return hash_algorithm; }
66 throw new ArgumentNullException ();
67 hash_algorithm = value;
71 public byte[] HashValue {
72 get { return hash_value; }
75 throw new ArgumentNullException ();
84 public bool Check (Evidence evidence)
87 throw new ArgumentNullException ();
89 // Loop through evidence finding the first Hash object
90 foreach (object obj in evidence) {
91 Hash hash = obj as Hash;
94 if (EqualsHashValue (hash.GenerateHash (hash_algorithm)))
101 public IMembershipCondition Copy ()
103 return new HashMembershipCondition (hash_algorithm, hash_value);
106 public override bool Equals (object o)
108 HashMembershipCondition other;
109 if (!(o is HashMembershipCondition))
112 other = (HashMembershipCondition)o;
114 return (other.HashAlgorithm == hash_algorithm &&
115 other.HashValue == hash_value);
118 public SecurityElement ToXml()
123 public SecurityElement ToXml (PolicyLevel level)
125 SecurityElement se = new SecurityElement (XmlTag);
126 Type type = this.GetType ();
127 string classString = type.FullName + ", " + type.Assembly;
128 se.AddAttribute ("class", classString);
129 se.AddAttribute ("version", "1");
130 se.AddAttribute ("HashValue", Encoding.Default.GetString (hash_value));
131 se.AddAttribute ("HashAlgorithm", hash_algorithm.GetType ().FullName);
135 public void FromXml (SecurityElement element)
137 FromXml (element, null);
140 public void FromXml (SecurityElement e,
144 throw new ArgumentNullException ();
146 throw new ArgumentException(
147 "e","The Tag of SecurityElement must be " + XmlTag);
149 string value = (string)e.Attributes["HashValue"];
150 string algorithm = (string)e.Attributes["HashAlgorithm"];
152 if (value == null || algorithm == null )
153 throw new ArgumentException ();
155 hash_value = Encoding.Default.GetBytes (value);
156 hash_algorithm = (HashAlgorithm)Assembly.GetExecutingAssembly ().CreateInstance (algorithm);
160 [MonoTODO("This is not right")]
161 public override int GetHashCode ()
163 return hash_value.GetHashCode ();
166 public override string ToString ()
168 StringBuilder builder = new StringBuilder ();
169 Type alg_type = hash_algorithm.GetType ();
171 builder.Append ("Hash -");
172 builder.AppendFormat ("{0} {1}", alg_type.FullName,
174 builder.AppendFormat (" = ", Encoding.Default.GetString (hash_value));
176 return builder.ToString ();
183 private bool EqualsHashValue (byte[] value)
187 if (value.Length != hash_value.Length)
191 for (int i=0; i<len; i++ ) {
192 if (value[i] != hash_value[i])