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)
72 public Version Version {
74 get { return version; }
76 set { version = value; }
79 public StrongNamePublicKeyBlob PublicKey {
85 throw new ArgumentNullException (
86 Locale.GetText ("The argument is null."));
92 public bool Check (Evidence evidence)
97 foreach (object o in evidence) {
98 if (o is StrongName) {
99 StrongName sn = (o as StrongName);
100 if (sn.PublicKey.Equals (blob) && (sn.Name == name) && (sn.Version.Equals (version)))
107 public IMembershipCondition Copy ()
109 return new StrongNameMembershipCondition (blob, name, version);
112 public override bool Equals (object o)
114 if (o is StrongNameMembershipCondition == false)
117 StrongNameMembershipCondition snmc = (StrongNameMembershipCondition) o;
118 return (snmc.Name == Name && snmc.Version == Version && snmc.PublicKey == PublicKey);
122 public override int GetHashCode ()
124 return blob.GetHashCode ();
127 public void FromXml (SecurityElement e)
132 public void FromXml (SecurityElement e, PolicyLevel level)
135 throw new ArgumentNullException ("e");
137 if (e.Attribute ("class").IndexOf (GetType ().Name) < 0)
138 throw new ArgumentException (Locale.GetText ("Invalid class"));
140 if (e.Attribute ("version") != "1")
141 throw new ArgumentException (Locale.GetText ("Invalid version"));
143 blob = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob"));
144 name = e.Attribute ("Name");
145 string v = (string) e.Attribute ("AssemblyVersion");
147 version = new Version ();
149 version = new Version (v);
152 public override string ToString ()
154 return String.Format ( "Strong Name - {0} name = {1} version {2}",
155 blob, name, version);
158 public SecurityElement ToXml ()
163 public SecurityElement ToXml (PolicyLevel level)
165 SecurityElement element = new SecurityElement ("IMembershipCondition");
166 element.AddAttribute ("class", this.GetType ().AssemblyQualifiedName);
167 element.AddAttribute ("version", "1");
169 element.AddAttribute ("PublicKeyBlob", blob.ToString ());
170 element.AddAttribute ("Name", name);
171 string v = version.ToString ();
173 element.AddAttribute ("AssemblyVersion", version.ToString ());