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.Collections;
32 using System.Globalization;
33 using System.Security.Permissions;
34 using System.Runtime.InteropServices;
37 namespace System.Security.Policy {
43 public sealed class StrongNameMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
45 private readonly int version = 1;
47 private StrongNamePublicKeyBlob blob;
49 private Version assemblyVersion;
51 public StrongNameMembershipCondition (StrongNamePublicKeyBlob blob, string name, Version version)
54 throw new ArgumentNullException ("blob");
59 assemblyVersion = (Version) version.Clone ();
62 // for PolicyLevel (to avoid validation duplication)
63 internal StrongNameMembershipCondition (SecurityElement e)
68 // so System.Activator.CreateInstance can create an instance...
69 internal StrongNameMembershipCondition ()
80 public Version Version {
81 get { return assemblyVersion; }
82 set { assemblyVersion = value; }
85 public StrongNamePublicKeyBlob PublicKey {
89 throw new ArgumentNullException ("PublicKey");
94 public bool Check (Evidence evidence)
99 IEnumerator e = evidence.GetHostEnumerator ();
100 while (e.MoveNext ()) {
101 StrongName sn = (e.Current as StrongName);
103 if (!sn.PublicKey.Equals (blob))
105 if ((name != null) && (name != sn.Name))
107 if ((assemblyVersion != null) && !assemblyVersion.Equals (sn.Version))
115 public IMembershipCondition Copy ()
117 return new StrongNameMembershipCondition (blob, name, assemblyVersion);
120 public override bool Equals (object o)
122 StrongNameMembershipCondition snmc = (o as StrongNameMembershipCondition);
125 if (!snmc.PublicKey.Equals (PublicKey))
127 if (name != snmc.Name)
129 if (assemblyVersion != null)
130 return assemblyVersion.Equals (snmc.Version);
131 return (snmc.Version == null);
134 public override int GetHashCode ()
136 // name and version aren't part of the calculation
137 return blob.GetHashCode ();
140 public void FromXml (SecurityElement e)
145 public void FromXml (SecurityElement e, PolicyLevel level)
147 MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
149 blob = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob"));
150 name = e.Attribute ("Name");
151 string v = (string) e.Attribute ("AssemblyVersion");
153 assemblyVersion = null;
155 assemblyVersion = new Version (v);
158 public override string ToString ()
160 StringBuilder sb = new StringBuilder ("StrongName - ");
163 sb.AppendFormat (" name = {0}", name);
164 if (assemblyVersion != null)
165 sb.AppendFormat (" version = {0}", assemblyVersion);
166 return sb.ToString ();
169 public SecurityElement ToXml ()
174 public SecurityElement ToXml (PolicyLevel level)
176 // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
177 SecurityElement se = MembershipConditionHelper.Element (typeof (StrongNameMembershipCondition), version);
180 se.AddAttribute ("PublicKeyBlob", blob.ToString ());
182 se.AddAttribute ("Name", name);
183 if (assemblyVersion != null) {
184 string v = assemblyVersion.ToString ();
186 se.AddAttribute ("AssemblyVersion", v);