2 // System.Security.Policy.StrongNameMembershipCondition.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2003 Duncan Mak, Ximian Inc.
9 // Copyright (C) 2004 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.Globalization;
32 using System.Security.Permissions;
34 namespace System.Security.Policy {
37 public sealed class StrongNameMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
39 private readonly int version = 1;
41 private StrongNamePublicKeyBlob blob;
43 private Version assemblyVersion;
45 public StrongNameMembershipCondition (StrongNamePublicKeyBlob blob, string name, Version version)
48 throw new ArgumentNullException ("blob");
52 assemblyVersion = version;
55 // for PolicyLevel (to avoid validation duplication)
56 internal StrongNameMembershipCondition (SecurityElement e)
61 // so System.Activator.CreateInstance can create an instance...
62 internal StrongNameMembershipCondition ()
73 public Version Version {
74 get { return assemblyVersion; }
75 set { assemblyVersion = value; }
78 public StrongNamePublicKeyBlob PublicKey {
82 throw new ArgumentNullException (
83 Locale.GetText ("The argument is null."));
88 public bool Check (Evidence evidence)
93 foreach (object o in evidence) {
94 if (o is StrongName) {
95 StrongName sn = (o as StrongName);
96 /* ??? partial match ??? */
97 if (sn.PublicKey.Equals (blob) && (sn.Name == name) && (sn.Version.Equals (assemblyVersion)))
104 public IMembershipCondition Copy ()
106 return new StrongNameMembershipCondition (blob, name, assemblyVersion);
109 public override bool Equals (object o)
111 if (o is StrongNameMembershipCondition == false)
114 StrongNameMembershipCondition snmc = (StrongNameMembershipCondition) o;
115 return (snmc.Name == Name && snmc.Version == Version && snmc.PublicKey == PublicKey);
119 public override int GetHashCode ()
121 return blob.GetHashCode ();
124 public void FromXml (SecurityElement e)
129 public void FromXml (SecurityElement e, PolicyLevel level)
131 MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
133 blob = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob"));
134 name = e.Attribute ("Name");
135 string v = (string) e.Attribute ("AssemblyVersion");
137 assemblyVersion = new Version ();
139 assemblyVersion = new Version (v);
142 public override string ToString ()
144 // ??? missing informations ???
145 return String.Format ( "Strong Name - {0} name = {1} version {2}",
146 blob, name, assemblyVersion);
149 public SecurityElement ToXml ()
154 public SecurityElement ToXml (PolicyLevel level)
156 // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
157 SecurityElement se = MembershipConditionHelper.Element (typeof (StrongNameMembershipCondition), version);
159 se.AddAttribute ("PublicKeyBlob", blob.ToString ());
160 se.AddAttribute ("Name", name);
161 string v = assemblyVersion.ToString ();
163 se.AddAttribute ("AssemblyVersion", assemblyVersion.ToString ());