2 // System.Security.Policy.StrongNameMembershipCondition.cs
5 // Duncan Mak (duncan@ximian.com)
7 // (C) 2003 Duncan Mak, Ximian Inc.
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.Globalization;
35 using System.Security.Permissions;
37 namespace System.Security.Policy {
40 public sealed class StrongNameMembershipCondition
41 : IMembershipCondition, ISecurityEncodable, ISecurityPolicyEncodable, IConstantMembershipCondition
43 StrongNamePublicKeyBlob blob;
47 public StrongNameMembershipCondition (StrongNamePublicKeyBlob blob, string name, Version version)
50 throw new ArgumentNullException ("blob");
54 this.version = version;
57 // for PolicyLevel (to avoid validation duplication)
58 internal StrongNameMembershipCondition (SecurityElement e)
63 // so System.Activator.CreateInstance can create an instance...
64 internal StrongNameMembershipCondition ()
77 public Version Version {
79 get { return version; }
81 set { version = value; }
84 public StrongNamePublicKeyBlob PublicKey {
90 throw new ArgumentNullException (
91 Locale.GetText ("The argument is null."));
97 public bool Check (Evidence evidence)
102 foreach (object o in evidence) {
103 if (o is StrongName) {
104 StrongName sn = (o as StrongName);
105 if (sn.PublicKey.Equals (blob) && (sn.Name == name) && (sn.Version.Equals (version)))
112 public IMembershipCondition Copy ()
114 return new StrongNameMembershipCondition (blob, name, version);
117 public override bool Equals (object o)
119 if (o is StrongNameMembershipCondition == false)
122 StrongNameMembershipCondition snmc = (StrongNameMembershipCondition) o;
123 return (snmc.Name == Name && snmc.Version == Version && snmc.PublicKey == PublicKey);
127 public override int GetHashCode ()
129 return blob.GetHashCode ();
132 public void FromXml (SecurityElement e)
137 public void FromXml (SecurityElement e, PolicyLevel level)
140 throw new ArgumentNullException ("e");
142 if (e.Attribute ("class").IndexOf (GetType ().Name) < 0)
143 throw new ArgumentException (Locale.GetText ("Invalid class"));
145 if (e.Attribute ("version") != "1")
146 throw new ArgumentException (Locale.GetText ("Invalid version"));
148 blob = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob"));
149 name = e.Attribute ("Name");
150 string v = (string) e.Attribute ("AssemblyVersion");
152 version = new Version ();
154 version = new Version (v);
157 public override string ToString ()
159 return String.Format ( "Strong Name - {0} name = {1} version {2}",
160 blob, name, version);
163 public SecurityElement ToXml ()
168 public SecurityElement ToXml (PolicyLevel level)
170 SecurityElement element = new SecurityElement ("IMembershipCondition");
171 element.AddAttribute ("class", this.GetType ().AssemblyQualifiedName);
172 element.AddAttribute ("version", "1");
174 element.AddAttribute ("PublicKeyBlob", blob.ToString ());
175 element.AddAttribute ("Name", name);
176 string v = version.ToString ();
178 element.AddAttribute ("AssemblyVersion", version.ToString ());