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-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.
31 using System.Globalization;
32 using System.Security.Permissions;
33 using System.Runtime.InteropServices;
35 namespace System.Security.Policy {
41 public sealed class StrongNameMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
43 private readonly int version = 1;
45 private StrongNamePublicKeyBlob blob;
47 private Version assemblyVersion;
49 public StrongNameMembershipCondition (StrongNamePublicKeyBlob blob, string name, Version version)
52 throw new ArgumentNullException ("blob");
56 assemblyVersion = version;
59 // for PolicyLevel (to avoid validation duplication)
60 internal StrongNameMembershipCondition (SecurityElement e)
65 // so System.Activator.CreateInstance can create an instance...
66 internal StrongNameMembershipCondition ()
77 public Version Version {
78 get { return assemblyVersion; }
79 set { assemblyVersion = value; }
82 public StrongNamePublicKeyBlob PublicKey {
86 throw new ArgumentNullException (
87 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 /* ??? partial match ??? */
101 if (sn.PublicKey.Equals (blob) && (sn.Name == name) && (sn.Version.Equals (assemblyVersion)))
108 public IMembershipCondition Copy ()
110 return new StrongNameMembershipCondition (blob, name, assemblyVersion);
113 public override bool Equals (object o)
115 if (o is StrongNameMembershipCondition == false)
118 StrongNameMembershipCondition snmc = (StrongNameMembershipCondition) o;
119 return (snmc.Name == Name && snmc.Version == Version && snmc.PublicKey == PublicKey);
123 public override int GetHashCode ()
125 return blob.GetHashCode ();
128 public void FromXml (SecurityElement e)
133 public void FromXml (SecurityElement e, PolicyLevel level)
135 MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
137 blob = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob"));
138 name = e.Attribute ("Name");
139 string v = (string) e.Attribute ("AssemblyVersion");
141 assemblyVersion = new Version ();
143 assemblyVersion = new Version (v);
146 public override string ToString ()
148 // ??? missing informations ???
149 return String.Format ( "Strong Name - {0} name = {1} version {2}",
150 blob, name, assemblyVersion);
153 public SecurityElement ToXml ()
158 public SecurityElement ToXml (PolicyLevel level)
160 // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
161 SecurityElement se = MembershipConditionHelper.Element (typeof (StrongNameMembershipCondition), version);
164 se.AddAttribute ("PublicKeyBlob", blob.ToString ());
166 se.AddAttribute ("Name", name);
167 if (assemblyVersion != null) {
168 string v = assemblyVersion.ToString ();
170 se.AddAttribute ("AssemblyVersion", v);