2 // System.Security.Policy.HashMembershipCondition.cs
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.
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
;
40 using Mono
.Security
.Cryptography
;
42 namespace System
.Security
.Policy
{
46 public sealed class HashMembershipCondition
: IMembershipCondition
, IDeserializationCallback
, ISerializable
{
47 private readonly int version
= 1;
49 private HashAlgorithm hash_algorithm
;
50 private byte[] hash_value
;
52 // so System.Activator.CreateInstance can create an instance...
53 internal HashMembershipCondition ()
57 public HashMembershipCondition (HashAlgorithm hashAlg
, byte[] value)
60 throw new ArgumentNullException ("hashAlg");
62 throw new ArgumentNullException ("value");
64 this.hash_algorithm
= hashAlg
;
65 this.hash_value
= (byte[]) value.Clone ();
72 public HashAlgorithm HashAlgorithm
{
74 if (hash_algorithm
== null)
75 hash_algorithm
= new SHA1Managed ();
76 return hash_algorithm
;
80 throw new ArgumentNullException ("HashAlgorithm");
81 hash_algorithm
= value;
85 public byte[] HashValue
{
87 if (hash_value
== null)
88 throw new ArgumentException (Locale
.GetText ("No HashValue available."));
89 return (byte[]) hash_value
.Clone ();
93 throw new ArgumentNullException ("HashValue");
94 hash_value
= (byte[]) value.Clone ();
102 public bool Check (Evidence evidence
)
104 if (evidence
== null)
107 IEnumerator e
= evidence
.GetHostEnumerator ();
108 while (e
.MoveNext ()) {
109 Hash hash
= (e
.Current
as Hash
);
112 if (Compare (hash_value
, hash
.GenerateHash (hash_algorithm
)))
119 public IMembershipCondition
Copy ()
121 return new HashMembershipCondition (hash_algorithm
, hash_value
);
124 public override bool Equals (object o
)
126 HashMembershipCondition other
= (o
as HashMembershipCondition
);
130 return ((other
.HashAlgorithm
== hash_algorithm
) &&
131 Compare (hash_value
, other
.hash_value
));
134 public SecurityElement
ToXml ()
139 public SecurityElement
ToXml (PolicyLevel level
)
141 SecurityElement se
= MembershipConditionHelper
.Element (typeof (HashMembershipCondition
), version
);
142 se
.AddAttribute ("HashValue", CryptoConvert
.ToHex (HashValue
));
143 se
.AddAttribute ("HashAlgorithm", hash_algorithm
.GetType ().FullName
);
147 public void FromXml (SecurityElement e
)
152 public void FromXml (SecurityElement e
, PolicyLevel level
)
154 MembershipConditionHelper
.CheckSecurityElement (e
, "e", version
, version
);
156 hash_value
= CryptoConvert
.FromHex (e
.Attribute ("HashValue"));
158 string algorithm
= e
.Attribute ("HashAlgorithm");
159 hash_algorithm
= (algorithm
== null) ? null : HashAlgorithm
.Create (algorithm
);
162 public override int GetHashCode ()
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
) {
174 public override string ToString ()
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
));
185 private bool Compare (byte[] expected
, byte[] actual
)
187 if (expected
.Length
!= actual
.Length
)
190 int len
= expected
.Length
;
191 for (int i
= 0; i
< len
; i
++) {
192 if (expected
[i
] != actual
[i
])
198 [MonoTODO ("fx 2.0")]
199 void IDeserializationCallback
.OnDeserialization (object sender
)
203 [MonoTODO ("fx 2.0")]
204 void ISerializable
.GetObjectData (SerializationInfo info
, StreamingContext context
)